# "sbcTest" (note capital "T")
# Usage: sbcTest <d>

# See '/u/x/7552' for utilities/etc. used by this script:
# 'xuartctl'
# 'canctl'

# Source "ts7500.subr", to have the support functions available
. ./ts7500.subr

# ANSI codes:
#
# This script uses ANSI codes to set the fore/background
# colors, to highlight PASS/FAIL strings
#
# "\033["= ESC code
# "##m"= color code, where "##"= color number
# "\033[##m"= set to color ##
#
# EXAMPLE:
#	echo -e "\033[31m --Hello"	NOTE the [-e]
#	(prints " --Hello" in red)
#
# 0 = Reset to default color scheme
# 1 = Bold
# 2 = Dim
# 5 = Blink
# 7 = Reverse video
# 11= Show char. as GFX
# 25= NO blink
# 27= NO rev. vid.

# ----------Foreground
# 30= Black
# 31= Red
# 32= Green
# 33= Yellow
# 34= Blue
# 35= Violet
# 36= Light Blue
# 37= White
# ----------Background
# same scheme as Foreground,
# but using 40-47
#
#
Acls="\033[2J\033[1H"		# Clear screen
Adef="\033[0m"			# Default colors
Afail="\033[30m\033[41m"	# Black on Red
Apass="\033[37m\033[42m"	# White on Green

# Clear the screen & set default color scheme
echo -e "${Acls}${Adef}"

# For debuging (print test info)
if [ "$1" = "d" ]; then
	debug="yes"
else	
	debug="no"
fi	

#===========================================================
# Determine wether to test a 7500/7550 board
# or the 752 base board
# If the a 9475 is present, DIO-6 and DIO-19 should be
# looped-back; if not, a 752 should be present
# Returns (0) for a 7500/7550, (1) for a 752
getBoard()
{
        # Set pin 19 as INPUT; set pin 6 LOW
        # If pin 19 is also LOW, we (should) have a loopback
        # so the 7500/7550 is mounted on a 9475
        setdiopin 19 z
        setdiopin 6 0
        ret=`eval getdiopin 19`
        return $ret
}


#============================================================
# Thest the USB ports on 7500/7550 boards
#
testUSB()
{
	# Test the USB ports (2)
	# Look in dmesg for "...usb 1-1" or "...usb 1-2"
	dmesg | grep "usb 1-1" > /dev/null
	if [ $? = "0" ]
	then
		echo -e "| USB-0 \t\t${Apass}PASS${Adef}"
	else
		echo -e "| USB-0 \t\t${Afail}FAIL${Adef} (not present?)"
	fi
	
	dmesg | grep "usb 1-2" > /dev/null
	if [ $? = "0" ]
	then
		echo -e "| USB-1 \t\t${Apass}PASS${Adef}"
	else
		echo -e "| USB-1 \t\t${Afail}FAIL${Adef} (not present?)"
	fi
}


