From 195f0a9e2b469b4169c3b603c27a9fcc14a7a43a Mon Sep 17 00:00:00 2001 From: skiinder Date: Mon, 23 Mar 2026 18:25:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(usb=5Fhid=5Fmodule):=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC=E5=92=8C=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BD=93=E5=AE=9A=E4=B9=89=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将所有结构体定义改为大括号独立成行的格式 - 调整函数参数对齐以提高可读性 - 统一 if 语句的大括号风格,确保一致性 - 优化代码缩进和空格布局 --- src/modules/usb_hid_module.c | 299 ++++++++++++++++++++++------------- 1 file changed, 190 insertions(+), 109 deletions(-) diff --git a/src/modules/usb_hid_module.c b/src/modules/usb_hid_module.c index 1e823a0..e3c79a8 100644 --- a/src/modules/usb_hid_module.c +++ b/src/modules/usb_hid_module.c @@ -38,25 +38,29 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF); * - 只有当 mode 切到 USB 且系统非休眠时才 enable; * - BLE 逻辑保持不变,不在本模块中触碰。 */ -struct usb_hid_iface { +struct usb_hid_iface +{ const struct device *dev; bool iface_ready; bool in_flight; }; -enum usb_hid_stack_state { +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 { +struct usb_hid_policy +{ bool usb_mode_selected; bool pm_suspended; }; -struct usb_hid_ctx { +struct usb_hid_ctx +{ struct usb_hid_iface boot; struct usb_hid_iface nkro; struct usb_hid_iface raw; @@ -75,8 +79,8 @@ static void submit_usb_tx_done(enum hid_tx_kind kind, bool success) } USBD_DEVICE_DEFINE(new_kbd_usbd, - DEVICE_DT_GET(DT_NODELABEL(usbd)), - APP_USB_VID, APP_USB_PID); + DEVICE_DT_GET(DT_NODELABEL(usbd)), + APP_USB_VID, APP_USB_PID); USBD_DESC_LANG_DEFINE(new_kbd_lang); USBD_DESC_MANUFACTURER_DEFINE(new_kbd_mfr, "new_kbd"); USBD_DESC_PRODUCT_DEFINE(new_kbd_product, "new_kbd composite HID"); @@ -104,15 +108,18 @@ static bool usb_hid_should_be_active(void) static struct usb_hid_iface *usb_hid_iface_from_dev(const struct device *dev) { - if (dev == g_usb_hid.boot.dev) { + if (dev == g_usb_hid.boot.dev) + { return &g_usb_hid.boot; } - if (dev == g_usb_hid.nkro.dev) { + if (dev == g_usb_hid.nkro.dev) + { return &g_usb_hid.nkro; } - if (dev == g_usb_hid.raw.dev) { + if (dev == g_usb_hid.raw.dev) + { return &g_usb_hid.raw; } @@ -138,14 +145,15 @@ static bool should_handle_led_input_from_dev(const struct device *dev) } static bool try_extract_led_mask(const struct device *dev, - uint16_t len, - const uint8_t *buf, - uint8_t *led_mask) + uint16_t len, + const uint8_t *buf, + uint8_t *led_mask) { 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; } @@ -153,7 +161,8 @@ static bool try_extract_led_mask(const struct device *dev, if (dev != g_usb_hid.nkro.dev) return false; - if (len >= 2U) { + if (len >= 2U) + { if (buf[0] != REPORT_ID_KEYBOARD) return false; @@ -166,24 +175,28 @@ static bool try_extract_led_mask(const struct device *dev, } static bool try_extract_vendor_mask(const struct device *dev, - uint16_t len, - const uint8_t *buf, - const uint8_t **mask_data, - size_t *mask_len) + uint16_t len, + const uint8_t *buf, + const uint8_t **mask_data, + size_t *mask_len) { - if ((buf == NULL) || (len < 1U)) { + if ((buf == NULL) || (len < 1U)) + { return false; } - if (dev != g_usb_hid.nkro.dev) { + if (dev != g_usb_hid.nkro.dev) + { return false; } - if (buf[0] != REPORT_ID_VENDOR) { + if (buf[0] != REPORT_ID_VENDOR) + { return false; } - if ((len - 1U) != HID_VENDOR_PAYLOAD_SIZE) { + if ((len - 1U) != HID_VENDOR_PAYLOAD_SIZE) + { return false; } @@ -193,8 +206,8 @@ static bool try_extract_vendor_mask(const struct device *dev, } static int hid_stub_get_report(const struct device *dev, - uint8_t type, uint8_t id, - uint16_t len, uint8_t *buf) + uint8_t type, uint8_t id, + uint16_t len, uint8_t *buf) { ARG_UNUSED(dev); ARG_UNUSED(type); @@ -205,27 +218,31 @@ static int hid_stub_get_report(const struct device *dev, } static int hid_stub_set_report(const struct device *dev, - uint8_t type, uint8_t id, - uint16_t len, const uint8_t *buf) + uint8_t type, uint8_t id, + uint16_t len, const uint8_t *buf) { ARG_UNUSED(type); ARG_UNUSED(id); - if (!should_handle_led_input_from_dev(dev)) { - const uint8_t *mask_data; - size_t mask_len; + const uint8_t *mask_data; + size_t mask_len; - if (try_extract_vendor_mask(dev, len, buf, &mask_data, &mask_len)) { - LOG_INF("hid_stub_set_report vendor mask len=%u", mask_len); - hid_vendor_mask_event_submit(mask_data, mask_len); - } + if (try_extract_vendor_mask(dev, len, buf, &mask_data, &mask_len)) + { + LOG_INF("hid_stub_set_report vendor mask len=%u", mask_len); + hid_vendor_mask_event_submit(mask_data, mask_len); + return 0; + } + if (!should_handle_led_input_from_dev(dev)) + { return 0; } uint8_t led_mask; - if (!try_extract_led_mask(dev, len, buf, &led_mask)) { + if (!try_extract_led_mask(dev, len, buf, &led_mask)) + { return 0; } @@ -256,7 +273,8 @@ static void hid_stub_set_protocol(const struct device *dev, uint8_t proto) enum hid_protocol_type new_protocol = (proto == HID_PROTOCOL_BOOT) ? HID_PROTO_BOOT : HID_PROTO_REPORT; - if (g_usb_hid.current_protocol == new_protocol) { + if (g_usb_hid.current_protocol == new_protocol) + { return; } @@ -266,7 +284,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) { + if (g_usb_hid.boot.iface_ready || g_usb_hid.nkro.iface_ready) + { hid_protocol_event_submit(new_protocol); } } @@ -282,11 +301,11 @@ static void hid_stub_input_done(const struct device *dev, const uint8_t *report) */ struct usb_hid_iface *iface = usb_hid_iface_from_dev(dev); - if (iface) { + if (iface) + { iface->in_flight = false; - submit_usb_tx_done((dev == g_usb_hid.boot.dev) ? - HID_TX_KIND_BOOT : HID_TX_KIND_REPORT, - true); + submit_usb_tx_done((dev == g_usb_hid.boot.dev) ? HID_TX_KIND_BOOT : HID_TX_KIND_REPORT, + true); return; } @@ -295,11 +314,13 @@ static void hid_stub_input_done(const struct device *dev, const uint8_t *report) static void hid_stub_output_report(const struct device *dev, uint16_t len, const uint8_t *buf) { - if (!should_handle_led_input_from_dev(dev)) { + if (!should_handle_led_input_from_dev(dev)) + { const uint8_t *mask_data; size_t mask_len; - if (try_extract_vendor_mask(dev, len, buf, &mask_data, &mask_len)) { + if (try_extract_vendor_mask(dev, len, buf, &mask_data, &mask_len)) + { LOG_INF("hid_stub_output_report vendor mask len=%u", mask_len); hid_vendor_mask_event_submit(mask_data, mask_len); } @@ -309,7 +330,8 @@ static void hid_stub_output_report(const struct device *dev, uint16_t len, const uint8_t led_mask; - if (!try_extract_led_mask(dev, len, buf, &led_mask)) { + if (!try_extract_led_mask(dev, len, buf, &led_mask)) + { return; } @@ -321,12 +343,14 @@ static void hid_iface_ready_cb(const struct device *dev, bool ready) { struct usb_hid_iface *iface = usb_hid_iface_from_dev(dev); - if (!iface) { + if (!iface) + { return; } iface->iface_ready = ready; - if (!ready) { + if (!ready) + { iface->in_flight = false; } } @@ -364,11 +388,13 @@ static const struct hid_device_ops raw_hid_ops = { }; static void usbd_msg_cb(struct usbd_context *const usbd_ctx, - const struct usbd_msg *const msg) + const struct usbd_msg *const msg) { - switch (msg->type) { + switch (msg->type) + { case USBD_MSG_VBUS_READY: - if (g_usb_hid.policy.pm_suspended) { + if (g_usb_hid.policy.pm_suspended) + { LOG_INF("VBUS ready: submit wake_up_event"); APP_EVENT_SUBMIT(new_wake_up_event()); } @@ -377,7 +403,8 @@ 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) && usb_hid_stack_is_active()) { + if (usbd_can_detect_vbus(usbd_ctx) && usb_hid_stack_is_active()) + { (void)usbd_enable(usbd_ctx); } break; @@ -403,22 +430,26 @@ 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; } - if (!device_is_ready(DEVICE_DT_GET(DT_NODELABEL(usbd)))) { + if (!device_is_ready(DEVICE_DT_GET(DT_NODELABEL(usbd)))) + { LOG_ERR("USBD device is not ready"); return false; } @@ -429,25 +460,28 @@ 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, - boot_report_desc, sizeof(boot_report_desc), - &boot_hid_ops); - if (err) { + boot_report_desc, sizeof(boot_report_desc), + &boot_hid_ops); + if (err) + { LOG_ERR("hid_device_register(boot) failed: %d", err); return err; } err = hid_device_register(g_usb_hid.nkro.dev, - nkro_report_desc, sizeof(nkro_report_desc), - &report_hid_ops); - if (err) { + nkro_report_desc, sizeof(nkro_report_desc), + &report_hid_ops); + if (err) + { LOG_ERR("hid_device_register(nkro) failed: %d", err); return err; } err = hid_device_register(g_usb_hid.raw.dev, - raw_report_desc, sizeof(raw_report_desc), - &raw_hid_ops); - if (err) { + raw_report_desc, sizeof(raw_report_desc), + &raw_hid_ops); + if (err) + { LOG_ERR("hid_device_register(raw) failed: %d", err); return err; } @@ -458,31 +492,36 @@ static int usb_hid_register_hid_devices(void) static int usb_hid_configure_usbd(void) { int err = usbd_add_descriptor(&new_kbd_usbd, &new_kbd_lang); - if (err) { + if (err) + { LOG_ERR("usbd_add_descriptor(lang) failed: %d", err); return err; } err = usbd_add_descriptor(&new_kbd_usbd, &new_kbd_mfr); - if (err) { + if (err) + { LOG_ERR("usbd_add_descriptor(mfr) failed: %d", err); return err; } err = usbd_add_descriptor(&new_kbd_usbd, &new_kbd_product); - if (err) { + if (err) + { LOG_ERR("usbd_add_descriptor(product) failed: %d", err); return err; } err = usbd_add_configuration(&new_kbd_usbd, USBD_SPEED_FS, &new_kbd_fs_config); - if (err) { + if (err) + { LOG_ERR("usbd_add_configuration failed: %d", err); return err; } err = usbd_register_all_classes(&new_kbd_usbd, USBD_SPEED_FS, 1, NULL); - if (err) { + if (err) + { LOG_ERR("usbd_register_all_classes failed: %d", err); return err; } @@ -497,13 +536,15 @@ static int usb_hid_init_usbd_stack(void) usbd_device_set_code_triple(&new_kbd_usbd, USBD_SPEED_FS, 0, 0, 0); err = usbd_msg_register_cb(&new_kbd_usbd, usbd_msg_cb); - if (err) { + if (err) + { LOG_ERR("usbd_msg_register_cb failed: %d", err); return err; } err = usbd_init(&new_kbd_usbd); - if (err && (err != -EALREADY)) { + if (err && (err != -EALREADY)) + { LOG_ERR("usbd_init failed: %d", err); return err; } @@ -513,7 +554,8 @@ static int usb_hid_init_usbd_stack(void) static int usb_hid_stack_init(void) { - if (g_usb_hid.stack_state != USB_HID_STACK_STATE_OFF) { + if (g_usb_hid.stack_state != USB_HID_STACK_STATE_OFF) + { return 0; } @@ -521,22 +563,26 @@ static int usb_hid_stack_init(void) 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()) { + if (!usb_hid_devices_ready()) + { return -ENODEV; } int err = usb_hid_register_hid_devices(); - if (err) { + if (err) + { return err; } err = usb_hid_configure_usbd(); - if (err) { + if (err) + { return err; } err = usb_hid_init_usbd_stack(); - if (err) { + if (err) + { return err; } @@ -548,33 +594,42 @@ static int usb_hid_set_enabled(bool enable) { int err; - if (usb_hid_stack_is_error()) { + if (usb_hid_stack_is_error()) + { return -EIO; } - if (g_usb_hid.stack_state == USB_HID_STACK_STATE_OFF) { + if (g_usb_hid.stack_state == USB_HID_STACK_STATE_OFF) + { err = usb_hid_stack_init(); - if (err) { + if (err) + { return err; } } - if (enable && usb_hid_stack_is_active()) { + if (enable && usb_hid_stack_is_active()) + { return 0; } - if (!enable && (g_usb_hid.stack_state == USB_HID_STACK_STATE_READY)) { + if (!enable && (g_usb_hid.stack_state == USB_HID_STACK_STATE_READY)) + { return 0; } - if (enable) { + if (enable) + { err = usbd_enable(&new_kbd_usbd); - } else { + } + else + { err = usbd_disable(&new_kbd_usbd); usb_hid_clear_runtime_iface_state(); } - if (err && (err != -EALREADY)) { + if (err && (err != -EALREADY)) + { LOG_ERR("usbd_%s failed: %d", enable ? "enable" : "disable", err); g_usb_hid.stack_state = USB_HID_STACK_STATE_ERROR; return err; @@ -594,19 +649,22 @@ static void refresh_usb_state_by_policy(void) bool should_enable = usb_hid_should_be_active(); int err = usb_hid_set_enabled(should_enable); - if (err) { + if (err) + { LOG_ERR("usb_hid_set_enabled(%d) failed: %d", should_enable, err); } } static bool handle_module_state_event(const struct module_state_event *event) { - if (!check_state(event, MODULE_ID(main), MODULE_STATE_READY)) { + if (!check_state(event, MODULE_ID(main), MODULE_STATE_READY)) + { return false; } int err = usb_hid_stack_init(); - if (err) { + if (err) + { LOG_ERR("USB HID stack init failed: %d", err); g_usb_hid.stack_state = USB_HID_STACK_STATE_ERROR; module_set_state(MODULE_STATE_ERROR); @@ -626,7 +684,8 @@ static bool handle_mode_event(const struct mode_event *event) static bool handle_power_down_event(void) { - if (g_usb_hid.policy.pm_suspended) { + if (g_usb_hid.policy.pm_suspended) + { /* 避免重复上报 STANDBY 导致 power_manager 在 SUSPENDING 期间反复迭代。 */ return false; } @@ -639,7 +698,8 @@ static bool handle_power_down_event(void) static bool handle_wake_up_event(void) { - if (!g_usb_hid.policy.pm_suspended) { + if (!g_usb_hid.policy.pm_suspended) + { return false; } @@ -651,36 +711,44 @@ static bool handle_wake_up_event(void) static bool handle_hid_tx_event(const struct hid_tx_event *event) { - if (!g_usb_hid.policy.usb_mode_selected || !usb_hid_stack_is_active()) { + if (!g_usb_hid.policy.usb_mode_selected || !usb_hid_stack_is_active()) + { return false; } - if (event->kind == HID_TX_KIND_BOOT) { + if (event->kind == HID_TX_KIND_BOOT) + { const uint8_t *payload = hid_tx_event_get_data(event); size_t payload_len = hid_tx_event_get_size(event); int err; - if (g_usb_hid.current_protocol != HID_PROTO_BOOT) { + if (g_usb_hid.current_protocol != HID_PROTO_BOOT) + { return false; } - if (!g_usb_hid.boot.iface_ready || !g_usb_hid.boot.dev) { + if (!g_usb_hid.boot.iface_ready || !g_usb_hid.boot.dev) + { submit_usb_tx_done(HID_TX_KIND_BOOT, false); return false; } - if (g_usb_hid.boot.in_flight) { + if (g_usb_hid.boot.in_flight) + { LOG_WRN("Drop boot tx: previous report not sent"); submit_usb_tx_done(HID_TX_KIND_BOOT, false); return false; } err = hid_device_submit_report(g_usb_hid.boot.dev, - payload_len, - payload); - if (err) { + payload_len, + payload); + if (err) + { LOG_WRN("USB boot report send failed err=%d", err); submit_usb_tx_done(HID_TX_KIND_BOOT, false); - } else { + } + else + { g_usb_hid.boot.in_flight = true; } @@ -692,7 +760,8 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) * - 当前 mode 为 USB; * - USB HID 栈已启用且对应接口 ready。 */ - if (g_usb_hid.current_protocol != HID_PROTO_REPORT) { + if (g_usb_hid.current_protocol != HID_PROTO_REPORT) + { return false; } @@ -700,25 +769,29 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) size_t data_len = hid_tx_event_get_size(event); uint8_t report_id; - if (data_len < 1U) { + if (data_len < 1U) + { submit_usb_tx_done(HID_TX_KIND_REPORT, false); return false; } report_id = 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) + { submit_usb_tx_done(HID_TX_KIND_REPORT, false); return false; } if ((report_id != REPORT_ID_KEYBOARD) && - (report_id != REPORT_ID_CONSUMER) && - (report_id != REPORT_ID_VENDOR)) { + (report_id != REPORT_ID_CONSUMER) && + (report_id != REPORT_ID_VENDOR)) + { submit_usb_tx_done(HID_TX_KIND_REPORT, false); return false; } - if (g_usb_hid.nkro.in_flight) { + if (g_usb_hid.nkro.in_flight) + { LOG_WRN("Drop tx report id=0x%02x: previous report not sent", report_id); submit_usb_tx_done(HID_TX_KIND_REPORT, false); return false; @@ -726,10 +799,13 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) /* Report 协议下 dyndata 是 [report_id|payload],可直接透传。 */ int err = hid_device_submit_report(g_usb_hid.nkro.dev, data_len, data); - if (err) { + if (err) + { LOG_WRN("USB report send failed id=0x%02x err=%d", report_id, err); submit_usb_tx_done(HID_TX_KIND_REPORT, false); - } else { + } + else + { g_usb_hid.nkro.in_flight = true; } @@ -738,23 +814,28 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) static bool app_event_handler(const struct app_event_header *aeh) { - if (is_module_state_event(aeh)) { + if (is_module_state_event(aeh)) + { return handle_module_state_event(cast_module_state_event(aeh)); } - if (is_mode_event(aeh)) { + if (is_mode_event(aeh)) + { return handle_mode_event(cast_mode_event(aeh)); } - if (is_power_down_event(aeh)) { + if (is_power_down_event(aeh)) + { return handle_power_down_event(); } - if (is_wake_up_event(aeh)) { + if (is_wake_up_event(aeh)) + { return handle_wake_up_event(); } - if (is_hid_tx_event(aeh)) { + if (is_hid_tx_event(aeh)) + { return handle_hid_tx_event(cast_hid_tx_event(aeh)); }