feat(protocol): 添加传输状态事件管理协议会话状态

添加了新的 proto_transport_state_event 事件类型来跟踪协议传输连接状态,
包括链接断开和就绪状态。为 BLE NUS 和 USB CDC 模块实现了状态机管理,
替换原有的简单布尔标志,提供更精确的连接状态跟踪。

- 添加 proto_transport_state_event 事件定义和实现
- 为 BLE NUS 模块引入业务状态机管理
- 为 USB CDC 模块引入业务状态机管理
- 实现协议模块中的会话状态管理
- 移除 protocol_module_reset_transport_state 函数
- 更新 CMakeLists.txt 包含新事件源文件
This commit is contained in:
2026-04-17 11:13:11 +08:00
parent 2ca02325c1
commit 0cbb16052d
11 changed files with 723 additions and 209 deletions

View File

@@ -22,6 +22,7 @@
#include "function_bitmap_update_event.h"
#include "hid_led_event.h"
#include "proto_rx_event.h"
#include "proto_transport_state_event.h"
#include "proto_tx_event.h"
#include "protocol_module.h"
#include "theme_rgb_update_event.h"
@@ -39,7 +40,14 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
static bool initialized;
static bool running;
static bool hello_done[PROTO_TRANSPORT_COUNT];
enum proto_session_state {
PROTO_SESSION_DOWN = 0,
PROTO_SESSION_WAIT_HELLO,
PROTO_SESSION_ACTIVE,
};
static enum proto_session_state session_state[PROTO_TRANSPORT_COUNT];
static int decode_body(const uint8_t *payload, size_t payload_len,
CdcPacketBody *body)
@@ -151,8 +159,8 @@ static int encode_function_bitmap_state(const uint8_t *bitmap, uint8_t *payload,
static int module_init(void)
{
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
hello_done[i] = false;
for (size_t i = 0; i < ARRAY_SIZE(session_state); i++) {
session_state[i] = PROTO_SESSION_DOWN;
}
return 0;
@@ -174,22 +182,13 @@ static void module_pause(void)
return;
}
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
hello_done[i] = false;
for (size_t i = 0; i < ARRAY_SIZE(session_state); i++) {
session_state[i] = PROTO_SESSION_DOWN;
}
running = false;
}
void protocol_module_reset_transport_state(enum proto_transport transport)
{
if (transport >= PROTO_TRANSPORT_COUNT) {
return;
}
hello_done[transport] = false;
}
int protocol_module_process_message(enum proto_transport transport,
const uint8_t *req_payload,
size_t req_payload_len,
@@ -216,6 +215,10 @@ int protocol_module_process_message(enum proto_transport transport,
switch (body.which_body) {
case CdcPacketBody_hello_req_tag:
if (session_state[transport] == PROTO_SESSION_DOWN) {
return -EAGAIN;
}
LOG_INF("HelloReq transport:%u protocol_version:%u",
transport, body.body.hello_req.protocol_version);
@@ -224,11 +227,11 @@ int protocol_module_process_message(enum proto_transport transport,
body.body.hello_req.protocol_version);
}
hello_done[transport] = true;
session_state[transport] = PROTO_SESSION_ACTIVE;
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
case CdcPacketBody_bitmap_tag:
if (!hello_done[transport]) {
if (session_state[transport] != PROTO_SESSION_ACTIVE) {
return encode_error(CdcPacketBody_bitmap_tag,
ErrorCode_ERROR_CODE_NOT_READY,
rsp_payload, rsp_payload_buf_size,
@@ -255,7 +258,7 @@ int protocol_module_process_message(enum proto_transport transport,
rsp_payload_buf_size, rsp_payload_len);
case CdcPacketBody_time_sync_tag:
if (!hello_done[transport]) {
if (session_state[transport] != PROTO_SESSION_ACTIVE) {
return encode_error(CdcPacketBody_time_sync_tag,
ErrorCode_ERROR_CODE_NOT_READY,
rsp_payload, rsp_payload_buf_size,
@@ -278,7 +281,7 @@ int protocol_module_process_message(enum proto_transport transport,
rsp_payload_buf_size, rsp_payload_len);
case CdcPacketBody_theme_rgb_tag:
if (!hello_done[transport]) {
if (session_state[transport] != PROTO_SESSION_ACTIVE) {
return encode_error(CdcPacketBody_theme_rgb_tag,
ErrorCode_ERROR_CODE_NOT_READY,
rsp_payload, rsp_payload_buf_size,
@@ -340,6 +343,29 @@ static bool handle_proto_rx_event(const struct proto_rx_event *event)
return false;
}
static bool handle_proto_transport_state_event(
const struct proto_transport_state_event *event)
{
if (event->transport >= PROTO_TRANSPORT_COUNT) {
return false;
}
switch (event->state) {
case PROTO_TRANSPORT_LINK_DOWN:
session_state[event->transport] = PROTO_SESSION_DOWN;
break;
case PROTO_TRANSPORT_LINK_READY:
session_state[event->transport] = PROTO_SESSION_WAIT_HELLO;
break;
default:
return false;
}
return false;
}
static bool handle_function_bitmap_state_event(
const struct function_bitmap_state_event *event)
{
@@ -353,7 +379,7 @@ static bool handle_function_bitmap_state_event(
for (enum proto_transport transport = 0; transport < PROTO_TRANSPORT_COUNT;
transport++) {
if (!hello_done[transport]) {
if (session_state[transport] != PROTO_SESSION_ACTIVE) {
continue;
}
@@ -388,7 +414,7 @@ static bool handle_hid_led_event(const struct hid_led_event *event)
PROTO_TRANSPORT_USB_CDC :
PROTO_TRANSPORT_BLE_NUS;
if (!hello_done[transport]) {
if (session_state[transport] != PROTO_SESSION_ACTIVE) {
return false;
}
@@ -412,6 +438,11 @@ static bool app_event_handler(const struct app_event_header *aeh)
return handle_proto_rx_event(cast_proto_rx_event(aeh));
}
if (is_proto_transport_state_event(aeh)) {
return handle_proto_transport_state_event(
cast_proto_transport_state_event(aeh));
}
if (is_function_bitmap_state_event(aeh)) {
return handle_function_bitmap_state_event(
cast_function_bitmap_state_event(aeh));
@@ -480,5 +511,6 @@ APP_EVENT_SUBSCRIBE(MODULE, function_bitmap_state_event);
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, proto_rx_event);
APP_EVENT_SUBSCRIBE(MODULE, proto_transport_state_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);