#============================================================
test7500()
{
	# Turn OFF the green LED
	# Tuen ON the red LED... to show we are testing
	ts7500ctl -b
	ts7500ctl -c
	
	# Test RJ4 loop-back
	#--------------------------------------------------
	# NOTE: (a standard ETH. cable must be connected
	# between the 7500 & 9475 boards)
	# Set DIO_05 HIGH, expect:
	#	DIO_20 HIGH
	#	Loop_38 & Loop_40 (DIO_37 -- DIO_40) LOW
	# Set DIO_05 LOW, expect:
	#	DIO_20 LOW
	#	Loop_38 & Loop_40 (DIO_37 -- DIO_40) HIGH

	POE_res="${Apass}PASS${Adef}"
	echo -n "| POE Loop-back:"
	if [ $debug = "y" ]
	then
		echo "|"
	fi

	# Set the DIO pins as inputs
	POEset="20 37 38 39 40"
	for i in $POEset; do
		setdiopin $i z
	done

	setdiopin 5 1
	val1=`eval getdiopin 20`
	val2=`eval getdiopin 37`
	val3=`eval getdiopin 38`
	val4=`eval getdiopin 39`
	val5=`eval getdiopin 40`
	# If 20 is LOW, fail
	# If any of 37-40 are HIGH, fail
	if [ $val1 = "0" -o $val2 = "1" -o $val3 = "1" -o $val4 = "1" -o $val5 = "1" ]
	then
		POE_res="${Afail}FAIL${Adef}"
	fi
	
	if [ $debug = "y" ]
	then
		echo "| DIO_05=1 ==> $val1 $val2 $val3 $val4 $val5 (WANT 1 0 0 0 0)"
	fi

	setdiopin 5 0
	val1=`eval getdiopin 20`
	val2=`eval getdiopin 37`
	val3=`eval getdiopin 38`
	val4=`eval getdiopin 39`
	val5=`eval getdiopin 40`
	# If 20 is HIGH, fail
	# If any of 37-40 are LOW, fail
	if [ $val1 = "1" -o $val2 = "0" -o $val3 = "0" -o $val4 = "0" -o $val5 = "0" ]
	then
		POE_res="${Afail}FAIL${Adef}"
	fi
		
	if [ $debug = "y" ]
	then
		echo "| DIO_05=0 ==> $val1 $val2 $val3 $val4 $val5 (WANT 0 1 1 1 1)"
	fi

	if [ $debug = "y" ]
	then
		echo "| POE Loop-back $POE_res"
	else	
		echo -e "\t$POE_res"
	fi

	if [ $debug = "y" ]
	then
		echo "|"
	fi

	# Test DIO loop-backs
	#--------------------------------------------------
	# Pins to test:
	#	21--22
	#	23--24
	#	25--26
	#	27--28
	#	29--30
	#	31--32
	#	33--34
	#	35--36
	# NOTE: pins 17 & 18 come from the CPU,
	#       need to test another way!

	DIO_res="${Apass}PASS${Adef}"
	echo -n "| DIO Loop-back:"
	if [ $debug = "y" ]
	then
		echo "|"
	fi
	
	DIOset1="21 23 25 27 29 31 33 35"
	DIOset2="22 24 26 28 30 32 34 36"

	# Set DIO set 2 as inputs
	for i in $DIOset2; do
		setdiopin $i z
	done

	# Set DIO set 1 LOW, expect DIO set 2 LOW
	for i in $DIOset1; do
		let j="$i + 1"
		setdiopin $i 0
		val=`eval getdiopin $j`
		if [ $debug = "y" ]
		then 
			echo "| i=${i}=0 j=${j}=${val}"
		fi	
		if [ $val = "1" ]
		then
			DIO_res="${Afail}FAIL${Adef}"
		fi	
	done

	# Set DIO set 1 HIGH, expect DIO set 2 HIGH
	for i in $DIOset1; do
		let j="$i + 1"
		setdiopin $i 1
		val=`eval getdiopin $j`
		if [ $debug = "y" ]
		then
			echo "| i=${i}=1 j=${j}=${val}"
		fi	
		if [ $val = "0" ]
		then
			DIO_res="${Afail}FAIL${Adef}"
		fi
	done

	if [ $debug = "y" ]
	then
		echo "|"
	fi	

	# Set DIO set 1 as inputs
	for i in $DIOset1; do
		setdiopin $i z
	done
		
	# Set DIO set 2 LOW, expect DIO set 1 LOW
	for i in $DIOset2; do
		let j="$i - 1"
		setdiopin $i 0
		val=`eval getdiopin $j`
		if [ $debug = "y" ]
		then
			echo "| i=${i}=0 j=${j}=${val}"
		fi
		if [ $val = "1" ]
		then
			DIO_res="${Afail}FAIL${Adef}"
		fi
	done
	
	# Set DIO set 2 HIGH, expect DIO set 1 HIGH
	for i in $DIOset2; do
		let j="$i - 1"
		setdiopin $i 1
		val=`eval getdiopin $j`
		if [ $debug = "y" ]
		then
			echo "| i=${i}=1 j=${j}=${val}"
		fi
		if [ $val = "0" ]
		then
			DIO_res="${Afail}FAIL${Adef}"
		fi
	done

	if [ $debug = "y" ]
	then
		echo "| DIO Loop-back $DIO_res"
	else
		echo -e "\t$DIO_res"
	fi

	testUSB
		
	# If ALL tests PASSED, turn ON the GREEN LED,
	# else, leave RED LED ON
		if [ $POE_res = "PASS" -a $DIO_res = "PASS" ]
	then
		ts7500ctl -d
		ts7500ctl -e
	else
		ts7500ctl -b
		ts7500ctl -c
	fi

	# Determine if an RTC is present
	# Call 'gettime' and put output in a temp. file
	# This file has "rtc_present=0" or "rtc_present=1"
	ts7500ctl -t > /tmp/time_check 2>&1
	t_check=`cat /tmp/time_check | grep rtc_present`
	if [ "$t_check" = "rtc_present=1" ]; then
		date +%D > /tmp/dt
		date +%r > /tmp/dt2
		echo "| RTC: `cat /tmp/dt` -- `cat /tmp/dt2`"
	else
		echo "| RTC: (NOT) detected"
	fi
			
	# Get the MAC
	echo -n "| MAC= "
	# Choose only the last 6 digits of the mac
	ts7500ctl -g | cut -d: -f4-6

		
}
# test7500


