+++ /dev/null
-#include <tzplatform_config.h>
-
-#include <utlist.h>
-#include <base64.h>
-#include <srmutility.h>
-#include <iotivity_config.h>
-#include <platform_features.h>
-#include <ocprovisioningmanager.h>
-#include <doxmresource.h>
-
-#include <comp_iot.h>
-#include <comp_group.h>
-#include <comp_gdbus_group.h>
-
-#define MAX_FILE_PATH_LEN 1024
-#define SVR_DB_FILE_NAME "oic_svr_db_comp_manager.dat"
-
-int last_get_result;
-
-#define CASE_TO_STR(x) case x: return #x;
-
-static const char *command2string(comp_request_type_e command)
-{
- switch (command) {
- CASE_TO_STR(COMP_REQ_SEND_DATA)
- CASE_TO_STR(COMP_REQ_CREATE_GROUP)
- CASE_TO_STR(COMP_REQ_INVITE_DEVICE)
- CASE_TO_STR(COMP_REQ_EJECT_DEVICE)
- CASE_TO_STR(COMP_REQ_DELETE_GROUP)
- default:
- return "Unknown Command";
- }
-}
-
-static comp_request_type_e string2command(char *command)
-{
- if (g_strcmp0(command, "5") == 0)
- return COMP_REQ_SEND_DATA;
- else if (g_strcmp0(command, "1") == 0)
- return COMP_REQ_CREATE_GROUP;
- else if (g_strcmp0(command, "2") == 0)
- return COMP_REQ_INVITE_DEVICE;
- else if (g_strcmp0(command, "3") == 0)
- return COMP_REQ_EJECT_DEVICE;
- else if (g_strcmp0(command, "4") == 0)
- return COMP_REQ_DELETE_GROUP;
- else
- return COMP_REQ_UNKNOWN_COMMAND;
-}
-
-int comp_iot_initialize()
-{
- char *device_id = NULL;
- char data_dir[MAX_FILE_PATH_LEN] = {0,};
-
- snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s",
- "/opt/usr/data", SVR_DB_FILE_NAME);
-
- int ret = iotcon_initialize(data_dir);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_initialize: Failed %s", get_error_message(ret));
- return false;
- }
- LOG_DEBUG("iotcon_initialize : %s", get_error_message(ret));
-
- OicUuid_t uuid;
- ret = GetDoxmDeviceID(&uuid);
- if (OC_STACK_OK != ret)
- LOG_DEBUG("GetDoxmDevOwnerId failed = [%d][%s]", ret, get_error_message(ret));
-
- ret = ConvertUuidToStr(&uuid, &device_id);
- if (OC_STACK_OK != ret)
- LOG_DEBUG("ConvertUuidToStr failed = [%d][%s]", ret, get_error_message(ret));
-
- LOG_DEBUG("device_id : %s", device_id);
-
- comp_context_t *comp_ctx = comp_context_get_context();
- comp_ctx->device_uuid = g_strdup(device_id);
-
- /* Do Self-Ownership Transfer */
- ret = OCConfigSelfOwnership();
- if (OC_STACK_OK != ret ) {
- LOG_ERR( "OCConfigSelfOwnership() error = [%d][%s]", ret, get_error_message(ret));
- }
-
- /*
- "operation" resource
- The operation resource is a control command channel between daemon and daemon.
- resource type is core.comp.operation
- Within this function we create a default operation resource. (uri is /comp/operation/1)
- If we need more control channels, increase the number.
- ex) /comp/operation/2, /comp/operation/3
- */
- comp_iot_add_resource(COMP_RESOURCE_TYPE_OPERATION, "1");
-
- /*
- "DATA" resource
- To show the send data to remote daemon.
- It can be used when the remote device is mot enable and pairwise.
- ex) /comp/data/1
- */
- comp_iot_add_resource(COMP_RESOURCE_TYPE_DATA, "1");
-
- return COMP_ERROR_NONE;
-}
-
-static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
- void *user_data)
-{
- LOG_DEBUG("_request_handler is called");
- //get resource element from comp resource list or, parsing resource uri
-
- //if request type is "get" and resource type is "group",
- //then OWNER send device information to CLIENT(Device info Exchange)
- //if request type is "put" and resource type is "operation",
- //then It is join request. CLIENT send device information to OWNER(Device info Exchange)
- //resource type "operation" don't have "get" request type.
-
- int ret;
- iotcon_request_type_e type;
-
- ret = iotcon_request_get_request_type(request, &type);
- if (IOTCON_ERROR_NONE != ret)
- return;
-
- LOG_DEBUG("iotcon request type %d", type);
- if (IOTCON_REQUEST_POST == type) {
- iotcon_response_h response = NULL;
- iotcon_representation_h req_repr;
- iotcon_attributes_h attributes;
- char *cmd;
- char *arg = NULL;
- int result;
- comp_request_type_e command;
-
- ret = iotcon_request_get_representation(request, &req_repr);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_request_get_representation() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_representation_get_attributes(req_repr, &attributes);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_representation_get_attributes() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_attributes_get_str(attributes, "CMD", &cmd);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_attributes_get_bool() Fail(%d)", ret);
- return;
- }
-
- LOG_DEBUG("cmd %s", cmd);
-
- command = string2command(cmd);
-
- if (command == COMP_REQ_CREATE_GROUP) {
- LOG_DEBUG("Request create group");
- char *group_name;
- iotcon_attributes_get_str(attributes, "name", &group_name);
-
- LOG_DEBUG("group_name : %s", group_name);
- result = comp_group_create(group_name);
- arg = g_strdup(group_name);
-
- } else if (command == COMP_REQ_INVITE_DEVICE) {
- LOG_DEBUG("Request invite");
- char *group_name;
- char *uuid;
-
- iotcon_attributes_get_str(attributes, "name", &group_name);
- iotcon_attributes_get_str(attributes, "id", &uuid);
-
- LOG_DEBUG("group_name : %s, UUID : %s", group_name, uuid);
-
- result = comp_group_invite(group_name, uuid, "12341234");
- arg = g_strdup(uuid);
-
- } else if (command == COMP_REQ_EJECT_DEVICE) {
- LOG_DEBUG("Request eject");
- char *group_name;
- char *uuid;
- comp_context_t *comp_ctx = comp_context_get_context();
-
- iotcon_attributes_get_str(attributes, "name", &group_name);
- iotcon_attributes_get_str(attributes, "id", &uuid);
-
- LOG_DEBUG("group_name : %s, Self UUID : %s Target UUID : %s",
- group_name, comp_ctx->device_uuid, uuid);
-
- result = comp_group_dismiss(comp_ctx->device_uuid, uuid);
- arg = g_strdup(uuid);
-
- } else if (command == COMP_REQ_DELETE_GROUP) {
- LOG_DEBUG("Request delete group");
- arg = g_strdup("DELETED");
- } else if (command == COMP_REQ_SEND_DATA) {
-#ifdef SUPPORT_BASE64_ENCODING
- int payload_len;
- char *data = NULL;
-
- LOG_DEBUG("Receive Data");
-
- iotcon_attributes_get_str(attributes, "data", &data);
- /*
- * BASE64 encoding/decoding system use '=' as padding byte
- * But, query parameter does not allow use '=' character.Basically,
- * So, in order to use BASE64 as query parameters, we need additioinal length param
- * such as 'len=xx'
- */
- payload_len = strlen(data); /* This data may be cliped the last padding 1~2 bytes ('='/'==') */
-
- LOG_DEBUG("data = %s payload_len = %d", data, payload_len);
-
- size_t outSize = B64DECODE_OUT_SAFESIZE(payload_len + 1);
- uint8_t* out = g_malloc0(outSize);
- if (NULL == out) {
- LOG_ERR("Can't allocate memory for base64 str");
- return;
- }
- uint32_t len = 0;
-
- if(B64_OK == b64Decode(data, payload_len, out, outSize, &len)) {
- LOG_ERR("Base64 decoding failed.");
- return;
- }
- memcpy(arg, out, len);
-
- LOG_DEBUG("successfully decoded to base64. %s", arg);
-
- iotcon_query_remove(query, "data");
-#else
- char *data = NULL;
- iotcon_attributes_get_str(attributes, "data", &data);
- LOG_DEBUG("Receive Data = %s", data);
- arg = g_strdup(data);
-#endif
- } else {
- LOG_ERR("Unknown request command");
- }
-
- notify_request_result(cmd, arg, result);
-
- free(arg);
-
- ret = iotcon_response_create(request, &response);
- if (IOTCON_ERROR_NONE != ret)
- return;
-
- ret = iotcon_response_set_result(response, IOTCON_RESPONSE_OK);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_response_set_result Faild = %d", ret);
- iotcon_response_destroy(response);
- return;
- }
-
- ret = iotcon_response_send(response);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_response_send Faild = %d", ret);
- iotcon_response_destroy(response);
- return;
- }
-
- iotcon_response_destroy(response);
- }
-}
-
-int comp_iot_add_resource(comp_resource_type_e resource_type, char *uri)
-{
- iotcon_resource_h resource = NULL;
- iotcon_resource_types_h resource_types = NULL;
- iotcon_resource_interfaces_h resource_ifaces = NULL;
- char uri_path[PATH_MAX] = {0,};
-
- int ret = iotcon_resource_types_create(&resource_types);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_resource_types_create failed! : %s", get_error_message(ret));
- goto EXIT;
- }
-
- ret = iotcon_resource_types_add(resource_types, comp_resource_get_type(resource_type));
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_resource_types_add failed! : %s", get_error_message(ret));
- goto EXIT;
- }
-
- ret = iotcon_resource_interfaces_create(&resource_ifaces);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_resource_interfaces_create failed! : %s", get_error_message(ret));
- goto EXIT;
- }
-
- ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_DEFAULT);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_resource_interfaces_add failed! : %s", get_error_message(ret));
- goto EXIT;
- }
-
- strncpy(uri_path, comp_resource_get_uri_prefix(resource_type), PATH_MAX);
- strncat(uri_path, uri, PATH_MAX);
-
- LOG_DEBUG("[ADD] resource uri is %s", uri_path);
- //name duplication check
-
- ret = iotcon_resource_create(uri_path, resource_types, resource_ifaces,
- comp_resource_get_policies(resource_type), _request_handler, NULL, &resource);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_resource_create failed! : %s", get_error_message(ret));
- goto EXIT;
- }
-
- //get resource list of resource type
- ret = comp_resource_append(resource_type, uri, resource);
- if (COMP_ERROR_NONE != ret) {
- LOG_ERR("comp_resource_append failed! : %s", comp_log_get_error_string(ret));
- goto EXIT;
- }
-
-EXIT:
- if (resource_types != NULL)
- iotcon_resource_types_destroy(resource_types);
-
- if (resource_ifaces != NULL)
- iotcon_resource_interfaces_destroy(resource_ifaces);
-
- return COMP_ERROR_NONE;
-}
-
-static bool _get_res_type_cb(const char *string, void *user_data)
-{
- char **resource_type = user_data;
-
- *resource_type = g_strdup(string);
-
- LOG_DEBUG("resource type : %s", *resource_type);
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-int found_group_count = 0;
-
-static void _clear_user_data(void *user_data)
-{
- comp_command_t *cmd = user_data;
-
- if (NULL == cmd)
- return;
-
- if (cmd->tid) {
- g_source_remove(cmd->tid);
- }
-
- if (cmd->uuid) {
- g_free(cmd->uuid);
- cmd->uuid = NULL;
- }
- if (cmd->host) {
- g_free(cmd->host);
- cmd->host = NULL;
- }
- if (cmd->arg1) {
- g_free(cmd->arg1);
- cmd->arg1 = NULL;
- }
- if (cmd->arg2) {
- g_free(cmd->arg2);
- cmd->arg2 = NULL;
- }
- if (cmd->arg3) {
- g_free(cmd->arg3);
- cmd->arg3 = NULL;
- }
- if (cmd->arg4) {
- g_free(cmd->arg4);
- cmd->arg4 = NULL;
- }
-}
-
-static void _on_get(iotcon_remote_resource_h resource, iotcon_error_e err,
- iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
-{
- last_get_result = err;
-
- int ret;
- iotcon_response_result_e response_result;
-
- ret = iotcon_response_get_result(response, &response_result);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_response_get_result() Fail(%d)", ret);
- return;
- }
-
- if (IOTCON_RESPONSE_OK != response_result) {
- LOG_ERR("_on_get Response error(%d)", response_result);
- return;
- }
-
- /* get the resource host address */
- char *resource_host = NULL;
- iotcon_remote_resource_get_host_address(resource, &resource_host);
- LOG_DEBUG("resource host : %s", resource_host);
-
- iotcon_remote_resource_destroy(resource);
-
- _clear_user_data(user_data);
-}
-
-static bool _found_resource(iotcon_remote_resource_h resource,
- iotcon_error_e result, void *user_data)
-{
- int ret;
- char *resource_uri_path = NULL;
- iotcon_resource_types_h resource_types;
- char *resource_device_id;
- char *resource_host;
- char *resource_type;
- comp_group_type_e group_type;
-
- /* Callback: When no more resource available.
- As timeout is configured by IOTCON Library*/
- if (resource == NULL) {
- LOG_ERR("No more resource available to find !!");
- return IOTCON_FUNC_CONTINUE;
- }
-
- /* get the resource URI */
- ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to get resource uri path");
- return IOTCON_FUNC_CONTINUE;
- }
-
-
- LOG_DEBUG("Resource Found: Resource uri : %s", resource_uri_path);
-
- /* get the resource device id */
- ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to get resource device id");
- return IOTCON_FUNC_CONTINUE;
- }
- LOG_DEBUG("resource device id : %s", resource_device_id);
-
- /* get the resource host address */
- ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to get resource host address");
- return IOTCON_FUNC_CONTINUE;
- }
- LOG_DEBUG("resource host : %s", resource_host);
-
- ret = iotcon_remote_resource_get_types(resource, &resource_types);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to get resource types");
- return IOTCON_FUNC_CONTINUE;
- }
-
- ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
- &resource_type);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to get foreach resource types");
- return IOTCON_FUNC_CONTINUE;
- }
-
- if (strcmp(resource_type, "core.comp.group") == 0) {
- comp_context_t *comp_ctx = comp_context_get_context();
- if (strcmp(resource_device_id, comp_ctx->device_uuid) == 0)
- group_type = COMP_GROUP_DEVICE;
- else
- group_type = COMP_GROUP_REMOTE_DEVICE;
-
- char *temp = strdup(resource_uri_path);
- char *ptr = strtok(temp, "/");
- ptr = strtok(NULL, "/");
- ptr = strtok(NULL, "/");
-
- LOG_DEBUG("group name is %s", ptr);
- comp_group_add_new(resource_uri_path, resource_device_id,
- ptr, resource_host, resource_type, group_type);
- found_group_count++;
-
- g_free(resource_type);
-
- return IOTCON_FUNC_CONTINUE;
- } else if (strcmp(resource_type, "core.comp.data") == 0) {
- comp_command_t *cmd = (comp_command_t *)user_data;
-
- if (cmd != NULL && strcmp(cmd->uuid, resource_device_id) == 0) {
- LOG_DEBUG("Request to Remote Device");
-
- iotcon_remote_resource_h resource_clone = NULL;
- iotcon_representation_h repr;
- iotcon_attributes_h attributes;
-
- ret = iotcon_remote_resource_clone(resource, &resource_clone);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_remote_resource_clone failed = %d", ret);
- return IOTCON_FUNC_CONTINUE;
- }
-
- ret = iotcon_representation_create(&repr);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_representation_create() Fail(%d)", ret);
- return IOTCON_FUNC_CONTINUE;
- }
-
- ret = iotcon_attributes_create(&attributes);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_attributes_create() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return;
- }
-
- LOG_DEBUG("CMD = %s", command2string(cmd->command));
-
- if (cmd->command == COMP_REQ_CREATE_GROUP) { //request create group
- iotcon_attributes_add_str(attributes, "CMD", "1");
- iotcon_attributes_add_str(attributes, "name", cmd->arg1);
- } else if (cmd->command == COMP_REQ_INVITE_DEVICE) { //request invite
- iotcon_attributes_add_str(attributes, "CMD", "2");
- iotcon_attributes_add_str(attributes, "name", cmd->arg1);
- iotcon_attributes_add_str(attributes, "id", cmd->arg2);
- iotcon_attributes_add_str(attributes, "PIN", cmd->arg3);
- } else if (cmd->command == COMP_REQ_EJECT_DEVICE) { //request eject
- iotcon_attributes_add_str(attributes, "CMD", "3");
- iotcon_attributes_add_str(attributes, "name", cmd->arg1);
- iotcon_attributes_add_str(attributes, "id", cmd->arg2);
- } else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group
- iotcon_attributes_add_str(attributes, "CMD", "4");
- iotcon_attributes_add_str(attributes, "name", cmd->arg1);
- } else { /* Send Data */
- iotcon_attributes_add_str(attributes, "CMD", "5");
-#ifdef SUPPORT_BASE64_ENCODING
- uint32_t outSize = 0;
- size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((cmd->arg1_len + 1));
- char* b64Buf = g_malloc0(b64BufSize);
- if (NULL == b64Buf) {
- iotcon_remote_resource_destroy(resource_clone);
- _clear_user_data(cmd);
- return IOTCON_FUNC_CONTINUE;
- }
- b64Encode((const char *)cmd->arg1, cmd->arg1_len, b64Buf,
- b64BufSize, &outSize);
- b64Buf[b64BufSize] = '\0';
- iotcon_attributes_add_str(attributes, "data", b64Buf);
- LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s", b64BufSize,
- outSize, b64Buf);
- g_free(b64Buf);
-#else
- iotcon_attributes_add_str(attributes, "data", cmd->arg1);
-#endif
- }
-
- ret = iotcon_representation_set_attributes(repr, attributes);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
- iotcon_attributes_destroy(attributes);
- iotcon_representation_destroy(repr);
- return IOTCON_FUNC_CONTINUE;
- }
-
- iotcon_attributes_destroy(attributes);
-
- ret = iotcon_remote_resource_post(resource_clone, repr, NULL,
- _on_get, NULL);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_remote_resource_put() Fail(%d)", ret);
- iotcon_remote_resource_destroy(resource_clone);
- _clear_user_data(cmd);
- return IOTCON_FUNC_CONTINUE;
- }
-
- iotcon_representation_destroy(repr);
- }
-
- }
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-static gboolean _timeout_cb(gpointer data)
-{
- int ret = -1;
- comp_command_t *cmd = (comp_command_t *)data;
-
- if (cmd->resource_type == COMP_RESOURCE_TYPE_GROUP) {
- if (found_group_count > 0)
- ret = 0;
-
- notify_group_find_finish(ret);
-
- } else if (cmd->resource_type == COMP_RESOURCE_TYPE_DATA) {
- notify_send_data_finish("RESP_DATA", last_get_result);
- }
-
- _clear_user_data(data);
-
- return false;
-}
-
-int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, void *user_data)
-{
- int ret;
- iotcon_query_h query;
- comp_command_t *cmd = user_data;
-
- found_group_count = 0;
-
- ret = iotcon_set_timeout(timeout);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to set timeout value");
- _clear_user_data(user_data);
- return COMP_ERROR_OPERATION_FAILED;
- }
-
- ret = iotcon_query_create(&query);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to create iotcon query");
- _clear_user_data(user_data);
- return COMP_ERROR_OPERATION_FAILED;
- }
-
- ret = iotcon_query_set_resource_type(query,
- comp_resource_get_type(resource_type));
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("Failed to set iotcon query resource type");
- _clear_user_data(user_data);
- return COMP_ERROR_OPERATION_FAILED;
- }
-
- LOG_DEBUG("Set Resource Type : %s", get_error_message(ret));
-
- ret = iotcon_set_timeout(timeout);
- if (IOTCON_ERROR_NONE != ret) {
- LOG_ERR("iotcon_set_timeout: Failed %s", get_error_message(ret));
- }
-
- if (cmd && (cmd->command == COMP_REQ_SEND_DATA_ALL))
- ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS,
- IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
- query, _found_resource, user_data);
- else
- ret = iotcon_find_resource(cmd->host,
- IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
- query, _found_resource, user_data);
-
- cmd->resource_type = resource_type;
- cmd->tid = g_timeout_add_seconds(timeout + 1, _timeout_cb, cmd);
-
- LOG_DEBUG("find resource : %s", get_error_message(ret));
-
- return ret;
-}
-
-int comp_iot_delete_resource(comp_resource_type_e resource_type)
-{
- //delete resource from resource list using resource_type and index
- //delete DB
- return COMP_ERROR_NONE;
-}
-
-int comp_iot_deinitialize()
-{
- return COMP_ERROR_NONE;
-}
--- /dev/null
+#include <tzplatform_config.h>
+
+#include <utlist.h>
+#include <base64.h>
+#include <srmutility.h>
+#include <iotivity_config.h>
+#include <platform_features.h>
+#include <ocprovisioningmanager.h>
+#include <doxmresource.h>
+#include <system_info.h>
+#include <vconf.h>
+
+#include <comp_iot.h>
+#include <comp_group.h>
+#include <comp_gdbus_group.h>
+
+#include "OCProvisioningManager.hpp"
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+using namespace OC;
+using namespace std;
+
+#define MAX_FILE_PATH_LEN 1024
+#define SVR_DB_FILE_NAME "oic_svr_db_comp_manager.dat"
+#define SYSTEM_INFO_PLATFORM_VERSION "http://tizen.org/feature/platform.version"
+#define SYSTEM_INFO_MANUF_NAME "http://tizen.org/system/manufacturer"
+#define SYSTEM_INFO_MODEL_NAME "http://tizen.org/system/model_name"
+#define SYSTEM_INFO_BUILD_STRING "http://tizen.org/system/build.string"
+#define SYSTEM_INFO_TIZEN_ID "http://tizen.org/system/tizenid"
+
+int last_get_result;
+OCPersistentStorage ps;
+
+#define CASE_TO_STR(x) case x: return #x;
+
+static const char *command2string(comp_request_type_e command)
+{
+ switch (command) {
+ CASE_TO_STR(COMP_REQ_SEND_DATA)
+ CASE_TO_STR(COMP_REQ_CREATE_GROUP)
+ CASE_TO_STR(COMP_REQ_INVITE_DEVICE)
+ CASE_TO_STR(COMP_REQ_EJECT_DEVICE)
+ CASE_TO_STR(COMP_REQ_DELETE_GROUP)
+ default:
+ return "Unknown Command";
+ }
+}
+
+static comp_request_type_e string2command(char *command)
+{
+ if (g_strcmp0(command, "5") == 0)
+ return COMP_REQ_SEND_DATA;
+ else if (g_strcmp0(command, "1") == 0)
+ return COMP_REQ_CREATE_GROUP;
+ else if (g_strcmp0(command, "2") == 0)
+ return COMP_REQ_INVITE_DEVICE;
+ else if (g_strcmp0(command, "3") == 0)
+ return COMP_REQ_EJECT_DEVICE;
+ else if (g_strcmp0(command, "4") == 0)
+ return COMP_REQ_DELETE_GROUP;
+ else
+ return COMP_REQ_UNKNOWN_COMMAND;
+}
+
+static FILE* client_open(const char* /*path*/, const char *mode)
+{
+ char data_dir[MAX_FILE_PATH_LEN] = {0,};
+
+ snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s",
+ "/opt/usr/data", SVR_DB_FILE_NAME);
+
+ LOG_DEBUG("Open file %s", data_dir);
+
+ return fopen(data_dir, mode);
+}
+
+int __comp_iot_get_platform_info(OCPlatformInfo *platform_info)
+{
+ int ret;
+ char *tizen_id = NULL;
+ char *device_name = NULL;
+ char platform_id[1024];
+
+ device_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
+ if (device_name == NULL)
+ LOG_ERR("vconf_get_str() Fail");
+
+ ret = system_info_get_platform_string(SYSTEM_INFO_TIZEN_ID, &tizen_id);
+ if (SYSTEM_INFO_ERROR_NONE != ret)
+ LOG_ERR("system_info_get_platform_string() Fail(%d)", ret);
+
+ snprintf(platform_id, sizeof(platform_id), "%s(%s)",
+ device_name ? device_name : "", tizen_id ? tizen_id : "");
+
+ free(device_name);
+ free(tizen_id);
+ LOG_DEBUG("platform_id: %s", platform_id);
+
+ /* Mandatory (oic.wk.p) */
+ platform_info->platformID = strdup(platform_id);
+
+ /* Mandatory (oic.wk.p) */
+ ret = system_info_get_platform_string(SYSTEM_INFO_MANUF_NAME,
+ &platform_info->manufacturerName);
+ if (SYSTEM_INFO_ERROR_NONE != ret) {
+ LOG_ERR("system_info_get_platform_string(manufacturer) Fail(%d)", ret);
+ free(platform_info->platformID);
+ return -1;
+ }
+
+ ret = system_info_get_platform_string(SYSTEM_INFO_MODEL_NAME,
+ &platform_info->modelNumber);
+ if (SYSTEM_INFO_ERROR_NONE != ret)
+ LOG_ERR("system_info_get_platform_string(model_name) Fail(%d)", ret);
+
+ ret = system_info_get_platform_string(SYSTEM_INFO_PLATFORM_VERSION,
+ &platform_info->platformVersion);
+ if (SYSTEM_INFO_ERROR_NONE != ret)
+ LOG_ERR("system_info_get_platform_string(platform_version) Fail(%d)", ret);
+
+ ret = system_info_get_platform_string(SYSTEM_INFO_BUILD_STRING,
+ &platform_info->firmwareVersion);
+ if (SYSTEM_INFO_ERROR_NONE != ret)
+ LOG_ERR("system_info_get_platform_string(build_string) Fail(%d)", ret);
+
+ /* platform_info.manufacturerUrl */
+ /* platform_info.dateOfManufacture */
+ /* platform_info.operatingSystemVersion */
+ /* platform_info.hardwareVersion */
+ /* platform_info.supportUrl */
+ /* platform_info.systemTime */
+
+ return 0;
+}
+
+int comp_iot_initialize()
+{
+ char *device_id = NULL;
+ int ret;
+ OCPlatformInfo platform_info = {0};
+
+ ps.open = client_open;
+ ps.read = fread;
+ ps.write = fwrite;
+ ps.close = fclose;
+ ps.unlink = unlink;
+
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Server,
+ (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
+ OC::QualityOfService::LowQos,
+ &ps
+ };
+
+ OCPlatform::Configure(cfg);
+
+ ret = __comp_iot_get_platform_info(&platform_info);
+ if (ret != 0) {
+ LOG_ERR("Failed to get platform info %d", ret);
+ return COMP_ERROR_NONE;
+ }
+
+ free(platform_info.platformID);
+ free(platform_info.manufacturerName);
+ free(platform_info.modelNumber);
+ free(platform_info.platformVersion);
+ free(platform_info.firmwareVersion);
+
+ ret = OCPlatform::registerPlatformInfo(platform_info);
+
+ if (ret != OC_STACK_OK)
+ {
+ LOG_ERR("Platform Registration failed");
+ return COMP_ERROR_NONE;
+ }
+
+ OCDeviceInfo device_info = {0};
+ device_info.deviceName = g_strdup("UNKNOWN");
+
+ ret = OCPlatform::registerDeviceInfo(device_info);
+
+ if (ret != OC_STACK_OK)
+ {
+ LOG_ERR("Device Registration failed");
+ return COMP_ERROR_NONE;
+ }
+
+ g_free(device_info.deviceName);
+
+#if 0
+ int ret = iotcon_initialize(data_dir);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_initialize: Failed %s", get_error_message(ret));
+ return false;
+ }
+ LOG_DEBUG("iotcon_initialize : %s", get_error_message(ret));
+#endif
+
+ OicUuid_t uuid;
+
+ ret = GetDoxmDeviceID(&uuid);
+ if (OC_STACK_OK != ret)
+ LOG_DEBUG("GetDoxmDevOwnerId failed = [%d][%s]", ret, get_error_message(ret));
+
+ ret = ConvertUuidToStr(&uuid, &device_id);
+ if (OC_STACK_OK != ret)
+ LOG_DEBUG("ConvertUuidToStr failed = [%d][%s]", ret, get_error_message(ret));
+
+ LOG_DEBUG("device_id : %s", device_id);
+
+ comp_context_t *comp_ctx = comp_context_get_context();
+ comp_ctx->device_uuid = g_strdup(device_id);
+
+ /* Do Self-Ownership Transfer */
+ ret = OCSecure::configSelfOwnership();
+#if 0
+ ret = OCConfigSelfOwnership();
+#endif
+ if (OC_STACK_OK != ret ) {
+ LOG_ERR( "OCConfigSelfOwnership() error = [%d][%s]", ret, get_error_message(ret));
+ }
+
+ /*
+ "operation" resource
+ The operation resource is a control command channel between daemon and daemon.
+ resource type is core.comp.operation
+ Within this function we create a default operation resource. (uri is /comp/operation/1)
+ If we need more control channels, increase the number.
+ ex) /comp/operation/2, /comp/operation/3
+ */
+ comp_iot_add_resource(COMP_RESOURCE_TYPE_OPERATION, "1");
+
+ /*
+ "DATA" resource
+ To show the send data to remote daemon.
+ It can be used when the remote device is mot enable and pairwise.
+ ex) /comp/data/1
+ */
+ comp_iot_add_resource(COMP_RESOURCE_TYPE_DATA, "1");
+
+ return COMP_ERROR_NONE;
+}
+
+OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> request)
+{
+ LOG_DEBUG("_request_handler is called");
+
+ if (request) {
+ std::string requestType = request->getRequestType();
+ int requestFlag = request->getRequestHandlerFlag();
+
+ LOG_DEBUG("request type %s flag %x", requestType.c_str(), requestFlag);
+ }
+
+ return OC_EH_OK;
+ //get resource element from comp resource list or, parsing resource uri
+
+ //if request type is "get" and resource type is "group",
+ //then OWNER send device information to CLIENT(Device info Exchange)
+ //if request type is "put" and resource type is "operation",
+ //then It is join request. CLIENT send device information to OWNER(Device info Exchange)
+ //resource type "operation" don't have "get" request type.
+
+#if 0
+ int ret;
+ iotcon_request_type_e type;
+
+ ret = iotcon_request_get_request_type(request, &type);
+ if (IOTCON_ERROR_NONE != ret)
+ return;
+
+ LOG_DEBUG("iotcon request type %d", type);
+ if (IOTCON_REQUEST_POST == type) {
+ iotcon_response_h response = NULL;
+ iotcon_representation_h req_repr;
+ iotcon_attributes_h attributes;
+ char *cmd;
+ char *arg = NULL;
+ int result;
+ comp_request_type_e command;
+
+ ret = iotcon_request_get_representation(request, &req_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_request_get_representation() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_representation_get_attributes(req_repr, &attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_representation_get_attributes() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_attributes_get_str(attributes, "CMD", &cmd);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_attributes_get_bool() Fail(%d)", ret);
+ return;
+ }
+
+ LOG_DEBUG("cmd %s", cmd);
+
+ command = string2command(cmd);
+
+ if (command == COMP_REQ_CREATE_GROUP) {
+ LOG_DEBUG("Request create group");
+ char *group_name;
+ iotcon_attributes_get_str(attributes, "name", &group_name);
+
+ LOG_DEBUG("group_name : %s", group_name);
+ result = comp_group_create(group_name);
+ arg = g_strdup(group_name);
+
+ } else if (command == COMP_REQ_INVITE_DEVICE) {
+ LOG_DEBUG("Request invite");
+ char *group_name;
+ char *uuid;
+
+ iotcon_attributes_get_str(attributes, "name", &group_name);
+ iotcon_attributes_get_str(attributes, "id", &uuid);
+
+ LOG_DEBUG("group_name : %s, UUID : %s", group_name, uuid);
+
+ result = comp_group_invite(group_name, uuid, "12341234");
+ arg = g_strdup(uuid);
+
+ } else if (command == COMP_REQ_EJECT_DEVICE) {
+ LOG_DEBUG("Request eject");
+ char *group_name;
+ char *uuid;
+ comp_context_t *comp_ctx = comp_context_get_context();
+
+ iotcon_attributes_get_str(attributes, "name", &group_name);
+ iotcon_attributes_get_str(attributes, "id", &uuid);
+
+ LOG_DEBUG("group_name : %s, Self UUID : %s Target UUID : %s",
+ group_name, comp_ctx->device_uuid, uuid);
+
+ result = comp_group_dismiss(comp_ctx->device_uuid, uuid);
+ arg = g_strdup(uuid);
+
+ } else if (command == COMP_REQ_DELETE_GROUP) {
+ LOG_DEBUG("Request delete group");
+ arg = g_strdup("DELETED");
+ } else if (command == COMP_REQ_SEND_DATA) {
+#ifdef SUPPORT_BASE64_ENCODING
+ int payload_len;
+ char *data = NULL;
+
+ LOG_DEBUG("Receive Data");
+
+ iotcon_attributes_get_str(attributes, "data", &data);
+ /*
+ * BASE64 encoding/decoding system use '=' as padding byte
+ * But, query parameter does not allow use '=' character.Basically,
+ * So, in order to use BASE64 as query parameters, we need additioinal length param
+ * such as 'len=xx'
+ */
+ payload_len = strlen(data); /* This data may be cliped the last padding 1~2 bytes ('='/'==') */
+
+ LOG_DEBUG("data = %s payload_len = %d", data, payload_len);
+
+ size_t outSize = B64DECODE_OUT_SAFESIZE(payload_len + 1);
+ uint8_t* out = g_malloc0(outSize);
+ if (NULL == out) {
+ LOG_ERR("Can't allocate memory for base64 str");
+ return;
+ }
+ uint32_t len = 0;
+
+ if(B64_OK == b64Decode(data, payload_len, out, outSize, &len)) {
+ LOG_ERR("Base64 decoding failed.");
+ return;
+ }
+ memcpy(arg, out, len);
+
+ LOG_DEBUG("successfully decoded to base64. %s", arg);
+
+ iotcon_query_remove(query, "data");
+#else
+ char *data = NULL;
+ iotcon_attributes_get_str(attributes, "data", &data);
+ LOG_DEBUG("Receive Data = %s", data);
+ arg = g_strdup(data);
+#endif
+ } else {
+ LOG_ERR("Unknown request command");
+ }
+
+ notify_request_result(cmd, arg, result);
+
+ free(arg);
+
+ ret = iotcon_response_create(request, &response);
+ if (IOTCON_ERROR_NONE != ret)
+ return;
+
+ ret = iotcon_response_set_result(response, IOTCON_RESPONSE_OK);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_response_set_result Faild = %d", ret);
+ iotcon_response_destroy(response);
+ return;
+ }
+
+ ret = iotcon_response_send(response);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_response_send Faild = %d", ret);
+ iotcon_response_destroy(response);
+ return;
+ }
+
+ iotcon_response_destroy(response);
+ }
+#endif
+}
+
+int comp_iot_add_resource(comp_resource_type_e resource_type, const char *uri)
+{
+ //char uri_path[PATH_MAX] = {0,};
+ OCResourceHandle resourceHandle;
+ std::string uri_path;
+
+ uri_path = std::string(comp_resource_get_uri_prefix(resource_type)) + std::string(uri);
+ //strncpy(uri_path, comp_resource_get_uri_prefix(resource_type), PATH_MAX);
+ //strncat(uri_path, uri, PATH_MAX);
+
+ LOG_DEBUG("[ADD] resource uri is %s", uri_path.c_str());
+
+ EntityHandler cb = std::bind(&_request_handler, std::placeholders::_1);
+
+ OCStackResult result = OCPlatform::registerResource(resourceHandle,
+ uri_path,
+ std::string(comp_resource_get_type(resource_type)),
+ std::string(DEFAULT_INTERFACE), cb,
+ comp_resource_get_policies(resource_type));
+ if (result != OC_STACK_OK) {
+ LOG_ERR("Failed to create resource");
+ return COMP_ERROR_NONE;
+ }
+
+ LOG_DEBUG("Successfully created resource");
+ return COMP_ERROR_NONE;
+
+#if 0
+ iotcon_resource_h resource = NULL;
+ iotcon_resource_types_h resource_types = NULL;
+ iotcon_resource_interfaces_h resource_ifaces = NULL;
+ char uri_path[PATH_MAX] = {0,};
+
+ int ret = iotcon_resource_types_create(&resource_types);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_resource_types_create failed! : %s", get_error_message(ret));
+ goto EXIT;
+ }
+
+ ret = iotcon_resource_types_add(resource_types, comp_resource_get_type(resource_type));
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_resource_types_add failed! : %s", get_error_message(ret));
+ goto EXIT;
+ }
+
+ ret = iotcon_resource_interfaces_create(&resource_ifaces);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_resource_interfaces_create failed! : %s", get_error_message(ret));
+ goto EXIT;
+ }
+
+ ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_DEFAULT);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_resource_interfaces_add failed! : %s", get_error_message(ret));
+ goto EXIT;
+ }
+
+ strncpy(uri_path, comp_resource_get_uri_prefix(resource_type), PATH_MAX);
+ strncat(uri_path, uri, PATH_MAX);
+
+ LOG_DEBUG("[ADD] resource uri is %s", uri_path);
+ //name duplication check
+
+ ret = iotcon_resource_create(uri_path, resource_types, resource_ifaces,
+ comp_resource_get_policies(resource_type), _request_handler, NULL, &resource);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_resource_create failed! : %s", get_error_message(ret));
+ goto EXIT;
+ }
+
+ //get resource list of resource type
+ ret = comp_resource_append(resource_type, uri, resource);
+ if (COMP_ERROR_NONE != ret) {
+ LOG_ERR("comp_resource_append failed! : %s", comp_log_get_error_string(ret));
+ goto EXIT;
+ }
+
+EXIT:
+ if (resource_types != NULL)
+ iotcon_resource_types_destroy(resource_types);
+
+ if (resource_ifaces != NULL)
+ iotcon_resource_interfaces_destroy(resource_ifaces);
+
+ return COMP_ERROR_NONE;
+#endif
+}
+
+static bool _get_res_type_cb(const char *string, void *user_data)
+{
+ char **resource_type = (char **)user_data;
+
+ *resource_type = g_strdup(string);
+
+ LOG_DEBUG("resource type : %s", *resource_type);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+int found_group_count = 0;
+
+static void _clear_user_data(void *user_data)
+{
+ comp_command_t *cmd = (comp_command_t *)user_data;
+
+ if (NULL == cmd)
+ return;
+
+ if (cmd->tid) {
+ g_source_remove(cmd->tid);
+ }
+
+ if (cmd->uuid) {
+ g_free(cmd->uuid);
+ cmd->uuid = NULL;
+ }
+ if (cmd->host) {
+ g_free(cmd->host);
+ cmd->host = NULL;
+ }
+ if (cmd->arg1) {
+ g_free(cmd->arg1);
+ cmd->arg1 = NULL;
+ }
+ if (cmd->arg2) {
+ g_free(cmd->arg2);
+ cmd->arg2 = NULL;
+ }
+ if (cmd->arg3) {
+ g_free(cmd->arg3);
+ cmd->arg3 = NULL;
+ }
+ if (cmd->arg4) {
+ g_free(cmd->arg4);
+ cmd->arg4 = NULL;
+ }
+}
+
+static void _on_get(iotcon_remote_resource_h resource, iotcon_error_e err,
+ iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
+{
+ last_get_result = err;
+
+ int ret;
+ iotcon_response_result_e response_result;
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_OK != response_result) {
+ LOG_ERR("_on_get Response error(%d)", response_result);
+ return;
+ }
+
+ /* get the resource host address */
+ char *resource_host = NULL;
+ iotcon_remote_resource_get_host_address(resource, &resource_host);
+ LOG_DEBUG("resource host : %s", resource_host);
+
+ iotcon_remote_resource_destroy(resource);
+
+ _clear_user_data(user_data);
+}
+
+static bool _found_resource(iotcon_remote_resource_h resource,
+ iotcon_error_e result, void *user_data)
+{
+ int ret;
+ char *resource_uri_path = NULL;
+ iotcon_resource_types_h resource_types;
+ char *resource_device_id;
+ char *resource_host;
+ char *resource_type;
+ comp_group_type_e group_type;
+
+ /* Callback: When no more resource available.
+ As timeout is configured by IOTCON Library*/
+ if (resource == NULL) {
+ LOG_ERR("No more resource available to find !!");
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ /* get the resource URI */
+ ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to get resource uri path");
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+
+ LOG_DEBUG("Resource Found: Resource uri : %s", resource_uri_path);
+
+ /* get the resource device id */
+ ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to get resource device id");
+ return IOTCON_FUNC_CONTINUE;
+ }
+ LOG_DEBUG("resource device id : %s", resource_device_id);
+
+ /* get the resource host address */
+ ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to get resource host address");
+ return IOTCON_FUNC_CONTINUE;
+ }
+ LOG_DEBUG("resource host : %s", resource_host);
+
+ ret = iotcon_remote_resource_get_types(resource, &resource_types);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to get resource types");
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
+ &resource_type);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to get foreach resource types");
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ if (strcmp(resource_type, "core.comp.group") == 0) {
+ comp_context_t *comp_ctx = comp_context_get_context();
+ if (strcmp(resource_device_id, comp_ctx->device_uuid) == 0)
+ group_type = COMP_GROUP_DEVICE;
+ else
+ group_type = COMP_GROUP_REMOTE_DEVICE;
+
+ char *temp = strdup(resource_uri_path);
+ char *ptr = strtok(temp, "/");
+ ptr = strtok(NULL, "/");
+ ptr = strtok(NULL, "/");
+
+ LOG_DEBUG("group name is %s", ptr);
+ comp_group_add_new(resource_uri_path, resource_device_id,
+ ptr, resource_host, resource_type, group_type);
+ found_group_count++;
+
+ g_free(resource_type);
+
+ return IOTCON_FUNC_CONTINUE;
+ } else if (strcmp(resource_type, "core.comp.data") == 0) {
+ comp_command_t *cmd = (comp_command_t *)user_data;
+
+ if (cmd != NULL && strcmp(cmd->uuid, resource_device_id) == 0) {
+ LOG_DEBUG("Request to Remote Device");
+
+ iotcon_remote_resource_h resource_clone = NULL;
+ iotcon_representation_h repr;
+ iotcon_attributes_h attributes;
+
+ ret = iotcon_remote_resource_clone(resource, &resource_clone);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_remote_resource_clone failed = %d", ret);
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ ret = iotcon_representation_create(&repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_representation_create() Fail(%d)", ret);
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ ret = iotcon_attributes_create(&attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_attributes_create() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ LOG_DEBUG("CMD = %s", command2string(cmd->command));
+
+ if (cmd->command == COMP_REQ_CREATE_GROUP) { //request create group
+ iotcon_attributes_add_str(attributes, "CMD", "1");
+ iotcon_attributes_add_str(attributes, "name", cmd->arg1);
+ } else if (cmd->command == COMP_REQ_INVITE_DEVICE) { //request invite
+ iotcon_attributes_add_str(attributes, "CMD", "2");
+ iotcon_attributes_add_str(attributes, "name", cmd->arg1);
+ iotcon_attributes_add_str(attributes, "id", cmd->arg2);
+ iotcon_attributes_add_str(attributes, "PIN", cmd->arg3);
+ } else if (cmd->command == COMP_REQ_EJECT_DEVICE) { //request eject
+ iotcon_attributes_add_str(attributes, "CMD", "3");
+ iotcon_attributes_add_str(attributes, "name", cmd->arg1);
+ iotcon_attributes_add_str(attributes, "id", cmd->arg2);
+ } else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group
+ iotcon_attributes_add_str(attributes, "CMD", "4");
+ iotcon_attributes_add_str(attributes, "name", cmd->arg1);
+ } else { /* Send Data */
+ iotcon_attributes_add_str(attributes, "CMD", "5");
+#ifdef SUPPORT_BASE64_ENCODING
+ uint32_t outSize = 0;
+ size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((cmd->arg1_len + 1));
+ char* b64Buf = g_malloc0(b64BufSize);
+ if (NULL == b64Buf) {
+ iotcon_remote_resource_destroy(resource_clone);
+ _clear_user_data(cmd);
+ return IOTCON_FUNC_CONTINUE;
+ }
+ b64Encode((const char *)cmd->arg1, cmd->arg1_len, b64Buf,
+ b64BufSize, &outSize);
+ b64Buf[b64BufSize] = '\0';
+ iotcon_attributes_add_str(attributes, "data", b64Buf);
+ LOG_DEBUG("b64BufSize =%d outSize = %d b64Buf = %s", b64BufSize,
+ outSize, b64Buf);
+ g_free(b64Buf);
+#else
+ iotcon_attributes_add_str(attributes, "data", cmd->arg1);
+#endif
+ }
+
+ ret = iotcon_representation_set_attributes(repr, attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
+ iotcon_attributes_destroy(attributes);
+ iotcon_representation_destroy(repr);
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ iotcon_attributes_destroy(attributes);
+
+ ret = iotcon_remote_resource_post(resource_clone, repr, NULL,
+ _on_get, NULL);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_remote_resource_put() Fail(%d)", ret);
+ iotcon_remote_resource_destroy(resource_clone);
+ _clear_user_data(cmd);
+ return IOTCON_FUNC_CONTINUE;
+ }
+
+ iotcon_representation_destroy(repr);
+ }
+
+ }
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+static gboolean _timeout_cb(gpointer data)
+{
+ int ret = -1;
+ comp_command_t *cmd = (comp_command_t *)data;
+
+ if (cmd->resource_type == COMP_RESOURCE_TYPE_GROUP) {
+ if (found_group_count > 0)
+ ret = 0;
+
+ notify_group_find_finish(ret);
+
+ } else if (cmd->resource_type == COMP_RESOURCE_TYPE_DATA) {
+ notify_send_data_finish("RESP_DATA", last_get_result);
+ }
+
+ _clear_user_data(data);
+
+ return false;
+}
+
+int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, void *user_data)
+{
+ int ret;
+ iotcon_query_h query;
+ comp_command_t *cmd = (comp_command_t *) user_data;
+
+ found_group_count = 0;
+
+ ret = iotcon_set_timeout(timeout);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to set timeout value");
+ _clear_user_data(user_data);
+ return COMP_ERROR_OPERATION_FAILED;
+ }
+
+ ret = iotcon_query_create(&query);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to create iotcon query");
+ _clear_user_data(user_data);
+ return COMP_ERROR_OPERATION_FAILED;
+ }
+
+ ret = iotcon_query_set_resource_type(query,
+ comp_resource_get_type(resource_type));
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("Failed to set iotcon query resource type");
+ _clear_user_data(user_data);
+ return COMP_ERROR_OPERATION_FAILED;
+ }
+
+ LOG_DEBUG("Set Resource Type : %s", get_error_message(ret));
+
+ ret = iotcon_set_timeout(timeout);
+ if (IOTCON_ERROR_NONE != ret) {
+ LOG_ERR("iotcon_set_timeout: Failed %s", get_error_message(ret));
+ }
+
+ if (cmd && (cmd->command == COMP_REQ_SEND_DATA_ALL))
+ ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS,
+ IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
+ query, _found_resource, user_data);
+ else
+ ret = iotcon_find_resource(cmd->host,
+ IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
+ query, _found_resource, user_data);
+
+ cmd->resource_type = resource_type;
+ cmd->tid = g_timeout_add_seconds(timeout + 1, _timeout_cb, cmd);
+
+ LOG_DEBUG("find resource : %s", get_error_message(ret));
+
+ return ret;
+}
+
+int comp_iot_delete_resource(comp_resource_type_e resource_type)
+{
+ //delete resource from resource list using resource_type and index
+ //delete DB
+ return COMP_ERROR_NONE;
+}
+
+int comp_iot_deinitialize()
+{
+ return COMP_ERROR_NONE;
+}