Skip to content

Commit 59e976a

Browse files
Add script for checking USB data connection (#1925)
Resolves tiny-pilot/tinypilot-pro#1636. This PR adds a script for determining the status of the USB data connection, based on the power state. (See docstring comment.) Note that in contrast to the [HDMI check script](#1924), we don’t need root privileges here, so the script can live in `/opt/tinypilot/scripts`. Otherwise, the mechanics of the script are equivalent. I’ve also added the info to the debug logs, just in case and mostly for the sake of completeness. <img width="824" height="902" alt="Screenshot 2025-10-20 at 19 53 13" src="https://github.com/user-attachments/assets/ab471090-e612-44f0-97d3-597b84bfc890" /> <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1925"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a> --------- Co-authored-by: Jan Heuermann <[email protected]>
1 parent c81ba3b commit 59e976a

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

debian-pkg/opt/tinypilot-privileged/scripts/collect-debug-logs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ print_info "Checking if mouse jiggler is enabled..."
136136
echo "Mouse jiggler: ${MOUSE_JIGGLER_STATUS}"
137137
} >> "${LOG_FILE}"
138138

139+
print_info "Checking status of USB data connection (HID)..."
140+
{
141+
echo "USB data connected: $(runuser tinypilot --command '/opt/tinypilot/scripts/check-usb-data-status')"
142+
printf "\n"
143+
} >> "${LOG_FILE}"
144+
139145
print_info "Checking storage..."
140146
{
141147
# Get the available space and total size of the root directory.

scripts/check-usb-data-status

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
#
3+
# This script checks the status of the USB data connection (for transmitting
4+
# HID signals).
5+
# It reports the status as output on stdout (either as 'true' or 'false').
6+
#
7+
# Note that we cannot directly check the healthiness of the USB data connection
8+
# in a reliable way, but we can merely infer it by querying its power state:
9+
# when the TinyPilot device is connected to the target machine via the USB
10+
# data cable, TinyPilot’s USB chip starts to draw power through the USB cable.
11+
# That acts as indication that the target machine is turned on, and that it
12+
# should physically be able to communicate via the USB connection. However,
13+
# it doesn’t necessarily guarantee that the target machine will actually
14+
# accept and process incoming HID signals successfully.
15+
16+
# Exit on unset variable.
17+
set -u
18+
19+
# Exit on first error.
20+
set -e
21+
22+
if (( "${EUID}" == 0 )); then
23+
>&2 echo "This script doesn't require root privileges."
24+
>&2 echo 'Please re-run as tinypilot:'
25+
>&2 echo " runuser tinypilot --command '$0 $*'"
26+
exit 1
27+
fi
28+
29+
print_help() {
30+
cat << EOF
31+
Usage: ${0##*/} [--help | --power | --signal]
32+
33+
Options:
34+
--help Display this help and exit.
35+
EOF
36+
}
37+
38+
# Parse command-line arguments.
39+
while (( "$#" > 0 )); do
40+
case "$1" in
41+
--help)
42+
print_help
43+
exit
44+
;;
45+
*)
46+
>&2 echo "Unknown option: $1"
47+
print_help >&2
48+
exit 1
49+
;;
50+
esac
51+
done
52+
53+
# Determine UDC identifier (UDC="USB device controller").
54+
UDC="$(cat /sys/kernel/config/usb_gadget/g1/UDC)"
55+
readonly UDC
56+
57+
# Check whether the UDC is self-powered or whether it receives power from the
58+
# attached target machine. We use the latter as an indication for a successful
59+
# data connection (see docstring commentary above).
60+
IS_SELFPOWERED="$(cat "/sys/devices/platform/soc/${UDC}/udc/${UDC}/is_selfpowered")"
61+
readonly IS_SELFPOWERED
62+
63+
if [[ "${IS_SELFPOWERED}" == 0 ]]; then
64+
echo 'true'
65+
else
66+
echo 'false'
67+
fi

0 commit comments

Comments
 (0)