From 29f2c78544e0e6e45e6e1d43d4610cf1afb4c4ea Mon Sep 17 00:00:00 2001 From: Saurav Babu Date: Thu, 15 Feb 2018 17:32:38 +0530 Subject: [PATCH] comp-manager: Register new signal for mowned_devs_added event This patch registers new signal to know whenever new mowned device is added. There is no need to free existing discovered device list when Already in progress error is not returned for disc_mowned_devs dbus method Signed-off-by: Saurav Babu --- src/companion-manager/include/comp_group.h | 2 +- src/companion-manager/src/comp_group.c | 15 ++- src/companion-manager/src/comp_mot_agent.c | 159 +++++++++++++++-------------- 3 files changed, 93 insertions(+), 83 deletions(-) diff --git a/src/companion-manager/include/comp_group.h b/src/companion-manager/include/comp_group.h index 472abb2..a7e5abe 100644 --- a/src/companion-manager/include/comp_group.h +++ b/src/companion-manager/include/comp_group.h @@ -86,7 +86,7 @@ GVariant *comp_group_get_remote_mot_enabled_devices(); int comp_group_get_mot_device_count(); void comp_group_add_new_mowned_device(comp_mot_device_t *device); -void comp_group_notify_mowned_device_find_done(int device_count); +void comp_group_notify_mowned_device_find_done(); int comp_group_find_mowned_devices(); GVariant *comp_group_get_mowned_devices(); int comp_group_get_mowned_device_count(); diff --git a/src/companion-manager/src/comp_group.c b/src/companion-manager/src/comp_group.c index a8534a7..7828fe6 100644 --- a/src/companion-manager/src/comp_group.c +++ b/src/companion-manager/src/comp_group.c @@ -740,7 +740,7 @@ int comp_group_get_mowned_device_count() return 0; } -void comp_group_notify_mowned_device_find_done(int device_count) +void comp_group_notify_mowned_device_find_done() { GVariant *device_data; int count = g_list_length(mowned_dev_list); @@ -760,16 +760,21 @@ int comp_group_find_mowned_devices(int timeout) LOG_BEGIN(); - /* list freeing */ - g_list_free_full(mowned_dev_list, _mot_enb_dev_list_free_func); - mowned_dev_list = NULL; - ret = agent_find_mowned_devices(timeout); if (ret != COMP_ERROR_NONE) { LOG_ERR("Failed to find mowned devices : %s", comp_log_get_error_string(ret)); } + /* No need to free old list when discovery is already in progress */ + if (ret != 7) { + /* list freeing */ + g_list_free_full(mowned_dev_list, _mot_enb_dev_list_free_func); + mowned_dev_list = NULL; + } else { + ret = 0; + } + LOG_END(); return ret; diff --git a/src/companion-manager/src/comp_mot_agent.c b/src/companion-manager/src/comp_mot_agent.c index 3382a14..3acfff0 100644 --- a/src/companion-manager/src/comp_mot_agent.c +++ b/src/companion-manager/src/comp_mot_agent.c @@ -159,6 +159,9 @@ int agent_find_mowned_devices(int timeout) return COMP_ERROR_IO_ERROR; } + if (result == 0) + found_mowned_device_count = 0; + g_timeout_add_seconds(timeout + 1, _find_mowned_device_timeout_cb, NULL); return result; @@ -658,98 +661,89 @@ static void _agent_signal_handler(GDBusConnection *connection, comp_group_change_mowned_device(device); } } else if (0 == g_strcmp0(signal_name, "disc_mowned_devs_done")) { + comp_group_notify_mowned_device_find_done(); + } else if (0 == g_strcmp0(signal_name, "mowned_devs_added")) { GVariantIter *iter = NULL; - GVariantIter *iter_row = NULL; - gchar *key; GVariant *val; gsize len = 0; - int mowned_devs_cnt = 0; comp_mot_device_t *device; if (NULL == parameters) { LOG_ERR("No Multiple Owned devices found"); return; } - g_variant_get(parameters, "(aa{sv})", &iter); - while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { - device = g_try_malloc0(sizeof(comp_mot_device_t)); - - while (g_variant_iter_loop(iter_row, "{sv}", &key, &val)) { - if (strcasecmp(key, "deviceId") == 0) { - const char *deviceid = g_variant_get_string(val, &len); - LOG_DEBUG("deviceId = %s", deviceid); - device->device_id = g_strdup(deviceid); - } else if (strcasecmp(key, "adapter") == 0) { - int adapter = g_variant_get_uint32(val); - LOG_DEBUG("adapter = %d", adapter); - device->adapter = adapter; - } else if (strcasecmp(key, "flags") == 0) { - int flags = g_variant_get_int32(val); - LOG_DEBUG("flags = %d", flags); - device->flags = flags; - } else if (strcasecmp(key, "port") == 0) { - int port = g_variant_get_uint16(val); - LOG_DEBUG("port = %d", port); - device->port = port; - } else if (strcasecmp(key, "addr") == 0) { - const char *addr = g_variant_get_string(val, &len); - LOG_DEBUG("addr = %s", addr); - device->addr = g_strdup(addr); - } else if (strcasecmp(key, "ifindex") == 0) { - int ifindex = g_variant_get_int32(val); - LOG_DEBUG("ifindex = %d", ifindex); - device->ifindex = ifindex; - } else if (strcasecmp(key, "routeData") == 0) { - const char *routeData = g_variant_get_string(val, &len); - LOG_DEBUG("routeData = %s", routeData); - device->route_data = g_strdup(routeData); - } else if (strcasecmp(key, "remoteId") == 0) { - const char *remoteId = g_variant_get_string(val, &len); - LOG_DEBUG("remoteId = %s", remoteId); - device->remote_id = g_strdup(remoteId); - } else if (strcasecmp(key, "connType") == 0) { - int connType = g_variant_get_uint32(val); - LOG_DEBUG("connType = %d", connType); - device->conn_type = connType; - } else if (strcasecmp(key, "securePort") == 0) { - int securePort = g_variant_get_uint16(val); - LOG_DEBUG("securePort = %d", securePort); - device->secure_port = securePort; + + device = g_try_malloc0(sizeof(comp_mot_device_t)); + g_variant_get(parameters, "(a{sv})", &iter); + while (g_variant_iter_loop(iter, "{sv}", &key, &val)) { + if (strcasecmp(key, "deviceId") == 0) { + const char *deviceid = g_variant_get_string(val, &len); + LOG_DEBUG("deviceId = %s", deviceid); + device->device_id = g_strdup(deviceid); + } else if (strcasecmp(key, "adapter") == 0) { + int adapter = g_variant_get_uint32(val); + LOG_DEBUG("adapter = %d", adapter); + device->adapter = adapter; + } else if (strcasecmp(key, "flags") == 0) { + int flags = g_variant_get_int32(val); + LOG_DEBUG("flags = %d", flags); + device->flags = flags; + } else if (strcasecmp(key, "port") == 0) { + int port = g_variant_get_uint16(val); + LOG_DEBUG("port = %d", port); + device->port = port; + } else if (strcasecmp(key, "addr") == 0) { + const char *addr = g_variant_get_string(val, &len); + LOG_DEBUG("addr = %s", addr); + device->addr = g_strdup(addr); + } else if (strcasecmp(key, "ifindex") == 0) { + int ifindex = g_variant_get_int32(val); + LOG_DEBUG("ifindex = %d", ifindex); + device->ifindex = ifindex; + } else if (strcasecmp(key, "routeData") == 0) { + const char *routeData = g_variant_get_string(val, &len); + LOG_DEBUG("routeData = %s", routeData); + device->route_data = g_strdup(routeData); + } else if (strcasecmp(key, "remoteId") == 0) { + const char *remoteId = g_variant_get_string(val, &len); + LOG_DEBUG("remoteId = %s", remoteId); + device->remote_id = g_strdup(remoteId); + } else if (strcasecmp(key, "connType") == 0) { + int connType = g_variant_get_uint32(val); + LOG_DEBUG("connType = %d", connType); + device->conn_type = connType; + } else if (strcasecmp(key, "securePort") == 0) { + int securePort = g_variant_get_uint16(val); + LOG_DEBUG("securePort = %d", securePort); + device->secure_port = securePort; #ifdef WITH_TCP - } else if (strcasecmp(key, "tcpPort") == 0) { - int tcpPort = g_variant_get_uint16(val); - LOG_DEBUG("tcpPort = %d", tcpPort); - device->tcp_port = tcpPort; - } -#endif - else if (strcasecmp(key, "secVer") == 0) { - const char *secVer = g_variant_get_string(val, &len); - LOG_DEBUG("secVer = %s", secVer); - device->sec_ver = g_strdup(secVer); - } else if (strcasecmp(key, "devStatus") == 0) { - int devStatus = g_variant_get_uint32(val); - LOG_DEBUG("devStatus = %d", devStatus); - device->dev_status = devStatus; - } + } else if (strcasecmp(key, "tcpPort") == 0) { + int tcpPort = g_variant_get_uint16(val); + LOG_DEBUG("tcpPort = %d", tcpPort); + device->tcp_port = tcpPort; } - g_variant_iter_free(iter_row); - - comp_context_t *comp_ctx = comp_context_get_context(); - if (strcmp(device->device_id, comp_ctx->device_uuid) != 0) { - mowned_devs_cnt += 1; - comp_group_add_new_mowned_device(device); - } else { - LOG_DEBUG("It is my mowned device. Store the mowned device in daemon"); - comp_group_change_mowned_device(device); +#endif + else if (strcasecmp(key, "secVer") == 0) { + const char *secVer = g_variant_get_string(val, &len); + LOG_DEBUG("secVer = %s", secVer); + device->sec_ver = g_strdup(secVer); + } else if (strcasecmp(key, "devStatus") == 0) { + int devStatus = g_variant_get_uint32(val); + LOG_DEBUG("devStatus = %d", devStatus); + device->dev_status = devStatus; } } - g_variant_iter_free(iter); - - LOG_DEBUG("mowned_devs_cnt = %d", mowned_devs_cnt); - found_mowned_device_count = mowned_devs_cnt; - comp_group_notify_mowned_device_find_done(mowned_devs_cnt); + comp_context_t *comp_ctx = comp_context_get_context(); + if (strcmp(device->device_id, comp_ctx->device_uuid) != 0) { + found_mowned_device_count += 1; + LOG_DEBUG("mowned_devs_cnt = %d", found_mowned_device_count); + comp_group_add_new_mowned_device(device); + } else { + LOG_DEBUG("It is my mowned device. Store the mowned device in daemon"); + comp_group_change_mowned_device(device); + } } else if (0 == g_strcmp0(signal_name, "acl_done")) { g_variant_get(parameters, "(i)", &result); LOG_DEBUG("Result : %d", result); @@ -871,6 +865,17 @@ static int _subscribe_event() h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id)); LOG_DEBUG("subscribed for (disc_mowned_devs_done) signal %d", id); + /* mowned_devs_added */ + id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, + AGENT_SERVER_NAME, "mowned_devs_added", AGENT_OBJECT_PATH, NULL, + G_DBUS_CALL_FLAGS_NONE, _agent_signal_handler, h, NULL); + if (0 == id) { + LOG_ERR("g_dbus_connection_signal_subscribe(mowned_devs_added) Fail(%d)", errno); + return -1; + } + h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id)); + LOG_DEBUG("subscribed for (mowned_devs_added) signal %d", id); + /* mot_done */ id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, AGENT_SERVER_NAME, "mot_done", AGENT_OBJECT_PATH, NULL, -- 2.7.4