#!/bin/sh

# NAME
#	bt_start
#
# SYNOPSIS
#	bt_start
#
# DESCRIPTION
#BCM43455 pins section 5 bluetooth power management
#BT_HOST_WAKE 	PTC1	INPUT   gpio46 VF610_PAD_PTC1__GPIO_46
#BT_WAKE 		PTC0	OUTPUT	gpio45 VF610_PAD_PTC0__GPIO_45
#BT_RST  		PTC2	OUTPUT	gpio47 VF610_PAD_PTC2__GPIO_47

type=`cat /sys/kernel/secure/type`
subtype=`cat /sys/kernel/secure/subtype`
TIMEOUT_MAX=36
timeout=$TIMEOUT_MAX
timeout1=$TIMEOUT_MAX
COUNTER=0
MAXRETRY=1

bt_murata_gpioinit()
{ 
   if [ "$type" -eq 1 ] 
   then 
      cd /sys/class/gpio
      if [ ! -e  /sys/class/gpio/gpio47 ]; then
          logger "export gpio 47 bt-reset"
          echo 47 > export
          cd /sys/class/gpio/gpio47
          echo out > direction
          echo 0 > value
      fi
      cd /sys/class/gpio
      if [ ! -e  /sys/class/gpio/gpio45 ]; then
          logger "export gpio 45 bt-wake"
          echo 45 > export
          cd /sys/class/gpio/gpio45
          echo out > direction
          echo 0 > value
      fi
      cd /sys/class/gpio
      if [ ! -e  /sys/class/gpio/gpio46 ]; then
          logger "export gpio 46 bt-host-wake"
          echo 46 > export
          cd /sys/class/gpio/gpio46
          echo in > direction
      fi
      return 0      
   else
       echo "phoenix:  hw not supported"
       return 1
   fi
}

bt_murata_gpiodeinit()
{ 
   if [ "$type" -eq 1 ] 
   then 
      cd /sys/class/gpio
      if [ -e  /sys/class/gpio/gpio47 ]; then
          echo 0 > gpio47/value
          logger "unexport gpio 47 bt-reset"
          echo 47 > unexport
      fi
      cd /sys/class/gpio
      if [ -e  /sys/class/gpio/gpio45 ]; then
          logger "unexport gpio 45 bt-wake"
          echo 45 > unexport
      fi
      cd /sys/class/gpio
      if [ -e  /sys/class/gpio/gpio46 ]; then
          logger "unexport gpio 46 bt-host-wake"
          echo 46 > unexport
      fi
      return 0      
   else
       echo "phoenix:  hw not supported"
       return 1
   fi
}

bt_murata_reset_toggle()
{
   if [ "$type" -eq 1 ]
   then
       logger "bt murata reset off"
       cd /sys/class/gpio/gpio47
       echo out > direction
       echo 0 > value
       usleep 500000
       echo 1 > value
       usleep 100000
       return 0
   else 
       echo "phoenix: hw not supported"
       return 1
   fi
}

bt_murata_wake_toggle()
{
   if [ "$type" -eq 1 ]
   then
       logger "bt murata wake on"
       cd /sys/class/gpio/gpio45
       echo out > direction
       echo 0 > value
       usleep 500000
       echo 1 > value
       usleep 100000
       return 0
   else 
       echo "phoenix: hw not supported"
       return 1
   fi
}

bt_murata_wake_reset()
{
   if [ "$type" -eq 1 ]
   then
       logger "bt murata wake dev reset"
       echo "bt murata wake dev reset"
       cd /sys/class/gpio/gpio45
       echo out > direction
       echo 0 > value
   else 
       echo "phoenix: hw not supported"
       return 1
   fi
}

attach_start ()
{
	local retval

	logger `date`: $0 starts
	echo `date`: $0 starts
	echo bt_murata_gpiodeinit
	bt_murata_gpiodeinit
	usleep 200000
	echo bt_murata_gpioinit
	bt_murata_gpioinit
		
	display_gpios
	while [  $COUNTER -lt $MAXRETRY ]; do
		echo attach_start:reset_toggle
        bt_murata_reset_toggle      

	    /usr/bin/hciattach-murata -n -t 15 -s 115200 /dev/ttyLP2 bcm43xx 3000000 flow
	    retval=$?
		logger hciattach-murata returns [$retval]
		logger The counter is $COUNTER
		echo hciattach-murata returns [$retval]
		echo The counter is $COUNTER
		if [ "$retval" -eq 0 ]
		then
			echo case1
			COUNTER=$MAXRETRY
			echo case1 counter is $COUNTER
		else
			echo retry due to timeout
			COUNTER=$(($COUNTER + 1))
			echo retry counter is $COUNTER
			sleep 1
		fi
	done
	bt_murata_gpiodeinit
	echo hciattach-murata exits 
	logger hciattach-murata exits 
	
	echo `date`:$0 exits
	
}