#===========================================================
test752()
{
	# Turn OFF green LED & turn ON red LED, 
	# to show we are testing
	ts7500ctl -b
	ts7500ctl -c

	# First, check the temp. sensor
	# Doesn't seem to work after other tests
	
	tempLong=`gettemp`
	tempShort=`echo ${tempLong%.*}`
	
	if [ $tempShort -ge 20 -a $tempShort -le 45 ]
	then
		tempRes="${Apass}PASS${Adef}"
	else
		tempRes="${Afail}FAIL${Adef}"
	fi
	
	echo -e "| Temp. Sensor\t\t${tempRes}"
	echo "| (`gettemp` degrees C)"
					
	
	# Test the relays:
	# Set each one and monitor the input pins
	# Pinout:
	# Relay #	INPUT #		expected output
	# 1		1		1
	# 2		2		2
	# 4		4		4
	# 0 (ALL OFF)			0
	#
	# First make sure the outputs are ALL 0
	setout 0

	echo -n "| Relay vs IN 1-3"
	if [ $debug = "y" ]
	then
		echo "|"
	fi	

	Relay_res="${Apass}PASS${Adef}"
	# We set relays: 1, 2 , 3, none
	Rset="1 2 4 0"
	for i in $Rset; do
		setrelay $i
		val=`eval getin`
		if [ $debug = "y" ]
		then
			echo "| Relay ${i} ==> IN=${val}"
		fi	
		if [ $val != "0x$i" ]
		then
			Relay_res="${Afail}FAIL${Adef}"
		fi
	done

	if [ $debug = "y" ]
	then
		echo "| Relay vs IN 1-3 $Relay_res"
	else	
		echo -e "\t$Relay_res"
	fi	


	if [ $debug = "y" ]
	then
		echo "|"
	fi
			
	# NOTE: here we assume the relays (from test above)
	#       were left in the ALL-OFF states
	# Test the 3 OUTPUTS vs INPUTS 4-8

	echo -n "| OUT vs IN 4-8"
	if [ $debug = "y" ]
	then
		echo
	fi

	
	OUT_res="${Apass}PASS${Adef}"
	# 0 ==> 0x0 ================
	setout 0
	val=`eval getin`
	if [ $debug = "y" ]
	then
		echo "| OUT=0 IN=${val}  (WANT 0x0)"
	fi
	if [ $val != "0x0" ]
	then
		OUT_res="${Afail}FAIL${Adef}"
	fi		

	# 1 ==> 0x18 ===============
	setout 1
	val=`eval getin`
	if [ $debug = "y" ]
	then
		echo "| OUT=1 IN=${val} (WANT 0x18)"
	fi
	if [ $val != "0x18" ]
	then
		OUT_res="${Afail}FAIL${Adef}"
	fi

	# 2 ==> 0x60 ===============
	setout 2
	val=`eval getin`
	if [ $debug = "y" ]
	then
		echo "| OUT=2 IN=${val} (WANT 0x60)"
	fi
	if [ $val != "0x60" ]
	then
		OUT_res="${Afail}FAIL${Adef}"
	fi

	# 4 ==> 0x80 ===============
	setout 4
	val=`eval getin`
	if [ $debug = "y" ]
	then
		echo "| OUT=4 IN=${val} (WANT 0x80)"
	fi
	if [ $val != "0x80" ]
	then
		OUT_res="${Afail}FAIL${Adef}"
	fi
	
	if [ $debug = "y" ]
	then
		echo "| OUT vs IN 4-8 $OUT_res"
	else
		echo -e "\t\t$OUT_res"
	fi

	# Test the USB ports
	testUSB
				
	# If ALL tests PASSED, turn ON GREEN
	# If any FAILED, turn on RED
	if [ $Relay_res = "PASS" -a $OUT_res = "PASS" ]
	then 
		ts7500ctl -d
		ts7500ctl -e
	else
		ts7500ctl -b
		ts7500ctl -c
	fi
}
# test752



# Determine which board we are testing
model=`ts7500ctl -i | grep "^model=" | cut -d= -f2 | cut -c3-`

