#!/sbin/runscript
# Copyright angellfear
# Distributed under the terms of the GNU General Public License v2


depend() {
	use logger dns
	need net
}

CSR_CONF="/etc/bluez.psr"

checkconfig() {
	if [ ! -e "${CSR_CONF}" ] ; then
		eerror "You need an ${CSR_CONF} file to run bluethooth"
		return 1
	fi
	if [ ! -e "/sys/class/gpio/export" ] ; then
		eerror "You need an /sys/class/gpio/export file to run bluethooth"
		return 1
	fi
}


start() {
	checkconfig || return 1
	ebegin "Starting ${SVCNAME}"

	modprobe hci_uart

	echo "83" > "/sys/class/gpio/export"
	echo "114" > "/sys/class/gpio/export"
	echo "out" > "/sys/class/gpio/gpio114/direction"
	echo "1" > "/sys/class/gpio/gpio114/value"
	echo "out" > "/sys/class/gpio/gpio83/direction"
	echo "0" > "/sys/class/gpio/gpio83/value"
	echo "in" > "/sys/class/gpio/gpio83/direction"
	sleep 3

	bccmd -t bcsp -d /dev/ttyS1 psload -r ${CSR_CONF}

	hciattach -s 115200 /dev/ttyS1 bcsp 115200 noflow
	hciconfig hci0 name linportege
	hciconfig hci0 up
	hciconfig hci0 piscan
	hciconfig -a hci0
	eend $?
}

stop() {
	ebegin "Stopping ${SVCNAME}"
	killall hciattach
	echo "out" > "/sys/class/gpio/gpio114/direction"
	echo "0" > "/sys/class/gpio/gpio114/value"
	echo "out" > "/sys/class/gpio/gpio83/direction"
	echo "0" > "/sys/class/gpio/gpio83/value"
	eend $?
}

reload() {
	checkconfig || return 1
	ebegin "Reloading ${SVCNAME}"
	stop
	sleep 3
	start
	eend $?
}
