Skip to contentSkip to conversation

monitor: Decode Periodic Advertising with Responses

PATCH Superseded Roland Schober 2 participants 6 messages Nov 17 – Nov 18 Bluetooth drivers

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover monitor: Decode Periodic Advertising with Responses

This series teaches btmon the HCI commands and events of Periodic Advertising with Responses (PAwR, Bluetooth 5.4). Until now they were only listed by name in the opcode table and the events were printed as unknown.

It is meant as the first step towards supporting the PAwR advertiser role in the kernel and in BlueZ, which is what an access point of the Electronic Shelf Label profile needs: being able to read the traces comes before changing anything else.

Patch 1 is a fix for an existing decoder found on the way: the Connection_Handle returned by the Periodic Advertising Sync Transfer commands and by LE Remove ISO Data Path was read as one octet.

Patch 2 splits the LE Periodic Advertising Report decoding into helpers without changing its output, so that patch 4 can share it.

Patch 3 decodes the five commands and patch 4 the six events, including the [v2] variants of existing events.

Testing: I have no controller with PAwR support at hand yet. The commands were sent with hcitool to a controller which rejects them, so that btmon decodes what the host sent, and the events and the successful command responses were decoded from hand-made btsnoop traces. The output of the existing LE Periodic Advertising Report decoder was compared before and after patch 2.

Roland Schober (4):
monitor: Fix handle decoding of status_handle_rsp
monitor: Split LE PA Report decoding into helpers
monitor: Add PAwR HCI commands decoding
monitor: Add PAwR HCI events decoding

 monitor/analyze.c |   1 +
 monitor/bt.h      | 174 +++++++++++++++++
 monitor/packet.c  | 487 ++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 626 insertions(+), 36 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/4 monitor: Fix handle decoding of status_handle_rsp

status_handle_rsp() decodes the return parameters of commands which return a Status followed by a Connection_Handle, but it only reads the first octet of the two octets of the handle, so any handle above 255 is printed wrong.

With LE Periodic Advertising Set Info Transfer returning handle 3585:

Quoted 1 line

HCI Event: Command Complete (0x0e) plen 6

      LE Periodic Advertising Set Info Transfer (0x08|0x005b) ncmd 1
        Status: Unknown Connection Identifier (0x02)
        Connection handle: 1

Read the handle as a 16-bit value:

Connection handle: 3585

Fixes b73d4bb9e3c9 ("monitor: Decode LE Periodic Advertising Sync Transfer command")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
monitor/packet.c +1 -1
index eddf8706a..4ff24092e 100644
@@ -4976,7 +4976,7 @@ static void status_handle_rsp(uint16_t index, const void *data, uint8_t size)
 	uint8_t status = *((const uint8_t *) data);
 
 	print_status(status);
-	print_field("Connection handle: %d", get_u8(data + 1));
+	print_field("Connection handle: %d", get_le16(data + 1));
 }
 
 static void status_bdaddr_rsp(uint16_t index, const void *data, uint8_t size)
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/4 monitor: Split LE PA Report decoding into helpers

In reply to Roland Schober

Move the decoding of the reception information (handle, TX power, RSSI and CTE type) and of the data (status, length and AD structures) of the LE Periodic Advertising Report event into helpers, and the CTE type names into a function of their own.

This is a preparation for decoding the events added by Periodic Advertising with Responses, which carry the same information, and does not change the output.

Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 75 +++++++++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 33 deletions(-)
monitor/packet.c +42 -33
index 4ff24092e..b31b8c31c 100644
@@ -13897,46 +13897,45 @@ static void le_pa_sync_evt(struct timeval *tv, uint16_t index,
 	print_field("Advertiser clock accuracy: 0x%2.2x", evt->clock_accuracy);
 }
 
