Skip to contentSkip to conversation

hfp_hf: Fix not creating modem during NewConnection

PATCH In review Luiz Augusto von Dentz 2 participants 6 messages Apr 28, 2017 – May 2, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 hfp_hf: Fix not creating modem during NewConnection

From: Luiz Augusto von Dentz <[email protected]>

In case the UUIDs are not updated, as they are still being resolved, when Paired property changes a modem will never be registered.

In order to fix this problem allow modems to be registered directly during NewConnection.

---
 plugins/hfp_hf_bluez5.c | 193 +++++++++++++++++++++++++-----------------------
 1 file changed, 102 insertions(+), 91 deletions(-)
plugins/hfp_hf_bluez5.c +102 -91
index 6ae98b3..fb7cc3f 100644
@@ -499,6 +499,96 @@ static int get_version(DBusMessageIter *iter, uint16_t *version)
 	return -ENOENT;
 }
 
+static gboolean has_hfp_ag_uuid(DBusMessageIter *array)
+{
+	DBusMessageIter value;
+
+	if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
+		return FALSE;
+
+	dbus_message_iter_recurse(array, &value);
+
+	while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
+		const char *uuid;
+
+		dbus_message_iter_get_basic(&value, &uuid);
+
+		if (g_str_equal(uuid, HFP_AG_UUID) == TRUE)
+			return TRUE;
+
+		dbus_message_iter_next(&value);
+	}
+
+	return FALSE;
+}
+
+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_from_proxy(GDBusProxy *proxy,
+							const char *path)
+{
+	const char *alias, *remote;
+	DBusMessageIter iter;
+	dbus_bool_t paired;
+	struct ofono_modem *modem;
+
+	if (g_dbus_proxy_get_property(proxy, "Paired", &iter) == FALSE)
+		return NULL;
+
+	dbus_message_iter_get_basic(&iter, &paired);
+
+	if (paired == FALSE) {
+		modem = ofono_modem_find(device_path_compare, (void *) path);
+
+		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);
+		}
+		return NULL;
+	}
+
+	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)
+		return NULL;
+
+	dbus_message_iter_get_basic(&iter, &remote);
+
+	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);
+
+	return modem;
+}
+
 static DBusMessage *profile_new_connection(DBusConnection *conn,
 					DBusMessage *msg, void *user_data)
 {
@@ -541,10 +631,18 @@ static DBusMessage *profile_new_connection(DBusConnection *conn,
 
 	modem = ofono_modem_find(device_path_compare, (void *) device);
 	if (modem == NULL) {
-		close(fd);
-		return g_dbus_create_error(msg, BLUEZ_ERROR_INTERFACE
-					".Rejected",
-					"Unknown Bluetooth device");
+		GDBusProxy *proxy;
+
+		proxy = g_dbus_proxy_new(bluez, device, BLUEZ_DEVICE_INTERFACE);
+		modem = modem_register_from_proxy(proxy, device);
+		g_dbus_proxy_unref(proxy);
+
+		if (!modem) {
+			close(fd);
+			return g_dbus_create_error(msg, BLUEZ_ERROR_INTERFACE
+						".Rejected",
+						"Unknown Bluetooth device");
+		}
 	}
 
 	err = service_level_connection(modem, fd, version);
@@ -686,93 +784,6 @@ static void connect_handler(DBusConnection *conn, void *user_data)
 					HFP_EXT_PROFILE_PATH, NULL, features);
 }
 
-static gboolean has_hfp_ag_uuid(DBusMessageIter *array)
-{
-	DBusMessageIter value;
-
-	if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
-		return FALSE;
-
-	dbus_message_iter_recurse(array, &value);
-
-	while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
-		const char *uuid;
-
-		dbus_message_iter_get_basic(&value, &uuid);
-
-		if (g_str_equal(uuid, HFP_AG_UUID) == TRUE)
-			return TRUE;
-
-		dbus_message_iter_next(&value);
-	}
-
-	return FALSE;
-}
-
-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 void modem_register_from_proxy(GDBusProxy *proxy, const char *path)
-{
-	const char *alias, *remote;
-	DBusMessageIter iter;
-	dbus_bool_t paired;
-	struct ofono_modem *modem;
-
-	if (g_dbus_proxy_get_property(proxy, "Paired", &iter) == FALSE)
-		return;
-
-	dbus_message_iter_get_basic(&iter, &paired);
-
-	if (paired == FALSE) {
-		modem = ofono_modem_find(device_path_compare, (void *) path);
-
-		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);
-		}
-		return;
-	}
-
-	if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE)
-		return;
-
-	if (has_hfp_ag_uuid(&iter) == FALSE)
-		return;
-
-	if (g_dbus_proxy_get_property(proxy, "Alias", &iter) == FALSE)
-		return;
-
-	dbus_message_iter_get_basic(&iter, &alias);
-
-	if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
-		return;
-
-	dbus_message_iter_get_basic(&iter, &remote);
-
-	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);
-}
-
 static void proxy_added(GDBusProxy *proxy, void *user_data)
 {
 	const char *interface, *path;
Signature · 1 line
2.9.3
#1 lore raw

PATCH v3 2/3 hfp_hf: Handle ServicesResolved signal

From: Luiz Augusto von Dentz <[email protected]>

This adds handling for ServicesResolved signal which tells when BlueZ is done resolving the device services so the code will no longer ignore devices that got its services resolved after Paired signal.

---
 plugins/hfp_hf_bluez5.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
plugins/hfp_hf_bluez5.c +7 -3
index fb7cc3f..f7d2450 100644
@@ -555,9 +555,9 @@ static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy,
 
 	dbus_message_iter_get_basic(&iter, &paired);
 
-	if (paired == FALSE) {
-		modem = ofono_modem_find(device_path_compare, (void *) path);
+	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);
@@ -566,6 +566,9 @@ static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy,
 		return NULL;
 	}
 
