don't access vhsl-capbilities when it isn't existing
[apps/agl-service-homescreen.git] / src / hs-helper.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <string.h>
18 #include <cstdarg>
19 #include "hs-helper.h"
20 #include <unistd.h>
21
22
23 const char* evlist[] = {
24     "tap_shortcut",
25     "on_screen_message",
26     "on_screen_reply",
27     "showWindow",
28     "hideWindow",
29     "replyShowWindow",
30     "showNotification",
31     "showInformation",
32     "application-list-changed",
33     "registerShortcut",
34     "updateShortcut",
35     "setDestination",
36     "cancelDestination",
37     "startNavigation",
38     "stopNavigation",
39     "reserved"
40   };
41
42 /**
43  * get uint16 value from source
44  *
45  * #### Parameters
46  * - request : Describes the request by bindings from afb-daemon
47  * - source  : input source
48  * - out_id  : output uint16 value
49  *
50  * #### Return
51  * error code
52  *
53  */
54 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
55 {
56     char* endptr;
57     const char* tmp = afb_req_value (request, source);
58     if(!tmp)
59     {
60         return REQ_FAIL;
61     }
62     long tmp_id = strtol(tmp,&endptr,10);
63
64     /* error check of range */
65     if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
66     {
67         return OUT_RANGE;
68     }
69     if(*endptr != '\0')
70     {
71         return NOT_NUMBER;
72     }
73
74     *out_id = (uint16_t)tmp_id;
75     return REQ_OK;
76 }
77
78 /**
79  * get int16 value from source
80  *
81  * #### Parameters
82  * - request : Describes the request by bindings from afb-daemon
83  * - source  : input source
84  * - out_id  : output int16 value
85  *
86  * #### Return
87  * error code
88  *
89  */
90 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
91 {
92     char* endptr;
93     const char* tmp = afb_req_value (request, source);
94     if(!tmp)
95     {
96         return REQ_FAIL;
97     }
98     long tmp_id = strtol(tmp,&endptr,10);
99
100     /* error check of range */
101     if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
102     {
103         return OUT_RANGE;
104     }
105     if(*endptr != '\0')
106     {
107         return NOT_NUMBER;
108     }
109
110     *out_id = (int16_t)tmp_id;
111     return REQ_OK;
112 }
113
114 /**
115  * get int32 value from source
116  *
117  * #### Parameters
118  * - request : Describes the request by bindings from afb-daemon
119  * - source  : input source
120  * - out_id  : output int32 value
121  *
122  * #### Return
123  * error code
124  *
125  */
126 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
127 {
128     char* endptr;
129     const char* tmp = afb_req_value (request, source);
130     if(!tmp)
131     {
132         return REQ_FAIL;
133     }
134     long tmp_id = strtol(tmp,&endptr,10);
135
136     /* error check of range */
137     if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
138     {
139         return OUT_RANGE;
140     }
141     if(*endptr != '\0')
142     {
143         return NOT_NUMBER;
144     }
145
146     *out_id = (int32_t)tmp_id;
147     return REQ_OK;
148 }
149
150 /**
151  * add int object to json object
152  *
153  * #### Parameters
154  * - j_obj : the json object will join in int json object
155  * - count : input parameter number
156  * - ...   : parameter list
157  *
158  * #### Return
159  * None
160  *
161  */
162 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
163 {
164     va_list args;
165     va_start(args, count);
166     for(int i = 0; i < count; ++i )
167     {
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));
171         ++i;
172     }
173     va_end(args);
174 }
175
176 /**
177  * add string object to json object
178  *
179  * #### Parameters
180  * - j_obj : the json object will join in string json object
181  * - count : input parameter number
182  * - ...   : parameter list
183  *
184  * #### Return
185  * None
186  *
187  */
188 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
189 {
190     va_list args;
191     va_start(args, count);
192     for(int i = 0; i < count; ++i )
193     {
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));
197         ++i;
198     }
199     va_end(args);
200 }
201
202 /**
203  * add new json object to json object
204  *
205  * #### Parameters
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
210  *
211  * #### Return
212  * None
213  *
214  */
215 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
216 {
217     va_list args;
218     va_start(args, count);
219
220     json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
221
222     for(int i = 0; i < count; ++i )
223     {
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));
227         ++i;
228     }
229     va_end(args);
230 }
231
232 /**
233  * search event position in event list
234  *
235  * #### Parameters
236  * - value : searched event name
237  *
238  * #### Return
239  * event's index in event list
240  *
241  */
242 int hs_search_event_name_index(const char* value)
243 {
244     size_t buf_size = 50;
245     size_t size = sizeof evlist / sizeof *evlist;
246     int ret = -1;
247     for(size_t i = 0 ; i < size ; ++i)
248     {
249         if(!strncmp(value, evlist[i], buf_size))
250         {
251             ret = i;
252             break;
253         }
254     }
255     return ret;
256 }
257
258 /**
259  * get application id from request
260  *
261  * #### Parameters
262  * - request : the request
263  *
264  * #### Return
265  * got application id
266  *
267  */
268 std::string get_application_id(const afb_req_t request)
269 {
270     std::string appid;
271     char *app_id = afb_req_get_application_id(request);
272     if(app_id == nullptr) {
273         appid = std::string("");
274     }
275     else {
276         appid = std::string(app_id);
277         free(app_id);
278     }
279
280     return appid;
281 }
282
283 /**
284  * read json file
285  *
286  * #### Parameters
287  * - file : file name
288  * - obj : json_object
289  *
290  * #### Return
291  * 0 : read success
292  * -1 : read fail
293  *
294  */
295 int readJsonFile(const char* file, struct json_object **obj)
296 {
297     int ret = -1;
298     FILE *fp = fopen(file, "rb");
299     if(fp == nullptr) {
300         AFB_ERROR("open %s failed", file);
301         return ret;
302     }
303
304     *obj = nullptr;
305     const int buf_size = 128;
306     char buf[buf_size];
307     struct json_tokener *tokener = json_tokener_new();
308     enum json_tokener_error json_error;
309     while(1) {
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);
314             ret = 0;
315             break;
316         }
317
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);
321             *obj = nullptr;
322             break;
323         }
324         }
325
326         fclose(fp);
327         json_tokener_free(tokener);
328         return ret;
329 }
330
331 /**
332  * write to json file
333  *
334  * #### Parameters
335  * - file : output file name
336  * - obj : json_object
337  *
338  * #### Return
339  * 0 : write success
340  * -1 : write fail
341  *
342  */
343 int writeJsonFile(const char* file,  struct json_object *obj)
344 {
345     int ret = -1;
346     FILE *fp = fopen(file, "w+");
347     if(fp == nullptr) {
348         AFB_ERROR("open %s failed", file);
349         return ret;
350     }
351
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);
355     if(cnt == 1) {
356         ret = 0;
357         fflush(fp);
358         fsync(fileno(fp));
359     }
360     else {
361         AFB_WARNING("write to %s failed.cnt=%d,len=%d,str=%s", file, cnt, len, str);
362     }
363
364     fclose(fp);
365     return ret;
366 }