#!nsh
#
# PX4FMU startup script for test hackery.
#
set unit_test_failure 0

uorb start

if rgbled start
then
	rgbled rgb 20 20 255
fi

if sercon
then
	echo "[i] USB interface connected"

	# Try to get an USB console
	nshterm /dev/ttyACM0 &
fi

#
# Try to mount the microSD card.
#
mount -t vfat /dev/mmcsd0 /fs/microsd
if [ $? == 0 ]
then
	echo "[i] card mounted at /fs/microsd"
	# Start playing the startup tune
	tone_alarm start
else
	echo "[i] no microSD card found"
	# Play SOS
	tone_alarm error
fi

#
# Start a minimal system
#

#
# Start the ORB (first app to start)
#
uorb start

#
# Load parameters
#
set PARAM_FILE /fs/microsd/params
if mtd start
then
	set PARAM_FILE /fs/mtd_params
fi

param select $PARAM_FILE
if param load
then
	echo "[param] Loaded: $PARAM_FILE"
else
	echo "[param] FAILED loading $PARAM_FILE"
fi

if [ -f /etc/extras/px4io-v2.bin ]
then
	set io_file /etc/extras/px4io-v2.bin
else
	set io_file /etc/extras/px4io-v1.bin
fi

if px4io start
then
	echo "PX4IO OK"
else
	set unit_test_failure 1
	set unit_test_failure_list "${unit_test_failure_list} px4io_start"
fi

if px4io checkcrc $io_file
then
	echo "PX4IO CRC OK"
else
	echo "PX4IO CRC failure"
	tone_alarm MBABGP
	if px4io forceupdate 14662 $io_file
	then
		if px4io start
		then
			echo "PX4IO restart OK"
			tone_alarm MSPAA
		else
			echo "PX4IO restart failed"
			tone_alarm MNGGG
			set unit_test_failure 1
			set unit_test_failure_list "${unit_test_failure_list} px4io_flash"
		fi
	else
		echo "PX4IO update failed"
		tone_alarm MNGGG
		set unit_test_failure 1
		set unit_test_failure_list "${unit_test_failure_list} px4io_flash"
	fi
fi

#
# The presence of this file suggests we're running a mount stress test
#
if [ -f /fs/microsd/mount_test_cmds.txt ]
then
	tests mount
fi

sh /etc/init.d/rc.sensors

# Check for flow sensor
if px4flow start
then
fi

if ll40ls start
then
fi

#
# Run unit tests at board boot, reporting failure as needed.
# Add new unit tests using the same pattern as below.
#

echo
echo "--------------------------------------------------------------------------------"
echo "[mavlink_tests]             STARTING TEST"
if mavlink_tests
then
	echo "[mavlink_tests]             PASS"
else
	set unit_test_failure 1
	set unit_test_failure_list "${unit_test_failure_list} mavlink_tests"
	echo "[mavlink_tests]             FAILED"
fi
echo "--------------------------------------------------------------------------------"
echo
echo "--------------------------------------------------------------------------------"
echo "[commander_tests]             STARTING TEST"
if commander_tests
then
	echo "[commander_tests]             PASS"
else
	set unit_test_failure 1
	set unit_test_failure_list "${unit_test_failure_list} commander_tests"
	echo "[commander_tests]             FAILED"
fi
echo "--------------------------------------------------------------------------------"
echo
echo "--------------------------------------------------------------------------------"
echo "[controllib_test]             STARTING TEST"
if controllib_test
then
	echo "[controllib_test]             PASS"
else
	set unit_test_failure 1
	set unit_test_failure_list "${unit_test_failure_list} controllib_tests"
	echo "[controllib_test]             FAILED"
fi
echo "--------------------------------------------------------------------------------"
echo
echo "--------------------------------------------------------------------------------"
echo "[uorb_tests]             STARTING TEST"
if uorb_tests
then
	echo "[uorb_tests]             PASS"
else
	set unit_test_failure 1
	set unit_test_failure_list "${unit_test_failure_list} uorb_tests"
	echo "[uorb_tests]             FAILED"
fi
echo "--------------------------------------------------------------------------------"

if tests all
then
else
	set unit_test_failure 1
	set unit_test_failure_list "${unit_test_failure_list} system_tests"
fi

if [ $unit_test_failure == 0 ]
then
	echo
	echo "All Unit Tests PASSED"
	rgbled rgb 20 255 20
else
	echo
	echo "Some Unit Tests FAILED:${unit_test_failure_list}"
	rgbled rgb 255 20 20
fi

ver all

free
