Skip to content

Commit 097a9ac

Browse files
author
Filip Jagodzinski
committed
Tests: USBHID: Make report test optional on Linux
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.
1 parent fbeefab commit 097a9ac

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

TESTS/host_tests/usb_device_hid.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import threading
2121
import uuid
2222
import mbed_host_tests
23-
import hid
2423
import usb.core
2524
from usb.util import (
2625
CTRL_IN,
@@ -32,6 +31,12 @@
3231
DESC_TYPE_CONFIG,
3332
build_request_type)
3433

34+
try:
35+
import hid
36+
except ImportError:
37+
CYTHON_HIDAPI_PRESENT = False
38+
else:
39+
CYTHON_HIDAPI_PRESENT = True
3540

3641
# USB device -- device classes
3742
USB_CLASS_HID = 0x03
@@ -81,6 +86,7 @@
8186
MSG_KEY_TEST_CASE_FAILED = 'fail'
8287
MSG_KEY_TEST_CASE_PASSED = 'pass'
8388
MSG_VALUE_DUMMY = '0'
89+
MSG_VALUE_NOT_SUPPORTED = 'not_supported'
8490

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

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

545553
def cb_test_raw_io(self, key, value, timestamp):
546554
"""Receive HID reports and send them back to the device."""
555+
if not CYTHON_HIDAPI_PRESENT:
556+
self.send_kv(MSG_KEY_HOST_READY, MSG_VALUE_NOT_SUPPORTED)
557+
return
547558
try:
548559
# The size of input and output reports used in test.
549560
report_size = int(value)

TESTS/usb_device/hid/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Testing the USB HID device with a Linux host
2+
3+
Before running `tests-usb_device-hid` test suite on a Linux machine, please
4+
make sure to install the `hidapi` Python module first, otherwise some test
5+
cases will be skipped. Due to external dependencies for Linux, this module
6+
is not installed during the initial setup, to keep the process as simple
7+
as possible.
8+
9+
For Debian-based Linux distros, the dependencies can be installed as follows
10+
(based on module's [README][1]):
11+
12+
```bash
13+
apt-get install python-dev libusb-1.0-0-dev libudev-dev
14+
pip install --upgrade setuptools
15+
```
16+
To install the `hidapi` module itself, please use the attached
17+
`TESTS/usb_device/hid/requirements.txt` file:
18+
```bash
19+
pip install -r requirements.txt
20+
```
21+
22+
[1]: https://github.com/trezor/cython-hidapi/blob/master/README.rst#install
23+

TESTS/usb_device/hid/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#define MSG_KEY_TEST_CASE_FAILED "fail"
5050
#define MSG_KEY_TEST_CASE_PASSED "pass"
5151
#define MSG_VALUE_DUMMY "0"
52+
#define MSG_VALUE_NOT_SUPPORTED "not_supported"
5253

5354
#define RAW_IO_REPS 16
5455

@@ -310,6 +311,10 @@ void test_generic_raw_io()
310311
char value[MSG_VALUE_LEN + 1] = { };
311312
greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
312313
TEST_ASSERT_EQUAL_STRING(MSG_KEY_HOST_READY, key);
314+
if (strcmp(value, MSG_VALUE_NOT_SUPPORTED) == 0) {
315+
TEST_IGNORE_MESSAGE("Test case not supported by host plarform.");
316+
return;
317+
}
313318

314319
// Report ID omitted here. There are no Report ID tags in the Report descriptor.
315320
HID_REPORT input_report = {};

TESTS/usb_device/hid/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hidapi>=0.7.99,<0.8.0

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ git+https://github.com/armmbed/[email protected]
2323
icetea>=1.2.1,<1.3
2424
pycryptodome>=3.7.2,<=3.7.3
2525
pyusb>=1.0.0,<2.0.0
26-
hidapi>=0.7.99,<0.8.0
26+
hidapi>=0.7.99,<0.8.0;platform_system!="Linux"
2727
cmsis-pack-manager>=0.2.3,<0.3.0

0 commit comments

Comments
 (0)