# If this is a 7500 or 7550, we test one way
# if other board, we test another way
if [ $model -eq 7500 -o $model -eq 7550 ]; then
	getBoard
	if [ $? = "0" ]
	then
		echo "TS-7500/7550 Test:"
		echo "|=============================="
		test7500
		echo "|=============================="
	else
		echo "TS-752 Test:"
		echo "|=============================="
		test752
		echo "|=============================="
	fi
		
	echo "---"
	echo -e "\"\033[34msbc\033[31mT\033[34mest\033[0m   [ENTER]\" to re-test"
	echo -e "\"\033[34msbc\033[31mT\033[34mest\033[0m \033[35md\033[0m [ENTER]\" re-test with debug info."
                                                   
	return
fi


#***********************************************************
#***********************************************************
# It was not a 7500/7550, so we do a different test
#***********************************************************

# Which tests to do
testDIO="yes"
testRTC="yes"
testTMP="yes"
testCOM="yes"
testCAN="yes"
testHDU="yes"
testUSB="yes"
testWIR="yes"


# If this is a WayMedia (7551), skip some tests
if [ "$model" -eq 7551 ]; then
	testTMP="no"
	testCOM="no"
	testCAN="no"
	testHDU="no"
fi
	

# Initial test state (not tested)
# ... add as more tests get included
DIO_res="NOT tested"
RTC_res="NOT tested"
TMP_res="NOT tested"
COM_res="NOT tested"
CAN_res="NOT tested"
HDU_res="NOT tested"
USB_res="NOT tested"
WIR_res="NOT tested"

#============================================================
# Test DIO loop-backs
#============================================================
test_DIO()
{
# md
	# Pins to test
	# 17 -- 19
	# 18 -- 20
	# 21 -- 23
	# 22 -- 24
	# 25 -- 26
	# NOTE: pins 13 & 15 are SDA & SCL
	# 	these pins are tested (as a side effect)
	#	during the 'test_TEMP()' test

	DIO_r="PASS"
	DIO_res="${Apass}PASS${Adef}"
	DIOset1="17 18 21 22 25"
	DIOset2="19 20 23 24 26"

	# Set DIO set 2 as inputs
	for i in $DIOset2; do
		setdiopin $i z
	done

	# Set DIO set 1 to LOW
	for i in $DIOset1; do
		setdiopin $i 0
	done

	# Expect DIO set 2 LOW
	for i in $DIOset2; do
		val=`eval getdiopin $i`
		if [ $debug = "yes" ]; then
			echo -e "|\t#${i}=0 --> ${val}"
		fi
			
		if [ $val = "1" ]; then
			DIO_r="FAIL"
			DIO_res="\t${Afail}FAIL${Adef}"
		fi
	done
		

	# Set DIO set 1 HIGH
	for i in $DIOset1; do
		setdiopin $i 1
	done

	# Expect DIO set 2 HIGH
	for i in $DIOset2; do
		val=`eval getdiopin $i`
		if [ $debug = "yes" ]; then
			echo -e "|\t#${i}=1 --> ${val}"
		fi
			
		if [ $val = "0" ]; then
			DIO_r="FAIL"
			DIO_res="\t${Afail}FAIL${Adef}"
		fi
	done

	# Now reverse the order (set2 --> set1)
	# Set DIO set 1 as input
	for i in $DIOset1; do
		setdiopin $i z
	done

	# Set DIO set 2 LOW
	for i in $DIOset2; do
		setdiopin $i 0
	done

	# Expect DIO set 1 LOW
	for i in $DIOset1; do
		val=`eval getdiopin $i`
		if [ $debug = "yes" ]; then
			echo -e "|\t#${i}=0 --> ${val}"
		fi
			
		if [ $val = "1" ]; then
			DIO_r="FAIL"
			DIO_res="\t${Afail}FAIL${Adef}"
		fi
	done

	# Set DIO set 2 HIGH
	for i in $DIOset2; do
		setdiopin $i 1
	done	

	# Expect DIO set 1 HIGH
	for i in $DIOset1; do
		val=`eval getdiopin $i`
		if [ $debug = "yes" ]; then
			echo -e "|\t#${i}=1 --> ${val}"
		fi
			
		if [ $val = "0" ]; then
			DIO_r="FAIL"
			DIO_res="\t${Afail}FAIL${Adef}"
		fi
	done
}


