FDD-book/examples/part-06/ch26-usb-serial/lab03-bulk-loopback/test_echo.sh

31 lines
679 B
Bash
Raw Normal View History

#!/bin/sh
#
# test_echo.sh - Minimal test for the bulk-loopback driver.
#
# Writes a short string to /dev/myfirst_usb0 and reads the result
# back. Exits 0 on match, 1 on mismatch.
set -eu
DEV=${DEV:-/dev/myfirst_usb0}
MSG=${MSG:-"hello usb"}
if [ ! -c "${DEV}" ]; then
echo "Device ${DEV} not present. Is the driver loaded and the"
echo "test hardware plugged in?"
exit 2
fi
echo "${MSG}" > "${DEV}"
# Read back up to len(MSG)+1 bytes. Allow short read.
got=$(dd if="${DEV}" bs=1 count=$((${#MSG}+1)) 2>/dev/null | tr -d '\n')
if [ "${got}" = "${MSG}" ]; then
echo "OK: echo loopback returned '${got}'"
exit 0
else
echo "FAIL: sent '${MSG}' got '${got}'"
exit 1
fi