From 0a1357c62de1b05335b6cdb3273a7e872acdfa57 Mon Sep 17 00:00:00 2001 From: skiinder Date: Tue, 21 Apr 2026 14:34:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(module=5Flifecycle):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E6=A8=A1=E5=9D=97=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除了LC_SUSPENDED状态并简化了生命周期状态转换逻辑, 将状态名称查询功能统一到module_lifecycle_name函数中, 删除了不再需要的状态检查和路径验证函数, 同时更新了相关的配置文件和模块调用以适配新的API。 --- inc/module_lifecycle.h | 173 +++++++++++++--------------------------- prj.conf | 3 + src/ble_nus_module.c | 22 +---- src/protocol_module.c | 24 +----- src/usb_device_module.c | 4 +- 5 files changed, 64 insertions(+), 162 deletions(-) diff --git a/inc/module_lifecycle.h b/inc/module_lifecycle.h index 8af1efe..8684d6e 100644 --- a/inc/module_lifecycle.h +++ b/inc/module_lifecycle.h @@ -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: diff --git a/prj.conf b/prj.conf index 66bbfb2..e0ba398 100644 --- a/prj.conf +++ b/prj.conf @@ -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 \ No newline at end of file diff --git a/src/ble_nus_module.c b/src/ble_nus_module.c index 526ad7b..f856db5 100644 --- a/src/ble_nus_module.c +++ b/src/ble_nus_module.c @@ -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; diff --git a/src/protocol_module.c b/src/protocol_module.c index 66a7f6f..571b74b 100644 --- a/src/protocol_module.c +++ b/src/protocol_module.c @@ -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); } diff --git a/src/usb_device_module.c b/src/usb_device_module.c index 425b614..2066b57 100644 --- a/src/usb_device_module.c +++ b/src/usb_device_module.c @@ -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; }