#!/bin/sh
# Author: Aaron Czajka
# Company: Inspectron Inc. 2019
#
# Licensed under terms of GPLv2

# console colors
RED='\033[0;31m'
ORNG='\033[0;33m'
NC='\033[0;0m'


BT_RST_GPIO=$1
BT_FW_DIR="/lib/firmware/rtlbt/"

if [ -z "$BT_RST_GPIO" ]; then
	echo -e "${RED} Invalid GPIO for BT_RST line ${NC}"
	exit
fi

#start the bluetooth daemon
/usr/libexec/bluetooth/bluetoothd --compat &

#toggle the reset line
echo ${BT_RST_GPIO} > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio${BT_RST_GPIO}/direction
echo 0 > /sys/class/gpio/gpio${BT_RST_GPIO}/value
sleep 1
echo 1 > /sys/class/gpio/gpio${BT_RST_GPIO}/value

#load the bluetooth kernel module
insmod ${BT_FW_DIR}hci_uart.ko

#tool to initialize the bluetooth firmware
rtk_hciattach -n -s 115200 ttyS0 rtk_h5 &

#let that tool do what it do
sleep 3

#tell the kernel this is the device we want to use
hciconfig hci0 up

