Skip to content

Commit 5be9ac3

Browse files
author
Zhou Xiao
committed
feat(ble): refactored loss report module and represent frame using struct
(cherry picked from commit 9800d71) Co-authored-by: Zhou Xiao <zhouxiao@espressif.com>
1 parent e100fc7 commit 5be9ac3

File tree

1 file changed

+63
-41
lines changed

1 file changed

+63
-41
lines changed

components/bt/common/ble_log/ble_log_spi_out.c

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,30 @@ typedef struct {
6767
typedef struct {
6868
spi_out_trans_cb_t *trans_cb[2];
6969
uint8_t trans_cb_idx;
70-
uint8_t frame_cnt;
71-
uint32_t bytes_loss_cnt;
72-
uint8_t trans_loss_cnt;
70+
uint8_t frame_sn;
71+
uint16_t lost_frame_cnt;
72+
uint32_t lost_bytes_cnt;
73+
uint8_t type;
7374
} spi_out_log_cb_t;
7475

76+
typedef struct {
77+
uint16_t length;
78+
uint8_t source;
79+
uint8_t frame_sn;
80+
} __attribute__((packed)) frame_head_t;
81+
82+
typedef struct {
83+
uint8_t type;
84+
uint16_t lost_frame_cnt;
85+
uint32_t lost_bytes_cnt;
86+
} __attribute__((packed)) loss_payload_t;
87+
88+
typedef struct {
89+
uint8_t io_level;
90+
uint32_t lc_ts;
91+
uint32_t esp_ts;
92+
} __attribute__((packed)) sync_payload_t;
93+
7594
// Private enums
7695
enum {
7796
TRANS_CB_FLAG_AVAILABLE = 0,
@@ -145,15 +164,15 @@ static void spi_out_deinit_trans(spi_out_trans_cb_t **trans_cb);
145164
static void spi_out_tx_done_cb(spi_transaction_t *ret_trans);
146165
static inline int spi_out_append_trans(spi_out_trans_cb_t *trans_cb);
147166

148-
static int spi_out_log_cb_init(spi_out_log_cb_t **log_cb, uint16_t buf_size);
167+
static int spi_out_log_cb_init(spi_out_log_cb_t **log_cb, uint16_t buf_size, uint8_t type);
149168
static void spi_out_log_cb_deinit(spi_out_log_cb_t **log_cb);
150169
static inline bool spi_out_log_cb_check_trans(spi_out_log_cb_t *log_cb, uint16_t len, bool *need_append);
151170
static inline void spi_out_log_cb_append_trans(spi_out_log_cb_t *log_cb);
152171
static inline void spi_out_log_cb_flush_trans(spi_out_log_cb_t *log_cb);
153172
static bool spi_out_log_cb_write(spi_out_log_cb_t *log_cb, const uint8_t *addr, uint16_t len,
154173
const uint8_t *addr_append, uint16_t len_append, uint8_t source,
155174
bool with_checksum);
156-
static void spi_out_log_cb_write_packet_loss(spi_out_log_cb_t *log_cb, uint8_t flag);
175+
static void spi_out_log_cb_write_loss(spi_out_log_cb_t *log_cb);
157176
static void spi_out_log_cb_dump(spi_out_log_cb_t *log_cb);
158177
static void spi_out_log_flush(void);
159178

@@ -270,7 +289,7 @@ IRAM_ATTR static inline int spi_out_append_trans(spi_out_trans_cb_t *trans_cb)
270289
}
271290
}
272291

273-
static int spi_out_log_cb_init(spi_out_log_cb_t **log_cb, uint16_t buf_size)
292+
static int spi_out_log_cb_init(spi_out_log_cb_t **log_cb, uint16_t buf_size, uint8_t type)
274293
{
275294
// Initialize log control block
276295
*log_cb = (spi_out_log_cb_t *)malloc(sizeof(spi_out_log_cb_t));
@@ -290,6 +309,8 @@ static int spi_out_log_cb_init(spi_out_log_cb_t **log_cb, uint16_t buf_size)
290309
spi_out_log_cb_deinit(log_cb);
291310
return -1;
292311
}
312+
313+
(*log_cb)->type = type;
293314
return 0;
294315
}
295316

