comp-manager: Send KeepAlive message after device invite is done
authorSaurav Babu <saurav.babu@samsung.com>
Wed, 18 Apr 2018 14:02:05 +0000 (19:32 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:50 +0000 (19:38 +0900)
Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
src/companion-manager/include/comp_enum.h
src/companion-manager/include/comp_group.h
src/companion-manager/src/comp_group.c
src/companion-manager/src/comp_iot.cpp

index 87dc9a2..2a4830b 100644 (file)
@@ -81,6 +81,7 @@ typedef enum {
        COMP_REQ_INVITE_DEVICE, /**< Invite Device */
        COMP_REQ_EJECT_DEVICE, /**< Exile Device */
        COMP_REQ_DELETE_GROUP, /**< Delete Group */
+       COMP_REQ_KEEP_ALIVE, /**< Keep Alive Message */
        COMP_REQ_SEND_DATA_ALL, /**< Send data in subnet */
 } comp_request_type_e;
 
index cdc6646..4b434b9 100755 (executable)
@@ -51,6 +51,7 @@ typedef struct {
 } comp_mot_device_t;
 
 typedef struct {
+       char *group_name;
        char *uuid_dev1; /**< Source device Id */
        char *uuid_dev2; /**< Target device Id */
        char *pin; /**< Random or pre-configured PIN */
@@ -64,6 +65,13 @@ typedef struct {
        int permission_2; /**< CRUDN for source device */
 } comp_group_invite_info_t;
 
+typedef struct {
+       char *group_name; /**< Group Name */
+       char *uuid; /**< Device ID */
+       char *host; /**< Device Host Address */
+       int count; /**< Count of successful post method */
+} comp_invited_device_t;
+
 /* Called when daemon is start. */
 int comp_group_initialize();
 
@@ -106,6 +114,7 @@ int comp_group_pair_resource(char* target1, char *subject1, char *uri1,
 int comp_group_unpair_resource(gchar *uuid_dev1, gchar *uuid_dev2);
 int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port,
                                                 unsigned char *data, int len);
+void comp_group_notify_keepalive_response(comp_command_t *cmd);
 
 GVariant *comp_group_get_remote_mot_enabled_devices();
 int comp_group_get_mot_device_count();
index 2e888f1..51942a3 100644 (file)
@@ -25,6 +25,7 @@
 GList *found_group_list;
 GList *mot_enb_dev_list = NULL;
 GList *mowned_dev_list = NULL;
+GList *invited_dev_list = NULL;
 comp_group_invite_info_t *group_invite_info = NULL;
 comp_mot_device_t *my_device = NULL;
 
@@ -417,6 +418,54 @@ int comp_group_find_mot_enabled_devices(int timeout)
        return ret;
 }
 
+void comp_group_notify_keepalive_response(comp_command_t *cmd)
+{
+       GList *iter = NULL;
+       gboolean is_exist = FALSE;
+       comp_invited_device_t *temp;
+
+       LOG_DEBUG("Response received from uuid %s", cmd->uuid);
+
+       iter = invited_dev_list;
+       while (iter != NULL) {
+               temp = (comp_invited_device_t *)iter->data;
+
+               if ((g_strcmp0(temp->group_name, cmd->arg1) == 0) &&
+                       (g_strcmp0(temp->uuid, cmd->uuid) == 0)) {
+                       LOG_DEBUG("Device %s count %d found in invited device list",
+                                         cmd->uuid, temp->count);
+                       is_exist = TRUE;
+                       break;
+               }
+
+               iter = g_list_next(iter);
+       }
+
+       if (is_exist == TRUE)
+               temp->count += 1;
+}
+
+int comp_group_request_keepalive(char *uuid, char *group_name, char *host)
+{
+       int ret = COMP_ERROR_NONE;
+
+       LOG_DEBUG("[Request KeepAlive] to %s", uuid);
+       comp_command_t *cmd = g_new0(comp_command_t, 1);
+       cmd->command = COMP_REQ_KEEP_ALIVE;
+       cmd->uuid = g_strdup(uuid);
+       cmd->arg1 = g_strdup(group_name);
+       cmd->host = g_strdup(host);
+
+       ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 2, cmd);
+       if (ret != COMP_ERROR_NONE) {
+               LOG_ERR("Failed to send data : %s",
+                               comp_log_get_error_string(ret));
+               return ret;
+       }
+
+       return 0;
+}
+
 comp_group_invite_info_t *comp_group_get_invite_info()
 {
        return group_invite_info;
@@ -427,6 +476,7 @@ void comp_group_free_invite_info()
        LOG_DEBUG("Remove Group Invite Information %p", group_invite_info);
 
        if (group_invite_info) {
+               g_free(group_invite_info->group_name);
                g_free(group_invite_info->uuid_dev1);
                g_free(group_invite_info->uuid_dev2);
                g_free(group_invite_info->pin);
@@ -442,8 +492,95 @@ void comp_group_free_invite_info()
        }
 }
 
