#!/bin/sh

. /etc_ro_fs/autoconf.conf
. /etc_ro_fs/init.d/common

offon()
{
	for n in 1 2 3 4; do
		echo 1 > /proc/bt_reset
	done	
	echo 0 > /proc/bt_reset
}
get_hci_dev()
{
	# get the list of usb modules - dect modules display version ericsson
	for dev in $(hciconfig | sed -n 's/\(hci.\).*/\1/p'); do
		hciconfig $dev up
		hciconfig $dev version | grep -q Ericsson || echo $dev
	done
}

if [ "$CONFIG_GENERIC_SPI" != y ]; then
	dev_ttySPI0=/dev/ttySPI0
	dev_ttySPI2=/dev/ttySPI0
	config_spi()   # param : CS speed
	{
		rmmod tty_spi
		modprobe tty_spi speed=$2 spi_cs=$1
	}
else
	dev_ttySPI0=/dev/ttySPI0
	dev_ttySPI2=/dev/ttySPI2	
	config_spi() 
	{
		echo -n
	}
fi
	

start () {

	force_start="$1"
	echo "Param: $1"

	if [ -f /var/run/bluetooth ]; then
		echo "bluez already started"
		exit 1
	fi
	
	readconf /etc/bluedsl.conf BT_WATCHDOG BLUETOOTH_SERVICES_DISABLE
	readconf /etc/hardware.conf BLUETOOTH_ADDR_0
	readconf /etc/bt_firmware.conf  DISABLE_BT_UPDATE BT_SW_REQUIRED_HEX
	
	
	#reset - no printk - no ttyS0
	modprobe bt_reset
	#offon
	
	
	# bluetooth configuration
	/bin/echo "Bluetooth configuration ..."
	modprobe bluez
	modprobe hci_uart
	modprobe l2cap
	modprobe rfcomm
	modprobe bnep
	modprobe sco
	
	echo "choix canal com et Verification du boot\n"
	config_spi 0 1
	echo 1 > /proc/sys/kernel/printk 
	sleep 1
	echo 1 > /proc/tty_selector
	
	# recherche du numero de version du hard
	hard_ver=""
	for count in 1 2 3; do 
		# test avec le SPI
		offon 
		v=$(blueflash -m bb2g -s $dev_ttySPI0 -S 9600 -T 500 -v hard)
		if [ $? -eq 0 ]; then
			# mode SPI
			hard_ver=$v
			break
			
		else
			# on test avec l'uart
			offon
			v=$(blueflash -m bb2g -s /dev/ttyS1 -T 500 -v hard)
			if [ $? -eq 0 ]; then
				hard_ver=$v
				break
			fi
		fi
	done
	
	
	offon
	sleep 1
	echo 0 > /proc/tty_selector
	echo 8 > /proc/sys/kernel/printk
	
	#if communcication failed, it is may be because the bootloader is corrupted. The script will flash it after
	#if [ -z "$hard_ver" ]; then 
	#	echo "There is no BT module connected"
	#	exit 1
	#fi
	
	
	echo "hardware version returned by bootloader: $hard_ver"
	# hack, comme on est sur qu'il n'y a qu'une seule version de hardware
	hard_ver=0x31303032   #il n'y a qu'une version de hard, c'est la 1002
	if [ ! -f /etc/bt_hard.ver ] || [ "$(cat /etc/bt_hard.ver)" != "HW_ver=$hard_ver" ]; then
		echo "write hard version in /etc/bt_hard.ver"
		echo "HW_ver=$hard_ver" > /etc/bt_hard.ver
	fi
	
	
	
	[ ! -f /etc/bluetooth/link_key ] && touch /etc/bluetooth/link_key
	
	[ "$BT_WATCHDOG" -eq 1 ] && WD="-w 60" || WD=""
	
	#try to connect a normal mode or in bcsp mode at 115200
	current_speed=115200
	sleep 2
	
	# try SPI CS0
	config_spi 0 2
	spi_mode=y
	hciattach_pid=$(hciattach -p -t 2 $WD $dev_ttySPI0 bcsp 19200 no)
	if [ "$hciattach_pid" = "" ]; then
		# try SPI CS2 (old hardware Blue5G-4 without strap and previous)
		config_spi 2 2
		hciattach_pid=$(hciattach -p -t 2 $WD $dev_ttySPI2 bcsp 19200 no)
	fi
	
	
	if [ "$hciattach_pid" = "" ]; then
		# impossible to join the firmware
		# if we found a bootloader, perform an update
		echo 1 > /proc/sys/kernel/printk
		echo 1 > /proc/tty_selector
	
		echo "BT module HW_ver=$hard_ver is here but doesn't work correctly -> udpate"
		exec /usr/blue/update_bt -f >> /root/adsld.log #try to update
	
		exit 1
	else
		uart_mode=bcsp
	fi
	echo "Mode: $uart_mode"
	
	hcidev=$(get_hci_dev)
	hciconfig $hcidev reset
	
	
	#lecture de la version HARD du module
	tmp=$(hcitool -i $hcidev cmd 0x3F 0x10)
	set ${tmp##*plen}
	shift 4
	SW_current=0x$9$8$7$6
	HW_current=0x$13$12$11$10
	echo "Hardware version: $HW_current - Software version: $SW_current" > /var/run/bluetooth_version
	cat /var/run/bluetooth_version
	
	if [ "$DISABLE_BT_UPDATE" != "y" ] && [ "$SW_current" != "$BT_SW_REQUIRED_HEX" ]; then
		echo "BT update needed to get version '$BT_SW_REQUIRED_HEX'"
		exec /usr/blue/update_bt -f >> /root/adsld.log  # try to update now
	fi
	
	
	#liberation port serie
	echo 0 > /proc/tty_selector
	echo 8 > /proc/sys/kernel/printk
	
	
	# Stop backlight if configured
	if [ "$CONFIG_NO_BACKLIGHT" = "y" ]; then
		hcitool -i $hcidev cmd 0x3A 0x200 00 00 00 00
	fi
	
	#save info for BT
	echo "
	BD_ADDR=$BLUETOOTH_ADDR_0
	HCIATTACH_PID=$hciattach_pid
	BT_HCIATTACH_PID=$hciattach_pid
	BT_HCIDEV=hci0
	BT_HCIDEV_ID=0
	UART_SPEED=$current_speed
	UART_MODE=$uart_mode
	FBUS_SPEED=$fbus_speed" > /var/run/bluetooth

	# Bluetooth services
	if [ "$CONFIG_BLUETOOTH_OFF" = "y" ] && [ "$force_start" != "force" ]; then 
		echo "Bluetooth is OFF in this configuration. Disable scan and exit."
		hciconfig $hcidev noscan
		exit 0 
	fi ; 
	
	
	hciconfig $hcidev reset
	#change the BD_addf
	set $(echo $BLUETOOTH_ADDR_0 | sed -e 's/:/ /g')
	hcitool -i $hcidev cmd 0x3F 0x2 $6 $5 $4 $3 $2 $1

	if [ "$CONFIG_WIFI_BLUETOOTH_COEXISTENCE" = y ] ; then 
		echo "Testing and configuring WIFI coexistence"
		/etc/init.d/coexistence
	fi
	if [ "$BLUETOOTH_SERVICES_DISABLE" != 1 ]; then
		start_blue_services
	else
		hciconfig $hcidev noscan
	fi
}


stop () 
{
	if [ ! -f /var/run/bluetooth ]; then
		echo "bluez not started"
		exit 1 
	fi
	stop_blue_services
	BT_HCIATTACH_PID=
	readconf /var/run/bluetooth  BT_HCIATTACH_PID
	kill $BT_HCIATTACH_PID
	
	#if no more bluetooth or dect device attached, remove hcid
	killall -0 hciattach 2>/dev/null || killall hcid
	rmmod rfcomm
	rmmod bnep
	rmmod hci_uart
	rmmod sco
	rmmod l2cap
	rmmod bluez
	rm -f /var/run/bluetooth
}

cmd=${1:-start}

hcidev ()
{
	if [ -f /var/run/bluetooth ]; then
		readconf /var/run/bluetooth  BT_HCIDEV
		echo -n "$BT_HCIDEV"
	else
		echo -n "none"
	fi
}

update_afh ()
{
	if [ ! -f /var/run/bluetooth ]; then
		echo "Bluetooth not running"
		return
	fi
	readconf /var/run/bluetooth  BT_HCIDEV
	echo "Configuring AFH"
	if [ -f /etc/wifi.conf ]; then
		readconf /etc/wifi.conf WIFI_CHANNEL WIFI_ENABLE
		if [ "$WIFI_ENABLE" = 1 ]; then
			case $WIFI_CHANNEL in
				1)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0x00 0x00 0x00 0xfe 0xff 0xff 0xff 0xff 0xff 0xff;;
				2)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0x00 0x00 0x00 0xc0 0xff 0xff 0xff 0xff 0xff 0xff;;
				3)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0x1f 0x00 0x00 0x00 0xf8 0xff 0xff 0xff 0xff 0xff;;
				4)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0x03 0x00 0x00 0x00 0xff 0xff 0xff 0xff 0xff;;
				5)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0x7f 0x00 0x00 0x00 0xe0 0xff 0xff 0xff 0xff;;
				6)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0x0f 0x00 0x00 0x00 0xfc 0xff 0xff 0xff;;
				7)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0x01 0x00 0x00 0x80 0xff 0xff 0xff;;
				8)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0x3f 0x00 0x00 0x00 0xf0 0xff 0xff;;
				9)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0xff 0x07 0x00 0x00 0x00 0xfe 0xff;;
				10)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0xff 0xff 0x00 0x00 0x00 0xc0 0xff;;
				11)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0xff 0xff 0x1f 0x00 0x00 0x00 0xf8;;
				12)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0xff 0xff 0xff 0x03 0x00 0x00 0x00;;
				13)	hcitool -i $BT_HCIDEV cmd 0x3 0x3f 0xff 0xff 0xff 0xff 0xff 0xff 0x7f 0x00 0x00 0x00;;
			esac
		fi
	fi
}

