From 8c19cbee79fb00580386083d405bec948c7217f0 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Mon, 8 Feb 2016 10:50:03 +0200 Subject: [PATCH] client: Fix not detecting connections when starting If a device is connected set it as default so the prompt is updated and attribute related commands can work. --- client/main.c | 93 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/client/main.c b/client/main.c index 6514be8df..be785c52e 100644 --- a/client/main.c +++ b/client/main.c @@ -321,6 +321,60 @@ static gboolean service_is_child(GDBusProxy *service) return FALSE; } +static void set_default_device(GDBusProxy *proxy, const char *attribute) +{ + char *desc = NULL; + DBusMessageIter iter; + const char *path; + + default_dev = proxy; + + if (proxy == NULL) { + default_attr = NULL; + goto done; + } + + if (!g_dbus_proxy_get_property(proxy, "Alias", &iter)) { + if (!g_dbus_proxy_get_property(proxy, "Address", &iter)) + goto done; + } + + path = g_dbus_proxy_get_path(proxy); + + dbus_message_iter_get_basic(&iter, &desc); + desc = g_strdup_printf(COLOR_BLUE "[%s%s%s]" COLOR_OFF "# ", desc, + attribute ? ":" : "", + attribute ? attribute + strlen(path) : ""); + +done: + rl_set_prompt(desc ? desc : PROMPT_ON); + printf("\r"); + rl_on_new_line(); + rl_redisplay(); + g_free(desc); +} + +static void device_added(GDBusProxy *proxy) +{ + DBusMessageIter iter; + + dev_list = g_list_append(dev_list, proxy); + + print_device(proxy, COLORED_NEW); + + if (default_dev) + return; + + if (g_dbus_proxy_get_property(proxy, "Connected", &iter)) { + dbus_bool_t connected; + + dbus_message_iter_get_basic(&iter, &connected); + + if (connected) + set_default_device(proxy, NULL); + } +} + static void proxy_added(GDBusProxy *proxy, void *user_data) { const char *interface; @@ -328,11 +382,9 @@ static void proxy_added(GDBusProxy *proxy, void *user_data) interface = g_dbus_proxy_get_interface(proxy); if (!strcmp(interface, "org.bluez.Device1")) { - if (device_is_child(proxy, default_ctrl) == TRUE) { - dev_list = g_list_append(dev_list, proxy); + if (device_is_child(proxy, default_ctrl) == TRUE) + device_added(proxy); - print_device(proxy, COLORED_NEW); - } } else if (!strcmp(interface, "org.bluez.Adapter1")) { ctrl_list = g_list_append(ctrl_list, proxy); @@ -360,39 +412,6 @@ static void proxy_added(GDBusProxy *proxy, void *user_data) } } -static void set_default_device(GDBusProxy *proxy, const char *attribute) -{ - char *desc = NULL; - DBusMessageIter iter; - const char *path; - - default_dev = proxy; - - if (proxy == NULL) { - default_attr = NULL; - goto done; - } - - if (!g_dbus_proxy_get_property(proxy, "Alias", &iter)) { - if (!g_dbus_proxy_get_property(proxy, "Address", &iter)) - goto done; - } - - path = g_dbus_proxy_get_path(proxy); - - dbus_message_iter_get_basic(&iter, &desc); - desc = g_strdup_printf(COLOR_BLUE "[%s%s%s]" COLOR_OFF "# ", desc, - attribute ? ":" : "", - attribute ? attribute + strlen(path) : ""); - -done: - rl_set_prompt(desc ? desc : PROMPT_ON); - printf("\r"); - rl_on_new_line(); - rl_redisplay(); - g_free(desc); -} - static void set_default_attribute(GDBusProxy *proxy) { const char *path; -- 2.47.3