+	if (modem)
+		return modem;
+
 	if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE)
 		return NULL;
 
@@ -808,7 +811,8 @@ static void property_changed(GDBusProxy *proxy, const char *name,
 	if (g_str_equal(BLUEZ_DEVICE_INTERFACE, interface) == FALSE)
 		return;
 
-	if (g_str_equal("Paired", name) != TRUE)
+	if (g_str_equal("Paired", name) != TRUE &&
+			g_str_equal("ServicesResolved", name) != TRUE)
 		return;
 
 	modem_register_from_proxy(proxy, path);
Signature · 1 line
2.9.3
#4 lore raw

PATCH v3 3/3 handsfree-audio: Add Acquire method

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds Acquire method which can be used by agents that require pulling the fd directly instead of waiting a NewConnection.

Note: sounds servers like PulseAudio do auto suspend stream when idle for a certain amount of time and once anything happens it will resume the stream, though this all happens in the so called IO thread in a blocking fashion making it impossible to receive the fd via NewConnetion causing the stream to fail to resume. There are other forms to work around but this seems to be most convenient as we do want the auto suspend feature to work properly and letting the stream to fail to resume may create unexpected artifacts while the NewConnection is handled in PA main thread.

---
 doc/handsfree-audio-api.txt | 16 ++++++++++++++++
 src/handsfree-audio.c       |  9 +++++++++
 2 files changed, 25 insertions(+)
doc/handsfree-audio-api.txt +16 -0
index d82035c..87d51cf 100644
@@ -76,6 +76,22 @@ Methods		dict GetProperties()
 					 [service].Error.NotImplemented
 					 [service].Error.NotAllowed
 
+		fd, byte Acquire()
+
+			Attempts to establish the SCO audio connection
+			returning the filedescriptor of the connection and the
+			codec in use.
+
+			Note: Contrary to Connect this does not call
+			NewConnection so it can be called in a blocking
+			manner.
+
+			Possible Errors: [service].Error.InProgress
+					 [service].Error.Failed
+					 [service].Error.NotAvailable
+					 [service].Error.NotImplemented
+					 [service].Error.NotAllowed
+
 Signals		PropertyChanged(string name, variant value)
 
 			This signal indicates a changed value of the given
src/handsfree-audio.c +9 -0
index dab16da..f2c323d 100644
@@ -326,6 +326,12 @@ static gboolean sco_connect_cb(GIOChannel *io, GIOCondition cond,
 
 	sk = g_io_channel_unix_get_fd(io);
 
+	if (card->msg && dbus_message_has_member(card->msg, "Acquire")) {
+		reply = g_dbus_create_reply(card->msg, DBUS_TYPE_UNIX_FD, &sk,
+					DBUS_TYPE_BYTE, &card->selected_codec);
+		goto done;
+	}
+
 	send_new_connection(card->path, sk, card->selected_codec);
 
 	close(sk);
@@ -403,6 +409,9 @@ static const GDBusMethodTable card_methods[] = {
 			NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
 			card_get_properties) },
 	{ GDBUS_ASYNC_METHOD("Connect", NULL, NULL, card_connect) },
+	{ GDBUS_ASYNC_METHOD("Acquire", NULL,
+			GDBUS_ARGS({"sco", "h"}, {"codec", "y"}),
+			card_connect) },
 	{ }
 };
 
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH v3 2/3] hfp_hf: Handle ServicesResolved signal

In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 26 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds handling for ServicesResolved signal which tells when BlueZ is done resolving the device services so the code will no longer ignore devices that got its services resolved after Paired signal.

