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"
23 const char* evlist[] = {
32 "application-list-changed",
37 * get uint16 value from source
40 * - request : Describes the request by bindings from afb-daemon
41 * - source : input source
42 * - out_id : output uint16 value
48 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
51 const char* tmp = afb_req_value (request, source);
56 long tmp_id = strtol(tmp,&endptr,10);
58 /* error check of range */
59 if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
68 *out_id = (uint16_t)tmp_id;
73 * get int16 value from source
76 * - request : Describes the request by bindings from afb-daemon
77 * - source : input source
78 * - out_id : output int16 value
84 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
87 const char* tmp = afb_req_value (request, source);
92 long tmp_id = strtol(tmp,&endptr,10);
94 /* error check of range */
95 if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
104 *out_id = (int16_t)tmp_id;
109 * get int32 value from source
112 * - request : Describes the request by bindings from afb-daemon
113 * - source : input source
114 * - out_id : output int32 value
120 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
123 const char* tmp = afb_req_value (request, source);
128 long tmp_id = strtol(tmp,&endptr,10);
130 /* error check of range */
131 if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
140 *out_id = (int32_t)tmp_id;
145 * add int object to json object
148 * - j_obj : the json object will join in int json object
149 * - count : input parameter number
150 * - ... : parameter list
156 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
159 va_start(args, count);
160 for(int i = 0; i < count; ++i )
162 char *key = va_arg(args, char*);
163 int value = va_arg(args, int);
164 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
171 * add string object to json object
174 * - j_obj : the json object will join in string json object
175 * - count : input parameter number
176 * - ... : parameter list
182 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
185 va_start(args, count);
186 for(int i = 0; i < count; ++i )
188 char *key = va_arg(args, char*);
189 char *value = va_arg(args, char*);
190 json_object_object_add(j_obj, key, json_object_new_string(value));
197 * add new json object to json object
200 * - j_obj : the json object will join in new json object
201 * - verb_name : new json object's verb value
202 * - count : input parameter number
203 * - ... : parameter list
209 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
212 va_start(args, count);
214 json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
216 for(int i = 0; i < count; ++i )
218 char *key = va_arg(args, char*);
219 int value = va_arg(args, int);
220 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
227 * search event position in event list
230 * - value : searched event name
233 * event's index in event list
236 int hs_search_event_name_index(const char* value)
238 size_t buf_size = 50;
239 size_t size = sizeof evlist / sizeof *evlist;
241 for(size_t i = 0 ; i < size ; ++i)
243 if(!strncmp(value, evlist[i], buf_size))
253 * get application id from request
256 * - request : the request
262 std::string get_application_id(const afb_req_t request)
265 char *app_id = afb_req_get_application_id(request);
266 if(app_id == nullptr) {
267 appid = std::string("");
270 appid = std::string(app_id);
282 * - obj : json_object
289 int readJsonFile(const char* file, struct json_object **obj)
293 FILE *fp = fopen(file, "rb");
295 AFB_ERROR("open %s failed", file);
299 const int buf_size = 128;
301 struct json_tokener *tokener = json_tokener_new();
302 enum json_tokener_error json_error;
304 size_t len = fread(buf, sizeof(char), buf_size, fp);
305 *obj = json_tokener_parse_ex(tokener, buf, len);
306 if(nullptr != *obj) {
307 AFB_NOTICE("read %s success", file);
312 json_error = json_tokener_get_error(tokener);
313 if ((json_tokener_continue != json_error) || (buf_size > len)) {
314 AFB_ERROR("parse %s error", file);
321 json_tokener_free(tokener);