refactor(module_lifecycle): 重构模块生命周期管理实现

移除了LC_SUSPENDED状态并简化了生命周期状态转换逻辑,
将状态名称查询功能统一到module_lifecycle_name函数中,
删除了不再需要的状态检查和路径验证函数,
同时更新了相关的配置文件和模块调用以适配新的API。
This commit is contained in:
2026-04-21 14:34:08 +08:00
parent e0817a7b44
commit 0a1357c62d
5 changed files with 64 additions and 162 deletions

View File

@@ -8,9 +8,8 @@
enum module_lifecycle {
LC_UNINIT,
LC_RUNNING,
LC_STOPPED,
LC_SUSPENDED,
LC_RUNNING,
LC_ERROR,
};
@@ -49,51 +48,19 @@ static inline bool module_lifecycle_is_initialized(
return (ctx->state != LC_UNINIT) && (ctx->state != LC_ERROR);
}
static inline bool module_lifecycle_reports_stopped_state(
const struct module_lifecycle_ctx *ctx)
static inline const char *module_lifecycle_name(enum module_lifecycle state)
{
return ctx->cfg->mode == ML_MODE_POWER;
}
static inline bool module_lifecycle_target_allowed(
const struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
switch (target) {
case LC_RUNNING:
case LC_ERROR:
return true;
case LC_STOPPED:
return ctx->cfg->mode == ML_MODE_POWER;
case LC_SUSPENDED:
return ctx->cfg->mode == ML_MODE_SUSPEND;
switch (state) {
case LC_UNINIT:
default:
return false;
}
}
static inline bool module_lifecycle_path_allowed(
enum module_lifecycle current,
enum module_lifecycle target)
{
switch (current) {
case LC_UNINIT:
return (target == LC_RUNNING) || (target == LC_STOPPED);
case LC_RUNNING:
return (target == LC_STOPPED) || (target == LC_SUSPENDED);
return "UNINIT";
case LC_STOPPED:
case LC_SUSPENDED:
return target == LC_RUNNING;
return "STOPPED";
case LC_RUNNING:
return "RUNNING";
case LC_ERROR:
return "ERROR";
default:
return false;
return "?";
}
}
@@ -130,21 +97,22 @@ static inline int module_lifecycle_report_state(
return 0;
case LC_STOPPED:
if (ctx->cfg->mode != ML_MODE_POWER) {
return -EINVAL;
}
switch (ctx->cfg->mode) {
case ML_MODE_POWER:
module_set_state(ctx->cfg->stopped_state);
return 0;
case LC_SUSPENDED:
if (ctx->cfg->mode != ML_MODE_SUSPEND) {
return -EINVAL;
}
case ML_MODE_SUSPEND:
module_set_state(MODULE_STATE_SUSPENDED);
return 0;
case ML_MODE_NONE:
return 0;
default:
return -EINVAL;
}
case LC_ERROR:
module_set_state(MODULE_STATE_ERROR);
return 0;
@@ -163,59 +131,6 @@ static inline int module_lifecycle_fail(struct module_lifecycle_ctx *ctx, int er
return err ? err : -EIO;
}
static inline int module_lifecycle_do_init(struct module_lifecycle_ctx *ctx)
{
int err = ctx->ops->do_init();
if (err) {
return module_lifecycle_fail(ctx, err);
}
return 0;
}
static inline int module_lifecycle_finish_transition(
struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
ctx->state = target;
return module_lifecycle_report_state(ctx, target);
}
static inline int module_lifecycle_start_running(struct module_lifecycle_ctx *ctx)
{
int err = 0;
if (ctx->ops->do_start != NULL) {
err = ctx->ops->do_start();
if (err) {
return module_lifecycle_fail(ctx, err);
}
}
return module_lifecycle_finish_transition(ctx, LC_RUNNING);
}
static inline int module_lifecycle_stop_running(
struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
int err;
if (ctx->ops->do_stop == NULL) {
return module_lifecycle_fail(ctx, -EINVAL);
}
err = ctx->ops->do_stop();
if (err) {
return module_lifecycle_fail(ctx, err);
}
return module_lifecycle_finish_transition(ctx, target);
}
static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
enum module_lifecycle target)
{
@@ -238,30 +153,50 @@ static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
return module_lifecycle_fail(ctx, -EIO);
}
if (!module_lifecycle_target_allowed(ctx, target) ||
!module_lifecycle_path_allowed(ctx->state, target)) {
if ((target != LC_STOPPED) && (target != LC_RUNNING)) {
return -EPERM;
}
switch (ctx->state) {
case LC_UNINIT:
err = module_lifecycle_do_init(ctx);
err = ctx->ops->do_init();
if (err) {
return err;
return module_lifecycle_fail(ctx, err);
}
if (target == LC_RUNNING) {
return module_lifecycle_start_running(ctx);
ctx->state = LC_STOPPED;
err = module_lifecycle_report_state(ctx, LC_STOPPED);
if (err) {
return module_lifecycle_fail(ctx, err);
}
return module_lifecycle_finish_transition(ctx, LC_STOPPED);
case LC_RUNNING:
return module_lifecycle_stop_running(ctx, target);
return module_set_lifecycle(ctx, target);
case LC_STOPPED:
case LC_SUSPENDED:
return module_lifecycle_start_running(ctx);
if (target != LC_RUNNING) {
return -EPERM;
}
err = ctx->ops->do_start ? ctx->ops->do_start() : 0;
if (err) {
return module_lifecycle_fail(ctx, err);
}
ctx->state = LC_RUNNING;
return module_lifecycle_report_state(ctx, LC_RUNNING);
case LC_RUNNING:
if (target != LC_STOPPED) {
return -EPERM;
}
err = ctx->ops->do_stop ? ctx->ops->do_stop() : 0;
if (err) {
return module_lifecycle_fail(ctx, err);
}
ctx->state = LC_STOPPED;
return module_lifecycle_report_state(ctx, LC_STOPPED);
case LC_ERROR:
default:

View File

@@ -130,3 +130,6 @@ CONFIG_LV_USE_LABEL=y
CONFIG_LV_FONT_MONTSERRAT_14=y
CONFIG_LV_FONT_MONTSERRAT_32=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_USE_SEGGER_RTT=y
CONFIG_SPEED_OPTIMIZATIONS=y

View File

@@ -57,24 +57,6 @@ static struct ble_nus_ctx ctx = {
.active_conn = NULL,
};
static const char *lifecycle_name(enum module_lifecycle state)
{
switch (state) {
case LC_UNINIT:
return "UNINIT";
case LC_RUNNING:
return "RUNNING";
case LC_STOPPED:
return "STOPPED";
case LC_SUSPENDED:
return "SUSPENDED";
case LC_ERROR:
return "ERROR";
default:
return "?";
}
}
static const char *business_state_name(enum ble_nus_business_state state)
{
switch (state) {
@@ -185,7 +167,7 @@ static void received(struct bt_conn *conn, const void *data, uint16_t len,
if (!lifecycle_is_ready() || (ctx.business == BLE_NUS_STACK_OFFLINE) ||
(conn != ctx.active_conn)) {
LOG_WRN("BLE NUS drop RX len:%u lc:%s business:%s active_conn:%p conn:%p link:%s",
len, lifecycle_name(ctx.lc.state),
len, module_lifecycle_name(ctx.lc.state),
business_state_name(ctx.business),
(void *)ctx.active_conn, (void *)conn,
link_state_name(transport_link_state_get()));
@@ -245,7 +227,7 @@ static int apply_lifecycle(enum module_lifecycle target)
state_reconcile(old_lifecycle, old_business);
} else {
LOG_WRN("BLE NUS lifecycle change failed target:%s err:%d",
lifecycle_name(target), err);
module_lifecycle_name(target), err);
}
return err;

View File

@@ -74,24 +74,6 @@ static struct protocol_module_ctx ctx = {
#define session_state ctx.session_state
static const char *lifecycle_name(enum module_lifecycle state)
{
switch (state) {
case LC_UNINIT:
return "UNINIT";
case LC_RUNNING:
return "RUNNING";
case LC_STOPPED:
return "STOPPED";
case LC_SUSPENDED:
return "SUSPENDED";
case LC_ERROR:
return "ERROR";
default:
return "?";
}
}
static const char *proto_session_name(enum proto_session_state state)
{
switch (state) {
@@ -275,7 +257,7 @@ int protocol_module_process_message(enum proto_transport transport,
if (!module_lifecycle_is_running(&ctx.lc)) {
LOG_WRN("Reject proto msg transport:%s len:%u lc:%s",
proto_transport_name(transport), (uint32_t)req_payload_len,
lifecycle_name(ctx.lc.state));
module_lifecycle_name(ctx.lc.state));
return -EAGAIN;
}
@@ -290,7 +272,7 @@ int protocol_module_process_message(enum proto_transport transport,
LOG_WRN("Reject HelloReq transport:%s session:%s lc:%s",
proto_transport_name(transport),
proto_session_name(session_state[transport]),
lifecycle_name(ctx.lc.state));
module_lifecycle_name(ctx.lc.state));
return -EAGAIN;
}
@@ -407,7 +389,7 @@ static bool handle_proto_rx_event(const struct proto_rx_event *event)
LOG_WRN("Protocol processing failed (%d) transport:%s session:%s lc:%s len:%u",
err, proto_transport_name(event->transport),
proto_session_name(session_state[event->transport]),
lifecycle_name(ctx.lc.state),
module_lifecycle_name(ctx.lc.state),
(uint32_t)event->dyndata.size);
}

View File

@@ -391,7 +391,7 @@ static bool handle_module_suspend_req_event(
return false;
}
if (module_set_lifecycle(&usb_ctx.lc, LC_SUSPENDED) == 0) {
if (module_set_lifecycle(&usb_ctx.lc, LC_STOPPED) == 0) {
submit_usb_state(usb_public_state_get());
}
@@ -402,7 +402,7 @@ static bool handle_module_resume_req_event(
const struct module_resume_req_event *event)
{
if ((event->sink_module_id != MODULE_ID(MODULE)) ||
(usb_ctx.lc.state != LC_SUSPENDED)) {
(usb_ctx.lc.state != LC_STOPPED)) {
return false;
}