comp-manager: Add logic to retry device discovery
authorSaurav Babu <saurav.babu@samsung.com>
Thu, 1 Feb 2018 12:32:12 +0000 (18:02 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:48 +0000 (19:38 +0900)
This patch retries device discovery after 100ms for 20 tries

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

index 86cfd7abcb0077cfd917b6f0b280b0f78c1053e7..96e17496a2b93da27dd4e6e63bff2d4b2093c08c 100644 (file)
@@ -8,6 +8,12 @@ GList *mot_enb_dev_list = NULL;
 GList *mowned_dev_list = NULL;
 comp_group_invite_info_t *group_invite_info = NULL;
 comp_mot_device_t *my_device = NULL;
+int find_mot_enb_dev_retry_cnt;
+int find_mowned_dev_retry_cnt;
+
+#define RETRY_INTERVAL 100
+#define RETRY_TIMEOUT 2
+#define RETRY_COUNT 20
 
 /* Called when daemon is start. */
 int comp_group_initialize()
@@ -237,9 +243,30 @@ int comp_group_add_new(char *uri_path, char *device_id, char *device_name,
 // Find MOT enabled devices
 void comp_group_add_new_mot_device(comp_mot_device_t *device)
 {
+       GList *iter = NULL;
+       gboolean is_exist = FALSE;
+
        LOG_BEGIN();
 
-       mot_enb_dev_list = g_list_append(mot_enb_dev_list, device);
+       iter = mot_enb_dev_list;
+       while(iter != NULL) {
+               comp_mot_device_t *temp = (comp_mot_device_t *)iter->data;
+
+               if (g_strcmp0(temp->device_id, device->device_id) == 0) {
+                       LOG_DEBUG("Device %s already exists in mot enable device list",
+                                         device->device_id);
+                       is_exist = TRUE;
+                       break;
+               }
+
+               iter = g_list_next(iter);
+       }
+
+       if (is_exist == FALSE) {
+               LOG_DEBUG("Add device with uuid %s to mot enabled device list",
+                                 device->device_id);
+               mot_enb_dev_list = g_list_append(mot_enb_dev_list, device);
+       }
 
        LOG_END();
 }
@@ -299,15 +326,39 @@ int comp_group_get_mot_device_count()
        return 0;
 }
 
+static gboolean __retry_find_mot_enable_device(gpointer data)
+{
+       int ret;
+       LOG_BEGIN();
+
+       LOG_DEBUG("Retry to find mot enabled devices count %d",
+                         find_mot_enb_dev_retry_cnt);
+
+       ret = agent_find_mot_enable_devices(RETRY_TIMEOUT);
+       if (ret != COMP_ERROR_NONE) {
+               LOG_ERR("Failed to find mot enable devices : %s",
+                                comp_log_get_error_string(ret));
+       }
+
+       LOG_END();
+       return FALSE;
+}
+
 void comp_group_notify_mot_enable_device_done(int device_count)
 {
        GVariant *device_data;
+       int count = g_list_length(mot_enb_dev_list);
 
        LOG_BEGIN();
 
        device_data = comp_group_get_remote_mot_enabled_devices();
 
-       notify_device_found(device_count, device_data);
+       notify_device_found(count, device_data);
+
+       if (find_mot_enb_dev_retry_cnt < RETRY_COUNT) {
+               find_mot_enb_dev_retry_cnt += 1;
+               g_timeout_add(RETRY_INTERVAL, __retry_find_mot_enable_device, NULL);
+       }
 
        LOG_END();
 }
@@ -322,6 +373,8 @@ int comp_group_find_mot_enabled_devices(int timeout)
        g_list_free_full (mot_enb_dev_list, _mot_enb_dev_list_free_func);
        mot_enb_dev_list = NULL;
 
+       find_mot_enb_dev_retry_cnt = 0;
+
        ret = agent_find_mot_enable_devices(timeout);
        if (ret != COMP_ERROR_NONE) {
                LOG_ERR("Failed to find mot enable devices : %s",
@@ -582,9 +635,30 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
 
 void comp_group_add_new_mowned_device(comp_mot_device_t *device)
 {
+       GList *iter = NULL;
+       gboolean is_exist = FALSE;
+
        LOG_BEGIN();
 
-       mowned_dev_list = g_list_append(mowned_dev_list, device);
+       iter = mowned_dev_list;
+       while(iter != NULL) {
+               comp_mot_device_t *temp = (comp_mot_device_t *)iter->data;
+
+               if (g_strcmp0(temp->device_id, device->device_id) == 0) {
+                       LOG_DEBUG("Device %s already exists in mowned device list",
+                                         device->device_id);
+                       is_exist = TRUE;
+                       break;
+               }
+
+               iter = g_list_next(iter);
+       }
+
+       if (is_exist == FALSE) {
+               LOG_DEBUG("Add device with uuid %s to mowned device list",
+                                 device->device_id);
+               mowned_dev_list = g_list_append(mowned_dev_list, device);
+       }
 
        LOG_END();
 }
@@ -692,15 +766,41 @@ int comp_group_get_mowned_device_count()
        return 0;
 }
 
+static gboolean __retry_find_mowned_devices(gpointer data)
+{
+       int ret;
+
+       LOG_BEGIN();
+
+       LOG_DEBUG("Retry to find mowned devices count %d",
+                         find_mowned_dev_retry_cnt);
+
+       ret = agent_find_mowned_devices(RETRY_TIMEOUT);
+       if (ret != COMP_ERROR_NONE) {
+               LOG_ERR("Failed to find mowned devices : %s",
+                                comp_log_get_error_string(ret));
+       }
+
+       LOG_END();
+
+       return FALSE;
+}
+
 void comp_group_notify_mowned_device_find_done(int device_count)
 {
        GVariant *device_data;
+       int count = g_list_length(mowned_dev_list);
 
        LOG_BEGIN();
 
        device_data = comp_group_get_mowned_devices();
 
-       notify_mowned_device_found(device_count, device_data);
+       notify_mowned_device_found(count, device_data);
+
+       if (find_mowned_dev_retry_cnt < RETRY_COUNT) {
+               find_mowned_dev_retry_cnt += 1;
+               g_timeout_add(RETRY_INTERVAL, __retry_find_mowned_devices, NULL);
+       }
 
        LOG_END();
 }
@@ -715,6 +815,8 @@ int comp_group_find_mowned_devices(int timeout)
        g_list_free_full(mowned_dev_list, _mot_enb_dev_list_free_func);
        mowned_dev_list = NULL;
 
+       find_mowned_dev_retry_cnt = 0;
+
        ret = agent_find_mowned_devices(timeout);
        if (ret != COMP_ERROR_NONE) {
                LOG_ERR("Failed to find mowned devices : %s",