-static void le_pa_report_evt(struct timeval *tv, uint16_t index,
-				const void *data, uint8_t size)
+static const char *pa_cte_type_str(uint8_t cte_type)
 {
-	const struct bt_hci_le_pa_report *evt = data;
-	const char *color_on;
-	const char *str;
-
-	print_field("Sync handle: %d", evt->handle);
-	print_power_level(evt->tx_power, NULL);
-	if (evt->rssi == 127)
-		print_field("RSSI: not available (0x%2.2x)",
-						(uint8_t) evt->rssi);
-	else if (evt->rssi >= -127 && evt->rssi <= 20)
-		print_field("RSSI: %d dBm (0x%2.2x)",
-				evt->rssi, (uint8_t) evt->rssi);
-	else
-		print_field("RSSI: reserved (0x%2.2x)",
-						(uint8_t) evt->rssi);
-
-	switch (evt->cte_type) {
+	switch (cte_type) {
 	case 0x00:
-		str = "AoA Constant Tone Extension";
-		break;
+		return "AoA Constant Tone Extension";
 	case 0x01:
-		str = "AoA Constant Tone Extension with 1us slots";
-		break;
+		return "AoA Constant Tone Extension with 1us slots";
 	case 0x02:
-		str = "AoD Constant Tone Extension with 2us slots";
-		break;
+		return "AoD Constant Tone Extension with 2us slots";
 	case 0xff:
-		str = "No Constant Tone Extension";
-		break;
+		return "No Constant Tone Extension";
 	default:
-		str = "Reserved";
-		break;
+		return "Reserved";
 	}
+}
+
+static void print_pa_report_info(uint16_t handle, uint8_t tx_power,
+						int8_t rssi, uint8_t cte_type)
+{
+	print_field("Sync handle: %d", handle);
+	print_power_level(tx_power, NULL);
+	if (rssi == 127)
+		print_field("RSSI: not available (0x%2.2x)", (uint8_t) rssi);
+	else if (rssi >= -127 && rssi <= 20)
+		print_field("RSSI: %d dBm (0x%2.2x)", rssi, (uint8_t) rssi);
+	else
+		print_field("RSSI: reserved (0x%2.2x)", (uint8_t) rssi);
 
-	print_field("CTE Type: %s (0x%2x)", str, evt->cte_type);
+	print_field("CTE Type: %s (0x%2x)", pa_cte_type_str(cte_type),
+								cte_type);
+}
 
-	switch (evt->data_status) {
+static void print_pa_report_data(uint8_t data_status, uint8_t data_len,
+							const uint8_t *data)
+{
+	const char *color_on;
+	const char *str;
+
+	switch (data_status) {
 	case 0x00:
 		str = "Complete";
 		color_on = COLOR_GREEN;
@@ -13956,8 +13955,18 @@ static void le_pa_report_evt(struct timeval *tv, uint16_t index,
 	}
 
 	print_field("Data status: %s%s%s", color_on, str, COLOR_OFF);
-	print_field("Data length: %u", evt->data_len);
-	print_eir("Data", evt->data, evt->data_len, true);
+	print_field("Data length: %u", data_len);
+	print_eir("Data", data, data_len, true);
+}
+
+static void le_pa_report_evt(struct timeval *tv, uint16_t index,
+				const void *data, uint8_t size)
+{
+	const struct bt_hci_le_pa_report *evt = data;
+
+	print_pa_report_info(evt->handle, evt->tx_power, evt->rssi,
+							evt->cte_type);
+	print_pa_report_data(evt->data_status, evt->data_len, evt->data);
 }
 
 static void le_pa_sync_lost_evt(struct timeval *tv, uint16_t index,
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/4 monitor: Add PAwR HCI commands decoding

In reply to Roland Schober

Decode the HCI commands introduced by Periodic Advertising with Responses (PAwR) in Bluetooth 5.4, which so far only had their names in the opcode table:

- LE Set Periodic Advertising Subevent Data
- LE Set Periodic Advertising Response Data
- LE Set Periodic Sync Subevent
- LE Extended Create Connection [v2]
- LE Set Periodic Advertising Parameters [v2]

The subevent data of LE Set Periodic Advertising Subevent Data is a list of elements of variable length, each with its own AD structures:

< HCI Command: LE Set Periodic Advertising Subevent.. (0x08|0x0082) plen 15
Handle: 0
Num subevents: 2
Entry 0
Subevent: 0 (0x00)
Response slot start: 0
Response slot count: 2
Data length: 5
Data[5]:
04 ff 59 00 01 ..Y..
Company: Nordic Semiconductor ASA (89)
Data[1]:
01 .
Entry 1
Subevent: 1 (0x01)
Response slot start: 0
Response slot count: 0
Data length: 0

< HCI Command: LE Set Periodic Advertising Paramet.. (0x08|0x0086) plen 12

        Handle: 0
        Min interval: 1000.00 msec (0x0320)
        Max interval: 1000.00 msec (0x0320)
        Properties: 0x0000
        Num subevents: 4 (0x04)
        Subevent interval: 100.00 msec (0x50)
        Response slot delay: 6.25 msec (0x05)
        Response slot spacing: 1.000 msec (0x08)
        Num response slots: 32 (0x20)
Assisted-by Claude:claude-opus-5-5
Assisted-by Claude:claude-fable-5-2
---
 monitor/bt.h     |  84 ++++++++++++++++++
 monitor/packet.c | 223 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 302 insertions(+), 5 deletions(-)
monitor/bt.h +84 -0
index 59bc90ccd..b97592afb 100644
@@ -3032,6 +3032,90 @@ struct bt_hci_rsp_le_read_iso_link_quality {
 	uint32_t duplicated_packets;
 } __packed;
 
+#define BT_HCI_CMD_LE_SET_PA_SUBEVENT_DATA	0x2082
+#define BT_HCI_BIT_LE_SET_PA_SUBEVENT_DATA	BT_HCI_CMD_BIT(46, 5)
+struct bt_hci_le_pa_subevent_data {
+	uint8_t  subevent;
+	uint8_t  response_slot_start;
+	uint8_t  response_slot_count;
+	uint8_t  data_len;
+	uint8_t  data[];
+} __packed;
+
+struct bt_hci_cmd_le_set_pa_subevent_data {
+	uint8_t  handle;
+	uint8_t  num_subevents;
+	struct bt_hci_le_pa_subevent_data subevents[];
+} __packed;
+
+struct bt_hci_rsp_le_set_pa_subevent_data {
+	uint8_t  status;
+	uint8_t  handle;
+} __packed;
+
+#define BT_HCI_CMD_LE_SET_PA_RESPONSE_DATA	0x2083
+#define BT_HCI_BIT_LE_SET_PA_RESPONSE_DATA	BT_HCI_CMD_BIT(46, 6)
+struct bt_hci_cmd_le_set_pa_response_data {
+	uint16_t sync_handle;
+	uint16_t request_event;
+	uint8_t  request_subevent;
+	uint8_t  response_subevent;
+	uint8_t  response_slot;
+	uint8_t  data_len;
+	uint8_t  data[];
+} __packed;
+
+struct bt_hci_rsp_le_set_pa_response_data {
+	uint8_t  status;
+	uint16_t sync_handle;
+} __packed;
+
+#define BT_HCI_CMD_LE_SET_PA_SYNC_SUBEVENT	0x2084
+#define BT_HCI_BIT_LE_SET_PA_SYNC_SUBEVENT	BT_HCI_CMD_BIT(46, 7)
+struct bt_hci_cmd_le_set_pa_sync_subevent {
+	uint16_t sync_handle;
+	uint16_t properties;
+	uint8_t  num_subevents;
+	uint8_t  subevents[];
+} __packed;
+
+struct bt_hci_rsp_le_set_pa_sync_subevent {
+	uint8_t  status;
+	uint16_t sync_handle;
+} __packed;
+
+#define BT_HCI_CMD_LE_EXT_CREATE_CONN_V2	0x2085
+#define BT_HCI_BIT_LE_EXT_CREATE_CONN_V2	BT_HCI_CMD_BIT(47, 0)
+struct bt_hci_cmd_le_ext_create_conn_v2 {
+	uint8_t  adv_handle;
+	uint8_t  subevent;
+	uint8_t  filter_policy;
+	uint8_t  own_addr_type;
+	uint8_t  peer_addr_type;
+	uint8_t  peer_addr[6];
+	uint8_t  phys;
+	uint8_t  data[];
+} __packed;
+
+#define BT_HCI_CMD_LE_SET_PA_PARAMS_V2		0x2086
+#define BT_HCI_BIT_LE_SET_PA_PARAMS_V2		BT_HCI_CMD_BIT(47, 1)
+struct bt_hci_cmd_le_set_pa_params_v2 {
+	uint8_t  handle;
+	uint16_t min_interval;
+	uint16_t max_interval;
+	uint16_t properties;
+	uint8_t  num_subevents;
+	uint8_t  subevent_interval;
+	uint8_t  response_slot_delay;
+	uint8_t  response_slot_spacing;
+	uint8_t  num_response_slots;
+} __packed;
+
+struct bt_hci_rsp_le_set_pa_params_v2 {
+	uint8_t  status;
+	uint8_t  handle;
+} __packed;
+
 #define BT_HCI_CMD_LE_READ_ALL_LOCAL_FEATURES	0x2087
 #define BT_HCI_BIT_LE_READ_ALL_LOCAL_FEATURES	BT_HCI_CMD_BIT(47, 2)
 struct bt_hci_rsp_le_read_all_local_features {
monitor/packet.c +218 -5

241 lines not on this page · raw message

Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/4 monitor: Add PAwR HCI events decoding

In reply to Roland Schober

Decode the LE Meta events introduced by Periodic Advertising with Responses (PAwR) in Bluetooth 5.4:

- LE Periodic Advertising Sync Established [v2]
- LE Periodic Advertising Report [v2]
- LE Periodic Advertising Sync Transfer Received [v2]
- LE Periodic Advertising Subevent Data Request
- LE Periodic Advertising Response Report
- LE Enhanced Connection Complete [v2]

The [v2] events reuse the decoding of the events they extend, and LE Enhanced Connection Complete [v2] is tracked like [v1], both by the decoder and by the analyze mode.

Quoted 1 line

HCI Event: LE Meta Event (0x3e) plen 4

      LE Periodic Advertising Subevent Data Request (0x27)
        Handle: 0
        Subevent start: 0 (0x00)
        Subevent data count: 4 (0x04)
Quoted 1 line

HCI Event: LE Meta Event (0x3e) plen 25

      LE Periodic Advertising Response Report (0x28)
        Handle: 0
        Subevent: 2 (0x02)
        TX status: AUX_SYNC_SUBEVENT_IND was transmitted (0x00)
        Num responses: 2
        Entry 0
          TX power: 127 dBm (0x7f)
          RSSI: -55 dBm (0xc9)
          CTE Type: No Constant Tone Extension (0xff)
          Response slot: 0 (0x00)
          Data status: Complete
          Data length: 5
          Data[5]:
        04 ff 59 00 2a                                   ..Y.*
        Company: Nordic Semiconductor ASA (89)
          Data[1]:
        2a                                               *
        Entry 1
          TX power: 127 dBm (0x7f)
          RSSI: not available (0x7f)
          CTE Type: No Constant Tone Extension (0xff)
          Response slot: 1 (0x01)
          Data status: Failed to receive AUX_SYNC_SUBEVENT_RSP
          Data length: 0
Assisted-by Claude:claude-fable-5-2
---
 monitor/analyze.c |   1 +
 monitor/bt.h      |  90 +++++++++++++++++++++
 monitor/packet.c  | 193 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 284 insertions(+)
monitor/analyze.c +1 -0
index 5d54ad61b..b16db8bd1 100644
@@ -1171,6 +1171,7 @@ static void evt_le_meta_event(struct hci_dev *dev, struct timeval *tv,
 		evt_le_conn_complete(dev, tv, frame, &iov);
 		break;
 	case BT_HCI_EVT_LE_ENHANCED_CONN_COMPLETE:
+	case BT_HCI_EVT_LE_ENHANCED_CONN_COMPLETE_V2:
 		evt_le_enh_conn_complete(dev, tv, frame, &iov);
 		break;
 	case BT_HCI_EVT_LE_CIS_ESTABLISHED:
monitor/bt.h +90 -0

96 lines not on this page · raw message

monitor/packet.c +193 -0

229 lines not on this page · raw message

Signature · 1 line
2.47.3
#5 lore raw
In reply to Roland Schober

Hi Roland,

Quoted 34 lines · Roland Schober

This series teaches btmon the HCI commands and events of Periodic Advertising with Responses (PAwR, Bluetooth 5.4). Until now they were only listed by name in the opcode table and the events were printed as unknown.

It is meant as the first step towards supporting the PAwR advertiser role in the kernel and in BlueZ, which is what an access point of the Electronic Shelf Label profile needs: being able to read the traces comes before changing anything else.

Patch 1 is a fix for an existing decoder found on the way: the Connection_Handle returned by the Periodic Advertising Sync Transfer commands and by LE Remove ISO Data Path was read as one octet.

Patch 2 splits the LE Periodic Advertising Report decoding into helpers without changing its output, so that patch 4 can share it.

Patch 3 decodes the five commands and patch 4 the six events, including the [v2] variants of existing events.

Testing: I have no controller with PAwR support at hand yet. The commands were sent with hcitool to a controller which rejects them, so that btmon decodes what the host sent, and the events and the successful command responses were decoded from hand-made btsnoop traces. The output of the existing LE Periodic Advertising Report decoder was compared before and after patch 2.

Roland Schober (4):
monitor: Fix handle decoding of status_handle_rsp
monitor: Split LE PA Report decoding into helpers
monitor: Add PAwR HCI commands decoding
monitor: Add PAwR HCI events decoding

Quoted 8 lines
 monitor/analyze.c |   1 +
 monitor/bt.h      | 174 +++++++++++++++++
 monitor/packet.c  | 487 ++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 626 insertions(+), 36 deletions(-)

--
2.47.3

Copilot flagged 2 problems:

https://github.com/bluez/bluez/pull/2842#pullrequestreview-5960745106

Signature · 1 line
Luiz Augusto von Dentz