#============================================================
# Read the temp. sensor
# If the temp. read is within our range, we pass
#============================================================
test_TEMP()
{
	# Assume failure
	TMP_r="FAIL"
	TMP_res="\t${Afail}FAIL${Adef}"

	# The range to compare to	
	tempLow="20"
	tempHigh="45"
	
	tempLong=`gettemp`
	tempShort=`echo $tempLong | cut -d= -f3`
	tempShort=`echo ${tempShort%.*}`

if [ $debug = "yes" ]; then
	echo -e "|\t$tempLong"
fi

	# If the read temp. is within our range, we pass
	if [ $tempShort -ge $tempLow -a $tempShort -le $tempHigh ]; then
		TMP_r="PASS"
		TMP_res="${Apass}PASS${Adef}"
	fi
}


#============================================================
# Count the # of entries in the variable 'usbs'
#============================================================
countUSB()
{
	USBcount="0"
	for i in $usbs; do
		USBcount=$((USBcount+1))
	done	

}


#============================================================
# Look in /sys/block for "sd*"
# (these are the USB devices)
# Count the # of devices found
# If the # equals the # required, we PASS
# If it is less, we FAIL 
#============================================================
test_USB()
{
# mu
	# Assume failure
	USB_r="FAIL"
	USB_res="\t${Afail}FAIL${Adef}"

	# The number of USB devices these models should have
	# (determined empirically)
	if [ $model = "7551" ]; then
		USBcountOK="5"
	elif [ $model = "7552" ]; then
		USBcountOK="4"
	elif [ $model = "7553" ]; then
		USBcountOK="2"	# is THIS ok?
	fi
					
	# Time to give the USB devices to be recognized
	# (adjust as needed)
	numTries="5"
	
	# Look for 'sd*' (USB devices) in /sys/block (from Eddie!)
	# (in this board, 'sdX' are USB devices..)
	usbs=`ls /sys/block | grep sd*`
	countUSB
	
	# Until we have the correct # of USB devices,
	# or we time out (adjust 'numTries' as needed)
	count="0"
	while [ $USBcount -lt $USBcountOK -a $count -le $numTries ]; do
		sleep 1
		count=$((count+1))
		usbs=`ls /sys/block | grep sd*`
		countUSB
	done


if [ $debug = "yes" ]; then 
	if [ $USBcount = "0" ]; then
		echo -e "\tNO USB devices found"
	else
		echo -e "|\tUSB found: "
		for i in $usbs; do
			echo -e "|\t$i "
		done			
	fi
fi
			
	
	# If the number of USB devices found is the one we expect,
	# we pass the test
	if [ $USBcount -ge $USBcountOK ]; then
		USB_r="PASS"
		USB_res="${Apass}PASS${Adef}"
	fi
				
}


#============================================================
# Test the Half Duplex function
# First call 'xuartctl' to set the port
# then call the binary 'test7552_HD'
#============================================================
test_HDU()
{
# md
	# Assume failure
	HDU_r="FAIL"
	HDU_res="\t${Afail}FAIL${Adef}"
	
	# Set the port
	xuartctl --port=2 --speed=9600 --server > /dev/null 2>&1
	
	# Call the test program
	if [ $debug = "yes" ]; then
		test7552_HD
	else
		test7552_HD > /dev/null 2>&1
	fi
	
	if [ "$?" = "0" ]; then
		HDU_r="PASS"
		HDU_res="${Apass}PASS${Adef}"
	fi	


	# Kill the server process?
}


#============================================================
# Test the COM ports
# Use 'xuartctl' for the test
# Call xuartctl for each port (ttyS0 & ttyS1)
#============================================================
test_COM()
{
# mc
	# Assume failure
	res1="1"
	res2="1"
	COM_r="FAIL"
	COM_res="\t${Afail}FAIL${Adef}"
	
	# Test COM1 (/dev/ttyS0)
	str1=`xuartctl --port=0 -t 2>&1`
	res1=$?
	if [ $debug = "yes" ]; then
		echo -e "|\t[ttyS0]:"
		echo -e "|\t`echo $str1 | cut -d' ' -f3`"
		echo -e "|\t`echo $str1 | cut -d' ' -f2`"
		echo -e "|\t`echo $str1 | cut -d' ' -f1`"
	fi
		
	
	# Test COM2 (/dev/ttyS1)
	str2=`xuartctl --port=1 -t 2>&1`
	res2=$?
	if [ $debug = "yes" ]; then
		echo
		echo -e "|\t[ttyS1]:"
		echo -e "|\t`echo $str2 | cut -d' ' -f3`"
		echo -e "|\t`echo $str2 | cut -d' ' -f2`"
		echo -e "|\t`echo $str2 | cut -d' ' -f1`"
	fi


	# If BOTH ports are OK, we pass the test		
	if [ $res1 = "0" -a $res2 = "0" ]; then
		COM_r="PASS"
		COM_res="${Apass}PASS${Adef}"
	fi
}