---
 plugins/hfp_hf_bluez5.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
plugins/hfp_hf_bluez5.c +2 -2
index fb7cc3f..f7d2450 100644
@@ -555,9 +555,9 @@ static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy,
 
 	dbus_message_iter_get_basic(&iter, &paired);
 
-	if (paired == FALSE) {
-		modem = ofono_modem_find(device_path_compare, (void *) path);
+	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);

plugins/hfp_hf_bluez5.c static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy, · line 571

Quoted 6 lines
plugins/hfp_hf_bluez5.c +3 -0
@@ -566,6 +566,9 @@ static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy,
 		return NULL;
 	}
 
+	if (modem)
+		return modem;
+

Why would you return a non-NULL value here? I thought your intended semantics (from patch 1) were to return the modem that was actually newly registered?

The new_connection code path is really quite special, so it really begs a comment. I also wonder if modem_register_from_proxy should be separated into two functions. One to handle the actual registration path, and one to handle the device removal.

plugins/hfp_hf_bluez5.c static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy, · line 573

Quoted 2 lines
plugins/hfp_hf_bluez5.c +0 -0
 	if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE)
 		return NULL;

Can UUIDs change from having HFP AG to not having HFP AG btw? We might be missing an additional remove path in that case.

Regards,
-Denis

#5 lore raw

Re: [PATCH v3 3/3] handsfree-audio: Add Acquire method

In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 14 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds Acquire method which can be used by agents that require pulling the fd directly instead of waiting a NewConnection.

Note: sounds servers like PulseAudio do auto suspend stream when idle for a certain amount of time and once anything happens it will resume the stream, though this all happens in the so called IO thread in a blocking fashion making it impossible to receive the fd via NewConnetion causing the stream to fail to resume. There are other forms to work around but this seems to be most convenient as we do want the auto suspend feature to work properly and letting the stream to fail to resume may create unexpected artifacts while the NewConnection is handled in PA main thread.

Quoted 5 lines
---
 doc/handsfree-audio-api.txt | 16 ++++++++++++++++
 src/handsfree-audio.c       |  9 +++++++++
 2 files changed, 25 insertions(+)

I'm fine with this one, but please separate it into two commits. See HACKING, 'Submitting patches', item 3.

Regards,
-Denis

#3 lore raw

Re: [PATCH v3 2/3] hfp_hf: Handle ServicesResolved signal

In reply to Denis Kenzior

Hi Denis,

Quoted 37 lines · Denis Kenzior

Hi Luiz,

Quoted 33 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds handling for ServicesResolved signal which tells when BlueZ is done resolving the device services so the code will no longer ignore devices that got its services resolved after Paired signal.

---
 plugins/hfp_hf_bluez5.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
plugins/hfp_hf_bluez5.c +2 -2
index fb7cc3f..f7d2450 100644
@@ -555,9 +555,9 @@ static struct ofono_modem
 *modem_register_from_proxy(GDBusProxy *proxy,
 
        dbus_message_iter_get_basic(&iter, &paired);
 
-       if (paired == FALSE) {
-               modem = ofono_modem_find(device_path_compare, (void *)
 path);
+       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);
plugins/hfp_hf_bluez5.c +0 -0
@@ -566,6 +566,9 @@ static struct ofono_modem
 *modem_register_from_proxy(GDBusProxy *proxy,
                return NULL;
        }
 
Quoted 8 lines

plugins/hfp_hf_bluez5.c static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy, · line 571

Quoted 3 lines
plugins/hfp_hf_bluez5.c +3 -0
+       if (modem)
+               return modem;
+

Why would you return a non-NULL value here? I thought your intended semantics (from patch 1) were to return the modem that was actually newly registered?

The ServiceResolved is toggled every time the device is connected since the BlueZ attempt to update it cache, thus this checks if the modem already exists and if it does return it.

Quoted 4 lines

The new_connection code path is really quite special, so it really begs a comment. I also wonder if modem_register_from_proxy should be separated into two functions. One to handle the actual registration path, and one to handle the device removal.

Yep, I will split this into registration and removal.

Quoted 7 lines

plugins/hfp_hf_bluez5.c static struct ofono_modem *modem_register_from_proxy(GDBusProxy *proxy, · line 573

Quoted 3 lines
plugins/hfp_hf_bluez5.c +0 -0
        if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE)
                return NULL;

Can UUIDs change from having HFP AG to not having HFP AG btw? We might be missing an additional remove path in that case.

It may actually remove UUIDs, so I guess we should remove the modem in the same way we remove if not paired.

Quoted 2 lines

Regards,
-Denis

Signature · 1 line
Luiz Augusto von Dentz