Skip to content

Commit d353bf2

Browse files
author
Zhou Xiao
committed
change(ble): replaced ul log codes with template
1 parent feaee0a commit d353bf2

File tree

1 file changed

+74
-111
lines changed

1 file changed

+74
-111
lines changed

components/bt/common/ble_log/ble_log_spi_out.c

Lines changed: 74 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define SPI_OUT_FRAME_OVERHEAD (8)
3737
#define SPI_OUT_PACKET_LOSS_FRAME_SIZE (6)
3838
#define SPI_OUT_TRANS_ITVL_MIN_US (30)
39-
#define SPI_OUT_UL_LOG_STR_BUF_SIZE (100)
39+
#define SPI_OUT_LOG_STR_BUF_SIZE (100)
4040
#define SPI_OUT_MALLOC(size) heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
4141

4242
#if SPI_OUT_TS_SYNC_ENABLED
@@ -137,11 +137,6 @@ static spi_device_handle_t spi_handle = NULL;
137137
static uint32_t last_tx_done_ts = 0;
138138
static uint32_t last_tx_done_os_ts = 0;
139139

140-
static bool ul_log_inited = false;
141-
static SemaphoreHandle_t ul_log_mutex = NULL;
142-
static spi_out_log_cb_t *ul_log_cb = NULL;
143-
static uint8_t *ul_log_str_buf = NULL;
144-
145140
#if SPI_OUT_LL_ENABLED
146141
static bool ll_log_inited = false;
147142
static spi_out_log_cb_t *ll_task_log_cb = NULL;
@@ -185,10 +180,10 @@ static void spi_out_log_cb_write_loss(spi_out_log_cb_t *log_cb);
185180
static void spi_out_log_cb_dump(spi_out_log_cb_t *log_cb);
186181
static void spi_out_log_flush(void);
187182

188-
static int spi_out_ul_log_init(void);
189-
static void spi_out_ul_log_deinit(void);
190-
static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts);
191-
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args, int offset);
183+
static inline spi_out_log_cb_t *spi_out_get_log_cb(uint8_t source);
184+
static inline uint8_t *spi_out_get_str_buf(uint8_t source);
185+
static void spi_out_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts);
186+
static bool spi_out_log_printf(uint8_t source, const char *format, va_list args, int offset);
192187

193188
#if SPI_OUT_LL_ENABLED
194189
static int spi_out_ll_log_init(void);
@@ -313,6 +308,8 @@ static void esp_timer_cb_ts_sync(void);
313308
) \
314309
} \
315310

311+
DECLARE_LOG_MODULE(ul, LOG_CB_TYPE_UL, SPI_OUT_UL_TASK_BUF_SIZE, 1, 1)
312+
316313
#if SPI_OUT_LE_AUDIO_ENABLED
317314
DECLARE_LOG_MODULE(le_audio, LOG_CB_TYPE_LE_AUDIO, SPI_OUT_LE_AUDIO_BUF_SIZE, 0, 0)
318315
#endif // SPI_OUT_LE_AUDIO_ENABLED
@@ -572,11 +569,7 @@ static void spi_out_log_cb_dump(spi_out_log_cb_t *log_cb)
572569

