Merge branch 'feat/ble_log_xor_checksum_v5.5' into 'release/v5.5'

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

See merge request espressif/esp-idf!44540
This commit is contained in:
Island
2025-12-26 10:38:41 +08:00
6 changed files with 84 additions and 3 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
@@ -60,7 +60,9 @@ BLE_LOG_IRAM_ATTR BLE_LOG_STATIC void ble_log_rt_task(void *pvParameters)
if (rt_ts_enabled) {
ble_log_ts_info_t *ts_info = NULL;
ble_log_ts_info_update(&ts_info);
ble_log_write_hex(BLE_LOG_SRC_INTERNAL, (const uint8_t *)ts_info, sizeof(ble_log_ts_info_t));
if (ts_info) {
ble_log_write_hex(BLE_LOG_SRC_INTERNAL, (const uint8_t *)ts_info, sizeof(ble_log_ts_info_t));
}
}
#endif /* CONFIG_BLE_LOG_TS_ENABLED */
@@ -139,6 +141,7 @@ bool ble_log_sync_enable(bool enable)
return false;
}
rt_ts_enabled = enable;
ble_log_ts_reset(enable);
return true;
}
#endif /* CONFIG_BLE_LOG_TS_ENABLED */
@@ -63,6 +63,10 @@ void ble_log_ts_deinit(void)
void ble_log_ts_info_update(ble_log_ts_info_t **info)
{
if (!ts_inited) {
return;
}
BLE_LOG_ENTER_CRITICAL();
ts_info->io_level = !ts_info->io_level;
gpio_set_level(CONFIG_BLE_LOG_SYNC_IO_NUM, ts_info->io_level);
@@ -73,3 +77,16 @@ void ble_log_ts_info_update(ble_log_ts_info_t **info)
*info = ts_info;
}
void ble_log_ts_reset(bool status)
{
if (!ts_inited) {
return;
}
if (!status && !ts_info->io_level) {
gpio_set_level(CONFIG_BLE_LOG_SYNC_IO_NUM, 1);
}
ts_info->io_level = 0;
gpio_set_level(CONFIG_BLE_LOG_SYNC_IO_NUM, 0);
}
@@ -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 */
@@ -53,5 +53,6 @@ typedef struct {
bool ble_log_ts_init(void);
void ble_log_ts_deinit(void);
void ble_log_ts_info_update(ble_log_ts_info_t **ts_info);
void ble_log_ts_reset(bool status);
#endif /* __BLE_LOG_TS_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 {