#!/bin/sh

# don't use highspeed UART
NOHU=/var/log/NOHU

# Startup script to load CSR stack and start Bluetooth Engine

# Use BTid ROM token if available, otherwise btconfig.  If no btconfig,
# append 2 randomly generated bytes to 'ID' below, and update btconfig.
ID="00:02:5b:12"
btid=`cat /dev/tokens/BToADDR`
if test -z $btid; then
   btid=`cat /var/log/btconfig`
   if test -z $btid; then
      # get two random bytes in the right format
      btid="$ID:`dd if=/dev/urandom bs=2 count=1 | hexdump -e '1/1 \"%02x:\"'`"
      btid=`expr substr "$btid" 1 17`
      echo $btid > /var/log/btconfig
   fi
fi

STACK_NAME=PmBtStack
ENGINE_NAME=PmBtEngine
CONTACTS_NAME=PmBtContacts
STACK_PATH=/usr/bin/$STACK_NAME

btck=26000	# 26MHz clock
btbaud=115200 # default 115200 baud rate
PS_PORT=16888

# initialize default non-highspeed UART port
DEV=/dev/ttyS2

# load CSR stack
if test -e $STACK_PATH; then
   echo $PS_PORT > /var/log/btport

   # load and use highspeed UART driver if available
   if test -e /dev/btuart && ! test -e $NOHU; then
      DEV=/dev/btuart
	  btbaud=3686400
   fi

   killall -w $ENGINE_NAME
   killall -w $STACK_NAME
   killall -w $CONTACTS_NAME
   echo exec $STACK_PATH -C $DEV -X $btid -F $btck -U $btbaud 
   exec $STACK_PATH -C $DEV -X $btid -F $btck -U $btbaud 
else
   logger -t $0 '*** BT stack failed to start.'
fi
