diff --git a/boards/atguigu/mini_keyboard/mini_keyboard.dts b/boards/atguigu/mini_keyboard/mini_keyboard.dts index 487161c..c117f26 100644 --- a/boards/atguigu/mini_keyboard/mini_keyboard.dts +++ b/boards/atguigu/mini_keyboard/mini_keyboard.dts @@ -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>; }; diff --git a/src/events/datetime_event.c b/src/events/datetime_event.c index 254ee65..bdbae43 100644 --- a/src/events/datetime_event.c +++ b/src/events/datetime_event.c @@ -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()); diff --git a/src/usb_hid_keyboard_module.c b/src/usb_hid_keyboard_module.c index 1186bfe..e5f9375 100644 --- a/src/usb_hid_keyboard_module.c +++ b/src/usb_hid_keyboard_module.c @@ -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,12 +122,18 @@ 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); - return -ENOTSUP; + 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, @@ -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 = {