From a1cbd923288d0fb1a80f284d40e7f4164c61e07e Mon Sep 17 00:00:00 2001 From: Zhou Xiao Date: Wed, 2 Sep 2026 11:40:18 +0800 Subject: [PATCH] refactor(ble_log): preserve full HCI type byte in ble_log_write_hci ble_log_write_hci encoded the direction in bit 7 of the caller's type byte and then cleared that bit unconditionally after the write, leaving the caller's buffer mutated. Save the complete type byte, set bit 7 only around the synchronous copy, and restore it afterwards; the direction argument is now treated as a boolean. HCI H4 type bytes (0x01..0x04) always have bit 7 clear, so no legal input observes a behavior change. --- .../bt/common/ble_log/include/ble_log.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/components/bt/common/ble_log/include/ble_log.h b/components/bt/common/ble_log/include/ble_log.h index 0a358a34f34..7668faf4d48 100644 --- a/components/bt/common/ble_log/include/ble_log.h +++ b/components/bt/common/ble_log/include/ble_log.h @@ -45,14 +45,17 @@ typedef enum { #define BLE_LOG_HCI_DOWNSTREAM 0 #define BLE_LOG_HCI_UPSTREAM 1 -/* HCI Log Write Macro - * Encodes direction in MSB of data[0] (HCI type byte) before writing. - * Safe because ble_log_write_hex -> ble_log_lbm_write_trans does synchronous memcpy. - * Parser reads MSB to determine direction; old firmware with MSB=0 defaults to "sent". */ -#define ble_log_write_hci(direction, data, len) do { \ - (data)[0] |= ((direction) << 7); \ - ble_log_write_hex(BLE_LOG_SRC_HCI, (data), (len)); \ - (data)[0] &= 0x7F; \ +/* Encodes HCI direction in payload byte 0 bit 7 for the synchronous copy, + * then restores the complete original HCI type byte. The caller guarantees a + * non-NULL buffer with len > 0. */ +#define ble_log_write_hci(direction, data, len) do { \ + uint8_t *const ble_log_hci_data__ = (data); \ + const uint8_t ble_log_hci_type__ = ble_log_hci_data__[0]; \ + ble_log_hci_data__[0] = (ble_log_hci_type__ & 0x7fU) | \ + ((direction) ? 0x80U : 0U); \ + (void)ble_log_write_hex(BLE_LOG_SRC_HCI, ble_log_hci_data__, \ + (len)); \ + ble_log_hci_data__[0] = ble_log_hci_type__; \ } while (0) /* INTERFACE */