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",
43 * get uint16 value from source
46 * - request : Describes the request by bindings from afb-daemon
47 * - source : input source
48 * - out_id : output uint16 value
54 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
57 const char* tmp = afb_req_value (request, source);
62 long tmp_id = strtol(tmp,&endptr,10);
64 /* error check of range */
65 if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
74 *out_id = (uint16_t)tmp_id;
79 * get int16 value from source
82 * - request : Describes the request by bindings from afb-daemon
83 * - source : input source
84 * - out_id : output int16 value
90 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
93 const char* tmp = afb_req_value (request, source);
98 long tmp_id = strtol(tmp,&endptr,10);
100 /* error check of range */
101 if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
110 *out_id = (int16_t)tmp_id;
115 * get int32 value from source
118 * - request : Describes the request by bindings from afb-daemon
119 * - source : input source
120 * - out_id : output int32 value
126 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
129 const char* tmp = afb_req_value (request, source);
134 long tmp_id = strtol(tmp,&endptr,10);
136 /* error check of range */
137 if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
146 *out_id = (int32_t)tmp_id;
151 * add int object to json object
154 * - j_obj : the json object will join in int json object
155 * - count : input parameter number
156 * - ... : parameter list
162 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
165 va_start(args, count);
166 for(int i = 0; i < count; ++i )
168 char *key = va_arg(args, char*);
169 int value = va_arg(args, int);
170 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
177 * add string object to json object
180 * - j_obj : the json object will join in string json object
181 * - count : input parameter number
182 * - ... : parameter list
188 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
191 va_start(args, count);
192 for(int i = 0; i < count; ++i )
194 char *key = va_arg(args, char*);
195 char *value = va_arg(args, char*);
196 json_object_object_add(j_obj, key, json_object_new_string(value));
203 * add new json object to json object
206 * - j_obj : the json object will join in new json object
207 * - verb_name : new json object's verb value
208 * - count : input parameter number
209 * - ... : parameter list
215 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
218 va_start(args, count);
220 json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
222 for(int i = 0; i < count; ++i )
224 char *key = va_arg(args, char*);
225 int value = va_arg(args, int);
226 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
233 * search event position in event list
236 * - value : searched event name
239 * event's index in event list
242 int hs_search_event_name_index(const char* value)
244 size_t buf_size = 50;
245 size_t size = sizeof evlist / sizeof *evlist;
247 for(size_t i = 0 ; i < size ; ++i)
249 if(!strncmp(value, evlist[i], buf_size))
259 * get application id from request
262 * - request : the request
268 std::string get_application_id(const afb_req_t request)
271 char *app_id = afb_req_get_application_id(request);
272 if(app_id == nullptr) {
273 appid = std::string("");
276 appid = std::string(app_id);
288 * - obj : json_object
295 int readJsonFile(const char* file, struct json_object **obj)
298 FILE *fp = fopen(file, "rb");
300 AFB_ERROR("open %s failed", file);
305 const int buf_size = 128;
307 struct json_tokener *tokener = json_tokener_new();
308 enum json_tokener_error json_error;
310 size_t len = fread(buf, sizeof(char), buf_size, fp);
311 *obj = json_tokener_parse_ex(tokener, buf, len);
312 if(nullptr != *obj) {
313 AFB_NOTICE("read %s success", file);
318 json_error = json_tokener_get_error(tokener);
319 if ((json_tokener_continue != json_error) || (buf_size > len)) {
320 AFB_ERROR("parse %s error", file);
327 json_tokener_free(tokener);
335 * - file : output file name
336 * - obj : json_object
343 int writeJsonFile(const char* file, struct json_object *obj)
346 FILE *fp = fopen(file, "w+");
348 AFB_ERROR("open %s failed", file);
352 const char *str = json_object_to_json_string(obj);
353 size_t len = strlen(str);
354 size_t cnt = fwrite(str, len, 1, fp);
361 AFB_WARNING("write to %s failed.cnt=%d,len=%d,str=%s", file, cnt, len, str);