#============================================================
# Test the CAN interface
# We assume: there's another CAN device
# sending packets to us
#
# (NOTE standard canctl takes '-y' for
# RX test; this version uses '-r' --from Michael)
# run  'canctl -r', expect:
# 'passStr' (see below) 
#============================================================
test_CAN()
{
# /ma
	# Assume failure
	CAN_r="FAIL"
	CAN_res="\t${Afail}FAIL${Adef}"
	passStr="test_sequence_errors=0"


	# Get the result of running 'canctl -r' and store in 'str';
	# get the 4th field and compare to 'passStr'
	str=`canctl -r 2>&1`
	str1=`echo $str | cut -d' ' -f4`
		
	
	# If we don't get this string, something happened
	# we'll have to deal with it later...
	if [ -z "$str1" ]; then
		echo "CAN test problem..."
		return
	fi

# Debug info.
if [ $debug = "yes" ]; then
	echo -e "|\t`echo $str | cut -d' ' -f1`"
	echo -e "|\t`echo $str | cut -d' ' -f2`"
	echo -e "|\t`echo $str | cut -d' ' -f3`"
	echo -e "|\t`echo $str | cut -d' ' -f4`"
	echo -e "|\t`echo $str | cut -d' ' -f5`"
fi	


	# If 'str1' = 'passStr', we pass the test
	if [ "$str1" = "$passStr" ]; then
		CAN_r="PASS"
		CAN_res="${Apass}PASS${Adef}"
	fi
}


#============================================================
# Test for the RTC presence
# Call 'ts7500ctl -t' and look for:
# passStr1 & passStr2 (see below)
# If we get the correct strings, we pass
#============================================================
test_RTC()
{
# mr
	# Assume failure
	RTC_r="FAIL"
	RTC_res="\t${Afail}FAIL${Adef}"

	# We need to see these strings, to pass
	passStr1="rtc_present=1"
	passStr2="rtc_oscillator_ok=1"
	
	# Call 'ts7500ctl -t' and get its result
	# It should return two strings
	str=`ts7500ctl -t 2>&1`
	str1=`echo $str | cut -d' ' -f1`
	str2=`echo $str | cut -d' ' -f2`
	
if [ $debug = "yes" ]; then
	echo -e "|\t$str1"
	echo -e "|\t$str2"
fi	
	
	# If we got the correct strings, we pass
	if [ "$str1" = "$passStr1" -a "$str2" = "$passStr2" ]; then
		RTC_r="PASS"
		RTC_res="${Apass}PASS${Adef}"
	fi
}


#============================================================
# Test the wireless module
# NOTE: this test will change...
# Remove the /lib/modules dir.
# Create a link from /lib/modules to /mnt/root/lib/modules
# Install the wireless driver(s): 'modprobe rt73usb'
#============================================================
test_WIRELESS()
{
	# NOTE: this test will change ...
	# Assume failure
	WIR_res="FAIL"
		
	rm -R /lib/modules
	ln -sf /mnt/root/lib/modules /lib/modules
	modprobe rt73usb
	wait
	ifconfig -a | grep wlan > /dev/null 2>&1
	if [ $? = "0" ]; then
		WIR_res="PASS"
	fi	
	rm /lib/modules
	mkdir /lib/modules
}


