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

@@ -14,6 +14,7 @@
#include <zephyr/logging/log.h>
#include <zephyr/pm/device.h>
#include "module_lifecycle.h"
#include "mode_switch_event.h"
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
@@ -26,15 +27,38 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
BUILD_ASSERT(DT_NODE_EXISTS(MODE_SWITCH_ADC_NODE),
"Missing mode_switch_adc node in devicetree");
static const struct device *const mode_switch_adc_dev =
DEVICE_DT_GET(MODE_SWITCH_ADC_NODE);
struct mode_switch_module_ctx {
struct module_lifecycle_ctx lc;
const struct device *mode_switch_adc_dev;
struct k_work_delayable mode_switch_sample_work;
bool force_report;
bool mode_valid;
enum mode_switch_mode last_mode;
};
static struct k_work_delayable mode_switch_sample_work;
static bool initialized;
static bool running;
static bool force_report;
static bool mode_valid;
static enum mode_switch_mode last_mode;
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_switch_module_ctx ctx = {
.lc = {
.state = LC_UNINIT,
.cfg = &lifecycle_cfg,
.ops = &lifecycle_ops,
},
.mode_switch_adc_dev = DEVICE_DT_GET(MODE_SWITCH_ADC_NODE),
};
static enum mode_switch_mode detect_mode(uint16_t voltage_mv)
{
@@ -53,7 +77,7 @@ static int mode_switch_enable(bool enable)
{
enum pm_device_action action = enable ? PM_DEVICE_ACTION_RESUME
: PM_DEVICE_ACTION_SUSPEND;
int err = pm_device_action_run(mode_switch_adc_dev, action);
int err = pm_device_action_run(ctx.mode_switch_adc_dev, action);
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
LOG_ERR("Cannot %s mode switch ADC (%d)",
@@ -71,17 +95,18 @@ static void mode_switch_sample_fn(struct k_work *work)
ARG_UNUSED(work);
if (!running) {
if (!module_lifecycle_is_running(&ctx.lc)) {
return;
}
err = sensor_sample_fetch(mode_switch_adc_dev);
err = sensor_sample_fetch(ctx.mode_switch_adc_dev);
if (err) {
LOG_WRN("Mode switch ADC sample fetch failed (%d)", err);
goto reschedule;
}
err = sensor_channel_get(mode_switch_adc_dev, SENSOR_CHAN_VOLTAGE, &voltage);
err = sensor_channel_get(ctx.mode_switch_adc_dev, SENSOR_CHAN_VOLTAGE,
&voltage);
if (err) {
LOG_WRN("Mode switch ADC channel get failed (%d)", err);
goto reschedule;
@@ -90,39 +115,40 @@ static void mode_switch_sample_fn(struct k_work *work)
uint16_t sample_mv = (uint16_t)((voltage.val1 * 1000) + (voltage.val2 / 1000));
enum mode_switch_mode mode = detect_mode(sample_mv);
if (force_report || !mode_valid || (mode != last_mode)) {
if (ctx.force_report || !ctx.mode_valid || (mode != ctx.last_mode)) {
submit_mode_switch_event(mode, sample_mv);
last_mode = mode;
mode_valid = true;
force_report = false;
ctx.last_mode = mode;
ctx.mode_valid = true;
ctx.force_report = false;
}
reschedule:
if (running) {
k_work_reschedule(&mode_switch_sample_work, MODE_SWITCH_SAMPLE_INTERVAL);
if (module_lifecycle_is_running(&ctx.lc)) {
k_work_reschedule(&ctx.mode_switch_sample_work,
MODE_SWITCH_SAMPLE_INTERVAL);
}
}
static int module_init(void)
static int do_init(void)
{
if (!device_is_ready(mode_switch_adc_dev)) {
if (!device_is_ready(ctx.mode_switch_adc_dev)) {
LOG_ERR("Mode switch ADC device not ready");
return -ENODEV;
}
k_work_init_delayable(&mode_switch_sample_work, mode_switch_sample_fn);
mode_valid = false;
force_report = false;
k_work_init_delayable(&ctx.mode_switch_sample_work, mode_switch_sample_fn);
ctx.mode_valid = false;
ctx.force_report = false;
return 0;
}
static int module_start(void)
static int do_start(void)
{
int err;
if (running) {
if (module_lifecycle_is_running(&ctx.lc)) {
return 0;
}
@@ -131,22 +157,22 @@ static int module_start(void)
return err;
}
running = true;
force_report = true;
k_work_reschedule(&mode_switch_sample_work, K_NO_WAIT);
ctx.force_report = true;
k_work_reschedule(&ctx.mode_switch_sample_work, K_NO_WAIT);
return 0;
}
static void module_pause(void)
static int do_stop(void)
{
if (!running) {
return;
if (!module_lifecycle_is_running(&ctx.lc)) {
return 0;
}
(void)k_work_cancel_delayable(&mode_switch_sample_work);
(void)k_work_cancel_delayable(&ctx.mode_switch_sample_work);
(void)mode_switch_enable(false);
running = false;
return 0;
}
static bool app_event_handler(const struct app_event_header *aeh)
@@ -155,47 +181,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;