Skip to contentSkip to conversation

monitor: Decode Periodic Advertising with Responses

PATCH In review Roland Schober 3 participants 13 messages Nov 20 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

v4 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.

Patches 1 to 7 fix existing decoders which the new ones use or which were 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,
- LE Periodic Advertising Report trusted Data_Length and LE Extended Create Connection trusted the number of PHY parameter blocks, so truncated packets made them decode octets outside the packet,
- the LE Set Periodic Advertising commands printed the advertising handle as a connection handle,
- LE Periodic Advertising Report printed an unavailable TX power as 127 dBm and named the AoD CTE type with 1 us slots AoA,
- LE PAST Received printed the handle twice and decoded the clock accuracy as a BR/EDR one,
- the Sync_Handle of two events and the properties of LE Set Periodic Advertising Parameters were printed without byte order conversion.

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

Patch 9 decodes the five commands and patch 10 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. Every command and event of these traces was also fed to the decoders cut to every length, in a buffer of exactly that size, with AddressSanitizer, which found no read beyond a packet. The output for well formed packets of the existing decoders was compared with the current tree. I have no big-endian host either: the byte order was checked with a btmon whose little-endian conversions swap, on traces with the fields of more than one octet reversed, which has to print the same as the normal btmon on the normal traces.

v4:
- Add patch 7, and convert the Sync_Handle and the properties in the new decoders as well. Copilot found two of these places, the test described above two more.
- The fix of the LE Extended Create Connection PHY decoding no longer adds a size to a void pointer, which CodeQL reported.

v3:
- Add the fixes of the advertising handle, of the TX power and CTE type and of LE PAST Received, and print these fields the same way in the new decoders.
- LE Periodic Advertising Report fix: also report octets beyond Data_Length.
- LE Extended Create Connection fix: report extra PHY parameter blocks, and do not report the remaining PHYs as unknown after a truncated block.
- Commands: reuse the [v1] decoder for LE Set Periodic Advertising Parameters [v2], print 0x00 subevent timings as none, check Response_Data_Length, and match LE Extended Create Connection [v2] with the events which complete it.
- Events: Data_Status 0xff keeps its reserved meaning in the [v1] LE Periodic Advertising Report, LE Enhanced Connection Complete [v2] is matched with the command which started the connection.
- Commands and events: report a subevent data command or a response report with fewer entries than announced.

v2:
- Add the fixes of the LE Periodic Advertising Report Data_Length and of the LE Extended Create Connection PHY blocks. The two problems Copilot found in v1 were in the [v2] decoders, but they came from the [v1] decoders they reuse, so they are fixed there.
- The patch which splits the LE Periodic Advertising Report decoding keeps the new Data_Length check in the shared helper.

Roland Schober (10):
monitor: Fix handle decoding of status_handle_rsp
monitor: Fix data length of LE PA Report decoding
monitor: Fix PHY decoding of LE Ext Create Connection
monitor: Fix advertising handle of LE PA commands
monitor: Fix TX power and CTE Type of LE PA Report
monitor: Fix LE PAST Received decoding
monitor: Fix byte order of LE PA Sync Handle and Properties
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  | 590 +++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 711 insertions(+), 54 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v4 1/10 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

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/series/1216977/

---Test result---

Test Summary:

CheckPatch                    PASS      3.81 seconds
GitLint                       PASS      2.39 seconds
BuildEll                      PASS      16.25 seconds
BluezMake                     PASS      236.60 seconds
MakeCheck                     PASS      0.78 seconds
MakeDistcheck                 PASS      124.05 seconds
CheckValgrind                 PASS      123.83 seconds
CheckSmatch                   WARNING   235.75 seconds
bluezmakeextell               PASS      76.08 seconds
TestFunctional                PASS      1011.03 seconds
IncrementalBuild              PASS      394.74 seconds
ScanBuild                     PASS      879.59 seconds

