This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathentry.sh
74 lines (61 loc) · 2.34 KB
/
entry.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
set -e
# Run balena base image entrypoint script
/usr/bin/entry.sh echo "Running balena base image entrypoint..."
function reset_hci_interface () {
local INTERFACE=$1
echo "Resetting $i"
btmgmt --index $i discov off > /dev/null
btmgmt --index $i pairable off > /dev/null
btmgmt --index $i connectable off > /dev/null
}
# Bluetooth block environment variables and defaults
DEVICE_NAME=${BLUETOOTH_DEVICE_NAME:-$(printf "balenaOS %s"$(echo "$BALENA_DEVICE_UUID" | cut -c -4))}
HCI_INTERFACE=${BLUETOOTH_HCI_INTERFACE:-"hci0"}
PAIRING_MODE=${BLUETOOTH_PAIRING_MODE:-"SSP"}
PIN_CODE=${BLUETOOTH_PIN_CODE:-"0000"}
echo "--- Bluetooth ---"
echo "Starting bluetooth service with settings:"
echo "- Device name: "$DEVICE_NAME
echo "- HCI interface: "$HCI_INTERFACE
echo "- Pairing mode: "$PAIRING_MODE
echo "- PIN code: "$PIN_CODE
# Get available interfaces
HCI_INTERFACES=$(btmgmt info | awk 'BEGIN { ORS=" " }; /^ *hci/ {gsub(":", ""); print $1}')
FS=' ' read -r -a HCI_INTERFACES <<< "$HCI_INTERFACES"
echo "Available HCI interfaces: "${HCI_INTERFACES[@]}
# Bail out if provided HCI interface is invalid
if [[ ! "${HCI_INTERFACES[@]}" =~ "$HCI_INTERFACE" ]]; then
echo "Exiting... selected HCI interface is invalid: $HCI_INTERFACE"
exit 0
fi
# Reset all interfaces. This helps shut off interfaces from previous runs.
for i in "${HCI_INTERFACES[@]}"
do
reset_hci_interface "$i"
done
# Configure selected interface
echo "Configuring selected interface: $HCI_INTERFACE"
# Set device name
btmgmt --index $HCI_INTERFACE name "$DEVICE_NAME"
# Ensure bluetooth is ready to connect
btmgmt --index $HCI_INTERFACE connectable on
btmgmt --index $HCI_INTERFACE pairable on
btmgmt --index $HCI_INTERFACE discov on
# Set bluetooth pairing mode:
# - SSP (default): Secure Simple Pairing, no PIN code required
# - LEGACY: disable SSP mode, PIN code required
if [[ $PAIRING_MODE == "LEGACY" ]]; then
AGENT_CAPABILITY="KeyboardDisplay"
btmgmt --index $HCI_INTERFACE ssp off
echo "Pairing mode set to 'Legacy Pairing Mode (LPM)'. PIN code is required."
else
AGENT_CAPABILITY="NoInputNoOutput"
btmgmt --index $HCI_INTERFACE ssp on
echo "Pairing mode set to 'Secure Simple Pairing Mode (SSPM)'. PIN code is NOT required."
fi
# If command starts with an option, prepend bluetooth-agent to it
if [[ "${1#-}" != "$1" ]]; then
set -- bluetooth-agent "$@"
fi
exec "$@"