2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "hs-helper.h"
22 const char* evlist[] = {
31 "application-list-changed",
36 * get uint16 value from source
39 * - request : Describes the request by bindings from afb-daemon
40 * - source : input source
41 * - out_id : output uint16 value
47 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
50 const char* tmp = afb_req_value (request, source);
55 long tmp_id = strtol(tmp,&endptr,10);
57 /* error check of range */
58 if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
67 *out_id = (uint16_t)tmp_id;
72 * get int16 value from source
75 * - request : Describes the request by bindings from afb-daemon
76 * - source : input source
77 * - out_id : output int16 value
83 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
86 const char* tmp = afb_req_value (request, source);
91 long tmp_id = strtol(tmp,&endptr,10);
93 /* error check of range */
94 if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
103 *out_id = (int16_t)tmp_id;
108 * get int32 value from source
111 * - request : Describes the request by bindings from afb-daemon
112 * - source : input source
113 * - out_id : output int32 value
119 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
122 const char* tmp = afb_req_value (request, source);
127 long tmp_id = strtol(tmp,&endptr,10);
129 /* error check of range */
130 if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
139 *out_id = (int32_t)tmp_id;
144 * add int object to json object
147 * - j_obj : the json object will join in int json object
148 * - count : input parameter number
149 * - ... : parameter list
155 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
158 va_start(args, count);
159 for(int i = 0; i < count; ++i )
161 char *key = va_arg(args, char*);
162 int value = va_arg(args, int);
163 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
170 * add string object to json object
173 * - j_obj : the json object will join in string json object
174 * - count : input parameter number
175 * - ... : parameter list
181 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
184 va_start(args, count);
185 for(int i = 0; i < count; ++i )
187 char *key = va_arg(args, char*);
188 char *value = va_arg(args, char*);
189 json_object_object_add(j_obj, key, json_object_new_string(value));
196 * add new json object to json object
199 * - j_obj : the json object will join in new json object
200 * - verb_name : new json object's verb value
201 * - count : input parameter number
202 * - ... : parameter list
208 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
211 va_start(args, count);
213 json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
215 for(int i = 0; i < count; ++i )
217 char *key = va_arg(args, char*);
218 int value = va_arg(args, int);
219 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
226 * search event position in event list
229 * - value : searched event name
232 * event's index in event list
235 int hs_search_event_name_index(const char* value)
237 size_t buf_size = 50;
238 size_t size = sizeof evlist / sizeof *evlist;
240 for(size_t i = 0 ; i < size ; ++i)
242 if(!strncmp(value, evlist[i], buf_size))
252 * get application id from request
255 * - request : the request
261 std::string get_application_id(const afb_req_t request)
264 char *app_id = afb_req_get_application_id(request);
265 if(app_id == nullptr) {
266 appid = std::string("");
269 appid = std::string(app_id);