diff options
author | youngman <yman.jung@samsung.com> | 2016-07-12 14:05:46 +0900 |
---|---|---|
committer | youngman <yman.jung@samsung.com> | 2016-08-10 17:38:12 +0900 |
commit | 30c06c9eb9be2d1b62ea1f9c3bb680ef48c51e3e (patch) | |
tree | 1131ec76e6e9218291f6a33586ec5ee271d2613a | |
parent | e5e3c00d8b13d8144da01134f3fea6513bc2177e (diff) | |
download | iotcon-30c06c9eb9be2d1b62ea1f9c3bb680ef48c51e3e.tar.gz iotcon-30c06c9eb9be2d1b62ea1f9c3bb680ef48c51e3e.tar.bz2 iotcon-30c06c9eb9be2d1b62ea1f9c3bb680ef48c51e3e.zip |
revise provisioning tool API
Change-Id: Ia2bbc9d9139fe3797c3cf7a66e186fe82d42ae27
Signed-off-by: youngman <yman.jung@samsung.com>
-rw-r--r-- | include/iotcon-provisioning.h | 59 | ||||
-rw-r--r-- | src/ic-ioty.c | 1 | ||||
-rw-r--r-- | src/ic-provisioning-find.c | 696 | ||||
-rw-r--r-- | src/ic-provisioning-remove.c | 430 | ||||
-rw-r--r-- | src/ic-provisioning-struct.c | 484 | ||||
-rw-r--r-- | src/ic-provisioning-struct.h | 33 | ||||
-rw-r--r-- | src/ic-provisioning.c | 784 | ||||
-rw-r--r-- | src/ic-utils.c | 4 |
8 files changed, 1377 insertions, 1114 deletions
diff --git a/include/iotcon-provisioning.h b/include/iotcon-provisioning.h index d1ef840..97d6455 100644 --- a/include/iotcon-provisioning.h +++ b/include/iotcon-provisioning.h @@ -20,11 +20,16 @@ #define IOTCON_ERROR_AUTHENTICATION_FAILURE (TIZEN_ERROR_IOTCON | 0xF001) -typedef struct icl_provisioning_devices* iotcon_provisioning_devices_h; typedef struct icl_provisioning_device* iotcon_provisioning_device_h; typedef struct icl_provisioning_acl* iotcon_provisioning_acl_h; typedef enum { + IOTCON_PROVISIONING_FIND_OWNED, + IOTCON_PROVISIONING_FIND_UNOWNED, + IOTCON_PROVISIONING_FIND_ALL, +} iotcon_provisioning_find_e; + +typedef enum { IOTCON_PERMISSION_CREATE = (1 << 0), IOTCON_PERMISSION_READ = (1 << 1), IOTCON_PERMISSION_WRITE = (1 << 2), @@ -48,22 +53,12 @@ int iotcon_provisioning_set_randompin_cb(iotcon_provisioning_randompin_cb cb, void *user_data); /* discover */ -int iotcon_provisioning_get_devices(iotcon_provisioning_devices_h *owned_devices, - iotcon_provisioning_devices_h *unowned_devices); - -typedef void (*iotcon_provisioning_found_devices_cb)( - iotcon_provisioning_devices_h owned_devices, - iotcon_provisioning_devices_h unowned_devices, - void *user_data); - -int iotcon_provisioning_find_all_devices(int timeout, - iotcon_provisioning_found_devices_cb cb, void *user_data); +typedef bool (*iotcon_provisioning_found_device_cb)(iotcon_provisioning_device_h device, + iotcon_error_e result, void *user_data); -int iotcon_provisioning_find_unowned_devices(int timeout, - iotcon_provisioning_found_devices_cb cb, void *user_data); +int iotcon_provisioning_find_device(iotcon_provisioning_find_e owned, + iotcon_provisioning_found_device_cb cb, void *user_data); -int iotcon_provisioning_find_owned_devices(int timeout, - iotcon_provisioning_found_devices_cb cb, void *user_data); /* register */ typedef void (*iotcon_provisioning_ownership_transfer_cb)( @@ -71,11 +66,6 @@ typedef void (*iotcon_provisioning_ownership_transfer_cb)( iotcon_error_e result, void *user_data); -int iotcon_provisioning_register_unowned_devices( - iotcon_provisioning_devices_h devices, - iotcon_provisioning_ownership_transfer_cb cb, - void *user_data); - int iotcon_provisioning_register_unowned_device( iotcon_provisioning_device_h device, iotcon_provisioning_ownership_transfer_cb cb, @@ -132,14 +122,16 @@ int iotcon_provisioning_unlink_pairwise(iotcon_provisioning_device_h device1, void *user_data); /* remove */ -typedef void (*iotcon_provisioning_remove_device_cb)(iotcon_provisioning_device_h device, - iotcon_error_e result, void *user_data); - -int iotcon_provisioning_remove_device(int timeout, - iotcon_provisioning_device_h device, - iotcon_provisioning_remove_device_cb cb, +typedef void (*iotcon_provisioning_remove_device_cb)( + const char *device_id, + iotcon_provisioning_device_h dest_device, + iotcon_error_e result, + bool is_complete, void *user_data); +int iotcon_provisioning_remove_device(const char *device_id, + iotcon_provisioning_remove_device_cb cb, void *user_data); + /* struct */ int iotcon_provisioning_device_clone(iotcon_provisioning_device_h device, iotcon_provisioning_device_h *cloned_device); @@ -155,21 +147,6 @@ int iotcon_provisioning_device_get_oxm(iotcon_provisioning_device_h device, int iotcon_provisioning_device_is_owned(iotcon_provisioning_device_h device, bool *is_owned); -int iotcon_provisioning_devices_create(iotcon_provisioning_devices_h *devices); -int iotcon_provisioning_devices_destroy(iotcon_provisioning_devices_h devices); -int iotcon_provisioning_devices_clone(iotcon_provisioning_devices_h devices, - iotcon_provisioning_devices_h *cloned_devices); - -typedef bool (*iotcon_provisioning_devices_foreach_cb)( - iotcon_provisioning_devices_h devices, - iotcon_provisioning_device_h device, - void *user_data); -int iotcon_provisioning_devices_foreach(iotcon_provisioning_devices_h devices, - iotcon_provisioning_devices_foreach_cb cb, void *user_data); - -int iotcon_provisioning_devices_add_device(iotcon_provisioning_devices_h devices, - iotcon_provisioning_device_h device); - int iotcon_provisioning_acl_create(iotcon_provisioning_acl_h *acl); int iotcon_provisioning_acl_set_all_subject(iotcon_provisioning_acl_h acl); int iotcon_provisioning_acl_set_subject(iotcon_provisioning_acl_h acl, diff --git a/src/ic-ioty.c b/src/ic-ioty.c index ec20dc5..9c771de 100644 --- a/src/ic-ioty.c +++ b/src/ic-ioty.c @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include <stdio.h> #include <stdlib.h> #include <errno.h> diff --git a/src/ic-provisioning-find.c b/src/ic-provisioning-find.c new file mode 100644 index 0000000..01d504d --- /dev/null +++ b/src/ic-provisioning-find.c @@ -0,0 +1,696 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdlib.h> +#include <stdint.h> +#include <errno.h> +#include <glib.h> +#include <octypes.h> +#include <ocstack.h> +#include <ocprovisioningmanager.h> +#include <pmutility.h> +#include <doxmresource.h> +#include <verresource.h> + +#include "iotcon.h" +#include "iotcon-provisioning.h" +#include "ic.h" +#include "ic-utils.h" +#include "ic-ioty.h" +#include "ic-ioty-parse.h" +#include "ic-provisioning-struct.h" + +#define ICL_PROVISIONING_MUTEX_FIND_LOCK(ctx) \ + _provisioning_find_mutex_lock(ICL_PROVISIONING_MUTEX_FIND, ctx) +#define ICL_PROVISIONING_MUTEX_FIND_UNLOCK(ctx) \ + _provisioning_find_mutex_unlock(ICL_PROVISIONING_MUTEX_FIND, ctx) +#define ICL_PROVISIONING_MUTEX_INTERNAL_FIND_LOCK(ctx) \ + _provisioning_find_mutex_lock(ICL_PROVISIONING_MUTEX_INTERNAL_FIND, ctx) +#define ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(ctx) \ + _provisioning_find_mutex_unlock(ICL_PROVISIONING_MUTEX_INTERNAL_FIND, ctx) + +typedef enum { + ICL_PROVISIONING_FIND_SECURE_PORT, + ICL_PROVISIONING_FIND_SECURITY_VERSION, +} icl_provisioning_find_e; + +typedef enum { + ICL_PROVISIONING_MUTEX_FIND, + ICL_PROVISIONING_MUTEX_INTERNAL_FIND, +} icl_provisioning_mutex_e; + +typedef struct { + int ref_count; + iotcon_provisioning_found_device_cb cb; + void *user_data; + int result; + int timeout; + unsigned int timer_id; + OCDoHandle handle; + pthread_mutex_t mutex; +} icl_provisioning_find_cb_s; + +typedef struct { + unsigned int timer_id; + OCDoHandle handle; + icl_provisioning_find_cb_s *cb_data; + OCProvisionDev_t *oic_device; + iotcon_provisioning_device_h device; + pthread_mutex_t mutex; +} icl_provisioning_find_cb_container_s; + +static const char *ICL_PROVISIONING_OWNED_MULTICAST_QUERY = "/oic/sec/doxm?Owned=TRUE"; +static const char *ICL_PROVISIONING_UNOWNED_MULTICAST_QUERY = "/oic/sec/doxm?Owned=FALSE"; +static const char *ICL_PROVISIONING_ALL_MULTICAST_QUERY = "/oic/sec/doxm"; + +static const char *ICL_PROVISIONING_DEFAULT_SEC_VERSION = "0.0.0"; + +static inline pthread_mutex_t* _provisioning_find_mutex_get(int type, void *data) +{ + pthread_mutex_t *mutex; + icl_provisioning_find_cb_s *cb_data; + icl_provisioning_find_cb_container_s *container; + + switch (type) { + case ICL_PROVISIONING_MUTEX_FIND: + cb_data = data; + mutex = &cb_data->mutex; + break; + case ICL_PROVISIONING_MUTEX_INTERNAL_FIND: + container = data; + mutex = &container->mutex; + break; + default: + ERR("Invalid Type(%d)", type); + mutex = NULL; + } + + return mutex; +} + + +static void _provisioning_find_mutex_lock(int type, void *data) +{ + int ret; + + ret = pthread_mutex_lock(_provisioning_find_mutex_get(type, data)); + WARN_IF(0 != ret, "pthread_mutex_lock() Fail(%d)", ret); +} + + +static void _provisioning_find_mutex_unlock(int type, void *data) +{ + int ret; + + ret = pthread_mutex_unlock(_provisioning_find_mutex_get(type, data)); + WARN_IF(0 != ret, "pthread_mutex_unlock() Fail(%d)", ret); +} + + +static icl_provisioning_find_cb_s* _provisioning_find_cb_ref( + icl_provisioning_find_cb_s *cb_data) +{ + RETV_IF(NULL == cb_data, NULL); + RETV_IF(cb_data->ref_count <= 0, NULL); + + cb_data->ref_count++; + + return cb_data; +} + + +static int _provisioning_find_create_device(const char *addr, + const uint16_t port, + OCTransportAdapter adapter, + OCConnectivityType conn_type, + OicSecDoxm_t *doxm, + OCProvisionDev_t **device_list) +{ + OCProvisionDev_t *temp; + + RETV_IF(NULL == addr, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == doxm, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == device_list, IOTCON_ERROR_INVALID_PARAMETER); + + temp = calloc(1, sizeof(OCProvisionDev_t)); + if (NULL == temp) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + snprintf(temp->endpoint.addr, sizeof(temp->endpoint.addr), "%s", addr); + temp->endpoint.port = port; + temp->endpoint.adapter = adapter; + temp->connType = conn_type; + temp->doxm = doxm; + temp->securePort = DEFAULT_SECURE_PORT; + temp->devStatus = DEV_STATUS_ON; + snprintf(temp->secVer, sizeof(temp->secVer), "%s", + ICL_PROVISIONING_DEFAULT_SEC_VERSION); + + *device_list = temp; + + return IOTCON_ERROR_NONE; +} + + +static gboolean _provisioning_find_cb_unref(gpointer p) +{ + icl_provisioning_find_cb_s *cb_data; + + RETV_IF(NULL == p, G_SOURCE_REMOVE); + + cb_data = p; + cb_data->ref_count--; + + if (0 == cb_data->ref_count) + free(cb_data); + + return G_SOURCE_REMOVE; +} + + +static void _provisioning_free_cb_data(void *data) +{ + icl_provisioning_find_cb_s *cb_data = data; + RET_IF(NULL == cb_data); + + if (cb_data->timer_id) { + g_source_remove(cb_data->timer_id); + cb_data->timer_id = 0; + } + g_idle_add(_provisioning_find_cb_unref, cb_data); +} + + +static gboolean _provisioning_find_internal_timeout(gpointer user_data) +{ + FN_CALL; + int ret; + icl_provisioning_find_cb_container_s *container = user_data; + + RETV_IF(NULL == container, G_SOURCE_REMOVE); + + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_LOCK(container); + if (0 == container->timer_id) { + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(container); + return G_SOURCE_REMOVE; + } + + container->timer_id = 0; + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(container); + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + return G_SOURCE_REMOVE; + } + ret = OCCancel(container->handle, OC_LOW_QOS, NULL, 0); + icl_ioty_mutex_unlock(); + + if (OC_STACK_OK != ret) { + ERR("OCCancel() Fail(%d)", ret); + return G_SOURCE_REMOVE; + } + + return G_SOURCE_REMOVE; +} + + +static gboolean _provisioning_find_timeout(gpointer user_data) +{ + FN_CALL; + int ret; + icl_provisioning_find_cb_s *cb_info = user_data; + + RETV_IF(NULL == cb_info, G_SOURCE_REMOVE); + + ICL_PROVISIONING_MUTEX_FIND_LOCK(cb_info); + cb_info->timer_id = 0; + ICL_PROVISIONING_MUTEX_FIND_UNLOCK(cb_info); + + if (cb_info->cb) + cb_info->cb(NULL, IOTCON_ERROR_TIMEOUT, cb_info->user_data); + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + return ret; + } + ret = OCCancel(cb_info->handle, OC_LOW_QOS, NULL, 0); + icl_ioty_mutex_unlock(); + + if (OC_STACK_OK != ret) { + ERR("OCCancel() Fail(%d)", ret); + return G_SOURCE_REMOVE; + } + + return G_SOURCE_REMOVE; +} + + +static void _provisioning_free_find_cb_container(void *data) +{ + icl_provisioning_find_cb_container_s *container = data; + + if (container->device) { + iotcon_provisioning_device_destroy(container->device); + container->oic_device = NULL; + } + if (container->oic_device) + OCDeleteDiscoveredDevices(container->oic_device); + + g_idle_add(_provisioning_find_cb_unref, container->cb_data); + + free(container); +} + + +static gboolean _provisioning_find_idle_cb(gpointer p) +{ + FN_CALL; + int ret; + icl_provisioning_find_cb_container_s *container; + iotcon_provisioning_found_device_cb cb; + + RETV_IF(NULL == p, G_SOURCE_REMOVE); + + container = p; + + icl_provisioning_device_set_found(container->device); + if (container->cb_data->cb) { + cb = container->cb_data->cb; + if (IOTCON_FUNC_STOP == cb(container->device, IOTCON_ERROR_NONE, + container->cb_data->user_data)) { + INFO("Stop the callback"); + container->cb_data->cb = NULL; + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + icl_provisioning_device_unset_found(container->device); + _provisioning_free_find_cb_container(container); + return G_SOURCE_REMOVE; + } + + OCCancel(container->cb_data->handle, OC_LOW_QOS, NULL, 0); + icl_ioty_mutex_unlock(); + } + } + icl_provisioning_device_unset_found(container->device); + _provisioning_free_find_cb_container(container); + + return G_SOURCE_REMOVE; +} + + +static OCStackApplicationResult _provisioning_find_security_version_cb(void *ctx, + OCDoHandle handle, OCClientResponse *resp) +{ + FN_CALL; + int ret; + size_t payload_size; + uint8_t *security_data; + OicSecVer_t *ver = NULL; + icl_provisioning_find_cb_container_s *container = ctx; + + RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION); + if (NULL == resp) { + ERR("resp is NULL"); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_LOCK(container); + if (0 == container->timer_id) { + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(container); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + + g_source_remove(container->timer_id); + container->timer_id = 0; + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(container); + + if (NULL == resp->payload) { + ret = icl_provisioning_device_create(container->oic_device, &container->device); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_provisioning_device_create() Fail(%d)", ret); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + + icl_provisioning_device_print(container->device); + + g_idle_add(_provisioning_find_idle_cb, container); + return OC_STACK_DELETE_TRANSACTION; + } + + if (PAYLOAD_TYPE_SECURITY != resp->payload->type) { + ERR("Invalid payload type(%d)", resp->payload->type); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + if (OC_STACK_OK != resp->result) { + ERR("resp->result is (%d)", resp->result); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + + /* cbor payload -> ver */ + security_data = ((OCSecurityPayload*)resp->payload)->securityData; + payload_size = ((OCSecurityPayload*)resp->payload)->payloadSize; + + ret = CBORPayloadToVer(security_data, payload_size, &ver); + if (NULL == ver || OC_STACK_OK != ret) { + ERR("CBORPayloadToVer() Fail(%d)", ret); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + + snprintf(container->oic_device->secVer, sizeof(container->oic_device->secVer), + "%s", ver->secv); + + ret = icl_provisioning_device_create(container->oic_device, &container->device); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_provisioning_device_create() Fail(%d)", ret); + _provisioning_free_find_cb_container(container); + return OC_STACK_DELETE_TRANSACTION; + } + + icl_provisioning_device_print(container->device); + + g_idle_add(_provisioning_find_idle_cb, container); + + return OC_STACK_DELETE_TRANSACTION; +} + + +static int _provisioning_find_security_version( + icl_provisioning_find_cb_container_s *container) +{ + FN_CALL; + int ret; + OCCallbackData cbdata = {0}; + char *host_address; + char uri[PATH_MAX] = {0}; + const char *version_uri = "/oic/sec/ver"; + + ret = icl_provisioning_parse_oic_dev_address(&container->oic_device->endpoint, 0, + container->oic_device->connType, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_provisioning_parse_oic_dev_address() Fail(%d)", ret); + return ret; + } + + snprintf(uri, sizeof(uri), "%s%s%s", IC_IOTY_COAP, host_address, version_uri); + DBG("uri : %s", uri); + + cbdata.cb = _provisioning_find_security_version_cb; + cbdata.context = container; + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + return ret; + } + ICL_PROVISIONING_MUTEX_FIND_LOCK(container->cb_data); + if (0 == container->cb_data->timer_id) { + ICL_PROVISIONING_MUTEX_FIND_UNLOCK(container->cb_data); + icl_ioty_mutex_unlock(); + return IOTCON_ERROR_NONE; + } + ICL_PROVISIONING_MUTEX_FIND_UNLOCK(container->cb_data); + + ret = OCDoResource(&container->handle, OC_REST_DISCOVER, uri, 0, 0, + container->oic_device->connType, OC_LOW_QOS, &cbdata, NULL, 0); + icl_ioty_mutex_unlock(); + if (OC_STACK_OK != ret) { + ERR("OCDoResource() Fail(%d)", ret); + return ic_ioty_parse_oic_error(ret); + } + + /* timeout */ + container->timer_id = g_timeout_add_seconds(container->cb_data->timeout, + _provisioning_find_internal_timeout, container); + + return IOTCON_ERROR_NONE; +} + + +static OCStackApplicationResult _provisioning_find_secure_port_cb(void *ctx, + OCDoHandle handle, OCClientResponse *resp) +{ + FN_CALL; + int ret; + OCResourcePayload *payload; + icl_provisioning_find_cb_container_s *container, *context; + + RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION); + RETV_IF(NULL == resp, OC_STACK_DELETE_TRANSACTION); + RETV_IF(NULL == resp->payload, OC_STACK_DELETE_TRANSACTION); + RETV_IF(OC_STACK_OK != resp->result, OC_STACK_DELETE_TRANSACTION); + RETVM_IF(PAYLOAD_TYPE_DISCOVERY != resp->payload->type, + OC_STACK_DELETE_TRANSACTION, "Invalid payload type(%d)", resp->payload->type); + + container = ctx; + + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_LOCK(container); + if (0 == container->timer_id) { + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(container); + return OC_STACK_DELETE_TRANSACTION; + } + + g_source_remove(container->timer_id); + container->timer_id = 0; + ICL_PROVISIONING_MUTEX_INTERNAL_FIND_UNLOCK(container); + + payload = ((OCDiscoveryPayload*)resp->payload)->resources; + if (NULL == payload || false == payload->secure) { + ERR("Not find secure port"); + return OC_STACK_DELETE_TRANSACTION; + } + + container->oic_device->securePort = payload->port; + + context = calloc(1, sizeof(icl_provisioning_find_cb_container_s)); + if (NULL == context) { + ERR("calloc() Fail(%d)", errno); + return OC_STACK_DELETE_TRANSACTION; + } + context->oic_device = container->oic_device; + context->device = container->device; + context->cb_data = _provisioning_find_cb_ref(container->cb_data); + container->oic_device = NULL; + container->device = NULL; + + ret = _provisioning_find_security_version(context); + if (IOTCON_ERROR_NONE != ret) { + ERR("_provisioning_find_security_version() Fail(%d)", ret); + _provisioning_free_find_cb_container(context); + return OC_STACK_DELETE_TRANSACTION; + } + + return OC_STACK_DELETE_TRANSACTION; +} + + +static int _provisioning_find_secure_port(icl_provisioning_find_cb_container_s *container) +{ + FN_CALL; + int ret; + char *host_address; + char uri[PATH_MAX] = {0}; + OCCallbackData cbdata = {0}; + + ret = icl_provisioning_parse_oic_dev_address(&container->oic_device->endpoint, 0, + container->oic_device->connType, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_provisioning_parse_oic_dev_address() Fail(%d)", ret); + return ret; + } + + snprintf(uri, sizeof(uri), "%s%s%s", IC_IOTY_COAP, host_address, + OC_RSRVD_WELL_KNOWN_URI); + + cbdata.cb = _provisioning_find_secure_port_cb; + cbdata.cd = _provisioning_free_find_cb_container; + cbdata.context = container; + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + return ret; + } + ICL_PROVISIONING_MUTEX_FIND_LOCK(container->cb_data); + if (0 == container->cb_data->timer_id) { + ICL_PROVISIONING_MUTEX_FIND_UNLOCK(container->cb_data); + icl_ioty_mutex_unlock(); + return IOTCON_ERROR_NONE; + } + ICL_PROVISIONING_MUTEX_FIND_UNLOCK(container->cb_data); + + ret = OCDoResource(&container->handle, OC_REST_DISCOVER, uri, 0, 0, + container->oic_device->connType, OC_LOW_QOS, &cbdata, NULL, 0); + icl_ioty_mutex_unlock(); + if (OC_STACK_OK != ret) { + ERR("OCDoResource() Fail(%d)", ret); + return ic_ioty_parse_oic_error(ret); + } + + /* timeout */ + container->timer_id = g_timeout_add_seconds(container->cb_data->timeout, + _provisioning_find_internal_timeout, container); + + return IOTCON_ERROR_NONE; +} + + +static OCStackApplicationResult _provisioning_find_cb(void *ctx, OCDoHandle handle, + OCClientResponse *resp) +{ + FN_CALL; + int ret; + OicUuid_t device_id; + size_t payload_size; + uint8_t *security_data; + OicSecDoxm_t *doxm = NULL; + icl_provisioning_find_cb_container_s *container; + + RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION); + RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION); + if (NULL == resp->payload) + return OC_STACK_KEEP_TRANSACTION; + RETV_IF(OC_STACK_OK != resp->result, OC_STACK_KEEP_TRANSACTION); + RETVM_IF(PAYLOAD_TYPE_SECURITY != resp->payload->type, + OC_STACK_KEEP_TRANSACTION, "Invalid payload type(%d)", resp->payload->type); + + /* cbor payload -> doxm */ + security_data = ((OCSecurityPayload*)resp->payload)->securityData; + payload_size = ((OCSecurityPayload*)resp->payload)->payloadSize; + + ret = CBORPayloadToDoxm(security_data, payload_size, &doxm); + if (NULL == doxm || OC_STACK_OK != ret) { + ERR("CBORPayloadToDoxm() Fail(%d)", ret); + return OC_STACK_KEEP_TRANSACTION; + } + + /* device id */ + ret = GetDoxmDevOwnerId(&device_id); + if (OC_STACK_OK != ret) { + ERR("GetDoxmDevOwnerId() Fail(%d)", ret); + DeleteDoxmBinData(doxm); + return OC_STACK_KEEP_TRANSACTION; + } + + /* check whether the device is not mine */ + if (true == doxm->owned && + IC_EQUAL != memcmp(&doxm->owner.id, &device_id.id, sizeof(device_id.id))) { + DBG("discovered device is not owned by me"); + DeleteDoxmBinData(doxm); + return OC_STACK_KEEP_TRANSACTION; + } + + container = calloc(1, sizeof(icl_provisioning_find_cb_container_s)); + if (NULL == container) { + ERR("calloc() Fail(%d)", errno); + DeleteDoxmBinData(doxm); + return OC_STACK_KEEP_TRANSACTION; + } + + container->cb_data = _provisioning_find_cb_ref(ctx); + + ret = _provisioning_find_create_device(resp->devAddr.addr, resp->devAddr.port, + resp->devAddr.adapter, resp->connType, doxm, &container->oic_device); + if (OC_STACK_OK != ret) { + ERR("_provisioning_find_create_device() Fail(%d)", ret); + DeleteDoxmBinData(doxm); + _provisioning_free_find_cb_container(container); + return OC_STACK_KEEP_TRANSACTION; + } + + ret = _provisioning_find_secure_port(container); + if (IOTCON_ERROR_NONE != ret) { + ERR("_provisioning_find_secure_port() Fail(%d)", ret); + _provisioning_free_find_cb_container(container); + return OC_STACK_KEEP_TRANSACTION; + } + + return OC_STACK_KEEP_TRANSACTION; +} + + +API int iotcon_provisioning_find_device(iotcon_provisioning_find_e type, + iotcon_provisioning_found_device_cb cb, void *user_data) +{ + FN_CALL; + int ret; + const char *query; + OCCallbackData cbdata = {0}; + icl_provisioning_find_cb_s *cb_data; + + RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + switch (type) { + case IOTCON_PROVISIONING_FIND_OWNED: + query = ICL_PROVISIONING_OWNED_MULTICAST_QUERY; + break; + case IOTCON_PROVISIONING_FIND_UNOWNED: + query = ICL_PROVISIONING_UNOWNED_MULTICAST_QUERY; + break; + case IOTCON_PROVISIONING_FIND_ALL: + query = ICL_PROVISIONING_ALL_MULTICAST_QUERY; + break; + default: + ERR("Invalid Type(%d)", type); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + cb_data = calloc(1, sizeof(icl_provisioning_find_cb_s)); + if (NULL == cb_data) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + cb_data->cb = cb; + cb_data->user_data = user_data; + cb_data->ref_count = 1; + + cbdata.cb = _provisioning_find_cb; + cbdata.cd = _provisioning_free_cb_data; + cbdata.context = cb_data; + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + _provisioning_free_cb_data(cb_data); + return ret; + } + ret = OCDoResource(&cb_data->handle, OC_REST_DISCOVER, query, 0, 0, CT_DEFAULT, + OC_LOW_QOS, &cbdata, NULL, 0); + icl_ioty_mutex_unlock(); + if (OC_STACK_OK != ret) { + ERR("OCDoResource() Fail(%d)", ret); + _provisioning_free_cb_data(cb_data); + return ic_ioty_parse_oic_error(ret); + } + + /* timeout */ + iotcon_get_timeout(&cb_data->timeout); + cb_data->timer_id = g_timeout_add_seconds(cb_data->timeout, + _provisioning_find_timeout, cb_data); + + return IOTCON_ERROR_NONE; +} diff --git a/src/ic-provisioning-remove.c b/src/ic-provisioning-remove.c new file mode 100644 index 0000000..ae038da --- /dev/null +++ b/src/ic-provisioning-remove.c @@ -0,0 +1,430 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdlib.h> +#include <stdint.h> +#include <errno.h> +#include <glib.h> +#include <octypes.h> +#include <ocstack.h> +#include <pmutility.h> +#include <provisioningdatabasemanager.h> + +#include "iotcon.h" +#include "iotcon-provisioning.h" +#include "ic.h" +#include "ic-ioty.h" +#include "ic-utils.h" +#include "ic-ioty-parse.h" +#include "ic-provisioning-struct.h" + +#define ICL_PROVISIONING_REMOVE_TIMEOUT -1 + +typedef struct { + int result; + int ref_count; + char *device_id; + iotcon_provisioning_remove_device_cb cb; + void *user_data; + size_t num_of_devices; + OCUuidList_t *linked_devices; +} icl_provisioning_remove_cb_container_s; + +typedef struct { + int timer_id; + iotcon_provisioning_device_h dest_device; + icl_provisioning_remove_cb_container_s *cb_data; +} icl_provisioning_remove_delete_container_s; + +static icl_provisioning_remove_cb_container_s* _provisioning_remove_cb_ref( + icl_provisioning_remove_cb_container_s *container) +{ + RETV_IF(NULL == container, NULL); + RETV_IF(container->ref_count <= 0, NULL); + + container->ref_count++; + + return container; +} + + +static void icl_provisioning_free_remove_cb_container( + icl_provisioning_remove_cb_container_s *container, bool is_complete) +{ + int ret; + OicUuid_t *uuid; + + RET_IF(NULL == container); + + container->ref_count--; + + if (0 < container->ref_count) + return; + + if (true == is_complete) { + uuid = icl_provisioning_convert_device_id(container->device_id); + + ret = PDMDeleteDevice(uuid); + if (OC_STACK_OK != ret) + ERR("PDMDeleteDevice() Fail(%d)", ret); + + free(uuid); + } + free(container->device_id); + + PDMDestoryOicUuidLinkList(container->linked_devices); + + free(container); +} + + +static void icl_provisioning_free_remove_delete_container( + icl_provisioning_remove_delete_container_s *container, bool is_complete) +{ + FN_CALL; + + RET_IF(NULL == container); + + iotcon_provisioning_device_destroy(container->dest_device); + icl_provisioning_free_remove_cb_container(container->cb_data, is_complete); + + free(container); +} + + +static gboolean _provisioning_remove_idle_cb(gpointer p) +{ + FN_CALL; + bool is_complete; + icl_provisioning_remove_delete_container_s *container; + + RETV_IF(NULL == p, G_SOURCE_REMOVE); + + container = p; + + if (0 == --container->cb_data->num_of_devices) + is_complete = true; + else + is_complete = false; + + if (container->cb_data->cb) { + container->cb_data->cb(container->cb_data->device_id, container->dest_device, + IOTCON_ERROR_NONE, is_complete, container->cb_data->user_data); + } + + icl_provisioning_free_remove_delete_container(container, is_complete); + + return G_SOURCE_REMOVE; +} + + +static gboolean _provisioning_remove_fail_idle_cb(gpointer p) +{ + FN_CALL; + bool is_complete; + int num_of_devices; + icl_provisioning_remove_delete_container_s *container; + + RETV_IF(NULL == p, G_SOURCE_REMOVE); + + container = p; + + num_of_devices = --container->cb_data->num_of_devices; + + if (IOTCON_ERROR_TIMEOUT == container->cb_data->result) { + is_complete = true; + + if (num_of_devices < 0) { + icl_provisioning_free_remove_delete_container(container, is_complete); + return G_SOURCE_REMOVE; + } + if (container->cb_data->cb) { + container->cb_data->cb(container->cb_data->device_id, NULL, + IOTCON_ERROR_TIMEOUT, is_complete, container->cb_data->user_data); + } + } else { + if (0 == num_of_devices) + is_complete = true; + else + is_complete = false; + + if (container->cb_data->cb) { + container->cb_data->cb(container->cb_data->device_id, container->dest_device, + container->cb_data->result, is_complete, container->cb_data->user_data); + } + } + + icl_provisioning_free_remove_delete_container(container, is_complete); + + return G_SOURCE_REMOVE; +} + + +static OCStackApplicationResult _provisioning_remove_device_delete_cb(void *ctx, + OCDoHandle handle, OCClientResponse *resp) +{ + int ret; + OicUuid_t *uuid; + OicUuid_t device_id = {{0}}; + icl_provisioning_remove_delete_container_s *container = ctx; + + RETV_IF(NULL == container, OC_STACK_DELETE_TRANSACTION); + + if (ICL_PROVISIONING_REMOVE_TIMEOUT == container->timer_id) { + icl_provisioning_free_remove_delete_container(container, true); + return OC_STACK_DELETE_TRANSACTION; + } + + if (container->timer_id) { + g_source_remove(container->timer_id); + container->timer_id = 0; + } + + if (NULL == resp) { + ERR("resp is NULL"); + container->cb_data->result = IOTCON_ERROR_IOTIVITY; + g_idle_add(_provisioning_remove_fail_idle_cb, container); + return OC_STACK_DELETE_TRANSACTION; + } + + if (OC_STACK_RESOURCE_DELETED != resp->result) { + ERR("_provisioning_remove_device_delete() Fail(%d)", resp->result); + container->cb_data->result = IOTCON_ERROR_IOTIVITY; + g_idle_add(_provisioning_remove_fail_idle_cb, container); + return OC_STACK_DELETE_TRANSACTION; + } + + memcpy(device_id.id, resp->identity.id, sizeof(device_id.id)); + + uuid = icl_provisioning_convert_device_id(container->cb_data->device_id); + + ret = PDMUnlinkDevices(uuid, &device_id); + if (OC_STACK_OK != ret) { + ERR("PDMUnlinkDevices() Fail(%d)", ret); + container->cb_data->result = IOTCON_ERROR_IOTIVITY; + g_idle_add(_provisioning_remove_fail_idle_cb, container); + free(uuid); + return OC_STACK_DELETE_TRANSACTION; + } + free(uuid); + + /* user cb */ + g_idle_add(_provisioning_remove_idle_cb, container); + + return OC_STACK_DELETE_TRANSACTION; +} + + +static gboolean _provisioning_remove_device_delete_timeout(gpointer p) +{ + icl_provisioning_remove_delete_container_s *container = p; + + RETV_IF(NULL == container, G_SOURCE_REMOVE); + RETV_IF(NULL == container->cb_data, G_SOURCE_REMOVE); + + container->timer_id = ICL_PROVISIONING_REMOVE_TIMEOUT; + + return G_SOURCE_REMOVE; +} + + +static int _provisioning_remove_device_delete( + icl_provisioning_remove_delete_container_s *container) +{ + int ret, timeout; + char *host_address; + char uri[PATH_MAX] = {0}; + OCProvisionDev_t *device; + OCCallbackData cbdata = {0}; + const char *cred_uri = "/oic/sec/cred"; + const char *subject_uuid = "subjectuuid"; + + device = icl_provisioning_device_get_device(container->dest_device); + + ret = icl_provisioning_parse_oic_dev_address(&device->endpoint, device->securePort, + device->connType, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_provisioning_parse_oic_dev_address() Fail(%d)", ret); + return ret; + } + + snprintf(uri, sizeof(uri), "%s%s%s?%s=%s", IC_IOTY_COAPS, host_address, cred_uri, + subject_uuid, container->cb_data->device_id); + + free(host_address); + + cbdata.cb = _provisioning_remove_device_delete_cb; + cbdata.context = container; + + ret = icl_ioty_mutex_lock(); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_mutex_lock() Fail(%d)", ret); + return ret; + } + ret = OCDoResource(NULL, OC_REST_DELETE, uri, &device->endpoint, NULL, + device->connType, OC_HIGH_QOS, &cbdata, NULL, 0); + icl_ioty_mutex_unlock(); + if (OC_STACK_OK != ret) { + ERR("OCDoResource() Fail(%d)", ret); + return ic_ioty_parse_oic_error(ret); + } + + /* timeout */ + iotcon_get_timeout(&timeout); + container->timer_id = g_timeout_add_seconds(timeout, + _provisioning_remove_device_delete_timeout, container); + + return IOTCON_ERROR_NONE; +} + + +static bool _provisioning_remove_device_found_cb(iotcon_provisioning_device_h device, + iotcon_error_e result, void *user_data) +{ + FN_CALL; + int ret; + OCUuidList_t *cur; + OCProvisionDev_t *found_device; + icl_provisioning_remove_cb_container_s *cb_data = user_data; + icl_provisioning_remove_delete_container_s *container; + + container = calloc(1, sizeof(icl_provisioning_remove_delete_container_s)); + if (NULL == container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_FUNC_CONTINUE; + } + container->cb_data = _provisioning_remove_cb_ref(cb_data); + + if (IOTCON_ERROR_TIMEOUT == result) { + container->cb_data->result = IOTCON_ERROR_TIMEOUT; + g_idle_add(_provisioning_remove_fail_idle_cb, container); + return IOTCON_FUNC_STOP; + } + + found_device = icl_provisioning_device_get_device(device); + + for (cur = cb_data->linked_devices; cur; cur = cur->next) { + if (IC_EQUAL == memcmp(found_device->doxm->deviceID.id, cur->dev.id, + sizeof(cur->dev.id))) { + ret = iotcon_provisioning_device_clone(device, &container->dest_device); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_clone() Fail"); + icl_provisioning_free_remove_delete_container(container, false); + return IOTCON_FUNC_CONTINUE; + } + + ret = _provisioning_remove_device_delete(container); + if (IOTCON_ERROR_NONE != ret) { + ERR("_provisioning_remove_device_delete() Fail(%d)", ret); + icl_provisioning_free_remove_delete_container(container, false); + return IOTCON_FUNC_CONTINUE; + } + } + } + + return IOTCON_FUNC_CONTINUE; +} + + +static gboolean _provisioning_remove_complete_idle_cb(gpointer p) +{ + FN_CALL; + bool is_complete; + icl_provisioning_remove_cb_container_s *container; + + RETV_IF(NULL == p, G_SOURCE_REMOVE); + + is_complete = true; + + container = p; + + if (container->cb) { + container->cb(container->device_id, NULL, IOTCON_ERROR_NONE, is_complete, + container->user_data); + } + + icl_provisioning_free_remove_cb_container(container, is_complete); + + return G_SOURCE_REMOVE; +} + + +API int iotcon_provisioning_remove_device(const char *device_id, + iotcon_provisioning_remove_device_cb cb, void *user_data) +{ + FN_CALL; + int ret; + OicUuid_t *uuid; + OCUuidList_t *cur; + icl_provisioning_remove_cb_container_s *container; + + RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); + RETV_IF(NULL == device_id, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + container = calloc(1, sizeof(icl_provisioning_remove_cb_container_s)); + if (NULL == container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + + container->cb = cb; + container->user_data = user_data; + container->device_id = strdup(device_id); + container->ref_count = 1; + + uuid = icl_provisioning_convert_device_id(container->device_id); + + /* Get Linked devices */ + ret = PDMGetLinkedDevices(uuid, &container->linked_devices, + &container->num_of_devices); + if (OC_STACK_OK != ret) { + ERR("PDMGetLinkedDevices() Fail(%d)", ret); + free(uuid); + icl_provisioning_free_remove_cb_container(container, false); + return ic_ioty_parse_oic_error(ret); + } + + if (0 == container->num_of_devices) { + ERR("No Linked devices"); + free(uuid); + g_idle_add(_provisioning_remove_complete_idle_cb, container); + return IOTCON_ERROR_NONE; + } + + /* link status : active -> stale */ + for (cur = container->linked_devices; cur; cur = cur->next) { + ret = PDMSetLinkStale(&cur->dev, uuid); + if (OC_STACK_OK != ret) { + ERR("PDMSetLinkStale() Fail(%d)", ret); + free(uuid); + icl_provisioning_free_remove_cb_container(container, false); + return ic_ioty_parse_oic_error(ret); + } + } + free(uuid); + + /* Find owned devices */ + ret = iotcon_provisioning_find_device(IOTCON_PROVISIONING_FIND_OWNED, + _provisioning_remove_device_found_cb, container); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_find_device() Fail(%d)", ret); + icl_provisioning_free_remove_cb_container(container, false); + return ret; + } + + return IOTCON_ERROR_NONE; +} + diff --git a/src/ic-provisioning-struct.c b/src/ic-provisioning-struct.c index 902175d..5d0db57 100644 --- a/src/ic-provisioning-struct.c +++ b/src/ic-provisioning-struct.c @@ -40,21 +40,18 @@ struct icl_provisioning_device { char *host_address; int connectivity_type; char *device_id; -}; - -struct icl_provisioning_devices { - bool is_found; - OCProvisionDev_t *dev_list; + int ref_count; }; struct icl_provisioning_acl { OCProvisionDev_t *device; GList *resource_list; int permission; + int ref_count; }; -static char* _provisioning_parse_uuid(OicUuid_t *uuid) +char* _provisioning_parse_uuid(OicUuid_t *uuid) { char uuid_string[256] = {0}; @@ -65,11 +62,114 @@ static char* _provisioning_parse_uuid(OicUuid_t *uuid) (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); + DBG("uuid : %s", uuid_string); + return strdup(uuid_string); } -static OCProvisionDev_t* _provisioning_device_clone(OCProvisionDev_t *src) +void icl_provisioning_print_uuid(OicUuid_t *uuid) +{ + DBG("uuid : %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], + (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], + (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], + (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); +} + + +OicUuid_t* icl_provisioning_convert_device_id(const char *device_id) +{ + OicUuid_t *uuid; + + RETV_IF(NULL == device_id, NULL); + + uuid = calloc(1, sizeof(struct OicUuid)); + if (NULL == uuid) { + ERR("calloc() Fail(%d)", errno); + return NULL; + } + + sscanf(&device_id[0], "%2hhx", &uuid->id[0]); + sscanf(&device_id[2], "%2hhx", &uuid->id[1]); + sscanf(&device_id[4], "%2hhx", &uuid->id[2]); + sscanf(&device_id[6], "%2hhx", &uuid->id[3]); + /* device_id[8] == '-' */ + sscanf(&device_id[9], "%2hhx", &uuid->id[4]); + sscanf(&device_id[11], "%2hhx", &uuid->id[5]); + /* device_id[13] == '-' */ + sscanf(&device_id[14], "%2hhx", &uuid->id[6]); + sscanf(&device_id[16], "%2hhx", &uuid->id[7]); + /* device_id[18] == '-' */ + sscanf(&device_id[19], "%2hhx", &uuid->id[8]); + sscanf(&device_id[21], "%2hhx", &uuid->id[9]); + /* device_id[23] == '-' */ + sscanf(&device_id[24], "%2hhx", &uuid->id[10]); + sscanf(&device_id[26], "%2hhx", &uuid->id[11]); + sscanf(&device_id[28], "%2hhx", &uuid->id[12]); + sscanf(&device_id[30], "%2hhx", &uuid->id[13]); + sscanf(&device_id[32], "%2hhx", &uuid->id[14]); + sscanf(&device_id[34], "%2hhx", &uuid->id[15]); + + icl_provisioning_print_uuid(uuid); + + return uuid; +} + + +bool icl_provisioning_compare_oic_uuid(OicUuid_t *a, OicUuid_t *b) +{ + int i; + bool ret = true; + + RETV_IF(NULL == a, false); + RETV_IF(NULL == b, false); + + for (i = 0; i < UUID_LENGTH; i++) { + if ((*a).id[i] != (*b).id[i]) { + ret = false; + break; + } + } + return ret; +} + + +int icl_provisioning_parse_oic_dev_address(OCDevAddr *dev_addr, int secure_port, + OCConnectivityType conn_type, char **host_address) +{ + int port; + char temp[PATH_MAX] = {0}; + + RETV_IF(NULL == dev_addr, IOTCON_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == host_address ,IOTCON_ERROR_INVALID_PARAMETER); + + if (0 == secure_port) + port = dev_addr->port; + else + port = secure_port; + + if (CT_ADAPTER_IP & conn_type) { + if (CT_IP_USE_V4 & conn_type) { + snprintf(temp, sizeof(temp), "%s:%d", dev_addr->addr, port); + } else if (CT_IP_USE_V6 & conn_type) { + snprintf(temp, sizeof(temp), "[%s]:%d", dev_addr->addr, port); + } else { + ERR("Invalid Connectivity Type(%d)", conn_type); + return IOTCON_ERROR_INVALID_PARAMETER; + } + } else { + ERR("Invalid Connectivity Type(%d)", conn_type); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + *host_address = strdup(temp); + + return IOTCON_ERROR_NONE; +} + + +OCProvisionDev_t* icl_provisioning_device_clone(OCProvisionDev_t *src) { FN_CALL; @@ -116,15 +216,6 @@ static OCProvisionDev_t* _provisioning_device_clone(OCProvisionDev_t *src) return clone; } -/* -void icl_provisioning_device_destroy(OCProvisionDev_t *src) -{ - FN_CALL; - - OCDeleteDiscoveredDevices(src); -} -*/ - static int _provisioning_device_get_host_address(OCProvisionDev_t *device, char **host_address, int *connectivity_type) @@ -168,7 +259,7 @@ static int _provisioning_device_get_host_address(OCProvisionDev_t *device, } -static int _provisioning_device_create(OCProvisionDev_t *device, +int icl_provisioning_device_create(OCProvisionDev_t *device, iotcon_provisioning_device_h *ret_device) { FN_CALL; @@ -183,9 +274,9 @@ static int _provisioning_device_create(OCProvisionDev_t *device, return IOTCON_ERROR_OUT_OF_MEMORY; } - temp->device = _provisioning_device_clone(device); + temp->device = icl_provisioning_device_clone(device); if (NULL == temp->device) { - ERR("_provisioning_device_clone() Fail"); + ERR("icl_provisioning_device_clone() Fail"); free(temp); return IOTCON_ERROR_OUT_OF_MEMORY; } @@ -201,6 +292,8 @@ static int _provisioning_device_create(OCProvisionDev_t *device, temp->device_id = _provisioning_parse_uuid(&device->doxm->deviceID); + temp->ref_count = 1; + *ret_device = temp; return IOTCON_ERROR_NONE; @@ -218,9 +311,9 @@ API int iotcon_provisioning_device_clone(iotcon_provisioning_device_h device, RETV_IF(NULL == device, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cloned_device, IOTCON_ERROR_INVALID_PARAMETER); - ret = _provisioning_device_create(device->device, &temp); + ret = icl_provisioning_device_create(device->device, &temp); if (IOTCON_ERROR_NONE != ret) { - ERR("_provisioning_device_create() Fail(%d)", ret); + ERR("icl_provisioning_device_create() Fail(%d)", ret); return ret; } @@ -230,6 +323,17 @@ API int iotcon_provisioning_device_clone(iotcon_provisioning_device_h device, } +iotcon_provisioning_device_h icl_provisioning_device_ref( + iotcon_provisioning_device_h device) +{ + RETV_IF(NULL == device, NULL); + + device->ref_count++; + + return device; +} + + API int iotcon_provisioning_device_destroy(iotcon_provisioning_device_h device) { FN_CALL; @@ -239,9 +343,14 @@ API int iotcon_provisioning_device_destroy(iotcon_provisioning_device_h device) if (true == device->is_found) { ERR("It can't be destroyed by user."); + ERR("host address : %s", device->host_address); return IOTCON_ERROR_INVALID_PARAMETER; } + device->ref_count--; + if (0 < device->ref_count) + return IOTCON_ERROR_NONE; + free(device->host_address); free(device->device_id); @@ -271,6 +380,14 @@ void icl_provisioning_device_unset_found(iotcon_provisioning_device_h device) } +bool icl_provisioning_device_is_found(iotcon_provisioning_device_h device) +{ + RETV_IF(NULL == device, false); + + return device->is_found; +} + + OCProvisionDev_t* icl_provisioning_device_get_device( iotcon_provisioning_device_h device) { @@ -395,314 +512,62 @@ void icl_provisioning_device_set_owned(iotcon_provisioning_device_h device) } -int icl_provisioning_devices_create(OCProvisionDev_t *dev_list, - iotcon_provisioning_devices_h *devices) -{ - FN_CALL; - iotcon_provisioning_devices_h temp; - - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - - temp = calloc(1, sizeof(struct icl_provisioning_devices)); - if (NULL == temp) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - temp->dev_list = dev_list; - - *devices = temp; - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_provisioning_devices_create(iotcon_provisioning_devices_h *devices) -{ - FN_CALL; - int ret; - iotcon_provisioning_devices_h temp; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - - ret = icl_provisioning_devices_create(NULL, &temp); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_devices_create() Fail(%d)", ret); - return ret; - } - - *devices = temp; - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_provisioning_devices_destroy(iotcon_provisioning_devices_h devices) +void icl_provisioning_device_print(iotcon_provisioning_device_h device) { - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - - if (true == devices->is_found) { - ERR("It can't be destroyed by user."); - return IOTCON_ERROR_INVALID_PARAMETER; - } - - if (devices->dev_list) - OCDeleteDiscoveredDevices(devices->dev_list); - - free(devices); - - return IOTCON_ERROR_NONE; -} - - -OCProvisionDev_t* icl_provisioning_devices_clone(OCProvisionDev_t *src) -{ - OCProvisionDev_t *clone; - OCProvisionDev_t *current; - - if (NULL == src) - return NULL; - - clone = _provisioning_device_clone(src); - if (NULL == clone) { - ERR("_provisioning_device_clone() Fail"); - return NULL; - } - - current = clone; - src = src->next; + RET_IF(NULL == device); - for (; src; src = src->next, current = current->next) { - current->next = _provisioning_device_clone(src); - if (NULL == current->next) { - ERR("_provisioning_device_clone() Fail"); - OCDeleteDiscoveredDevices(clone); - return NULL; - } - } + INFO("Device Information"); + INFO("host address : %s", device->host_address); + INFO("device ID : %s", device->device_id); - return clone; + if (device->device && device->device->secVer) + INFO("security version : %s", device->device->secVer); } -API int iotcon_provisioning_devices_clone(iotcon_provisioning_devices_h devices, - iotcon_provisioning_devices_h *cloned_devices) +API int iotcon_provisioning_acl_create(iotcon_provisioning_acl_h *acl) { - int ret; - iotcon_provisioning_devices_h temp; + iotcon_provisioning_acl_h temp; RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == devices->dev_list, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cloned_devices, IOTCON_ERROR_INVALID_PARAMETER); - - ret = iotcon_provisioning_devices_create(&temp); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_create() Fail(%d)", ret); - return ret; - } + RETV_IF(NULL == acl, IOTCON_ERROR_INVALID_PARAMETER); - temp->dev_list = icl_provisioning_devices_clone(devices->dev_list); - if (NULL == temp->dev_list) { - ERR("icl_provisioning_devices_clone() Fail"); - free(temp); + temp = calloc(1, sizeof(struct icl_provisioning_acl)); + if (NULL == temp) { + ERR("calloc() Fail(%d)", errno); return IOTCON_ERROR_OUT_OF_MEMORY; } - *cloned_devices = temp; - - return IOTCON_ERROR_NONE; -} - - -void icl_provisioning_devices_set_found(iotcon_provisioning_devices_h devices) -{ - if (NULL == devices) - return; - - devices->is_found = true; -} - - -void icl_provisioning_devices_unset_found(iotcon_provisioning_devices_h devices) -{ - if (NULL == devices) - return; - - devices->is_found = false; -} - - -API int iotcon_provisioning_devices_foreach(iotcon_provisioning_devices_h devices, - iotcon_provisioning_devices_foreach_cb cb, void *user_data) -{ - int ret; - OCProvisionDev_t *current; - iotcon_provisioning_device_h device; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - current = devices->dev_list; - - for (; current; current = current->next) { - ret = _provisioning_device_create(current, &device); - if (IOTCON_ERROR_NONE != ret) { - ERR("_provisioning_device_create() Fail(%d)", ret); - return ret; - } - - if (IOTCON_FUNC_STOP == cb(devices, device, user_data)) { - iotcon_provisioning_device_destroy(device); - break; - } - iotcon_provisioning_device_destroy(device); - } - - return IOTCON_ERROR_NONE; -} - - -OCProvisionDev_t* icl_provisioning_devices_get_devices( - iotcon_provisioning_devices_h devices) -{ - FN_CALL; - RETV_IF(NULL == devices, NULL); - - icl_provisioning_devices_print_uuid(devices); - - return devices->dev_list; -} - - -bool icl_provisioning_compare_oic_uuid(OicUuid_t *a, OicUuid_t *b) -{ - int i; - bool ret = true; - - RETV_IF(NULL == a, false); - RETV_IF(NULL == b, false); - - for (i = 0; i < UUID_LENGTH; i++) { - if ((*a).id[i] != (*b).id[i]) { - ret = false; - break; - } - } - return ret; -} - - -void icl_provisioning_devices_move_device(OicUuid_t *a, - iotcon_provisioning_devices_h unowned_devices, - iotcon_provisioning_devices_h owned_devices) -{ - FN_CALL; - OCProvisionDev_t *owned_dev_list = owned_devices->dev_list; - OCProvisionDev_t *previous; - OCProvisionDev_t *current; - - previous = NULL; - current = icl_provisioning_devices_get_devices(unowned_devices); - - for (; current; previous = current, current = current->next) { - if (false == icl_provisioning_compare_oic_uuid(a, ¤t->doxm->deviceID)) - continue; - - if (previous) - previous->next = current->next; - else - unowned_devices->dev_list = current->next; - - current->next = owned_dev_list; - owned_devices->dev_list = current; - - current->doxm->owned = true; - - return; - } -} - + temp->ref_count = 1; -API int iotcon_provisioning_devices_add_device(iotcon_provisioning_devices_h devices, - iotcon_provisioning_device_h device) -{ - FN_CALL; - OCProvisionDev_t *current; - OCProvisionDev_t *dev_list; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == device, IOTCON_ERROR_INVALID_PARAMETER); - - dev_list = devices->dev_list; - - current = icl_provisioning_devices_get_devices(devices); - for (; current; current = current->next) { - if (true == icl_provisioning_compare_oic_uuid(¤t->doxm->deviceID, - &device->device->doxm->deviceID)) { - ERR("%s is already contained.", device->device_id); - return IOTCON_ERROR_ALREADY; - } - } - - current = _provisioning_device_clone(device->device); - if (NULL == current) { - ERR("_provisioning_device_clone() Fail"); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - if (NULL == dev_list) - devices->dev_list = current; - else { - while (dev_list->next) - dev_list = dev_list->next; - dev_list->next = current; - } + *acl = temp; return IOTCON_ERROR_NONE; } -static void _provisioning_print_uuid(OicUuid_t *uuid) -{ - DBG("Device ID : %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], - (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], - (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], - (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); -} - - -void icl_provisioning_devices_print_uuid(iotcon_provisioning_devices_h devices) +iotcon_provisioning_acl_h icl_provisioning_acl_ref(iotcon_provisioning_acl_h acl) { - OCProvisionDev_t *dev_list; + RETV_IF(NULL == acl, NULL); - if (NULL == devices) - return; + acl->ref_count++; - for (dev_list = devices->dev_list; dev_list; dev_list = dev_list->next) - _provisioning_print_uuid(&dev_list->doxm->deviceID); + return acl; } -API int iotcon_provisioning_acl_create(iotcon_provisioning_acl_h *acl) +API int iotcon_provisioning_acl_destroy(iotcon_provisioning_acl_h acl) { - iotcon_provisioning_acl_h temp; - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); RETV_IF(NULL == acl, IOTCON_ERROR_INVALID_PARAMETER); - temp = calloc(1, sizeof(struct icl_provisioning_acl)); - if (NULL == temp) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } + acl->ref_count--; + if (0 < acl->ref_count) + return IOTCON_ERROR_NONE; - *acl = temp; + OCDeleteDiscoveredDevices(acl->device); + g_list_free_full(acl->resource_list, free); + free(acl); return IOTCON_ERROR_NONE; } @@ -730,9 +595,9 @@ int icl_provisioning_acl_clone(iotcon_provisioning_acl_h acl, return ret; } - temp->device = _provisioning_device_clone(acl->device); + temp->device = icl_provisioning_device_clone(acl->device); if (NULL == temp->device) { - ERR("_provisioning_device_clone() Fail"); + ERR("icl_provisioning_device_clone() Fail"); iotcon_provisioning_acl_destroy(temp); return IOTCON_ERROR_OUT_OF_MEMORY; } @@ -757,9 +622,9 @@ API int iotcon_provisioning_acl_set_subject(iotcon_provisioning_acl_h acl, RETV_IF(NULL == acl, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == device, IOTCON_ERROR_INVALID_PARAMETER); - dev = _provisioning_device_clone(device->device); + dev = icl_provisioning_device_clone(device->device); if (NULL == dev) { - ERR("_provisioning_device_clone() Fail"); + ERR("icl_provisioning_device_clone() Fail"); return IOTCON_ERROR_OUT_OF_MEMORY; } @@ -851,19 +716,6 @@ char* icl_provisioning_acl_get_nth_resource(iotcon_provisioning_acl_h acl, int i } -API int iotcon_provisioning_acl_destroy(iotcon_provisioning_acl_h acl) -{ - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == acl, IOTCON_ERROR_INVALID_PARAMETER); - - OCDeleteDiscoveredDevices(acl->device); - g_list_free_full(acl->resource_list, free); - free(acl); - - return IOTCON_ERROR_NONE; -} - - int icl_provisioning_acl_convert_permission(int permission) { int oic_permission = 0; diff --git a/src/ic-provisioning-struct.h b/src/ic-provisioning-struct.h index 013cf50..fd5f2a0 100644 --- a/src/ic-provisioning-struct.h +++ b/src/ic-provisioning-struct.h @@ -18,32 +18,33 @@ #include <ocprovisioningmanager.h> +void icl_provisioning_print_uuid(OicUuid_t *uuid); +OicUuid_t* icl_provisioning_convert_device_id(const char *device_id); +int icl_provisioning_parse_oic_dev_address(OCDevAddr *dev_addr, int secure_port, + OCConnectivityType conn_type, char **host_address); + +iotcon_provisioning_device_h icl_provisioning_device_ref( + iotcon_provisioning_device_h device); +OCProvisionDev_t* icl_provisioning_device_clone(OCProvisionDev_t *src); +int icl_provisioning_device_create(OCProvisionDev_t *device, + iotcon_provisioning_device_h *ret_device); +iotcon_provisioning_device_h icl_provisioning_device_ref( + iotcon_provisioning_device_h device); + void icl_provisioning_device_set_found(iotcon_provisioning_device_h device); void icl_provisioning_device_unset_found(iotcon_provisioning_device_h device); +bool icl_provisioning_device_is_found(iotcon_provisioning_device_h device); void icl_provisioning_device_set_owned(iotcon_provisioning_device_h device); OCProvisionDev_t* icl_provisioning_device_get_device( - iotcon_provisioning_device_h devices); - -void icl_provisioning_devices_set_found(iotcon_provisioning_devices_h devices); -void icl_provisioning_devices_unset_found(iotcon_provisioning_devices_h devices); - -int icl_provisioning_devices_create(OCProvisionDev_t *dev_list, - iotcon_provisioning_devices_h *devices); - -OCProvisionDev_t* icl_provisioning_devices_get_devices( - iotcon_provisioning_devices_h devices); - -void icl_provisioning_devices_move_device(OicUuid_t *a, - iotcon_provisioning_devices_h unowned_devices, - iotcon_provisioning_devices_h owned_devices); + iotcon_provisioning_device_h device); -OCProvisionDev_t* icl_provisioning_devices_clone(OCProvisionDev_t *src); +void icl_provisioning_device_print(iotcon_provisioning_device_h device); void icl_provisioning_device_print_uuid(iotcon_provisioning_device_h device); -void icl_provisioning_devices_print_uuid(iotcon_provisioning_devices_h devices); bool icl_provisioning_compare_oic_uuid(OicUuid_t *a, OicUuid_t *b); +iotcon_provisioning_acl_h icl_provisioning_acl_ref(iotcon_provisioning_acl_h acl); int icl_provisioning_acl_clone(iotcon_provisioning_acl_h acl, iotcon_provisioning_acl_h *cloned_acl); OCProvisionDev_t* icl_provisioning_acl_get_subject( diff --git a/src/ic-provisioning.c b/src/ic-provisioning.c index 962435a..9321480 100644 --- a/src/ic-provisioning.c +++ b/src/ic-provisioning.c @@ -35,30 +35,14 @@ #define ICL_PROVISIONING_TIMEOUT_MAX 10 -typedef enum { - ICL_PROVISIONING_DISCOVER_ALL_DEVICES, - ICL_PROVISIONING_DISCOVER_OWNED_DEVICES, - ICL_PROVISIONING_DISCOVER_UNOWNED_DEVICES, - ICL_PROVISIONING_REMOVE_DEVICE, -} icl_provisioning_discover_e; - struct icl_provisioning_randompin_cb_container { iotcon_provisioning_randompin_cb cb; void *user_data; }; -struct icl_provisioning_discover_cb_container { - int timeout; - iotcon_provisioning_found_devices_cb cb; - void *user_data; - iotcon_provisioning_devices_h owned_devices; - iotcon_provisioning_devices_h unowned_devices; -}; - struct icl_provisioning_ownership_transfer_cb_container { - int count; - OCProvisionResult_t *result_list; - iotcon_provisioning_devices_h devices; + int result; + iotcon_provisioning_device_h device; iotcon_provisioning_ownership_transfer_cb cb; void *user_data; }; @@ -97,23 +81,10 @@ struct icl_provisioning_unlink_pairwise_cb_container { void *user_data; }; -struct icl_provisioning_remove_device_cb_container { - int timeout; - iotcon_provisioning_device_h device; - iotcon_error_e result; - iotcon_provisioning_remove_device_cb cb; - void *user_data; -}; - static OTMCallbackData_t icl_justworks_otmcb; static OTMCallbackData_t icl_pinbased_otmcb; static struct icl_provisioning_randompin_cb_container icl_randompin_cb_container; -static iotcon_provisioning_devices_h icl_owned_devices; -static iotcon_provisioning_devices_h icl_unowned_devices; - -static void* _provisioning_remove_device_thread(void *user_data); - static iotcon_error_e _provisioning_parse_oic_error(OCStackResult ret) { switch (ret) { @@ -128,6 +99,7 @@ static iotcon_error_e _provisioning_parse_oic_error(OCStackResult ret) } } + static void _provisioning_set_justworks() { icl_justworks_otmcb.loadSecretCB = LoadSecretJustWorksCallback; @@ -250,457 +222,26 @@ API int iotcon_provisioning_set_randompin_cb(iotcon_provisioning_randompin_cb cb } -API int iotcon_provisioning_get_devices(iotcon_provisioning_devices_h *owned_devices, - iotcon_provisioning_devices_h *unowned_devices) -{ - FN_CALL; - - RETV_IF(NULL == owned_devices && NULL == unowned_devices, - IOTCON_ERROR_INVALID_PARAMETER); - - if (owned_devices) - *owned_devices = icl_owned_devices; - - if (unowned_devices) - *unowned_devices = icl_unowned_devices; - - return IOTCON_ERROR_NONE; -} - - -static void _provisioning_discover_cb_container_destroy( - struct icl_provisioning_discover_cb_container *container) -{ - FN_CALL; - - if (container->owned_devices) { - iotcon_provisioning_devices_destroy(container->owned_devices); - container->owned_devices = NULL; - } - - if (container->unowned_devices) { - iotcon_provisioning_devices_destroy(container->unowned_devices); - container->unowned_devices = NULL; - } - - free(container); -} - - -static gboolean _provisioning_discover_idle_cb(gpointer p) -{ - FN_CALL; - struct icl_provisioning_discover_cb_container *container = p; - - DBG("discovered owned devices"); - icl_provisioning_devices_print_uuid(container->owned_devices); - DBG("discovered unowned devices"); - icl_provisioning_devices_print_uuid(container->unowned_devices); - - icl_provisioning_devices_set_found(container->owned_devices); - icl_provisioning_devices_set_found(container->unowned_devices); - - if (container->cb) - container->cb(container->owned_devices, container->unowned_devices, - container->user_data); - - icl_provisioning_devices_unset_found(container->unowned_devices); - icl_provisioning_devices_unset_found(container->owned_devices); - - _provisioning_discover_cb_container_destroy(container); - - return G_SOURCE_REMOVE; -} - - -static void* _provisioning_discover_all_thread(void *user_data) -{ - FN_CALL; - int ret; - OCProvisionDev_t *owned_list = NULL; - OCProvisionDev_t *unowned_list = NULL; - iotcon_provisioning_devices_h temp_devices; - struct icl_provisioning_discover_cb_container *container = user_data; - - ret = icl_ioty_mutex_lock(); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_ioty_mutex_lock() Fail(%d)", ret); - return NULL; - } - - ret = OCGetDevInfoFromNetwork(container->timeout, &owned_list, &unowned_list); - icl_ioty_mutex_unlock(); - if (OC_STACK_OK != ret) { - ERR("OCGetDevInfoFromNetwork() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - if (owned_list) { - ret = icl_provisioning_devices_create(owned_list, &container->owned_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_devices_create() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - ret = iotcon_provisioning_devices_clone(container->owned_devices, &temp_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_clone() Fail(%d)", ret); - return NULL; - } - - if (icl_owned_devices) { - icl_provisioning_devices_unset_found(icl_owned_devices); - iotcon_provisioning_devices_destroy(icl_owned_devices); - } - icl_owned_devices = temp_devices; - icl_provisioning_devices_set_found(icl_owned_devices); - } - - if (unowned_list) { - ret = icl_provisioning_devices_create(unowned_list, &container->unowned_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_devices_create() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - ret = iotcon_provisioning_devices_clone(container->unowned_devices, &temp_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_clone() Fail(%d)", ret); - return NULL; - } - - if (icl_unowned_devices) { - icl_provisioning_devices_unset_found(icl_unowned_devices); - iotcon_provisioning_devices_destroy(icl_unowned_devices); - } - icl_unowned_devices = temp_devices; - icl_provisioning_devices_set_found(icl_unowned_devices); - } - - g_idle_add(_provisioning_discover_idle_cb, container); - - return NULL; -} - - -static void* _provisioning_discover_unowned_thread(void *user_data) -{ - FN_CALL; - int ret; - OCProvisionDev_t *unowned_list = NULL; - iotcon_provisioning_devices_h temp_devices; - struct icl_provisioning_discover_cb_container *container = user_data; - - ret = icl_ioty_mutex_lock(); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_ioty_mutex_lock() Fail(%d)", ret); - return NULL; - } - - ret = OCDiscoverUnownedDevices(container->timeout, &unowned_list); - icl_ioty_mutex_unlock(); - if (OC_STACK_OK != ret) { - ERR("OCDiscoverUnownedDevices() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - if (unowned_list) { - ret = icl_provisioning_devices_create(unowned_list, &container->unowned_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_devices_create() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - ret = iotcon_provisioning_devices_clone(container->unowned_devices, &temp_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_clone() Fail(%d)", ret); - return NULL; - } - - if (icl_unowned_devices) { - icl_provisioning_devices_unset_found(icl_unowned_devices); - iotcon_provisioning_devices_destroy(icl_unowned_devices); - } - icl_unowned_devices = temp_devices; - icl_provisioning_devices_set_found(icl_unowned_devices); - } - - g_idle_add(_provisioning_discover_idle_cb, container); - - return NULL; -} - - -static void* _provisioning_discover_owned_thread(void *user_data) -{ - FN_CALL; - int ret; - OCProvisionDev_t *owned_list = NULL; - iotcon_provisioning_devices_h temp_devices; - struct icl_provisioning_discover_cb_container *container = user_data; - - ret = icl_ioty_mutex_lock(); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_ioty_mutex_lock() Fail(%d)", ret); - return NULL; - } - - ret = OCDiscoverOwnedDevices(container->timeout, &owned_list); - icl_ioty_mutex_unlock(); - if (OC_STACK_OK != ret) { - ERR("OCDiscoverOwnedDevices() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - if (owned_list) { - ret = icl_provisioning_devices_create(owned_list, &container->owned_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_devices_create() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return NULL; - } - - ret = iotcon_provisioning_devices_clone(container->owned_devices, &temp_devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_clone() Fail(%d)", ret); - return NULL; - } - - if (icl_owned_devices) { - icl_provisioning_devices_unset_found(icl_owned_devices); - iotcon_provisioning_devices_destroy(icl_owned_devices); - } - icl_owned_devices = temp_devices; - icl_provisioning_devices_set_found(icl_owned_devices); - } - - g_idle_add(_provisioning_discover_idle_cb, container); - - return NULL; -} - - -static int _provisioning_thread(int type, void *user_data) -{ - int ret; - pthread_t thread; - void *thread_routine; - - switch (type) { - case ICL_PROVISIONING_DISCOVER_ALL_DEVICES: - thread_routine = _provisioning_discover_all_thread; - break; - case ICL_PROVISIONING_DISCOVER_OWNED_DEVICES: - thread_routine = _provisioning_discover_owned_thread; - break; - case ICL_PROVISIONING_DISCOVER_UNOWNED_DEVICES: - thread_routine = _provisioning_discover_unowned_thread; - break; - case ICL_PROVISIONING_REMOVE_DEVICE: - thread_routine = _provisioning_remove_device_thread; - break; - default: - ERR("Invalid Type(%d)", type); - return IOTCON_ERROR_INVALID_TYPE; - } - - ret = pthread_create(&thread, NULL, thread_routine, user_data); - if (0 != ret) { - ERR("pthread_create() Fail(%d)", ret); - return IOTCON_ERROR_SYSTEM; - } - pthread_detach(thread); - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_provisioning_find_all_devices(int timeout, - iotcon_provisioning_found_devices_cb cb, void *user_data) -{ - FN_CALL; - int ret; - struct icl_provisioning_discover_cb_container *container; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(timeout < 0 || ICL_PROVISIONING_TIMEOUT_MAX < timeout, - IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - INFO("Discover All Devices"); - - container = calloc(1, sizeof(struct icl_provisioning_discover_cb_container)); - if (NULL == container) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - container->timeout = timeout; - container->cb = cb; - container->user_data = user_data; - - ret = _provisioning_thread(ICL_PROVISIONING_DISCOVER_ALL_DEVICES, container); - if (IOTCON_ERROR_NONE != ret) { - ERR("_provisioning_thread() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return ret; - } - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_provisioning_find_unowned_devices(int timeout, - iotcon_provisioning_found_devices_cb cb, void *user_data) -{ - FN_CALL; - int ret; - struct icl_provisioning_discover_cb_container *container; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(timeout < 0 || ICL_PROVISIONING_TIMEOUT_MAX < timeout, - IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - INFO("Discover Unowned Devices"); - - container = calloc(1, sizeof(struct icl_provisioning_discover_cb_container)); - if (NULL == container) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - container->timeout = timeout; - container->cb = cb; - container->user_data = user_data; - - ret = _provisioning_thread(ICL_PROVISIONING_DISCOVER_UNOWNED_DEVICES, - container); - if (IOTCON_ERROR_NONE != ret) { - ERR("_provisioning_thread() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return ret; - } - - return IOTCON_ERROR_NONE; -} - - -API int iotcon_provisioning_find_owned_devices(int timeout, - iotcon_provisioning_found_devices_cb cb, void *user_data) -{ - FN_CALL; - int ret; - struct icl_provisioning_discover_cb_container *container; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(timeout < 0 || ICL_PROVISIONING_TIMEOUT_MAX < timeout, - IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - INFO("Discover Owned Devices"); - - container = calloc(1, sizeof(struct icl_provisioning_discover_cb_container)); - if (NULL == container) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - container->timeout = timeout; - container->cb = cb; - container->user_data = user_data; - - ret = _provisioning_thread(ICL_PROVISIONING_DISCOVER_OWNED_DEVICES, - container); - if (IOTCON_ERROR_NONE != ret) { - ERR("_provisioning_thread() Fail(%d)", ret); - _provisioning_discover_cb_container_destroy(container); - return ret; - } - - return IOTCON_ERROR_NONE; -} - - static void _provisioning_ownership_transfer_cb_container_destroy( struct icl_provisioning_ownership_transfer_cb_container *container) { - if (container->devices) { - iotcon_provisioning_devices_destroy(container->devices); - container->devices = NULL; + if (container->device) { + iotcon_provisioning_device_destroy(container->device); + container->device = NULL; } - free(container->result_list); free(container); } -static int _provisioning_ownership_transfer_get_result( - iotcon_provisioning_device_h device, OCProvisionResult_t *result_list, int count) -{ - int i, ret; - OCProvisionDev_t *oic_device; - - oic_device = icl_provisioning_device_get_device(device); - - for (i = 0; i < count; i++) { - if (true == icl_provisioning_compare_oic_uuid(&oic_device->doxm->deviceID, - (OicUuid_t*)&result_list[i].deviceId)) { - ret = _provisioning_parse_oic_error(result_list[i].res); - if (IOTCON_ERROR_NONE == ret) { - icl_provisioning_devices_move_device((OicUuid_t*)&result_list[i].deviceId, - icl_unowned_devices, icl_owned_devices); - } - return ret; - } - } - - return IOTCON_ERROR_IOTIVITY; -} - - -static bool _provisioning_ownership_transfer_foreach_cb( - iotcon_provisioning_devices_h devices, - iotcon_provisioning_device_h device, - void *user_data) -{ - int result; - struct icl_provisioning_ownership_transfer_cb_container *container = user_data; - - result = _provisioning_ownership_transfer_get_result(device, container->result_list, - container->count); - - icl_provisioning_device_set_found(device); - - icl_provisioning_device_set_owned(device); - - if (container->cb) - container->cb(device, result, container->user_data); - - icl_provisioning_device_unset_found(device); - - return IOTCON_FUNC_CONTINUE; -} - - static gboolean _provisioning_ownership_transfer_idle_cb(gpointer p) { - int ret; struct icl_provisioning_ownership_transfer_cb_container *container = p; - ret = iotcon_provisioning_devices_foreach(container->devices, - _provisioning_ownership_transfer_foreach_cb, container); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_foreach() Fail(%d)", ret); - if (container->cb) - container->cb(NULL, ret, container->user_data); - } + icl_provisioning_device_set_owned(container->device); + + if (container->cb) + container->cb(container->device, container->result, container->user_data); _provisioning_ownership_transfer_cb_container_destroy(container); @@ -713,22 +254,13 @@ static void _provisioning_ownership_transfer_cb(void *ctx, int n_of_res, { FN_CALL; struct icl_provisioning_ownership_transfer_cb_container *container; - OCProvisionResult_t *result_list; - - container = ctx; - container->count = n_of_res; - result_list = calloc(n_of_res + 1, sizeof(OCProvisionResult_t)); - if (NULL == result_list) { - ERR("calloc() Fail(%d)", errno); - return; - } - memcpy(result_list, arr, n_of_res * sizeof(OCProvisionResult_t)); + RET_IF(NULL == ctx); - container->result_list = result_list; + container = ctx; - if (true == has_error) - DBG("ownership transfer has error"); + container->result = _provisioning_parse_oic_error(arr[0].res); + DBG("result : %d", container->result); g_idle_add(_provisioning_ownership_transfer_idle_cb, container); @@ -736,57 +268,6 @@ static void _provisioning_ownership_transfer_cb(void *ctx, int n_of_res, } -API int iotcon_provisioning_register_unowned_devices( - iotcon_provisioning_devices_h devices, - iotcon_provisioning_ownership_transfer_cb cb, - void *user_data) -{ - FN_CALL; - int ret; - OCProvisionDev_t *dev_list; - struct icl_provisioning_ownership_transfer_cb_container *container; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(NULL == devices, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - container = calloc(1, sizeof(struct icl_provisioning_ownership_transfer_cb_container)); - if (NULL == container) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - ret = iotcon_provisioning_devices_clone(devices, &container->devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_devices_clone() Fail(%d)", ret); - _provisioning_ownership_transfer_cb_container_destroy(container); - return ret; - } - - container->cb = cb; - container->user_data = user_data; - - dev_list = icl_provisioning_devices_get_devices(container->devices); - - ret = icl_ioty_mutex_lock(); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_ioty_mutex_lock() Fail(%d)", ret); - _provisioning_ownership_transfer_cb_container_destroy(container); - return ret; - } - - ret = OCDoOwnershipTransfer(container, dev_list, _provisioning_ownership_transfer_cb); - icl_ioty_mutex_unlock(); - if (OC_STACK_OK != ret) { - ERR("OCDoOwnershipTransfer() Fail(%d)", ret); - _provisioning_ownership_transfer_cb_container_destroy(container); - return _provisioning_parse_oic_error(ret); - } - - return IOTCON_ERROR_NONE; -} - - API int iotcon_provisioning_register_unowned_device( iotcon_provisioning_device_h device, iotcon_provisioning_ownership_transfer_cb cb, @@ -794,13 +275,18 @@ API int iotcon_provisioning_register_unowned_device( { FN_CALL; int ret; - OCProvisionDev_t *dev_list, *cloned_list; + OCProvisionDev_t *dev_list; struct icl_provisioning_ownership_transfer_cb_container *container; RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); RETV_IF(NULL == device, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + if (true == icl_provisioning_device_is_found(device)) { + ERR("The device should be cloned"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + container = calloc(1, sizeof(struct icl_provisioning_ownership_transfer_cb_container)); if (NULL == container) { ERR("calloc() Fail(%d)", errno); @@ -810,14 +296,9 @@ API int iotcon_provisioning_register_unowned_device( container->cb = cb; container->user_data = user_data; - dev_list = icl_provisioning_device_get_device(device); - cloned_list = icl_provisioning_devices_clone(dev_list); + container->device = icl_provisioning_device_ref(device); - ret = icl_provisioning_devices_create(cloned_list, &container->devices); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_devices_create() Fail(%d)", ret); - return ret; - } + dev_list = icl_provisioning_device_get_device(device); ret = icl_ioty_mutex_lock(); if (IOTCON_ERROR_NONE != ret) { @@ -826,7 +307,7 @@ API int iotcon_provisioning_register_unowned_device( return ret; } - ret = OCDoOwnershipTransfer(container, cloned_list, _provisioning_ownership_transfer_cb); + ret = OCDoOwnershipTransfer(container, dev_list, _provisioning_ownership_transfer_cb); icl_ioty_mutex_unlock(); if (OC_STACK_OK != ret) { ERR("OCDoOwnershipTransfer() Fail(%d)", ret); @@ -907,6 +388,16 @@ API int iotcon_provisioning_provision_cred(iotcon_provisioning_device_h device1, RETV_IF(NULL == device2, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + if (true == icl_provisioning_device_is_found(device1)) { + ERR("The device should be cloned(device1)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + if (true == icl_provisioning_device_is_found(device2)) { + ERR("The device should be cloned(device2)"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + key_size = OWNER_PSK_LENGTH_256; /* or OWNER_PSK_LENGTH_128 */ container = calloc(1, sizeof(struct icl_provisioning_provision_cred_cb_container)); @@ -915,18 +406,8 @@ API int iotcon_provisioning_provision_cred(iotcon_provisioning_device_h device1, return IOTCON_ERROR_OUT_OF_MEMORY; } - ret = iotcon_provisioning_device_clone(device1, &container->device1); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_provision_cred_cb_container_destroy(container); - return ret; - } - ret = iotcon_provisioning_device_clone(device2, &container->device2); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_provision_cred_cb_container_destroy(container); - return ret; - } + container->device1 = icl_provisioning_device_ref(device1); + container->device2 = icl_provisioning_device_ref(device2); container->cb = cb; container->user_data = user_data; @@ -968,24 +449,6 @@ static void _provisioning_provision_acl_cb_container_destroy( } - -static char* _provisioning_parse_uuid(OicUuid_t *uuid) -{ - char uuid_string[256] = {0}; - - snprintf(uuid_string, sizeof(uuid_string), - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], - (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], - (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], - (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); - - DBG("uuid : %s", uuid_string); - - return strdup(uuid_string); -} - - static void _provisioning_ace_add_resource(OicSecAce_t *ace, OicSecRsrc_t *resource) { OicSecRsrc_t *current; @@ -1033,7 +496,7 @@ static OicSecAcl_t* _provisioning_convert_acl(iotcon_provisioning_device_h devic memcpy(&ace->subjectuuid, &subject->doxm->deviceID, 128/8); - _provisioning_parse_uuid(&ace->subjectuuid); + icl_provisioning_print_uuid(&ace->subjectuuid); num_of_resources = icl_provisioning_acl_get_resource_count(acl); @@ -1066,7 +529,7 @@ static OicSecAcl_t* _provisioning_convert_acl(iotcon_provisioning_device_h devic memcpy(&oic_acl->rownerID, &oic_device->doxm->deviceID, sizeof(OicUuid_t)); - _provisioning_parse_uuid(&oic_acl->rownerID); + icl_provisioning_print_uuid(&oic_acl->rownerID); return oic_acl; } @@ -1129,19 +592,8 @@ API int iotcon_provisioning_provision_acl(iotcon_provisioning_device_h device, return IOTCON_ERROR_OUT_OF_MEMORY; } - ret = iotcon_provisioning_device_clone(device, &container->device); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_provision_acl_cb_container_destroy(container); - return ret; - } - - ret = icl_provisioning_acl_clone(acl, &container->acl); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_acl_clone() Fail(%d)", ret); - _provisioning_provision_acl_cb_container_destroy(container); - return ret; - } + container->device = icl_provisioning_device_ref(device); + container->acl = icl_provisioning_acl_ref(acl); container->cb = cb; container->user_data = user_data; @@ -1260,41 +712,20 @@ API int iotcon_provisioning_pairwise_devices(iotcon_provisioning_device_h device return IOTCON_ERROR_OUT_OF_MEMORY; } - ret = iotcon_provisioning_device_clone(device1, &container->device1); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_pairwise_devices_cb_container_destroy(container); - return ret; - } + container->device1 = icl_provisioning_device_ref(device1); if (acl1) { - ret = icl_provisioning_acl_clone(acl1, &container->acl1); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_acl_clone() Fail(%d)", ret); - _provisioning_pairwise_devices_cb_container_destroy(container); - return ret; - } + container->acl1 = icl_provisioning_acl_ref(acl1); oic_acl1 = _provisioning_convert_acl(container->device1, container->acl1); } - ret = iotcon_provisioning_device_clone(device2, &container->device2); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_pairwise_devices_cb_container_destroy(container); - return ret; - } + container->device2 = icl_provisioning_device_ref(device2); if (acl2) { - ret = icl_provisioning_acl_clone(acl2, &container->acl2); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_provisioning_acl_clone() Fail(%d)", ret); - _provisioning_pairwise_devices_cb_container_destroy(container); - return ret; - } + container->acl2 = icl_provisioning_acl_ref(acl2); oic_acl2 = _provisioning_convert_acl(container->device2, container->acl2); } - container->cb = cb; container->user_data = user_data; @@ -1395,19 +826,8 @@ API int iotcon_provisioning_unlink_pairwise(iotcon_provisioning_device_h device1 return IOTCON_ERROR_OUT_OF_MEMORY; } - ret = iotcon_provisioning_device_clone(device1, &container->device1); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_unlink_pairwise_cb_container_destroy(container); - return ret; - } - - ret = iotcon_provisioning_device_clone(device2, &container->device2); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_unlink_pairwise_cb_container_destroy(container); - return ret; - } + container->device1 = icl_provisioning_device_ref(device1); + container->device2 = icl_provisioning_device_ref(device2); container->cb = cb; container->user_data = user_data; @@ -1432,117 +852,3 @@ API int iotcon_provisioning_unlink_pairwise(iotcon_provisioning_device_h device1 return IOTCON_ERROR_NONE; } - - -static void _provisioning_remove_cb_container_destroy( - struct icl_provisioning_remove_device_cb_container *container) -{ - if (container->device) { - iotcon_provisioning_device_destroy(container->device); - container->device = NULL; - } - - free(container); -} - - -static gboolean _provisioning_remove_device_idle_cb(gpointer p) -{ - FN_CALL; - struct icl_provisioning_remove_device_cb_container *container = p; - - if (container->cb) - container->cb(container->device, container->result, container->user_data); - - _provisioning_remove_cb_container_destroy(container); - - return G_SOURCE_REMOVE; -} - - -static void _provisioning_remove_device_cb(void *ctx, int n_of_res, - OCProvisionResult_t *arr, bool has_error) -{ - FN_CALL; - struct icl_provisioning_remove_device_cb_container *container = ctx; - - container->result = IOTCON_ERROR_NONE; - - if (true == has_error) { - DBG("remove device has error"); - container->result = IOTCON_ERROR_IOTIVITY; - } - - g_idle_add(_provisioning_remove_device_idle_cb, container); -} - - -static void* _provisioning_remove_device_thread(void *user_data) -{ - FN_CALL; - int ret; - struct icl_provisioning_remove_device_cb_container *container = user_data; - - ret = icl_ioty_mutex_lock(); - if (IOTCON_ERROR_NONE != ret) { - ERR("icl_ioty_mutex_lock() Fail(%d)", ret); - _provisioning_remove_cb_container_destroy(container); - return NULL; - } - - ret = OCRemoveDevice(container, - container->timeout, - icl_provisioning_device_get_device(container->device), - _provisioning_remove_device_cb); - icl_ioty_mutex_unlock(); - if (OC_STACK_OK != ret) { - ERR("OCRemoveDevice() Fail(%d)", ret); - _provisioning_remove_cb_container_destroy(container); - return NULL; - } - - return NULL; -} - - -API int iotcon_provisioning_remove_device(int timeout, - iotcon_provisioning_device_h device, - iotcon_provisioning_remove_device_cb cb, - void *user_data) -{ - FN_CALL; - int ret; - struct icl_provisioning_remove_device_cb_container *container; - - RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); - RETV_IF(timeout < 0 || ICL_PROVISIONING_TIMEOUT_MAX < timeout, - IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == device, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); - - container = calloc(1, sizeof(struct icl_provisioning_remove_device_cb_container)); - if (NULL == container) { - ERR("calloc() Fail(%d)", errno); - return IOTCON_ERROR_OUT_OF_MEMORY; - } - - container->timeout = timeout; - container->cb = cb; - container->user_data = user_data; - - ret = iotcon_provisioning_device_clone(device, &container->device); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); - _provisioning_remove_cb_container_destroy(container); - return ret; - } - - ret = _provisioning_thread(ICL_PROVISIONING_REMOVE_DEVICE, container); - if (IOTCON_ERROR_NONE != ret) { - ERR("_provisinoing_discover_thread() Fail(%d)", ret); - _provisioning_remove_cb_container_destroy(container); - return ret; - } - - return IOTCON_ERROR_NONE; -} diff --git a/src/ic-utils.c b/src/ic-utils.c index 415bc6b..55fbe1c 100644 --- a/src/ic-utils.c +++ b/src/ic-utils.c @@ -13,11 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#define _GNU_SOURCE #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <time.h> +#include <pthread.h> #include <glib.h> #include <system_info.h> #include <system_settings.h> @@ -54,7 +56,7 @@ static const char *IC_SYSTEM_INFO_BUILD_STRING = "http://tizen.org/system/build. static const char *IC_SYSTEM_INFO_TIZEN_ID = "http://tizen.org/system/tizenid"; static pthread_mutex_t ic_utils_mutex_init = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t ic_utils_mutex_ioty = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t ic_utils_mutex_ioty = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; static pthread_mutex_t ic_utils_mutex_polling = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t ic_utils_cond_polling; static __thread int ic_utils_pthread_oldstate; |