feat(usb_hid_keyboard): 支持键盘LED报告处理

- 添加KBD_LED_REPORT_WITH_ID_SIZE宏定义以支持带ID的LED报告
- 实现keyboard_handle_led_report函数来处理不同长度的LED报告
- 在keyboard_set_report中添加对HID_REPORT_TYPE_OUTPUT类型的支持
- 优化keyboard_output_report函数以复用LED报告处理逻辑
- 移除datetime_event中的INIT_LOG_ENABLE标志

支持处理长度为1字节或2字节(包含report ID)的键盘LED报告,
并提供适当的错误日志记录功能。
This commit is contained in:
2026-04-15 17:54:15 +08:00
parent bd57b00080
commit 2ca02325c1
3 changed files with 47 additions and 14 deletions

View File

@@ -30,7 +30,7 @@
label = "HID_KBD";
protocol-code = "keyboard";
in-report-size = <29>;
out-report-size = <1>;
out-report-size = <29>;
in-polling-period-us = <1000>;
out-polling-period-us = <1000>;
};

View File

@@ -23,5 +23,4 @@ APP_EVENT_INFO_DEFINE(datetime_event,
APP_EVENT_TYPE_DEFINE(datetime_event,
log_datetime_event,
&datetime_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
APP_EVENT_FLAGS_CREATE());

View File

@@ -26,6 +26,7 @@
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define KBD_LED_REPORT_SIZE 1U
#define KBD_LED_REPORT_WITH_ID_SIZE (KBD_LED_REPORT_SIZE + 1U)
static const struct device *const hid_dev = DEVICE_DT_GET(DT_NODELABEL(hid_kbd));
@@ -73,6 +74,36 @@ static void keyboard_iface_ready(const struct device *dev, const bool ready)
publish_keyboard_state();
}
static int keyboard_handle_led_report(const char *source, uint8_t type, uint8_t id,
uint16_t len, const uint8_t *buf)
{
uint8_t led_bm;
if (buf == NULL) {
LOG_WRN("%s invalid keyboard report", source);
return -EINVAL;
}
if (len == KBD_LED_REPORT_SIZE) {
led_bm = buf[0];
} else if (len == KBD_LED_REPORT_WITH_ID_SIZE) {
led_bm = buf[1];
} else {
LOG_WRN("%s unexpected len %u type %u id %u",
source, len, type, id);
if (len >= KBD_LED_REPORT_WITH_ID_SIZE) {
LOG_WRN("%s possible report_id=0x%02x led_bm=0x%02x",
source, buf[0], buf[1]);
}
return -EMSGSIZE;
}
submit_hid_led_event(HID_TRANSPORT_USB, led_bm);
return 0;
}
static int keyboard_get_report(const struct device *dev,
const uint8_t type, const uint8_t id,
const uint16_t len, uint8_t *const buf)
@@ -91,14 +122,20 @@ static int keyboard_set_report(const struct device *dev,
const uint16_t len, const uint8_t *const buf)
{
ARG_UNUSED(dev);
ARG_UNUSED(type);
ARG_UNUSED(id);
ARG_UNUSED(len);
ARG_UNUSED(buf);
if (type != HID_REPORT_TYPE_OUTPUT) {
LOG_WRN("USB keyboard set_report unsupported type %u id %u len %u",
type, id, len);
if (buf != NULL) {
LOG_HEXDUMP_INF(buf, len, "USB keyboard set_report raw");
}
return -ENOTSUP;
}
return keyboard_handle_led_report("USB keyboard set_report", type, id,
len, buf);
}
static void keyboard_set_idle(const struct device *dev,
const uint8_t id, const uint32_t duration)
{
@@ -156,12 +193,9 @@ static void keyboard_output_report(const struct device *dev,
{
ARG_UNUSED(dev);
if ((len < KBD_LED_REPORT_SIZE) || (buf == NULL)) {
LOG_WRN("Invalid keyboard output report");
return;
}
submit_hid_led_event(HID_TRANSPORT_USB, buf[0]);
(void)keyboard_handle_led_report("USB keyboard output report",
HID_REPORT_TYPE_OUTPUT, 0U,
len, buf);
}
static const struct hid_device_ops keyboard_ops = {