#============================================================
# Create a log file in the dir. "/etc", 
# in NAND, called "LOG"
# In it, put model, JTAG, test results,
# blast history, etc.
# Do it ONLY if the SERVER is mounted
# If the tester does NOT want to write/overwrite
# a previous log file, disconnect the SERVER
#============================================================
getINFO()
{
# mi
	# Create a file in "/mnt/root/etc" (/etc, in NAND)
	# which will contain information for this board
	# do this ONLY if the SERVER is mounted
	
	# If the SERVER is NOT mounted, abort
	if [ "$serverMounted" = "no" ]; then
		return
	fi
	
	# Wait for /dev/nbd3 to finish mounting
	# If it never mounts, notify and exit
	maxCnt="5"
	echo "Waiting ($maxCnt sec) for [/mnt/root] to mount ... "	
	cnt=0
	cat /proc/mounts | grep "/dev/nbd3" > /dev/null
	while [ "$?" = "1" -a "$cnt" -lt "$maxCnt" ]; do
		sleep 1
		cnt=$((cnt+1))
		echo -n "$cnt "
		cat /proc/mounts | grep "/dev/nbd3" > /dev/null
	done

	# If we timed-out, notify
	if [ "$cnt" -ge "$maxCnt" ]; then
		echo "[/dev/nbd3] NOT found! - problem!"
		return
	fi


	# ****************************************			
	# /dev/ndb3 IS mounted on /mnt/root,
	# now make sure it is NOT read-only
	mount -o remount,rw /mnt/root
	wait

	
	# (for now?) the file name is: "LOG"	
	fileName="/mnt/root/etc/LOG" 
	echo "================================" > $fileName
	echo "| Revision & Test Data file    |" >> $fileName
	echo "| `date` |" >> $fileName
	echo "================================" >> $fileName
	echo >> $fileName


	# Use 'ts7500ctl -i' to get some info
	ts7500ctl -i > /tmp/info

	
	# Get the board model
	hdr="Model:\t\t"
	mod=`cat /tmp/info | grep ^model | cut -c9-`
	BoardModel=${hdr}${mod}	
	echo -e $BoardModel >> $fileName
	
	
	# Get the JTAG rev.
	hdr="JTAG rev:\t"
	rev=`cat /tmp/info | grep ^revision | cut -c12-`
	JTAG_rev=${hdr}${rev}
	echo -e "$JTAG_rev" >> $fileName


	# Get the MAC
	hdr="MAC:\t\t"
	mac=`ts7500ctl -g | cut -c14-21`
	echo -e "${hdr}${mac}" >> $fileName
	echo >> $fileName

	
	# Append the test results
	echo "Test Results:" >> $fileName
	echo "=============" >> $fileName
	echo "DIO: ${DIO_r}" >> $fileName
	echo "RTC: ${RTC_r}" >> $fileName
	echo "TMP: ${TMP_r}" >> $fileName
	echo "COM: ${COM_r}" >> $fileName
	echo "CAN: ${CAN_r}" >> $fileName
	echo "HDU: ${HDU_r}" >> $fileName
	echo "USB: ${USB_r}" >> $fileName
	# add more tests as needed...	


	echo "==============================" >> $fileName
	echo >> $fileName
	echo "Log file:" >> $fileName
	logFileLoc="/mnt2/var/ts-production/logs/"
	fName="ts${model}-prod-`ts7500ctl -g | cut -d= -f2`"
	logFileName="${logFileLoc}${fName}"
# echo $logFileName
	cat $logFileName >> $fileName
	sync
		
	
	# ****************************************	
	# Verify the file "LOG" IS there
	if [ $debug = "yes" ]; then echo -n "sync'ing... "; fi
	sync
	
	if [ $debug = "yes" ]; then echo -n "un-mounting FS... "; fi
	umount /mnt/root
	wait
	
	if [ $debug = "yes" ]; then echo -n "re-mounting FS... "; fi
	mount /dev/nbd3 /mnt/root	
	wait
	
	if [ $debug = "yes" ]; then echo -e "verifying file \"LOG\"..."; fi
	
	if [ -e $fileName ]; then
		echo -e "LOG file created in \"/mnt/root/etc/\""
	else
		echo -e "File \"${fileName}\" NOT there! --something wrong..."	
	fi
}



#*********************************************************************
# BEGIN the tests
#*********************************************************************
echo
echo "Testing TS-${model}..."
echo "-----------------------------"
# ml
# Do we test DIO?
echo -n "| DIO ... "
if [ $testDIO = "yes" ]; then
	if [ $debug = "yes" ]; then
		echo
	fi	
	test_DIO
	if [ $debug = "no" ]; then
		echo -e "${DIO_res}"
	else
		echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"	
	fi	
else
	echo -e "${DIO_res}"	
fi	
echo "|"
# **************************************************


# Do we test the RTC?
echo -n "| RTC ... "
if [ $testRTC = "yes" ]; then
	if [ $debug = "yes" ]; then
		echo
	fi
	test_RTC
	if [ $debug = "no" ]; then
		echo -e "${RTC_res}"
	else
		echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"
	fi
else
	echo -e "${RTC_res}"
