Skip to contentSkip to conversation

handsfree-audio: Add setting SCO air mode

PATCH In review Vinicius Costa Gomes 2 participants 12 messages Sep 11, 2013 – Sep 12, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 handsfree-audio: Add setting SCO air mode

---

I did a few quick tests here, but it would be much appreciated if someone with access to more devices could test it.

Sorry for the delay.

Cheers,

 src/handsfree-audio.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
src/handsfree-audio.c +32 -0
index 0bdeb99..c311b04 100644
@@ -68,6 +68,33 @@ static GSList *drivers = 0;
 static ofono_bool_t has_wideband = FALSE;
 static int defer_setup = 1;
 
+static uint16_t codec2setting(uint8_t codec)
+{
+	switch (codec) {
+		case HFP_CODEC_CVSD:
+			return BT_VOICE_CVSD_16BIT;
+		default:
+			return BT_VOICE_TRANSPARENT;
+	}
+}
+
+static ofono_bool_t apply_settings_from_codec(int fd, uint8_t codec)
+{
+	struct bt_voice voice;
+
+	/* CVSD is the default, no need to set BT_VOICE. */
+	if (codec == HFP_CODEC_CVSD)
+		return TRUE;
+
+	memset(&voice, 0, sizeof(voice));
+	voice.setting = codec2setting(codec);
+
+	if (setsockopt(fd, SOL_BLUETOOTH, BT_VOICE, &voice, sizeof(voice)) < 0)
+		return FALSE;
+
+	return TRUE;
+}
+
 static void send_new_connection(const char *card, int fd, uint8_t codec)
 {
 	DBusMessage *msg;
@@ -150,6 +177,11 @@ static gboolean sco_accept(GIOChannel *io, GIOCondition cond,
 		return TRUE;
 	}
 
+	if (apply_settings_from_codec(nsk, card->selected_codec) == FALSE) {
+		close(nsk);
+		return TRUE;
+	}
+
 	send_new_connection(card->path, nsk, card->selected_codec);
 	close(nsk);
 

--
1.8.4

#1 lore raw

PATCH 2/7 handsfree-audio: Add support for detecting Transparent SCO support

---
 include/handsfree-audio.h |  2 +-
 plugins/hfp_hf_bluez5.c   |  2 +-
 src/handsfree-audio.c     | 18 ++++++++++++++----
 3 files changed, 16 insertions(+), 6 deletions(-)
include/handsfree-audio.h +1 -1
index 846a032..03e3b38 100644
@@ -53,7 +53,7 @@ ofono_bool_t ofono_handsfree_card_set_codec(struct ofono_handsfree_card *card,
 
 ofono_bool_t ofono_handsfree_audio_has_wideband(void);
 
-ofono_bool_t ofono_handsfree_audio_has_defer_setup(void);
+ofono_bool_t ofono_handsfree_audio_has_transparent_sco(void);
 
 void ofono_handsfree_card_set_data(struct ofono_handsfree_card *card,
 					void *data);
plugins/hfp_hf_bluez5.c +1 -1
index 40d8ce0..f08bf53 100644
@@ -645,7 +645,7 @@ static void connect_handler(DBusConnection *conn, void *user_data)
 	 * Assuming that if defer_setup is supported, then SCO transparent
 	 * mode is also supported
 	*/
-	if (ofono_handsfree_audio_has_defer_setup())
+	if (ofono_handsfree_audio_has_transparent_sco())
 		features |= HFP_SDP_HF_FEATURE_WIDEBAND_SPEECH;
 
 	DBG("Registering External Profile handler ...");
src/handsfree-audio.c +14 -4
index c311b04..52d111a 100644
@@ -67,6 +67,7 @@ static guint sco_watch = 0;
 static GSList *drivers = 0;
 static ofono_bool_t has_wideband = FALSE;
 static int defer_setup = 1;
+static ofono_bool_t transparent_sco = FALSE;
 
 static uint16_t codec2setting(uint8_t codec)
 {
@@ -192,7 +193,9 @@ static int sco_init(void)
 {
 	GIOChannel *sco_io;
 	struct sockaddr_sco saddr;
+	struct bt_voice voice;
 	int sk;
+	socklen_t len;
 
 	sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | O_NONBLOCK | SOCK_CLOEXEC,
 								BTPROTO_SCO);
@@ -216,6 +219,13 @@ static int sco_init(void)
 						strerror(errno), errno);
 	}
 
+	memset(&voice, 0, sizeof(voice));
+	len = sizeof(voice);
+
+	if (defer_setup && getsockopt(sk, SOL_BLUETOOTH, BT_VOICE,
+						&voice, &len) == 0)
+		transparent_sco = TRUE;
+
 	if (listen(sk, 5) < 0) {
 		close(sk);
 		return -errno;
@@ -599,9 +609,9 @@ ofono_bool_t ofono_handsfree_audio_has_wideband(void)
 	return has_wideband;
 }
 
-ofono_bool_t ofono_handsfree_audio_has_defer_setup(void)
+ofono_bool_t ofono_handsfree_audio_has_transparent_sco(void)
 {
-	return defer_setup == 1;
+	return transparent_sco;
 }
 
 static void agent_free(struct agent *agent)
@@ -721,12 +731,12 @@ static DBusMessage *am_agent_register(DBusConnection *conn,
 	DBG("Agent %s registered with the CODECs:%s%s", sender,
 		has_cvsd ? " CVSD" : "", has_msbc ? " mSBC" : "");
 
-	if (has_msbc && defer_setup == 1)
+	if (has_msbc && transparent_sco)
 		has_wideband = TRUE;
 	else {
 		has_wideband = FALSE;
 		DBG("Wideband speech disabled: %s", has_msbc ?
-			"no SCO defer setup support" : "no mSBC support");
+			"no Transparent SCO support" : "no mSBC support");
 	}
 
 	if (has_cvsd == FALSE) {
Signature · 1 line
1.8.4
#2 lore raw

Re: [PATCH 2/7] handsfree-audio: Add support for detecting Transparent SCO support

Hi Vinicius,

Quoted 6 lines · Vinicius Costa Gomes
---
  include/handsfree-audio.h |  2 +-
  plugins/hfp_hf_bluez5.c   |  2 +-
  src/handsfree-audio.c     | 18 ++++++++++++++----
  3 files changed, 16 insertions(+), 6 deletions(-)

Patches 2-5 have been applied with a tweak of the commit description.

Regards,
-Denis

#3 lore raw

PATCH 3/7 handsfree-audio: Set the voice setting for outgoing connections

In reply to Vinicius Costa Gomes

In the AG case, the voice setting needs to be set before we can use Transparent SCO mode, which is necessary for Wideband speech support.

---
 src/handsfree-audio.c | 5 +++++
 1 file changed, 5 insertions(+)
src/handsfree-audio.c +5 -0
index 52d111a..5cfc939 100644
@@ -478,6 +478,11 @@ int ofono_handsfree_card_connect_sco(struct ofono_handsfree_card *card)
 	addr.sco_family = AF_BLUETOOTH;
 	bt_str2ba(card->remote, &addr.sco_bdaddr);
 
+	if (apply_settings_from_codec(sk, card->selected_codec) == FALSE) {
+		close(sk);
+		return -1;
+	}
+
 	ret = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
 	if (ret < 0 && errno != EINPROGRESS) {
 		close(sk);
Signature · 1 line
1.8.4
#4 lore raw

PATCH 4/7 handsfree-audio: Do not allow cards to be registered if no defer_setup

In reply to Vinicius Costa Gomes

If the kernel doesn't support defer_setup for SCO, we shouldn't allow cards to be registered, because in that case we won't be able to properly send the file descriptor to the Agent.

---
 src/handsfree-audio.c | 3 +++
 1 file changed, 3 insertions(+)
src/handsfree-audio.c +3 -0
index 5cfc939..4d2c21f 100644
@@ -816,6 +816,9 @@ int ofono_handsfree_card_driver_register(
 {
 	DBG("driver: %p", d);
 
+	if (defer_setup == 0)
+		return -ENOSYS;
+
 	if (d->probe == NULL)
 		return -EINVAL;
 
Signature · 1 line
1.8.4
#5 lore raw

PATCH 5/7 handsfree-audio: Fix keeping the socket open without defer_setup

In reply to Vinicius Costa Gomes

As we won't allow any card to be registered when the kernel doesn't support defer_setup, we don't need to have the listening SCO socket open in this case.

---
 src/handsfree-audio.c | 3 +++
 1 file changed, 3 insertions(+)
src/handsfree-audio.c +3 -0
index 4d2c21f..a1d9714 100644
@@ -214,9 +214,12 @@ static int sco_init(void)
 
 	if (setsockopt(sk, SOL_BLUETOOTH, BT_DEFER_SETUP,
 				&defer_setup, sizeof(defer_setup)) < 0) {
+		int err = -errno;
 		defer_setup = 0;
 		ofono_warn("Can't enable deferred setup: %s (%d)",
 						strerror(errno), errno);
+		close(sk);
+		return err;
 	}
 
 	memset(&voice, 0, sizeof(voice));
Signature · 1 line
1.8.4
#6 lore raw

PATCH 6/7 hfp_hf_bluez5: Handle org.bluez.Profile1.Cancel()

In reply to Vinicius Costa Gomes

Cancel() will be called when the ConnectProfile() is called, perhaps even replied with a success, and the user cancels the connection attempt.

---
 plugins/hfp_hf_bluez5.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
plugins/hfp_hf_bluez5.c +1 -3
index f08bf53..7e98a45 100644
@@ -574,9 +574,7 @@ static DBusMessage *profile_cancel(DBusConnection *conn,
 {
 	DBG("Profile handler Cancel");
 
-	return g_dbus_create_error(msg, BLUEZ_ERROR_INTERFACE
-					".NotImplemented",
-					"Implementation not provided");
+	return dbus_message_new_method_return(msg);
 }
 
 static DBusMessage *profile_disconnection(DBusConnection *conn,
Signature · 1 line
1.8.4
#7 lore raw

Re: [PATCH 6/7] hfp_hf_bluez5: Handle org.bluez.Profile1.Cancel()

Hi Vinicius,

Quoted 7 lines · Vinicius Costa Gomes

Cancel() will be called when the ConnectProfile() is called, perhaps even replied with a success, and the user cancels the connection attempt.

---
  plugins/hfp_hf_bluez5.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)

Since BlueZ removed the Cancel method from the profile implementation, I've removed it as well in commit 62c2b81db78eb32de0be9c3a809ddbf27b6e1c32.

Regards,
-Denis

#8 lore raw

PATCH 7/7 hfp_hf_bluez5: Handle org.bluez.Profile1.Release()

In reply to Vinicius Costa Gomes

There's nothing we can do in this case, so we only reply with a success.

---
 plugins/hfp_hf_bluez5.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
plugins/hfp_hf_bluez5.c +1 -3
index 7e98a45..391a74e 100644
@@ -564,9 +564,7 @@ static DBusMessage *profile_release(DBusConnection *conn,
 {
 	DBG("Profile handler Release");
 
-	return g_dbus_create_error(msg, BLUEZ_ERROR_INTERFACE
-						".NotImplemented",
-						"Implementation not provided");
+	return dbus_message_new_method_return(msg);
 }
 
 static DBusMessage *profile_cancel(DBusConnection *conn,
Signature · 1 line
1.8.4
#9 lore raw

Re: [PATCH 7/7] hfp_hf_bluez5: Handle org.bluez.Profile1.Release()

Hi Vinicius,

Quoted 6 lines · Vinicius Costa Gomes

There's nothing we can do in this case, so we only reply with a success.

---
  plugins/hfp_hf_bluez5.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)

Since BlueZ now marks this method as noreply, I fixed it to be the same in commit f2103a7ada1a4f5bfb7fee8e5d0ff261718ca759.

Regards,
-Denis

#10 lore raw
In reply to Vinicius Costa Gomes

Hi Vinicius,

Quoted 11 lines · Vinicius Costa Gomes

---

I did a few quick tests here, but it would be much appreciated if someone with access to more devices could test it.

Sorry for the delay.

Cheers,

  src/handsfree-audio.c | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)

Patch has been applied, but I tweaked the logic a bit in follow on commit e574d44d00780f2c7010677a6c261ffba82ea83f. Can you double check and see if you are okay with it?

Regards,
-Denis

#11 lore raw

Hi Denis,

Quoted 13 lines · Denis Kenzior

Hi Vinicius,

Quoted 8 lines · Vinicius Costa Gomes

---

I did a few quick tests here, but it would be much appreciated if someone with access to more devices could test it.

Sorry for the delay.

Quoted 7 lines
Quoted 4 lines

Cheers,

  src/handsfree-audio.c | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)

Patch has been applied, but I tweaked the logic a bit in follow on commit e574d44d00780f2c7010677a6c261ffba82ea83f. Can you double check and see if you are okay with it?

Yeah, I am ok with it.

Quoted 3 lines

Regards,
-Denis

Cheers,

Signature · 1 line
Vinicius