#!/usr/bin/env sh
# For on-board BT, configure the BDADDR if necessary and route SCO packets
# to the HCI interface (enables HFP/HSP)

set -e

if [ "$#" -ne '1' ]; then
  /usr/bin/echo "Usage: ${0} <bluetooth hci device>"
  exit 0
fi

DEV="${1}"

# Need to bring hci up before looking at MAC as it can be all zeros during init
/usr/bin/hciconfig "${DEV}" up
if ! /usr/bin/hciconfig "${DEV}" | /usr/bin/grep --quiet 'Bus: UART'; then
  /usr/bin/echo 'Not a UART-attached BT Modem'
  exit 0
fi

if ( /usr/bin/hcitool -i "${DEV}" dev | /usr/bin/grep --extended-regexp --quiet '\s43:4[35]:|AA:AA:AA:AA:AA:AA' ); then
  SERIAL=$(< '/proc/device-tree/serial-number' /usr/bin/cut '-c9-')
  B1=$(/usr/bin/echo "${SERIAL}" | /usr/bin/cut '-c3-4')
  B2=$(/usr/bin/echo "${SERIAL}" | /usr/bin/cut '-c5-6')
  B3=$(/usr/bin/echo "${SERIAL}" | /usr/bin/cut '-c7-8')
  BDADDR=$(/usr/bin/printf '0x%02x 0x%02x 0x%02x 0xeb 0x27 0xb8' $((0x$B3 ^ 0xaa)) $((0x$B2 ^ 0xaa)) $((0x$B1 ^ 0xaa)))

  /usr/bin/hcitool -i "${DEV}" cmd 0x3f 0x001 "${BDADDR}"
  /usr/bin/hciconfig "${DEV}" reset
else
  /usr/bin/echo 'Raspberry Pi BDADDR already set'
fi

# Route SCO packets to the HCI interface (enables HFP/HSP)
/usr/bin/hcitool -i "${DEV}" cmd 0x3f 0x1c 0x01 0x02 0x00 0x01 0x01 > '/dev/null'

# Force reinitialisation to allow extra features such as Secure Simple Pairing
# to be enabled, for currently unknown reasons. This requires bluetoothd to be
# running, which it isn't yet. Use this kludge of forking off another shell
# with a delay, pending a complete understanding of the issues.
(/usr/bin/sleep '5s'; /usr/bin/bluetoothctl power off; /usr/bin/bluetoothctl power on) &
