feat: 添加模块生命周期管理框架并重构现有模块

添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机,
包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID
模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。

提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
2026-04-17 19:12:57 +08:00
parent 8bfb8b540c
commit ceebaaa600
19 changed files with 1361 additions and 1079 deletions

View File

@@ -18,6 +18,7 @@
#include "hid_report_sent_event.h"
#include "hid_tx_report_event.h"
#include "keyboard_core.h"
#include "module_lifecycle.h"
#include "set_protocol_event.h"
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
@@ -40,15 +41,40 @@ BT_HIDS_DEF(hids_obj,
KEYBOARD_CONSUMER_REPORT_SIZE,
BLE_HID_KEYS_LED_REPORT_SIZE);
static struct bt_conn *active_conn;
static struct in_flight_report in_flight;
static enum keyboard_protocol_mode protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
static bool initialized;
static bool running;
static bool secured;
static bool keyboard_report_notify_enabled;
static bool consumer_report_notify_enabled;
static bool boot_keyboard_notify_enabled;
struct ble_hid_module_ctx {
struct module_lifecycle_ctx lc;
struct bt_conn *active_conn;
struct in_flight_report in_flight;
enum keyboard_protocol_mode protocol_mode;
bool secured;
bool keyboard_report_notify_enabled;
bool consumer_report_notify_enabled;
bool boot_keyboard_notify_enabled;
};
static int do_init(void);
static int do_start(void);
static int do_stop(void);
static const struct module_lifecycle_cfg lifecycle_cfg = {
.mode = ML_MODE_POWER,
.stopped_state = MODULE_STATE_STANDBY,
};
static const struct module_lifecycle_ops lifecycle_ops = {
.do_init = do_init,
.do_start = do_start,
.do_stop = do_stop,
};
static struct ble_hid_module_ctx ctx = {
.lc = {
.state = LC_UNINIT,
.cfg = &lifecycle_cfg,
.ops = &lifecycle_ops,
},
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
};
static const uint8_t hid_report_desc[] = {
0x05, 0x01, /* Usage Page (Generic Desktop) */
@@ -100,24 +126,25 @@ static const uint8_t hid_report_desc[] = {
static void submit_ble_transport_state_event(void)
{
bool ready = running && secured && (active_conn != NULL);
bool ready = module_lifecycle_is_running(&ctx.lc) &&
ctx.secured && (ctx.active_conn != NULL);
uint8_t report_ready_bm = 0U;
if (ready && ((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
boot_keyboard_notify_enabled :
keyboard_report_notify_enabled)) {
if (ready && ((ctx.protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
ctx.boot_keyboard_notify_enabled :
ctx.keyboard_report_notify_enabled)) {
report_ready_bm |= BIT(KEYBOARD_REPORT_TYPE_KEYS);
}
if (ready && (protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
consumer_report_notify_enabled) {
if (ready && (ctx.protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
ctx.consumer_report_notify_enabled) {
report_ready_bm |= BIT(KEYBOARD_REPORT_TYPE_CONSUMER);
}
submit_hid_channel_state_event(
HID_SEND_CH_BLE_SHARED,
report_ready_bm,
protocol_mode);
ctx.protocol_mode);
}
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
@@ -125,9 +152,9 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
bool enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
if (report_id == BLE_HID_KEYS_REPORT_ID) {
keyboard_report_notify_enabled = enabled;
ctx.keyboard_report_notify_enabled = enabled;
} else if (report_id == BLE_HID_CONSUMER_REPORT_ID) {
consumer_report_notify_enabled = enabled;
ctx.consumer_report_notify_enabled = enabled;
}
submit_ble_transport_state_event();
@@ -135,7 +162,7 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
{
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
ctx.boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
submit_ble_transport_state_event();
}
@@ -144,14 +171,14 @@ static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
ARG_UNUSED(conn);
ARG_UNUSED(user_data);
if (!in_flight.active) {
if (!ctx.in_flight.active) {
return;
}
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
in_flight.report_type,
in_flight.sequence, false);
in_flight.active = false;
ctx.in_flight.report_type,
ctx.in_flight.sequence, false);
ctx.in_flight.active = false;
}
static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
@@ -187,22 +214,22 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
switch (evt) {
case BT_HIDS_PM_EVT_BOOT_MODE_ENTERED:
protocol_mode = KEYBOARD_PROTOCOL_MODE_BOOT;
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_BOOT;
break;
case BT_HIDS_PM_EVT_REPORT_MODE_ENTERED:
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
break;
default:
return;
}
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
submit_set_protocol_event(HID_TRANSPORT_BLE, ctx.protocol_mode);
submit_ble_transport_state_event();
}
static int module_init(void)
static int do_init(void)
{
struct bt_hids_init_param hids_init_param = { 0 };
struct bt_hids_inp_rep *input_report;
@@ -239,38 +266,38 @@ static int module_init(void)
return bt_hids_init(&hids_obj, &hids_init_param);
}
static int module_start(void)
static int do_start(void)
{
if (running) {
if (module_lifecycle_is_running(&ctx.lc)) {
return 0;
}
running = true;
submit_ble_transport_state_event();
return 0;
}
static void module_pause(void)
static int do_stop(void)
{
if (!running) {
return;
if (!module_lifecycle_is_running(&ctx.lc)) {
return 0;
}
in_flight.active = false;
running = false;
ctx.in_flight.active = false;
submit_ble_transport_state_event();
return 0;
}
static void reset_connection_state(void)
{
active_conn = NULL;
secured = false;
keyboard_report_notify_enabled = false;
consumer_report_notify_enabled = false;
boot_keyboard_notify_enabled = false;
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
in_flight.active = false;
ctx.active_conn = NULL;
ctx.secured = false;
ctx.keyboard_report_notify_enabled = false;
ctx.consumer_report_notify_enabled = false;
ctx.boot_keyboard_notify_enabled = false;
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
ctx.in_flight.active = false;
}
static bool handle_ble_peer_event(const struct ble_peer_event *event)
@@ -279,13 +306,13 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
switch (event->state) {
case PEER_STATE_CONNECTED:
if (active_conn != NULL) {
if (ctx.active_conn != NULL) {
return false;
}
active_conn = event->id;
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
ctx.active_conn = event->id;
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
submit_set_protocol_event(HID_TRANSPORT_BLE, ctx.protocol_mode);
err = bt_hids_connected(&hids_obj, event->id);
if (err) {
LOG_ERR("bt_hids_connected failed (%d)", err);
@@ -294,16 +321,16 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
return false;
case PEER_STATE_SECURED:
if (active_conn != event->id) {
if (ctx.active_conn != event->id) {
return false;
}
secured = true;
ctx.secured = true;
submit_ble_transport_state_event();
return false;
case PEER_STATE_DISCONNECTED:
if (active_conn != event->id) {
if (ctx.active_conn != event->id) {
return false;
}
@@ -325,34 +352,35 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
{
int err;
if (!running || (event->channel != HID_SEND_CH_BLE_SHARED) ||
in_flight.active) {
if (!module_lifecycle_is_running(&ctx.lc) ||
(event->channel != HID_SEND_CH_BLE_SHARED) ||
ctx.in_flight.active) {
return false;
}
if ((active_conn == NULL) || !secured) {
if ((ctx.active_conn == NULL) || !ctx.secured) {
return false;
}
if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
if (event->protocol_mode != protocol_mode) {
if (event->protocol_mode != ctx.protocol_mode) {
LOG_WRN("Drop BLE keys report due to protocol mismatch");
return false;
}
in_flight.active = true;
in_flight.report_type = event->report_type;
in_flight.sequence = event->sequence;
ctx.in_flight.active = true;
ctx.in_flight.report_type = event->report_type;
ctx.in_flight.sequence = event->sequence;
if (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
if (ctx.protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
err = bt_hids_boot_kb_inp_rep_send(&hids_obj,
active_conn,
ctx.active_conn,
event->dyndata.data,
(uint8_t)event->dyndata.size,
hid_report_complete_cb);
} else {
err = bt_hids_inp_rep_send(&hids_obj,
active_conn,
ctx.active_conn,
BLE_HID_KEYS_REPORT_IDX,
event->dyndata.data,
(uint8_t)event->dyndata.size,
@@ -360,7 +388,7 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
}
if (err) {
in_flight.active = false;
ctx.in_flight.active = false;
LOG_WRN("BLE keyboard report submit failed (%d)", err);
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
KEYBOARD_REPORT_TYPE_KEYS,
@@ -371,23 +399,23 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
}
if (event->report_type == KEYBOARD_REPORT_TYPE_CONSUMER) {
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_REPORT) {
if (ctx.protocol_mode != KEYBOARD_PROTOCOL_MODE_REPORT) {
LOG_WRN("Drop BLE consumer report in boot mode");
return false;
}
in_flight.active = true;
in_flight.report_type = event->report_type;
in_flight.sequence = event->sequence;
ctx.in_flight.active = true;
ctx.in_flight.report_type = event->report_type;
ctx.in_flight.sequence = event->sequence;
err = bt_hids_inp_rep_send(&hids_obj,
active_conn,
ctx.active_conn,
BLE_HID_CONSUMER_REPORT_IDX,
event->dyndata.data,
(uint8_t)event->dyndata.size,
hid_report_complete_cb);
if (err) {
in_flight.active = false;
ctx.in_flight.active = false;
LOG_WRN("BLE consumer report submit failed (%d)", err);
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
KEYBOARD_REPORT_TYPE_CONSUMER,
@@ -410,48 +438,25 @@ static bool app_event_handler(const struct app_event_header *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);
} else {
module_set_state(MODULE_STATE_READY);
}
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
}
return false;
}
if (is_power_down_event(aeh)) {
if (initialized) {
module_pause();
module_set_state(MODULE_STATE_STANDBY);
if (module_lifecycle_is_initialized(&ctx.lc)) {
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
}
return false;
}
if (is_wake_up_event(aeh)) {
if (initialized) {
int err = module_start();
if (err) {
module_set_state(MODULE_STATE_ERROR);
} else {
module_set_state(MODULE_STATE_READY);
}
if (module_lifecycle_is_initialized(&ctx.lc)) {
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
}
return false;