All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command
@ 2024-08-05 12:04 Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 1/7] shared/bass: Add API to send GATT write command Iulia Tanasescu
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

This implements the MediaAssistant "Push" command, in BlueZ and
bluetoothctl assistant submenu.

After issuing the "Push" command, the user is prompted to enter
any stream metadata to be sent to the peer. If the "auto" value
is chosen, the default metadata found in the BASE will be sent.
Otherwise, the LTVs found in the BASE will be overwritten by the
user input.

If the stream is encrypted, the user is also prompted to enter the
Broadcast Code for decrypting. If the "auto" value is chosen, a zero
filled array will be provided over DBus.

Below is a bluetoothctl log to exercise the "Push" command for an
unencrypted stream:

client/bluetoothctl
[bluetooth]# [CHG] Controller 00:60:37:31:7E:3F Pairable: yes
[bluetooth]# AdvertisementMonitor path registered
[bluetooth]# scan on
[bluetooth]# [NEW] Device 00:60:37:31:7E:3F 00-60-37-31-7E-3F
[bluetooth]# connect 00:60:37:31:7E:3F
Attempting to connect to 00:60:37:31:7E:3F
[CHG] Device 00:60:37:31:7E:3F Connected: yes
[00-60-37-31-7E-3F]# Connection successful
[00-60-37-31-7E-3F]# [NEW] Device 15:19:44:63:76:7A 15-19-44-63-76-7A
[00-60-37-31-7E-3F]# [NEW] Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
[00-60-37-31-7E-3F]# assistant.push
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
[Assistant] Enter Metadata (auto/value): 0x03 0x02 0x04 0x00
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
      State: pending
[00-60-37-31-7E-3F]# Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
      pushed
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
      State: active

The btmon log below shows the GATT write command sent by the Assistant
and the GATT notification received from the peer:

< ACL Data TX: Handle 0 flags 0x00 dlen 32
      ATT: Write Command (0x52) len 27
        Handle: 0x0040 Type: Broadcast Audio Scan Control Point (0x2bc7)
          Data[25]: 02017a766344191500c21a3702ffff01010000000403020400
            Opcode: Add Source (0x02)
            Source_Address_Type: 1
            Source_Address: 15:19:44:63:76:7A
            Source_Adv_SID: 0
            Broadcast_ID: 0x371ac2
            PA_Sync_State: Synchronize to PA - PAST not available
            PA_Interval: 0xffff
            Num_Subgroups: 1
            Subgroup #0:
              BIS_Sync State: 0x00000001
              Metadata: #0: len 0x03 type 0x02
              Metadata:   04 00

> ACL Data RX: Handle 0 flags 0x01 dlen 6
      ATT: Handle Multiple Value Notification (0x23) len 28
        Length: 0x0018
        Handle: 0x003a Type: Broadcast Receive State (0x2bc8)
          Data[24]: 01017a766344191500c21a37020001010000000403020400
          Source_ID: 1
          Source_Address_Type: 1
          Source_Address: 15:19:44:63:76:7A
          Source_Adv_SID: 0
          Broadcast_ID: 0x371ac2
          PA_Sync_State: Synchronized to PA
          BIG_Encryption: Not encrypted
          Num_Subgroups: 1
          Subgroup #0:
            BIS_Sync State: 0x00000001
            Metadata: #0: len 0x03 type 0x02
            Metadata:   04 00

The bluetoothctl log below shows the "Push" command flow for an
encrypted stream:

client/bluetoothctl
[bluetooth]# [CHG] Controller 00:60:37:31:7E:3F Pairable: yes
[bluetooth]# AdvertisementMonitor path registered
[bluetooth]# scan on
[bluetooth]# [NEW] Device 00:60:37:31:7E:3F 00-60-37-31-7E-3F
[bluetooth]# connect 00:60:37:31:7E:3F
Attempting to connect to 00:60:37:31:7E:3F
[CHG] Device 00:60:37:31:7E:3F Connected: yes
[00-60-37-31-7E-3F]# Connection successful
[00-60-37-31-7E-3F]# [NEW] Device 05:1F:EE:F3:F8:7D 05-1F-EE-F3-F8-7D
[00-60-37-31-7E-3F]# [NEW] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
[00-60-37-31-7E-3F]# assistant.push
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
[Assistant] Enter Metadata (auto/value): 0x03 0x02 0x04 0x00
[Assistant] Enter Broadcast Code (auto/value): 0x01 0x02 0x68 0x05 0x53
      0xf1 0x41 0x5a 0xa2 0x65 0xbb 0xaf 0xc6 0xea 0x03 0xb8
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      State: pending
[00-60-37-31-7E-3F]# Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      pushed
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      State: requesting
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      State: active

The GATT write commands and notifications for this scenario are shown
in the btmon log below:

< ACL Data TX: Handle 0 flags 0x00 dlen 32
      ATT: Write Command (0x52) len 27
        Handle: 0x0040 Type: Broadcast Audio Scan Control Point (0x2bc7)
          Data[25]: 02017df8f3ee1f0500f4015d02ffff01010000000403020400
            Opcode: Add Source (0x02)
            Source_Address_Type: 1
            Source_Address: 05:1F:EE:F3:F8:7D
            Source_Adv_SID: 0
            Broadcast_ID: 0x5d01f4
            PA_Sync_State: Synchronize to PA - PAST not available
            PA_Interval: 0xffff
            Num_Subgroups: 1
            Subgroup #0:
              BIS_Sync State: 0x00000001
              Metadata: #0: len 0x03 type 0x02
              Metadata:   04 00

> ACL Data RX: Handle 0 flags 0x01 dlen 6
      ATT: Handle Multiple Value Notification (0x23) len 28
        Length: 0x0018
        Handle: 0x003a Type: Broadcast Receive State (0x2bc8)
          Data[24]: 01017df8f3ee1f0500f4015d020101000000000403020400
          Source_ID: 1
          Source_Address_Type: 1
          Source_Address: 05:1F:EE:F3:F8:7D
          Source_Adv_SID: 0
          Broadcast_ID: 0x5d01f4
          PA_Sync_State: Synchronized to PA
          BIG_Encryption: Broadcast_Code required
          Num_Subgroups: 1
          Subgroup #0:
            BIS_Sync State: 0x00000000
            Metadata: #0: len 0x03 type 0x02
            Metadata:   04 00

< ACL Data TX: Handle 0 flags 0x00 dlen 25
      ATT: Write Command (0x52) len 20
        Handle: 0x0040 Type: Broadcast Audio Scan Control Point (0x2bc7)
          Data[18]: 04010102680553f1415aa265bbafc6ea03b8
            Opcode: Set Broadcast_Code (0x04)
            Source_ID: 1
            Broadcast_Code[16]: 0102680553f1415aa265bbafc6ea03b8

> ACL Data RX: Handle 0 flags 0x01 dlen 6
      ATT: Handle Multiple Value Notification (0x23) len 28
        Length: 0x0018
        Handle: 0x003a Type: Broadcast Receive State (0x2bc8)
          Data[24]: 01017df8f3ee1f0500f4015d020201010000000403020400
          Source_ID: 1
          Source_Address_Type: 1
          Source_Address: 05:1F:EE:F3:F8:7D
          Source_Adv_SID: 0
          Broadcast_ID: 0x5d01f4
          PA_Sync_State: Synchronized to PA
          BIG_Encryption: Decrypting
          Num_Subgroups: 1
          Subgroup #0:
            BIS_Sync State: 0x00000001
            Metadata: #0: len 0x03 type 0x02
            Metadata:   04 00

