comp-manager: Send command directly without discovering resource
authorSaurav Babu <saurav.babu@samsung.com>
Wed, 4 Apr 2018 11:52:38 +0000 (17:22 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:49 +0000 (19:38 +0900)
This patch sends command directly without discovering if resource is
already discovered

Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
src/companion-manager/src/comp_group.c
src/companion-manager/src/comp_iot.cpp

index a09bc89..c3bd8ba 100755 (executable)
@@ -634,15 +634,11 @@ int comp_group_unpair_resource(gchar *uuid_dev1, gchar *uuid_dev2)
        return ret;
 }
 
-int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
-                                                int len)
+static char *_addr2host(char *addr, int port)
 {
-       int ret;
        char ip[50];
        char *percent;
 
-       LOG_BEGIN();
-
        g_strlcpy(ip, addr, strlen(addr) + 1);
 
        /* Remove % from address */
@@ -650,16 +646,26 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
        if (percent)
                percent[0] = '\0';
 
-       comp_command_t *cmd = g_new0(comp_command_t, 1);
-       cmd->command = COMP_REQ_SEND_DATA;
-       cmd->uuid = g_strdup(uuid_dev);
-
        /* IPv6 Address should be encoded for RFC6874  */
        if (strchr(ip, ':')) /* IPv6 Adress */
-               cmd->host = g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
+               return g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
                                                                        percent + 1, port);
        else /* IPv4 Address */
-               cmd->host = g_strdup_printf("coaps://%s:%d", ip, port);
+               return g_strdup_printf("coaps://%s:%d", ip, port);
+}
+
+int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
+                                                int len)
+{
+       int ret;
+
+       LOG_BEGIN();
+
+       comp_command_t *cmd = g_new0(comp_command_t, 1);
+       cmd->command = COMP_REQ_SEND_DATA;
+       cmd->uuid = g_strdup(uuid_dev);
+
+       cmd->host = _addr2host(addr, port);
 
        cmd->arg1 = g_malloc0(len + 1);
        if (NULL == cmd->arg1) {
@@ -917,9 +923,29 @@ int comp_group_get_group_device_id(comp_group_t *handle)
        return ret;
 }
 
+static comp_mot_device_t *_check_device_in_mot_enb_dev_list(char *uuid)
+{
+       comp_mot_device_t *device;
+       GList *iter;
+
+       iter = mot_enb_dev_list;
+
+       while (iter != NULL) {
+               device = (comp_mot_device_t *)iter->data;
+
+               if (g_strcmp0(device->device_id, uuid) == 0) {
+                       LOG_DEBUG("Device %s is available in mot enable device list", uuid);
+                       return device;
+               }
+       }
+
+       return NULL;
+}
+
 int comp_group_request_create_group(char *uuid, char *group_name)
 {
        int ret = COMP_ERROR_NONE;
+       comp_mot_device_t *device;
 
        LOG_ERR("[Request Create Group] %s", group_name);
        LOG_ERR("[Request Create Group] to %s", uuid);
@@ -929,10 +955,20 @@ int comp_group_request_create_group(char *uuid, char *group_name)
        cmd->uuid = g_strdup(uuid);
        cmd->arg1 = g_strdup(group_name);
 
-       ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
-       if (ret != COMP_ERROR_NONE) {
-               LOG_ERR("Failed to discover resource : %s",
-                                comp_log_get_error_string(ret));
+       device = _check_device_in_mot_enb_dev_list(uuid);
+       if (device) {
+               cmd->host = _addr2host(device->addr, device->secure_port);
+               ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to send data : %s",
+                                       comp_log_get_error_string(ret));
+               }
+       } else {
+               ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to discover resource : %s",
+                                       comp_log_get_error_string(ret));
+               }
        }
 
        return ret;
@@ -941,6 +977,7 @@ int comp_group_request_create_group(char *uuid, char *group_name)
 int comp_group_request_invite(char *uuid, char *group_name, char *target_uuid, char *PIN)
 {
        int ret = COMP_ERROR_NONE;
+       comp_mot_device_t *device;
 
        LOG_ERR("[Request Invite] %s, %s, %s", group_name, target_uuid, PIN);
        LOG_ERR("[Request Invite] to %s", uuid);
@@ -952,10 +989,20 @@ int comp_group_request_invite(char *uuid, char *group_name, char *target_uuid, c
        cmd->arg2 = g_strdup(target_uuid);
        cmd->arg3 = g_strdup(PIN);
 
-       ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
-       if (ret != COMP_ERROR_NONE) {
-               LOG_ERR("Failed to discover resource : %s",
-                                comp_log_get_error_string(ret));
+       device = _check_device_in_mot_enb_dev_list(uuid);
+       if (device) {
+               cmd->host = _addr2host(device->addr, device->secure_port);
+               ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to send data : %s",
+                                       comp_log_get_error_string(ret));
+               }
+       } else {
+               ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to discover resource : %s",
+                                       comp_log_get_error_string(ret));
+               }
        }
 
        return ret;
