#!/bin/bash

bt_hciattach="rtk_hciattach"

is_rtl_uart_bt()
{
        cat /proc/device-tree/modules_usrload | grep 8852bs
        if [ "$?" -eq 0 ]; then
                echo "1"
        else
                echo "0"
        fi
}

is_aic_uart_bt()
{
        cat /proc/device-tree/modules_usrload | grep aic8800_btlpm
        if [ "$?" -eq 0 ]; then
                echo "1"
        else
                echo "0"
        fi
}

if [ "$(is_aic_uart_bt)" -eq 1 ]; then
	bt_hciattach="hciattach"
fi

is_uart_bt()
{
	if [ "$(is_rtl_uart_bt)" -eq 1 ]; then
		echo "1"
	elif [ "$(is_aic_uart_bt)" -eq 1 ]; then
		echo "1"
	else
		echo "0"
	fi
}

start_hci_attach()
{
	h=`ps | grep "$bt_hciattach" | grep -v grep`
	[ -n "$h" ] && {
		killall "$bt_hciattach"
	}

	#realtek h5 init
	echo 0 > /sys/class/rfkill/rfkill0/state;
	sleep 1
	echo 1 > /sys/class/rfkill/rfkill0/state;
	sleep 1

	if [ "$(is_rtl_uart_bt)" -eq 1 ]; then
		"$bt_hciattach" -n -s 115200 /dev/ttyS2 rtk_h5 >/dev/null 2>&1 &
	fi

	if [ "$(is_aic_uart_bt)" -eq 1 ]; then
        	"$bt_hciattach" -s 1500000 /dev/ttyS2 any 1500000 flow nosleep &
        	hciconfig hci0 up 
	fi

	wait_hci0_count=0
	while true
	do
		[ -d /sys/class/bluetooth/hci0 ] && break
		sleep 0.1 
		let wait_hci0_count=wait_hci0_count+1
		[ $wait_hci0_count -eq 70 ] && {
			echo "bring up hci0 failed"
			exit 1
		}
	done
	return 0
}

hci_start() {
	h=$(is_uart_bt)
	if [ "$h" -eq 1 ]; then
		echo 1 > /tmp/hci_fifo
	fi
}

hci_stop() {
	h=$(is_uart_bt)
	if [ "$h" -eq 1 ]; then
		echo 0 > /tmp/hci_fifo
	fi
}

hci_start_imp()
{
	h=`ps | grep "$bt_hciattach" | grep -v grep`
	if [ -n "$h" ] ;then
		echo "Bluetooth init has been completed!!"
	else
		start_hci_attach
	fi
}

hci_stop_imp()
{

	h=`ps | grep "$bt_hciattach" | grep -v grep`

	if [ -n "$h" ] ;then
		echo 0 > /sys/class/rfkill/rfkill0/state;
		killall "$bt_hciattach"
	fi
	return 0
}

loop() {
	if [ ! -p "/tmp/hci_fifo" ];then
		mkfifo /tmp/hci_fifo
		chmod 666 /tmp/hci_fifo
	fi
	h=$(is_uart_bt)
	if [ "$h" -eq 1 ] ;then
		echo "uart bt"
	else
		return
	fi
	sleep 5
	hci_start_imp
        while true;do
		read flag < /tmp/hci_fifo
		echo $flag
		if [[ "$flag" -eq 1 ]];then
		        hci_start_imp
	        else
	 	        hci_stop_imp
		fi		
		sleep 1
	done
}

ble_start() {
	if [ -d "/sys/class/bluetooth/hci0" ];then
		echo "Bluetooth init has been completed!!"
	else
		start_hci_attach
	fi

	hci_is_up=`hciconfig hci0 | grep UP RUNNING`
	[ -z "$hci_is_up" ] && {
		hciconfig hci0 up
	}

	MAC_STR=`hciconfig | grep "BD Address" | awk '{print $3}'`
	LE_MAC=${MAC_STR/2/C}
	OLD_LE_MAC_T=`cat /sys/kernel/debug/bluetooth/hci0/random_address`
	OLD_LE_MAC=$(echo $OLD_LE_MAC_T | tr [a-z] [A-Z])
	if [ -n "$LE_MAC" ];then
		if [ "$LE_MAC" != "$OLD_LE_MAC" ];then
			hciconfig hci0 lerandaddr $LE_MAC
		else
			echo "the ble random_address has been set."
		fi
	fi
}

case "$1" in
  loop|"")
	loop
	;;
  ble_start)
	    ble_start
		;;
  hci_start)
		hci_start
		;;
  hci_stop)
		hci_stop
		;;
  *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac
