#!/bin/sh
#
# USB TEST
# 
# SYNOPSYS:
# # USB_TEST - This test will verify that USB Hub is present, and that 2nd port is populated with 
#              external USB device (ex. USB Flash drive). 
#

DEVICE=sda1

# Unmount USB drive if currently mounted.
MOUNTPOINT=$(mount | grep $DEVICE | sed 's/^[^ ]* on \([^ ]*\) .*/\1/')
if [ ! -z "$MOUNTPOINT" ]; then umount $MOUNTPOINT; fi
rm -rf /tmp/usb_test_mnt

# Mount USB Drive
mkdir /tmp/usb_test_mnt
mount -t vfat /dev/$DEVICE /tmp/usb_test_mnt/
if [ $? != 0 ]; then
	echo "ERROR failed to mount USB device"
	rmdir /tmp/usb_test_mnt
	exit 1
fi

testfs -c 2097152 > /tmp/usb_test_mnt/testfile
if [ $? != "0" ]; then
	echo "ERROR writing to USB device"
	umount /tmp/usb_test_mnt
	rmdir /tmp/usb_test_mnt
	exit 1
fi

# Dismount USB Drive and Re-mount
umount /tmp/usb_test_mnt

mount -t vfat /dev/$DEVICE /tmp/usb_test_mnt/
if [ $? != "0" ]; then
	echo "ERROR failed to remount USB device"
	rmdir /tmp/usb_test_mnt
	exit 1
fi

# Read the file from USB Flash
testfs -v -c 2097152 < /tmp/usb_test_mnt/testfile > /tmp/usb_test_time
if [ $? != "0" ]; then
	echo "ERROR failed to verify test file"
	umount /tmp/usb_test_mnt
	rmdir /tmp/usb_test_mnt
	rm /tmp/usb_test_time
	exit 1
fi

TIME=`cat /tmp/usb_test_time`

umount /tmp/usb_test_mnt
rmdir /tmp/usb_test_mnt
rm /tmp/usb_test_time

if [ "$TIME" -gt 1000 ] ; then
	echo "WARNING - file read took long than expected - nominal test time < 1000 for high speed USB, actual time $TIME"
else
echo "OK - expected time = 140, actual elapsed test time = $TIME"
fi
exit 0