fi
echo "|"
# **************************************************

			
# Do we test the Temp. Sensor?
# Only on models 7552 & 7553
if [ $model = "7552" -o $model = "7553" ]; then
	echo -n "| TEMP .. "
	if [ $testTMP = "yes" ]; then
		if [ $debug = "yes" ]; then
			echo
		fi
		test_TEMP
		if [ $debug = "no" ]; then
			echo -e "${TMP_res}"
		else
			echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"
		fi
		echo "|"	
	else
		echo -e "${TMP_res}"
	fi
fi
# **************************************************


# Do we test COM?
# Only on models 7552 & 7553
if [ "$model" -eq 7552 -o $model -eq 7553 ]; then
	echo -n "| COM ... "
	if [ $testCOM = "yes" ]; then
		if [ $debug = "yes" ]; then
			echo
		fi	
		test_COM
		if [ $debug = "no" ]; then
			echo -e "${COM_res}"
		else	
			echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"
		fi	
	else
		echo -e "${COM_res}"	
	fi
	echo "|"
fi
# **************************************************


# Do we test CAN?
# Only on models 7552 & 7553
if [ $model -eq 7552 -o $model -eq 7553 ]; then
	echo -n "| CAN ... "
	if [ $testCAN = "yes" ]; then
		if [ $debug = "yes" ]; then
			echo
		fi
		test_CAN
		if [ $debug = "no" ]; then
			echo -e "${CAN_res}"
		else
			echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"
		fi
		echo "|"
	else
		echo -e "${CAN_res}"
	fi
fi
# **************************************************


# Do we test the Half Duplex function?
# Only on models 7552 & 7553
if [ $model -eq 7552 -o $model -eq 7553 ]; then
	echo -n "| HD .... "
	if [ $testHDU = "yes" ]; then
		if [ $debug = "yes" ]; then
			echo
		fi
		test_HDU
		if [ $debug = "no" ]; then
			echo -e "${HDU_res}"
		else
			echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"
		fi	
	else
		echo -e "${HDU_res}"	
	fi
	echo "|"
fi
# **************************************************

	
# Do we test USB?	
echo -n "| USB ... "
if [ $testUSB = "yes" ]; then
	if [ $debug = "yes" ]; then
		echo
	fi	
	test_USB
	if [ $debug = "no" ]; then
		echo -e "${USB_res}"
	else		
		echo "| <<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"
	fi	
else
	echo -e "${USB_res}"	
fi	
echo "|"


				
# Display summary of results ONLY if in debug mode
if [ $debug = "yes" ]; then
	echo "|"
	echo "| Test Results:"
	echo "|***********************"
	echo -e "| DIO:\t${DIO_res}"
	echo -e "| RTC:\t${RTC_res}"
	echo -e "| TEMP:\t${TMP_res}"
	echo -e "| COM:\t${COM_res}"
	echo -e "| CAN:\t${CAN_res}"
	echo -e "| HD:\t${HDU_res}"
	echo -e "| USB:\t${USB_res}"
fi
	
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo "Testing complete"
echo "---"
# Display the MAC
echo -e "MAC:\t`ts7500ctl -g | cut -c14-21`"


# Display date/time
echo -e "date:\t`date "+%b %d, %Y"`"
echo -e "time:\t`date "+%I:%M %P"`"


# This does NOT apply to the model 7551 (WayMedia)
if [ "$model" -ne 7551 ]; then
	echo "---"
	serverMounted="no"
	
	# Mount the SERVER
	# un-mount, ping, mount if ping OK
	if [ $debug = "yes" ]; then
		echo -n "Mounting SERVER ... "
	fi	

	
	# If ping succeeds, we assume we can mount the SERVER
	ping 192.168.0.11 -c 1 > /dev/null
	if [ "$?" = "0" ]; then
		mount -o nolock,vers=2 192.168.0.11:/u/x /mnt2 > /dev/null 2>&1
		wait
		serverMounted="yes"
		if [ $debug = "yes" ]; then
			echo "OK"
		fi	
	else
		if [ $debug = "yes" ]; then
			echo "FAIL -- Ethernet cable not connected?"
			echo "(LOG file will NOT be included)"
		fi		
	fi

	getINFO
fi

# (for now) only test the 7552
if [ $model -eq 7552 ]; then
	test_WIRELESS
fi
	
echo "WIRELESS test = $WIR_res"
echo "---"
echo -e "\"\033[34msbc\033[31mT\033[34mest\033[0m   [ENTER]\" to re-test"
echo -e "\"\033[34msbc\033[31mT\033[34mest\033[0m \033[35md\033[0m [ENTER]\" re-test with debug info."