Iulia Tanasescu (7):
  shared/bass: Add API to send GATT write command
  device: Add support to iterate through service data
  bass: Store Broadcast ID inside assistant struct
  shared/bass: Add APIs to register bcast src changed cb
  shared/bass: Add additional defines
  bass: Implement MediaAssistant Push method
  assistant: Implement MediaAssistant Push command

 client/assistant.c    | 247 ++++++++++++++++++++++++++++++++
 profiles/audio/bass.c | 317 +++++++++++++++++++++++++++++++++++++++++-
 src/device.c          |   8 +-
 src/device.h          |   5 +-
 src/shared/bass.c     | 133 +++++++++++++++++-
 src/shared/bass.h     |  15 +-
 6 files changed, 720 insertions(+), 5 deletions(-)


base-commit: 100c845b2d20e7f4f96b371e044b8b59944230ab
-- 
2.39.2


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 1/7] shared/bass: Add API to send GATT write command
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-05 16:39   ` Implement the MediaAssistant "Push" command bluez.test.bot
  2024-08-05 12:04 ` [PATCH BlueZ 2/7] device: Add support to iterate through service data Iulia Tanasescu
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

This adds a BASS API to send a GATT write command for the
Broadcast Audio Scan Control Point characteristic.
---
 src/shared/bass.c | 38 +++++++++++++++++++++++++++++++++++++-
 src/shared/bass.h |  5 ++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/shared/bass.c b/src/shared/bass.c
index 268e3bd86..d9ab210b7 100644
--- a/src/shared/bass.c
+++ b/src/shared/bass.c
@@ -3,7 +3,7 @@
  *
  *  BlueZ - Bluetooth protocol stack for Linux
  *
- *  Copyright 2023 NXP
+ *  Copyright 2023-2024 NXP
  *
  */
 
@@ -1754,3 +1754,39 @@ void bt_bass_add_db(struct gatt_db *db, const bdaddr_t *adapter_bdaddr)
 {
 	bass_db_new(db, adapter_bdaddr);
 }
+
+int bt_bass_send(struct bt_bass *bass,
+		struct bt_bass_bcast_audio_scan_cp_hdr *hdr,
+		struct iovec *params)
+{
+	struct iovec req = {0};
+	uint16_t handle;
+	int err = 0;
+
+	if (!bass || !bass->client || !bass->rdb)
+		return -EINVAL;
+
+	DBG(bass, "bass %p", bass);
+
+	req.iov_base = malloc0(sizeof(*hdr) + params->iov_len);
+	if (!req.iov_base)
+		return -EINVAL;
+
+	util_iov_push_mem(&req, sizeof(*hdr), hdr);
+	util_iov_push_mem(&req, params->iov_len, params->iov_base);
+
+	if (!gatt_db_attribute_get_char_data(bass->rdb->bcast_audio_scan_cp,
+			NULL, &handle, NULL, NULL, NULL)) {
+		err = -EINVAL;
+		goto done;
+	}
+
+	if (!bt_gatt_client_write_without_response(bass->client, handle,
+					false, req.iov_base, req.iov_len))
+		err = -EINVAL;
+
+done:
+	free(req.iov_base);
+
+	return err;
+}
diff --git a/src/shared/bass.h b/src/shared/bass.h
index 1674146bc..864b01637 100644
--- a/src/shared/bass.h
+++ b/src/shared/bass.h
@@ -3,7 +3,7 @@
  *
  *  BlueZ - Bluetooth protocol stack for Linux
  *
- *  Copyright 2023 NXP
+ *  Copyright 2023-2024 NXP
  *
  */
 
@@ -135,3 +135,6 @@ bool bt_bass_attach(struct bt_bass *bass, struct bt_gatt_client *client);
 bool bt_bass_set_att(struct bt_bass *bass, struct bt_att *att);
 void bt_bass_detach(struct bt_bass *bass);
 void bt_bass_add_db(struct gatt_db *db, const bdaddr_t *adapter_bdaddr);
+int bt_bass_send(struct bt_bass *bass,
+		struct bt_bass_bcast_audio_scan_cp_hdr *hdr,
+		struct iovec *params);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 2/7] device: Add support to iterate through service data
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 1/7] shared/bass: Add API to send GATT write command Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 3/7] bass: Store Broadcast ID inside assistant struct Iulia Tanasescu
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

Some usecases require iterating through the service data structures
advertised by a scanned device - for example, a Broadcast Source
advertises the Broadcast Audio Announcement Service UUID, with the
Broadcast ID as service data. The Broadcast ID is used by audio
profiles to uniquely identify the Broadcast Source.

This adds an API to iterate through the service data advertised by
a device.
---
 src/device.c | 8 +++++++-
 src/device.h | 5 ++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/device.c b/src/device.c
index a1dc0750c..cf7264051 100644
--- a/src/device.c
+++ b/src/device.c
@@ -5,7 +5,7 @@
  *
  *  Copyright (C) 2006-2010  Nokia Corporation
  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
- *
+ *  Copyright 2024 NXP
  *
  */
 
@@ -7374,3 +7374,9 @@ void btd_device_set_conn_param(struct btd_device *device, uint16_t min_interval,
 					max_interval, latency,
 					timeout);
 }
+
+void btd_device_foreach_service_data(struct btd_device *dev, bt_ad_func_t func,
+							void *data)
+{
+	bt_ad_foreach_service_data(dev->ad, func, data);
+}
diff --git a/src/device.h b/src/device.h
index 3742f6028..97536774e 100644
--- a/src/device.h
+++ b/src/device.h
@@ -5,7 +5,7 @@
  *
  *  Copyright (C) 2006-2010  Nokia Corporation
  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
- *
+ *  Copyright 2024 NXP
  *
  */
 
@@ -211,3 +211,6 @@ void btd_device_foreach_ad(struct btd_device *dev, bt_device_ad_func_t func,
 void btd_device_set_conn_param(struct btd_device *device, uint16_t min_interval,
 					uint16_t max_interval, uint16_t latency,
 					uint16_t timeout);
+void btd_device_foreach_service_data(struct btd_device *dev,
+					bt_device_ad_func_t func,
+					void *data);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 3/7] bass: Store Broadcast ID inside assistant struct
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 1/7] shared/bass: Add API to send GATT write command Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 2/7] device: Add support to iterate through service data Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 4/7] shared/bass: Add APIs to register bcast src changed cb Iulia Tanasescu
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

A Broadcast Source is uniquely identified by its Broadcast ID, which
is being advertised in the Extended Advertising Data.

After discovering a Broadcast Source, a BAP Broadcast Assistant needs
to internally store the Broadcast ID, which will be transmitted to the
peer Scan Delegator via the BASS "Add Source" operation.
---
 profiles/audio/bass.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/profiles/audio/bass.c b/profiles/audio/bass.c
index 083988358..795b4b80a 100644
--- a/profiles/audio/bass.c
+++ b/profiles/audio/bass.c
@@ -3,7 +3,7 @@
  *
  *  BlueZ - Bluetooth protocol stack for Linux
  *
