Skip to content

Commit c3cdc9d

Browse files
committed
linux: improve errors for gap
1 parent 5c61529 commit c3cdc9d

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

gap_linux.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
230230
// Check if the adapter is powered on.
231231
powered, err := a.adapter.GetProperty("org.bluez.Adapter1.Powered")
232232
if err != nil {
233-
return err
233+
return fmt.Errorf("bluetooth: scan bluez get Powered: %w", err)
234234
}
235235
if !powered.Value().(bool) {
236236
return errAdaptorNotPowered
@@ -244,11 +244,10 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
244244

245245
// This appears to be necessary to receive any BLE discovery results at all.
246246
defer a.adapter.Call("org.bluez.Adapter1.SetDiscoveryFilter", 0)
247-
err = a.adapter.Call("org.bluez.Adapter1.SetDiscoveryFilter", 0, map[string]interface{}{
247+
if err := a.adapter.Call("org.bluez.Adapter1.SetDiscoveryFilter", 0, map[string]interface{}{
248248
"Transport": "le",
249-
}).Err
250-
if err != nil {
251-
return err
249+
}).Err; err != nil {
250+
return fmt.Errorf("bluetooth: scan bluez SetDiscoveryFilter: %w", err)
252251
}
253252

254253
// Go through all connected devices and present the connected devices as
@@ -257,9 +256,8 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
257256
// list of cached devices as scan results as devices may be cached for a
258257
// long time, long after they have moved out of range.
259258
var deviceList map[dbus.ObjectPath]map[string]map[string]dbus.Variant
260-
err = a.bluez.Call("org.freedesktop.DBus.ObjectManager.GetManagedObjects", 0).Store(&deviceList)
261-
if err != nil {
262-
return err
259+
if err := a.bluez.Call("org.freedesktop.DBus.ObjectManager.GetManagedObjects", 0).Store(&deviceList); err != nil {
260+
return fmt.Errorf("bluetooth: scan dbus GetManagedObjects: %w", err)
263261
}
264262
devices := make(map[dbus.ObjectPath]map[string]dbus.Variant)
265263
for path, v := range deviceList {
@@ -293,7 +291,10 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
293291
// StopScan is called).
294292
select {
295293
case <-cancelChan:
296-
return a.adapter.Call("org.bluez.Adapter1.StopDiscovery", 0).Err
294+
if err := a.adapter.Call("org.bluez.Adapter1.StopDiscovery", 0).Err; err != nil {
295+
return fmt.Errorf("bluetooth: scan bluez StopDiscovery: %w", err)
296+
}
297+
return nil
297298
default:
298299
}
299300

@@ -302,7 +303,10 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
302303
if startDiscovery.Err != nil {
303304
close(cancelChan)
304305
a.scanCancelChan = nil
305-
return startDiscovery.Err
306+
if err := startDiscovery.Err; err != nil {
307+
return fmt.Errorf("bluetooth: scan bluez StartDiscovery: %w", err)
308+
}
309+
return nil
306310
}
307311
case sig := <-signal:
308312
// This channel receives anything that we watch for, so we'll have
@@ -462,7 +466,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
462466

463467
powered, err := a.adapter.GetProperty("org.bluez.Adapter1.Powered")
464468
if err != nil {
465-
return Device{}, err
469+
return Device{}, fmt.Errorf("bluetooth: bluez get Powered: %w", err)
466470
}
467471
if !powered.Value().(bool) {
468472
return Device{}, errAdaptorNotPowered
@@ -471,19 +475,18 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
471475
// Read whether this device is already connected.
472476
connected, err := device.device.GetProperty("org.bluez.Device1.Connected")
473477
if err != nil {
474-
return Device{}, err
478+
return Device{}, fmt.Errorf("bluetooth: bluez get Connected: %w", err)
475479
}
476480

477481
// Connect to the device, if not already connected.
478482
if !connected.Value().(bool) {
479483
// Start connecting (async).
480-
err := device.device.Call("org.bluez.Device1.Connect", 0).Err
481-
if err != nil {
484+
if err := device.device.Call("org.bluez.Device1.Connect", 0).Err; err != nil {
482485
return Device{}, fmt.Errorf("bluetooth: failed to connect: %w", err)
483486
}
484487

485-
// Wait until the device has connected.
486-
connectChan := make(chan struct{})
488+
// Wait until the device has connected successfully or finished with error.
489+
errChan := make(chan error)
487490
go func() {
488491
for sig := range signal {
489492
switch sig.Name {
@@ -496,8 +499,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
496499
for k, v := range changes {
497500
if k == "Powered" && !v.Value().(bool) {
498501
// adapter is powered off, stop the scan
499-
err = errAdaptorNotPowered
500-
close(connectChan)
502+
errChan <- errAdaptorNotPowered
501503
}
502504
}
503505
case "org.bluez.Device1":
@@ -506,14 +508,15 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
506508
}
507509
changes := sig.Body[1].(map[string]dbus.Variant)
508510
if connected, ok := changes["Connected"].Value().(bool); ok && connected {
509-
close(connectChan)
511+
errChan <- nil
512+
close(errChan)
510513
}
511514
}
512515
}
513516
}
514517
}()
515-
<-connectChan
516518

519+
err := <-errChan
517520
if err != nil {
518521
return Device{}, err
519522
}
@@ -535,7 +538,11 @@ func (d Device) Disconnect() error {
535538

536539
// we don't call our cancel function here, instead we wait for the
537540
// property change in `watchForConnect` and cancel things then
538-
return d.device.Call("org.bluez.Device1.Disconnect", 0).Err
541+
if err := d.device.Call("org.bluez.Device1.Disconnect", 0).Err; err != nil {
542+
return fmt.Errorf("bluetooth: bluez failed to disconnect: %w", err)
543+
}
544+
545+
return nil
539546
}
540547

541548
// RequestConnectionParams requests a different connection latency and timeout

0 commit comments

Comments
 (0)