- 添加了模块生命周期状态转换验证函数,包括目标状态允许性检查和路径允许性检查 - 实现了模块操作验证功能,确保必要的回调函数存在 - 更新了状态报告逻辑,增加了模式检查以防止无效状态转换 - 修改了生命周期转换流程,区分运行启动和停止操作 - 优化了错误处理机制,返回适当的错误码 refactor(ble_modules): 简化BLE模块生命周期配置 - 将多个BLE模块(lifecycle_cfg)的模式从ML_MODE_POWER改为ML_MODE_NONE - 移除了power_event相关依赖和事件订阅 - 更新了BLE BAS模块的状态转换逻辑 - 简化了BLE HID/NUS/Protocol模块的电源管理相关代码 fix(mode_switch): 修复唤醒后模式恢复功能 - 在唤醒事件处理中添加了最后模式的恢复逻辑 - 确保设备唤醒后能够重新提交之前的模式切换事件
203 lines
4.0 KiB
C
203 lines
4.0 KiB
C
#include <stdbool.h>
|
|
|
|
#include <app_event_manager.h>
|
|
|
|
#define MODULE mode_policy_module
|
|
#include <caf/events/module_state_event.h>
|
|
#include <caf/events/module_suspend_event.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
#include "module_lifecycle.h"
|
|
#include "mode_switch_event.h"
|
|
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|
|
|
struct mode_policy_module_ctx {
|
|
struct module_lifecycle_ctx lc;
|
|
enum mode_switch_mode active_mode;
|
|
};
|
|
|
|
static int do_init(void);
|
|
static int do_start(void);
|
|
static int do_stop(void);
|
|
|
|
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
|
.mode = ML_MODE_NONE,
|
|
.stopped_state = MODULE_STATE_OFF,
|
|
};
|
|
|
|
static const struct module_lifecycle_ops lifecycle_ops = {
|
|
.do_init = do_init,
|
|
.do_start = do_start,
|
|
.do_stop = do_stop,
|
|
};
|
|
|
|
static struct mode_policy_module_ctx ctx = {
|
|
.lc = {
|
|
.state = LC_UNINIT,
|
|
.cfg = &lifecycle_cfg,
|
|
.ops = &lifecycle_ops,
|
|
},
|
|
.active_mode = MODE_SWITCH_USB,
|
|
};
|
|
|
|
static void mode_policy_set_ble(bool enable)
|
|
{
|
|
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 mode_policy_set_usb(bool enable)
|
|
{
|
|
if (enable) {
|
|
struct module_resume_req_event *event = new_module_resume_req_event();
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
case MODE_SWITCH_USB:
|
|
mode_policy_set_ble(false);
|
|
mode_policy_set_usb(true);
|
|
break;
|
|
|
|
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.active_mode = MODE_SWITCH_USB;
|
|
return 0;
|
|
}
|
|
|
|
static int do_start(void)
|
|
{
|
|
apply_active_mode();
|
|
return 0;
|
|
}
|
|
|
|
static int do_stop(void)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static bool app_event_handler(const struct app_event_header *aeh)
|
|
{
|
|
if (is_mode_switch_event(aeh)) {
|
|
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
|
|
|
if (module_lifecycle_is_running(&ctx.lc)) {
|
|
ctx.active_mode = event->mode;
|
|
apply_active_mode();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
if (is_module_state_event(aeh)) {
|
|
const struct module_state_event *event = cast_module_state_event(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;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|