#!/bin/bash
# This is a Pengutronix automated remote test script
# It has been customized to verify a
# TQMA53 on a TQMBa53

SSH_COMMAND='rsh'

if [ ! -e "${PTXDIST_PLATFORMCONFIG}" ]; then
	echo "platformconfig is missing"
	exit 1
fi

. "${PTXDIST_PLATFORMCONFIG}"

if test -f "${PTXDIST_TOPDIR}/tests/libptxdisttest.sh"
then
        source "${PTXDIST_TOPDIR}/tests/libptxdisttest.sh"
else
        echo "${PTXDIST_TOPDIR}/tests/libptxdisttest.sh not found." >&2
        exit 1
fi

if [ ! -e ${PTXDIST_WORKSPACE}/tests/common_testlib ]; then
        echo "${PTXDIST_WORKSPACE}/tests/common_testlib not found." >&2
        exit 1
fi

. ${PTXDIST_WORKSPACE}/tests/common_testlib

#
# sanity checks
#
acctest_boardsetup

. "${PTXDIST_BOARDSETUP}"

#
# do we have network access to the target?
#
acctest_ping

#
# check for required PTXdist version
#
acctest_ptxdist 2013.03.0

#
# check that the platform announces the correct name [EDITME]
#
acctest_platform_name MBa53

#
# check that the platform announces the correct release number [EDITME]
#
acctest_platform_release -2013.03.0

#
# check for RSH
#
acctest_rsh_available

#
# no need to continue if vital things are missing
#
all_on_board

#
# check that "Kernel announces correct kernel release number" [EDITME]
#
KERNEL_RELEASE=3.9.0-rc6-20130409
acctest_kernel_release ${KERNEL_RELEASE}

#
# first, acquire the time to enable a realtimeclock clockspeed judgment at the end
#
startingtimethere=$(remote 'date +%s')
startingtimehere=$(date +%s)

#
# check memory
#
acctest_memory

#
# check if gpio expander is available
#
test_begin
checking "if gpio expander is available"
remote "cat /sys/class/gpio/gpiochip*/label  | grep -q pca9554"
result
test_end

#
# check if vital processes are running, uhh sleeping
#
for process in \
	systemd-udevd \
;do acctest_process "$process"; done

#
# check for vital files and devices
#
for device in \
	/dev/ttymxc0 \
	/dev/ttymxc1 \
	/dev/ttymxc2 \
	/dev/rtc0 \
	/dev/zero \
	/dev/null \
	/dev/urandom \
;do acctest_devicenode "$device"; done

#
# check for network interface(s) in sysfs
#
for device in \
	eth0 \
	eth1 \
;do acctest_network_device "$device"; done

#
# check for specific bus(ses) in sysfs
#
for bus in \
	i2c \
	spi \
	usb \
;do acctest_bus "$bus"; done

#
# check for specific class(ses) in sysfs
#
for class in \
	rtc \
	spi_master \
	gpio \
;do acctest_class "$class"; done

#
# is an executable binary available
#
for command in \
	/bin/busybox \
	/bin/cp \
	/bin/dd \
	/bin/lsmod \
	/bin/mount \
	/bin/ping \
	/bin/ps \
	/bin/umount \
	/sbin/insmod \
	/sbin/rmmod \
	/sbin/modprobe \
	/sbin/hwclock \
	/bin/date \
;do acctest_command_executable "$command"; done

## kernel module present?
#for module in \
#	spidev \
#	i2c_mxs \
#;do acctest_module "$module"; done

#
# every target should support various filesystems
#
for filesystem in \
	autofs \
	devpts \
	devtmpfs \
	ext2 \
	ext3 \
	ext4 \
	mqueue \
	nfs \
	pipefs \
	tmpfs \
;do acctest_filesystem_support "$filesystem"; done

#
# check network services [EDITME]
#
test_begin

#acctest_network_services "ftp port is reachable" "21" "21/tcp open ftp"
#acctest_network_services "ssh port is reachable" "22" "22/tcp open ssh"
acctest_network_services "telnet port is not reachable" "23" ""

checking "that thttpd is not running"
remote_assure_process "thttpd"
exitstatus=$?
host "test ${exitstatus} = 1"
result

test_end

# check network throughput with iperf
# broken-iperf OFF
#acctest_network_throughput 10 50000

# check for a running busybox multi-call binary
#
acctest_busybox

#
# check eeprom read and write capability
#
acctest_eeprom_rw /sys/bus/i2c/devices/1-0050/eeprom 8192

#
# LM75 baseboard temperature sensor on I2C2, 0x49 (baseboard)
#
test_begin
checking "LM75 I2C device at 1-0049"
remote_compare 'cat /sys/bus/i2c/devices/1-0049/name' "lm75"
result
test_end

#
# LM75 baseboard temperature sensor on I2C2, 0x48 (cpu module)
#
test_begin
checking "LM75 I2C device at 1-0048"
remote_compare 'cat /sys/bus/i2c/devices/1-0048/name' "lm75"
result
test_end

#
# DVI transmitter on I2C3, 0x38
#
#test_begin
#checking "DVI Transmitter I2C device at 0-0038"
#remote 'echo y | i2cdetect 0 2>/dev/null | grep " 38 "'
#result
#test_end

#
# EDID device on I2C3, 0x50
#
#test_begin
#checking "EDID I2C device at 0-0050 (external display necessary)"
#remote_compare 'echo y | i2cdump 0 0x50 2>/dev/null | grep -c "00: 00 ff ff ff ff ff ff 00"' "1"
#result
#test_end

#
# check if hardware clock is accepting and reproducing date/time
#
acctest_hwclock

# CAN tests
# UNFINISHED - OFF
#acctest_can

# check network reliability
# TAKES LONG - OFF
#acctest_network_reliability 3

#
# at last, remember that we wanted to compare the two realtime clocks
#
acctest_realtime_clock

echo -n All tests completed. >&2
if [ $fail_count -gt 0 ]
then
	printf "%8b" "${RED} $fail_count TEST(S) FAILED.${NC}\n" >&2
	exit 1
else
	echo >&2
	exit 0
fi
