feat: 更新键盘固件的事件系统和模块配置

- 在CMakeLists.txt中添加hid_boot_event.c、keyboard_led_event.c和ble_slot_ctrl_module.c源文件
- 新增Kconfig配置项NEW_KBD_BLE_BOND_ENABLE用于启用应用特定的BLE绑定支持
- 修改prj.conf配置,禁用配对模式下的设备名称广播功能
- 重构电池状态事件结构,将charging和full布尔字段改为flags位域,并提供相应的访问函数
- 添加hid_boot_event事件类型,用于处理HID Boot协议输入报告
- 重命名keyboard_led_state_event为keyboard_led_event并改进LED状态处理逻辑
- 移除hid_protocol_event中的transport字段,简化协议事件处理
- 分离hid_report_event和hid_boot_event,明确区分Report和Boot协议报文处理
- 重构battery_module.c代码结构,改用上下文结构体管理电池模块状态
- 更新ble_battery_module.c使用新的电池状态事件访问接口
This commit is contained in:
2026-03-18 13:41:36 +08:00
parent 2a8b44d058
commit 2a389ef19b
25 changed files with 1237 additions and 484 deletions

View File

@@ -13,9 +13,10 @@
#include <caf/events/module_state_event.h>
#include "hid_report_descriptor.h"
#include "hid_boot_event.h"
#include "hid_protocol_event.h"
#include "hid_report_event.h"
#include "keyboard_led_state_event.h"
#include "keyboard_led_event.h"
#include "mode_event.h"
#include <zephyr/logging/log.h>
@@ -35,24 +36,30 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
* - 只有当 mode 切到 USB 且系统非休眠时才 enable
* - BLE 逻辑保持不变,不在本模块中触碰。
*/
struct usb_hid_ctx {
const struct device *boot_dev;
const struct device *nkro_dev;
const struct device *raw_dev;
struct usb_hid_iface {
const struct device *dev;
bool iface_ready;
bool in_flight;
};
bool stack_initialized;
bool stack_enabled;
bool stack_error;
enum usb_hid_stack_state {
USB_HID_STACK_STATE_OFF,
USB_HID_STACK_STATE_READY,
USB_HID_STACK_STATE_ACTIVE,
USB_HID_STACK_STATE_ERROR,
};
struct usb_hid_policy {
bool usb_mode_selected;
bool pm_suspended;
};
bool boot_iface_ready;
bool nkro_iface_ready;
bool raw_iface_ready;
bool boot_in_flight;
bool nkro_in_flight;
struct usb_hid_ctx {
struct usb_hid_iface boot;
struct usb_hid_iface nkro;
struct usb_hid_iface raw;
enum usb_hid_stack_state stack_state;
struct usb_hid_policy policy;
enum hid_protocol_type current_protocol;
};
@@ -73,22 +80,54 @@ 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();
/* 统一入口仅处理单字节 LED 报告并发布事件。 */
static void process_usb_led_input_report(uint8_t led_report)
static bool usb_hid_stack_is_active(void)
{
struct keyboard_led_state_event *event = new_keyboard_led_state_event();
return g_usb_hid.stack_state == USB_HID_STACK_STATE_ACTIVE;
}
event->led_mask = led_report;
event->num_lock = (led_report & BIT(0)) != 0U;
APP_EVENT_SUBMIT(event);
static bool usb_hid_stack_is_error(void)
{
return g_usb_hid.stack_state == USB_HID_STACK_STATE_ERROR;
}
static bool usb_hid_should_be_active(void)
{
return g_usb_hid.policy.usb_mode_selected && !g_usb_hid.policy.pm_suspended;
}
static struct usb_hid_iface *usb_hid_iface_from_dev(const struct device *dev)
{
if (dev == g_usb_hid.boot.dev) {
return &g_usb_hid.boot;
}
if (dev == g_usb_hid.nkro.dev) {
return &g_usb_hid.nkro;
}
if (dev == g_usb_hid.raw.dev) {
return &g_usb_hid.raw;
}
return NULL;
}
static void usb_hid_clear_runtime_iface_state(void)
{
g_usb_hid.boot.iface_ready = false;
g_usb_hid.nkro.iface_ready = false;
g_usb_hid.raw.iface_ready = false;
g_usb_hid.boot.in_flight = false;
g_usb_hid.nkro.in_flight = false;
g_usb_hid.raw.in_flight = false;
}
static bool should_handle_led_input_from_dev(const struct device *dev)
{
if (g_usb_hid.current_protocol == HID_PROTO_BOOT)
return (dev == g_usb_hid.boot_dev);
return (dev == g_usb_hid.boot.dev);
return (dev == g_usb_hid.nkro_dev);
return (dev == g_usb_hid.nkro.dev);
}
static bool try_extract_led_mask(const struct device *dev,
@@ -99,12 +138,12 @@ static bool try_extract_led_mask(const struct device *dev,
if ((buf == NULL) || (len == 0U))
return false;
if (dev == g_usb_hid.boot_dev) {
if (dev == g_usb_hid.boot.dev) {
*led_mask = buf[0];
return true;
}
if (dev != g_usb_hid.nkro_dev)
if (dev != g_usb_hid.nkro.dev)
return false;
if (len >= 2U) {
@@ -149,7 +188,7 @@ static int hid_stub_set_report(const struct device *dev,
}
LOG_INF("hid_stub_set_report led_mask=0x%02x", led_mask);
process_usb_led_input_report(led_mask);
keyboard_led_event_submit(led_mask);
return 0;
}
@@ -185,12 +224,8 @@ static void hid_stub_set_protocol(const struct device *dev, uint8_t proto)
* 按需求USB HID 在连接后收到 set_protocol 时上报 hid_protocol_event。
* 这里额外检查接口 ready避免在未枚举完成阶段上报无意义协议切换。
*/
if (g_usb_hid.boot_iface_ready || g_usb_hid.nkro_iface_ready) {
struct hid_protocol_event *event = new_hid_protocol_event();
event->transport = HID_TRANSPORT_USB;
event->protocol = new_protocol;
APP_EVENT_SUBMIT(event);
if (g_usb_hid.boot.iface_ready || g_usb_hid.nkro.iface_ready) {
hid_protocol_event_submit(new_protocol);
}
}
@@ -203,13 +238,10 @@ static void hid_stub_input_done(const struct device *dev, const uint8_t *report)
* - 仅在这里清除“在途发送”标志,确保“上一包未完成则丢弃新包”的策略可闭环;
* - 若收到未知 dev 的回调,仅记录告警,避免静默状态错乱。
*/
if (dev == g_usb_hid.boot_dev) {
g_usb_hid.boot_in_flight = false;
return;
}
struct usb_hid_iface *iface = usb_hid_iface_from_dev(dev);
if (dev == g_usb_hid.nkro_dev) {
g_usb_hid.nkro_in_flight = false;
if (iface) {
iface->in_flight = false;
return;
}
@@ -229,34 +261,21 @@ static void hid_stub_output_report(const struct device *dev, uint16_t len, const
}
LOG_INF("hid_stub_output_report led_mask=0x%02x", led_mask);
process_usb_led_input_report(led_mask);
keyboard_led_event_submit(led_mask);
}
static void hid_iface_ready_cb(const struct device *dev, bool ready)
{
if (dev == g_usb_hid.boot_dev) {
g_usb_hid.boot_iface_ready = ready;
if (!ready) {
g_usb_hid.boot_in_flight = false;
}
} else if (dev == g_usb_hid.nkro_dev) {
g_usb_hid.nkro_iface_ready = ready;
if (!ready) {
g_usb_hid.nkro_in_flight = false;
}
} else if (dev == g_usb_hid.raw_dev) {
g_usb_hid.raw_iface_ready = ready;
struct usb_hid_iface *iface = usb_hid_iface_from_dev(dev);
if (!iface) {
return;
}
if (ready) {
/* 连接可用后同步一次当前协议,让 keyboard_module 与传输侧编码一致。 */
struct hid_protocol_event *event = new_hid_protocol_event();
event->transport = HID_TRANSPORT_USB;
event->protocol = g_usb_hid.current_protocol;
APP_EVENT_SUBMIT(event);
iface->iface_ready = ready;
if (!ready) {
iface->in_flight = false;
}
}
static const struct hid_device_ops boot_hid_ops = {
@@ -296,7 +315,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
{
switch (msg->type) {
case USBD_MSG_VBUS_READY:
if (g_usb_hid.pm_suspended) {
if (g_usb_hid.policy.pm_suspended) {
LOG_INF("VBUS ready: submit wake_up_event");
APP_EVENT_SUBMIT(new_wake_up_event());
}
@@ -305,7 +324,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
* 只有在 USB 模式下才允许拉起 USB 栈。
* 这样即使插着线,只要用户切到 BLE/2.4G,也不会强制进入 USB HID。
*/
if (usbd_can_detect_vbus(usbd_ctx) && g_usb_hid.stack_enabled) {
if (usbd_can_detect_vbus(usbd_ctx) && usb_hid_stack_is_active()) {
(void)usbd_enable(usbd_ctx);
}
break;
@@ -321,7 +340,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
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;
g_usb_hid.stack_state = USB_HID_STACK_STATE_ERROR;
break;
default:
@@ -331,17 +350,17 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
static bool usb_hid_devices_ready(void)
{
if (!device_is_ready(g_usb_hid.boot_dev)) {
if (!device_is_ready(g_usb_hid.boot.dev)) {
LOG_ERR("HID boot device is not ready");
return false;
}
if (!device_is_ready(g_usb_hid.nkro_dev)) {
if (!device_is_ready(g_usb_hid.nkro.dev)) {
LOG_ERR("HID nkro device is not ready");
return false;
}
if (!device_is_ready(g_usb_hid.raw_dev)) {
if (!device_is_ready(g_usb_hid.raw.dev)) {
LOG_ERR("HID raw device is not ready");
return false;
}
@@ -356,7 +375,7 @@ static bool usb_hid_devices_ready(void)
static int usb_hid_register_hid_devices(void)
{
int err = hid_device_register(g_usb_hid.boot_dev,
int err = hid_device_register(g_usb_hid.boot.dev,
boot_report_desc, sizeof(boot_report_desc),
&boot_hid_ops);
if (err) {
@@ -364,7 +383,7 @@ static int usb_hid_register_hid_devices(void)
return err;
}
err = hid_device_register(g_usb_hid.nkro_dev,
err = hid_device_register(g_usb_hid.nkro.dev,
nkro_report_desc, sizeof(nkro_report_desc),
&report_hid_ops);
if (err) {
@@ -372,7 +391,7 @@ static int usb_hid_register_hid_devices(void)
return err;
}
err = hid_device_register(g_usb_hid.raw_dev,
err = hid_device_register(g_usb_hid.raw.dev,
raw_report_desc, sizeof(raw_report_desc),
&raw_hid_ops);
if (err) {
@@ -441,13 +460,13 @@ static int usb_hid_init_usbd_stack(void)
static int usb_hid_stack_init(void)
{
if (g_usb_hid.stack_initialized) {
if (g_usb_hid.stack_state != USB_HID_STACK_STATE_OFF) {
return 0;
}
g_usb_hid.boot_dev = DEVICE_DT_GET(DT_NODELABEL(hid_dev_0));
g_usb_hid.nkro_dev = DEVICE_DT_GET(DT_NODELABEL(hid_dev_1));
g_usb_hid.raw_dev = DEVICE_DT_GET(DT_NODELABEL(raw_hid));
g_usb_hid.boot.dev = DEVICE_DT_GET(DT_NODELABEL(hid_dev_0));
g_usb_hid.nkro.dev = DEVICE_DT_GET(DT_NODELABEL(hid_dev_1));
g_usb_hid.raw.dev = DEVICE_DT_GET(DT_NODELABEL(raw_hid));
if (!usb_hid_devices_ready()) {
return -ENODEV;
@@ -468,7 +487,7 @@ static int usb_hid_stack_init(void)
return err;
}
g_usb_hid.stack_initialized = true;
g_usb_hid.stack_state = USB_HID_STACK_STATE_READY;
return 0;
}
@@ -476,36 +495,39 @@ static int usb_hid_set_enabled(bool enable)
{
int err;
if (!g_usb_hid.stack_initialized) {
if (usb_hid_stack_is_error()) {
return -EIO;
}
if (g_usb_hid.stack_state == USB_HID_STACK_STATE_OFF) {
err = usb_hid_stack_init();
if (err) {
return err;
}
}
if (g_usb_hid.stack_enabled == enable) {
if (enable && usb_hid_stack_is_active()) {
return 0;
}
g_usb_hid.stack_enabled = enable;
if (!enable && (g_usb_hid.stack_state == USB_HID_STACK_STATE_READY)) {
return 0;
}
if (enable) {
err = usbd_enable(&new_kbd_usbd);
} else {
err = usbd_disable(&new_kbd_usbd);
g_usb_hid.boot_iface_ready = false;
g_usb_hid.nkro_iface_ready = false;
g_usb_hid.raw_iface_ready = false;
g_usb_hid.boot_in_flight = false;
g_usb_hid.nkro_in_flight = false;
usb_hid_clear_runtime_iface_state();
}
if (err && (err != -EALREADY)) {
LOG_ERR("usbd_%s failed: %d", enable ? "enable" : "disable", err);
g_usb_hid.stack_error = true;
g_usb_hid.stack_state = USB_HID_STACK_STATE_ERROR;
return err;
}
g_usb_hid.stack_state = enable ? USB_HID_STACK_STATE_ACTIVE : USB_HID_STACK_STATE_READY;
return 0;
}
@@ -516,7 +538,7 @@ static void refresh_usb_state_by_policy(void)
* - USB 模式 + 非休眠:启用 USB HID。
* - 其他情况:关闭 USB HID不销毁初始化结果后续可快速恢复
*/
bool should_enable = g_usb_hid.usb_mode_selected && !g_usb_hid.pm_suspended;
bool should_enable = usb_hid_should_be_active();
int err = usb_hid_set_enabled(should_enable);
if (err) {
@@ -533,7 +555,7 @@ static bool handle_module_state_event(const struct module_state_event *event)
int err = usb_hid_stack_init();
if (err) {
LOG_ERR("USB HID stack init failed: %d", err);
g_usb_hid.stack_error = true;
g_usb_hid.stack_state = USB_HID_STACK_STATE_ERROR;
module_set_state(MODULE_STATE_ERROR);
return false;
}
@@ -544,19 +566,19 @@ static bool handle_module_state_event(const struct module_state_event *event)
static bool handle_mode_event(const struct mode_event *event)
{
g_usb_hid.usb_mode_selected = (event->mode_type == MODE_TYPE_USB);
g_usb_hid.policy.usb_mode_selected = (event->mode_type == MODE_TYPE_USB);
refresh_usb_state_by_policy();
return false;
}
static bool handle_power_down_event(void)
{
if (g_usb_hid.pm_suspended) {
if (g_usb_hid.policy.pm_suspended) {
/* 避免重复上报 STANDBY 导致 power_manager 在 SUSPENDING 期间反复迭代。 */
return false;
}
g_usb_hid.pm_suspended = true;
g_usb_hid.policy.pm_suspended = true;
refresh_usb_state_by_policy();
module_set_state(MODULE_STATE_STANDBY);
return false;
@@ -564,16 +586,49 @@ static bool handle_power_down_event(void)
static bool handle_wake_up_event(void)
{
if (!g_usb_hid.pm_suspended) {
if (!g_usb_hid.policy.pm_suspended) {
return false;
}
g_usb_hid.pm_suspended = false;
g_usb_hid.policy.pm_suspended = false;
refresh_usb_state_by_policy();
module_set_state(MODULE_STATE_READY);
return false;
}
static bool handle_hid_boot_event(const struct hid_boot_event *event)
{
if (!g_usb_hid.policy.usb_mode_selected || !usb_hid_stack_is_active()) {
return false;
}
if (g_usb_hid.current_protocol != HID_PROTO_BOOT) {
return false;
}
const uint8_t *payload = hid_boot_event_get_data(event);
size_t payload_len = hid_boot_event_get_size(event);
if (!g_usb_hid.boot.iface_ready || !g_usb_hid.boot.dev) {
return false;
}
if (g_usb_hid.boot.in_flight) {
LOG_WRN("Drop boot report: previous report not sent");
return false;
}
int err = hid_device_submit_report(g_usb_hid.boot.dev,
payload_len,
payload);
if (err) {
LOG_WRN("USB boot report send failed err=%d", err);
} else {
g_usb_hid.boot.in_flight = true;
}
return false;
}
static bool handle_hid_report_event(const struct hid_report_event *event)
{
/*
@@ -581,63 +636,42 @@ static bool handle_hid_report_event(const struct hid_report_event *event)
* - 当前 mode 为 USB
* - USB HID 栈已启用且对应接口 ready。
*/
if (!g_usb_hid.usb_mode_selected || !g_usb_hid.stack_enabled) {
if (!g_usb_hid.policy.usb_mode_selected || !usb_hid_stack_is_active()) {
return false;
}
if (g_usb_hid.current_protocol != HID_PROTO_REPORT) {
return false;
}
const uint8_t *data = hid_report_event_get_data(event);
size_t data_len = hid_report_event_get_size(event);
uint8_t report_id;
if (event->protocol != g_usb_hid.current_protocol) {
if (data_len < 1U) {
return false;
}
if (event->protocol == HID_PROTO_BOOT) {
const uint8_t *payload = event->dyndata.data;
size_t payload_len = event->dyndata.size;
report_id = data[0];
if (!g_usb_hid.boot_iface_ready || !g_usb_hid.boot_dev) {
return false;
}
if (g_usb_hid.boot_in_flight) {
LOG_WRN("Drop boot report: previous report not sent");
return false;
}
int err = hid_device_submit_report(g_usb_hid.boot_dev,
payload_len,
payload);
if (err) {
LOG_WRN("USB boot report send failed err=%d", err);
} else {
g_usb_hid.boot_in_flight = true;
}
return false;
}
if (event->dyndata.size < 1U) {
return false;
}
report_id = event->dyndata.data[0];
if (!g_usb_hid.nkro_iface_ready || !g_usb_hid.nkro_dev) {
if (!g_usb_hid.nkro.iface_ready || !g_usb_hid.nkro.dev) {
return false;
}
if ((report_id != REPORT_ID_KEYBOARD) && (report_id != REPORT_ID_CONSUMER)) {
return false;
}
if (g_usb_hid.nkro_in_flight) {
if (g_usb_hid.nkro.in_flight) {
LOG_WRN("Drop report id=0x%02x: previous report not sent", report_id);
return false;
}
/* Report 协议下 dyndata 是 [report_id|payload],可直接透传。 */
int err = hid_device_submit_report(g_usb_hid.nkro_dev, event->dyndata.size, event->dyndata.data);
int err = hid_device_submit_report(g_usb_hid.nkro.dev, data_len, data);
if (err) {
LOG_WRN("USB report send failed id=0x%02x err=%d", report_id, err);
} else {
g_usb_hid.nkro_in_flight = true;
g_usb_hid.nkro.in_flight = true;
}
return false;
@@ -665,6 +699,10 @@ static bool app_event_handler(const struct app_event_header *aeh)
return handle_hid_report_event(cast_hid_report_event(aeh));
}
if (is_hid_boot_event(aeh)) {
return handle_hid_boot_event(cast_hid_boot_event(aeh));
}
__ASSERT_NO_MSG(false);
return false;
}
@@ -674,4 +712,5 @@ APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, mode_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, hid_boot_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, hid_report_event);