- *  Copyright 2023 NXP
+ *  Copyright 2023-2024 NXP
  *
  */
 
@@ -40,6 +40,7 @@
 #include "src/adapter.h"
 #include "src/shared/bass.h"
 #include "src/shared/bap.h"
+#include "src/shared/ad.h"
 
 #include "src/plugin.h"
 #include "src/gatt-database.h"
@@ -80,6 +81,7 @@ struct bass_assistant {
 	struct bass_data *data;		/* BASS session with peer device */
 	uint8_t sgrp;
 	uint8_t bis;
+	uint32_t bid;
 	struct bt_iso_qos qos;
 	struct iovec *meta;
 	struct iovec *caps;
@@ -198,6 +200,21 @@ static void assistant_free(void *data)
 	free(assistant);
 }
 
+static void src_ad_search_bid(void *data, void *user_data)
+{
+	struct bt_ad_service_data *sd = data;
+	struct bass_assistant *assistant = user_data;
+	struct iovec iov;
+
+	if (sd->uuid.type != BT_UUID16 || sd->uuid.value.u16 != BCAA_SERVICE)
+		return;
+
+	iov.iov_base = sd->data;
+	iov.iov_len = sd->len;
+
+	util_iov_pull_le24(&iov, &assistant->bid);
+}
+
 static struct bass_assistant *assistant_new(struct btd_adapter *adapter,
 		struct btd_device *device, struct bass_data *data,
 		uint8_t sgrp, uint8_t bis, struct bt_iso_qos *qos,
@@ -221,6 +238,9 @@ static struct bass_assistant *assistant_new(struct btd_adapter *adapter,
 	assistant->meta = util_iov_dup(meta, 1);
 	assistant->caps = util_iov_dup(caps, 1);
 
+	btd_device_foreach_service_data(assistant->device, src_ad_search_bid,
+							assistant);
+
 	ba2str(device_get_address(device), src_addr);
 	ba2str(device_get_address(data->device), dev_addr);
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 4/7] shared/bass: Add APIs to register bcast src changed cb
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
                   ` (2 preceding siblings ...)
  2024-08-05 12:04 ` [PATCH BlueZ 3/7] bass: Store Broadcast ID inside assistant struct Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 5/7] shared/bass: Add additional defines Iulia Tanasescu
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

A BAP Broadcast Assistant (BASS Client) needs to listen for GATT
notifications for Broadcast Receive State characteristics from peer
Scan Delegators (BASS Servers), in order to learn about updates in
Broadcasters known by the peer - for example, the Assistant can be
informed if the peer requires a Broadcast Code to decrypt an encrypted
stream.

This adds APIs that the BASS plugin can use to register/unregister
callbacks to be called on each received notification.
---
 src/shared/bass.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/bass.h |  5 +++
 2 files changed, 100 insertions(+)

diff --git a/src/shared/bass.c b/src/shared/bass.c
index d9ab210b7..b54bf3094 100644
--- a/src/shared/bass.c
+++ b/src/shared/bass.c
@@ -71,6 +71,8 @@ struct bt_bass {
 	bt_bass_destroy_func_t debug_destroy;
 	void *debug_data;
 
+	struct queue *src_cbs;
+
 	void *user_data;
 };
 
@@ -118,6 +120,13 @@ static struct bt_iso_qos default_qos = {
 	}
 };
 
+struct bt_bass_src_changed {
+	unsigned int id;
+	bt_bass_src_func_t cb;
+	bt_bass_destroy_func_t destroy;
+	void *data;
+};
+
 static void bass_bcast_src_free(void *data);
 
 static void bass_debug(struct bt_bass *bass, const char *format, ...)
@@ -132,6 +141,64 @@ static void bass_debug(struct bt_bass *bass, const char *format, ...)
 	va_end(ap);
 }
 
+unsigned int bt_bass_src_register(struct bt_bass *bass, bt_bass_src_func_t cb,
+				void *user_data, bt_bass_destroy_func_t destroy)
+{
+	struct bt_bass_src_changed *changed;
+	static unsigned int id;
+
+	if (!bass)
+		return 0;
+
+	changed = new0(struct bt_bass_src_changed, 1);
+	if (!changed)
+		return 0;
+
+	changed->id = ++id ? id : ++id;
+	changed->cb = cb;
+	changed->destroy = destroy;
+	changed->data = user_data;
+
+	queue_push_tail(bass->src_cbs, changed);
+
+	return changed->id;
+}
+
+static void bass_src_changed_free(void *data)
+{
+	struct bt_bass_src_changed *changed = data;
+
+	if (changed->destroy)
+		changed->destroy(changed->data);
+
+	free(changed);
+}
+
+static bool match_src_changed_id(const void *data, const void *match_data)
+{
+	const struct bt_bass_src_changed *changed = data;
+	unsigned int id = PTR_TO_UINT(match_data);
+
+	return (changed->id == id);
+}
+
+bool bt_bass_src_unregister(struct bt_bass *bass, unsigned int id)
+{
+	struct bt_bass_src_changed *changed;
+
+	if (!bass)
+		return false;
+
+	changed = queue_remove_if(bass->src_cbs, match_src_changed_id,
+						UINT_TO_PTR(id));
+	if (!changed)
+		return false;
+
+	bass_src_changed_free(changed);
+
+	return true;
+}
+
 static int bass_build_bcast_src(struct bt_bcast_src *bcast_src,
 				const uint8_t *value, uint16_t length)
 {
@@ -1321,6 +1388,27 @@ static void read_bcast_recv_state(bool success, uint8_t att_ecode,
 	}
 }
 
+static void notify_src_changed(void *data, void *user_data)
+{
+	struct bt_bass_src_changed *changed = data;
+	struct bt_bcast_src *bcast_src = user_data;
+	uint32_t bis_sync = 0;
+
+	for (uint8_t i = 0; i < bcast_src->num_subgroups; i++) {
+		struct bt_bass_subgroup_data *sgrp =
+				&bcast_src->subgroup_data[i];
+
+		/* Create a bitmask of all BIS indices that the peer has
+		 * synchronized with.
+		 */
+		bis_sync |= sgrp->bis_sync;
+	}
+
+	if (changed->cb)
+		changed->cb(bcast_src->id, bcast_src->bid, bcast_src->enc,
+					bis_sync, changed->data);
+}
+
 static void bcast_recv_state_notify(struct bt_bass *bass, uint16_t value_handle,
 				const uint8_t *value, uint16_t length,
 				void *user_data)
@@ -1353,6 +1441,11 @@ static void bcast_recv_state_notify(struct bt_bass *bass, uint16_t value_handle,
 
 	if (new_src)
 		queue_push_tail(bass->rdb->bcast_srcs, bcast_src);
+
+	/* Notify the update in the Broadcast Receive State characteristic
+	 * to all drivers that registered a callback.
+	 */
+	queue_foreach(bass->src_cbs, notify_src_changed, bcast_src);
 }
 
 static void bass_register(uint16_t att_ecode, void *user_data)
@@ -1562,6 +1655,7 @@ static void bass_free(void *data)
 	bt_bass_detach(bass);
 	bass_db_free(bass->rdb);
 	queue_destroy(bass->notify, NULL);
