#!/bin/sh
#
# Start the bt module
#

RUN_SPECIFIC_FILE="/home/qlx/cfg/run_specific"

case "$1" in
  start)
	echo -n "Starting BT module..."
	if [ -e ${RUN_SPECIFIC_FILE} ]; then
		# force bluetoothd & hciattch to cpu core 2 affinity
		/usr/bin/taskset 0x02 hciattach -s 115200 /dev/ttymxc1 texas 3000000 noflow
		HCIATTACH_STATUS=$?
		if [ ${HCIATTACH_STATUS} -eq 0 ]; then
			/usr/bin/taskset 0x02 bluetoothd
		fi;
		echo "RUN_SPECIFIC"
	else
		# NO core affinity is being used
		hciattach -s 115200 /dev/ttymxc1 texas 3000000 noflow
		HCIATTACH_STATUS=$?
		if [ ${HCIATTACH_STATUS} -eq 0 ]; then
			bluetoothd
		fi;
		echo "RUN_GENERAL"
	fi;

	#bluetoothd
	#hciattach -s 115200 /dev/ttymxc1 texas 3000000 noflow
#	if [ $? -eq 0 ]; then
	if [ ${HCIATTACH_STATUS} -eq 0 ]; then
		myHOST=`hostname`
		btMACID1=`hcitool dev|grep hci0|cut -d':' -f5`
		btMACID2=`hcitool dev|grep hci0|cut -d':' -f6`
		btNAME="${myHOST}_${btMACID1}${btMACID2}"
		echo ${btNAME} > /tmp/btNAME
		hciconfig hci0 name ${btNAME}
		hciconfig hci0 up piscan
		mknod /dev/rfcomm0 c 216 0
		sdptool add --channel=7 SP
		if [ -e ${RUN_SPECIFIC_FILE} ]; then
			# force rfcomm to cpu core 2 affinity
			taskset 0x02 rfcomm -i hci0 watch /dev/rfcomm0 7 &
			echo "RUN_SPECIFIC"
		else
			# NO core affinity is being used
			rfcomm -i hci0 watch /dev/rfcomm0 7 &
			echo "RUN_GENERAL"
		fi;
	fi
	;;
  stop)
	echo -n "Stopping BT module..."
	killall rfcomm
	hciconfig hci0 down
	;;
  restart|reload)
	"$0" stop
	"$0" start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?


