2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "icl-dbus-type.h"
28 #include "icl-device.h"
31 * @brief The maximum length which can be held in a manufacturer name.
35 #define ICL_MANUFACTURER_NAME_LENGTH_MAX 15
38 * @brief The maximum length which can be held in a manufacturer url.
42 #define ICL_MANUFACTURER_URL_LENGTH_MAX 32
46 iotcon_device_info_cb cb;
52 iotcon_platform_info_cb cb;
55 } icl_platform_info_s;
58 iotcon_tizen_info_cb cb;
63 API int iotcon_device_info_create(iotcon_device_info_h *device_info)
65 iotcon_device_info_h info = NULL;
67 RETV_IF(NULL == device_info, IOTCON_ERROR_INVALID_PARAMETER);
69 info = calloc(1, sizeof(struct icl_device_info));
71 ERR("calloc() Fail(%d)", errno);
72 return IOTCON_ERROR_OUT_OF_MEMORY;
77 return IOTCON_ERROR_NONE;
81 API void iotcon_device_info_destroy(iotcon_device_info_h device_info)
83 RET_IF(NULL == device_info);
85 free(device_info->device_name);
86 free(device_info->spec_ver);
87 free(device_info->device_id);
88 free(device_info->data_model_ver);
93 API int iotcon_device_info_set_property(iotcon_device_info_h device_info,
94 iotcon_device_info_e property, const char *value)
98 RETV_IF(NULL == device_info, IOTCON_ERROR_INVALID_PARAMETER);
101 dup_value = strdup(value);
102 if (NULL == dup_value) {
103 ERR("strdup() Fail(%d)", errno);
104 return IOTCON_ERROR_OUT_OF_MEMORY;
111 case IOTCON_DEVICE_INFO_NAME:
112 if (device_info->device_name)
113 free(device_info->device_name);
114 device_info->device_name = dup_value;
116 case IOTCON_DEVICE_INFO_SPEC_VER:
117 if (device_info->spec_ver)
118 free(device_info->spec_ver);
119 device_info->spec_ver = dup_value;
121 case IOTCON_DEVICE_INFO_ID:
122 if (device_info->device_id)
123 free(device_info->device_id);
124 device_info->device_id = dup_value;
126 case IOTCON_DEVICE_INFO_DATA_MODEL_VER:
127 if (device_info->data_model_ver)
128 free(device_info->data_model_ver);
129 device_info->data_model_ver = dup_value;
132 ERR("Invalid property(%d)", property);
134 return IOTCON_ERROR_INVALID_PARAMETER;
137 return IOTCON_ERROR_NONE;
141 API int iotcon_device_info_get_property(iotcon_device_info_h device_info,
142 iotcon_device_info_e property, char **value)
144 RETV_IF(NULL == device_info, IOTCON_ERROR_INVALID_PARAMETER);
145 RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
148 case IOTCON_DEVICE_INFO_NAME:
149 *value = device_info->device_name;
151 case IOTCON_DEVICE_INFO_SPEC_VER:
152 *value = device_info->spec_ver;
154 case IOTCON_DEVICE_INFO_ID:
155 *value = device_info->device_id;
157 case IOTCON_DEVICE_INFO_DATA_MODEL_VER:
158 *value = device_info->data_model_ver;
161 ERR("Invalid property(%d)", property);
162 return IOTCON_ERROR_INVALID_PARAMETER;
165 return IOTCON_ERROR_NONE;
169 API int iotcon_set_device_info(iotcon_device_info_h device_info)
172 GError *error = NULL;
175 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
176 RETV_IF(NULL == device_info, IOTCON_ERROR_INVALID_PARAMETER);
177 RETV_IF(NULL == device_info->device_name, IOTCON_ERROR_INVALID_PARAMETER);
179 arg_info = icl_dbus_device_info_to_gvariant(device_info);
180 ic_dbus_call_register_device_info_sync(icl_dbus_get_object(), arg_info, &ret,
183 ERR("ic_dbus_call_register_device_info_sync() Fail(%s)", error->message);
184 ret = icl_dbus_convert_dbus_error(error->code);
186 g_variant_unref(arg_info);
190 if (IOTCON_ERROR_NONE != ret) {
191 ERR("iotcon-daemon Fail(%d)", ret);
192 return icl_dbus_convert_daemon_error(ret);
199 static void _icl_device_info_cb(GDBusConnection *connection,
200 const gchar *sender_name,
201 const gchar *object_path,
202 const gchar *interface_name,
203 const gchar *signal_name,
204 GVariant *parameters,
208 struct icl_device_info info = {0};
209 icl_device_info_s *cb_container = user_data;
210 iotcon_device_info_cb cb = cb_container->cb;
212 g_variant_get(parameters, "(&s&s&s&s&s)", &uri_path, &info.device_name,
213 &info.spec_ver, &info.device_id, &info.data_model_ver);
215 /* From iotivity, we can get uri_path. But, the value is always "/oic/d". */
218 cb(&info, cb_container->user_data);
222 API int iotcon_get_device_info(const char *host_address, iotcon_device_info_cb cb,
225 GError *error = NULL;
227 int ret, signal_number;
228 char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
229 icl_device_info_s *cb_container;
231 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
232 RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
233 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
235 signal_number = icl_dbus_generate_signal_number();
237 ic_dbus_call_get_device_info_sync(icl_dbus_get_object(), host_address,
238 signal_number, &ret, NULL, &error);
240 ERR("ic_dbus_call_get_device_info_sync() Fail(%s)", error->message);
241 ret = icl_dbus_convert_dbus_error(error->code);
246 if (IOTCON_ERROR_NONE != ret) {
247 ERR("iotcon-daemon Fail(%d)", ret);
248 return icl_dbus_convert_daemon_error(ret);
251 snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_DEVICE,
254 cb_container = calloc(1, sizeof(icl_device_info_s));
255 if (NULL == cb_container) {
256 ERR("calloc() Fail(%d)", errno);
257 return IOTCON_ERROR_OUT_OF_MEMORY;
260 cb_container->cb = cb;
261 cb_container->user_data = user_data;
263 sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free,
264 _icl_device_info_cb);
266 ERR("icl_dbus_subscribe_signal() Fail");
268 return IOTCON_ERROR_DBUS;
271 cb_container->id = sub_id;
277 API int iotcon_platform_info_create(iotcon_platform_info_h *platform_info)
279 iotcon_platform_info_h info = NULL;
281 RETV_IF(NULL == platform_info, IOTCON_ERROR_INVALID_PARAMETER);
283 info = calloc(1, sizeof(struct icl_platform_info));
285 ERR("calloc() Fail(%d)", errno);
286 return IOTCON_ERROR_OUT_OF_MEMORY;
289 *platform_info = info;
291 return IOTCON_ERROR_NONE;
295 API void iotcon_platform_info_destroy(iotcon_platform_info_h platform_info)
297 RET_IF(NULL == platform_info);
299 free(platform_info->platform_id);
300 free(platform_info->manuf_name);
301 free(platform_info->manuf_url);
302 free(platform_info->model_number);
303 free(platform_info->date_of_manuf);
304 free(platform_info->platform_ver);
305 free(platform_info->os_ver);
306 free(platform_info->hardware_ver);
307 free(platform_info->firmware_ver);
308 free(platform_info->support_url);
309 free(platform_info->system_time);
314 API int iotcon_platform_info_set_property(iotcon_platform_info_h platform_info,
315 iotcon_platform_info_e property, const char *value)
319 RETV_IF(NULL == platform_info, IOTCON_ERROR_INVALID_PARAMETER);
322 dup_value = strdup(value);
323 if (NULL == dup_value) {
324 ERR("strdup() Fail(%d)", errno);
325 return IOTCON_ERROR_OUT_OF_MEMORY;
332 case IOTCON_PLATFORM_INFO_ID:
333 if (platform_info->platform_id)
334 free(platform_info->platform_id);
335 platform_info->platform_id = dup_value;
337 case IOTCON_PLATFORM_INFO_MANUF_NAME:
338 if (platform_info->manuf_name)
339 free(platform_info->manuf_name);
340 platform_info->manuf_name = dup_value;
342 case IOTCON_PLATFORM_INFO_MANUF_URL:
343 if (platform_info->manuf_url)
344 free(platform_info->manuf_url);
345 platform_info->manuf_url = dup_value;
347 case IOTCON_PLATFORM_INFO_MODEL_NUMBER:
348 if (platform_info->model_number)
349 free(platform_info->model_number);
350 platform_info->model_number = dup_value;
352 case IOTCON_PLATFORM_INFO_DATE_OF_MANUF:
353 if (platform_info->date_of_manuf)
354 free(platform_info->date_of_manuf);
355 platform_info->date_of_manuf = dup_value;
357 case IOTCON_PLATFORM_INFO_PLATFORM_VER:
358 if (platform_info->platform_ver)
359 free(platform_info->platform_ver);
360 platform_info->platform_ver = dup_value;
362 case IOTCON_PLATFORM_INFO_OS_VER:
363 if (platform_info->os_ver)
364 free(platform_info->os_ver);
365 platform_info->os_ver = dup_value;
367 case IOTCON_PLATFORM_INFO_HARDWARE_VER:
368 if (platform_info->hardware_ver)
369 free(platform_info->hardware_ver);
370 platform_info->hardware_ver = dup_value;
372 case IOTCON_PLATFORM_INFO_FIRMWARE_VER:
373 if (platform_info->firmware_ver)
374 free(platform_info->firmware_ver);
375 platform_info->firmware_ver = dup_value;
377 case IOTCON_PLATFORM_INFO_SUPPORT_URL:
378 if (platform_info->support_url)
379 free(platform_info->support_url);
380 platform_info->support_url = dup_value;
382 case IOTCON_PLATFORM_INFO_SYSTEM_TIME:
383 if (platform_info->system_time)
384 free(platform_info->system_time);
385 platform_info->system_time = dup_value;
388 ERR("Invalid property(%d)", property);
390 return IOTCON_ERROR_INVALID_PARAMETER;
393 return IOTCON_ERROR_NONE;
397 API int iotcon_platform_info_get_property(iotcon_platform_info_h platform_info,
398 iotcon_platform_info_e property, char **value)
400 RETV_IF(NULL == platform_info, IOTCON_ERROR_INVALID_PARAMETER);
401 RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
404 case IOTCON_PLATFORM_INFO_ID:
405 *value = platform_info->platform_id;
407 case IOTCON_PLATFORM_INFO_MANUF_NAME:
408 *value = platform_info->manuf_name;
410 case IOTCON_PLATFORM_INFO_MANUF_URL:
411 *value = platform_info->manuf_url;
413 case IOTCON_PLATFORM_INFO_MODEL_NUMBER:
414 *value = platform_info->model_number;
416 case IOTCON_PLATFORM_INFO_DATE_OF_MANUF:
417 *value = platform_info->date_of_manuf;
419 case IOTCON_PLATFORM_INFO_PLATFORM_VER:
420 *value = platform_info->platform_ver;
422 case IOTCON_PLATFORM_INFO_OS_VER:
423 *value = platform_info->os_ver;
425 case IOTCON_PLATFORM_INFO_HARDWARE_VER:
426 *value = platform_info->hardware_ver;
428 case IOTCON_PLATFORM_INFO_FIRMWARE_VER:
429 *value = platform_info->firmware_ver;
431 case IOTCON_PLATFORM_INFO_SUPPORT_URL:
432 *value = platform_info->support_url;
434 case IOTCON_PLATFORM_INFO_SYSTEM_TIME:
435 *value = platform_info->system_time;
438 ERR("Invalid property(%d)", property);
439 return IOTCON_ERROR_INVALID_PARAMETER;
442 return IOTCON_ERROR_NONE;
446 /* The length of manufacturer_name should be less than and equal to 16.
447 * The length of manufacturer_url should be less than and equal to 32. */
448 API int iotcon_set_platform_info(iotcon_platform_info_h platform_info)
451 GError *error = NULL;
454 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
455 RETV_IF(NULL == platform_info, IOTCON_ERROR_INVALID_PARAMETER);
457 if (NULL == platform_info->platform_id
458 || NULL == platform_info->manuf_name
459 || NULL == platform_info->manuf_url
460 || NULL == platform_info->model_number
461 || NULL == platform_info->date_of_manuf
462 || NULL == platform_info->platform_ver
463 || NULL == platform_info->os_ver
464 || NULL == platform_info->hardware_ver
465 || NULL == platform_info->firmware_ver
466 || NULL == platform_info->support_url
467 || NULL == platform_info->system_time) {
468 ERR("one of parameter is NULL");
469 return IOTCON_ERROR_INVALID_PARAMETER;
472 if (platform_info->manuf_name
473 && (ICL_MANUFACTURER_NAME_LENGTH_MAX < strlen(platform_info->manuf_name))) {
474 ERR("The length of manufacturer_name(%s) is invalid.", platform_info->manuf_name);
475 return IOTCON_ERROR_INVALID_PARAMETER;
478 if (platform_info->manuf_url
479 && (ICL_MANUFACTURER_URL_LENGTH_MAX < strlen(platform_info->manuf_url))) {
480 ERR("The length of manufacturer_url(%s) is invalid.", platform_info->manuf_url);
481 return IOTCON_ERROR_INVALID_PARAMETER;
484 arg_info = icl_dbus_platform_info_to_gvariant(platform_info);
485 ic_dbus_call_register_platform_info_sync(icl_dbus_get_object(), arg_info, &ret,
488 ERR("ic_dbus_call_register_platform_info_sync() Fail(%s)", error->message);
489 ret = icl_dbus_convert_dbus_error(error->code);
491 g_variant_unref(arg_info);
495 if (IOTCON_ERROR_NONE != ret) {
496 ERR("iotcon-daemon Fail(%d)", ret);
497 return icl_dbus_convert_daemon_error(ret);
504 static void _icl_platform_info_cb(GDBusConnection *connection,
505 const gchar *sender_name,
506 const gchar *object_path,
507 const gchar *interface_name,
508 const gchar *signal_name,
509 GVariant *parameters,
513 struct icl_platform_info info = {0};
514 icl_platform_info_s *cb_container = user_data;
515 iotcon_platform_info_cb cb = cb_container->cb;
517 g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s&s)",
531 /* From iotivity, we can get uri_path. But, the value is always "/oic/p". */
534 cb(&info, cb_container->user_data);
538 API int iotcon_get_platform_info(const char *host_address, iotcon_platform_info_cb cb,
541 GError *error = NULL;
543 int ret, signal_number;
544 char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
545 icl_platform_info_s *cb_container;
547 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
548 RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
549 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
551 signal_number = icl_dbus_generate_signal_number();
553 ic_dbus_call_get_platform_info_sync(icl_dbus_get_object(), host_address,
554 signal_number, &ret, NULL, &error);
556 ERR("ic_dbus_call_get_platform_info_sync() Fail(%s)", error->message);
557 ret = icl_dbus_convert_dbus_error(error->code);
562 if (IOTCON_ERROR_NONE != ret) {
563 ERR("iotcon-daemon Fail(%d)", ret);
564 return icl_dbus_convert_daemon_error(ret);
567 snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_PLATFORM,
570 cb_container = calloc(1, sizeof(icl_platform_info_s));
571 if (NULL == cb_container) {
572 ERR("calloc() Fail(%d)", errno);
573 return IOTCON_ERROR_OUT_OF_MEMORY;
576 cb_container->cb = cb;
577 cb_container->user_data = user_data;
579 sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free,
580 _icl_platform_info_cb);
582 ERR("icl_dbus_subscribe_signal() Fail");
584 return IOTCON_ERROR_DBUS;
587 cb_container->id = sub_id;
593 static void _icl_tizen_info_cb(GObject *object, GAsyncResult *g_async_res,
599 char *tizen_device_id;
600 GError *error = NULL;
601 struct icl_tizen_info info = {0};
602 icl_tizen_info_s *cb_container = user_data;
603 iotcon_tizen_info_cb cb = cb_container->cb;
605 ic_dbus_call_get_tizen_info_finish(IC_DBUS(object), &result, g_async_res, &error);
607 ERR("ic_dbus_call_get_tizen_info_finish() Fail(%s)", error->message);
609 cb(&info, IOTCON_ERROR_DBUS, cb_container->user_data);
610 /* TODO contain time out error */
615 g_variant_get(result, "(&s&si)", &device_name, &tizen_device_id, &res);
617 if (IOTCON_ERROR_NONE == res && NULL != ic_utils_dbus_decode_str(tizen_device_id)) {
618 info.device_name = ic_utils_dbus_decode_str(device_name);
619 info.tizen_device_id = tizen_device_id;
623 cb(&info, res, cb_container->user_data);
629 API int iotcon_get_tizen_info(const char *host_address, iotcon_tizen_info_cb cb,
632 icl_tizen_info_s *cb_container;
634 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
635 RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
636 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
638 cb_container = calloc(1, sizeof(icl_tizen_info_s));
639 if (NULL == cb_container) {
640 ERR("calloc() Fail(%d)", errno);
641 return IOTCON_ERROR_OUT_OF_MEMORY;
644 cb_container->cb = cb;
645 cb_container->user_data = user_data;
647 ic_dbus_call_get_tizen_info(icl_dbus_get_object(), host_address, NULL,
648 _icl_tizen_info_cb, cb_container);
650 return IOTCON_ERROR_NONE;
654 API int iotcon_tizen_info_get_property(iotcon_tizen_info_h tizen_info,
655 iotcon_tizen_info_e property, char **value)
657 RETV_IF(NULL == tizen_info, IOTCON_ERROR_INVALID_PARAMETER);
658 RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
661 case IOTCON_TIZEN_INFO_DEVICE_NAME:
662 *value = tizen_info->device_name;
664 case IOTCON_TIZEN_INFO_TIZEN_DEVICE_ID:
665 *value = tizen_info->tizen_device_id;
668 ERR("Invalid property(%d)", property);
669 return IOTCON_ERROR_INVALID_PARAMETER;
672 return IOTCON_ERROR_NONE;