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[] = {
30 * get uint16 value from source
33 * - request : Describes the request by bindings from afb-daemon
34 * - source : input source
35 * - out_id : output uint16 value
41 REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id)
44 const char* tmp = afb_req_value (request, source);
49 long tmp_id = strtol(tmp,&endptr,10);
51 /* error check of range */
52 if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
61 *out_id = (uint16_t)tmp_id;
66 * get int16 value from source
69 * - request : Describes the request by bindings from afb-daemon
70 * - source : input source
71 * - out_id : output int16 value
77 REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id)
80 const char* tmp = afb_req_value (request, source);
85 long tmp_id = strtol(tmp,&endptr,10);
87 /* error check of range */
88 if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
97 *out_id = (int16_t)tmp_id;
102 * get int32 value from source
105 * - request : Describes the request by bindings from afb-daemon
106 * - source : input source
107 * - out_id : output int32 value
113 REQ_ERROR get_value_int32(const struct afb_req request, const char *source, int32_t *out_id)
116 const char* tmp = afb_req_value (request, source);
121 long tmp_id = strtol(tmp,&endptr,10);
123 /* error check of range */
124 if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
133 *out_id = (int32_t)tmp_id;
138 * add int object to json object
141 * - j_obj : the json object will join in int json object
142 * - count : input parameter number
143 * - ... : parameter list
149 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
152 va_start(args, count);
153 for(int i = 0; i < count; ++i )
155 char *key = va_arg(args, char*);
156 int value = va_arg(args, int);
157 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
164 * add string object to json object
167 * - j_obj : the json object will join in string json object
168 * - count : input parameter number
169 * - ... : parameter list
175 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
178 va_start(args, count);
179 for(int i = 0; i < count; ++i )
181 char *key = va_arg(args, char*);
182 char *value = va_arg(args, char*);
183 json_object_object_add(j_obj, key, json_object_new_string(value));
190 * add new json object to json object
193 * - j_obj : the json object will join in new json object
194 * - verb_name : new json object's verb value
195 * - count : input parameter number
196 * - ... : parameter list
202 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
205 va_start(args, count);
207 json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
209 for(int i = 0; i < count; ++i )
211 char *key = va_arg(args, char*);
212 int value = va_arg(args, int);
213 json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
220 * search event position in event list
223 * - value : searched event name
226 * event's index in event list
229 int hs_search_event_name_index(const char* value)
231 size_t buf_size = 50;
232 size_t size = sizeof evlist / sizeof *evlist;
234 for(size_t i = 0 ; i < size ; ++i)
236 if(!strncmp(value, evlist[i], buf_size))