# Script: "test75x"

# Determine if the 7500 is mounted on a 9475 or a 752
# and test the corresponding board

# First, source the sub's from "ts7500.subr"
. /ts7500.subr

# Any argument puts the script in 'debug' mode
if [ -z $1 ]
then
	debug="n"
else	
	debug="y"
fi


#===========================================================
getBoard()
{
	# Set pin 19 as INPUT; set pin 6 LOW
	# If pin 19 is also LOW, we (should) have a loopback
	# so the 7500 is mounted on a 9475
	setdiopin 19 z
	setdiopin 6 0 
	ret=`eval getdiopin 19`
	return $ret
}


#==========================================================
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\tPASS"
	else
		echo -e "| USB-0 \t\tFAIL (not present?)"
	fi

	dmesg | grep "usb 1-2" > /dev/null
	if [ $? = "0" ]
	then
		echo -e "| USB-1 \t\tPASS"
	else
		echo -e "| USB-1 \t\tFAIL (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="PASS"
	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="FAIL"
	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="FAIL"
	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="PASS"
	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="FAIL"
		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="FAIL"
		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="FAIL"
		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="FAIL"
		fi
	done

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

	# Test the 2 USB ports
	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 is 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
		echo "| RTC detected"
		echo -n "| "
		date
	else
		echo "| RTC (NOT) detected"	
	fi
		
	# Get the MAC
	echo -n "| "
	ts7500ctl -g
	
}
# test7500


#jw
#===========================================================
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
	gettemp > /dev/null 2>&1
	currTemp=$?
	if [ $currTemp -ge 20 -a $currTemp -le 40 ]
	then
		tempRes="PASS"	
	else
		tempRes="FAIL"
	fi

	echo -e "| Temp Sensor\t\t${tempRes}"
	echo "| (`gettemp` degrees C)"

	if [ $debug = "y" ]
	then
		echo "| (20 - 40 is OK @ ambient)"
	fi
					
	# 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 ==> IN [1-3]"
	if [ $debug = "y" ]
	then
		echo "|"
	fi	

	Relay_res="PASS"
	# 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="FAIL"
		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 ====> IN [4-8]"
	if [ $debug = "y" ]
	then
		echo
	fi

	
	OUT_res="PASS"
	# 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="FAIL"
	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="FAIL"
	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="FAIL"
	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="FAIL"
	fi
	
	if [ $debug = "y" ]
	then
		echo "| OUT vs IN 4-8 $OUT_res"
	else
		echo -e "\t$OUT_res"
	fi

			
	# 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

# Begin execution 
# ******************************
getBoard
if [ $? = "0" ]
then
	echo "TS-7500 Test:"
	echo "|=============================="
	test7500
else
	echo "TS-752 Test:"
	echo "|=============================="
	test752
fi

echo "|------------------------------"
echo
				
