1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <sys/unistd.h> 19 #include "unity.h" 20 #include "unity_test_runner.h" 21 #include "test_utils.h" 22 #include "esp_log.h" 23 #include "esp_hid_common.h" 24 #include "hid_descriptor.h" 25 26 TEST_CASE("can parse hidReportMap", "[esp_hid]") 27 { 28 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(hidReportMap, sizeof(hidReportMap)); 29 TEST_ASSERT_NOT_NULL(report_map); 30 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_KEYBOARD); 31 TEST_ASSERT(report_map->appearance == 0x03C1); 32 TEST_ASSERT(report_map->reports_len == 8); 33 TEST_ASSERT(report_map->reports[0].report_id == 1); 34 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 35 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 36 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_MOUSE); 37 TEST_ASSERT(report_map->reports[0].value_len == 4); 38 TEST_ASSERT(report_map->reports[1].report_id == 1); 39 TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); 40 TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 41 TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_MOUSE); 42 TEST_ASSERT(report_map->reports[1].value_len == 3); 43 TEST_ASSERT(report_map->reports[2].report_id == 2); 44 TEST_ASSERT(report_map->reports[2].report_type == ESP_HID_REPORT_TYPE_INPUT); 45 TEST_ASSERT(report_map->reports[2].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 46 TEST_ASSERT(report_map->reports[2].usage == ESP_HID_USAGE_KEYBOARD); 47 TEST_ASSERT(report_map->reports[2].value_len == 8); 48 TEST_ASSERT(report_map->reports[3].report_id == 2); 49 TEST_ASSERT(report_map->reports[3].report_type == ESP_HID_REPORT_TYPE_INPUT); 50 TEST_ASSERT(report_map->reports[3].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 51 TEST_ASSERT(report_map->reports[3].usage == ESP_HID_USAGE_KEYBOARD); 52 TEST_ASSERT(report_map->reports[3].value_len == 8); 53 TEST_ASSERT(report_map->reports[4].report_id == 2); 54 TEST_ASSERT(report_map->reports[4].report_type == ESP_HID_REPORT_TYPE_OUTPUT); 55 TEST_ASSERT(report_map->reports[4].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 56 TEST_ASSERT(report_map->reports[4].usage == ESP_HID_USAGE_KEYBOARD); 57 TEST_ASSERT(report_map->reports[4].value_len == 1); 58 TEST_ASSERT(report_map->reports[5].report_id == 2); 59 TEST_ASSERT(report_map->reports[5].report_type == ESP_HID_REPORT_TYPE_OUTPUT); 60 TEST_ASSERT(report_map->reports[5].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 61 TEST_ASSERT(report_map->reports[5].usage == ESP_HID_USAGE_KEYBOARD); 62 TEST_ASSERT(report_map->reports[5].value_len == 1); 63 TEST_ASSERT(report_map->reports[6].report_id == 3); 64 TEST_ASSERT(report_map->reports[6].report_type == ESP_HID_REPORT_TYPE_INPUT); 65 TEST_ASSERT(report_map->reports[6].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 66 TEST_ASSERT(report_map->reports[6].usage == ESP_HID_USAGE_CCONTROL); 67 TEST_ASSERT(report_map->reports[6].value_len == 2); 68 TEST_ASSERT(report_map->reports[7].report_id == 4); 69 TEST_ASSERT(report_map->reports[7].report_type == ESP_HID_REPORT_TYPE_OUTPUT); 70 TEST_ASSERT(report_map->reports[7].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 71 TEST_ASSERT(report_map->reports[7].usage == ESP_HID_USAGE_VENDOR); 72 TEST_ASSERT(report_map->reports[7].value_len == 127); 73 esp_hid_free_report_map(report_map); 74 } 75 76 TEST_CASE("can parse relMouseReportMap", "[esp_hid]") 77 { 78 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(relMouseReportMap, sizeof(relMouseReportMap)); 79 TEST_ASSERT_NOT_NULL(report_map); 80 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_MOUSE); 81 TEST_ASSERT(report_map->appearance == 0x03C2); 82 TEST_ASSERT(report_map->reports_len == 2); 83 TEST_ASSERT(report_map->reports[0].report_id == 1); 84 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 85 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 86 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_MOUSE); 87 TEST_ASSERT(report_map->reports[0].value_len == 4); 88 TEST_ASSERT(report_map->reports[1].report_id == 1); 89 TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); 90 TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 91 TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_MOUSE); 92 TEST_ASSERT(report_map->reports[1].value_len == 3); 93 esp_hid_free_report_map(report_map); 94 } 95 96 TEST_CASE("can parse absMouseReportMap", "[esp_hid]") 97 { 98 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(absMouseReportMap, sizeof(absMouseReportMap)); 99 TEST_ASSERT_NOT_NULL(report_map); 100 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_MOUSE); 101 TEST_ASSERT(report_map->appearance == 0x03C2); 102 TEST_ASSERT(report_map->reports_len == 2); 103 TEST_ASSERT(report_map->reports[0].report_id == 1); 104 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 105 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 106 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_MOUSE); 107 TEST_ASSERT(report_map->reports[0].value_len == 6); 108 TEST_ASSERT(report_map->reports[1].report_id == 1); 109 TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); 110 TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 111 TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_MOUSE); 112 TEST_ASSERT(report_map->reports[1].value_len == 3); 113 esp_hid_free_report_map(report_map); 114 } 115 116 TEST_CASE("can parse keyboardReportMap", "[esp_hid]") 117 { 118 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(keyboardReportMap, sizeof(keyboardReportMap)); 119 TEST_ASSERT_NOT_NULL(report_map); 120 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_KEYBOARD); 121 TEST_ASSERT(report_map->appearance == 0x03C1); 122 TEST_ASSERT(report_map->reports_len == 4); 123 TEST_ASSERT(report_map->reports[0].report_id == 1); 124 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 125 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 126 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_KEYBOARD); 127 TEST_ASSERT(report_map->reports[0].value_len == 7); 128 TEST_ASSERT(report_map->reports[1].report_id == 1); 129 TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_INPUT); 130 TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 131 TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_KEYBOARD); 132 TEST_ASSERT(report_map->reports[1].value_len == 8); 133 TEST_ASSERT(report_map->reports[2].report_id == 1); 134 TEST_ASSERT(report_map->reports[2].report_type == ESP_HID_REPORT_TYPE_OUTPUT); 135 TEST_ASSERT(report_map->reports[2].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 136 TEST_ASSERT(report_map->reports[2].usage == ESP_HID_USAGE_KEYBOARD); 137 TEST_ASSERT(report_map->reports[2].value_len == 1); 138 TEST_ASSERT(report_map->reports[3].report_id == 1); 139 TEST_ASSERT(report_map->reports[3].report_type == ESP_HID_REPORT_TYPE_OUTPUT); 140 TEST_ASSERT(report_map->reports[3].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT); 141 TEST_ASSERT(report_map->reports[3].usage == ESP_HID_USAGE_KEYBOARD); 142 TEST_ASSERT(report_map->reports[3].value_len == 1); 143 esp_hid_free_report_map(report_map); 144 } 145 146 TEST_CASE("can parse joystickReportMap", "[esp_hid]") 147 { 148 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(joystickReportMap, sizeof(joystickReportMap)); 149 TEST_ASSERT_NOT_NULL(report_map); 150 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_JOYSTICK); 151 TEST_ASSERT(report_map->appearance == 0x03C3); 152 TEST_ASSERT(report_map->reports_len == 1); 153 TEST_ASSERT(report_map->reports[0].report_id == 1); 154 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 155 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 156 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_JOYSTICK); 157 TEST_ASSERT(report_map->reports[0].value_len == 8); 158 esp_hid_free_report_map(report_map); 159 } 160 161 TEST_CASE("can parse mediaReportMap", "[esp_hid]") 162 { 163 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(mediaReportMap, sizeof(mediaReportMap)); 164 TEST_ASSERT_NOT_NULL(report_map); 165 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_CCONTROL); 166 TEST_ASSERT(report_map->appearance == 0x03C1); 167 TEST_ASSERT(report_map->reports_len == 1); 168 TEST_ASSERT(report_map->reports[0].report_id == 3); 169 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 170 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 171 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_CCONTROL); 172 TEST_ASSERT(report_map->reports[0].value_len == 6); 173 esp_hid_free_report_map(report_map); 174 } 175 176 TEST_CASE("can parse mediaReportMap2", "[esp_hid]") 177 { 178 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(mediaReportMap2, sizeof(mediaReportMap2)); 179 TEST_ASSERT_NOT_NULL(report_map); 180 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_CCONTROL); 181 TEST_ASSERT(report_map->appearance == 0x03C1); 182 TEST_ASSERT(report_map->reports_len == 1); 183 TEST_ASSERT(report_map->reports[0].report_id == 3); 184 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 185 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 186 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_CCONTROL); 187 TEST_ASSERT(report_map->reports[0].value_len == 2); 188 esp_hid_free_report_map(report_map); 189 } 190 191 TEST_CASE("can parse hidapiReportMap", "[esp_hid]") 192 { 193 esp_hid_report_map_t * report_map = esp_hid_parse_report_map(hidapiReportMap, sizeof(hidapiReportMap)); 194 TEST_ASSERT_NOT_NULL(report_map); 195 TEST_ASSERT(report_map->usage == ESP_HID_USAGE_GENERIC); 196 TEST_ASSERT(report_map->appearance == 0x03C0); 197 TEST_ASSERT(report_map->reports_len == 3); 198 TEST_ASSERT(report_map->reports[0].report_id == 1); 199 TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT); 200 TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 201 TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_VENDOR); 202 TEST_ASSERT(report_map->reports[0].value_len == 8); 203 TEST_ASSERT(report_map->reports[1].report_id == 1); 204 TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_OUTPUT); 205 TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 206 TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_VENDOR); 207 TEST_ASSERT(report_map->reports[1].value_len == 8); 208 TEST_ASSERT(report_map->reports[2].report_id == 1); 209 TEST_ASSERT(report_map->reports[2].report_type == ESP_HID_REPORT_TYPE_FEATURE); 210 TEST_ASSERT(report_map->reports[2].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT); 211 TEST_ASSERT(report_map->reports[2].usage == ESP_HID_USAGE_VENDOR); 212 TEST_ASSERT(report_map->reports[2].value_len == 8); 213 esp_hid_free_report_map(report_map); 214 } 215