bluetoothd_start ()
{
	local timeout=$TIMEOUT_MAX
	local daemonpid=`pgrep bluetoothd`
	echo bluetoothd_start wait $timeout seconds for hciattach-murata

	while ( [ ! -d /sys/class/bluetooth/hci0 ] && [ "$timeout" -ge 0 ] )
    do
    	timeout=$(($timeout-1))
        sleep 1
    done 
    
    if [ -d /sys/class/bluetooth/hci0 ] && [ -z "$daemonpid" ] ; then         
		logger "starting /usr/libexec/bluetooth/bluetoothd"
		echo "starting /usr/libexec/bluetooth/bluetoothd"
        /usr/libexec/bluetooth/bluetoothd --noplugin=* -n -E -C -f /usr/share/bluetooth/bluez-default.conf
		logger "/usr/libexec/bluetooth/bluetoothd EXITs"
		echo "EXIT: /usr/libexec/bluetooth/bluetoothd EXITs"
    else
    	logger ERROR:no hci device created
    	echo ERROR bluetoothd_start:no hci device created
    fi
}

bluez_equinox_profiles_start ()
{
	local timeout=$TIMEOUT_MAX
	local daemonpid=`pgrep bluetoothd`
	local extapppid=`pgrep bluez-equinox-profiles`
	echo bluez_equinox_profiles_start wait $timeout seconds for bluetoothd
	
	while ( [ -z "$daemonpid" ] && [ "$timeout" -ge 0 ] )
    do
        daemonpid=`pgrep bluetoothd`
    	timeout=$(($timeout-1))
        sleep 1
    done    
    if [ ! -z "$daemonpid" ]; then 
    	echo bluetoothd started
    	if [ -z "$extapppid" ]; then
    		echo starting /usr/bin/bluez-equinox-profiles
    		/usr/bin/bluez-equinox-profiles 
    		echo /usr/bin/bluez-equinox-profiles EXITS
    	else
    	    logger ERROR:bluez-equinox-profiles ALREADY STARTED
    		echo ERROR:bluez-equinox-profiles ALREADY STARTED
    	
    	fi
    else
    	logger ERROR:bluez_equinox_profiles_start NO bluetoothd started
    	echo ERROR bluez_equinox_profiles_start: NO bluetoothd started
    fi
}

equinox_profiles_create_and_register ()
{
	local timeout=$TIMEOUT_MAX
	local daemonpid=`pgrep bluetoothd`
	local extapppid=`pgrep bluez-equinox-profiles`
	echo equinox_profiles_create_and_register: wait $timeout seconds for bluetoothd and bluez-equinox-profiles
	
	while (( [ -z "$daemonpid" ] || [ -z "$extapppid" ] ) && [ "$timeout" -ge 0 ] )
    do
        sleep 1
        daemonpid=`pgrep bluetoothd`
        extapppid=`pgrep bluez-equinox-profiles`
    	timeout=$(($timeout-1))
    done 
    sleep 1   

	local result=`dbus-send --print-reply --system --dest=org.freedesktop.DBus  /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep extapp`
	echo equinox_profiles_create_and_register:check /usr/bin/bluez-equinox-profiles started and registered on dbus
    if [ ! -z "$result" ]; then 
    	echo OK./usr/bin/bluez-equinox-profiles started and registered on dbus:$result
    	echo creating ble profile
		result=`dbus-send --print-reply --type=method_call --system --dest=org.bluez.extapp /org/bluez/extapp org.bluez.extapp.EqProfileBle string:'on' |grep OK`
    	echo create-ble-profile=$result
    	if [ ! -z "$result" ]; then
    		echo SUCCESS: create-profile. 
    		echo registering ble profile
			result=`dbus-send --print-reply --type=method_call --system --dest=org.bluez.extapp /org/bluez/extapp org.bluez.extapp.EqRegBle string:'on' |grep OK`
    		if [ ! -z "$result" ]; then
    			echo SUCCESS: register ble profile.
    		else
    			echo ERROR: register ble profile.
    		fi
    	else
    		echo ERROR: create-ble-profile .
    	fi 
    	
    	echo creating basic profile
		result=`dbus-send --print-reply --type=method_call --system --dest=org.bluez.extapp /org/bluez/extapp org.bluez.extapp.EqProfileBasic string:'on' |grep OK`
    	echo create-basic-profile=$result
    	if [ ! -z "$result" ]; then
    		echo SUCCESS: create-basic-profile. 
    		echo registering basic profile
			result=`dbus-send --print-reply --type=method_call --system --dest=org.bluez.extapp /org/bluez/extapp org.bluez.extapp.EqRegBasic string:'on' |grep OK`
    		if [ ! -z "$result" ]; then
    			echo SUCCESS: register basic profile.
    		else
    			echo ERROR: register basic profile.
    		fi
    	else
    		echo ERROR: create-basic-profile .
    	fi 
    	
    else
    	echo ERROR:/usr/bin/bluez-equinox-profiles NOT started and registered on dbus
    fi

}