update_name ()
{
	if [ ! -f /var/run/bluetooth ]; then
		echo "Bluetooth not running"
		return
	fi
	readconf /var/run/bluetooth  BT_HCIDEV
	echo "Configuring BT name"
	if [ ! -z "$CONFIG_BTNAME_WIFI" ]; then
		readconf /etc/wifi.conf WIFI_ESSID
		name=$WIFI_ESSID
	else 
		readconf /etc/bluedsl.conf HOSTNAME 
		name=$HOSTNAME
	fi
	hciconfig $BT_HCIDEV name $name
	sed -e '/NAME=/d' /var/run/bluetooth > /var/run/bluetooth.tmp
	echo "NAME=$name
		BT_NAME=$name" >> /var/run/bluetooth.tmp
	mv -f /var/run/bluetooth.tmp /var/run/bluetooth
}

stop_blue_services () 
{
	if [ ! -f /var/run/bluetooth ]; then
		echo "Bluetooth not running"
		return
	fi
	readconf /var/run/bluetooth  BT_HCIDEV
	[ -f /etc/init.d/start_uma ] && /etc/init.d/start_uma stop
	killall sdpd dund pand 2>/dev/null
	hciconfig $BT_HCIDEV reset
	hciconfig $BT_HCIDEV noscan
	rm -f /var/run/bluetooth_services
}

