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

308
src/ble_serial_module.c Normal file
View File

@@ -0,0 +1,308 @@
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <app_event_manager.h>
#define MODULE ble_serial_module
#include <caf/events/module_state_event.h>
#include <caf/events/power_event.h>
#include <caf/events/ble_common_event.h>
#include <zephyr/bluetooth/att.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/logging/log.h>
#include "ble_serial_rx_event.h"
#include "ble_serial_tx_event.h"
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define BLE_SERIAL_RX_MAX_LEN 64U
#define BLE_SERIAL_MIN_PAYLOAD_LEN 32U
#define BLE_SERIAL_NOTIFY_OVERHEAD 3U
#define BLE_SERIAL_SERVICE_UUID_VAL \
BT_UUID_128_ENCODE(0x0b7f6000, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
#define BLE_SERIAL_RX_UUID_VAL \
BT_UUID_128_ENCODE(0x0b7f6001, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
#define BLE_SERIAL_TX_UUID_VAL \
BT_UUID_128_ENCODE(0x0b7f6002, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
static struct bt_conn *active_conn;
static bool initialized;
static bool running;
static bool ble_ready;
static bool secured;
static bool tx_notify_enabled;
static const struct bt_uuid_128 ble_serial_service_uuid =
BT_UUID_INIT_128(BLE_SERIAL_SERVICE_UUID_VAL);
static const struct bt_uuid_128 ble_serial_rx_uuid =
BT_UUID_INIT_128(BLE_SERIAL_RX_UUID_VAL);
static const struct bt_uuid_128 ble_serial_tx_uuid =
BT_UUID_INIT_128(BLE_SERIAL_TX_UUID_VAL);
static void submit_ble_serial_rx_event(const uint8_t *data, size_t len)
{
struct ble_serial_rx_event *event = new_ble_serial_rx_event(len);
memcpy(event->dyndata.data, data, len);
APP_EVENT_SUBMIT(event);
}
static void tx_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
ARG_UNUSED(attr);
tx_notify_enabled = (value == BT_GATT_CCC_NOTIFY);
LOG_INF("BLE serial TX notify %s", tx_notify_enabled ? "enabled" : "disabled");
}
static ssize_t rx_write_handler(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf,
uint16_t len,
uint16_t offset,
uint8_t flags)
{
ARG_UNUSED(attr);
ARG_UNUSED(flags);
if (!running || !ble_ready || !secured || (conn != active_conn)) {
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
if (offset != 0U) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if ((buf == NULL) || (len == 0U) || (len > BLE_SERIAL_RX_MAX_LEN)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}
submit_ble_serial_rx_event(buf, len);
return len;
}
BT_GATT_SERVICE_DEFINE(ble_serial_svc,
BT_GATT_PRIMARY_SERVICE(&ble_serial_service_uuid.uuid),
BT_GATT_CHARACTERISTIC(&ble_serial_rx_uuid.uuid,
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE_ENCRYPT,
NULL, rx_write_handler, NULL),
BT_GATT_CHARACTERISTIC(&ble_serial_tx_uuid.uuid,
BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_NONE,
NULL, NULL, NULL),
BT_GATT_CCC(tx_ccc_cfg_changed,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT)
);
static void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
{
if (conn != active_conn) {
return;
}
LOG_INF("BLE serial MTU updated tx:%u rx:%u", tx, rx);
}
static struct bt_gatt_cb gatt_callbacks = {
.att_mtu_updated = mtu_updated,
};
static size_t notify_payload_max(void)
{
uint16_t mtu;
if (active_conn == NULL) {
return 0U;
}
mtu = bt_gatt_get_mtu(active_conn);
if (mtu <= BLE_SERIAL_NOTIFY_OVERHEAD) {
return 0U;
}
return (size_t)(mtu - BLE_SERIAL_NOTIFY_OVERHEAD);
}
static void reset_connection_state(void)
{
active_conn = NULL;
secured = false;
tx_notify_enabled = false;
}
static int module_init(void)
{
bt_gatt_cb_register(&gatt_callbacks);
reset_connection_state();
return 0;
}
static int module_start(void)
{
if (running) {
return 0;
}
running = true;
return 0;
}
static void module_pause(void)
{
if (!running) {
return;
}
running = false;
tx_notify_enabled = false;
}
static bool handle_ble_peer_event(const struct ble_peer_event *event)
{
switch (event->state) {
case PEER_STATE_CONNECTED:
if (active_conn != NULL) {
return false;
}
active_conn = event->id;
secured = false;
tx_notify_enabled = false;
return false;
case PEER_STATE_SECURED:
if (active_conn != event->id) {
return false;
}
secured = true;
return false;
case PEER_STATE_DISCONNECTED:
if (active_conn != event->id) {
return false;
}
reset_connection_state();
return false;
default:
return false;
}
}
static bool handle_ble_serial_tx_event(const struct ble_serial_tx_event *event)
{
size_t payload_max;
int err;
if (!running || !ble_ready || (active_conn == NULL) || !secured || !tx_notify_enabled) {
return false;
}
payload_max = notify_payload_max();
if (payload_max < BLE_SERIAL_MIN_PAYLOAD_LEN) {
LOG_WRN("BLE serial MTU payload too small:%u", (uint32_t)payload_max);
return false;
}
if (event->dyndata.size > payload_max) {
LOG_WRN("BLE serial TX too large len:%u max:%u",
(uint32_t)event->dyndata.size, (uint32_t)payload_max);
return false;
}
err = bt_gatt_notify(active_conn, &ble_serial_svc.attrs[4],
event->dyndata.data,
(uint16_t)event->dyndata.size);
if (err) {
LOG_WRN("bt_gatt_notify failed (%d)", err);
}
return false;
}
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_ble_serial_tx_event(aeh)) {
return handle_ble_serial_tx_event(cast_ble_serial_tx_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);
int err;
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
if (!initialized) {
err = module_init();
if (err) {
module_set_state(MODULE_STATE_ERROR);
return false;
}
initialized = true;
}
err = module_start();
if (err) {
module_set_state(MODULE_STATE_ERROR);
}
return false;
}
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
ble_ready = true;
if (running) {
module_set_state(MODULE_STATE_READY);
}
return false;
}
return false;
}
if (is_power_down_event(aeh)) {
if (initialized) {
module_pause();
module_set_state(MODULE_STATE_STANDBY);
}
return false;
}
if (is_wake_up_event(aeh)) {
if (initialized) {
int err = module_start();
if (err) {
module_set_state(MODULE_STATE_ERROR);
} else if (ble_ready) {
module_set_state(MODULE_STATE_READY);
}
}
return false;
}
return false;
}
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, ble_serial_tx_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);