equinox_profiles_advertise_on ()
{
	local result=`dbus-send --print-reply --system --dest=org.freedesktop.DBus  /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep extapp`
	echo equinox_profiles_advertise_on:equinox_profiles_create_and_register:check /usr/bin/bluez-equinox-profiles started and registered on dbus
    if [ ! -z "$result" ]; then 
    	echo SUCCESS:/usr/bin/bluez-equinox-profiles started and registered on dbus:$result
   		echo start ble adverting.
    	result=`dbus-send --print-reply --type=method_call --system --dest=org.bluez.extapp /org/bluez/extapp org.bluez.extapp.Leadv string:'on' | grep OK`
     	if [ ! -z "$result" ]; then
    		echo SUCCESS: advertise_on.
    	else
    		echo ERROR: advertise_on.
    	fi
    else
    	echo ERROR:/usr/bin/bluez-equinox-profiles NOT started and registered on dbus
    fi
}

equinox_profiles_advertise_off ()
{
	local result=`dbus-send --print-reply --system --dest=org.freedesktop.DBus  /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep extapp`
	echo equinox_profiles_advertise_off:equinox_profiles_create_and_register:check /usr/bin/bluez-equinox-profiles started and registered on dbus
    if [ ! -z "$result" ]; then 
    	echo SUCCESS:/usr/bin/bluez-equinox-profiles started and registered on dbus:$result
   		echo stop ble adverting.
    	result=`dbus-send --print-reply --type=method_call --system --dest=org.bluez.extapp /org/bluez/extapp org.bluez.extapp.Leadv string:'off' | grep OK`
     	if [ ! -z "$result" ]; then
    		echo SUCCESS: advertise_off.
    	else
    		echo ERROR: advertise_off.
    	fi
    else
    	echo ERROR:/usr/bin/bluez-equinox-profiles NOT started and registered on dbus
    fi
}


set_sleepmode_param ()
{
	local result=`hciconfig hci0 -a | grep RUNNING`
	if [ ! -z "$result" ]; then
		if [ -d /sys/class/bluetooth/hci0 ]; then
			result=`hcitool -i hci0 cmd 0x3F 0x0027 01 01 01 00 00 01 01 00 00 00 00 01`
			echo $0:result $result
		fi 
		
	else
    	echo ERROR:$0 :hci0 is not UP
	fi 
}

adaptor_power_on ()
{
	local result=`dbus-send --system --dest=org.bluez --print-reply /org/bluez/hci0 org.freedesktop.DBus.Properties.Set string:org.bluez.Adapter1 string:Powered variant:boolean:true`
	
	local result1=`dbus-send --system --dest=org.bluez --print-reply /org/bluez/hci0 org.freedesktop.DBus.Properties.Get string:org.bluez.Adapter1 string:Powered | grep true`

	if [ ! -z "$result1" ]; then
		echo $0:adapter /org/bluez/hci0 power is UP
		logger $0:adapter /org/bluez/hci0 power is UP
	else
    	echo ERROR:$0 :cant power adapter /org/bluez/hci0:[$result1]
    	logger ERROR:$0 :cant power adapter /org/bluez/hci0 :[$result1]
	fi 
}

