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

@@ -7,9 +7,12 @@
#include <caf/events/module_state_event.h>
#include <caf/events/power_event.h>
#include <caf/events/ble_common_event.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include "ble_serial_tx_event.h"
#include "usb_cdc_tx_event.h"
#include "usb_device_state_event.h"
@@ -18,11 +21,13 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define USB_CDC_TEST_PERIOD K_SECONDS(1)
static const uint8_t hello_message[] = "hello\r\n";
static const uint8_t ble_hello_message[] = "ble_hello\r\n";
static struct k_work_delayable hello_work;
static bool initialized;
static bool running;
static bool usb_active;
static bool ble_active;
static void submit_hello_message(void)
{
@@ -33,15 +38,31 @@ static void submit_hello_message(void)
APP_EVENT_SUBMIT(event);
}
static void submit_ble_hello_message(void)
{
struct ble_serial_tx_event *event =
new_ble_serial_tx_event(sizeof(ble_hello_message) - 1U);
memcpy(event->dyndata.data, ble_hello_message, sizeof(ble_hello_message) - 1U);
APP_EVENT_SUBMIT(event);
}
static void hello_work_handler(struct k_work *work)
{
ARG_UNUSED(work);
if (!running || !usb_active) {
if (!running) {
return;
}
submit_hello_message();
if (usb_active) {
submit_hello_message();
}
if (ble_active) {
submit_ble_hello_message();
}
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
}
@@ -58,7 +79,7 @@ static int module_start(void)
}
running = true;
if (usb_active) {
if (usb_active || ble_active) {
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
}
@@ -98,12 +119,56 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
return false;
}
static bool handle_ble_peer_event(const struct ble_peer_event *event)
{
bool new_ble_active = ble_active;
switch (event->state) {
case PEER_STATE_CONNECTED:
new_ble_active = false;
break;
case PEER_STATE_SECURED:
new_ble_active = true;
break;
case PEER_STATE_DISCONNECTED:
new_ble_active = false;
break;
default:
return false;
}
if (new_ble_active == ble_active) {
return false;
}
ble_active = new_ble_active;
if (!running) {
return false;
}
if (usb_active || ble_active) {
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
} else {
k_work_cancel_delayable(&hello_work);
}
return false;
}
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_usb_device_state_event(aeh)) {
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
}
if (is_ble_peer_event(aeh)) {
return handle_ble_peer_event(cast_ble_peer_event(aeh));
}
if (is_module_state_event(aeh)) {
const struct module_state_event *event = cast_module_state_event(aeh);
@@ -159,6 +224,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, ble_peer_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);