+	queue_destroy(bass->src_cbs, bass_src_changed_free);
 
 	free(bass);
 }
@@ -1656,6 +1750,7 @@ struct bt_bass *bt_bass_new(struct gatt_db *ldb, struct gatt_db *rdb,
 	bass = new0(struct bt_bass, 1);
 	bass->ldb = db;
 	bass->notify = queue_new();
+	bass->src_cbs = queue_new();
 
 	if (!rdb)
 		goto done;
diff --git a/src/shared/bass.h b/src/shared/bass.h
index 864b01637..01c1279bb 100644
--- a/src/shared/bass.h
+++ b/src/shared/bass.h
@@ -119,6 +119,8 @@ struct bt_bass_remove_src_params {
 typedef void (*bt_bass_func_t)(struct bt_bass *bass, void *user_data);
 typedef void (*bt_bass_destroy_func_t)(void *user_data);
 typedef void (*bt_bass_debug_func_t)(const char *str, void *user_data);
+typedef void (*bt_bass_src_func_t)(uint8_t id, uint32_t bid, uint8_t enc,
+					uint32_t bis_sync, void *user_data);
 
 struct bt_att *bt_bass_get_att(struct bt_bass *bass);
 struct bt_gatt_client *bt_bass_get_client(struct bt_bass *bass);
@@ -138,3 +140,6 @@ void bt_bass_add_db(struct gatt_db *db, const bdaddr_t *adapter_bdaddr);
 int bt_bass_send(struct bt_bass *bass,
 		struct bt_bass_bcast_audio_scan_cp_hdr *hdr,
 		struct iovec *params);
+unsigned int bt_bass_src_register(struct bt_bass *bass, bt_bass_src_func_t cb,
+			void *user_data, bt_bass_destroy_func_t destroy);
+bool bt_bass_src_unregister(struct bt_bass *bass, unsigned int id);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 5/7] shared/bass: Add additional defines
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
                   ` (3 preceding siblings ...)
  2024-08-05 12:04 ` [PATCH BlueZ 4/7] shared/bass: Add APIs to register bcast src changed cb Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 6/7] bass: Implement MediaAssistant Push method Iulia Tanasescu
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

This adds BASS macros for Broadcast Source address types and for the
unknown PA interval value. These values are to be used as parameters
for the "Add Source" command.
---
 src/shared/bass.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/shared/bass.h b/src/shared/bass.h
