feat: 添加蓝牙串口模块支持

- 添加 ble_serial_module.c 实现蓝牙串口功能
- 添加 ble_serial_rx_event 和 ble_serial_tx_event 事件定义及实现
- 在 CMakeLists.txt 中注册新的源文件和事件
- 配置蓝牙 L2CAP MTU 和缓冲区大小参数
- 修改 usb_cdc_test_module 支持通过蓝牙发送测试消息
- 实现蓝牙连接状态管理及数据收发功能
This commit is contained in:
2026-04-13 10:10:46 +08:00
parent 33fb416cfa
commit 15307dfde5
8 changed files with 562 additions and 3 deletions

View File

@@ -0,0 +1,73 @@
#include <ctype.h>
#include <stdio.h>
#include "ble_serial_rx_event.h"
#define BLE_SERIAL_RX_EVENT_LOG_BUF_LEN 384
static void log_ble_serial_rx_event(const struct app_event_header *aeh)
{
const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh);
char log_buf[BLE_SERIAL_RX_EVENT_LOG_BUF_LEN];
int pos;
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
event->dyndata.size);
if ((pos < 0) || (pos >= sizeof(log_buf))) {
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
return;
}
for (size_t i = 0; i < event->dyndata.size; i++) {
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
isprint(event->dyndata.data[i]) ?
event->dyndata.data[i] : '.');
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
event->dyndata.size);
return;
}
pos += tmp;
}
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\" hex:");
if ((pos < 0) || (pos >= sizeof(log_buf))) {
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
event->dyndata.size);
return;
}
for (size_t i = 0; i < event->dyndata.size; i++) {
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, " %02x",
event->dyndata.data[i]);
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
break;
}
pos += tmp;
}
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
}
static void profile_ble_serial_rx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh);
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(ble_serial_rx_event,
ENCODE(NRF_PROFILER_ARG_U8),
ENCODE("len"),
profile_ble_serial_rx_event);
APP_EVENT_TYPE_DEFINE(ble_serial_rx_event,
log_ble_serial_rx_event,
&ble_serial_rx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -0,0 +1,62 @@
#include <ctype.h>
#include <stdio.h>
#include "ble_serial_tx_event.h"
#define BLE_SERIAL_TX_EVENT_LOG_BUF_LEN 256
static void log_ble_serial_tx_event(const struct app_event_header *aeh)
{
const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh);
char log_buf[BLE_SERIAL_TX_EVENT_LOG_BUF_LEN];
int pos;
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
event->dyndata.size);
if ((pos < 0) || (pos >= sizeof(log_buf))) {
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
return;
}
for (size_t i = 0; i < event->dyndata.size; i++) {
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
isprint(event->dyndata.data[i]) ?
event->dyndata.data[i] : '.');
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
event->dyndata.size);
return;
}
pos += tmp;
}
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\"");
if ((pos < 0) || (pos >= sizeof(log_buf))) {
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
event->dyndata.size);
return;
}
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
}
static void profile_ble_serial_tx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh);
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(ble_serial_tx_event,
ENCODE(NRF_PROFILER_ARG_U8),
ENCODE("len"),
profile_ble_serial_tx_event);
APP_EVENT_TYPE_DEFINE(ble_serial_tx_event,
log_ble_serial_tx_event,
&ble_serial_tx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));