feat: 添加模块生命周期管理框架并重构现有模块
添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机, 包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID 模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。 提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
@@ -9,14 +9,40 @@
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "module_lifecycle.h"
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_adv_suspended = true;
|
||||
static bool usb_enabled;
|
||||
struct mode_policy_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
bool ble_adv_suspended;
|
||||
bool usb_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 mode_policy_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.ble_adv_suspended = true,
|
||||
};
|
||||
|
||||
static void broadcast_ble_adv_req(bool suspend)
|
||||
{
|
||||
@@ -40,14 +66,14 @@ static void apply_mode_policy(enum mode_switch_mode mode)
|
||||
bool should_suspend_ble_adv = (mode != MODE_SWITCH_BLE);
|
||||
bool should_enable_usb = (mode == MODE_SWITCH_USB);
|
||||
|
||||
if (should_suspend_ble_adv != ble_adv_suspended) {
|
||||
ble_adv_suspended = should_suspend_ble_adv;
|
||||
if (should_suspend_ble_adv != ctx.ble_adv_suspended) {
|
||||
ctx.ble_adv_suspended = should_suspend_ble_adv;
|
||||
broadcast_ble_adv_req(should_suspend_ble_adv);
|
||||
}
|
||||
|
||||
if (should_enable_usb != usb_enabled) {
|
||||
usb_enabled = should_enable_usb;
|
||||
if (usb_enabled) {
|
||||
if (should_enable_usb != ctx.usb_enabled) {
|
||||
ctx.usb_enabled = should_enable_usb;
|
||||
if (ctx.usb_enabled) {
|
||||
struct module_resume_req_event *event =
|
||||
new_module_resume_req_event();
|
||||
|
||||
@@ -65,26 +91,25 @@ static void apply_mode_policy(enum mode_switch_mode mode)
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
ble_adv_suspended = true;
|
||||
usb_enabled = false;
|
||||
ctx.ble_adv_suspended = true;
|
||||
ctx.usb_enabled = 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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
running = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
@@ -92,7 +117,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
apply_mode_policy(event->mode);
|
||||
}
|
||||
|
||||
@@ -103,47 +128,23 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
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