Skip to content

Commit

Permalink
Tests: USBHID: Make report test optional on Linux
Browse files Browse the repository at this point in the history
This test case uses `hidapi` -- a cross-platform Python module.
To keep the initial Mbed setup as simple as possible, the `hidapi`
module is skipped on Linux hosts because of its external dependancies
for this platform.

The module can be easily installed following instructions from the
README file.
The test case is skipped if the host machine lacks `hidapi` module.
  • Loading branch information
Filip Jagodzinski committed Apr 9, 2019
1 parent 111f485 commit 852390f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
13 changes: 12 additions & 1 deletion TESTS/host_tests/usb_device_hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import threading
import uuid
import mbed_host_tests
import hid
import usb.core
from usb.util import (
CTRL_IN,
Expand All @@ -32,6 +31,12 @@
DESC_TYPE_CONFIG,
build_request_type)

try:
import hid
except ImportError:
CYTHON_HIDAPI_PRESENT = False
else:
CYTHON_HIDAPI_PRESENT = True

# USB device -- device classes
USB_CLASS_HID = 0x03
Expand Down Expand Up @@ -81,6 +86,7 @@
MSG_KEY_TEST_CASE_FAILED = 'fail'
MSG_KEY_TEST_CASE_PASSED = 'pass'
MSG_VALUE_DUMMY = '0'
MSG_VALUE_NOT_SUPPORTED = 'not_supported'

# Constants for the tests.
KEYBOARD_IDLE_RATE_TO_SET = 0x00 # Duration = 0 (indefinite)
Expand All @@ -95,6 +101,8 @@ def build_get_desc_value(desc_type, desc_index):

def usb_hid_path(serial_number):
"""Get a USB HID device system path based on the serial number."""
if not CYTHON_HIDAPI_PRESENT:
return None
for device_info in hid.enumerate(): # pylint: disable=no-member
if device_info.get('serial_number') == serial_number: # pylint: disable=not-callable
return device_info['path']
Expand Down Expand Up @@ -544,6 +552,9 @@ def start_bg_task(self, **thread_kwargs):

def cb_test_raw_io(self, key, value, timestamp):
"""Receive HID reports and send them back to the device."""
if not CYTHON_HIDAPI_PRESENT:
self.send_kv(MSG_KEY_HOST_READY, MSG_VALUE_NOT_SUPPORTED)
return
try:
# The size of input and output reports used in test.
report_size = int(value)
Expand Down
23 changes: 23 additions & 0 deletions TESTS/usb_device/hid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Testing the USB HID device with a Linux host

Before running `tests-usb_device-hid` test suite on a Linux machine, please
make sure to install the `hidapi` Python module first, otherwise some test
cases will be skipped. Due to external dependencies for Linux, this module
is not installed during the initial setup, to keep the process as simple
as possible.

For Debian-based Linux distros, the dependencies can be installed as follows
(based on module's [README][1]):

```bash
apt-get install python-dev libusb-1.0-0-dev libudev-dev
pip install --upgrade setuptools
```
To install the `hidapi` module itself, please use the attached
`TESTS/usb_device/hid/requirements.txt` file:
```bash
pip install -r requirements.txt
```

[1]: https://github.com/trezor/cython-hidapi/blob/master/README.rst#install

5 changes: 5 additions & 0 deletions TESTS/usb_device/hid/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#define MSG_KEY_TEST_CASE_FAILED "fail"
#define MSG_KEY_TEST_CASE_PASSED "pass"
#define MSG_VALUE_DUMMY "0"
#define MSG_VALUE_NOT_SUPPORTED "not_supported"

#define RAW_IO_REPS 16

Expand Down Expand Up @@ -310,6 +311,10 @@ void test_generic_raw_io()
char value[MSG_VALUE_LEN + 1] = { };
greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
TEST_ASSERT_EQUAL_STRING(MSG_KEY_HOST_READY, key);
if (strcmp(value, MSG_VALUE_NOT_SUPPORTED) == 0) {
TEST_IGNORE_MESSAGE("Test case not supported by host plarform.");
return;
}

// Report ID omitted here. There are no Report ID tags in the Report descriptor.
HID_REPORT input_report = {};
Expand Down
1 change: 1 addition & 0 deletions TESTS/usb_device/hid/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hidapi>=0.7.99,<0.8.0
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ manifest-tool==1.4.8
icetea>=1.2.1,<1.3
pycryptodome>=3.7.2,<=3.7.3
pyusb>=1.0.0,<2.0.0
hidapi>=0.7.99,<0.8.0
hidapi>=0.7.99,<0.8.0;platform_system!="Linux"
cmsis-pack-manager>=0.2.3,<0.3.0

0 comments on commit 852390f

Please sign in to comment.