feat: 添加模块生命周期管理框架并重构现有模块
添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机, 包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID 模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。 提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
@@ -18,32 +18,57 @@
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "usb_function_hook.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static const struct device *const hid_dev =
|
||||
DEVICE_DT_GET(DT_NODELABEL(hid_consumer));
|
||||
|
||||
static const uint8_t consumer_report_desc[] = {
|
||||
0x05, 0x0C, 0x09, 0x01, 0xA1, 0x01, 0x15, 0x00,
|
||||
0x26, 0xFF, 0x03, 0x19, 0x00, 0x2A, 0xFF, 0x03,
|
||||
0x75, 0x10, 0x95, 0x01, 0x81, 0x00, 0xC0
|
||||
};
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool iface_ready;
|
||||
static bool report_in_flight;
|
||||
static uint16_t in_flight_sequence;
|
||||
struct usb_hid_consumer_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
const struct device *hid_dev;
|
||||
bool usb_active;
|
||||
bool iface_ready;
|
||||
bool report_in_flight;
|
||||
uint16_t in_flight_sequence;
|
||||
};
|
||||
|
||||
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 usb_hid_consumer_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.hid_dev = DEVICE_DT_GET(DT_NODELABEL(hid_consumer)),
|
||||
};
|
||||
|
||||
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
|
||||
static void publish_consumer_state(void)
|
||||
{
|
||||
bool ready = running && usb_active && iface_ready;
|
||||
bool ready = module_lifecycle_is_running(&ctx.lc) &&
|
||||
ctx.usb_active && ctx.iface_ready;
|
||||
|
||||
submit_hid_channel_state_event(HID_SEND_CH_USB_CONSUMER,
|
||||
ready ? BIT(KEYBOARD_REPORT_TYPE_CONSUMER) : 0U,
|
||||
@@ -54,12 +79,13 @@ static void consumer_iface_ready(const struct device *dev, const bool ready)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
iface_ready = ready;
|
||||
ctx.iface_ready = ready;
|
||||
if (!ready) {
|
||||
report_in_flight = false;
|
||||
ctx.report_in_flight = false;
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", hid_dev->name, ready ? "ready" : "not ready");
|
||||
LOG_INF("%s interface %s", ctx.hid_dev->name,
|
||||
ready ? "ready" : "not ready");
|
||||
publish_consumer_state();
|
||||
}
|
||||
|
||||
@@ -117,10 +143,10 @@ static void consumer_input_report_done(const struct device *dev,
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(report);
|
||||
|
||||
report_in_flight = false;
|
||||
ctx.report_in_flight = false;
|
||||
submit_hid_report_sent_event(HID_SEND_CH_USB_CONSUMER,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
in_flight_sequence, false);
|
||||
ctx.in_flight_sequence, false);
|
||||
}
|
||||
|
||||
static void consumer_output_report(const struct device *dev,
|
||||
@@ -145,59 +171,59 @@ static const struct hid_device_ops consumer_ops = {
|
||||
|
||||
static int usb_hid_consumer_register_device(void)
|
||||
{
|
||||
if (!device_is_ready(hid_dev)) {
|
||||
LOG_ERR("HID device %s not ready", hid_dev->name);
|
||||
if (!device_is_ready(ctx.hid_dev)) {
|
||||
LOG_ERR("HID device %s not ready", ctx.hid_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return hid_device_register(hid_dev, consumer_report_desc,
|
||||
return hid_device_register(ctx.hid_dev, consumer_report_desc,
|
||||
sizeof(consumer_report_desc), &consumer_ops);
|
||||
}
|
||||
|
||||
USB_FUNCTION_HOOK_DEFINE(usb_hid_consumer_hook, usb_hid_consumer_register_device);
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
usb_active = false;
|
||||
iface_ready = false;
|
||||
report_in_flight = false;
|
||||
ctx.usb_active = false;
|
||||
ctx.iface_ready = false;
|
||||
ctx.report_in_flight = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
publish_consumer_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = false;
|
||||
report_in_flight = false;
|
||||
ctx.report_in_flight = false;
|
||||
publish_consumer_state();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_STATE_ACTIVE);
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
if (new_usb_active == ctx.usb_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_active = new_usb_active;
|
||||
if (!usb_active) {
|
||||
iface_ready = false;
|
||||
report_in_flight = false;
|
||||
ctx.usb_active = new_usb_active;
|
||||
if (!ctx.usb_active) {
|
||||
ctx.iface_ready = false;
|
||||
ctx.report_in_flight = false;
|
||||
}
|
||||
|
||||
publish_consumer_state();
|
||||
@@ -208,7 +234,7 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !usb_active ||
|
||||
if (!module_lifecycle_is_running(&ctx.lc) || !ctx.usb_active ||
|
||||
(event->channel != HID_SEND_CH_USB_CONSUMER)) {
|
||||
return false;
|
||||
}
|
||||
@@ -217,17 +243,17 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!iface_ready) {
|
||||
if (!ctx.iface_ready) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (report_in_flight) {
|
||||
if (ctx.report_in_flight) {
|
||||
LOG_WRN("Drop USB consumer report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(consumer_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
err = hid_device_submit_report(hid_dev, (uint16_t)event->dyndata.size,
|
||||
err = hid_device_submit_report(ctx.hid_dev, (uint16_t)event->dyndata.size,
|
||||
consumer_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||
@@ -235,8 +261,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
report_in_flight = true;
|
||||
in_flight_sequence = event->sequence;
|
||||
ctx.report_in_flight = true;
|
||||
ctx.in_flight_sequence = event->sequence;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -254,48 +280,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;
|
||||
|
||||
Reference in New Issue
Block a user