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) {
switch (ctx->cfg->mode) {
case ML_MODE_POWER:
module_set_state(ctx->cfg->stopped_state);
return 0;
case ML_MODE_SUSPEND:
module_set_state(MODULE_STATE_SUSPENDED);
return 0;
case ML_MODE_NONE:
return 0;
default:
return -EINVAL;
}
module_set_state(ctx->cfg->stopped_state);
return 0;
case LC_SUSPENDED:
if (ctx->cfg->mode != ML_MODE_SUSPEND) {
return -EINVAL;
}
module_set_state(MODULE_STATE_SUSPENDED);
return 0;
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: