Skip to content

Commit

Permalink
ninafw: should support muliple connections as a central
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram authored and bgould committed Jan 24, 2024
1 parent 00a475a commit 0a9bffe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions adapter_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (a *Adapter) startNotifications() {
println("notification received", not.connectionHandle, not.handle, not.data)
}

d := a.findDevice(not.connectionHandle)
d := a.findConnectedDevice(not.connectionHandle)
if d.deviceInternal == nil {
if debug {
println("no device found for handle", not.connectionHandle)
Expand Down Expand Up @@ -212,7 +212,23 @@ func (a *Adapter) startNotifications() {
}()
}

func (a *Adapter) findDevice(handle uint16) Device {
func (a *Adapter) addDevice(d Device) {
a.connectedDevices = append(a.connectedDevices, d)
}

func (a *Adapter) removeDevice(d Device) {
for i := range a.connectedDevices {
if d.handle == a.connectedDevices[i].handle {
copy(a.connectedDevices[i:], a.connectedDevices[i+1:])
a.connectedDevices[len(a.connectedDevices)-1] = Device{} // the zero value of T
a.connectedDevices = a.connectedDevices[:len(a.connectedDevices)-1]

return
}
}
}

func (a *Adapter) findConnectedDevice(handle uint16) Device {
for _, d := range a.connectedDevices {
if d.handle == handle {
if debug {
Expand Down
4 changes: 2 additions & 2 deletions gap_ninafw.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
notificationRegistrations: make([]notificationRegistration, 0),
},
}
a.connectedDevices = append(a.connectedDevices, d)
a.addDevice(d)

return d, nil

Expand Down Expand Up @@ -228,7 +228,7 @@ func (d Device) Disconnect() error {
return err
}

d.adapter.connectedDevices = []Device{}
d.adapter.removeDevice(d)
return nil
}

Expand Down

0 comments on commit 0a9bffe

Please sign in to comment.