Skip to content

Commit 3725379

Browse files
authored
Merge pull request #259 from achauras-qcom/feature/usb-hid
Add test script to validate USB HID
2 parents ba7c32f + db1fdad commit 3725379

3 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
# USB HID Validation
5+
6+
## Overview
7+
8+
This shell script executes on the DUT (Device-Under-Test) and verifies enumeration of connected USB Human Interface Devices (HID).
9+
10+
---
11+
12+
## Setup
13+
14+
- Connect USB HID peripheral(s) to USB port(s) on DUT.
15+
- Only applicable for USB ports that support Host Mode functionality.
16+
- USB HID peripherals examples: Mouse, Keyboard, USB headset, etc.
17+
18+
---
19+
20+
## Usage
21+
### Instructions:
22+
1. **Copy the test suite to the target device** using `scp` or any preferred method.
23+
2. **Navigate to the test directory** on the target device.
24+
3. **Run the test script** using the test runner or directly.
25+
26+
---
27+
28+
### Quick Example
29+
```
30+
cd Runner
31+
./run-test.sh usb_hid
32+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Validate USB HID device detection
7+
# Requires at least one USB HID peripheral (keyboard/mouse, etc.) connected to a USB Host port.
8+
9+
TESTNAME="usb_hid"
10+
11+
# Robustly find and source init_env
12+
SCRIPT_DIR="$(
13+
cd "$(dirname "$0")" || exit 1
14+
pwd
15+
)"
16+
17+
# Default result file (works even before functestlib is available)
18+
# shellcheck disable=SC2034
19+
RES_FILE="$SCRIPT_DIR/${TESTNAME}.res"
20+
21+
INIT_ENV=""
22+
SEARCH="$SCRIPT_DIR"
23+
while [ "$SEARCH" != "/" ]; do
24+
if [ -f "$SEARCH/init_env" ]; then
25+
INIT_ENV="$SEARCH/init_env"
26+
break
27+
fi
28+
SEARCH=$(dirname "$SEARCH")
29+
done
30+
31+
if [ -z "$INIT_ENV" ]; then
32+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
33+
echo "$TESTNAME SKIP" >"$RES_FILE" 2>/dev/null || true
34+
exit 0
35+
fi
36+
37+
# Only source if not already loaded (idempotent)
38+
if [ -z "${__INIT_ENV_LOADED:-}" ]; then
39+
# shellcheck disable=SC1090
40+
. "$INIT_ENV"
41+
__INIT_ENV_LOADED=1
42+
fi
43+
# Always source functestlib.sh, using $TOOLS exported by init_env
44+
# shellcheck disable=SC1090,SC1091
45+
. "$TOOLS/functestlib.sh"
46+
47+
# Resolve test path and cd (single SKIP/exit path)
48+
SKIP_REASON=""
49+
test_path=$(find_test_case_by_name "$TESTNAME")
50+
if [ -z "$test_path" ] || [ ! -d "$test_path" ]; then
51+
SKIP_REASON="$TESTNAME SKIP - test path not found"
52+
elif ! cd "$test_path"; then
53+
SKIP_REASON="$TESTNAME SKIP - cannot cd into $test_path"
54+
else
55+
RES_FILE="$test_path/${TESTNAME}.res"
56+
fi
57+
58+
if [ -n "$SKIP_REASON" ]; then
59+
log_skip "$SKIP_REASON"
60+
echo "$TESTNAME SKIP" >"$RES_FILE" 2>/dev/null || true
61+
exit 0
62+
fi
63+
64+
log_info "-----------------------------------------------------------------------------------------"
65+
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
66+
log_info "=== Test Initialization ==="
67+
68+
# Check if grep is installed, else skip test
69+
deps_list="grep sed sort wc"
70+
if ! check_dependencies "$deps_list"; then
71+
log_skip "$TESTNAME SKIP - missing dependencies: $deps_list"
72+
echo "$TESTNAME SKIP" >"$RES_FILE"
73+
exit 0
74+
fi
75+
76+
# Count uniques devices with bInterfaceClass = 03 (HID) under /sys/bus/usb/devices
77+
hid_device_count=0
78+
log_info "=== USB HID device Detection ==="
79+
hid_device_count=$(
80+
for f in /sys/bus/usb/devices/*/bInterfaceClass; do
81+
[ -r "$f" ] || continue
82+
if grep -qx '03' "$f"; then
83+
d=${f%/bInterfaceClass}
84+
echo "${d##*/}"
85+
fi
86+
done 2>/dev/null | sed 's/:.*$//' | sort -u | wc -l | tr -d '[:space:]'
87+
)
88+
89+
log_info "Number of HID devices found: $hid_device_count"
90+
91+
if [ "$hid_device_count" -gt 0 ]; then
92+
log_pass "$TESTNAME : Test Passed - USB HID device(s) detected"
93+
echo "$TESTNAME PASS" > "$RES_FILE"
94+
exit 0
95+
else
96+
log_fail "$TESTNAME : Test Failed - No USB 'Human Interface Device' found"
97+
echo "$TESTNAME FAIL" > "$RES_FILE"
98+
exit 0
99+
fi
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
metadata:
2+
name: usb_hid
3+
format: "Lava-Test Test Definition 1.0"
4+
description: "This shell script executes on the DUT (Device-Under-Test) and verifies enumeration of connected USB Human Interface Devices (HID)."
5+
os:
6+
- linux
7+
scope:
8+
- functional
9+
10+
run:
11+
steps:
12+
- REPO_PATH=$PWD
13+
- cd Runner/suites/Kernel/Baseport/usb_hid
14+
- ./run.sh || true
15+
- $REPO_PATH/Runner/utils/send-to-lava.sh usb_hid.res
16+

0 commit comments

Comments
 (0)