@@ -330,8 +351,9 @@ IRAM_ATTR static inline bool spi_out_log_cb_check_trans(spi_out_log_cb_t *log_cb
330351
log_cb->trans_cb_idx = !(log_cb->trans_cb_idx);
331352
}
332353
failed:
333-
log_cb->bytes_loss_cnt += len + SPI_OUT_FRAME_OVERHEAD;
334-
log_cb->frame_cnt++;
354+
log_cb->lost_bytes_cnt += frame_len;
355+
log_cb->lost_frame_cnt++;
356+
log_cb->frame_sn++;
335357
return false;
336358
}
337359

@@ -343,9 +365,7 @@ IRAM_ATTR static inline void spi_out_log_cb_append_trans(spi_out_log_cb_t *log_c
343365
for (uint8_t i = 0; i < 2; i++) {
344366
trans_cb = log_cb->trans_cb[idx];
345367
if (trans_cb->flag == TRANS_CB_FLAG_NEED_QUEUE) {
346-
if (spi_out_append_trans(trans_cb) != 0) {
347-
log_cb->trans_loss_cnt++;
348-
}
368+
spi_out_append_trans(trans_cb);
349369
}
350370
idx = !idx;
351371
}
@@ -371,7 +391,11 @@ IRAM_ATTR static bool spi_out_log_cb_write(spi_out_log_cb_t *log_cb, const uint8
371391

372392
uint8_t *buf = (uint8_t *)trans_cb->trans.tx_buffer + trans_cb->length;
373393
uint16_t total_length = len + len_append;
374-
const uint8_t head[4] = {total_length & 0xFF, (total_length >> 8) & 0xFF, source, log_cb->frame_cnt};
394+
frame_head_t head = {
395+
.length = total_length,
396+
.source = source,
397+
.frame_sn = log_cb->frame_sn,
398+
};
375399
uint32_t checksum = 0;
376400
if (with_checksum) {
377401
for (int i = 0; i < len; i++) {
@@ -382,38 +406,39 @@ IRAM_ATTR static bool spi_out_log_cb_write(spi_out_log_cb_t *log_cb, const uint8
382406
}
383407
}
384408

385-
memcpy(buf, head, SPI_OUT_FRAME_HEAD_LEN);
409+
memcpy(buf, (const uint8_t *)&head, SPI_OUT_FRAME_HEAD_LEN);
386410
memcpy(buf + SPI_OUT_FRAME_HEAD_LEN, addr, len);
387411
if (len_append) {
388412
memcpy(buf + SPI_OUT_FRAME_HEAD_LEN + len, addr_append, len_append);
389413
}
390414
memcpy(buf + SPI_OUT_FRAME_HEAD_LEN + total_length, &checksum, SPI_OUT_FRAME_TAIL_LEN);
391415

392416
trans_cb->length += total_length + SPI_OUT_FRAME_OVERHEAD;
393-
log_cb->frame_cnt++;
417+
log_cb->frame_sn++;
394418
if ((trans_cb->buf_size - trans_cb->length) <= SPI_OUT_FRAME_OVERHEAD) {
395419
trans_cb->flag = TRANS_CB_FLAG_NEED_QUEUE;
396420
return true;
397421
}
398422
return false;
399423
}
400424

401-
IRAM_ATTR static void spi_out_log_cb_write_packet_loss(spi_out_log_cb_t *log_cb, uint8_t flag)
425+
IRAM_ATTR static void spi_out_log_cb_write_loss(spi_out_log_cb_t *log_cb)
402426
{
403-
if (!log_cb->bytes_loss_cnt && !log_cb->trans_loss_cnt) {
427+
if (!log_cb->lost_bytes_cnt || !log_cb->lost_frame_cnt) {
404428
return;
405429
}
406430
bool need_append;
407-
if (spi_out_log_cb_check_trans(log_cb, SPI_OUT_PACKET_LOSS_FRAME_SIZE, &need_append)) {
408-
uint8_t packet_loss_frame[SPI_OUT_PACKET_LOSS_FRAME_SIZE];
409-
packet_loss_frame[0] = flag;
410-
memcpy(packet_loss_frame + 1, (uint8_t *)&log_cb->bytes_loss_cnt, 4);
411-
packet_loss_frame[5] = log_cb->trans_loss_cnt;
412-
spi_out_log_cb_write(log_cb, packet_loss_frame, SPI_OUT_PACKET_LOSS_FRAME_SIZE,
431+
if (spi_out_log_cb_check_trans(log_cb, sizeof(loss_payload_t), &need_append)) {
432+
loss_payload_t payload = {
433+
.type = log_cb->type,
434+
.lost_frame_cnt = log_cb->lost_frame_cnt,
435+
.lost_bytes_cnt = log_cb->lost_bytes_cnt,
436+
};
437+
spi_out_log_cb_write(log_cb, (const uint8_t *)&payload, sizeof(loss_payload_t),
413438
NULL, 0, BLE_LOG_SPI_OUT_SOURCE_LOSS, true);
414439

415-
log_cb->bytes_loss_cnt = 0;
416-
log_cb->trans_loss_cnt = 0;
440+
log_cb->lost_frame_cnt = 0;
441+
log_cb->lost_bytes_cnt = 0;
417442
}
418443
}
419444

@@ -482,7 +507,7 @@ static int spi_out_ul_log_init(void)
482507
}
483508

484509
// Initialize log control block
485-
if (spi_out_log_cb_init(&ul_log_cb, SPI_OUT_UL_TASK_BUF_SIZE) != 0) {
510+
if (spi_out_log_cb_init(&ul_log_cb, SPI_OUT_UL_TASK_BUF_SIZE, LOG_CB_TYPE_UL) != 0) {
486511
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for upper layer task log!");
487512
goto log_cb_init_failed;
488513
}
@@ -541,7 +566,7 @@ static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t l
541566
if (need_append) {
542567
spi_out_log_cb_append_trans(ul_log_cb);
543568
}
544-
spi_out_log_cb_write_packet_loss(ul_log_cb, LOG_CB_TYPE_UL);
569+
spi_out_log_cb_write_loss(ul_log_cb);
545570
}
546571

547572
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args)
@@ -569,15 +594,15 @@ static int spi_out_ll_log_init(void)
569594
}
570595

571596
// Initialize log control blocks for controller task & ISR logs
572-
if (spi_out_log_cb_init(&ll_task_log_cb, SPI_OUT_LL_TASK_BUF_SIZE) != 0) {
597+
if (spi_out_log_cb_init(&ll_task_log_cb, SPI_OUT_LL_TASK_BUF_SIZE, LOG_CB_TYPE_LL_TASK) != 0) {
573598
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for controller task!");
574599
goto task_log_cb_init_failed;
575600
}
576-
if (spi_out_log_cb_init(&ll_isr_log_cb, SPI_OUT_LL_ISR_BUF_SIZE) != 0) {
601+
if (spi_out_log_cb_init(&ll_isr_log_cb, SPI_OUT_LL_ISR_BUF_SIZE, LOG_CB_TYPE_LL_ISR) != 0) {
577602
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for controller ISR!");
578603
goto isr_log_cb_init_failed;
579604
}
580-
if (spi_out_log_cb_init(&ll_hci_log_cb, SPI_OUT_LL_HCI_BUF_SIZE) != 0) {
605+
if (spi_out_log_cb_init(&ll_hci_log_cb, SPI_OUT_LL_HCI_BUF_SIZE, LOG_CB_TYPE_LL_HCI) != 0) {
581606
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for controller ISR!");
582607
goto hci_log_cb_init_failed;
583608
}
@@ -738,11 +763,12 @@ static void esp_timer_cb_ts_sync(void)
738763
// Exit critical
739764

740765
// Write timestamp sync log
741-
uint8_t sync_frame[9];
742-
sync_frame[0] = ((uint8_t)sync_io_level & 0xFF);
743-
memcpy(sync_frame + 1, &lc_ts, sizeof(lc_ts));
744-
memcpy(sync_frame + 5, &esp_ts, sizeof(esp_ts));
745-
ble_log_spi_out_write(BLE_LOG_SPI_OUT_SOURCE_SYNC, sync_frame, 9);
766+
sync_payload_t payload = {
767+
.io_level = sync_io_level,
768+
.lc_ts = lc_ts,
769+
.esp_ts = esp_ts,
770+
};
771+
ble_log_spi_out_write(BLE_LOG_SPI_OUT_SOURCE_SYNC, (const uint8_t *)&payload, sizeof(sync_payload_t));
746772
}
747773
#endif // SPI_OUT_TS_SYNC_ENABLED
748774

@@ -754,7 +780,7 @@ static int spi_out_le_audio_log_init(void)
754780
}
755781

756782
// Initialize log control blocks for controller task & ISR logs
757-
if (spi_out_log_cb_init(&le_audio_log_cb, SPI_OUT_LE_AUDIO_BUF_SIZE) != 0) {
783+
if (spi_out_log_cb_init(&le_audio_log_cb, SPI_OUT_LE_AUDIO_BUF_SIZE, LOG_CB_TYPE_LE_AUDIO) != 0) {
758784
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for LE audio!");
759785
return -1;
760786
}
@@ -968,21 +994,17 @@ IRAM_ATTR void ble_log_spi_out_ll_write(uint32_t len, const uint8_t *addr, uint3
968994
}
969995

970996
bool in_isr = false;
971-
uint8_t log_cb_type;
972997
uint8_t source;
973998
spi_out_log_cb_t *log_cb;
974999
if (flag & BIT(LL_LOG_FLAG_ISR)) {
9751000
log_cb = ll_isr_log_cb;
976-
log_cb_type = LOG_CB_TYPE_LL_ISR;
9771001
source = BLE_LOG_SPI_OUT_SOURCE_ESP_ISR;
9781002
in_isr = true;
9791003
} else if (flag & BIT(LL_LOG_FLAG_HCI)) {
9801004
log_cb = ll_hci_log_cb;
981-
log_cb_type = LOG_CB_TYPE_LL_HCI;
9821005
source = BLE_LOG_SPI_OUT_SOURCE_ESP;
9831006
} else {
9841007
log_cb = ll_task_log_cb;
985-
log_cb_type = LOG_CB_TYPE_LL_TASK;
9861008
source = BLE_LOG_SPI_OUT_SOURCE_ESP;
9871009
}
9881010

@@ -999,7 +1021,7 @@ IRAM_ATTR void ble_log_spi_out_ll_write(uint32_t len, const uint8_t *addr, uint3
9991021
spi_out_log_cb_append_trans(log_cb);
10001022
}
10011023
}
1002-
spi_out_log_cb_write_packet_loss(log_cb, log_cb_type);
1024+
spi_out_log_cb_write_loss(log_cb);
10031025

10041026
// Update controller status
10051027
if (!in_isr) {
@@ -1156,7 +1178,7 @@ IRAM_ATTR void ble_log_spi_out_le_audio_write(const uint8_t *addr, uint16_t len)
11561178
if (need_append) {
11571179
spi_out_log_cb_append_trans(le_audio_log_cb);
11581180
}
1159-
spi_out_log_cb_write_packet_loss(le_audio_log_cb, LOG_CB_TYPE_LE_AUDIO);
1181+
spi_out_log_cb_write_loss(le_audio_log_cb);
11601182
return;
11611183
}
11621184
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_LE_AUDIO_ENABLED

0 commit comments

Comments
 (0)