Details
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/bt.h:4304:52: warning: array of flexible structuresmonitor/bt.h:4292:40: warning: array of flexible structuresmonitor/bt.h:3048:52: warning: array of flexible structuresmonitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.monitor/bt.h:4304:52: warning: array of flexible structuresmonitor/bt.h:4292:40: warning: array of flexible structuresmonitor/bt.h:3048:52: warning: array of flexible structuresmonitor/packet.c: note: in included file:monitor/bt.h:4530:47: warning: array of flexible structuresmonitor/packet.c:2300:26: warning: Variable length array is used.

https://github.com/bluez/bluez/pull/2855

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v4 2/10 monitor: Fix data length of LE PA Report decoding

In reply to Roland Schober

The LE Periodic Advertising Report decoder passes Data_Length from the event to the advertising data decoder without checking it against the size of the event, so a truncated event makes it decode octets which are not part of it, and octets beyond Data_Length are dropped silently:

Quoted 1 line

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

      LE Periodic Advertising Report (0x0f)
        ...
        Data length: 1
        Data[1]:
        00                                               .

Report a length which does not match the event as invalid and dump the remaining octets instead:

Data length: 1
invalid data length

Fixes 5441a5b91a08 ("monitor: Decode LE Periodic Advertising Report Event")
Assisted-by Claude:claude-opus-5-5
---
 monitor/packet.c | 7 +++++++
 1 file changed, 7 insertions(+)
monitor/packet.c +7 -0
index 4ff24092e..0e40300fd 100644
@@ -13957,6 +13957,13 @@ 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);
+
+	if (evt->data_len != size - sizeof(*evt)) {
+		print_text(COLOR_ERROR, "invalid data length");
+		packet_hexdump(evt->data, size - sizeof(*evt));
+		return;
+	}
+
 	print_eir("Data", evt->data, evt->data_len, true);
 }
 
Signature · 1 line
2.47.3
#4 lore raw

PATCH v4 3/10 monitor: Fix PHY decoding of LE Ext Create Connection

In reply to Roland Schober

The LE Extended Create Connection decoder reads one block of connection parameters for each bit set in Initiating_PHYs without checking that the command carries that many blocks, so a truncated command makes it decode octets which are not part of it:

< HCI Command: LE Extended Create Connection (0x08|0x0043) plen 10

        ...
        Initiating PHYs: 0x01
        Entry 0: LE 1M
          Scan interval: 0.000 msec (0x0000)
          Scan window: 0.000 msec (0x0000)
          ...

Check the size of the command before decoding each block, and report octets left over after the last block. The blocks are indexed through a pointer to the block type, not by adding their size to a void pointer:

Initiating PHYs: 0x01
invalid packet size

Fixes e29ef3b85116 ("monitor: Add LE Extended Scan commands decoding")
Assisted-by Claude:claude-opus-5-5
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
monitor/packet.c +23 -3
index 0e40300fd..9814e0e96 100644
@@ -8816,10 +8816,13 @@ static const struct bitfield_data ext_conn_phys_table[] = {
 	{ }
 };
 