recover_adaptor_fw_failure ()
{
    if [ "$type" -eq 1 ] && [ "$subtype" -eq 4 ]
    then
    	local daemonpid=`pgrep bluetoothd`
    	local profilespid=`pgrep bluez-equinox-profiles`
    	if [ ! -z "$daemonpid" ] && [ ! -z "$profilespid" ]; then
    		echo remove previous instances of hciattach
    		killall hciattach-murata
    		sleep 1
			echo  attach_start
			attach_start  &             
			logger attach_start done
			echo attach_start done
			local timeout=$TIMEOUT_MAX
			
			while ( [ ! -d /sys/class/bluetooth/hci0 ] && [ "$timeout" -ge 0 ] )
    		do
    			timeout=$(($timeout-1))
        		sleep 1
    		done 
    
    		if [ -d /sys/class/bluetooth/hci0 ] ; then  
    			logger " hci0 device created"       
				logger "recreate ble profiles"
				echo "recreate ble profiles"
				equinox_profiles_create_and_register 
				logger "adaptor_power_on"
				echo "adaptor_power_on"
				adaptor_power_on
				logger "set_sleepmode_param"
				echo "set_sleepmode_param"
				set_sleepmode_param
				logger "advertise_on"
				echo "advertise_on"
				equinox_profiles_advertise_on
    		else
    			logger ERROR:no hci device created
    			echo ERROR bluetoothd_start:no hci device created
    		fi
			
		else
    		logger ERROR:$0:bluezdaemon not running 
    		echo ERROR:$0:bluezdaemon not running
		fi
		
    else
    	logger ERROR:$0:incompatible hw type $type subtype $subtype 
    	echo ERROR:$0:incompatible hw type $type subtype $subtype 
    fi

}

display_gpios ()
{
	echo "murata_bt_reset(gpio 47) direction:"
	cat /sys/class/gpio/gpio47/direction
	echo value:
	cat /sys/class/gpio/gpio47/value
		
	echo "murata_dev_wake(gpio 45) direction:"
	cat /sys/class/gpio/gpio45/direction
	echo value:
	cat /sys/class/gpio/gpio45/value
	
	echo "murata_host_wake(gpio 46) direction:"
	cat /sys/class/gpio/gpio46/direction
	echo value:
	cat /sys/class/gpio/gpio46/value
}

if [ "$1" = "reattach" ]
then
	echo "hciattach ttyLP2"
	logger hciattach ttyLP2
    if [ "$type" -eq 1 ] && [ "$subtype" -eq 4 ]
    then
    	echo remove previous instances
    	killall hciattach-murata
    	sleep 1
		echo  attach_start
		attach_start  &             
		logger attach_start started
		echo attach_start started
		echo `date`
		echo $0 exits
		logger `date`: $0 exits
    else
    	logger ERROR:incompatible hw type $type subtype $subtype 
    	echo ERROR:incompatible hw type $type subtype $subtype 
    fi
fi

if [ "$1" = "ssp" ]
then
	set_sleepmode_param
fi

if [ "$1" = "extapp" ]
then
		bluez_equinox_profiles_start &
fi


if [ "$1" = "daemonrestart" ]
then
	killall bluetoothd
	sleep 1
	bluetoothd_start & 
fi

if [ "$1" = "crreg" ]
then
	equinox_profiles_create_and_register 
fi

if [ "$1" = "advon" ]
then
	equinox_profiles_advertise_on
fi

if [ "$1" = "advoff" ]
then
	equinox_profiles_advertise_off
fi

if [ "$1" = "recover" ]
then
	recover_adaptor_fw_failure
fi

if [ "$1" = "poweron" ]
then
	adaptor_power_on
fi


if [ "$1" = "start" ]
then
    if [ "$type" -eq 1 ] && [ "$subtype" -eq 4 ]
    then
    	echo `date`
		echo $0 start
		logger `date`: $0 start
    
		attach_start  &             
		bluetoothd_start & 
		bluez_equinox_profiles_start &
		equinox_profiles_create_and_register &
		
		echo `date`
		echo $0 exits
		logger `date`: $0 exits
		
    else
    	logger ERROR:incompatible hw type $type subtype $subtype 
    	echo ERROR:incompatible hw type $type subtype $subtype 
    fi
fi


if [ "$1" = "stop" ]
then
    if [ "$type" -eq 1 ] && [ "$subtype" -eq 4 ]
    then
        hciconfig hci0 down
        logger kill bluetoothd
        killall bluetoothd
        logger kill hciattach ttyLP2
        killall hciattach-murata
        logger kill bluez-equinox-profiles application
        killall bluez-equinox-profiles
        bt_murata_gpiodeinit
    fi
fi
