fix issue,not free string
[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
21
22 const char* evlist[] = {
23     "tap_shortcut",
24     "on_screen_message",
25     "on_screen_reply",
26     "showWindow",
27     "hideWindow",
28     "replyShowWindow",
29     "showNotification",
30     "showInformation",
31     "reserved"
32   };
33
34 /**
35  * get uint16 value from source
36  *
37  * #### Parameters
38  * - request : Describes the request by bindings from afb-daemon
39  * - source  : input source
40  * - out_id  : output uint16 value
41  *
42  * #### Return
43  * error code
44  *
45  */
46 REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
47 {
48     char* endptr;
49     const char* tmp = afb_req_value (request, source);
50     if(!tmp)
51     {
52         return REQ_FAIL;
53     }
54     long tmp_id = strtol(tmp,&endptr,10);
55
56     /* error check of range */
57     if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
58     {
59         return OUT_RANGE;
60     }
61     if(*endptr != '\0')
62     {
63         return NOT_NUMBER;
64     }
65
66     *out_id = (uint16_t)tmp_id;
67     return REQ_OK;
68 }
69
70 /**
71  * get int16 value from source
72  *
73  * #### Parameters
74  * - request : Describes the request by bindings from afb-daemon
75  * - source  : input source
76  * - out_id  : output int16 value
77  *
78  * #### Return
79  * error code
80  *
81  */
82 REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
83 {
84     char* endptr;
85     const char* tmp = afb_req_value (request, source);
86     if(!tmp)
87     {
88         return REQ_FAIL;
89     }
90     long tmp_id = strtol(tmp,&endptr,10);
91
92     /* error check of range */
93     if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
94     {
95         return OUT_RANGE;
96     }
97     if(*endptr != '\0')
98     {
99         return NOT_NUMBER;
100     }
101
102     *out_id = (int16_t)tmp_id;
103     return REQ_OK;
104 }
105
106 /**
107  * get int32 value from source
108  *
109  * #### Parameters
110  * - request : Describes the request by bindings from afb-daemon
111  * - source  : input source
112  * - out_id  : output int32 value
113  *
114  * #### Return
115  * error code
116  *
117  */
118 REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
119 {
120     char* endptr;
121     const char* tmp = afb_req_value (request, source);
122     if(!tmp)
123     {
124         return REQ_FAIL;
125     }
126     long tmp_id = strtol(tmp,&endptr,10);
127
128     /* error check of range */
129     if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
130     {
131         return OUT_RANGE;
132     }
133     if(*endptr != '\0')
134     {
135         return NOT_NUMBER;
136     }
137
138     *out_id = (int32_t)tmp_id;
139     return REQ_OK;
140 }
141
142 /**
143  * add int object to json object
144  *
145  * #### Parameters
146  * - j_obj : the json object will join in int json object
147  * - count : input parameter number
148  * - ...   : parameter list
149  *
150  * #### Return
151  * None
152  *
153  */
154 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
155 {
156     va_list args;
157     va_start(args, count);
158     for(int i = 0; i < count; ++i )
159     {
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));
163         ++i;
164     }
165     va_end(args);
166 }
167
168 /**
169  * add string object to json object
170  *
171  * #### Parameters
172  * - j_obj : the json object will join in string json object
173  * - count : input parameter number
174  * - ...   : parameter list
175  *
176  * #### Return
177  * None
178  *
179  */
180 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
181 {
182     va_list args;
183     va_start(args, count);
184     for(int i = 0; i < count; ++i )
185     {
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));
189         ++i;
190     }
191     va_end(args);
192 }
193
194 /**
195  * add new json object to json object
196  *
197  * #### Parameters
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
202  *
203  * #### Return
204  * None
205  *
206  */
207 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
208 {
209     va_list args;
210     va_start(args, count);
211
212     json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
213
214     for(int i = 0; i < count; ++i )
215     {
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));
219         ++i;
220     }
221     va_end(args);
222 }
223
224 /**
225  * search event position in event list
226  *
227  * #### Parameters
228  * - value : searched event name
229  *
230  * #### Return
231  * event's index in event list
232  *
233  */
234 int hs_search_event_name_index(const char* value)
235 {
236     size_t buf_size = 50;
237     size_t size = sizeof evlist / sizeof *evlist;
238     int ret = -1;
239     for(size_t i = 0 ; i < size ; ++i)
240     {
241         if(!strncmp(value, evlist[i], buf_size))
242         {
243             ret = i;
244             break;
245         }
246     }
247     return ret;
248 }
249
250 /**
251  * get application id from request
252  *
253  * #### Parameters
254  * - request : the request
255  *
256  * #### Return
257  * got application id
258  *
259  */
260 std::string get_application_id(const afb_req_t request)
261 {
262     std::string appid;
263     char *app_id = afb_req_get_application_id(request);
264     if(app_id == nullptr) {
265         appid = std::string("");
266     }
267     else {
268         appid = std::string(app_id);
269         free(app_id);
270     }
271
272     return appid;
273 }