feat(ble): 添加BLE NUS模块替换原有的BLE串口功能

- 将ble_serial_module替换为ble_nus_module以使用标准的BLE NUS服务
- 移除不再使用的cdc_wrapper_module和相关事件处理
- 更新协议传输层抽象,支持USB CDC和BLE NUS两种传输方式
- 创建统一的proto_rx_event和proto_tx_event替代专用的串行通信事件
- 添加proto_common.h定义传输类型枚举
- 修改protocol_module接口以支持多传输方式
- 在prj.conf中启用CONFIG_BT_ZEPHYR_NUS配置选项
This commit is contained in:
2026-04-15 10:23:12 +08:00
parent c4b205b8a1
commit bc42a4dd63
25 changed files with 538 additions and 1499 deletions

View File

@@ -1,73 +0,0 @@
#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

@@ -1,62 +0,0 @@
#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));

View File

@@ -1,29 +0,0 @@
#include "cdc_proto_tx_event.h"
static void log_cdc_proto_tx_event(const struct app_event_header *aeh)
{
const struct cdc_proto_tx_event *event = cast_cdc_proto_tx_event(aeh);
APP_EVENT_MANAGER_LOG(aeh, "type:0x%02x len:%zu",
event->type, event->dyndata.size);
}
static void profile_cdc_proto_tx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct cdc_proto_tx_event *event = cast_cdc_proto_tx_event(aeh);
nrf_profiler_log_encode_uint8(buf, event->type);
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(cdc_proto_tx_event,
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8),
ENCODE("type", "len"),
profile_cdc_proto_tx_event);
APP_EVENT_TYPE_DEFINE(cdc_proto_tx_event,
log_cdc_proto_tx_event,
&cdc_proto_tx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -0,0 +1,42 @@
#include "proto_rx_event.h"
static const char *transport_name(enum proto_transport transport)
{
switch (transport) {
case PROTO_TRANSPORT_USB_CDC:
return "usb_cdc";
case PROTO_TRANSPORT_BLE_NUS:
return "ble_nus";
default:
return "?";
}
}
static void log_proto_rx_event(const struct app_event_header *aeh)
{
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
transport_name(event->transport),
event->dyndata.size);
}
static void profile_proto_rx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
nrf_profiler_log_encode_uint8(buf, event->transport);
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(proto_rx_event,
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
ENCODE("transport", "len"),
profile_proto_rx_event);
APP_EVENT_TYPE_DEFINE(proto_rx_event,
log_proto_rx_event,
&proto_rx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -0,0 +1,42 @@
#include "proto_tx_event.h"
static const char *transport_name(enum proto_transport transport)
{
switch (transport) {
case PROTO_TRANSPORT_USB_CDC:
return "usb_cdc";
case PROTO_TRANSPORT_BLE_NUS:
return "ble_nus";
default:
return "?";
}
}
static void log_proto_tx_event(const struct app_event_header *aeh)
{
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
transport_name(event->transport),
event->dyndata.size);
}
static void profile_proto_tx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
nrf_profiler_log_encode_uint8(buf, event->transport);
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(proto_tx_event,
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
ENCODE("transport", "len"),
profile_proto_tx_event);
APP_EVENT_TYPE_DEFINE(proto_tx_event,
log_proto_tx_event,
&proto_tx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -1,73 +0,0 @@
#include <ctype.h>
#include <stdio.h>
#include "usb_cdc_rx_event.h"
#define USB_CDC_RX_EVENT_LOG_BUF_LEN 384
static void log_usb_cdc_rx_event(const struct app_event_header *aeh)
{
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
char log_buf[USB_CDC_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_usb_cdc_rx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(usb_cdc_rx_event,
ENCODE(NRF_PROFILER_ARG_U8),
ENCODE("len"),
profile_usb_cdc_rx_event);
APP_EVENT_TYPE_DEFINE(usb_cdc_rx_event,
log_usb_cdc_rx_event,
&usb_cdc_rx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -1,62 +0,0 @@
#include <ctype.h>
#include <stdio.h>
#include "usb_cdc_tx_event.h"
#define USB_CDC_TX_EVENT_LOG_BUF_LEN 256
static void log_usb_cdc_tx_event(const struct app_event_header *aeh)
{
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
char log_buf[USB_CDC_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_usb_cdc_tx_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
}
APP_EVENT_INFO_DEFINE(usb_cdc_tx_event,
ENCODE(NRF_PROFILER_ARG_U8),
ENCODE("len"),
profile_usb_cdc_tx_event);
APP_EVENT_TYPE_DEFINE(usb_cdc_tx_event,
log_usb_cdc_tx_event,
&usb_cdc_tx_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));