Merge "Fix invalid memory access" into tizen
[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_query_to_gvariant(iotcon_query_h query)
172 {
173         FN_CALL;
174         gpointer key, value;
175         GHashTableIter iter;
176         GVariantBuilder builder;
177
178         g_variant_builder_init(&builder, G_VARIANT_TYPE("a(ss)"));
179         if (query) {
180                 g_hash_table_iter_init(&iter, query->hash);
181                 while (g_hash_table_iter_next(&iter, &key, &value))
182                         g_variant_builder_add(&builder, "(ss)", key, value);
183         }
184
185         return g_variant_builder_end(&builder);
186 }
187
188
189 GVariant* icl_dbus_options_to_gvariant(iotcon_options_h options)
190 {
191         GHashTableIter iter;
192         GVariantBuilder builder;
193         gpointer option_id, option_data;
194
195         g_variant_builder_init(&builder, G_VARIANT_TYPE("a(qs)"));
196         if (options) {
197                 g_hash_table_iter_init(&iter, options->hash);
198                 while (g_hash_table_iter_next(&iter, &option_id, &option_data)) {
199                         g_variant_builder_add(&builder, "(qs)", GPOINTER_TO_INT(option_id),
200                                         option_data);
201                 }
202         }
203
204         return g_variant_builder_end(&builder);
205 }
206
207
208 GVariant* icl_dbus_observers_to_gvariant(iotcon_observers_h observers)
209 {
210         GList *node;
211         GVariantBuilder builder;
212
213         g_variant_builder_init(&builder, G_VARIANT_TYPE("ai"));
214         if (observers) {
215                 for (node = observers->observers_list; node; node = node->next)
216                         g_variant_builder_add(&builder, "i", GPOINTER_TO_INT(node->data));
217         }
218
219         return g_variant_builder_end(&builder);
220 }
221