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