feat: 添加模块生命周期管理框架并重构现有模块
添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机, 包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID 模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。 提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
|
||||
#include <caf/events/power_manager_event.h>
|
||||
|
||||
#include "module_lifecycle.h"
|
||||
#include "usb_function_hook.h"
|
||||
#include "usb_control_event.h"
|
||||
#include "usb_state_event.h"
|
||||
@@ -40,9 +41,6 @@ static const char *const class_blocklist[] = {
|
||||
NULL,
|
||||
};
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
|
||||
enum usb_stack_state {
|
||||
USB_STACK_UNINITIALIZED = 0,
|
||||
USB_STACK_DISABLED,
|
||||
@@ -58,11 +56,35 @@ enum usb_bus_state {
|
||||
};
|
||||
|
||||
struct usb_owner_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
enum usb_stack_state stack;
|
||||
enum usb_bus_state bus;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
/* Project exception: mode policy suspend/resume controls USB availability,
|
||||
* while global power events still map to standby lifecycle transitions.
|
||||
*/
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_SUSPEND,
|
||||
.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_owner_ctx usb_ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.stack = USB_STACK_UNINITIALIZED,
|
||||
.bus = USB_BUS_DISCONNECTED,
|
||||
};
|
||||
@@ -363,7 +385,7 @@ static int usb_stack_disable_if_needed(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
usb_ctx.stack = USB_STACK_UNINITIALIZED;
|
||||
usb_ctx.bus = USB_BUS_DISCONNECTED;
|
||||
@@ -372,25 +394,25 @@ static int module_init(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&usb_ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&usb_ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = false;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool handle_module_state_event(const struct module_state_event *event)
|
||||
@@ -399,26 +421,7 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!initialized) {
|
||||
int err = module_init();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
if (!running) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_RUNNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -426,38 +429,38 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
static bool handle_module_suspend_req_event(
|
||||
const struct module_suspend_req_event *event)
|
||||
{
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) || !running) {
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) ||
|
||||
(usb_ctx.lc.state != LC_RUNNING)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = usb_stack_disable_if_needed();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
LOG_WRN("USB suspend request failed (%d)", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_SUSPENDED);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_module_resume_req_event(
|
||||
const struct module_resume_req_event *event)
|
||||
{
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) || !running) {
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) ||
|
||||
(usb_ctx.lc.state != LC_SUSPENDED)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = usb_stack_enable_if_needed();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
LOG_WRN("USB resume request failed (%d)", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_RUNNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -479,23 +482,16 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&usb_ctx.lc)) {
|
||||
(void)module_set_lifecycle(&usb_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 if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&usb_ctx.lc)) {
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user