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