Modify callback parameter
[platform/core/iot/iotcon.git] / lib / icl-device.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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <glib.h>
21
22 #include "iotcon.h"
23 #include "ic-utils.h"
24 #include "icl.h"
25 #include "icl-dbus.h"
26 #include "icl-dbus-type.h"
27
28 #ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */
29 typedef struct {
30         iotcon_device_info_cb cb;
31         void *user_data;
32         unsigned int id;
33 } icl_device_info_s;
34 #endif
35
36 typedef struct {
37         iotcon_platform_info_cb cb;
38         void *user_data;
39         unsigned int id;
40 } icl_platform_info_s;
41
42
43 #ifdef DEVICE_INFO_IMPL /* not implemented in iotivity 0.9.1 */
44 /* The length of manufacturer_name should be less than and equal to 16.
45  * The length of manufacturer_url should be less than and equal to 32. */
46 int iotcon_register_device_info(iotcon_device_info_s device_info)
47 {
48         int ret;
49         GError *error = NULL;
50         GVariant *arg_info;
51
52         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
53
54         if (device_info.manuf_name
55                         && (IOTCON_MANUFACTURER_NAME_LENGTH_MAX < strlen(device_info.manuf_name))) {
56                 ERR("The length of manufacturer_name(%s) is invalid.", device_info.manuf_name);
57                 return IOTCON_ERROR_INVALID_PARAMETER;
58         }
59
60         if (device_info.manuf_url
61                         && (IOTCON_MANUFACTURER_URL_LENGTH_MAX < strlen(device_info.manuf_url))) {
62                 ERR("The length of manufacturer_url(%s) is invalid.", device_info.manuf_url);
63                 return IOTCON_ERROR_INVALID_PARAMETER;
64         }
65
66         arg_info = icl_dbus_device_info_to_gvariant(&device_info);
67         ic_dbus_call_register_device_info_sync(icl_dbus_get_object(), arg_info, &ret,
68                         NULL, &error);
69         if (error) {
70                 ERR("ic_dbus_call_register_device_info_sync() Fail(%s)", error->message);
71                 g_error_free(error);
72                 g_variant_unref(arg_info);
73                 return IOTCON_ERROR_DBUS;
74         }
75
76         if (IOTCON_ERROR_NONE != ret) {
77                 ERR("iotcon-daemon Fail(%d)", ret);
78                 return icl_dbus_convert_daemon_error(ret);
79         }
80
81         return ret;
82 }
83
84
85 static void _icl_device_info_cb(GDBusConnection *connection,
86                 const gchar *sender_name,
87                 const gchar *object_path,
88                 const gchar *interface_name,
89                 const gchar *signal_name,
90                 GVariant *parameters,
91                 gpointer user_data)
92 {
93         icl_device_info_s *cb_container = user_data;
94         iotcon_device_info_cb cb = cb_container->cb;
95
96         iotcon_device_info_s info = {0};
97
98         g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s&s)",
99                         &info.name,
100                         &info.host_name,
101                         &info.uuid,
102                         &info.content_type,
103                         &info.version,
104                         &info.manuf_name,
105                         &info.manuf_url,
106                         &info.model_number,
107                         &info.date_of_manufacture,
108                         &info.platform_ver,
109                         &info.firmware_ver,
110                         &info.support_url);
111
112         info.name = ic_utils_dbus_decode_str(info.name);
113         info.host_name = ic_utils_dbus_decode_str(info.host_name);
114         info.uuid = ic_utils_dbus_decode_str(info.uuid);
115         info.content_type = ic_utils_dbus_decode_str(info.content_type);
116         info.version = ic_utils_dbus_decode_str(info.version);
117         info.manuf_name = ic_utils_dbus_decode_str(info.manuf_name);
118         info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url);
119         info.model_number = ic_utils_dbus_decode_str(info.model_number);
120         info.date_of_manufacture = ic_utils_dbus_decode_str(info.date_of_manufacture);
121         info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver);
122         info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver);
123         info.support_url = ic_utils_dbus_decode_str(info.support_url);
124
125         if (cb)
126                 cb(info, cb_container->user_data);
127 }
128
129
130 int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb cb,
131                 void *user_data)
132 {
133         int ret;
134         GError *error = NULL;
135         unsigned int sub_id;
136         int signal_number;
137         char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
138         icl_device_info_s *cb_container;
139
140         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
141         RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
142         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
143
144         signal_number = icl_dbus_generate_signal_number();
145
146         ic_dbus_call_get_device_info_sync(icl_dbus_get_object(), host_address,
147                         signal_number, &ret, NULL, &error);
148         if (error) {
149                 ERR("ic_dbus_call_get_device_info_sync() Fail(%s)", error->message);
150                 g_error_free(error);
151                 return IOTCON_ERROR_DBUS;
152         }
153
154         if (IOTCON_ERROR_NONE != ret) {
155                 ERR("iotcon-daemon Fail(%d)", ret);
156                 return icl_dbus_convert_daemon_error(ret);
157         }
158
159         snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DEVICE,
160                         signal_number);
161
162         cb_container = calloc(1, sizeof(icl_device_info_container_s));
163         if (NULL == cb_container) {
164                 ERR("calloc() Fail(%d)", errno);
165                 return IOTCON_ERROR_OUT_OF_MEMORY;
166         }
167
168         cb_container->cb = cb;
169         cb_container->user_data = user_data;
170
171         sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free,
172                         _icl_device_info_cb);
173         if (0 == sub_id) {
174                 ERR("icl_dbus_subscribe_signal() Fail");
175                 return IOTCON_ERROR_DBUS;
176         }
177
178         cb_container->id = sub_id;
179
180         return ret;
181 }
182 #endif
183
184
185 /* The length of manufacturer_name should be less than and equal to 16.
186  * The length of manufacturer_url should be less than and equal to 32. */
187 API int iotcon_register_platform_info(iotcon_platform_info_s platform_info)
188 {
189         int ret;
190         GError *error = NULL;
191         GVariant *arg_info;
192
193         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
194
195         if (platform_info.manuf_name
196                         && (IOTCON_MANUFACTURER_NAME_LENGTH_MAX < strlen(platform_info.manuf_name))) {
197                 ERR("The length of manufacturer_name(%s) is invalid.", platform_info.manuf_name);
198                 return IOTCON_ERROR_INVALID_PARAMETER;
199         }
200
201         if (platform_info.manuf_url
202                         && (IOTCON_MANUFACTURER_URL_LENGTH_MAX < strlen(platform_info.manuf_url))) {
203                 ERR("The length of manufacturer_url(%s) is invalid.", platform_info.manuf_url);
204                 return IOTCON_ERROR_INVALID_PARAMETER;
205         }
206
207         arg_info = icl_dbus_platform_info_to_gvariant(&platform_info);
208         ic_dbus_call_register_platform_info_sync(icl_dbus_get_object(), arg_info, &ret,
209                         NULL, &error);
210         if (error) {
211                 ERR("ic_dbus_call_register_platform_info_sync() Fail(%s)", error->message);
212                 g_error_free(error);
213                 g_variant_unref(arg_info);
214                 return IOTCON_ERROR_DBUS;
215         }
216
217         if (IOTCON_ERROR_NONE != ret) {
218                 ERR("iotcon-daemon Fail(%d)", ret);
219                 return icl_dbus_convert_daemon_error(ret);
220         }
221
222         return ret;
223 }
224
225
226 static void _icl_platform_info_cb(GDBusConnection *connection,
227                 const gchar *sender_name,
228                 const gchar *object_path,
229                 const gchar *interface_name,
230                 const gchar *signal_name,
231                 GVariant *parameters,
232                 gpointer user_data)
233 {
234         icl_platform_info_s *cb_container = user_data;
235         iotcon_platform_info_cb cb = cb_container->cb;
236
237         iotcon_platform_info_s info = {0};
238
239         g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s)",
240                         &info.platform_id,
241                         &info.manuf_name,
242                         &info.manuf_url,
243                         &info.model_number,
244                         &info.date_of_manufacture,
245                         &info.platform_ver,
246                         &info.os_ver,
247                         &info.hardware_ver,
248                         &info.firmware_ver,
249                         &info.support_url,
250                         &info.system_time);
251
252         info.platform_id = ic_utils_dbus_decode_str(info.platform_id);
253         info.manuf_name = ic_utils_dbus_decode_str(info.manuf_name);
254         info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url);
255         info.model_number = ic_utils_dbus_decode_str(info.model_number);
256         info.date_of_manufacture = ic_utils_dbus_decode_str(info.date_of_manufacture);
257         info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver);
258         info.os_ver = ic_utils_dbus_decode_str(info.os_ver);
259         info.hardware_ver = ic_utils_dbus_decode_str(info.hardware_ver);
260         info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver);
261         info.support_url = ic_utils_dbus_decode_str(info.support_url);
262         info.system_time = ic_utils_dbus_decode_str(info.system_time);
263
264         if (cb)
265                 cb(info, cb_container->user_data);
266 }
267
268
269 API int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_cb cb,
270                 void *user_data)
271 {
272         int ret;
273         GError *error = NULL;
274         unsigned int sub_id;
275         int signal_number;
276         char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
277         icl_platform_info_s *cb_container;
278
279         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
280         RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
281         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
282
283         signal_number = icl_dbus_generate_signal_number();
284
285         ic_dbus_call_get_platform_info_sync(icl_dbus_get_object(), host_address,
286                         signal_number, &ret, NULL, &error);
287         if (error) {
288                 ERR("ic_dbus_call_get_platform_info_sync() Fail(%s)", error->message);
289                 g_error_free(error);
290                 return IOTCON_ERROR_DBUS;
291         }
292
293         if (IOTCON_ERROR_NONE != ret) {
294                 ERR("iotcon-daemon Fail(%d)", ret);
295                 return icl_dbus_convert_daemon_error(ret);
296         }
297
298         snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PLATFORM,
299                         signal_number);
300
301         cb_container = calloc(1, sizeof(icl_platform_info_s));
302         if (NULL == cb_container) {
303                 ERR("calloc() Fail(%d)", errno);
304                 return IOTCON_ERROR_OUT_OF_MEMORY;
305         }
306
307         cb_container->cb = cb;
308         cb_container->user_data = user_data;
309
310         sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free,
311                         _icl_platform_info_cb);
312         if (0 == sub_id) {
313                 ERR("icl_dbus_subscribe_signal() Fail");
314                 return IOTCON_ERROR_DBUS;
315         }
316
317         cb_container->id = sub_id;
318
319         return ret;
320 }