esp-idf/components/esp_hid/test/test_esp_hid.c
Hristo Gochkov 25281ef4de Add HID Support to IDF
- Adds HID Host support in Buedroid
- Adds BLE HID Host and Device support
- Adds some general HID utilities and definitions to help integrate with other stacks and native USB
2020-04-29 17:24:01 +08:00

215 lines
12 KiB
C

// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/unistd.h>
#include "unity.h"
#include "unity_test_runner.h"
#include "test_utils.h"
#include "esp_log.h"
#include "esp_hid_common.h"
#include "hid_descriptor.h"
TEST_CASE("can parse hidReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(hidReportMap, sizeof(hidReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->appearance == 0x03C1);
TEST_ASSERT(report_map->reports_len == 8);
TEST_ASSERT(report_map->reports[0].report_id == 1);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_MOUSE);
TEST_ASSERT(report_map->reports[0].value_len == 4);
TEST_ASSERT(report_map->reports[1].report_id == 1);
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[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);
TEST_ASSERT(report_map->reports[2].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[2].value_len == 8);
TEST_ASSERT(report_map->reports[3].report_id == 2);
TEST_ASSERT(report_map->reports[3].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[3].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT);
TEST_ASSERT(report_map->reports[3].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[3].value_len == 8);
TEST_ASSERT(report_map->reports[4].report_id == 2);
TEST_ASSERT(report_map->reports[4].report_type == ESP_HID_REPORT_TYPE_OUTPUT);
TEST_ASSERT(report_map->reports[4].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[4].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[4].value_len == 1);
TEST_ASSERT(report_map->reports[5].report_id == 2);
TEST_ASSERT(report_map->reports[5].report_type == ESP_HID_REPORT_TYPE_OUTPUT);
TEST_ASSERT(report_map->reports[5].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT);
TEST_ASSERT(report_map->reports[5].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[5].value_len == 1);
TEST_ASSERT(report_map->reports[6].report_id == 3);
TEST_ASSERT(report_map->reports[6].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[6].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[6].usage == ESP_HID_USAGE_CCONTROL);
TEST_ASSERT(report_map->reports[6].value_len == 2);
TEST_ASSERT(report_map->reports[7].report_id == 4);
TEST_ASSERT(report_map->reports[7].report_type == ESP_HID_REPORT_TYPE_OUTPUT);
TEST_ASSERT(report_map->reports[7].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[7].usage == ESP_HID_USAGE_VENDOR);
TEST_ASSERT(report_map->reports[7].value_len == 127);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse relMouseReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(relMouseReportMap, sizeof(relMouseReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_MOUSE);
TEST_ASSERT(report_map->appearance == 0x03C2);
TEST_ASSERT(report_map->reports_len == 2);
TEST_ASSERT(report_map->reports[0].report_id == 1);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_MOUSE);
TEST_ASSERT(report_map->reports[0].value_len == 4);
TEST_ASSERT(report_map->reports[1].report_id == 1);
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);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse absMouseReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(absMouseReportMap, sizeof(absMouseReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_MOUSE);
TEST_ASSERT(report_map->appearance == 0x03C2);
TEST_ASSERT(report_map->reports_len == 2);
TEST_ASSERT(report_map->reports[0].report_id == 1);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_MOUSE);
TEST_ASSERT(report_map->reports[0].value_len == 6);
TEST_ASSERT(report_map->reports[1].report_id == 1);
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);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse keyboardReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(keyboardReportMap, sizeof(keyboardReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->appearance == 0x03C1);
TEST_ASSERT(report_map->reports_len == 4);
TEST_ASSERT(report_map->reports[0].report_id == 1);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[0].value_len == 7);
TEST_ASSERT(report_map->reports[1].report_id == 1);
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_KEYBOARD);
TEST_ASSERT(report_map->reports[1].value_len == 8);
TEST_ASSERT(report_map->reports[2].report_id == 1);
TEST_ASSERT(report_map->reports[2].report_type == ESP_HID_REPORT_TYPE_OUTPUT);
TEST_ASSERT(report_map->reports[2].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[2].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[2].value_len == 1);
TEST_ASSERT(report_map->reports[3].report_id == 1);
TEST_ASSERT(report_map->reports[3].report_type == ESP_HID_REPORT_TYPE_OUTPUT);
TEST_ASSERT(report_map->reports[3].protocol_mode == ESP_HID_PROTOCOL_MODE_BOOT);
TEST_ASSERT(report_map->reports[3].usage == ESP_HID_USAGE_KEYBOARD);
TEST_ASSERT(report_map->reports[3].value_len == 1);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse joystickReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(joystickReportMap, sizeof(joystickReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_JOYSTICK);
TEST_ASSERT(report_map->appearance == 0x03C3);
TEST_ASSERT(report_map->reports_len == 1);
TEST_ASSERT(report_map->reports[0].report_id == 1);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_JOYSTICK);
TEST_ASSERT(report_map->reports[0].value_len == 8);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse mediaReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(mediaReportMap, sizeof(mediaReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_CCONTROL);
TEST_ASSERT(report_map->appearance == 0x03C1);
TEST_ASSERT(report_map->reports_len == 1);
TEST_ASSERT(report_map->reports[0].report_id == 3);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_CCONTROL);
TEST_ASSERT(report_map->reports[0].value_len == 6);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse mediaReportMap2", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(mediaReportMap2, sizeof(mediaReportMap2));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_CCONTROL);
TEST_ASSERT(report_map->appearance == 0x03C1);
TEST_ASSERT(report_map->reports_len == 1);
TEST_ASSERT(report_map->reports[0].report_id == 3);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_CCONTROL);
TEST_ASSERT(report_map->reports[0].value_len == 2);
esp_hid_free_report_map(report_map);
}
TEST_CASE("can parse hidapiReportMap", "[esp_hid]")
{
esp_hid_report_map_t * report_map = esp_hid_parse_report_map(hidapiReportMap, sizeof(hidapiReportMap));
TEST_ASSERT_NOT_NULL(report_map);
TEST_ASSERT(report_map->usage == ESP_HID_USAGE_GENERIC);
TEST_ASSERT(report_map->appearance == 0x03C0);
TEST_ASSERT(report_map->reports_len == 3);
TEST_ASSERT(report_map->reports[0].report_id == 1);
TEST_ASSERT(report_map->reports[0].report_type == ESP_HID_REPORT_TYPE_INPUT);
TEST_ASSERT(report_map->reports[0].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[0].usage == ESP_HID_USAGE_VENDOR);
TEST_ASSERT(report_map->reports[0].value_len == 8);
TEST_ASSERT(report_map->reports[1].report_id == 1);
TEST_ASSERT(report_map->reports[1].report_type == ESP_HID_REPORT_TYPE_OUTPUT);
TEST_ASSERT(report_map->reports[1].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[1].usage == ESP_HID_USAGE_VENDOR);
TEST_ASSERT(report_map->reports[1].value_len == 8);
TEST_ASSERT(report_map->reports[2].report_id == 1);
TEST_ASSERT(report_map->reports[2].report_type == ESP_HID_REPORT_TYPE_FEATURE);
TEST_ASSERT(report_map->reports[2].protocol_mode == ESP_HID_PROTOCOL_MODE_REPORT);
TEST_ASSERT(report_map->reports[2].usage == ESP_HID_USAGE_VENDOR);
TEST_ASSERT(report_map->reports[2].value_len == 8);
esp_hid_free_report_map(report_map);
}