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[] = {
35 * get uint16 value from source
38 * - request : Describes the request by bindings from afb-daemon
39 * - source : input source
40 * - out_id : output uint16 value
46 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
49 const char* tmp = afb_req_value (request, source);
54 long tmp_id = strtol(tmp,&endptr,10);
56 /* error check of range */
57 if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
66 *out_id = (uint16_t)tmp_id;
71 * get int16 value from source
74 * - request : Describes the request by bindings from afb-daemon
75 * - source : input source
76 * - out_id : output int16 value
82 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
85 const char* tmp = afb_req_value (request, source);
90 long tmp_id = strtol(tmp,&endptr,10);
92 /* error check of range */
93 if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
102 *out_id = (int16_t)tmp_id;
107 * get int32 value from source
110 * - request : Describes the request by bindings from afb-daemon
111 * - source : input source
112 * - out_id : output int32 value
118 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
121 const char* tmp = afb_req_value (request, source);
126 long tmp_id = strtol(tmp,&endptr,10);
128 /* error check of range */
129 if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
138 *out_id = (int32_t)tmp_id;
143 * add int object to json object
146 * - j_obj : the json object will join in int json object
147 * - count : input parameter number
148 * - ... : parameter list
154 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
157 va_start(args, count);
158 for(int i = 0; i < count; ++i )
160 char *key = va_arg(args, char*);
161 int value = va_arg(args, int);
162 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
169 * add string object to json object
172 * - j_obj : the json object will join in string json object
173 * - count : input parameter number
174 * - ... : parameter list
180 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
183 va_start(args, count);
184 for(int i = 0; i < count; ++i )
186 char *key = va_arg(args, char*);
187 char *value = va_arg(args, char*);
188 json_object_object_add(j_obj, key, json_object_new_string(value));
195 * add new json object to json object
198 * - j_obj : the json object will join in new json object
199 * - verb_name : new json object's verb value
200 * - count : input parameter number
201 * - ... : parameter list
207 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
210 va_start(args, count);
212 json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
214 for(int i = 0; i < count; ++i )
216 char *key = va_arg(args, char*);
217 int value = va_arg(args, int);
218 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
225 * search event position in event list
228 * - value : searched event name
231 * event's index in event list
234 int hs_search_event_name_index(const char* value)
236 size_t buf_size = 50;
237 size_t size = sizeof evlist / sizeof *evlist;
239 for(size_t i = 0 ; i < size ; ++i)
241 if(!strncmp(value, evlist[i], buf_size))
251 * get application id from request
254 * - request : the request
260 std::string get_application_id(const afb_req_t request)
263 char *app_id = afb_req_get_application_id(request);
264 if(app_id == nullptr) {
265 appid = std::string("");
268 appid = std::string(app_id);