Fix invalid memory access
[platform/core/iot/iotcon.git] / lib / icl-dbus-type.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <glib.h>
20
21 #include "iotcon.h"
22 #include "ic-utils.h"
23 #include "icl.h"
24 #include "icl-resource.h"
25 #include "icl-resource-types.h"
26 #include "icl-resource-interfaces.h"
27 #include "icl-options.h"
28 #include "icl-query.h"
29 #include "icl-request.h"
30 #include "icl-response.h"
31 #include "icl-remote-resource.h"
32 #include "icl-representation.h"
33 #include "icl-device.h"
34 #include "icl-payload.h"
35 #include "icl-observation.h"
36 #include "icl-dbus-type.h"
37
38 const char** icl_dbus_resource_interfaces_to_array(iotcon_resource_interfaces_h ifaces)
39 {
40         int i = 0;
41         GList *node = ifaces->iface_list;
42         int len = g_list_length(node);
43
44         if (0 == len) {
45                 ERR("Empty Resource Interface List");
46                 return NULL;
47         }
48
49         const char **array = calloc(len + 1, sizeof(char*));
50         if (NULL == array) {
51                 ERR("calloc() Fail(%d)", errno);
52                 return NULL;
53         }
54
55         for (node = ifaces->iface_list; node; node = node->next, i++)
56                 array[i] = node->data;
57         array[i] = NULL;
58
59         return array;
60 }
61
62
63 const char** icl_dbus_resource_types_to_array(iotcon_resource_types_h types)
64 {
65         int i = 0;
66         GList *node = types->type_list;
67         int len = g_list_length(node);
68
69         if (0 == len) {
70                 ERR("Empty Resource Type List");
71                 return NULL;
72         }
73
74         const char **array = calloc(len + 1, sizeof(char*));
75         if (NULL == array) {
76                 ERR("calloc() Fail(%d)", errno);
77                 return NULL;
78         }
79
80         for (node = types->type_list; node; node = node->next, i++)
81                 array[i] = node->data;
82         array[i] = NULL;
83
84         return array;
85 }
86
87
88 GVariant* icl_dbus_representation_to_gvariant(struct icl_representation_s *repr)
89 {
90         GVariantBuilder builder;
91         GVariant *repr_gvar;
92
93         g_variant_builder_init(&builder, G_VARIANT_TYPE("av"));
94
95         if (repr) {
96                 repr_gvar = icl_representation_to_gvariant(repr);
97                 if (NULL == repr_gvar) {
98                         ERR("icl_representation_to_gvariant() Fail");
99                         g_variant_builder_clear(&builder);
100                         return NULL;
101                 }
102                 g_variant_builder_add(&builder, "v", repr_gvar);
103         }
104
105         return g_variant_builder_end(&builder);
106 }
107
108
109 GVariant* icl_dbus_response_to_gvariant(struct icl_resource_response *response)
110 {
111         FN_CALL;
112         GVariant *value;
113         GVariant *repr_gvar;
114         GHashTableIter iter;
115         GVariantBuilder options;
116         gpointer option_id, option_data;
117
118         g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
119         if (response->header_options) {
120                 g_hash_table_iter_init(&iter, response->header_options->hash);
121                 while (g_hash_table_iter_next(&iter, &option_id, &option_data)) {
122                         g_variant_builder_add(&options, "(qs)", GPOINTER_TO_INT(option_id),
123                                         option_data);
124                 }
125         }
126
127         repr_gvar = icl_representation_to_gvariant(response->repr);
128         if (NULL == repr_gvar) {
129                 ERR("icl_representation_to_gvariant() Fail");
130                 g_variant_builder_clear(&options);
131                 return NULL;
132         }
133
134         value = g_variant_new("(a(qs)ivxx)",
135                         &options,
136                         response->result,
137                         repr_gvar,
138                         response->oic_request_h,
139                         response->oic_resource_h);
140
141         return value;
142 }
143
144
145 GVariant* icl_dbus_remote_resource_to_gvariant(struct icl_remote_resource *resource)
146 {
147         FN_CALL;
148         bool is_secure;
149         GVariant *value;
150         GHashTableIter iter;
151         GVariantBuilder options;
152         gpointer option_id, option_data;
153
154         g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
155         if (resource->header_options) {
156                 g_hash_table_iter_init(&iter, resource->header_options->hash);
157                 while (g_hash_table_iter_next(&iter, &option_id, &option_data)) {
158                         g_variant_builder_add(&options, "(qs)", GPOINTER_TO_INT(option_id),
159                                         option_data);
160                 }
161         }
162
163         is_secure = resource->properties & IOTCON_RESOURCE_SECURE;
164         value = g_variant_new("(ssba(qs)i)", resource->uri_path, resource->host_address,
165                         is_secure, &options, resource->connectivity_type);
166
167         return value;
168 }
169
170
171 GVariant* icl_dbus_device_info_to_gvariant(iotcon_device_info_h device_info)
172 {
173         GVariant *value;
174
175         value = g_variant_new("(s)", device_info->device_name);
176
177         return value;
178 }
179
180
181 GVariant* icl_dbus_platform_info_to_gvariant(iotcon_platform_info_h platform_info)
182 {
183         GVariant *value;
184
185         value = g_variant_new("(sssssssssss)",
186                         platform_info->platform_id,
187                         platform_info->manuf_name,
188                         platform_info->manuf_url,
189                         platform_info->model_number,
190                         platform_info->date_of_manuf,
191                         platform_info->platform_ver,
192                         platform_info->os_ver,
193                         platform_info->hardware_ver,
194                         platform_info->firmware_ver,
195                         platform_info->support_url,
196                         platform_info->system_time);
197
198         return value;
199 }
200
201
202 GVariant* icl_dbus_query_to_gvariant(iotcon_query_h query)
203 {
204         FN_CALL;
205         gpointer key, value;
206         GHashTableIter iter;
207         GVariantBuilder builder;
208
209         g_variant_builder_init(&builder, G_VARIANT_TYPE("a(ss)"));
210         if (query) {
211                 g_hash_table_iter_init(&iter, query->hash);
212                 while (g_hash_table_iter_next(&iter, &key, &value))
213                         g_variant_builder_add(&builder, "(ss)", key, value);
214         }
215
216         return g_variant_builder_end(&builder);
217 }
218
219
220 GVariant* icl_dbus_options_to_gvariant(iotcon_options_h options)
221 {
222         GHashTableIter iter;
223         GVariantBuilder builder;
224         gpointer option_id, option_data;
225
226         g_variant_builder_init(&builder, G_VARIANT_TYPE("a(qs)"));
227         if (options) {
228                 g_hash_table_iter_init(&iter, options->hash);
229                 while (g_hash_table_iter_next(&iter, &option_id, &option_data)) {
230                         g_variant_builder_add(&builder, "(qs)", GPOINTER_TO_INT(option_id),
231                                         option_data);
232                 }
233         }
234
235         return g_variant_builder_end(&builder);
236 }
237
238
239 GVariant* icl_dbus_observers_to_gvariant(iotcon_observers_h observers)
240 {
241         GList *node;
242         GVariantBuilder builder;
243
244         g_variant_builder_init(&builder, G_VARIANT_TYPE("ai"));
245         if (observers) {
246                 for (node = observers->observers_list; node; node = node->next)
247                         g_variant_builder_add(&builder, "i", GPOINTER_TO_INT(node->data));
248         }
249
250         return g_variant_builder_end(&builder);
251 }
252