#!/bin/sh
#===============================================================================
#
#  Copyright (C) 2024,2025 by Digi International Inc.
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2 as published by
#  the Free Software Foundation.
#
#
#  !Description: Initialize Bluetooth interface
#
#===============================================================================

SCRIPTNAME="$(basename ${0})"
export MBT_TRANSPORT=/dev/ttySTM1

log() {
	if type "systemd-cat" >/dev/null 2>/dev/null; then
		systemd-cat -p "${1}" -t "${SCRIPTNAME}" printf "%s" "${2}"
	else
		logger -p "${1}" -t "${SCRIPTNAME}" "${2}"
	fi
}

bt_power() {
	#CCMP2 BT_REG_EN GPIO PZ5
	gpioset gpiochip11 5="${1}"
}

bt_init() {
	bt_power 0
	sleep 0.5
	bt_power 1
	# Load Bluetooth firmware on device
	mbt download /lib/firmware/brcm/CYW55500A1.hcd
	# Attach serial UART to the Bluetooth stack
	btattach -B /dev/ttySTM1 -P bcm -S 921600 &
	sleep 2

	# Up the interface to be able to send hci commands
	if ! hciconfig hci0 up; then
		log err "FAILED (hci0 up)"
		exit
	fi
	log info "OK"
}

bt_stop() {
	killall btattach
	bt_power 0
}

case "$1" in
	start)
		bt_init
		;;
	stop)
		bt_stop
		;;
	restart)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
		;;
esac
