780b7d48fd64c1876f3b39a1bce9970b3ccded62
[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     "reserved"
36   };
37
38 /**
39  * get uint16 value from source
40  *
41  * #### Parameters
42  * - request : Describes the request by bindings from afb-daemon
43  * - source  : input source
44  * - out_id  : output uint16 value
45  *
46  * #### Return
47  * error code
48  *
49  */
50 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
51 {
52     char* endptr;
53     const char* tmp = afb_req_value (request, source);
54     if(!tmp)
55     {
56         return REQ_FAIL;
57     }
58     long tmp_id = strtol(tmp,&endptr,10);
59
60     /* error check of range */
61     if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
62     {
63         return OUT_RANGE;
64     }
65     if(*endptr != '\0')
66     {
67         return NOT_NUMBER;
68     }
69
70     *out_id = (uint16_t)tmp_id;
71     return REQ_OK;
72 }
73
74 /**
75  * get int16 value from source
76  *
77  * #### Parameters
78  * - request : Describes the request by bindings from afb-daemon
79  * - source  : input source
80  * - out_id  : output int16 value
81  *
82  * #### Return
83  * error code
84  *
85  */
86 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
87 {
88     char* endptr;
89     const char* tmp = afb_req_value (request, source);
90     if(!tmp)
91     {
92         return REQ_FAIL;
93     }
94     long tmp_id = strtol(tmp,&endptr,10);
95
96     /* error check of range */
97     if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
98     {
99         return OUT_RANGE;
100     }
101     if(*endptr != '\0')
102     {
103         return NOT_NUMBER;
104     }
105
106     *out_id = (int16_t)tmp_id;
107     return REQ_OK;
108 }
109
110 /**
111  * get int32 value from source
112  *
113  * #### Parameters
114  * - request : Describes the request by bindings from afb-daemon
115  * - source  : input source
116  * - out_id  : output int32 value
117  *
118  * #### Return
119  * error code
120  *
121  */
122 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
123 {
124     char* endptr;
125     const char* tmp = afb_req_value (request, source);
126     if(!tmp)
127     {
128         return REQ_FAIL;
129     }
130     long tmp_id = strtol(tmp,&endptr,10);
131
132     /* error check of range */
133     if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
134     {
135         return OUT_RANGE;
136     }
137     if(*endptr != '\0')
138     {
139         return NOT_NUMBER;
140     }
141
142     *out_id = (int32_t)tmp_id;
143     return REQ_OK;
144 }
145
146 /**
147  * add int object to json object
148  *
149  * #### Parameters
150  * - j_obj : the json object will join in int json object
151  * - count : input parameter number
152  * - ...   : parameter list
153  *
154  * #### Return
155  * None
156  *
157  */
158 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
159 {
160     va_list args;
161     va_start(args, count);
162     for(int i = 0; i < count; ++i )
163     {
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));
167         ++i;
168     }
169     va_end(args);
170 }
171
172 /**
173  * add string object to json object
174  *
175  * #### Parameters
176  * - j_obj : the json object will join in string json object
177  * - count : input parameter number
178  * - ...   : parameter list
179  *
180  * #### Return
181  * None
182  *
183  */
184 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
185 {
186     va_list args;
187     va_start(args, count);
188     for(int i = 0; i < count; ++i )
189     {
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));
193         ++i;
194     }
195     va_end(args);
196 }
197
198 /**
199  * add new json object to json object
200  *
201  * #### Parameters
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
206  *
207  * #### Return
208  * None
209  *
210  */
211 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
212 {
213     va_list args;
214     va_start(args, count);
215
216     json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
217
218     for(int i = 0; i < count; ++i )
219     {
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));
223         ++i;
224     }
225     va_end(args);
226 }
227
228 /**
229  * search event position in event list
230  *
231  * #### Parameters
232  * - value : searched event name
233  *
234  * #### Return
235  * event's index in event list
236  *
237  */
238 int hs_search_event_name_index(const char* value)
239 {
240     size_t buf_size = 50;
241     size_t size = sizeof evlist / sizeof *evlist;
242     int ret = -1;
243     for(size_t i = 0 ; i < size ; ++i)
244     {
245         if(!strncmp(value, evlist[i], buf_size))
246         {
247             ret = i;
248             break;
249         }
250     }
251     return ret;
252 }
253
254 /**
255  * get application id from request
256  *
257  * #### Parameters
258  * - request : the request
259  *
260  * #### Return
261  * got application id
262  *
263  */
264 std::string get_application_id(const afb_req_t request)
265 {
266     std::string appid;
267     char *app_id = afb_req_get_application_id(request);
268     if(app_id == nullptr) {
269         appid = std::string("");
270     }
271     else {
272         appid = std::string(app_id);
273         free(app_id);
274     }
275
276     return appid;
277 }
278
279 /**
280  * read json file
281  *
282  * #### Parameters
283  * - file : file name
284  * - obj : json_object
285  *
286  * #### Return
287  * 0 : read success
288  * -1 : read fail
289  *
290  */
291 int readJsonFile(const char* file, struct json_object **obj)
292 {
293     int ret = -1;
294     FILE *fp = fopen(file, "rb");
295     if(fp == nullptr) {
296         AFB_ERROR("open %s failed", file);
297         return ret;
298     }
299
300     *obj = nullptr;
301     const int buf_size = 128;
302     char buf[buf_size];
303     struct json_tokener *tokener = json_tokener_new();
304     enum json_tokener_error json_error;
305     while(1) {
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);
310             ret = 0;
311             break;
312         }
313
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);
317             *obj = nullptr;
318             break;
319         }
320         }
321
322         fclose(fp);
323         json_tokener_free(tokener);
324         return ret;
325 }
326
327 /**
328  * write to json file
329  *
330  * #### Parameters
331  * - file : output file name
332  * - obj : json_object
333  *
334  * #### Return
335  * 0 : write success
336  * -1 : write fail
337  *
338  */
339 int writeJsonFile(const char* file,  struct json_object *obj)
340 {
341     int ret = -1;
342     FILE *fp = fopen(file, "w+");
343     if(fp == nullptr) {
344         AFB_ERROR("open %s failed", file);
345         return ret;
346     }
347
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);
351     if(cnt == 1) {
352         ret = 0;
353         fflush(fp);
354         fsync(fileno(fp));
355     }
356     else {
357         AFB_WARNING("write to %s failed.cnt=%d,len=%d,str=%s", file, cnt, len, str);
358     }
359
360     fclose(fp);
361     return ret;
362 }