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,12 +0,0 @@
#ifndef BLINKY_CDC_WRAPPER_MODULE_H_
#define BLINKY_CDC_WRAPPER_MODULE_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_CDC_WRAPPER_MODULE_H_ */

View File

@@ -1,43 +0,0 @@
#ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_
#define BLINKY_BLE_SERIAL_RX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#ifdef __cplusplus
extern "C" {
#endif
struct ble_serial_rx_event {
struct app_event_header header;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_rx_event);
static inline int submit_ble_serial_rx_event(const uint8_t *data, size_t len)
{
struct ble_serial_rx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_ble_serial_rx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_BLE_SERIAL_RX_EVENT_H_ */

View File

@@ -1,43 +0,0 @@
#ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_
#define BLINKY_BLE_SERIAL_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#ifdef __cplusplus
extern "C" {
#endif
struct ble_serial_tx_event {
struct app_event_header header;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_tx_event);
static inline int submit_ble_serial_tx_event(const uint8_t *data, size_t len)
{
struct ble_serial_tx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_ble_serial_tx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_BLE_SERIAL_TX_EVENT_H_ */

View File

@@ -1,46 +0,0 @@
#ifndef BLINKY_CDC_PROTO_TX_EVENT_H_
#define BLINKY_CDC_PROTO_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#ifdef __cplusplus
extern "C" {
#endif
struct cdc_proto_tx_event {
struct app_event_header header;
uint8_t type;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(cdc_proto_tx_event);
static inline int submit_cdc_proto_tx_event(uint8_t type, const uint8_t *payload,
size_t payload_len)
{
struct cdc_proto_tx_event *event;
if ((payload == NULL) && (payload_len > 0U)) {
return -EINVAL;
}
event = new_cdc_proto_tx_event(payload_len);
event->type = type;
if (payload_len > 0U) {
memcpy(event->dyndata.data, payload, payload_len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_CDC_PROTO_TX_EVENT_H_ */

18
inc/events/proto_common.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef BLINKY_PROTO_COMMON_H_
#define BLINKY_PROTO_COMMON_H_
#ifdef __cplusplus
extern "C" {
#endif
enum proto_transport {
PROTO_TRANSPORT_USB_CDC = 0,
PROTO_TRANSPORT_BLE_NUS,
PROTO_TRANSPORT_COUNT,
};
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_PROTO_COMMON_H_ */

View File

@@ -0,0 +1,50 @@
#ifndef BLINKY_PROTO_RX_EVENT_H_
#define BLINKY_PROTO_RX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#include "proto_common.h"
#ifdef __cplusplus
extern "C" {
#endif
struct proto_rx_event {
struct app_event_header header;
enum proto_transport transport;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_rx_event);
static inline int submit_proto_rx_event(enum proto_transport transport,
const uint8_t *data, size_t len)
{
struct proto_rx_event *event;
if ((transport >= PROTO_TRANSPORT_COUNT) ||
((data == NULL) && (len > 0U))) {
return -EINVAL;
}
event = new_proto_rx_event(len);
event->transport = transport;
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_PROTO_RX_EVENT_H_ */

View File

@@ -0,0 +1,50 @@
#ifndef BLINKY_PROTO_TX_EVENT_H_
#define BLINKY_PROTO_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#include "proto_common.h"
#ifdef __cplusplus
extern "C" {
#endif
struct proto_tx_event {
struct app_event_header header;
enum proto_transport transport;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_tx_event);
static inline int submit_proto_tx_event(enum proto_transport transport,
const uint8_t *data, size_t len)
{
struct proto_tx_event *event;
if ((transport >= PROTO_TRANSPORT_COUNT) ||
((data == NULL) && (len > 0U))) {
return -EINVAL;
}
event = new_proto_tx_event(len);
event->transport = transport;
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_PROTO_TX_EVENT_H_ */

View File

@@ -1,43 +0,0 @@
#ifndef BLINKY_USB_CDC_RX_EVENT_H_
#define BLINKY_USB_CDC_RX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#ifdef __cplusplus
extern "C" {
#endif
struct usb_cdc_rx_event {
struct app_event_header header;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_rx_event);
static inline int submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
{
struct usb_cdc_rx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_usb_cdc_rx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_USB_CDC_RX_EVENT_H_ */

View File

@@ -1,43 +0,0 @@
#ifndef BLINKY_USB_CDC_TX_EVENT_H_
#define BLINKY_USB_CDC_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h>
#ifdef __cplusplus
extern "C" {
#endif
struct usb_cdc_tx_event {
struct app_event_header header;
struct event_dyndata dyndata;
};
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_tx_event);
static inline int submit_usb_cdc_tx_event(const uint8_t *data, size_t len)
{
struct usb_cdc_tx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_usb_cdc_tx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_USB_CDC_TX_EVENT_H_ */

View File

@@ -1,30 +1,24 @@
#ifndef BLINKY_PROTOCOL_MODULE_H_
#define BLINKY_PROTOCOL_MODULE_H_
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "proto_common.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CDC_PROTO_TYPE_HELLO_REQ 0x01U
#define CDC_PROTO_TYPE_HELLO_RSP 0x02U
#define CDC_PROTO_TYPE_BITMAP 0x10U
#define CDC_PROTO_TYPE_FUNCTION_KEY_EVENT 0x20U
#define CDC_PROTO_TYPE_LED_STATE 0x21U
#define CDC_PROTO_TYPE_TIME_SYNC 0x30U
#define CDC_PROTO_TYPE_THEME_RGB 0x31U
#define CDC_PROTO_TYPE_ACK 0x7EU
#define CDC_PROTO_TYPE_ERROR 0x7FU
int protocol_module_process_message(enum proto_transport transport,
const uint8_t *req_payload,
size_t req_payload_len,
uint8_t *rsp_payload,
size_t rsp_payload_buf_size,
size_t *rsp_payload_len);
int protocol_module_process_cdc_packet(uint8_t req_type,
const uint8_t *req_payload,
size_t req_payload_len,
uint8_t *rsp_type,
uint8_t *rsp_payload,
size_t rsp_payload_buf_size,
size_t *rsp_payload_len);
void protocol_module_reset_transport_state(enum proto_transport transport);
#ifdef __cplusplus
}

View File

@@ -1,26 +0,0 @@
#ifndef BLINKY_USB_DEVICE_MODULE_H_
#define BLINKY_USB_DEVICE_MODULE_H_
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
enum usb_function {
USB_FUNCTION_HID = 0x01,
USB_FUNCTION_CDC_ACM = 0x02,
};
enum usb_device_state {
USB_DEVICE_STATE_DISCONNECTED,
USB_DEVICE_STATE_POWERED,
USB_DEVICE_STATE_ACTIVE,
USB_DEVICE_STATE_SUSPENDED,
};
#ifdef __cplusplus
}
#endif
#endif /* BLINKY_USB_DEVICE_MODULE_H_ */