feat(usb): 简化USB状态管理并引入模式策略模块
- 修改usb_state_event结构,将复杂的flag操作简化为单一的state枚举值 - 新增usb_function_hook机制用于USB功能预初始化 - 将ble_adv_ctrl_module重命名为mode_policy_module以更好地反映其功能 - 在mode_policy_module中添加USB设备启用/暂停控制逻辑 - 添加对电源事件的处理支持休眠/唤醒功能 - 更新CMakeLists.txt添加必要的链接器脚本和源文件 - 移除不再需要的ble_adv_ctrl_module并添加新的mode_policy_module
This commit is contained in:
159
src/mode_policy_module.c
Normal file
159
src/mode_policy_module.c
Normal file
@@ -0,0 +1,159 @@
|
||||
#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 <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_adv_suspended = true;
|
||||
static bool usb_enabled;
|
||||
|
||||
static void broadcast_ble_adv_req(bool suspend)
|
||||
{
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_mode_policy(enum mode_switch_mode mode)
|
||||
{
|
||||
bool should_suspend_ble_adv = (mode != MODE_SWITCH_BLE);
|
||||
bool should_enable_usb = (mode == MODE_SWITCH_USB);
|
||||
|
||||
if (should_suspend_ble_adv != ble_adv_suspended) {
|
||||
ble_adv_suspended = should_suspend_ble_adv;
|
||||
broadcast_ble_adv_req(should_suspend_ble_adv);
|
||||
}
|
||||
|
||||
if (should_enable_usb != usb_enabled) {
|
||||
usb_enabled = should_enable_usb;
|
||||
if (usb_enabled) {
|
||||
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 int module_init(void)
|
||||
{
|
||||
ble_adv_suspended = true;
|
||||
usb_enabled = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
|
||||
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 (running) {
|
||||
apply_mode_policy(event->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)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
Reference in New Issue
Block a user