diff --git a/adapter_ninafw.go b/adapter_ninafw.go index 483501a0..0834b35d 100644 --- a/adapter_ninafw.go +++ b/adapter_ninafw.go @@ -164,24 +164,29 @@ func (a *Adapter) startNotifications() { println("notification received", not.connectionHandle, not.handle, not.data) } - if d := a.findDevice(not.connectionHandle); d != nil { - if n := d.findNotification(not.handle); n != nil { - if n.callback != nil { - n.callback(not.data) - } - } else { - if _debug { - println("no notification registered for handle", not.handle) - } - } - } else { + d := a.findDevice(not.connectionHandle) + if d == nil { if _debug { println("no device found for handle", not.connectionHandle) } + continue + } + + n := d.findNotificationRegistration(not.handle) + if n == nil { + if _debug { + println("no notification registered for handle", not.handle) + } + continue + } + + if n.callback != nil { + n.callback(not.data) } default: } + runtime.Gosched() } }() @@ -191,7 +196,7 @@ func (a *Adapter) findDevice(handle uint16) *Device { for _, d := range a.connectedDevices { if d.handle == handle { if _debug { - println("found device", handle, d.Address.String(), "with notifications registered", len(d.notifications)) + println("found device", handle, d.Address.String(), "with notifications registered", len(d.notificationRegistrations)) } return d diff --git a/gap_ninafw.go b/gap_ninafw.go index 914aa740..9b3dce2b 100644 --- a/gap_ninafw.go +++ b/gap_ninafw.go @@ -152,7 +152,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er handle: a.hci.connectData.handle, Address: Address{MACAddress{MAC: makeAddress(a.hci.connectData.peerBdaddr)}, a.hci.connectData.peerBdaddrType}, - notifications: make([]deviceNotification, 0), + notificationRegistrations: make([]notificationRegistration, 0), } a.connectedDevices = append(a.connectedDevices, d) @@ -176,7 +176,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er return nil, ErrConnect } -type deviceNotification struct { +type notificationRegistration struct { handle uint16 callback func([]byte) } @@ -187,7 +187,7 @@ type Device struct { Address Address handle uint16 - notifications []deviceNotification + notificationRegistrations []notificationRegistration } // Disconnect from the BLE device. @@ -203,8 +203,8 @@ func (d *Device) Disconnect() error { return nil } -func (d *Device) findNotification(handle uint16) *deviceNotification { - for _, n := range d.notifications { +func (d *Device) findNotificationRegistration(handle uint16) *notificationRegistration { + for _, n := range d.notificationRegistrations { if n.handle == handle { return &n } @@ -212,3 +212,15 @@ func (d *Device) findNotification(handle uint16) *deviceNotification { return nil } + +func (d *Device) addNotificationRegistration(handle uint16, callback func([]byte)) { + d.notificationRegistrations = append(d.notificationRegistrations, + notificationRegistration{ + handle: handle, + callback: callback, + }) +} + +func (d *Device) startNotifications() { + d.adapter.startNotifications() +} diff --git a/gattc_ninafw.go b/gattc_ninafw.go index daf742dd..8a85f9a4 100644 --- a/gattc_ninafw.go +++ b/gattc_ninafw.go @@ -96,8 +96,9 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { d.adapter.att.services = []rawService{} } - // put into correct order if needed - if len(uuids) > 0 { + switch { + case len(uuids) > 0: + // put into correct order for _, uuid := range uuids { s, ok := foundServices[uuid] if !ok { @@ -106,7 +107,7 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { services = append(services, s) } - } else { + default: for _, s := range foundServices { services = append(services, s) } @@ -212,8 +213,9 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter s.device.adapter.att.characteristics = []rawCharacteristic{} } - // put into correct order if needed - if len(uuids) > 0 { + switch { + case len(uuids) > 0: + // put into correct order for _, uuid := range uuids { c, ok := foundCharacteristics[uuid] if !ok { @@ -221,10 +223,11 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter } characteristics = append(characteristics, c) } - } else { + default: for _, c := range foundCharacteristics { characteristics = append(characteristics, c) } + } return characteristics, nil @@ -258,7 +261,8 @@ func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) er return errNoNotify } - if callback == nil { + switch { + case callback == nil: // disable notifications if _debug { println("disabling notifications") @@ -268,7 +272,7 @@ func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) er if err != nil { return err } - } else { + default: // enable notifications if _debug { println("enabling notifications") @@ -280,14 +284,10 @@ func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) er } } - c.service.device.adapter.startNotifications() - c.callback = callback - c.service.device.notifications = append(c.service.device.notifications, - deviceNotification{ - handle: c.handle, - callback: callback, - }) + + c.service.device.startNotifications() + c.service.device.addNotificationRegistration(c.handle, c.callback) return nil }