Skip to content

Commit

Permalink
bthelper: Add the ability to set the Pi BDADDR
Browse files Browse the repository at this point in the history
If Bluetooth is being enabled via the kernel, hciattach isn't run and
we lose the ability to set the BDADDR. Extend bthelper to use the
bdaddr utility on Pis that haven't had the BT section of the OTP
programmed.

Also ensure that btuart doesn't do anything unless necessary.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
  • Loading branch information
pelwell authored and XECDesign committed Jul 29, 2020
1 parent df540ff commit 294a534
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
33 changes: 25 additions & 8 deletions usr/bin/bthelper
@@ -1,21 +1,38 @@
#!/bin/sh
# For on-board BT, route SCO packets to the HCI interface (enables HFP/HSP)
# 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
echo "Usage: $0 <bluetooth hci device>"
exit 0
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
/bin/hciconfig $dev up
/bin/hciconfig $dev | grep -q "Bus: UART" ||
(echo Not a UART-attached BT Modem; exit 0)

if ( /usr/bin/bdaddr -i "$dev" | grep -q -E '\s(B8:27:EB:|DC:A6:32:)' ); then
echo Raspberry Pi BDADDR already set
else
SERIAL=`cat /proc/device-tree/serial-number | cut -c9-`
B1=`echo $SERIAL | cut -c3-4`
B2=`echo $SERIAL | cut -c5-6`
B3=`echo $SERIAL | cut -c7-8`
BDADDR=`printf b8:27:eb:%02x:%02x:%02x $((0x$B1 ^ 0xaa)) $((0x$B2 ^ 0xaa)) $((0x$B3 ^ 0xaa))`

/usr/bin/bdaddr -i "$dev" $BDADDR
/bin/hciconfig $dev reset
fi

# Force reinitialisation to allow extra features such as Secure Simple Pairing
# to be enabled
/usr/bin/bluetoothctl power off
/usr/bin/bluetoothctl power on

dev="$1"
# Need to bring hci up before looking at MAC as it can be all zeros during init
/bin/hciconfig "$dev" up
/bin/hciconfig "$dev" | grep -qE "BD Address: (B8:27:EB|DC:A6:32):" || exit 0
# 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

5 changes: 5 additions & 0 deletions usr/bin/btuart
Expand Up @@ -11,6 +11,11 @@ else
BDADDR=`printf b8:27:eb:%02x:%02x:%02x $((0x$B1 ^ 0xaa)) $((0x$B2 ^ 0xaa)) $((0x$B3 ^ 0xaa))`
fi

if [ -e /sys/class/bluetooth/hci0 ]; then
# Bluetooth is already enabled
exit 0
fi

uart0="`cat /proc/device-tree/aliases/uart0`"
serial1="`cat /proc/device-tree/aliases/serial1`"

Expand Down

0 comments on commit 294a534

Please sign in to comment.