HomeScreenBinding
hs-helper.cpp
Go to the documentation of this file.
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 
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 
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 
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 
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 
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 
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 
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 }
REQ_ERROR get_value_uint16(const afb_req_t request, const char *source, uint16_t *out_id)
Definition: hs-helper.cpp:46
REQ_ERROR get_value_int16(const afb_req_t request, const char *source, int16_t *out_id)
Definition: hs-helper.cpp:82
void hs_add_object_to_json_object_str(struct json_object *j_obj, int count,...)
Definition: hs-helper.cpp:180
void hs_add_object_to_json_object(struct json_object *j_obj, int count,...)
Definition: hs-helper.cpp:154
REQ_ERROR
Definition: hs-helper.h:29
const char * evlist[]
Definition: hs-helper.cpp:22
void hs_add_object_to_json_object_func(struct json_object *j_obj, const char *verb_name, int count,...)
Definition: hs-helper.cpp:207
REQ_ERROR get_value_int32(const afb_req_t request, const char *source, int32_t *out_id)
Definition: hs-helper.cpp:118
int hs_search_event_name_index(const char *value)
Definition: hs-helper.cpp:234