From 05ff6f2d952ffd55466d15b3dff2cc04c212d0d1 Mon Sep 17 00:00:00 2001 From: Zhou Xiao Date: Wed, 24 Dec 2025 17:05:03 +0800 Subject: [PATCH] feat(ble): added xor checksum for integrity check performance optimization (cherry picked from commit c604afa272f9b4e81835562eb812d86288f040c5) Co-authored-by: Zhou Xiao --- components/bt/common/ble_log/Kconfig.in | 6 ++ .../bt/common/ble_log/src/ble_log_util.c | 56 ++++++++++++++++++- .../src/internal_include/ble_log_util.h | 2 +- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/components/bt/common/ble_log/Kconfig.in b/components/bt/common/ble_log/Kconfig.in index 2bb05ede5a..edf459311d 100644 --- a/components/bt/common/ble_log/Kconfig.in +++ b/components/bt/common/ble_log/Kconfig.in @@ -91,6 +91,12 @@ if BLE_LOG_ENABLED checksum of frame head and payload all together by default, or only calculate the checksum of frame head to minimize performance decrease + config BLE_LOG_XOR_CHECKSUM_ENABLED + bool "Enable XOR checksum for BLE Log payload integrity check" + default y + help + XOR checksum is introduced for integrity check performance optimization. + config BLE_LOG_ENH_STAT_ENABLED bool "Enable enhanced statistics for BLE Log" default n diff --git a/components/bt/common/ble_log/src/ble_log_util.c b/components/bt/common/ble_log/src/ble_log_util.c index 7657ad92dd..c084d6d249 100644 --- a/components/bt/common/ble_log/src/ble_log_util.c +++ b/components/bt/common/ble_log/src/ble_log_util.c @@ -17,7 +17,60 @@ portMUX_TYPE ble_log_spin_lock = portMUX_INITIALIZER_UNLOCKED; #endif /* !UNIT_TEST */ /* INTERNAL INTERFACE */ -BLE_LOG_IRAM_ATTR uint32_t ble_log_fast_checksum(const uint8_t *data, size_t len) +#if CONFIG_BLE_LOG_XOR_CHECKSUM_ENABLED +#include "esp_compiler.h" + +static inline uint32_t ror32(uint32_t word, uint32_t shift) +{ + if (unlikely(shift == 0)) { + return word; + } + return (word >> shift) | (word << ((32 - shift) & 0x1F)); +} + +__attribute__((optimize("-O3"))) +BLE_LOG_IRAM_ATTR +uint32_t ble_log_fast_checksum(const uint8_t *data, size_t len) +{ + /* Validate input length */ + if (unlikely(len == 0)) { + return 0; + } + + /* Step 1: Force 32-bit align read and calculate offset */ + const uint32_t start_offset_shift = ((uintptr_t)data & 0x3) << 3; + const uint32_t *p_aligned = (const uint32_t *)((uintptr_t)data & ~0x3); + const uint32_t *p_end_aligned = (const uint32_t *)((uintptr_t)(data + len) & ~0x3); + + /* Step 2: Handle first word with mask (Little endian) */ + uint32_t checksum = (*p_aligned) & (0xFFFFFFFFU << start_offset_shift); + + /* Step 3: Check if first word is last word */ + const uint32_t end_offset_shift = ((uintptr_t)(data + len) & 0x3) << 3; + const uint32_t end_offset_shift_mask = ~(0xFFFFFFFFU << end_offset_shift); + if (unlikely(p_aligned == p_end_aligned)) { + if (end_offset_shift) { + checksum &= end_offset_shift_mask; + } + } else { + /* Step 4: Handle word in the middle */ + p_aligned++; + while (p_aligned < p_end_aligned) { + checksum ^= *p_aligned++; + } + + /* Step 5: Handle last word with mask (Little endian) */ + if (end_offset_shift) { + checksum ^= (*p_aligned) & end_offset_shift_mask; + } + } + + /* Step 6: Rotate the final result */ + return ror32(checksum, start_offset_shift); +} +#else /* !CONFIG_BLE_LOG_XOR_CHECKSUM_ENABLED */ +BLE_LOG_IRAM_ATTR +uint32_t ble_log_fast_checksum(const uint8_t *data, size_t len) { uint32_t sum = 0; size_t i = 0; @@ -46,3 +99,4 @@ BLE_LOG_IRAM_ATTR uint32_t ble_log_fast_checksum(const uint8_t *data, size_t len return sum; } +#endif /* CONFIG_BLE_LOG_XOR_CHECKSUM_ENABLED */ diff --git a/components/bt/common/ble_log/src/internal_include/ble_log_util.h b/components/bt/common/ble_log/src/internal_include/ble_log_util.h index d0e7c460f0..352ae80cbd 100644 --- a/components/bt/common/ble_log/src/internal_include/ble_log_util.h +++ b/components/bt/common/ble_log/src/internal_include/ble_log_util.h @@ -130,7 +130,7 @@ bool ble_log_cas_acquire(volatile bool *cas_lock); void ble_log_cas_release(volatile bool *cas_lock); #endif /* UNIT_TEST */ -#define BLE_LOG_VERSION (2) +#define BLE_LOG_VERSION (3) /* TYPEDEF */ typedef enum {