index 01c1279bb..878bab51a 100644
--- a/src/shared/bass.h
+++ b/src/shared/bass.h
@@ -74,6 +74,9 @@ struct bt_bass_bcast_audio_scan_cp_hdr {
 
 #define BT_BASS_ADD_SRC					0x02
 
+#define BT_BASS_ADDR_PUBLIC				0x00
+#define BT_BASS_ADDR_RANDOM				0x01
+
 /* PA_Sync values */
 #define PA_SYNC_NO_SYNC					0x00
 #define PA_SYNC_PAST					0x01
@@ -82,6 +85,8 @@ struct bt_bass_bcast_audio_scan_cp_hdr {
 /* BIS_Sync no preference bitmask */
 #define BIS_SYNC_NO_PREF				0xFFFFFFFF
 
+#define PA_INTERVAL_UNKNOWN				0xFFFF
+
 struct bt_bass_add_src_params {
 	uint8_t addr_type;
 	bdaddr_t addr;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 6/7] bass: Implement MediaAssistant Push method
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
                   ` (4 preceding siblings ...)
  2024-08-05 12:04 ` [PATCH BlueZ 5/7] shared/bass: Add additional defines Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-05 12:04 ` [PATCH BlueZ 7/7] assistant: Implement MediaAssistant Push command Iulia Tanasescu
  2024-08-06 20:29 ` [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command patchwork-bot+bluetooth
  7 siblings, 0 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

This implements the MediaAssistant "Push" method, which sends a GATT
write command for the Broadcast Audio Scan Control Point characteristic
with the "Add Source" opcode.

This also registers a callback with shared/bass, to handle GATT
notifications from the peer. A MediaAssistant object transitions
to different states, depending on information received in the
callback. The callback is also useful to check if the peer requires
a Broadcast Code to decrypt the stream - in this case, the Assistant
uses the "Set Broadcast_Code" operation to provide the code.
---
 profiles/audio/bass.c | 295 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 295 insertions(+)

diff --git a/profiles/audio/bass.c b/profiles/audio/bass.c
index 795b4b80a..b3740f64e 100644
--- a/profiles/audio/bass.c
+++ b/profiles/audio/bass.c
@@ -70,10 +70,18 @@ enum assistant_state {
 					 */
 };
 
+static const char *const str_state[] = {
+	"ASSISTANT_STATE_IDLE",
+	"ASSISTANT_STATE_PENDING",
+	"ASSISTANT_STATE_REQUESTING",
+	"ASSISTANT_STATE_ACTIVE",
+};
+
 struct bass_data {
 	struct btd_device *device;
 	struct btd_service *service;
 	struct bt_bass *bass;
+	unsigned int src_id;
 };
 
 struct bass_assistant {
@@ -92,14 +100,206 @@ struct bass_assistant {
 static struct queue *sessions;
 static struct queue *assistants;
 
+static const char *state2str(enum assistant_state state);
+
 static void bass_debug(const char *str, void *user_data)
 {
 	DBG_IDX(0xffff, "%s", str);
 }
 
+static void assistant_set_state(struct bass_assistant *assistant,
+					enum assistant_state state)
+{
+	enum assistant_state old_state = assistant->state;
+	const char *str;
+
+	if (old_state == state)
+		return;
+
+	assistant->state = state;
+
+	DBG("State changed %s: %s -> %s", assistant->path, str_state[old_state],
+							str_state[state]);
+
+	str = state2str(state);
+
+	if (g_strcmp0(str, state2str(old_state)) != 0)
+		g_dbus_emit_property_changed(btd_get_dbus_connection(),
+						assistant->path,
+						MEDIA_ASSISTANT_INTERFACE,
+						"State");
+}
+
+static int assistant_parse_qos(struct bass_assistant *assistant,
+						DBusMessageIter *iter)
+{
+	DBusMessageIter dict;
+	const char *key;
+
+	dbus_message_iter_recurse(iter, &dict);
+
+	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+		DBusMessageIter value, entry;
+		int var;
+
+		dbus_message_iter_recurse(&dict, &entry);
+		dbus_message_iter_get_basic(&entry, &key);
+
+		dbus_message_iter_next(&entry);
+		dbus_message_iter_recurse(&entry, &value);
+
+		var = dbus_message_iter_get_arg_type(&value);
+
+		if (!strcasecmp(key, "BCode")) {
+			DBusMessageIter array;
+			struct iovec iov = {0};
+
+			if (var != DBUS_TYPE_ARRAY)
+				return -EINVAL;
+
+			dbus_message_iter_recurse(&value, &array);
+			dbus_message_iter_get_fixed_array(&array,
+							&iov.iov_base,
+							(int *)&iov.iov_len);
+
+			if (iov.iov_len != BT_BASS_BCAST_CODE_SIZE) {
+				error("Invalid size for BCode: %zu != 16",
+								iov.iov_len);
+				return -EINVAL;
+			}
+
+			memcpy(assistant->qos.bcast.bcode, iov.iov_base,
+								iov.iov_len);
+
+			return 0;
+		}
+
+		dbus_message_iter_next(&dict);
+	}
+
+	return 0;
+}
+
+static int assistant_parse_props(struct bass_assistant *assistant,
+					DBusMessageIter *props)
+{
+	DBusMessageIter value, entry, array;
+	const char *key;
+
+	while (dbus_message_iter_get_arg_type(props) == DBUS_TYPE_DICT_ENTRY) {
+		dbus_message_iter_recurse(props, &entry);
+		dbus_message_iter_get_basic(&entry, &key);
+
+		dbus_message_iter_next(&entry);
+		dbus_message_iter_recurse(&entry, &value);
+
+		if (!strcasecmp(key, "Metadata")) {
+			struct iovec iov;
+
+			if (dbus_message_iter_get_arg_type(&value) !=
+							DBUS_TYPE_ARRAY)
+				goto fail;
+
+			dbus_message_iter_recurse(&value, &array);
+			dbus_message_iter_get_fixed_array(&array,
+							&iov.iov_base,
+							(int *)&iov.iov_len);
+
+			util_iov_free(assistant->meta, 1);
+			assistant->meta = util_iov_dup(&iov, 1);
+			DBG("Parsed Metadata");
+		} else if (!strcasecmp(key, "QoS")) {
+			if (dbus_message_iter_get_arg_type(&value) !=
+							DBUS_TYPE_ARRAY)
+				goto fail;
+
+			if (assistant_parse_qos(assistant, &value))
+				goto fail;
+
+			DBG("Parsed QoS");
+		}
+
+		dbus_message_iter_next(props);
+	}
+
+	return 0;
+
+fail:
+	DBG("Failed parsing %s", key);
+
+	return -EINVAL;
+}
+
 static DBusMessage *push(DBusConnection *conn, DBusMessage *msg,
 							  void *user_data)
 {
+	struct bass_assistant *assistant = user_data;
+	struct bt_bass_bcast_audio_scan_cp_hdr hdr;
+	struct bt_bass_add_src_params params;
+	struct iovec iov = {0};
+	uint32_t bis_sync = 0;
+	uint8_t meta_len = 0;
+	int err;
+	DBusMessageIter props, dict;
+
+	DBG("");
+
+	dbus_message_iter_init(msg, &props);
+
+	if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_ARRAY) {
+		DBG("Unable to parse properties");
+		return btd_error_invalid_args(msg);
+	}
+
+	dbus_message_iter_recurse(&props, &dict);
+
+	if (assistant_parse_props(assistant, &dict)) {
+		DBG("Unable to parse properties");
+		return btd_error_invalid_args(msg);
+	}
+
+	hdr.op = BT_BASS_ADD_SRC;
+
+	if (device_get_le_address_type(assistant->device) == BDADDR_LE_PUBLIC)
+		params.addr_type = BT_BASS_ADDR_PUBLIC;
+	else
+		params.addr_type = BT_BASS_ADDR_RANDOM;
+
+	bacpy(&params.addr, device_get_address(assistant->device));
+	put_le24(assistant->bid, params.bid);
+	params.pa_sync = PA_SYNC_NO_PAST;
+	params.pa_interval = PA_INTERVAL_UNKNOWN;
+	params.num_subgroups = assistant->sgrp + 1;
+
+	util_iov_append(&iov, &params, sizeof(params));
+
+	/* Metadata and the BIS index associated with the MediaAssistant
+	 * object will be set in the subgroup they belong to. For the other
+	 * subgroups, no metadata and no BIS index will be provided.
+	 */
+	for (uint8_t sgrp = 0; sgrp < assistant->sgrp; sgrp++) {
+		util_iov_append(&iov, &bis_sync, sizeof(bis_sync));
+		util_iov_append(&iov, &meta_len, sizeof(meta_len));
+	}
+
+	bis_sync = (1 << (assistant->bis - 1));
+	meta_len = assistant->meta->iov_len;
+
+	util_iov_append(&iov, &bis_sync, sizeof(bis_sync));
+	util_iov_append(&iov, &meta_len, sizeof(meta_len));
+	util_iov_append(&iov, assistant->meta->iov_base,
+				assistant->meta->iov_len);
+
+	err = bt_bass_send(assistant->data->bass, &hdr, &iov);
+	if (err) {
+		DBG("Unable to send BASS Write Command");
+		return btd_error_failed(msg, strerror(-err));
+	}
+
+	free(iov.iov_base);
+
+	assistant_set_state(assistant, ASSISTANT_STATE_PENDING);
+
 	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
@@ -381,6 +581,8 @@ static void bass_data_free(struct bass_data *data)
 		bt_bass_set_user_data(data->bass, NULL);
 	}
 
+	bt_bass_src_unregister(data->bass, data->src_id);
+
 	bt_bass_unref(data->bass);
 
 	queue_remove_all(assistants, assistant_match_data,
@@ -453,6 +655,93 @@ static void bass_attached(struct bt_bass *bass, void *user_data)
 	bass_data_add(data);
 }
 
+static void bass_handle_bcode_req(struct bass_assistant *assistant, int id)
+{
+	struct bt_bass_bcast_audio_scan_cp_hdr hdr;
+	struct bt_bass_set_bcast_code_params params;
+	struct iovec iov = {0};
+	int err;
+
+	assistant_set_state(assistant, ASSISTANT_STATE_REQUESTING);
+
+	hdr.op = BT_BASS_SET_BCAST_CODE;
+
+	params.id = id;
+	memcpy(params.bcast_code, assistant->qos.bcast.bcode,
+					BT_BASS_BCAST_CODE_SIZE);
+
+	iov.iov_base = malloc0(sizeof(params));
+	if (!iov.iov_base)
+		return;
+
+	util_iov_push_mem(&iov, sizeof(params), &params);
+
+	err = bt_bass_send(assistant->data->bass, &hdr, &iov);
+	if (err) {
+		DBG("Unable to send BASS Write Command");
+		return;
+	}
+
+	free(iov.iov_base);
+}
+
+static void bass_src_changed(uint8_t id, uint32_t bid, uint8_t enc,
+					uint32_t bis_sync, void *user_data)
+{
+	const struct queue_entry *entry;
+
+	for (entry = queue_get_entries(assistants); entry;
+						entry = entry->next) {
+		struct bass_assistant *assistant = entry->data;
+		uint32_t bis = 1 << (assistant->bis - 1);
+
+		if (assistant->bid != bid)
+			/* Only handle assistant objects
+			 * that match the source
+			 */
+			continue;
+
+		switch (enc) {
+		case BT_BASS_BIG_ENC_STATE_BCODE_REQ:
+			if (assistant->state != ASSISTANT_STATE_PENDING)
+				/* Only handle assistant objects that
+				 * have been pushed by the user
+				 */
+				break;
+
+			/* Provide Broadcast Code to peer */
+			bass_handle_bcode_req(assistant, id);
+			break;
+		case BT_BASS_BIG_ENC_STATE_NO_ENC:
+			if (assistant->state != ASSISTANT_STATE_PENDING)
+				/* Only handle assistant objects that
+				 * have been pushed by the user
+				 */
+				break;
+
+			/* Match BIS index */
+			if (bis & bis_sync)
+				assistant_set_state(assistant,
+						ASSISTANT_STATE_ACTIVE);
+			break;
+		case BT_BASS_BIG_ENC_STATE_DEC:
+			/* Only handle assistant objects that
+			 * have requested a Broadcast Code
+			 */
+			if (assistant->state != ASSISTANT_STATE_REQUESTING)
+				break;
+
+			/* Match BIS index */
+			if (bis & bis_sync)
+				assistant_set_state(assistant,
+						ASSISTANT_STATE_ACTIVE);
+			break;
+		default:
+			continue;
+		}
+	}
+}
+
 static int bass_probe(struct btd_service *service)
 {
 	struct btd_device *device = btd_service_get_device(service);
@@ -485,6 +774,12 @@ static int bass_probe(struct btd_service *service)
 	bass_data_add(data);
 	bt_bass_set_user_data(data->bass, service);
 
+	/* Register callback to be called when notifications for
+	 * Broadcast Receive State characteristics are received.
+	 */
+	data->src_id = bt_bass_src_register(data->bass, bass_src_changed,
+						data, NULL);
+
 	return 0;
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH BlueZ 7/7] assistant: Implement MediaAssistant Push command
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
                   ` (5 preceding siblings ...)
  2024-08-05 12:04 ` [PATCH BlueZ 6/7] bass: Implement MediaAssistant Push method Iulia Tanasescu
@ 2024-08-05 12:04 ` Iulia Tanasescu
  2024-08-06 20:29 ` [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command patchwork-bot+bluetooth
  7 siblings, 0 replies; 10+ messages in thread
From: Iulia Tanasescu @ 2024-08-05 12:04 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: claudia.rosu, mihai-octavian.urzica, vlad.pruteanu,
	andrei.istodorescu, luiz.dentz, Iulia Tanasescu

This implements the MediaAssistant "Push" command, to trigger the
BlueZ Broadcast Assistant to send stream information to the peer.

After issuing the "Push" command, the user is prompted to enter
any stream metadata to be sent to the peer. If the "auto" value
is chosen, the default metadata found in the BASE will be sent.
Otherwise, the LTVs found in the BASE will be overwritten by the
user input.

If the stream is encrypted, the user is also prompted to enter the
Broadcast Code for decrypting. If the "auto" value is chosen, a zero
filled array will be provided over DBus.

Below is a bluetoothctl log to exercise the "Push" command for an
unencrypted stream:

client/bluetoothctl
[bluetooth]# [CHG] Controller 00:60:37:31:7E:3F Pairable: yes
[bluetooth]# AdvertisementMonitor path registered
[bluetooth]# scan on
[bluetooth]# [NEW] Device 00:60:37:31:7E:3F 00-60-37-31-7E-3F
[bluetooth]# connect 00:60:37:31:7E:3F
Attempting to connect to 00:60:37:31:7E:3F
[CHG] Device 00:60:37:31:7E:3F Connected: yes
[00-60-37-31-7E-3F]# Connection successful
[00-60-37-31-7E-3F]# [NEW] Device 15:19:44:63:76:7A 15-19-44-63-76-7A
[00-60-37-31-7E-3F]# [NEW] Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
[00-60-37-31-7E-3F]# assistant.push
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
[Assistant] Enter Metadata (auto/value): 0x03 0x02 0x04 0x00
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
      State: pending
[00-60-37-31-7E-3F]# Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
      pushed
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_15_19_44_63_76_7A/dev_00_60_37_31_7E_3F/bis1
      State: active

The btmon log below shows the GATT write command sent by the Assistant
and the GATT notification received from the peer:

< ACL Data TX: Handle 0 flags 0x00 dlen 32
      ATT: Write Command (0x52) len 27
        Handle: 0x0040 Type: Broadcast Audio Scan Control Point (0x2bc7)
          Data[25]: 02017a766344191500c21a3702ffff01010000000403020400
            Opcode: Add Source (0x02)
            Source_Address_Type: 1
            Source_Address: 15:19:44:63:76:7A
            Source_Adv_SID: 0
            Broadcast_ID: 0x371ac2
            PA_Sync_State: Synchronize to PA - PAST not available
            PA_Interval: 0xffff
            Num_Subgroups: 1
            Subgroup #0:
              BIS_Sync State: 0x00000001
              Metadata: #0: len 0x03 type 0x02
              Metadata:   04 00

> ACL Data RX: Handle 0 flags 0x01 dlen 6
      ATT: Handle Multiple Value Notification (0x23) len 28
        Length: 0x0018
        Handle: 0x003a Type: Broadcast Receive State (0x2bc8)
          Data[24]: 01017a766344191500c21a37020001010000000403020400
          Source_ID: 1
          Source_Address_Type: 1
          Source_Address: 15:19:44:63:76:7A
          Source_Adv_SID: 0
          Broadcast_ID: 0x371ac2
          PA_Sync_State: Synchronized to PA
          BIG_Encryption: Not encrypted
          Num_Subgroups: 1
          Subgroup #0:
            BIS_Sync State: 0x00000001
            Metadata: #0: len 0x03 type 0x02
            Metadata:   04 00

The bluetoothctl log below shows the "Push" command flow for an
encrypted stream:

client/bluetoothctl
[bluetooth]# [CHG] Controller 00:60:37:31:7E:3F Pairable: yes
[bluetooth]# AdvertisementMonitor path registered
[bluetooth]# scan on
[bluetooth]# [NEW] Device 00:60:37:31:7E:3F 00-60-37-31-7E-3F
[bluetooth]# connect 00:60:37:31:7E:3F
Attempting to connect to 00:60:37:31:7E:3F
[CHG] Device 00:60:37:31:7E:3F Connected: yes
[00-60-37-31-7E-3F]# Connection successful
[00-60-37-31-7E-3F]# [NEW] Device 05:1F:EE:F3:F8:7D 05-1F-EE-F3-F8-7D
[00-60-37-31-7E-3F]# [NEW] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
[00-60-37-31-7E-3F]# assistant.push
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
[Assistant] Enter Metadata (auto/value): 0x03 0x02 0x04 0x00
[Assistant] Enter Broadcast Code (auto/value): 0x01 0x02 0x68 0x05 0x53
      0xf1 0x41 0x5a 0xa2 0x65 0xbb 0xaf 0xc6 0xea 0x03 0xb8
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      State: pending
[00-60-37-31-7E-3F]# Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      pushed
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      State: requesting
[00-60-37-31-7E-3F]# [CHG] Assistant
      /org/bluez/hci0/src_05_1F_EE_F3_F8_7D/dev_00_60_37_31_7E_3F/bis1
      State: active

The GATT write commands and notifications for this scenario are shown
in the btmon log below:

< ACL Data TX: Handle 0 flags 0x00 dlen 32
      ATT: Write Command (0x52) len 27
        Handle: 0x0040 Type: Broadcast Audio Scan Control Point (0x2bc7)
          Data[25]: 02017df8f3ee1f0500f4015d02ffff01010000000403020400
            Opcode: Add Source (0x02)
            Source_Address_Type: 1
            Source_Address: 05:1F:EE:F3:F8:7D
            Source_Adv_SID: 0
            Broadcast_ID: 0x5d01f4
            PA_Sync_State: Synchronize to PA - PAST not available
            PA_Interval: 0xffff
            Num_Subgroups: 1
            Subgroup #0:
              BIS_Sync State: 0x00000001
              Metadata: #0: len 0x03 type 0x02
              Metadata:   04 00

> ACL Data RX: Handle 0 flags 0x01 dlen 6
      ATT: Handle Multiple Value Notification (0x23) len 28
        Length: 0x0018
        Handle: 0x003a Type: Broadcast Receive State (0x2bc8)
          Data[24]: 01017df8f3ee1f0500f4015d020101000000000403020400
          Source_ID: 1
          Source_Address_Type: 1
          Source_Address: 05:1F:EE:F3:F8:7D
          Source_Adv_SID: 0
          Broadcast_ID: 0x5d01f4
          PA_Sync_State: Synchronized to PA
          BIG_Encryption: Broadcast_Code required
          Num_Subgroups: 1
          Subgroup #0:
            BIS_Sync State: 0x00000000
            Metadata: #0: len 0x03 type 0x02
            Metadata:   04 00

< ACL Data TX: Handle 0 flags 0x00 dlen 25
      ATT: Write Command (0x52) len 20
        Handle: 0x0040 Type: Broadcast Audio Scan Control Point (0x2bc7)
          Data[18]: 04010102680553f1415aa265bbafc6ea03b8
            Opcode: Set Broadcast_Code (0x04)
            Source_ID: 1
            Broadcast_Code[16]: 0102680553f1415aa265bbafc6ea03b8

> ACL Data RX: Handle 0 flags 0x01 dlen 6
      ATT: Handle Multiple Value Notification (0x23) len 28
        Length: 0x0018
        Handle: 0x003a Type: Broadcast Receive State (0x2bc8)
          Data[24]: 01017df8f3ee1f0500f4015d020201010000000403020400
          Source_ID: 1
          Source_Address_Type: 1
          Source_Address: 05:1F:EE:F3:F8:7D
          Source_Adv_SID: 0
          Broadcast_ID: 0x5d01f4
          PA_Sync_State: Synchronized to PA
          BIG_Encryption: Decrypting
          Num_Subgroups: 1
          Subgroup #0:
            BIS_Sync State: 0x00000001
            Metadata: #0: len 0x03 type 0x02
            Metadata:   04 00
---
 client/assistant.c | 247 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 247 insertions(+)

diff --git a/client/assistant.c b/client/assistant.c
index 69a955c18..77fb78329 100644
--- a/client/assistant.c
+++ b/client/assistant.c
@@ -43,6 +43,14 @@
 
 #define MEDIA_ASSISTANT_INTERFACE "org.bluez.MediaAssistant1"
 
+#define BCODE_LEN		16
+
+struct assistant_config {
+	GDBusProxy *proxy;	/* DBus object reference */
+	struct iovec *meta;	/* Stream metadata LTVs */
+	struct bt_iso_qos qos;	/* Stream QoS parameters */
+};
+
 static DBusConnection *dbus_conn;
 
 static GList *assistants;
@@ -141,10 +149,249 @@ static void disconnect_handler(DBusConnection *connection, void *user_data)
 	assistants = NULL;
 }
 
+static uint8_t *str2bytearray(char *arg, size_t *val_len)
+{
+	uint8_t value[UINT8_MAX];
+	char *entry;
+	unsigned int i;
+
+	for (i = 0; (entry = strsep(&arg, " \t")) != NULL; i++) {
+		long val;
+		char *endptr = NULL;
+
+		if (*entry == '\0')
+			continue;
+
+		if (i >= G_N_ELEMENTS(value)) {
+			bt_shell_printf("Too much data\n");
+			return NULL;
+		}
+
+		val = strtol(entry, &endptr, 0);
+		if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
+			bt_shell_printf("Invalid value at index %d\n", i);
+			return NULL;
+		}
+
+		value[i] = val;
+	}
+
+	*val_len = i;
+
+	return util_memdup(value, i);
+}
+
+static void append_qos(DBusMessageIter *iter, struct assistant_config *cfg)
+{
+	DBusMessageIter entry, var, dict;
+	const char *key = "QoS";
+	const char *bcode_key = "BCode";
+	uint8_t *bcode = cfg->qos.bcast.bcode;
+
+	dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY,
+						NULL, &entry);
+
+	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
+
+	dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
+						"a{sv}", &var);
+
+	dbus_message_iter_open_container(&var, DBUS_TYPE_ARRAY, "{sv}",
+					&dict);
+
+	g_dbus_dict_append_basic_array(&dict, DBUS_TYPE_STRING,
+					&bcode_key, DBUS_TYPE_BYTE,
+					&bcode, BCODE_LEN);
+
+	dbus_message_iter_close_container(&var, &dict);
+	dbus_message_iter_close_container(&entry, &var);
+	dbus_message_iter_close_container(iter, &entry);
+}
+
+static void push_setup(DBusMessageIter *iter, void *user_data)
+{
+	struct assistant_config *cfg = user_data;
+	DBusMessageIter dict;
+	const char *meta = "Metadata";
+
+	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &dict);
+
+	if (cfg->meta)
+		g_dbus_dict_append_basic_array(&dict, DBUS_TYPE_STRING, &meta,
+				DBUS_TYPE_BYTE, &cfg->meta->iov_base,
+				cfg->meta->iov_len);
+
+	if (cfg->qos.bcast.encryption)
+		append_qos(&dict, cfg);
+
+	dbus_message_iter_close_container(iter, &dict);
+}
+
+static void push_reply(DBusMessage *message, void *user_data)
+{
+	struct assistant_config *cfg = user_data;
+	DBusError error;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message)) {
+		bt_shell_printf("Failed to push assistant: %s\n",
+				error.name);
+
+		dbus_error_free(&error);
+
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_printf("Assistant %s pushed\n",
+				g_dbus_proxy_get_path(cfg->proxy));
+
+	free(cfg->meta);
+	g_free(cfg);
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+static void assistant_set_bcode_cfg(const char *input, void *user_data)
+{
+	struct assistant_config *cfg = user_data;
+	char *endptr;
+	uint8_t *bcode = cfg->qos.bcast.bcode;
+
+	if (!strcasecmp(input, "a") || !strcasecmp(input, "auto")) {
+		memset(bcode, 0, BCODE_LEN);
+	} else {
+		bcode[0] = strtol(input, &endptr, 16);
+
+		for (uint8_t i = 1; i < BCODE_LEN; i++)
+			bcode[i] = strtol(endptr, &endptr, 16);
+	}
+
+	if (!g_dbus_proxy_method_call(cfg->proxy, "Push",
+					push_setup, push_reply,
+					cfg, NULL)) {
+		bt_shell_printf("Failed to push assistant\n");
+
+		free(cfg->meta);
+		g_free(cfg);
+
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+}
+
+static void assistant_set_metadata_cfg(const char *input, void *user_data)
+{
+	struct assistant_config *cfg = user_data;
+	DBusMessageIter iter, dict, entry, value;
+	const char *key;
+
+	if (!strcasecmp(input, "a") || !strcasecmp(input, "auto"))
+		goto done;
+
+	if (!cfg->meta)
+		cfg->meta = g_new0(struct iovec, 1);
+
+	cfg->meta->iov_base = str2bytearray((char *) input,
+				&cfg->meta->iov_len);
+	if (!cfg->meta->iov_base) {
+		free(cfg->meta);
+		cfg->meta = NULL;
+	}
+
+done:
+	/* Get QoS property to check if the stream is encrypted */
+	if (!g_dbus_proxy_get_property(cfg->proxy, "QoS", &iter))
+		goto fail;
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
+		goto fail;
+
+	dbus_message_iter_recurse(&iter, &dict);
+
+	if (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_DICT_ENTRY)
+		goto fail;
+
+	dbus_message_iter_recurse(&dict, &entry);
+	dbus_message_iter_get_basic(&entry, &key);
+
+	if (strcasecmp(key, "Encryption") != 0)
+		goto fail;
+
+	dbus_message_iter_next(&entry);
+	dbus_message_iter_recurse(&entry, &value);
+
+	if (dbus_message_iter_get_arg_type(&value) != DBUS_TYPE_BYTE)
+		goto fail;
+
+	dbus_message_iter_get_basic(&value, &cfg->qos.bcast.encryption);
+
+	if (cfg->qos.bcast.encryption)
+		/* Prompt user to enter the Broadcast Code to decrypt
+		 * the stream
+		 */
+		bt_shell_prompt_input("Assistant",
+				"Enter Broadcast Code (auto/value):",
+				assistant_set_bcode_cfg, cfg);
+	else
+		if (!g_dbus_proxy_method_call(cfg->proxy, "Push",
+						push_setup, push_reply,
+						cfg, NULL)) {
+			bt_shell_printf("Failed to push assistant\n");
+			goto fail;
+		}
+
+	return;
+
+fail:
+	free(cfg->meta);
+	g_free(cfg);
+
+	return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
+static void cmd_push_assistant(int argc, char *argv[])
+{
+	struct assistant_config *cfg;
+
+	cfg = new0(struct assistant_config, 1);
+	if (!cfg)
+		goto fail;
+
+	/* Search for DBus object */
+	cfg->proxy = g_dbus_proxy_lookup(assistants, NULL, argv[1],
+						MEDIA_ASSISTANT_INTERFACE);
+	if (!cfg->proxy) {
+		bt_shell_printf("Assistant %s not found\n", argv[1]);
+		goto fail;
+	}
+
+	/* Prompt user to enter metadata */
+	bt_shell_prompt_input("Assistant",
+			"Enter Metadata (auto/value):",
+			assistant_set_metadata_cfg, cfg);
+
+	return;
+
+fail:
+	g_free(cfg);
+	return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
+static const struct bt_shell_menu assistant_menu = {
+	.name = "assistant",
+	.desc = "Media Assistant Submenu",
+	.entries = {
+	{ "push", "<assistant>", cmd_push_assistant,
+					"Send stream information to peer" },
+	{} },
+};
+
 static GDBusClient * client;
 
 void assistant_add_submenu(void)
 {
+	bt_shell_add_submenu(&assistant_menu);
+
 	dbus_conn = bt_shell_get_env("DBUS_CONNECTION");
 	if (!dbus_conn || client)
 		return;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* RE: Implement the MediaAssistant "Push" command
  2024-08-05 12:04 ` [PATCH BlueZ 1/7] shared/bass: Add API to send GATT write command Iulia Tanasescu
@ 2024-08-05 16:39   ` bluez.test.bot
  0 siblings, 0 replies; 10+ messages in thread
From: bluez.test.bot @ 2024-08-05 16:39 UTC (permalink / raw)
  To: linux-bluetooth, iulia.tanasescu

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

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=876697

---Test result---

Test Summary:
CheckPatch                    PASS      4.44 seconds
GitLint                       PASS      3.63 seconds
BuildEll                      PASS      24.73 seconds
BluezMake                     PASS      1713.09 seconds
MakeCheck                     PASS      13.55 seconds
MakeDistcheck                 PASS      181.30 seconds
CheckValgrind                 PASS      253.21 seconds
CheckSmatch                   PASS      357.08 seconds
bluezmakeextell               PASS      120.23 seconds
IncrementalBuild              PASS      11308.49 seconds
ScanBuild                     PASS      1006.70 seconds



---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command
  2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
                   ` (6 preceding siblings ...)
  2024-08-05 12:04 ` [PATCH BlueZ 7/7] assistant: Implement MediaAssistant Push command Iulia Tanasescu
@ 2024-08-06 20:29 ` patchwork-bot+bluetooth
  7 siblings, 0 replies; 10+ messages in thread
From: patchwork-bot+bluetooth @ 2024-08-06 20:29 UTC (permalink / raw)
  To: Iulia Tanasescu
  Cc: linux-bluetooth, claudia.rosu, mihai-octavian.urzica,
	vlad.pruteanu, andrei.istodorescu, luiz.dentz

Hello:

This series was applied to bluetooth/bluez.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:

On Mon,  5 Aug 2024 15:04:22 +0300 you wrote:
> This implements the MediaAssistant "Push" command, in BlueZ and
> bluetoothctl assistant submenu.
> 
> After issuing the "Push" command, the user is prompted to enter
> any stream metadata to be sent to the peer. If the "auto" value
> is chosen, the default metadata found in the BASE will be sent.
> Otherwise, the LTVs found in the BASE will be overwritten by the
> user input.
> 
> [...]

Here is the summary with links:
  - [BlueZ,1/7] shared/bass: Add API to send GATT write command
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=a626ae163fd7
  - [BlueZ,2/7] device: Add support to iterate through service data
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=11fce8b09897
  - [BlueZ,3/7] bass: Store Broadcast ID inside assistant struct
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=a426c3617109
  - [BlueZ,4/7] shared/bass: Add APIs to register bcast src changed cb
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=fc9c4f2313b7
  - [BlueZ,5/7] shared/bass: Add additional defines
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=15811387144c
  - [BlueZ,6/7] bass: Implement MediaAssistant Push method
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=117b993f3d68
  - [BlueZ,7/7] assistant: Implement MediaAssistant Push command
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=e1aa24a43949

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-08-06 20:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-05 12:04 [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command Iulia Tanasescu
2024-08-05 12:04 ` [PATCH BlueZ 1/7] shared/bass: Add API to send GATT write command Iulia Tanasescu
2024-08-05 16:39   ` Implement the MediaAssistant "Push" command bluez.test.bot
2024-08-05 12:04 ` [PATCH BlueZ 2/7] device: Add support to iterate through service data Iulia Tanasescu
2024-08-05 12:04 ` [PATCH BlueZ 3/7] bass: Store Broadcast ID inside assistant struct Iulia Tanasescu
2024-08-05 12:04 ` [PATCH BlueZ 4/7] shared/bass: Add APIs to register bcast src changed cb Iulia Tanasescu
2024-08-05 12:04 ` [PATCH BlueZ 5/7] shared/bass: Add additional defines Iulia Tanasescu
2024-08-05 12:04 ` [PATCH BlueZ 6/7] bass: Implement MediaAssistant Push method Iulia Tanasescu
2024-08-05 12:04 ` [PATCH BlueZ 7/7] assistant: Implement MediaAssistant Push command Iulia Tanasescu
2024-08-06 20:29 ` [PATCH BlueZ 0/7] Implement the MediaAssistant "Push" command patchwork-bot+bluetooth

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.