@@ -965,6 +1012,7 @@ int comp_group_request_invite(char *uuid, char *group_name, char *target_uuid, c
 int comp_group_request_eject(char *uuid, char *group_name, char *target_uuid)
 {
        int ret = COMP_ERROR_NONE;
+       comp_mot_device_t *device;
 
        LOG_ERR("[Request Eject] %s, %s", group_name, target_uuid);
        LOG_ERR("[Request Eject] to %s", uuid);
@@ -975,10 +1023,20 @@ int comp_group_request_eject(char *uuid, char *group_name, char *target_uuid)
        cmd->arg1 = g_strdup(group_name);
        cmd->arg2 = g_strdup(target_uuid);
 
-       ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
-       if (ret != COMP_ERROR_NONE) {
-               LOG_ERR("Failed to discover resource : %s",
-                                comp_log_get_error_string(ret));
+       device = _check_device_in_mot_enb_dev_list(uuid);
+       if (device) {
+               cmd->host = _addr2host(device->addr, device->secure_port);
+               ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to send data : %s",
+                                       comp_log_get_error_string(ret));
+               }
+       } else {
+               ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to discover resource : %s",
+                                       comp_log_get_error_string(ret));
+               }
        }
 
        return ret;
@@ -989,6 +1047,7 @@ int comp_group_request_eject(char *uuid, char *group_name, char *target_uuid)
 int comp_group_request_delete_group(char *uuid, char *group_name)
 {
        int ret = COMP_ERROR_NONE;
+       comp_mot_device_t *device;
 
        LOG_ERR("[Request Delete Group] %s", group_name);
        LOG_ERR("[Request Delete Group] to %s", uuid);
@@ -998,10 +1057,20 @@ int comp_group_request_delete_group(char *uuid, char *group_name)
        cmd->uuid = g_strdup(uuid);
        cmd->arg1 = g_strdup(group_name);
 
-       ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
-       if (ret != COMP_ERROR_NONE) {
-               LOG_ERR("Failed to discover resource : %s",
-                                comp_log_get_error_string(ret));
+       device = _check_device_in_mot_enb_dev_list(uuid);
+       if (device) {
+               cmd->host = _addr2host(device->addr, device->secure_port);
+               ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to send data : %s",
+                                       comp_log_get_error_string(ret));
+               }
+       } else {
+               ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+               if (ret != COMP_ERROR_NONE) {
+                       LOG_ERR("Failed to discover resource : %s",
+                                       comp_log_get_error_string(ret));
+               }
        }
 
        return ret;
index 1f50e06..5126930 100755 (executable)
@@ -578,6 +578,8 @@ static void _on_post(const HeaderOptions& /*headerOptions*/,
                                         void *user_data)
 {
        int ret;
+       comp_command_t *cmd = (comp_command_t *)user_data;
+
        try {
                if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED ||
                        eCode == OC_STACK_RESOURCE_CHANGED) {
@@ -592,6 +594,7 @@ static void _on_post(const HeaderOptions& /*headerOptions*/,
        }
 
        notify_send_data_finish("RESP_DATA", last_get_result);
+
        _clear_user_data(user_data);
 }
 
@@ -804,8 +807,28 @@ int comp_iot_send_data(comp_resource_type_e resource_type, int timeout,
                }
 
                OCRepresentation rep;
-               rep.setValue("CMD", COMP_REQ_SEND_DATA);
-               rep.setValue("data", std::string(cmd->arg1));
+               rep.setValue("CMD", cmd->command);
+               switch (cmd->command) {
+               case COMP_REQ_SEND_DATA:
+                       rep.setValue("data", std::string(cmd->arg1));
+                       break;
+               case COMP_REQ_CREATE_GROUP:
+               case COMP_REQ_DELETE_GROUP:
+                       rep.setValue("name", std::string(cmd->arg1));
+                       break;
+               case COMP_REQ_INVITE_DEVICE:
+                       rep.setValue("name", std::string(cmd->arg1));
+                       rep.setValue("id", std::string(cmd->arg2));
+                       rep.setValue("PIN", std::string(cmd->arg3));
+                       break;
+               case COMP_REQ_EJECT_DEVICE:
+                       rep.setValue("name", std::string(cmd->arg1));
+                       rep.setValue("id", std::string(cmd->arg2));
+                       break;
+               default:
+                       LOG_ERR("Invalid command %d", cmd->command);
+                       return -1;
+               }
 
                ret = resource->post(rep, QueryParamsMap(),
                                                   std::bind (&_on_post, std::placeholders::_1,