Skip to content

Commit

Permalink
ninafw: some refactoring and other cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jan 3, 2024
1 parent 02f1168 commit ecc76a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
29 changes: 17 additions & 12 deletions adapter_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}()
Expand All @@ -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
Expand Down
22 changes: 17 additions & 5 deletions gap_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
Expand All @@ -187,7 +187,7 @@ type Device struct {
Address Address
handle uint16

notifications []deviceNotification
notificationRegistrations []notificationRegistration
}

// Disconnect from the BLE device.
Expand All @@ -203,12 +203,24 @@ 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
}
}

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()
}
30 changes: 15 additions & 15 deletions gattc_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -212,19 +213,21 @@ 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 {
return nil, errCharacteristicNotFound
}
characteristics = append(characteristics, c)
}
} else {
default:
for _, c := range foundCharacteristics {
characteristics = append(characteristics, c)
}

}

return characteristics, nil
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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
}
Expand Down

0 comments on commit ecc76a8

Please sign in to comment.