#!/bin/sh

# SPDX-FileCopyrightText: 2021-2023 KUNBUS GmbH
#
# SPDX-License-Identifier: GPL-2.0-or-later

# referenced pi-bluetooth, bluetooth helper for Revolution Pi BT

set -e

if [ "$#" -ne 1 ]; then
	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

if ! /bin/hciconfig "$dev" | grep -q "Bus: UART"; then
	echo Not UART-attached BT Modem
	exit 0
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

# 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
