#!/bin/sh
#
# Start the gpio polling ....
#
# 1.2 
# Correct an invalid test for detecting HBS box when managing the bus polarization
#

S85POLLGPIO_VERSION=1.2

BYPASS_APPLICATION=`fw_printenv bypass_application 2> /dev/null`

# Bypass Application on Test Bench
if [[ "$BYPASS_APPLICATION" = "bypass_application=1" ]] ; then
   echo "'$BYPASS_APPLICATION' => Bypassing $0"
   exit 1
fi

case "$1" in

  start)
 	
 	echo "Starting gpio polling..."
	
	# Manage KNX MMI Switches
	if [ "`grep "HagerBox for KNX" /proc/cpuinfo`" != "" ]; then
		/usr/bin/pollgpio gpio234 both COM_SEL &
		/usr/bin/pollgpio gpio235 both COM_SEL &
		if [ -x "/etc/gpio/COM_SEL.sh" ]; then
			/bin/sh /etc/gpio/COM_SEL.sh
		fi
	fi
	
	# Manage HBS/EMSS Default Ethernet Network Config
	##### Not needed anymore by EMSS
	##### if [ "`grep "HagerBox for HBS" /proc/cpuinfo`" != "" ]; then
	#####	if [ -x "/etc/network/netconfig.sh" ]; then
	#####		/bin/sh /etc/network/netconfig.sh emss
	#####	fi
	##### fi
	
	echo 1 > /sys/class/gpio/gpio16/active_low
	/usr/bin/pollgpio gpio16 both POE_OK &
	echo 1 > /sys/class/gpio/gpio24/active_low
	/usr/bin/pollgpio gpio24 both PFAIL &
	/usr/bin/pollgpio gpio29 both CAPGOOD &

	# Manage EMSS 485 bus polarization only for HBS/EMSS boxes
	if [ "`grep "HagerBox for HBS" /proc/cpuinfo`" != "" ]; then
		echo out > /sys/class/gpio/gpio26/direction 				# Configuration de la pin en sortie 
		echo 0 > /sys/class/gpio/gpio26/value 						# Polarisation du Bus actif 
	fi

	;;


  stop)
	echo "Stopping gpio polling..."
	kill `pidof pollgpio`
	;;

  restart|reload)
	"$0" stop
	"$0" start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
 