start_blue_services ()
{
	if [ ! -f /var/run/bluetooth ]; then
		echo "Bluetooth not running"
		return
	fi
	if [ -f /var/run/bluetooth_services ]; then
		echo "Bluetooth services already running"
		return
	fi
	readconf /var/run/bluetooth  BT_HCIDEV
	
	hciconfig $BT_HCIDEV reset
	update_name
	hciconfig $BT_HCIDEV lm master
	
	update_afh

	echo "Connecting Daemons ..."
	killall -0 hcid 2>/dev/null || hcid
	echo "Launching sdp and rfcomm services ..."
	/sbin/sdpd
	# /usr/sbin/ip_server -i br0 &
	/bin/dund -s -P 1 --pppd /usr/sbin/memuld
	sdptool add DUN
	if [ "$CONFIG_UMA" = y ]; then
		/etc/init.d/start_uma start
	else
		/bin/pand -s -r NAP
	fi
	if [ "$CONFIG_BLUEZ_PHONE" = y ] || [ "$CONFIG_VOIP_INVH323_CTP" = y ] || [ "$CONFIG_VOIP_SIP_CTP" = y ]; then
		sdptool add CTP
	fi
	echo "restart pairingd"
	/etc/init.d/pair restart > /dev/null 2>&1
	
	touch /var/run/bluetooth_services
}

case $cmd in 
    start)
		start
	    ;;
    force-start)
    	start force
    ;;
    stop)
		stop
	    ;;
	hcidev)
		hcidev
		;;
	start_blue_services)
		start_blue_services
		;;
	stop_blue_services)
		stop_blue_services
		;;
	update_afh)
		update_afh
		;;
	update_name)
		update_name
		;;
    *)
		echo "Usage : $0 [start | stop | hcidev | start_blue_services | stop_blue_services | update_afh | update_name ]"
		exit 1
	    ;;
esac
