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