#!/bin/sh
#set -x

PORT="/dev/ttymxc4"
BAUD="115200"

HWID_0=64  #GPIO3_0
HWID_1=65  #GPIO3_1
HWID_2=66  #GPIO3_2
HWID_3=67  #GPIO3_3

get_gpio()
{
        if [ ! -d /sys/class/gpio/gpio$1 ]; then
                echo $1 > /sys/class/gpio/export
                echo in > /sys/class/gpio/gpio$1/direction
        fi

		VAL=`cat /sys/class/gpio/gpio$1/value`
		if [ $? -ne 0 ]; then
			echo "Error getting HWID"
			exit 1
		fi

        return ${VAL}
}

set_gpio()
{
	if [ ! -d /sys/class/gpio/gpio$1 ]; then
		echo $1 > /sys/class/gpio/export
		echo out > /sys/class/gpio/gpio$1/direction
	fi
	
	echo $2 > /sys/class/gpio/gpio$1/value
}

set_gpio_input()
{
	if [ -d /sys/class/gpio/gpio$1 ]; then
		echo in > /sys/class/gpio/gpio$1/direction
		echo $1 > /sys/class/gpio/unexport
	fi
}

HWID=0
get_gpio ${HWID_0}
if [ $? -eq 1 ]; then
	HWID=`expr ${HWID} + 1`
fi

get_gpio ${HWID_1}
if [ $? -eq 1 ]; then
	HWID=`expr ${HWID} + 2`
fi

get_gpio ${HWID_2}
if [ $? -eq 1 ]; then
	HWID=`expr ${HWID} + 4`
fi

get_gpio ${HWID_3}
if [ $? -eq 1 ]; then
	HWID=`expr ${HWID} + 8`
fi

reset_micro()
{
	echo "Resetting target..."
	set_gpio 176 0

	if [ ${1} -ge 2 ]; then
		set_gpio 34 1
		sleep 1
		set_gpio 34 0
		sleep 1
		set_gpio_input 34
	else
		set_gpio 39 1
		sleep 1
		set_gpio 39 0
		sleep 1
		set_gpio_input 39		
	fi

	set_gpio_input 176
}


if [ $# -ne 0 ]; then
	
	REV=`dbus-send --system --print-reply=literal --dest=com.raven.hardware.microcontroller.v1 /com/raven/hardware/microcontroller/v1/system com.raven.hardware.microcontroller.v1.system.getHWID | awk '{ print $2 }'`; 
	REV=`echo ${REV}`
	echo "Rev:${REV}"

	VERSION=`dbus-send --system --print-reply=literal --dest=com.raven.hardware.microcontroller.v1 /com/raven/hardware/microcontroller/v1/micro com.raven.hardware.microcontroller.v1.micro.getVersion`
	VERSION=`echo ${VERSION}` #trim whitespace
	echo "Version:${VERSION}"
	
	if [ "$1" = "reset" ]; then
		reset_micro ${HWID}
	fi

	exit 0
fi

#dbus might not be available in early system startup, default to raw gpio calls
#		HWID=`dbus-send --system --print-reply=literal --dest=com.raven.hardware.microcontroller.v1 /com/raven/hardware/microcontroller/v1/system com.raven.hardware.microcontroller.v1.system.getHWID | awk '{ print $2 }'`


stty -F ${PORT} ${BAUD} -parodd

RET="1"
RETRY="10"

#use this to shut off first
/etc/init.d/micro disable

while [ "${RET}" -ne "0" -a "${RETRY}" -ne "0" ]; do
		killall -9 hwMicroCon 2&> /dev/null  #incase anyone keeps turning it on

		if [ ${HWID} -ge 2 ]; then
		    /usr/local/bin/stm32flash -i 34,176,-34:-176 -b ${BAUD} -w /usr/local/src/bitstreams/micro/micro.hex -v -g 0x0 ${PORT}
	        RET="$?"
		else
		    /usr/local/bin/stm32flash -i 39,176,-39:-176 -b ${BAUD} -w /usr/local/src/bitstreams/micro/micro.hex -v -g 0x0 ${PORT}
	        RET="$?"
		fi

       RETRY=`expr ${RETRY} - 1`
done;

if [ "${RET}" -eq "0" ]; then
	#give the micro a second to boot
	sleep 1

	RET="1"
	RETRY="10"

	while [ "${RET}" -ne "0" -a "${RETRY}" -ne "0" ]; do
		killall -9 hwMicroCon 2&> /dev/null
		
		hwMicroCon --hwid
		RET="$?"
		
		if [ "${RET}" -eq "254" ]; then #254 is -2
			reset_micro ${HWID}
		fi

		RETRY=`expr ${RETRY} - 1`
	done;
fi

#dbus likes to forget the micro exists if we dont restart it before launching the service again, 
#but dont restart it as a bunch of dbus apps will all go offline 
#/etc/init.d/dbus-1 restart
sleep 1
/etc/init.d/micro enable
