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

@@ -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;
}