diff --git a/epicardium/ble/hid_work.c b/epicardium/ble/hid_work.c index 268fa3d9abae09dcf29d6bf6dcd820343dee5bdf..0f6dbdc9916d31197eab854a6f0156fe9847c04c 100644 --- a/epicardium/ble/hid_work.c +++ b/epicardium/ble/hid_work.c @@ -31,7 +31,7 @@ struct /* clang-format on */ struct report { - uint8_t reportId; + uint8_t report_id; uint8_t data[8]; uint8_t len; }; @@ -42,10 +42,20 @@ static QueueHandle_t queue; static uint8_t buffer[sizeof(struct report) * QUEUE_SIZE]; static StaticQueue_t queue_data; -static int hid_queue_data(uint8_t reportId, uint8_t *data, uint8_t len) +static int hid_queue_data(uint8_t report_id, uint8_t *data, uint8_t len) { struct report report; - report.reportId = reportId; + + if (report_id < 1 || report_id > 3) { + return -EINVAL; + } + + report.report_id = report_id; + + if (len > sizeof(report.data)) { + return -EINVAL; + } + memcpy(report.data, data, len); report.len = len; @@ -76,21 +86,21 @@ static bool hid_dequeue_data(dmConnId_t connId) } if (HidGetProtocolMode() == HID_PROTOCOL_MODE_BOOT) { - if (report.reportId == HIDAPP_KEYBOARD_REPORT_ID) { - report.reportId = HID_KEYBOARD_BOOT_ID; - cccHandle = HIDAPP_KBI_CCC_HDL; - } else if (report.reportId == HIDAPP_MOUSE_REPORT_ID) { - report.reportId = HID_MOUSE_BOOT_ID; - cccHandle = HIDAPP_MBI_CCC_HDL; + if (report.report_id == HIDAPP_KEYBOARD_REPORT_ID) { + report.report_id = HID_KEYBOARD_BOOT_ID; + cccHandle = HIDAPP_KBI_CCC_HDL; + } else if (report.report_id == HIDAPP_MOUSE_REPORT_ID) { + report.report_id = HID_MOUSE_BOOT_ID; + cccHandle = HIDAPP_MBI_CCC_HDL; } else { break; } } else { - if (report.reportId == HIDAPP_KEYBOARD_REPORT_ID) { + if (report.report_id == HIDAPP_KEYBOARD_REPORT_ID) { cccHandle = HIDAPP_IN_KEYBOARD_CCC_HDL; - } else if (report.reportId == HIDAPP_MOUSE_REPORT_ID) { + } else if (report.report_id == HIDAPP_MOUSE_REPORT_ID) { cccHandle = HIDAPP_IN_MOUSE_CCC_HDL; - } else if (report.reportId == HIDAPP_CONSUMER_REPORT_ID) { + } else if (report.report_id == HIDAPP_CONSUMER_REPORT_ID) { cccHandle = HIDAPP_IN_CONSUMER_CCC_HDL; } else { break; @@ -102,7 +112,7 @@ static bool hid_dequeue_data(dmConnId_t connId) /* Send the message */ HidSendInputReport( connId, - report.reportId, + report.report_id, report.len, report.data ); @@ -114,27 +124,6 @@ static bool hid_dequeue_data(dmConnId_t connId) return true; } -int epic_ble_hid_send_report(uint8_t reportId, uint8_t *data, uint8_t len) -{ - dmConnId_t connId = AppConnIsOpen(); - if (connId == DM_CONN_ID_NONE) { - return -EIO; - } - - int ret; - ret = hid_queue_data(reportId, data, len); - - if (ret < 0) { - return ret; - } - - if (hid_dequeue_data(connId)) { - return 0; - } else { - return 1; - } -} - /*************************************************************************************************/ /*! * \brief Callback to handle an output report from the host. @@ -218,6 +207,27 @@ void HidProcMsg(wsfMsgHdr_t *pMsg) } } +int epic_ble_hid_send_report(uint8_t report_id, uint8_t *data, uint8_t len) +{ + dmConnId_t connId = AppConnIsOpen(); + if (connId == DM_CONN_ID_NONE) { + return -EIO; + } + + int ret; + ret = hid_queue_data(report_id, data, len); + + if (ret < 0) { + return ret; + } + + if (hid_dequeue_data(connId)) { + return 0; + } else { + return 1; + } +} + void hid_work_init(void) { queue = xQueueCreateStatic( diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 3a00a64783697be953fb5cedfd7d7ef19d0252a9..ca295376f2b0f28653df5a57dcf8e82d46fca2a3 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -2322,28 +2322,22 @@ API(API_BLE_SET_MODE, void epic_ble_set_mode(bool bondable, bool scanner)); */ API(API_BLE_GET_SCAN_REPORT, int epic_ble_get_scan_report(struct epic_scan_report *rpt)); - - -/** - * Human Interface Device (HID) - * ========================== - */ - /** * Send an input report to the host. * - * :param uint8_t reportId: The id of the report to use. 0: remote, 1: keyboard, 2: mouse + * :param uint8_t report_id: The id of the report to use. 1: keyboard, 2: mouse, 3: consumer control * :param uint8_t *data: Data to be reported. - * :param uint8_t len: Length in bytes of the data to be reported. Maximum length in 8 bytes. + * :param uint8_t len: Length in bytes of the data to be reported. Maximum length is 8 bytes. * * :return: `0` on success, `1` if the report is queued or a negative value if an error occured. Possible * errors: * * - ``-EIO``: There is no host device connected * - ``-EAGAIN``: There is no space in the queue available. Try again later. + * - ``-EINVAL``: Either the report_id is out of range or the data is too long. * */ -API(API_BLE_HID_SEND_REPORT, int epic_ble_hid_send_report(uint8_t reportId, uint8_t *data, uint8_t len)); +API(API_BLE_HID_SEND_REPORT, int epic_ble_hid_send_report(uint8_t report_id, uint8_t *data, uint8_t len)); #endif /* _EPICARDIUM_H */