GCC Code Coverage Report


Directory: main/
File: bluetooth/mon.c
Date: 2025-10-04 14:03:00
Exec Total Coverage
Lines: 3 49 6.1%
Functions: 1 3 33.3%
Branches: 1 14 7.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2024-2025, Jacques Gagnon
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include "freertos/FreeRTOS.h"
10 #include "freertos/task.h"
11 #include <esp_timer.h>
12 #include <esp_efuse.h>
13 #include "esp_app_desc.h"
14 #include "hal/efuse_hal.h"
15 #include "mon.h"
16 #include "host.h"
17 #include "zephyr/hci.h"
18 #include "adapter/config.h"
19 #include "adapter/memory_card.h"
20
21 #define BT_MON_TX_PIN 12
22 #define BT_MON_RX_PIN 13
23 #define BT_MON_RTS_PIN 15
24 #define BT_MON_CTS_PIN 14
25
26 struct bt_mon_hdr {
27 uint16_t data_len;
28 uint16_t opcode;
29 uint8_t flags;
30 uint8_t hdr_len;
31 uint8_t ts_type;
32 uint32_t ts_data;
33 } __packed;
34
35 #ifdef CONFIG_BLUERETRO_BT_H4_TRACE
36 static int uart_port = UART_NUM_1;
37 #endif
38 static struct bt_mon_hdr mon_hdr = {0};
39 static uint32_t log_offset = 0;
40 static char log_buffer[512];
41
42 void bt_mon_init(void) {
43 #ifdef CONFIG_BLUERETRO_BT_H4_TRACE
44 uart_config_t uart_cfg = {
45 .baud_rate = 921600,
46 .data_bits = UART_DATA_8_BITS,
47 .parity = UART_PARITY_DISABLE,
48 .stop_bits = UART_STOP_BITS_1,
49 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
50 .source_clk = UART_SCLK_DEFAULT,
51 .rx_flow_ctrl_thresh = UART_HW_FIFO_LEN(UART_NUM_1) - 1,
52 };
53
54 printf("# %s: set uart pin tx:%d\n", __FUNCTION__, BT_MON_TX_PIN);
55 printf("# %s: set baud_rate:%d.\n", __FUNCTION__, uart_cfg.baud_rate);
56
57 ESP_ERROR_CHECK(uart_driver_delete(uart_port));
58 ESP_ERROR_CHECK(uart_driver_install(uart_port, UART_HW_FIFO_LEN(port_num) * 2, 0, 0, NULL, 0));
59 ESP_ERROR_CHECK(uart_param_config(uart_port, &uart_cfg));
60 ESP_ERROR_CHECK(uart_set_pin(uart_port, BT_MON_TX_PIN, BT_MON_RX_PIN, BT_MON_RTS_PIN, BT_MON_CTS_PIN));
61 #endif /* CONFIG_BLUERETRO_BT_H4_TRACE */
62
63 const esp_app_desc_t *app_desc = esp_app_get_description();
64 uint32_t chip_package = esp_efuse_get_pkg_ver();
65 uint32_t chip_revision = efuse_hal_chip_revision();
66 bt_mon_log(true, "Project name: %s", app_desc->project_name);
67 bt_mon_log(true, "App version: %s", app_desc->version);
68 bt_mon_log(true, "Compile time: %s %s", app_desc->date, app_desc->time);
69 bt_mon_log(true, "ESP-IDF version: %s", app_desc->idf_ver);
70 bt_mon_log(true, "Chip package: %d revision: %d", chip_package, chip_revision);
71
72 config_debug_log();
73 }
74
75 void IRAM_ATTR bt_mon_tx(uint16_t opcode, uint8_t *data, uint16_t len) {
76 #ifdef CONFIG_BLUERETRO_BT_H4_TRACE
77 mon_hdr.data_len = len + 4 + 5;
78 mon_hdr.opcode = opcode;
79 mon_hdr.hdr_len = 5;
80 mon_hdr.ts_type = 8;
81 mon_hdr.ts_data = esp_timer_get_time() / 100;
82
83 uart_write_bytes(uart_port, &mon_hdr, sizeof(mon_hdr));
84 uart_write_bytes(uart_port, data, len);
85 #else
86 static uint32_t offset = 0;
87 if (config.global_cfg.banksel == CONFIG_BANKSEL_DBG && (offset + len) <= MC_BUFFER_SIZE) {
88 uint32_t hdr_len = sizeof(struct bt_mon_hdr);
89 uint8_t *hdr_data = (uint8_t *)&mon_hdr;
90 mon_hdr.data_len = len + 4 + 5;
91 mon_hdr.opcode = opcode;
92 mon_hdr.hdr_len = 5;
93 mon_hdr.ts_type = 8;
94 mon_hdr.ts_data = esp_timer_get_time() / 100;
95
96 while (hdr_len) {
97 uint32_t max_len = MC_BUFFER_BLOCK_SIZE - (offset % MC_BUFFER_BLOCK_SIZE);
98 uint32_t write_len = (hdr_len > max_len) ? max_len : hdr_len;
99 mc_write(offset, hdr_data, hdr_len);
100 offset += write_len;
101 hdr_data += write_len;
102 hdr_len -= write_len;
103 }
104
105 while (len) {
106 uint32_t max_len = MC_BUFFER_BLOCK_SIZE - (offset % MC_BUFFER_BLOCK_SIZE);
107 uint32_t write_len = (len > max_len) ? max_len : len;
108 mc_write(offset, data, len);
109 offset += write_len;
110 data += write_len;
111 len -= write_len;
112 }
113 }
114 #endif /* CONFIG_BLUERETRO_BT_H4_TRACE */
115 }
116
117 1043 void IRAM_ATTR bt_mon_log(bool end, const char * format, ...) {
118 #ifndef CONFIG_BLUERETRO_BT_H4_TRACE
119
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→9) taken 1043 times.
1043 if (config.global_cfg.banksel == CONFIG_BANKSEL_DBG)
120 #endif
121 {
122 va_list args;
123 va_start(args, format);
124 size_t max_len = sizeof(log_buffer) - log_offset;
125 int len = vsnprintf(log_buffer + log_offset, max_len, format, args);
126 if (len > 0 && len < max_len) {
127 log_offset += len;
128 }
129 va_end(args);
130 if (end) {
131 bt_mon_tx(BT_MON_SYS_NOTE, (uint8_t *)log_buffer, log_offset);
132 log_offset = 0;
133 }
134 }
135 1043 }
136