-static void print_ext_conn_phys(const void *data, uint8_t flags)
+static void print_ext_conn_phys(const void *data, uint8_t size,
+							uint8_t flags)
 {
+	const struct bt_hci_le_ext_create_conn *entries = data;
 	const struct bt_hci_le_ext_create_conn *entry;
 	uint8_t mask = flags;
+	bool truncated = false;
 	int bits_set = 0;
 	int i;
 
@@ -8827,9 +8830,20 @@ static void print_ext_conn_phys(const void *data, uint8_t flags)
 
 	for (i = 0; ext_conn_phys_table[i].str; i++) {
 		if (flags & (1 << ext_conn_phys_table[i].bit)) {
-			entry = data + bits_set * sizeof(*entry);
 			mask &= ~(1 << ext_conn_phys_table[i].bit);
 
+			if (truncated)
+				continue;
+
+			if ((bits_set + 1) * sizeof(*entry) > size) {
+				print_text(COLOR_ERROR,
+						"  invalid packet size");
+				truncated = true;
+				continue;
+			}
+
+			entry = &entries[bits_set];
+
 			print_field("Entry %d: %s", bits_set,
 						ext_conn_phys_table[i].str);
 			print_slot_625("  Scan interval", entry->scan_interval);
@@ -8852,6 +8866,12 @@ static void print_ext_conn_phys(const void *data, uint8_t flags)
 		}
 	}
 
+	if (!truncated && bits_set * sizeof(*entry) < size) {
+		print_text(COLOR_ERROR, "  invalid packet size");
+		packet_hexdump((const void *) &entries[bits_set],
+					size - bits_set * sizeof(*entry));
+	}
+
 	if (mask)
 		print_text(COLOR_UNKNOWN_ADV_FLAG, "  Unknown scanning PHYs"
 							" (0x%2.2x)", mask);
@@ -8880,7 +8900,7 @@ static void le_ext_create_conn_cmd(uint16_t index, const void *data,
 	print_own_addr_type(cmd->own_addr_type);
 	print_peer_addr_type("Peer address type", cmd->peer_addr_type);
 	print_addr("Peer address", cmd->peer_addr, cmd->peer_addr_type);
-	print_ext_conn_phys(cmd->data, cmd->phys);
+	print_ext_conn_phys(cmd->data, size - sizeof(*cmd), cmd->phys);
 }
 
 static const struct bitfield_data create_sync_cte_type[] = {
Signature · 1 line
2.47.3
#5 lore raw

PATCH v4 4/10 monitor: Fix advertising handle of LE PA commands

In reply to Roland Schober

The decoders of LE Set Periodic Advertising Parameters, Data and Enable print the Advertising_Handle with print_handle(), which looks the value up as a connection handle. With a connection using the same handle, the advertising set is shown with the peer of that connection:

< HCI Command: LE Set Periodic Advertising Parameters (0x08|0x003e) plen 7

        Handle: 0 (LE-ACL) Address: C0:11:22:33:44:55 (Static)

Print it the way the extended advertising commands do:

        Handle: 0x00
Fixes 61563bd7b477 ("monitor: Add LE Set Periodic Advertising commands decoding")
Assisted-by Claude:claude-opus-5-5
---
 monitor/packet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
monitor/packet.c +3 -3
index 9814e0e96..da4e8aa98 100644
@@ -8704,7 +8704,7 @@ static void le_set_pa_params_cmd(uint16_t index, const void *data, uint8_t size)
 {
 	const struct bt_hci_cmd_le_set_pa_params *cmd = data;
 
-	print_handle(cmd->handle);
+	print_field("Handle: 0x%2.2x", cmd->handle);
 	print_slot_125("Min interval", cmd->min_interval);
 	print_slot_125("Max interval", cmd->max_interval);
 	print_pa_properties(cmd->properties);
@@ -8715,7 +8715,7 @@ static void le_set_pa_data_cmd(uint16_t index, const void *data, uint8_t size)
 	const struct bt_hci_cmd_le_set_pa_data *cmd = data;
 	const char *str;
 
-	print_handle(cmd->handle);
+	print_field("Handle: 0x%2.2x", cmd->handle);
 
 	switch (cmd->operation) {
 	case 0x00:
@@ -8745,7 +8745,7 @@ static void le_set_pa_enable_cmd(uint16_t index, const void *data, uint8_t size)
 	const struct bt_hci_cmd_le_set_pa_enable *cmd = data;
 
 	print_enable("Periodic advertising", cmd->enable);
-	print_handle(cmd->handle);
+	print_field("Handle: 0x%2.2x", cmd->handle);
 }
 
 static const struct bitfield_data ext_scan_phys_table[] = {
Signature · 1 line
2.47.3
#6 lore raw

PATCH v4 5/10 monitor: Fix TX power and CTE Type of LE PA Report

In reply to Roland Schober

The LE Periodic Advertising Report decoder prints TX_Power 0x7f, which means that the information is not available, as a power of 127 dBm. It names CTE_Type 0x01 an AoA Constant Tone Extension, but 0x01 is the AoD Constant Tone Extension with 1 us slots, and it pads the value with a space:

        TX power: 127 dbm (0x7f)
        CTE Type: AoA Constant Tone Extension with 1us slots (0x 1)

Decode them as the specification defines them:

        TX power: not available (0x7f)
        CTE Type: AoD Constant Tone Extension with 1us slots (0x01)
Fixes 5441a5b91a08 ("monitor: Decode LE Periodic Advertising Report Event")
Fixes f1354119897a ("monitor: Update periodic sync commands.")
Assisted-by Claude:claude-opus-5-5
---
 monitor/packet.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
monitor/packet.c +6 -3
index da4e8aa98..034a1e82a 100644
@@ -13925,7 +13925,10 @@ static void le_pa_report_evt(struct timeval *tv, uint16_t index,
 	const char *str;
 
 	print_field("Sync handle: %d", evt->handle);
-	print_power_level(evt->tx_power, NULL);
+	if (evt->tx_power == 127)
+		print_field("TX power: not available (0x7f)");
+	else
+		print_power_level(evt->tx_power, NULL);
 	if (evt->rssi == 127)
 		print_field("RSSI: not available (0x%2.2x)",
 						(uint8_t) evt->rssi);
@@ -13941,7 +13944,7 @@ static void le_pa_report_evt(struct timeval *tv, uint16_t index,
 		str = "AoA Constant Tone Extension";
 		break;
 	case 0x01:
-		str = "AoA Constant Tone Extension with 1us slots";
+		str = "AoD Constant Tone Extension with 1us slots";
 		break;
 	case 0x02:
 		str = "AoD Constant Tone Extension with 2us slots";
@@ -13954,7 +13957,7 @@ static void le_pa_report_evt(struct timeval *tv, uint16_t index,
 		break;
 	}
 
-	print_field("CTE Type: %s (0x%2x)", str, evt->cte_type);
+	print_field("CTE Type: %s (0x%2.2x)", str, evt->cte_type);
 
 	switch (evt->data_status) {
 	case 0x00:
Signature · 1 line
2.47.3
#7 lore raw

PATCH v4 6/10 monitor: Fix LE PAST Received decoding

In reply to Roland Schober

The LE Periodic Advertising Sync Transfer Received decoder prints the connection handle twice, doubles the colon of three labels, prints the interval without a unit and decodes the 1 octet Advertiser_Clock_Accuracy as a BR/EDR clock accuracy in units of 0.3125 ms:

        Handle: 3585
        Connection handle: 3585
        ...
        Address type:: Random (0x01)
        Address:: C0:11:22:33:44:55 (Static)
        PHY:: LE 2M (0x02)
        Periodic advertising Interval: 1000.000
        Accuracy: 1.5625 msec (0x0005)

Decode it the way LE Periodic Advertising Sync Established is decoded:

        Handle: 3585 (LE-ACL) Address: C0:11:22:33:44:55 (Static)
        ...
        Address type: Random (0x01)
        Address: C0:11:22:33:44:55 (Static)
        PHY: LE 2M (0x02)
        Periodic advertising interval: 1000.00 msec (0x0320)
        Advertiser clock accuracy: 0x05
Fixes 228c945a93a4 ("monitor: Decode LE Periodic Advertising Sync Transfer Received Event")
Assisted-by Claude:claude-opus-5-5
---
 monitor/packet.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
monitor/packet.c +8 -10
index 034a1e82a..ce05928c1 100644
@@ -14059,17 +14059,15 @@ static void le_past_received_evt(struct timeval *tv, uint16_t index,
 	const struct bt_hci_evt_le_past_recv *evt = data;
 
 	print_status(evt->status);
-	print_field("Handle: %d", evt->handle);
-	print_field("Connection handle: %d", evt->handle);
-	print_field("Service data: 0x%4.4x", evt->service_data);
-	print_field("Sync handle: %d", evt->sync_handle);
+	print_handle(evt->handle);
+	print_field("Service data: 0x%4.4x", le16_to_cpu(evt->service_data));
+	print_field("Sync handle: %d", le16_to_cpu(evt->sync_handle));
 	print_field("SID: 0x%2.2x", evt->sid);
-	print_peer_addr_type("Address type:", evt->addr_type);
-	print_addr("Address:", evt->addr, evt->addr_type);
-	print_le_phy("PHY:", evt->phy);
-	print_field("Periodic advertising Interval: %.3f",
-							1.25 * evt->interval);
-	print_clock_accuracy(evt->clock_accuracy);
+	print_peer_addr_type("Address type", evt->addr_type);
+	print_addr("Address", evt->addr, evt->addr_type);
+	print_le_phy("PHY", evt->phy);
+	print_slot_125("Periodic advertising interval", evt->interval);
+	print_field("Advertiser clock accuracy: 0x%2.2x", evt->clock_accuracy);
 }
 
 static void le_cis_established_evt(struct timeval *tv, uint16_t index,
Signature · 1 line
2.47.3
#8 lore raw

PATCH v4 7/10 monitor: Fix byte order of LE PA Sync Handle and Properties

In reply to Roland Schober

The decoders of the LE Periodic Advertising Sync Established and LE Periodic Advertising Report events print the Sync_Handle, and the decoder of LE Set Periodic Advertising Parameters prints the Periodic_Advertising_Properties, as they are stored in the packet. On a big-endian host Sync_Handle 2 is therefore shown as 512, and the Include TxPower property (0x0040) as unknown property 0x4000.

Convert them from little-endian like the other fields.

Fixes 9285a4e33882 ("monitor: Decode LE Periodic Advertising Sync Established Event")
Fixes 5441a5b91a08 ("monitor: Decode LE Periodic Advertising Report Event")
Fixes 61563bd7b477 ("monitor: Add LE Set Periodic Advertising commands decoding")
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
monitor/packet.c +3 -3
index ce05928c1..4b7a82ac0 100644
@@ -8707,7 +8707,7 @@ static void le_set_pa_params_cmd(uint16_t index, const void *data, uint8_t size)
 	print_field("Handle: 0x%2.2x", cmd->handle);
 	print_slot_125("Min interval", cmd->min_interval);
 	print_slot_125("Max interval", cmd->max_interval);
-	print_pa_properties(cmd->properties);
+	print_pa_properties(le16_to_cpu(cmd->properties));
 }
 
 static void le_set_pa_data_cmd(uint16_t index, const void *data, uint8_t size)
@@ -13904,7 +13904,7 @@ static void le_pa_sync_evt(struct timeval *tv, uint16_t index,
 	const struct bt_hci_evt_le_per_sync_established *evt = data;
 
 	print_status(evt->status);
-	print_field("Sync handle: %d", evt->handle);
+	print_field("Sync handle: %d", le16_to_cpu(evt->handle));
 	if (evt->sid > 0x0f)
 		print_field("Advertising SID: Reserved (0x%2.2x)", evt->sid);
 	else
@@ -13924,7 +13924,7 @@ static void le_pa_report_evt(struct timeval *tv, uint16_t index,
 	const char *color_on;
 	const char *str;
 
-	print_field("Sync handle: %d", evt->handle);
+	print_field("Sync handle: %d", le16_to_cpu(evt->handle));
 	if (evt->tx_power == 127)
 		print_field("TX power: not available (0x7f)");
 	else
Signature · 1 line
2.47.3
#9 lore raw

PATCH v4 8/10 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 TX power, the RSSI and the CTE type names into functions 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-opus-5-5
Assisted-by Claude:claude-fable-5-2
---
 monitor/packet.c | 100 +++++++++++++++++++++++++++++------------------
 1 file changed, 62 insertions(+), 38 deletions(-)
monitor/packet.c +62 -38
index 4b7a82ac0..22ec1f131 100644
@@ -13917,49 +13917,62 @@ 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", le16_to_cpu(evt->handle));
-	if (evt->tx_power == 127)
-		print_field("TX power: not available (0x7f)");
-	else
-		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 = "AoD Constant Tone Extension with 1us slots";
-		break;
+		return "AoD 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";
 	}
+}
 
-	print_field("CTE Type: %s (0x%2.2x)", str, evt->cte_type);
+static void print_pa_rssi(const char *prefix, int8_t rssi)
+{
+	if (rssi == 127)
+		print_field("%sRSSI: not available (0x%2.2x)", prefix,
+							(uint8_t) rssi);
+	else if (rssi >= -127 && rssi <= 20)
+		print_field("%sRSSI: %d dBm (0x%2.2x)", prefix, rssi,
+							(uint8_t) rssi);
+	else
+		print_field("%sRSSI: reserved (0x%2.2x)", prefix,
+							(uint8_t) rssi);
+}
 
-	switch (evt->data_status) {
+static void print_pa_tx_power(const char *prefix, int8_t tx_power)
+{
+	if (tx_power == 127)
+		print_field("%sTX power: not available (0x7f)", prefix);
+	else
+		print_field("%sTX power: %d dbm (0x%2.2x)", prefix, tx_power,
+							(uint8_t) tx_power);
+}
+
+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_pa_tx_power("", tx_power);
+	print_pa_rssi("", rssi);
+
+	print_field("CTE Type: %s (0x%2.2x)", pa_cte_type_str(cte_type),
+								cte_type);
+}
+
+static void print_pa_report_data(uint8_t data_status, uint8_t data_len,
+					const uint8_t *data, uint8_t size)
+{
+	const char *color_on;
+	const char *str;
+
+	switch (data_status) {
 	case 0x00:
 		str = "Complete";
 		color_on = COLOR_GREEN;
@@ -13979,15 +13992,26 @@ 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_field("Data length: %u", data_len);
 
-	if (evt->data_len != size - sizeof(*evt)) {
+	if (data_len != size) {
 		print_text(COLOR_ERROR, "invalid data length");
-		packet_hexdump(evt->data, size - sizeof(*evt));
+		packet_hexdump(data, size);
 		return;
 	}
 
-	print_eir("Data", evt->data, evt->data_len, true);
+	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(le16_to_cpu(evt->handle), evt->tx_power,
+						evt->rssi, evt->cte_type);
+	print_pa_report_data(evt->data_status, evt->data_len, evt->data,
+							size - sizeof(*evt));
 }
 
 static void le_pa_sync_lost_evt(struct timeval *tv, uint16_t index,
Signature · 1 line
2.47.3
#10 lore raw

PATCH v4 9/10 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]

LE Extended Create Connection [v2] is matched with the events which complete it like [v1].

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.. (0x08|0x0082) plen 15
Handle: 0x00
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.. (0x08|0x0086) plen 12

        Handle: 0x00
        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 | 247 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 326 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 +242 -5

271 lines not on this page · raw message

Signature · 1 line
2.47.3
#11 lore raw

PATCH v4 10/10 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, and is matched with the LE Create Connection command which started the connection.

Quoted 1 line

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

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

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

      LE Periodic Advertising Response Report (0x28)
        Handle: 0x00
        Subevent: 2 (0x02)
        TX status: AUX_SYNC_SUBEVENT_IND was transmitted (0x00)
        Num responses: 2
        Entry 0
          TX power: not available (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: not available (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-opus-5-5
Assisted-by Claude:claude-fable-5-2
---
 monitor/analyze.c |   1 +
 monitor/bt.h      |  90 +++++++++++++++++++++
 monitor/packet.c  | 197 +++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 286 insertions(+), 2 deletions(-)
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 +195 -2

245 lines not on this page · raw message

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

Hi Roland,

Quoted 94 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.

Patches 1 to 7 fix existing decoders which the new ones use or which were 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,
- LE Periodic Advertising Report trusted Data_Length and LE Extended Create Connection trusted the number of PHY parameter blocks, so truncated packets made them decode octets outside the packet,
- the LE Set Periodic Advertising commands printed the advertising handle as a connection handle,
- LE Periodic Advertising Report printed an unavailable TX power as 127 dBm and named the AoD CTE type with 1 us slots AoA,
- LE PAST Received printed the handle twice and decoded the clock accuracy as a BR/EDR one,
- the Sync_Handle of two events and the properties of LE Set Periodic Advertising Parameters were printed without byte order conversion.

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

Patch 9 decodes the five commands and patch 10 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. Every command and event of these traces was also fed to the decoders cut to every length, in a buffer of exactly that size, with AddressSanitizer, which found no read beyond a packet. The output for well formed packets of the existing decoders was compared with the current tree. I have no big-endian host either: the byte order was checked with a btmon whose little-endian conversions swap, on traces with the fields of more than one octet reversed, which has to print the same as the normal btmon on the normal traces.

v4:
- Add patch 7, and convert the Sync_Handle and the properties in the new decoders as well. Copilot found two of these places, the test described above two more.
- The fix of the LE Extended Create Connection PHY decoding no longer adds a size to a void pointer, which CodeQL reported.

v3:
- Add the fixes of the advertising handle, of the TX power and CTE type and of LE PAST Received, and print these fields the same way in the new decoders.
- LE Periodic Advertising Report fix: also report octets beyond Data_Length.
- LE Extended Create Connection fix: report extra PHY parameter blocks, and do not report the remaining PHYs as unknown after a truncated block.
- Commands: reuse the [v1] decoder for LE Set Periodic Advertising Parameters [v2], print 0x00 subevent timings as none, check Response_Data_Length, and match LE Extended Create Connection [v2] with the events which complete it.
- Events: Data_Status 0xff keeps its reserved meaning in the [v1] LE Periodic Advertising Report, LE Enhanced Connection Complete [v2] is matched with the command which started the connection.
- Commands and events: report a subevent data command or a response report with fewer entries than announced.

v2:
- Add the fixes of the LE Periodic Advertising Report Data_Length and of the LE Extended Create Connection PHY blocks. The two problems Copilot found in v1 were in the [v2] decoders, but they came from the [v1] decoders they reuse, so they are fixed there.
- The patch which splits the LE Periodic Advertising Report decoding keeps the new Data_Length check in the shared helper.

Roland Schober (10):
monitor: Fix handle decoding of status_handle_rsp
monitor: Fix data length of LE PA Report decoding
monitor: Fix PHY decoding of LE Ext Create Connection
monitor: Fix advertising handle of LE PA commands
monitor: Fix TX power and CTE Type of LE PA Report
monitor: Fix LE PAST Received decoding
monitor: Fix byte order of LE PA Sync Handle and Properties
monitor: Split LE PA Report decoding into helpers
monitor: Add PAwR HCI commands decoding
monitor: Add PAwR HCI events decoding

Quoted 7 lines
 monitor/analyze.c |   1 +
 monitor/bt.h      | 174 ++++++++++++++
 monitor/packet.c  | 590 +++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 711 insertions(+), 54 deletions(-)

--
2.47.3

Copilot flagged more problem:

https://github.com/bluez/bluez/pull/2855#pullrequestreview-5995150018

Signature · 1 line
Luiz Augusto von Dentz