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