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",
39 * get uint16 value from source
42 * - request : Describes the request by bindings from afb-daemon
43 * - source : input source
44 * - out_id : output uint16 value
50 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
53 const char* tmp = afb_req_value (request, source);
58 long tmp_id = strtol(tmp,&endptr,10);
60 /* error check of range */
61 if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
70 *out_id = (uint16_t)tmp_id;
75 * get int16 value from source
78 * - request : Describes the request by bindings from afb-daemon
79 * - source : input source
80 * - out_id : output int16 value
86 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
89 const char* tmp = afb_req_value (request, source);
94 long tmp_id = strtol(tmp,&endptr,10);
96 /* error check of range */
97 if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
106 *out_id = (int16_t)tmp_id;
111 * get int32 value from source
114 * - request : Describes the request by bindings from afb-daemon
115 * - source : input source
116 * - out_id : output int32 value
122 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
125 const char* tmp = afb_req_value (request, source);
130 long tmp_id = strtol(tmp,&endptr,10);
132 /* error check of range */
133 if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
142 *out_id = (int32_t)tmp_id;
147 * add int object to json object
150 * - j_obj : the json object will join in int json object
151 * - count : input parameter number
152 * - ... : parameter list
158 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
161 va_start(args, count);
162 for(int i = 0; i < count; ++i )
164 char *key = va_arg(args, char*);
165 int value = va_arg(args, int);
166 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
173 * add string object to json object
176 * - j_obj : the json object will join in string json object
177 * - count : input parameter number
178 * - ... : parameter list
184 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
187 va_start(args, count);
188 for(int i = 0; i < count; ++i )
190 char *key = va_arg(args, char*);
191 char *value = va_arg(args, char*);
192 json_object_object_add(j_obj, key, json_object_new_string(value));
199 * add new json object to json object
202 * - j_obj : the json object will join in new json object
203 * - verb_name : new json object's verb value
204 * - count : input parameter number
205 * - ... : parameter list
211 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
214 va_start(args, count);
216 json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
218 for(int i = 0; i < count; ++i )
220 char *key = va_arg(args, char*);
221 int value = va_arg(args, int);
222 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
229 * search event position in event list
232 * - value : searched event name
235 * event's index in event list
238 int hs_search_event_name_index(const char* value)
240 size_t buf_size = 50;
241 size_t size = sizeof evlist / sizeof *evlist;
243 for(size_t i = 0 ; i < size ; ++i)
245 if(!strncmp(value, evlist[i], buf_size))
255 * get application id from request
258 * - request : the request
264 std::string get_application_id(const afb_req_t request)
267 char *app_id = afb_req_get_application_id(request);
268 if(app_id == nullptr) {
269 appid = std::string("");
272 appid = std::string(app_id);
284 * - obj : json_object
291 int readJsonFile(const char* file, struct json_object **obj)
294 FILE *fp = fopen(file, "rb");
296 AFB_ERROR("open %s failed", file);
301 const int buf_size = 128;
303 struct json_tokener *tokener = json_tokener_new();
304 enum json_tokener_error json_error;
306 size_t len = fread(buf, sizeof(char), buf_size, fp);
307 *obj = json_tokener_parse_ex(tokener, buf, len);
308 if(nullptr != *obj) {
309 AFB_NOTICE("read %s success", file);
314 json_error = json_tokener_get_error(tokener);
315 if ((json_tokener_continue != json_error) || (buf_size > len)) {
316 AFB_ERROR("parse %s error", file);
323 json_tokener_free(tokener);
331 * - file : output file name
332 * - obj : json_object
339 int writeJsonFile(const char* file, struct json_object *obj)
342 FILE *fp = fopen(file, "w+");
344 AFB_ERROR("open %s failed", file);
348 const char *str = json_object_to_json_string(obj);
349 size_t len = strlen(str);
350 size_t cnt = fwrite(str, len, 1, fp);
357 AFB_WARNING("write to %s failed.cnt=%d,len=%d,str=%s", file, cnt, len, str);