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