refactor(events): 移除未使用的usb_hid_event定义
移除src/events/usb_hid_event.c和usb_hid_event.h文件, 以及CMakeLists.txt中的相关源文件引用。 feat(modules): 电池模块增加充电状态电源管理限制策略 - 添加充电状态下的电源限制逻辑,充电时限制到ALIVE级别禁止自动休眠, 非充电时恢复到SUSPENDED级别 - 默认非充电态允许进入SUSPENDED但禁止进入OFF refactor(modules): 重构usb_hid_module减少模块间耦合 - 移除对power_manager_event.h和usb_hid_event.h的依赖 - 简化模块功能描述,去除对外发布usb_hid_event的职责 - 移除内部usb_hid_event相关的状态管理和发布逻辑 - 修改USB连接处理逻辑,在VBUS就绪时提交唤醒事件
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
#include "usb_hid_event.h"
|
||||
|
||||
static const char *const usb_hid_evt_type_name[] = {
|
||||
[USB_HID_EVT_STATE_REPORT] = "STATE_REPORT",
|
||||
};
|
||||
|
||||
static const char *const usb_hid_usbd_state_name[] = {
|
||||
[USB_HID_USBD_VBUS_DISCONNECTED] = "VBUS_DISCONNECTED",
|
||||
[USB_HID_USBD_VBUS_CONNECTED] = "VBUS_CONNECTED",
|
||||
};
|
||||
|
||||
static const char *const usb_hid_stack_state_name[] = {
|
||||
[USB_HID_STACK_DISABLED] = "DISABLED",
|
||||
[USB_HID_STACK_ENABLED] = "ENABLED",
|
||||
};
|
||||
|
||||
static void log_usb_hid_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_hid_event *event = cast_usb_hid_event(aeh);
|
||||
|
||||
__ASSERT_NO_MSG(event->evt_type < ARRAY_SIZE(usb_hid_evt_type_name));
|
||||
__ASSERT_NO_MSG(event->usbd_state < ARRAY_SIZE(usb_hid_usbd_state_name));
|
||||
__ASSERT_NO_MSG(event->stack_state < ARRAY_SIZE(usb_hid_stack_state_name));
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "type=%s stack=%s usbd=%s",
|
||||
usb_hid_evt_type_name[event->evt_type],
|
||||
usb_hid_stack_state_name[event->stack_state],
|
||||
usb_hid_usbd_state_name[event->usbd_state]);
|
||||
}
|
||||
|
||||
static void profile_usb_hid_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_hid_event *event = cast_usb_hid_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->evt_type);
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->stack_state);
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->usbd_state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_hid_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("evt_type", "stack", "usbd"),
|
||||
profile_usb_hid_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_hid_event,
|
||||
log_usb_hid_event,
|
||||
&usb_hid_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,36 +0,0 @@
|
||||
#ifndef USB_HID_EVENT_H__
|
||||
#define USB_HID_EVENT_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
enum usb_hid_event_type {
|
||||
USB_HID_EVT_STATE_REPORT = 0,
|
||||
};
|
||||
|
||||
/* 仅反映 VBUS 连接状态。 */
|
||||
enum usb_hid_usbd_state {
|
||||
USB_HID_USBD_VBUS_DISCONNECTED = 0,
|
||||
USB_HID_USBD_VBUS_CONNECTED,
|
||||
};
|
||||
|
||||
/* USB HID 协议栈启停状态。 */
|
||||
enum usb_hid_stack_state {
|
||||
USB_HID_STACK_DISABLED = 0,
|
||||
USB_HID_STACK_ENABLED,
|
||||
};
|
||||
|
||||
struct usb_hid_event {
|
||||
struct app_event_header header;
|
||||
|
||||
enum usb_hid_event_type evt_type;
|
||||
enum usb_hid_stack_state stack_state;
|
||||
enum usb_hid_usbd_state usbd_state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_hid_event);
|
||||
|
||||
#endif /* USB_HID_EVENT_H__ */
|
||||
@@ -63,6 +63,7 @@ static int32_t battery_mv_window[BATTERY_MV_WINDOW_SIZE];
|
||||
static int64_t battery_mv_sum;
|
||||
static size_t battery_mv_count;
|
||||
static size_t battery_mv_index;
|
||||
static enum power_manager_level pm_restrict_level = POWER_MANAGER_LEVEL_MAX;
|
||||
|
||||
static void battery_module_resume(void);
|
||||
|
||||
@@ -191,6 +192,23 @@ static void publish_battery_status_event(const struct battery_status *status)
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
/*
|
||||
* 电源限制策略:
|
||||
* - 充电线插入(charging=true)时限制到 ALIVE,禁止自动休眠;
|
||||
* - 非充电时恢复到 SUSPENDED,允许系统进入挂起但不进入 OFF。
|
||||
*/
|
||||
static void update_power_restrict_by_charging(bool charging)
|
||||
{
|
||||
enum power_manager_level target = charging ?
|
||||
POWER_MANAGER_LEVEL_ALIVE : POWER_MANAGER_LEVEL_SUSPENDED;
|
||||
|
||||
if (pm_restrict_level == target)
|
||||
return;
|
||||
|
||||
pm_restrict_level = target;
|
||||
power_manager_restrict(MODULE_IDX(MODULE), target);
|
||||
}
|
||||
|
||||
static void battery_sample_fn(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
@@ -208,6 +226,8 @@ static void battery_sample_fn(struct k_work *work)
|
||||
goto out_reschedule;
|
||||
}
|
||||
|
||||
update_power_restrict_by_charging(sampled.charging);
|
||||
|
||||
/*
|
||||
* 仅在状态发生变化时上报,避免重复事件淹没总线。
|
||||
* 变化条件:充电标志、满电标志、SOC 任意一个变化。
|
||||
@@ -260,11 +280,8 @@ static int battery_module_init(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* 按需求将最深电源级别限制在 SUSPENDED,禁止进入 OFF。
|
||||
* 这样即使 CPU 进入挂起,IP5305 的硬件保活后端仍可持续输出保活脉冲。
|
||||
*/
|
||||
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
||||
/* 默认非充电态允许进入 SUSPENDED,但禁止进入 OFF。 */
|
||||
update_power_restrict_by_charging(false);
|
||||
|
||||
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
||||
has_last_status = false;
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <caf/events/power_event.h>
|
||||
#include <caf/events/power_manager_event.h>
|
||||
|
||||
#define MODULE usb_hid
|
||||
#include <caf/events/module_state_event.h>
|
||||
@@ -18,7 +17,6 @@
|
||||
#include "hid_report_event.h"
|
||||
#include "keyboard_led_state_event.h"
|
||||
#include "mode_event.h"
|
||||
#include "usb_hid_event.h"
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
@@ -28,8 +26,8 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
/*
|
||||
* 模块目标:
|
||||
* 1) 模块内聚控制 USB HID 栈生命周期(初始化/启用/禁用),不依赖外部 usb_hid_event 控制。
|
||||
* 2) 对外统一上报 usb_hid_event,供 LED/上层状态机消费。
|
||||
* 1) 模块内聚控制 USB HID 栈生命周期(初始化/启用/禁用)。
|
||||
* 2) 对外发布 HID 协议事件和 NumLock 指示事件。
|
||||
* 3) 仅响应 mode_event(USB/BLE/2.4G)和 power_event(休眠/唤醒)。
|
||||
*
|
||||
* 约束:
|
||||
@@ -55,15 +53,11 @@ struct usb_hid_ctx {
|
||||
bool boot_in_flight;
|
||||
bool nkro_in_flight;
|
||||
|
||||
enum usb_hid_usbd_state usbd_state;
|
||||
enum hid_protocol_type current_protocol;
|
||||
enum power_manager_level pm_restrict_level;
|
||||
};
|
||||
|
||||
static struct usb_hid_ctx g_usb_hid = {
|
||||
.usbd_state = USB_HID_USBD_VBUS_DISCONNECTED,
|
||||
.current_protocol = HID_PROTO_REPORT,
|
||||
.pm_restrict_level = POWER_MANAGER_LEVEL_MAX,
|
||||
};
|
||||
|
||||
USBD_DEVICE_DEFINE(new_kbd_usbd,
|
||||
@@ -79,17 +73,6 @@ static const uint8_t boot_report_desc[] = HID_KEYBOARD_REPORT_DESC();
|
||||
static const uint8_t nkro_report_desc[] = HID_DESC_KEYBOARD_NKRO_CONSUMER();
|
||||
static const uint8_t raw_report_desc[] = HID_DESC_RAW_64();
|
||||
|
||||
static void publish_usb_hid_state(void)
|
||||
{
|
||||
struct usb_hid_event *event = new_usb_hid_event();
|
||||
|
||||
event->evt_type = USB_HID_EVT_STATE_REPORT;
|
||||
event->stack_state = g_usb_hid.stack_enabled ?
|
||||
USB_HID_STACK_ENABLED : USB_HID_STACK_DISABLED;
|
||||
event->usbd_state = g_usb_hid.usbd_state;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
/* 统一入口仅处理单字节 LED 报告并发布事件。 */
|
||||
static void process_usb_led_input_report(uint8_t led_report)
|
||||
{
|
||||
@@ -136,40 +119,6 @@ static bool try_extract_led_mask(const struct device *dev,
|
||||
return true;
|
||||
}
|
||||
|
||||
static void recompute_hid_state(void)
|
||||
{
|
||||
/* 兼容现有调用点:对外仅发布 enable + usbd 状态。 */
|
||||
publish_usb_hid_state();
|
||||
}
|
||||
|
||||
/*
|
||||
* USB 自动休眠策略:
|
||||
* - 只要检测到 USB 线已连接(USBD 状态非 DISCONNECTED),限制系统保持 ALIVE;
|
||||
* - USB 线断开后,恢复为 MAX,允许系统按原有超时策略自动休眠。
|
||||
*/
|
||||
static void refresh_usb_power_restrict(void)
|
||||
{
|
||||
enum power_manager_level target =
|
||||
(g_usb_hid.usbd_state == USB_HID_USBD_VBUS_DISCONNECTED) ?
|
||||
POWER_MANAGER_LEVEL_MAX : POWER_MANAGER_LEVEL_ALIVE;
|
||||
|
||||
if (g_usb_hid.pm_restrict_level == target) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_usb_hid.pm_restrict_level = target;
|
||||
power_manager_restrict(MODULE_IDX(MODULE), target);
|
||||
}
|
||||
|
||||
static void set_usbd_state(enum usb_hid_usbd_state state)
|
||||
{
|
||||
if (g_usb_hid.usbd_state != state) {
|
||||
g_usb_hid.usbd_state = state;
|
||||
refresh_usb_power_restrict();
|
||||
publish_usb_hid_state();
|
||||
}
|
||||
}
|
||||
|
||||
static int hid_stub_get_report(const struct device *dev,
|
||||
uint8_t type, uint8_t id,
|
||||
uint16_t len, uint8_t *buf)
|
||||
@@ -308,7 +257,6 @@ static void hid_iface_ready_cb(const struct device *dev, bool ready)
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
recompute_hid_state();
|
||||
}
|
||||
|
||||
static const struct hid_device_ops boot_hid_ops = {
|
||||
@@ -348,7 +296,11 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
{
|
||||
switch (msg->type) {
|
||||
case USBD_MSG_VBUS_READY:
|
||||
set_usbd_state(USB_HID_USBD_VBUS_CONNECTED);
|
||||
if (g_usb_hid.pm_suspended) {
|
||||
LOG_INF("VBUS ready: submit wake_up_event");
|
||||
APP_EVENT_SUBMIT(new_wake_up_event());
|
||||
}
|
||||
|
||||
/*
|
||||
* 只有在 USB 模式下才允许拉起 USB 栈。
|
||||
* 这样即使插着线,只要用户切到 BLE/2.4G,也不会强制进入 USB HID。
|
||||
@@ -359,26 +311,17 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
break;
|
||||
|
||||
case USBD_MSG_VBUS_REMOVED:
|
||||
set_usbd_state(USB_HID_USBD_VBUS_DISCONNECTED);
|
||||
break;
|
||||
|
||||
case USBD_MSG_SUSPEND:
|
||||
publish_usb_hid_state();
|
||||
break;
|
||||
|
||||
case USBD_MSG_RESUME:
|
||||
publish_usb_hid_state();
|
||||
break;
|
||||
|
||||
case USBD_MSG_CONFIGURATION:
|
||||
publish_usb_hid_state();
|
||||
break;
|
||||
|
||||
case USBD_MSG_UDC_ERROR:
|
||||
case USBD_MSG_STACK_ERROR:
|
||||
LOG_ERR("USBD stack error message: %d", msg->type);
|
||||
g_usb_hid.stack_error = true;
|
||||
recompute_hid_state();
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -526,7 +469,6 @@ static int usb_hid_stack_init(void)
|
||||
}
|
||||
|
||||
g_usb_hid.stack_initialized = true;
|
||||
recompute_hid_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -556,17 +498,14 @@ static int usb_hid_set_enabled(bool enable)
|
||||
g_usb_hid.raw_iface_ready = false;
|
||||
g_usb_hid.boot_in_flight = false;
|
||||
g_usb_hid.nkro_in_flight = false;
|
||||
publish_usb_hid_state();
|
||||
}
|
||||
|
||||
if (err && (err != -EALREADY)) {
|
||||
LOG_ERR("usbd_%s failed: %d", enable ? "enable" : "disable", err);
|
||||
g_usb_hid.stack_error = true;
|
||||
recompute_hid_state();
|
||||
return err;
|
||||
}
|
||||
|
||||
recompute_hid_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -596,13 +535,10 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
LOG_ERR("USB HID stack init failed: %d", err);
|
||||
g_usb_hid.stack_error = true;
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
recompute_hid_state();
|
||||
return false;
|
||||
}
|
||||
|
||||
refresh_usb_power_restrict();
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
publish_usb_hid_state();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user