feat(mode_switch): 添加无效模式枚举值并更新事件处理

添加 MODE_SWITCH_INVALID 枚举值到 mode_switch_mode 中,
用于表示无效的模式状态。同时更新相关的事件处理逻辑,
确保在模式切换时能够正确处理无效模式的情况。

refactor(mode_policy): 简化模块上下文结构并优化模式策略

将模块上下文中的 ble_adv_suspended 和 usb_enabled 标志位
替换为 active_mode 枚举值,简化了数据结构。重新设计了
模式策略应用逻辑,使其更清晰易懂,并改进了BLE和USB设备
的启用/暂停控制流程。

refactor(usb_device): 重构USB设备生命周期管理

移除了 USB_STACK_DISABLED 状态,简化了USB栈的状态管理。
改进了USB设备的启动和停止逻辑,更好地与模块生命周期
集成。现在USB状态事件仅在模块初始化后提交,避免了
不必要的事件发布。

feat(logging): 增加详细的状态转换日志记录

为BLE NUS模块添加了业务状态转换的日志记录功能,
增加了详细的错误和警告日志,包括生命周期状态、
业务状态和连接信息,便于调试和问题排查。

refactor(mode_switch): 优化模式检测和报告机制

修改了模式切换采样的判断逻辑,移除了force_report和
mode_valid标志位,改用last_mode状态来决定是否提交
模式切换事件,使代码更简洁且易于理解。
This commit is contained in:
2026-04-18 11:27:48 +08:00
parent ceebaaa600
commit caf8d5acc6
8 changed files with 292 additions and 159 deletions

View File

@@ -16,8 +16,7 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
struct mode_policy_module_ctx {
struct module_lifecycle_ctx lc;
bool ble_adv_suspended;
bool usb_enabled;
enum mode_switch_mode active_mode;
};
static int do_init(void);
@@ -41,69 +40,115 @@ static struct mode_policy_module_ctx ctx = {
.cfg = &lifecycle_cfg,
.ops = &lifecycle_ops,
},
.ble_adv_suspended = true,
.active_mode = MODE_SWITCH_USB,
};
static void broadcast_ble_adv_req(bool suspend)
static void mode_policy_set_ble(bool enable)
{
if (suspend) {
struct module_suspend_req_event *event = new_module_suspend_req_event();
event->sink_module_id = MODULE_ID(ble_adv);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
} else {
if (enable) {
struct module_resume_req_event *event = new_module_resume_req_event();
event->sink_module_id = MODULE_ID(ble_adv);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
} else {
struct module_suspend_req_event *event = new_module_suspend_req_event();
event->sink_module_id = MODULE_ID(ble_adv);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
}
}
static void apply_mode_policy(enum mode_switch_mode mode)
static void mode_policy_set_usb(bool enable)
{
bool should_suspend_ble_adv = (mode != MODE_SWITCH_BLE);
bool should_enable_usb = (mode == MODE_SWITCH_USB);
if (enable) {
struct module_resume_req_event *event = new_module_resume_req_event();
if (should_suspend_ble_adv != ctx.ble_adv_suspended) {
ctx.ble_adv_suspended = should_suspend_ble_adv;
broadcast_ble_adv_req(should_suspend_ble_adv);
event->sink_module_id = MODULE_ID(usb_device_module);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
} else {
struct module_suspend_req_event *event = new_module_suspend_req_event();
event->sink_module_id = MODULE_ID(usb_device_module);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
}
}
if (should_enable_usb != ctx.usb_enabled) {
ctx.usb_enabled = should_enable_usb;
if (ctx.usb_enabled) {
struct module_resume_req_event *event =
new_module_resume_req_event();
static void apply_active_mode(void)
{
switch (ctx.active_mode) {
case MODE_SWITCH_BLE:
mode_policy_set_ble(true);
mode_policy_set_usb(false);
break;
event->sink_module_id = MODULE_ID(usb_device_module);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
} else {
struct module_suspend_req_event *event =
new_module_suspend_req_event();
case MODE_SWITCH_USB:
mode_policy_set_ble(false);
mode_policy_set_usb(true);
break;
event->sink_module_id = MODULE_ID(usb_device_module);
event->src_module_id = MODULE_ID(MODULE);
APP_EVENT_SUBMIT(event);
}
case MODE_SWITCH_24G:
mode_policy_set_ble(false);
mode_policy_set_usb(false);
break;
default:
break;
}
}
static void apply_ble_mode_policy(void)
{
switch (ctx.active_mode) {
case MODE_SWITCH_BLE:
mode_policy_set_ble(true);
break;
case MODE_SWITCH_USB:
mode_policy_set_ble(false);
break;
case MODE_SWITCH_24G:
mode_policy_set_ble(false);
break;
default:
break;
}
}
static void apply_usb_mode_policy(void)
{
switch (ctx.active_mode) {
case MODE_SWITCH_BLE:
mode_policy_set_usb(false);
break;
case MODE_SWITCH_USB:
mode_policy_set_usb(true);
break;
case MODE_SWITCH_24G:
mode_policy_set_usb(false);
break;
default:
break;
}
}
static int do_init(void)
{
ctx.ble_adv_suspended = true;
ctx.usb_enabled = false;
ctx.active_mode = MODE_SWITCH_USB;
return 0;
}
static int do_start(void)
{
if (module_lifecycle_is_running(&ctx.lc)) {
return 0;
}
apply_active_mode();
return 0;
}
@@ -118,7 +163,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
if (module_lifecycle_is_running(&ctx.lc)) {
apply_mode_policy(event->mode);
ctx.active_mode = event->mode;
apply_active_mode();
}
return false;
@@ -129,6 +175,21 @@ static bool app_event_handler(const struct app_event_header *aeh)
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
return false;
}
if (check_state(event, MODULE_ID(ble_adv), MODULE_STATE_READY)) {
if (module_lifecycle_is_running(&ctx.lc)) {
apply_ble_mode_policy();
}
return false;
}
if (check_state(event, MODULE_ID(usb_device_module),
MODULE_STATE_READY)) {
if (module_lifecycle_is_running(&ctx.lc)) {
apply_usb_mode_policy();
}
}
return false;