From 4557b5d70cc04f25e7f8a533e583c73db0fc4472 Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Tue, 2 Apr 2024 21:13:31 -0700 Subject: [PATCH 1/2] Fix goroutine leaks --- gap_linux.go | 1 + gattc_linux.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gap_linux.go b/gap_linux.go index 2e4ecc3a..ae32d01c 100644 --- a/gap_linux.go +++ b/gap_linux.go @@ -345,6 +345,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err // were connected between the two calls the signal wouldn't be picked up. signal := make(chan *dbus.Signal) a.bus.Signal(signal) + defer close(signal) defer a.bus.RemoveSignal(signal) propertiesChangedMatchOptions := []dbus.MatchOption{dbus.WithMatchInterface("org.freedesktop.DBus.Properties")} a.bus.AddMatchSignal(propertiesChangedMatchOptions...) diff --git a/gattc_linux.go b/gattc_linux.go index 8ffbab35..eb39877f 100644 --- a/gattc_linux.go +++ b/gattc_linux.go @@ -4,6 +4,7 @@ package bluetooth import ( "errors" + "path" "sort" "strings" "time" @@ -242,6 +243,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err return errDupNotif } + // Figure out the path of the device that this characteristic belongs to + devicePath := dbus.ObjectPath(path.Dir(path.Dir(string(c.characteristic.Path())))) + // Start watching for changes in the Value property. c.property = make(chan *dbus.Signal) c.adapter.bus.Signal(c.property) @@ -257,12 +261,22 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err for sig := range c.property { if sig.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" { interfaceName := sig.Body[0].(string) - if interfaceName != "org.bluez.GattCharacteristic1" { + + if interfaceName == "org.bluez.Device1" && sig.Path == devicePath { + changes := sig.Body[1].(map[string]dbus.Variant) + + if connected, ok := changes["Connected"].Value().(bool); ok && !connected { + c.EnableNotifications(nil) + return + } + } else if interfaceName != "org.bluez.GattCharacteristic1" { continue } + if sig.Path != c.characteristic.Path() { continue } + changes := sig.Body[1].(map[string]dbus.Variant) if value, ok := changes["Value"].Value().([]byte); ok { callback(value) @@ -280,6 +294,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err err := c.adapter.bus.RemoveMatchSignal(c.propertiesChangedMatchOption) c.adapter.bus.RemoveSignal(c.property) + close(c.property) c.property = nil return err } From 4f54e74b090cdc58f86b6fcd30161fd2b67f062e Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Sat, 27 Apr 2024 13:32:48 -0700 Subject: [PATCH 2/2] Change if statement to switch statement --- gattc_linux.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/gattc_linux.go b/gattc_linux.go index eb39877f..e5a51c2c 100644 --- a/gattc_linux.go +++ b/gattc_linux.go @@ -262,18 +262,17 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err if sig.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" { interfaceName := sig.Body[0].(string) - if interfaceName == "org.bluez.Device1" && sig.Path == devicePath { + switch { + case interfaceName == "org.bluez.Device1" && sig.Path == devicePath: changes := sig.Body[1].(map[string]dbus.Variant) if connected, ok := changes["Connected"].Value().(bool); ok && !connected { c.EnableNotifications(nil) return } - } else if interfaceName != "org.bluez.GattCharacteristic1" { + case interfaceName != "org.bluez.GattCharacteristic1": continue - } - - if sig.Path != c.characteristic.Path() { + case sig.Path != c.characteristic.Path(): continue }