feat: 添加模块生命周期管理框架并重构现有模块
添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机, 包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID 模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。 提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
#include "bat_state_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -32,17 +33,43 @@ BUILD_ASSERT(DT_NODE_HAS_STATUS(VBATT_NODE, okay),
|
||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(IP5306_NODE, okay),
|
||||
"Missing ip5306 node in devicetree");
|
||||
|
||||
static const struct device *const vbatt_dev = DEVICE_DT_GET(VBATT_NODE);
|
||||
static const struct device *const ip5306_dev = DEVICE_DT_GET(IP5306_NODE);
|
||||
static struct k_work_delayable battery_sample_work;
|
||||
static struct {
|
||||
bool valid;
|
||||
uint8_t soc;
|
||||
bool charging;
|
||||
bool full;
|
||||
} last_bat_state;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
struct battery_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
const struct device *vbatt_dev;
|
||||
const struct device *ip5306_dev;
|
||||
struct k_work_delayable battery_sample_work;
|
||||
struct {
|
||||
bool valid;
|
||||
uint8_t soc;
|
||||
bool charging;
|
||||
bool full;
|
||||
} last_bat_state;
|
||||
};
|
||||
|
||||
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 battery_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.vbatt_dev = DEVICE_DT_GET(VBATT_NODE),
|
||||
.ip5306_dev = DEVICE_DT_GET(IP5306_NODE),
|
||||
};
|
||||
|
||||
static int sensor_value_to_mv(const struct sensor_value *value)
|
||||
{
|
||||
@@ -53,7 +80,7 @@ static int measurement_enable(bool enable)
|
||||
{
|
||||
enum pm_device_action action = enable ? PM_DEVICE_ACTION_RESUME
|
||||
: PM_DEVICE_ACTION_SUSPEND;
|
||||
int err = pm_device_action_run(vbatt_dev, action);
|
||||
int err = pm_device_action_run(ctx.vbatt_dev, action);
|
||||
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_ERR("Cannot %s vbatt sensor (%d)", enable ? "resume" : "suspend", err);
|
||||
@@ -90,23 +117,23 @@ static void battery_sample_fn(struct k_work *work)
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_sample_fetch(vbatt_dev);
|
||||
err = sensor_sample_fetch(ctx.vbatt_dev);
|
||||
if (err) {
|
||||
LOG_WRN("Battery sample fetch failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
||||
err = sensor_channel_get(ctx.vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
||||
if (err) {
|
||||
LOG_WRN("Battery channel get failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = ip5306_get_status(ip5306_dev, &pmic_status);
|
||||
err = ip5306_get_status(ctx.ip5306_dev, &pmic_status);
|
||||
if (err) {
|
||||
LOG_WRN("IP5306 status read failed (%d)", err);
|
||||
goto reschedule;
|
||||
@@ -115,53 +142,53 @@ static void battery_sample_fn(struct k_work *work)
|
||||
voltage_mv = sensor_value_to_mv(&voltage);
|
||||
uint8_t soc = battery_soc_from_mv(voltage_mv);
|
||||
|
||||
if (!last_bat_state.valid ||
|
||||
(last_bat_state.soc != soc) ||
|
||||
(last_bat_state.charging != pmic_status.charging) ||
|
||||
(last_bat_state.full != pmic_status.full)) {
|
||||
last_bat_state.valid = true;
|
||||
last_bat_state.soc = soc;
|
||||
last_bat_state.charging = pmic_status.charging;
|
||||
last_bat_state.full = pmic_status.full;
|
||||
if (!ctx.last_bat_state.valid ||
|
||||
(ctx.last_bat_state.soc != soc) ||
|
||||
(ctx.last_bat_state.charging != pmic_status.charging) ||
|
||||
(ctx.last_bat_state.full != pmic_status.full)) {
|
||||
ctx.last_bat_state.valid = true;
|
||||
ctx.last_bat_state.soc = soc;
|
||||
ctx.last_bat_state.charging = pmic_status.charging;
|
||||
ctx.last_bat_state.full = pmic_status.full;
|
||||
submit_bat_state_event(soc, pmic_status.charging, pmic_status.full);
|
||||
}
|
||||
|
||||
reschedule:
|
||||
if (running) {
|
||||
k_work_reschedule(&battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
k_work_reschedule(&ctx.battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
if (!device_is_ready(vbatt_dev)) {
|
||||
if (!device_is_ready(ctx.vbatt_dev)) {
|
||||
LOG_ERR("vbatt device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_ready(ip5306_dev)) {
|
||||
if (!device_is_ready(ctx.ip5306_dev)) {
|
||||
LOG_ERR("ip5306 device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int err = ip5306_init(ip5306_dev);
|
||||
int err = ip5306_init(ctx.ip5306_dev);
|
||||
if (err) {
|
||||
LOG_ERR("ip5306 init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
||||
memset(&last_bat_state, 0, sizeof(last_bat_state));
|
||||
k_work_init_delayable(&ctx.battery_sample_work, battery_sample_fn);
|
||||
memset(&ctx.last_bat_state, 0, sizeof(ctx.last_bat_state));
|
||||
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -170,21 +197,21 @@ static int module_start(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
running = true;
|
||||
k_work_reschedule(&battery_sample_work, K_NO_WAIT);
|
||||
k_work_reschedule(&ctx.battery_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(&battery_sample_work);
|
||||
(void)k_work_cancel_delayable(&ctx.battery_sample_work);
|
||||
(void)measurement_enable(false);
|
||||
running = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
@@ -193,47 +220,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