Change gitreview to new location
[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 REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id)
31 {
32     char* endptr;
33     const char* tmp = afb_req_value (request, source);
34     if(!tmp)
35     {
36         return REQ_FAIL;
37     }
38     long tmp_id = strtol(tmp,&endptr,10);
39
40     /* error check of range */
41     if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
42     {
43         return OUT_RANGE;
44     }
45     if(*endptr != '\0')
46     {
47         return NOT_NUMBER;
48     }
49
50     *out_id = (uint16_t)tmp_id;
51     return REQ_OK;
52 }
53
54 REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id)
55 {
56     char* endptr;
57     const char* tmp = afb_req_value (request, source);
58     if(!tmp)
59     {
60         return REQ_FAIL;
61     }
62     long tmp_id = strtol(tmp,&endptr,10);
63
64     /* error check of range */
65     if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
66     {
67         return OUT_RANGE;
68     }
69     if(*endptr != '\0')
70     {
71         return NOT_NUMBER;
72     }
73
74     *out_id = (int16_t)tmp_id;
75     return REQ_OK;
76 }
77
78 REQ_ERROR get_value_int32(const struct afb_req request, const char *source, int32_t *out_id)
79 {
80     char* endptr;
81     const char* tmp = afb_req_value (request, source);
82     if(!tmp)
83     {
84         return REQ_FAIL;
85     }
86     long tmp_id = strtol(tmp,&endptr,10);
87
88     /* error check of range */
89     if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
90     {
91         return OUT_RANGE;
92     }
93     if(*endptr != '\0')
94     {
95         return NOT_NUMBER;
96     }
97
98     *out_id = (int32_t)tmp_id;
99     return REQ_OK;
100 }
101
102 void hs_add_object_to_json_object(struct json_object* j_obj, int count,...)
103 {
104     va_list args;
105     va_start(args, count);
106     for(int i = 0; i < count; ++i )
107     {
108         char *key = va_arg(args, char*);
109         int value = va_arg(args, int);
110         json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
111         ++i;
112     }
113     va_end(args);
114 }
115
116 void hs_add_object_to_json_object_str(struct json_object* j_obj, int count,...)
117 {
118     va_list args;
119     va_start(args, count);
120     for(int i = 0; i < count; ++i )
121     {
122         char *key = va_arg(args, char*);
123         char *value = va_arg(args, char*);
124         json_object_object_add(j_obj, key, json_object_new_string(value));
125         ++i;
126     }
127     va_end(args);
128 }
129
130
131 void hs_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
132 {
133     va_list args;
134     va_start(args, count);
135
136     json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
137
138     for(int i = 0; i < count; ++i )
139     {
140         char *key = va_arg(args, char*);
141         int value = va_arg(args, int);
142         json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
143         ++i;
144     }
145     va_end(args);
146 }
147
148 int hs_search_event_name_index(const char* value)
149 {
150     size_t buf_size = 50;
151     size_t size = sizeof evlist / sizeof *evlist;
152     int ret = -1;
153     for(size_t i = 0 ; i < size ; ++i)
154     {
155         if(!strncmp(value, evlist[i], buf_size))
156         {
157             ret = i;
158             break;
159         }
160     }
161     return ret;
162 }