Skip to content

Commit 97c0a95

Browse files
committed
Add test script to validate USB HID
The shell script verifies the enumeration of USB Human Interface Devices. Signed-off-by: “Aanchal <achauras@qti.qualcomm.com>
1 parent ba7c32f commit 97c0a95

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

  • Runner/suites/Kernel/Baseport/usb_hid
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

0 commit comments

Comments
 (0)