From 0a9bffe3978d9e01e5e866f1f19f57f07b133cf1 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 19 Jan 2024 00:33:07 +0100 Subject: [PATCH] ninafw: should support muliple connections as a central Signed-off-by: deadprogram --- adapter_ninafw.go | 20 ++++++++++++++++++-- gap_ninafw.go | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/adapter_ninafw.go b/adapter_ninafw.go index 688e568..43eebe3 100644 --- a/adapter_ninafw.go +++ b/adapter_ninafw.go @@ -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) @@ -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 { diff --git a/gap_ninafw.go b/gap_ninafw.go index a964eaa..488a07c 100644 --- a/gap_ninafw.go +++ b/gap_ninafw.go @@ -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 @@ -228,7 +228,7 @@ func (d Device) Disconnect() error { return err } - d.adapter.connectedDevices = []Device{} + d.adapter.removeDevice(d) return nil }