#!/bin/bash
set -e

WATCHDOG=watchdog0

rmmod de01 || true
insmod de01.ko
trap "rmmod de01" EXIT

echo "reporting watchdog timeout every 2 seconds"
while true
do
	sleep 2
	echo "watchdog time left:" $(cat /sys/class/watchdog/$WATCHDOG/timeleft)
done &

echo "watchdog ping test"
echo "keeping the watchdog file open for 2 minutes and pinging every 30 seconds..."
for i in {0..3}
do
	sleep 30
	echo "ping watchdog" >&2
	# just write something to /dev/$WATCHDOG to ping
	echo ping
done > /dev/$WATCHDOG
echo "if you can read this, the watchdog ping test PASSED"

echo "watchdog reset test"
echo "device should reset in 60 seconds"
echo "press Ctrl+C to cancel"
echo "keeping the watchdog file open without ping..."
sleep 65 > /dev/$WATCHDOG
echo "if you can read this, the watchdog reset test FAILED"