+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;
+}
+
+static char *_addr2host(char *addr, int port)
+{
+       char ip[50];
+       char *percent;
+
+       g_strlcpy(ip, addr, strlen(addr) + 1);
+
+       /* Remove % from address */
+       percent = strchr(ip, '%');
+       if (percent)
+               percent[0] = '\0';
+
+       /* IPv6 Address should be encoded for RFC6874  */
+       if (strchr(ip, ':')) /* IPv6 Adress */
+               return g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
+                                                                       percent + 1, port);
+       else /* IPv4 Address */
+               return g_strdup_printf("coaps://%s:%d", ip, port);
+}
+
 void comp_group_notify_group_invite(int ret)
 {
+       /*
+        * ret = 0 OC_STACK_OK
+        * ret = 49 OC_STACK_DUPLICATE_UUID
+        */
+       if ((ret == 0 || ret == 49) && group_invite_info) {
+               comp_invited_device_t *device;
+               GList *iter = NULL;
+               gboolean is_exist = FALSE;
+
+               device = g_try_malloc0(sizeof(comp_invited_device_t));
+               device->group_name = g_strdup(group_invite_info->group_name);
+               device->uuid = g_strdup(group_invite_info->uuid_dev2);
+
+               iter = invited_dev_list;
+               while (iter != NULL) {
+                       comp_invited_device_t *temp = (comp_invited_device_t *)iter->data;
+
+                       if ((g_strcmp0(temp->group_name, device->group_name) == 0) &&
+                               (g_strcmp0(temp->uuid, device->uuid) == 0)) {
+                               LOG_DEBUG("Device %s already exists in invited device list",
+                                                 temp->uuid);
+                               is_exist = TRUE;
+                               break;
+                       }
+
+                       iter = g_list_next(iter);
+               }
+
+               if (is_exist == FALSE) {
+                       comp_mot_device_t *temp;
+                       temp = _check_device_in_mot_enb_dev_list(device->uuid);
+                       if (temp)
+                               device->host = _addr2host(temp->addr, temp->secure_port);
+
+                       device->count = 0;
+                       LOG_DEBUG("Add device %s group %s host %s to invited device list",
+                                         device->uuid, device->group_name, device->host);
+                       invited_dev_list = g_list_prepend(invited_dev_list, device);
+
+                       comp_group_request_keepalive(device->uuid, device->group_name,
+                                                                                device->host);
+               } else {
+                       g_free(device->uuid);
+                       g_free(device->group_name);
+                       g_free(device);
+               }
+       }
+
        comp_group_free_invite_info();
 
        notify_group_device_invite_result(ret);
@@ -543,6 +680,10 @@ int comp_group_invite(const char *group_name, const char *uuid, const char *pin)
        }
 
 #else
