feat(ble): added xor checksum for integrity check performance optimization

(cherry picked from commit c604afa272f9b4e81835562eb812d86288f040c5)

Co-authored-by: Zhou Xiao <zhouxiao@espressif.com>
This commit is contained in:
Zhou Xiao
2025-12-24 17:05:03 +08:00
parent ce76ba349c
commit 05ff6f2d95
3 changed files with 62 additions and 2 deletions
+6
View File
@@ -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
@@ -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 */
@@ -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 {