Skip to content

Commit

Permalink
Add more resilience to bluetooth infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-santiago committed Jul 2, 2024
1 parent 0cccb32 commit 3a50ab3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ create_usb_sto_mount_point() {
}

patch_out_etc_bluetooth_main_conf() {
# TIP(Rafael): With it I got rid of cases when I could connect to a bluetooth device
# but no sound emanated from it.
# TIP(Rafael): With bredr stuff I got rid of cases when I could connect to a
# bluetooth device but no sound emanated from it.
# With multiple stuff I got rid of low quality sound connection cases.
sed -i 's/.*ControllerMode.*=.*$/ControllerMode = bredr/g' /etc/bluetooth/main.conf &&
systemctl restart bluetooth >/dev/null 2>&1
sed -i 's/.*MultiProfile.*=.*$/MultiProfile = multiple/g' /etc/bluetooth/main.conf &&
systemctl restart bluetooth >/dev/null 2>&1
echo 0
}

Expand Down
16 changes: 16 additions & 0 deletions src/internal/actions/probe_bluetooth_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@ func ProbeBluetoothDevices(eutherpeVars *vars.EutherpeVars,
return err
}
eutherpeVars.BluetoothDevices = blueDevs
if len(eutherpeVars.CachedDevices.BlueDevId) == 0 {
pairedDevices := bluebraces.GetPairedDevices()
pairedDevicesLen := len(pairedDevices)
if pairedDevicesLen > 1 {
unpairNosyDevices(pairedDevices)
} else if pairedDevicesLen == 1 {
eutherpeVars.CachedDevices.BlueDevId = pairedDevices[0].Id
}
}
return nil
}

func unpairNosyDevices(pairedDevices []bluebraces.BluetoothDevice) {
for _, nosyDevice := range pairedDevices {
bluebraces.DisconnectDevice(nosyDevice.Id)
bluebraces.UnpairDevice(nosyDevice.Id)
}
}
9 changes: 9 additions & 0 deletions src/internal/bluebraces/bluetoothctl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ info() {
return $retval
}

paired_devices() {
exit_code=`eval_exit_code`
if [[ $exit_code == 0 ]] ; then
printf "Device DE:AD:BE:EF:FF:FF Philips TAT1235\nDevice BA:BA:CA:BA:BA:CA Babaca Sound Pinico's\n" >&2
fi
return $exit_code
}

case $1 in
power) exit `power $2` ;;
discoverable) exit `discoverable $2` ;;
Expand All @@ -118,5 +126,6 @@ case $1 in
untrust) exit `untrust $2` ;;
scan) exit `scan $2` ;;
info) exit `info $2` ;;
paired-devices) exit `paired_devices $2` ;;
*) exit 1 ;;
esac
29 changes: 29 additions & 0 deletions src/internal/bluebraces/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@ const (
var g_HasSystemCtlCallNr int
var g_HasSystemCtl bool

func GetPairedDevices(customPath ...string) []BluetoothDevice {
cp := getToolPath(customPath...)
out, err := exec.Command(cp + "bluetoothctl", "paired-devices").CombinedOutput()
pairedDevices := make([]BluetoothDevice, 0)
if err != nil {
return pairedDevices
}
foundDevices := strings.Split(string(out), "\n")
for _, device := range foundDevices {
off := strings.Index(device, "Device ")
if off == -1 {
continue
}
off = strings.Index(device[7:], " ")
if off == -1 {
continue
}
off += 7
newDevice := BluetoothDevice{}
newDevice.Id = device[7:off]
if len(device) < off + 1 {
continue
}
newDevice.Alias = device[off+1:]
pairedDevices = append(pairedDevices, newDevice)
}
return pairedDevices
}

func Wear(customPath ...string) error {
cp := getToolPath(customPath...)
err := exec.Command(cp + "bluetoothctl", "power", "on").Run()
Expand Down
15 changes: 15 additions & 0 deletions src/internal/bluebraces/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,18 @@ func TestUntrustDeviceMustFail(t *testing.T) {
t.Error("bluebrances.UntrustDevice() has succeeded.")
}
}

func TestGetPairedDevicesMustPass(t *testing.T) {
pairedDevices := GetPairedDevices("../bluebraces/")
if len(pairedDevices) != 2 {
t.Errorf("pairedDevices does not have length two. %v", pairedDevices)
} else if pairedDevices[0].Id != "DE:AD:BE:EF:FF:FF" {
t.Error("Returned a device with unexpected id.")
} else if pairedDevices[0].Alias != "Philips TAT1235" {
t.Error("Returned a device with unexpected alias.")
} else if pairedDevices[1].Id != "BA:BA:CA:BA:BA:CA" {
t.Error("Returned a device with unexpected id.")
} else if pairedDevices[1].Alias != "Babaca Sound Pinico's" {
t.Error("Returned a device with unexpected alias.")
}
}

0 comments on commit 3a50ab3

Please sign in to comment.