573570
static void spi_out_log_flush(void)
574571
{
575-
// Flush ul log
576-
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
577-
spi_out_log_cb_flush_trans(ul_log_cb);
578-
spi_out_log_cb_append_trans(ul_log_cb);
579-
xSemaphoreGive(ul_log_mutex);
572+
LOG_MODULE_FLUSH(ul)();
580573

581574
#if SPI_OUT_LL_ENABLED
582575
if (esp_bt_controller_get_status() >= ESP_BT_CONTROLLER_STATUS_INITED) {
@@ -599,105 +592,63 @@ static void esp_timer_cb_log_flush(void)
599592
}
600593
#endif // SPI_OUT_FLUSH_TIMER_ENABLED
601594

602-
static int spi_out_ul_log_init(void)
595+
static inline spi_out_log_cb_t *spi_out_get_log_cb(uint8_t source)
603596
{
604-
if (ul_log_inited) {
605-
return 0;
606-
}
607-
608-
// Initialize mutex
609-
ul_log_mutex = xSemaphoreCreateMutex();
610-
if (!ul_log_mutex) {
611-
ESP_LOGE(BLE_LOG_TAG, "Failed to create mutex for upper layer task log!");
612-
goto mutex_init_failed;
613-
}
614-
615-
// Initialize string buffer
616-
ul_log_str_buf = (uint8_t *)SPI_OUT_MALLOC(SPI_OUT_UL_LOG_STR_BUF_SIZE);
617-
if (!ul_log_str_buf) {
618-
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize string buffer for upper layer task log!");
619-
goto str_buf_init_failed;
620-
}
621-
622-
// Initialize log control block
623-
if (spi_out_log_cb_init(&ul_log_cb, SPI_OUT_UL_TASK_BUF_SIZE, LOG_CB_TYPE_UL) != 0) {
624-
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for upper layer task log!");
625-
goto log_cb_init_failed;
626-
}
627-
628-
// Initialization done
629-
ESP_LOGI(BLE_LOG_TAG, "Succeeded to initialize upper layer task log!");
630-
ul_log_inited = true;
631-
return 0;
632-
633-
log_cb_init_failed:
634-
if (ul_log_str_buf) {
635-
free(ul_log_str_buf);
636-
ul_log_str_buf = NULL;
597+
spi_out_log_cb_t *log_cb;
598+
switch (source) {
599+
default:
600+
log_cb = LOG_MODULE_CB(ul);
637601
}
638-
str_buf_init_failed:
639-
vSemaphoreDelete(ul_log_mutex);
640-
mutex_init_failed:
641-
return -1;
602+
return log_cb;
642603
}
643604

644-
static void spi_out_ul_log_deinit(void)
605+
static inline uint8_t *spi_out_get_str_buf(uint8_t source)
645606
{
646-
if (!ul_log_inited) {
647-
return;
648-
}
649-
ul_log_inited = false;
650-
651-
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
652-
if (ul_log_str_buf) {
653-
free(ul_log_str_buf);
654-
ul_log_str_buf = NULL;
607+
uint8_t *str_buf;
608+
switch (source) {
609+
default:
610+
str_buf = LOG_MODULE_STR_BUF(ul);
655611
}
656-
spi_out_log_cb_deinit(&ul_log_cb);
657-
xSemaphoreGive(ul_log_mutex);
658-
659-
vSemaphoreDelete(ul_log_mutex);
660-
ul_log_mutex = NULL;
661-
662-
ESP_LOGI(BLE_LOG_TAG, "Succeeded to deinitialize upper layer log!");
663-
return;
612+
return str_buf;
664613
}
665614

666-
static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts)
615+
static void spi_out_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts)
667616
{
668617
uint16_t total_len = with_ts? (len + sizeof(uint32_t)): len;
669618
bool need_append;
670-
if (spi_out_log_cb_check_trans(ul_log_cb, total_len, &need_append)) {
619+
spi_out_log_cb_t *log_cb = spi_out_get_log_cb(source);
620+
if (spi_out_log_cb_check_trans(log_cb, total_len, &need_append)) {
671621
if (with_ts) {
672622
uint32_t os_ts = pdTICKS_TO_MS(xTaskGetTickCount());
673-
need_append |= spi_out_log_cb_write(ul_log_cb, (const uint8_t *)&os_ts,
623+
need_append |= spi_out_log_cb_write(log_cb, (const uint8_t *)&os_ts,
674624
sizeof(uint32_t), addr, len, source, true);
675625
} else {
676-
need_append |= spi_out_log_cb_write(ul_log_cb, addr, len, NULL, 0, source, true);
626+
need_append |= spi_out_log_cb_write(log_cb, addr, len, NULL, 0, source, true);
677627
}
678628
}
679629
if (need_append) {
680-
spi_out_log_cb_append_trans(ul_log_cb);
630+
spi_out_log_cb_append_trans(log_cb);
681631
}
682-
spi_out_log_cb_write_loss(ul_log_cb);
632+
spi_out_log_cb_write_loss(log_cb);
683633
}
684634

685-
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args, int offset)
635+
static bool spi_out_log_printf(uint8_t source, const char *format, va_list args, int offset)
686636
{
687-
int len = vsnprintf((char *)(ul_log_str_buf + offset),
688-
SPI_OUT_UL_LOG_STR_BUF_SIZE - offset, format, args);
637+
uint8_t *str_buf = spi_out_get_str_buf(source);
638+
int len = vsnprintf((char *)(str_buf + offset),
639+
SPI_OUT_LOG_STR_BUF_SIZE - offset, format, args);
689640
if (len < 0) {
690641
return false;
691642
}
692643
len += offset;
693644

694645
// Truncate string if overflowed
695-
if (len >= SPI_OUT_UL_LOG_STR_BUF_SIZE) {
696-
len = SPI_OUT_UL_LOG_STR_BUF_SIZE - 1;
697-
ul_log_str_buf[len] = '\0';
646+
if (len >= SPI_OUT_LOG_STR_BUF_SIZE) {
647+
len = SPI_OUT_LOG_STR_BUF_SIZE - 1;
648+
str_buf[len] = '\0';
698649
}
699650

700-
spi_out_ul_log_write(source, ul_log_str_buf, len, true);
651+
spi_out_log_write(source, str_buf, len, true);
701652
return true;
702653
}
703654

@@ -754,6 +705,10 @@ static void spi_out_ll_log_deinit(void)
754705
// Context: LL task
755706
static void spi_out_ll_log_flush(void)
756707
{
708+
if (!ll_log_inited) {
709+
return;
710+
}
711+
757712
// Flush task log and hci log buffer
758713
spi_out_log_cb_flush_trans(ll_task_log_cb);
759714
spi_out_log_cb_flush_trans(ll_hci_log_cb);
@@ -942,7 +897,7 @@ int ble_log_spi_out_init(void)
942897
goto spi_device_add_failed;
943898
}
944899

945-
if (spi_out_ul_log_init() != 0) {
900+
if (LOG_MODULE_INIT(ul)() != 0) {
946901
goto ul_log_init_failed;
947902
}
948903

@@ -1000,7 +955,7 @@ int ble_log_spi_out_init(void)
1000955
spi_out_ll_log_deinit();
1001956
ll_log_init_failed:
1002957
#endif // SPI_OUT_LL_ENABLED
1003-
spi_out_ul_log_deinit();
958+
LOG_MODULE_DEINIT(ul)();
1004959
ul_log_init_failed:
1005960
spi_bus_remove_device(spi_handle);
1006961
spi_handle = NULL;
@@ -1017,6 +972,10 @@ void ble_log_spi_out_deinit(void)
1017972
return;
1018973
}
1019974

975+
// Reset init flag
976+
spi_out_inited = false;
977+
spi_out_enabled = false;
978+
1020979
#if SPI_OUT_FLUSH_TIMER_ENABLED
1021980
esp_timer_stop(flush_timer);
1022981
esp_timer_delete(flush_timer);
@@ -1044,11 +1003,7 @@ void ble_log_spi_out_deinit(void)
10441003
spi_out_ll_log_deinit();
10451004
#endif // SPI_OUT_LL_ENABLED
10461005

1047-
spi_out_ul_log_deinit();
1048-
1049-
// Reset init flag
1050-
spi_out_inited = false;
1051-
spi_out_enabled = false;
1006+
LOG_MODULE_DEINIT(ul)();
10521007
}
10531008

10541009
#if SPI_OUT_TS_SYNC_ENABLED
@@ -1156,19 +1111,19 @@ IRAM_ATTR void ble_log_spi_out_ll_log_ev_proc(void)
11561111

11571112
int ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16_t len)
11581113
{
1159-
if (!ul_log_inited) {
1114+
if (!LOG_MODULE_INIT_FLAG(ul)) {
11601115
return -1;
11611116
}
11621117

1163-
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1164-
spi_out_ul_log_write(source, addr, len, false);
1165-
xSemaphoreGive(ul_log_mutex);
1118+
xSemaphoreTake(LOG_MODULE_MUTEX(ul), portMAX_DELAY);
1119+
spi_out_log_write(source, addr, len, false);
1120+
xSemaphoreGive(LOG_MODULE_MUTEX(ul));
11661121
return 0;
11671122
}
11681123

11691124
int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
11701125
{
1171-
if (!ul_log_inited) {
1126+
if (!LOG_MODULE_INIT_FLAG(ul)) {
11721127
return -1;
11731128
}
11741129

@@ -1183,9 +1138,9 @@ int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
11831138
va_list args_copy;
11841139
va_copy(args_copy, args);
11851140

1186-
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1187-
bool ret = spi_out_ul_log_printf(source, format, args_copy, 0);
1188-
xSemaphoreGive(ul_log_mutex);
1141+
xSemaphoreTake(LOG_MODULE_MUTEX(ul), portMAX_DELAY);
1142+
bool ret = spi_out_log_printf(source, format, args_copy, 0);
1143+
xSemaphoreGive(LOG_MODULE_MUTEX(ul));
11891144

11901145
va_end(args_copy);
11911146
va_end(args);
@@ -1194,7 +1149,7 @@ int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
11941149

11951150
int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, const char *format, ...)
11961151
{
1197-
if (!ul_log_inited) {
1152+
if (!LOG_MODULE_INIT_FLAG(ul)) {
11981153
return -1;
11991154
}
12001155

@@ -1210,34 +1165,38 @@ int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, c
12101165

12111166
// Create log prefix in the format: "[level][tag] "
12121167
bool ret = false;
1213-
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1214-
int prefix_len = snprintf((char *)ul_log_str_buf, SPI_OUT_UL_LOG_STR_BUF_SIZE,
1168+
xSemaphoreTake(LOG_MODULE_MUTEX(ul), portMAX_DELAY);
1169+
int prefix_len = snprintf((char *)LOG_MODULE_STR_BUF(ul), SPI_OUT_LOG_STR_BUF_SIZE,
12151170
"[%d][%s]", level, tag? tag: "NULL");
1216-
if ((prefix_len < 0) || (prefix_len >= SPI_OUT_UL_LOG_STR_BUF_SIZE)) {
1171+
if ((prefix_len < 0) || (prefix_len >= SPI_OUT_LOG_STR_BUF_SIZE)) {
12171172
goto exit;
12181173
}
1219-
ret = spi_out_ul_log_printf(source, format, args_copy, prefix_len);
1174+
ret = spi_out_log_printf(source, format, args_copy, prefix_len);
12201175
exit:
1221-
xSemaphoreGive(ul_log_mutex);
1176+
xSemaphoreGive(LOG_MODULE_MUTEX(ul));
12221177
va_end(args_copy);
12231178
va_end(args);
12241179
return ret? 0: -1;
12251180
}
12261181

12271182
int ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len)
12281183
{
1229-
if (!ul_log_inited) {
1184+
if (!LOG_MODULE_INIT_FLAG(ul)) {
12301185
return -1;
12311186
}
12321187

1233-
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1234-
spi_out_ul_log_write(source, addr, len, true);
1235-
xSemaphoreGive(ul_log_mutex);
1188+
xSemaphoreTake(LOG_MODULE_MUTEX(ul), portMAX_DELAY);
1189+
spi_out_log_write(source, addr, len, true);
1190+
xSemaphoreGive(LOG_MODULE_MUTEX(ul));
12361191
return 0;
12371192
}
12381193

12391194
void ble_log_spi_out_dump_all(void)
12401195
{
1196+
if (!spi_out_inited) {
1197+
return;
1198+
}
1199+
12411200
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
12421201
portENTER_CRITICAL_SAFE(&spinlock);
12431202

@@ -1258,10 +1217,10 @@ void ble_log_spi_out_dump_all(void)
12581217
}
12591218
#endif // SPI_OUT_LL_ENABLED
12601219

1261-
if (ul_log_inited) {
1220+
if (LOG_MODULE_INIT_FLAG(ul)) {
12621221
// Dump upper layer log buffer
12631222
esp_rom_printf("[UL_LOG_DUMP_START:\n");
1264-
spi_out_log_cb_dump(ul_log_cb);
1223+
spi_out_log_cb_dump(LOG_MODULE_CB(ul));
12651224
esp_rom_printf("\n:UL_LOG_DUMP_END]\n\n");
12661225
}
12671226

@@ -1278,6 +1237,10 @@ void ble_log_spi_out_dump_all(void)
12781237

12791238
void ble_log_spi_out_enable(bool enable)
12801239
{
1240+
if (!spi_out_inited) {
1241+
return;
1242+
}
1243+
12811244
spi_out_enabled = enable;
12821245

12831246
if (!enable) {

0 commit comments

Comments
 (0)