Sound Manager
sm-helper.c
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 "sm-helper.h"
18 #include <stdlib.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <json-c/json.h>
22 #include <stdarg.h>
23 
24 REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id)
25 {
26  char* endptr;
27  const char* tmp = afb_req_value (request, source);
28  if(!tmp)
29  {
30  return REQ_FAIL;
31  }
32  long tmp_id = strtol(tmp,&endptr,10);
33 
34  /* error check of range */
35  if( (tmp_id > UINT16_MAX) || (tmp_id < 0) )
36  {
37  return OUT_RANGE;
38  }
39  if(*endptr != '\0')
40  {
41  return NOT_NUMBER;
42  }
43 
44  *out_id = (uint16_t)tmp_id;
45  return REQ_OK;
46 }
47 
48 REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id)
49 {
50  char* endptr;
51  const char* tmp = afb_req_value (request, source);
52  if(!tmp)
53  {
54  return REQ_FAIL;
55  }
56  long tmp_id = strtol(tmp,&endptr,10);
57 
58  /* error check of range */
59  if( (tmp_id > INT16_MAX) || (tmp_id < INT16_MIN) )
60  {
61  return OUT_RANGE;
62  }
63  if(*endptr != '\0')
64  {
65  return NOT_NUMBER;
66  }
67 
68  *out_id = (int16_t)tmp_id;
69  return REQ_OK;
70 }
71 
72 REQ_ERROR get_value_int32(const struct afb_req request, const char *source, int32_t *out_id)
73 {
74  char* endptr;
75  const char* tmp = afb_req_value (request, source);
76  if(!tmp)
77  {
78  return REQ_FAIL;
79  }
80  long tmp_id = strtol(tmp,&endptr,10);
81 
82  /* error check of range */
83  if( (tmp_id > INT32_MAX) || (tmp_id < INT32_MIN) )
84  {
85  return OUT_RANGE;
86  }
87  if(*endptr != '\0')
88  {
89  return NOT_NUMBER;
90  }
91 
92  *out_id = (int32_t)tmp_id;
93  return REQ_OK;
94 }
95 
96 void sm_add_object_to_json_object(struct json_object* j_obj, int count,...)
97 {
98  va_list args;
99  va_start(args, count);
100  for(int i = 0; i < count; ++i )
101  {
102  char *key = va_arg(args, char*);
103  int value = va_arg(args, int);
104  json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
105  ++i;
106  }
107  va_end(args);
108 }
109 
110 void sm_add_object_to_json_object_func(struct json_object* j_obj, const char* verb_name, int count, ...)
111 {
112  va_list args;
113  va_start(args, count);
114 
115  json_object_object_add(j_obj,"verb", json_object_new_string(verb_name));
116 
117  for(int i = 0; i < count; ++i )
118  {
119  char *key = va_arg(args, char*);
120  int value = va_arg(args, int);
121  json_object_object_add(j_obj, key, json_object_new_int((int32_t)value));
122  ++i;
123  }
124  va_end(args);
125 }
126 
127 int sm_search_event_name_index(const char* value)
128 {
129  size_t buf_size = 50;
130  size_t size = sizeof cmd_evlist / sizeof *cmd_evlist;
131  int ret = -1;
132  for(size_t i = 0 ; i < size ; ++i)
133  {
134  if(!strncmp(value, cmd_evlist[i], buf_size))
135  {
136  ret = i;
137  break;
138  }
139  }
140  return ret;
141 }
142 
144 {
145  size_t buf_size = 50;
146  size_t size = sizeof route_evlist / sizeof *route_evlist;
147  int ret = -1;
148  for(size_t i = 0 ; i < size ; ++i)
149  {
150  if(!strncmp(value, route_evlist[i], buf_size))
151  {
152  ret = i;
153  break;
154  }
155  }
156  return ret;
157 }
158 
159 GVariant* create_source_data(guint16 sourceID, guint16 domainID, const char* appname, guint16 sourceClassID,
160  gint32 sourceState, gint16 volume, gboolean visible, struct availability_s availables,
161  guint16 interrupt, struct sound_property_s soundPropertyList, gint32 connectionFormatList,
162  struct main_sound_property_s mainPropertyList, struct notification_config_s NConfRouting,
163  struct notification_config_s NConfCommand)
164 {
165  GVariantBuilder builder;
166 
167  AFB_DEBUG("create sourceData %d", __LINE__);
168  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(qqsqinb(ii)qa(in)aia(in)a(iin)a(iin))"));
169  g_variant_builder_add (&builder, "q", sourceID);
170  g_variant_builder_add (&builder, "q", domainID);
171  g_variant_builder_add (&builder, "s", appname);
172  g_variant_builder_add (&builder, "q", sourceClassID);
173  g_variant_builder_add (&builder, "i", sourceState);
174  g_variant_builder_add (&builder, "n", volume);
175  g_variant_builder_add (&builder, "b", visible);
176  g_variant_builder_add (&builder, "(ii)", availables.availability, availables.avalilable_reason);
177  g_variant_builder_add (&builder, "q", interrupt);
178 
179  g_variant_builder_open(&builder, G_VARIANT_TYPE("a(in)"));
180  g_variant_builder_open(&builder, G_VARIANT_TYPE("(in)"));
181  g_variant_builder_add (&builder, "i", soundPropertyList.type);
182  g_variant_builder_add (&builder, "n", soundPropertyList.value);
183  g_variant_builder_close(&builder);
184  g_variant_builder_close (&builder);
185 
186  g_variant_builder_open(&builder, G_VARIANT_TYPE("ai"));
187  g_variant_builder_add (&builder, "i", connectionFormatList);
188  g_variant_builder_close (&builder);
189 
190  g_variant_builder_open(&builder, G_VARIANT_TYPE("a(in)"));
191  g_variant_builder_open(&builder, G_VARIANT_TYPE("(in)"));
192  g_variant_builder_add (&builder, "i", mainPropertyList.type);
193  g_variant_builder_add (&builder, "n", mainPropertyList.value);
194  g_variant_builder_close (&builder);
195  g_variant_builder_close(&builder);
196 
197  g_variant_builder_open(&builder, G_VARIANT_TYPE("a(iin)"));
198  g_variant_builder_open(&builder, G_VARIANT_TYPE("(iin)"));
199  g_variant_builder_add (&builder, "i", NConfRouting.type);
200  g_variant_builder_add (&builder, "i", NConfRouting.status);
201  g_variant_builder_add (&builder, "n", NConfRouting.parameter);
202  g_variant_builder_close(&builder);
203  g_variant_builder_close (&builder);
204 
205 
206  g_variant_builder_open(&builder, G_VARIANT_TYPE("a(iin)"));
207  g_variant_builder_open(&builder, G_VARIANT_TYPE("(iin)"));
208  g_variant_builder_add (&builder, "i", NConfCommand.type);
209  g_variant_builder_add (&builder, "i", NConfCommand.status);
210  g_variant_builder_add (&builder, "n", NConfCommand.parameter);
211  g_variant_builder_close(&builder);
212  g_variant_builder_close (&builder);
213 
214  AFB_DEBUG("created sourceData %d", __LINE__);
215  return g_variant_builder_end (&builder);
216 }
217 
218 GVariant* create_domain_data(struct domain_data* data)
219 {
220  GVariantBuilder builder;
221  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(qsssbbn)"));
222  g_variant_builder_add (&builder, "q", data->domainID);
223  g_variant_builder_add (&builder, "s", data->name);
224  g_variant_builder_add (&builder, "s", data->busname);
225  g_variant_builder_add (&builder, "s", data->nodename);
226  g_variant_builder_add (&builder, "b", data->early);
227  g_variant_builder_add (&builder, "b", data->complete);
228  g_variant_builder_add (&builder, "n", data->state);
229  AFB_DEBUG("created domainData %d", __LINE__);
230  return g_variant_builder_end (&builder);
231 }
gchar * nodename
Definition: sm-helper.h:73
gchar * name
Definition: sm-helper.h:71
gint32 availability
Definition: sm-helper.h:56
gint16 state
Definition: sm-helper.h:76
void sm_add_object_to_json_object_func(struct json_object *j_obj, const char *verb_name, int count,...)
Definition: sm-helper.c:110
gboolean complete
Definition: sm-helper.h:75
gint32 avalilable_reason
Definition: sm-helper.h:57
REQ_ERROR get_value_int16(const struct afb_req request, const char *source, int16_t *out_id)
Definition: sm-helper.c:48
guint16 domainID
Definition: sm-helper.h:70
int sm_search_event_name_index(const char *value)
Definition: sm-helper.c:127
REQ_ERROR get_value_uint16(const struct afb_req request, const char *source, uint16_t *out_id)
Definition: sm-helper.c:24
guint16 type
Definition: sm-helper.h:52
gboolean early
Definition: sm-helper.h:74
REQ_ERROR get_value_int32(const struct afb_req request, const char *source, int32_t *out_id)
Definition: sm-helper.c:72
void sm_add_object_to_json_object(struct json_object *j_obj, int count,...)
Definition: sm-helper.c:96
gchar * busname
Definition: sm-helper.h:72
GVariant * create_domain_data(struct domain_data *data)
Definition: sm-helper.c:218
GVariant * create_source_data(guint16 sourceID, guint16 domainID, const char *appname, guint16 sourceClassID, gint32 sourceState, gint16 volume, gboolean visible, struct availability_s availables, guint16 interrupt, struct sound_property_s soundPropertyList, gint32 connectionFormatList, struct main_sound_property_s mainPropertyList, struct notification_config_s NConfRouting, struct notification_config_s NConfCommand)
Definition: sm-helper.c:159
REQ_ERROR
Definition: sm-helper.h:26
int sm_search_routing_event_name_index(const char *value)
Definition: sm-helper.c:143