From 97e2465439eaadd7cbaec644b71919f6d56c17b2 Mon Sep 17 00:00:00 2001 From: liqigan Date: Thu, 9 Sep 2021 15:46:46 +0800 Subject: [PATCH] fix unit test failure for report_len in Boot Protocol --- components/esp_hid/src/bt_hidh.c | 44 ++++++++++++++++--------- components/esp_hid/src/esp_hid_common.c | 6 ++-- components/esp_hid/test/test_esp_hid.c | 6 ++-- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/components/esp_hid/src/bt_hidh.c b/components/esp_hid/src/bt_hidh.c index ed0ab3bc3f..6c3ea158d5 100644 --- a/components/esp_hid/src/bt_hidh.c +++ b/components/esp_hid/src/bt_hidh.c @@ -41,7 +41,7 @@ static hidh_local_param_t hidh_local_param; #define TRANS_TO 1000000 // us #define is_init() (hidh_local_param.event_loop_handle != NULL) -#define get_protocol_mode(mode) (mode) ? "BOOT" : "REPORT" +#define get_protocol_mode(mode) (mode) ? "REPORT" : "BOOT" static const char *s_esp_hh_evt_names[] = {"INIT", "DEINIT", "OPEN", "CLOSE", "GET_RPT", "SET_RPT", "GET_PROTO", "SET_PROTO", "GET_IDLE", "SET_IDLE", "GET_DSCP", "ADD_DEV", "RMV_DEV", "VC_UNPLUG", "DATA", "DATA_IND", "SET_INFO"}; static const char *s_esp_hh_status_names[] = {"OK", "HS_HID_NOT_READY", @@ -654,6 +654,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) break; } case ESP_HIDH_DATA_IND_EVT: { + esp_hid_usage_t _usage; if (param->data_ind.status != ESP_HIDH_OK) { ESP_LOGE(TAG, "DATA_IND ERROR: handle: %d, status: %s", param->data_ind.handle, s_esp_hh_status_names[param->data_ind.status]); @@ -663,30 +664,43 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) ESP_LOGE(TAG, "Device Not Found: handle %u", param->data_ind.handle); break; } - esp_hidh_dev_lock(dev); + if (param->data_ind.len > 0 && param->data_ind.data != NULL) { + esp_hidh_dev_lock(dev); event_data_size += param->data_ind.len; if (param->data_ind.proto_mode == ESP_HID_PROTOCOL_MODE_BOOT) { - // first data is report id - if (param->data_ind.data[0]) { - report = esp_hidh_dev_get_input_report_by_len_and_proto(dev, param->data_ind.len, - ESP_HID_PROTOCOL_MODE_BOOT); + /** + * first data shall have report_id, according to HID_SPEC_V10 + * | Device | Report ID | Report Size | + * -------------------------------------- + * | Keyboard | 1 | 9 Bytes | + * | Mouse | 2 | 4 Bytes | + * | Reserved | 0, 3-255 | N/A | + */ + if (param->data_ind.len == 9 && *(param->data_ind.data) == 1) { has_report_id = true; + _usage = ESP_HID_USAGE_KEYBOARD; + } else if (param->data_ind.len == 4 && *(param->data_ind.data) == 2) { + has_report_id = true; + _usage = ESP_HID_USAGE_MOUSE; } else { esp_hidh_dev_unlock(dev); - ESP_LOGE(TAG, "report_id=0 in boot mode!"); + ESP_LOGE(TAG, "Invalid Boot Report format, rpt_len:%d, rpt_id:%d!", param->data_ind.len, + *(param->data_ind.data)); break; } } else { report = esp_hidh_dev_get_input_report_by_proto_and_data( dev, ESP_HID_PROTOCOL_MODE_REPORT, param->data_ind.len, param->data_ind.data, &has_report_id); + if (report == NULL) { + esp_hidh_dev_unlock(dev); + ESP_LOGE(TAG, "Not find report handle: %d mode: %s", param->data_ind.handle, + param->data_ind.proto_mode == ESP_HID_PROTOCOL_MODE_REPORT ? "REPORT" : "BOOT"); + break; + } + _usage = report->usage; } - if (report == NULL) { - esp_hidh_dev_unlock(dev); - ESP_LOGE(TAG, "Not find report handle: %d mode: %s", param->data_ind.handle, - param->data_ind.proto_mode == ESP_HID_PROTOCOL_MODE_REPORT ? "REPORT" : "BOOT"); - break; - } + if ((p_param = (esp_hidh_event_data_t *)malloc(event_data_size)) == NULL) { esp_hidh_dev_unlock(dev); ESP_LOGE(TAG, "DATA_IND ERROR: malloc event data failed!"); @@ -694,7 +708,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) } memset(p_param, 0, event_data_size); p_param->input.dev = dev; - p_param->input.usage = report->usage; + p_param->input.usage = _usage; if (has_report_id) { data_len = param->data_ind.len - 1; p_data = (uint8_t *)param->data_ind.data + 1; @@ -710,9 +724,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) esp_hidh_dev_unlock(dev); esp_event_post_to(hidh_local_param.event_loop_handle, ESP_HIDH_EVENTS, ESP_HIDH_INPUT_EVENT, p_param, event_data_size, portMAX_DELAY); - break; } - esp_hidh_dev_unlock(dev); break; } case ESP_HIDH_DATA_EVT: diff --git a/components/esp_hid/src/esp_hid_common.c b/components/esp_hid/src/esp_hid_common.c index e6d0cd8544..b549d00663 100644 --- a/components/esp_hid/src/esp_hid_common.c +++ b/components/esp_hid/src/esp_hid_common.c @@ -103,8 +103,7 @@ static int handle_report(hid_report_params_t *report, bool first) } item.protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT; - item.report_id = 0x01; - item.value_len = 9; + item.value_len = 8; if (add_report(map, &item) != 0) { return -1; } @@ -143,8 +142,7 @@ static int handle_report(hid_report_params_t *report, bool first) } item.protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT; - item.report_id = 0x02; - item.value_len = 4; + item.value_len = 3; if (add_report(map, &item) != 0) { return -1; } diff --git a/components/esp_hid/test/test_esp_hid.c b/components/esp_hid/test/test_esp_hid.c index 2bdde5b30e..db9b0d10af 100644 --- a/components/esp_hid/test/test_esp_hid.c +++ b/components/esp_hid/test/test_esp_hid.c @@ -39,7 +39,7 @@ TEST_CASE("can parse hidReportMap", "[esp_hid]") TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_MOUSE); - TEST_ASSERT(report_map->reports[1].value_len == 4); + TEST_ASSERT(report_map->reports[1].value_len == 3); TEST_ASSERT(report_map->reports[2].report_id == 2); TEST_ASSERT(report_map->reports[2].report_type == ESP_HID_REPORT_TYPE_INPUT); TEST_ASSERT(report_map->reports[2].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); @@ -89,7 +89,7 @@ TEST_CASE("can parse relMouseReportMap", "[esp_hid]") TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_MOUSE); - TEST_ASSERT(report_map->reports[1].value_len == 4); + TEST_ASSERT(report_map->reports[1].value_len == 3); esp_hid_free_report_map(report_map); } @@ -109,7 +109,7 @@ TEST_CASE("can parse absMouseReportMap", "[esp_hid]") TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_MOUSE); - TEST_ASSERT(report_map->reports[1].value_len == 4); + TEST_ASSERT(report_map->reports[1].value_len == 3); esp_hid_free_report_map(report_map); }