+       group_invite_info = g_try_malloc0(sizeof(comp_group_invite_info_t));
+       group_invite_info->uuid_dev2 = g_strdup(uuid);
+       group_invite_info->group_name = g_strdup(group_name);
+
        ret = agent_pair(pin, comp_ctx->device_uuid, uuid, "/comp/data/1",
                                         "core.comp.data", "oic.if.baseline", FULL_PERMISSION, uuid,
                                         comp_ctx->device_uuid, "/comp/data/1", "core.comp.data",
@@ -641,26 +782,6 @@ int comp_group_unpair_resource(gchar *uuid_dev1, gchar *uuid_dev2)
        return ret;
 }
 
-static char *_addr2host(char *addr, int port)
-{
-       char ip[50];
-       char *percent;
-
-       g_strlcpy(ip, addr, strlen(addr) + 1);
-
-       /* Remove % from address */
-       percent = strchr(ip, '%');
-       if (percent)
-               percent[0] = '\0';
-
-       /* IPv6 Address should be encoded for RFC6874  */
-       if (strchr(ip, ':')) /* IPv6 Adress */
-               return g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
-                                                                       percent + 1, port);
-       else /* IPv4 Address */
-               return g_strdup_printf("coaps://%s:%d", ip, port);
-}
-
 int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port,
                                                 unsigned char *data, int len)
 {
@@ -956,25 +1077,6 @@ 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;
index 3cc21de..9d7beae 100755 (executable)
@@ -502,13 +502,24 @@ OCEntityHandlerResult _request_handler(std::shared_ptr<OCResourceRequest> reques
                                        arg = (unsigned char *) g_strdup("DELETED");
                                        arg_len = strlen("DELETED");
                                        break;
+                               case COMP_REQ_KEEP_ALIVE:
+                                       LOG_DEBUG("Keep Alive message");
+
+                                       if (rep.getValue("group_name", group_name)) {
+                                               LOG_DEBUG("group_name : %s", group_name.c_str());
+                                       }
+                                       break;
                                default:
                                        LOG_ERR("Unknown request command %d", cmd);
                                        break;
                                }
 
-                               notify_request_result(command2string((comp_request_type_e) cmd),
-                                                                 requester_id.c_str(), arg, arg_len, result);
+                               if (cmd != COMP_REQ_KEEP_ALIVE) {
+                                       const char *cmd_str;
+                                       cmd_str = command2string((comp_request_type_e) cmd);
+                                       notify_request_result(cmd_str, requester_id.c_str(), arg,
+                                                                                 arg_len, result);
+                               }
 
                                g_free(arg);
 
@@ -615,6 +626,11 @@ static void _on_post(const HeaderOptions& /*headerOptions*/,
                        eCode == OC_STACK_RESOURCE_CHANGED) {
                        last_get_result = 0;
                        LOG_DEBUG("Post request is successful");
+
+                       if (cmd->command == COMP_REQ_KEEP_ALIVE) {
+                               comp_group_notify_keepalive_response(cmd);
+                               return;
+                       }
                } else {
                        last_get_result = eCode;
                        LOG_ERR("_on_post Response error %d", eCode);
@@ -762,7 +778,8 @@ static gboolean _timeout_cb(gpointer data)
                notify_group_find_finish(ret);
 
        } else if (cmd->resource_type == COMP_RESOURCE_TYPE_DATA) {
-               notify_send_data_finish("RESP_DATA", last_get_result);
+               if (cmd->command != COMP_REQ_KEEP_ALIVE)
+                       notify_send_data_finish("RESP_DATA", last_get_result);
        }
 
        _clear_user_data(data);
@@ -879,6 +896,9 @@ int comp_iot_send_data(comp_resource_type_e resource_type, int timeout,
                        rep.setValue("name", std::string(cmd->arg1));
                        rep.setValue("id", std::string(cmd->arg2));
                        break;
+               case COMP_REQ_KEEP_ALIVE:
+                       rep.setValue("group_name", std::string(cmd->arg1));
+                       break;
                default:
                        LOG_ERR("Invalid command %d", cmd->command);
                        return -1;