Open Source Telephony
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: ofono@ofono.org
Subject: [PATCH v6 1/3] hfp_hf_bluez5: Rework code handling device changes
Date: Thu, 04 May 2017 11:22:34 +0300	[thread overview]
Message-ID: <20170504082236.9977-1-luiz.dentz@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 5611 bytes --]

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This splits the handling of device changes and modem registration so
they can be uses separately.
---
v6: Now properly rebased

 plugins/hfp_hf_bluez5.c | 104 +++++++++++++++++++++++++-----------------------
 1 file changed, 55 insertions(+), 49 deletions(-)

diff --git a/plugins/hfp_hf_bluez5.c b/plugins/hfp_hf_bluez5.c
index f7d2450..bdeb3ca 100644
--- a/plugins/hfp_hf_bluez5.c
+++ b/plugins/hfp_hf_bluez5.c
@@ -176,11 +176,42 @@ static int service_level_connection(struct ofono_modem *modem,
 	return -EINPROGRESS;
 }
 
-static struct ofono_modem *modem_register(const char *device,
-				const char *device_address, const char *alias)
+static void modem_removed(GDBusProxy *proxy, void *user_data)
+{
+	struct ofono_modem *modem = user_data;
+
+	ofono_modem_remove(modem);
+}
+
+static void alias_changed(GDBusProxy *proxy, const char *name,
+					DBusMessageIter *iter, void *user_data)
+{
+	const char *alias;
+	struct ofono_modem *modem = user_data;
+
+	if (g_str_equal("Alias", name) == FALSE)
+		return;
+
+	dbus_message_iter_get_basic(iter, &alias);
+	ofono_modem_set_name(modem, alias);
+}
+
+static struct ofono_modem *modem_register(const char *device, GDBusProxy *proxy)
 {
 	struct ofono_modem *modem;
 	char *path;
+	DBusMessageIter iter;
+	const char *alias, *remote;
+
+	if (g_dbus_proxy_get_property(proxy, "Alias", &iter) == FALSE)
+		return NULL;
+
+	dbus_message_iter_get_basic(&iter, &alias);
+
+	if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
+		return NULL;
+
+	dbus_message_iter_get_basic(&iter, &remote);
 
 	path = g_strconcat("hfp", device, NULL);
 
@@ -191,12 +222,15 @@ static struct ofono_modem *modem_register(const char *device,
 	if (modem == NULL)
 		return NULL;
 
-	ofono_modem_set_string(modem, "Remote", device_address);
+	ofono_modem_set_string(modem, "Remote", remote);
 	ofono_modem_set_string(modem, "DevicePath", device);
 
 	ofono_modem_set_name(modem, alias);
 	ofono_modem_register(modem);
 
+	g_dbus_proxy_set_property_watch(proxy, alias_changed, modem);
+	g_dbus_proxy_set_removed_watch(proxy, modem_removed, modem);
+
 	return modem;
 }
 
@@ -522,30 +556,15 @@ static gboolean has_hfp_ag_uuid(DBusMessageIter *array)
 	return FALSE;
 }
 
-static void modem_removed(GDBusProxy *proxy, void *user_data)
+static void modem_unregister(struct ofono_modem *modem, GDBusProxy *proxy)
 {
-	struct ofono_modem *modem = user_data;
-
 	ofono_modem_remove(modem);
+	g_dbus_proxy_set_removed_watch(proxy, NULL, NULL);
+	g_dbus_proxy_set_property_watch(proxy, NULL, NULL);
 }
 
-static void alias_changed(GDBusProxy *proxy, const char *name,
-					DBusMessageIter *iter, void *user_data)
-{
-	const char *alias;
-	struct ofono_modem *modem = user_data;
-
-	if (g_str_equal("Alias", name) == FALSE)
-		return;
-
-	dbus_message_iter_get_basic(iter, &alias);
-	ofono_modem_set_name(modem, alias);
-}
-
-static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy,
-							const char *path)
+static void *device_changed(GDBusProxy *proxy, const char *path)
 {
-	const char *alias, *remote;
 	DBusMessageIter iter;
 	dbus_bool_t paired;
 	struct ofono_modem *modem;
@@ -558,36 +577,23 @@ static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy,
 	modem = ofono_modem_find(device_path_compare, (void *) path);
 
 	if (paired == FALSE) {
-		if (modem != NULL) {
-			ofono_modem_remove(modem);
-			g_dbus_proxy_set_removed_watch(proxy, NULL, NULL);
-			g_dbus_proxy_set_property_watch(proxy, NULL, NULL);
-		}
+		if (modem != NULL)
+			modem_unregister(modem, proxy);
 		return NULL;
 	}
 
-	if (modem)
-		return modem;
-
-	if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE)
-		return NULL;
-
-	if (has_hfp_ag_uuid(&iter) == FALSE)
-		return NULL;
-
-	if (g_dbus_proxy_get_property(proxy, "Alias", &iter) == FALSE)
-		return NULL;
-
-	dbus_message_iter_get_basic(&iter, &alias);
-
-	if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
+	if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE ||
+					has_hfp_ag_uuid(&iter) == FALSE) {
+		if (modem != NULL)
+			modem_unregister(modem, proxy);
 		return NULL;
+	}
 
-	dbus_message_iter_get_basic(&iter, &remote);
+	/* Skip if modem already registered */
+	if (modem)
+		return modem;
 
-	modem = modem_register(path, remote, alias);
-	g_dbus_proxy_set_property_watch(proxy, alias_changed, modem);
-	g_dbus_proxy_set_removed_watch(proxy, modem_removed, modem);
+	modem = modem_register(path, proxy);
 
 	return modem;
 }
@@ -637,7 +643,7 @@ static DBusMessage *profile_new_connection(DBusConnection *conn,
 		GDBusProxy *proxy;
 
 		proxy = g_dbus_proxy_new(bluez, device, BLUEZ_DEVICE_INTERFACE);
-		modem = modem_register_from_proxy(proxy, device);
+		modem = modem_register(device, proxy);
 		g_dbus_proxy_unref(proxy);
 
 		if (!modem) {
@@ -797,7 +803,7 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 	if (g_str_equal(BLUEZ_DEVICE_INTERFACE, interface) == FALSE)
 		return;
 
-	modem_register_from_proxy(proxy, path);
+	device_changed(proxy, path);
 }
 
 static void property_changed(GDBusProxy *proxy, const char *name,
@@ -815,7 +821,7 @@ static void property_changed(GDBusProxy *proxy, const char *name,
 			g_str_equal("ServicesResolved", name) != TRUE)
 		return;
 
-	modem_register_from_proxy(proxy, path);
+	device_changed(proxy, path);
 }
 
 static int hfp_init(void)
-- 
2.9.3


             reply	other threads:[~2017-05-04  8:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-04  8:22 Luiz Augusto von Dentz [this message]
2017-05-04  8:22 ` [PATCH v6 2/3] doc/handsfree-audio-api: Add Acquire method Luiz Augusto von Dentz
2017-05-04  8:22 ` [PATCH v6 3/3] handsfree-audio: Add Acquire implementation Luiz Augusto von Dentz
2017-05-04 17:35 ` [PATCH v6 1/3] hfp_hf_bluez5: Rework code handling device changes Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170504082236.9977-1-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=ofono@ofono.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox