Add connection tracking and timeouts.
This commit is contained in:
parent
c91c83de9c
commit
64b684e419
115
ble_darwin.go
115
ble_darwin.go
|
@ -14,18 +14,17 @@ import (
|
|||
)
|
||||
|
||||
type BLE struct {
|
||||
events chan interface{}
|
||||
|
||||
state ns.CBManagerState
|
||||
|
||||
events chan interface{}
|
||||
peripherals Peripherals
|
||||
hr int
|
||||
cd *ns.CBDelegate
|
||||
cm *ns.CBCentralManager
|
||||
|
||||
ready, wantScan bool
|
||||
sync.Mutex
|
||||
|
||||
cd *ns.CBDelegate
|
||||
cm *ns.CBCentralManager
|
||||
connections Connections
|
||||
}
|
||||
|
||||
var cdLookup map[unsafe.Pointer]*BLE
|
||||
|
@ -68,6 +67,29 @@ type Peripherals struct {
|
|||
sync.Mutex
|
||||
}
|
||||
|
||||
type ConnectionListItem struct {
|
||||
p Peripheral
|
||||
state chan string
|
||||
}
|
||||
|
||||
type Connections struct {
|
||||
items []ConnectionListItem
|
||||
close chan struct{}
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func (c *Connections) UpdateState(p Peripheral, s string) {
|
||||
var ch chan string
|
||||
c.Lock()
|
||||
for _, item := range c.items {
|
||||
if p.Identifier == item.p.Identifier {
|
||||
ch = item.state
|
||||
}
|
||||
}
|
||||
c.Unlock()
|
||||
ch <- s
|
||||
}
|
||||
|
||||
func (ps *Peripherals) Add(x Peripheral) bool {
|
||||
ps.Lock()
|
||||
defer ps.Unlock()
|
||||
|
@ -92,6 +114,23 @@ func (ps *Peripherals) Add(x Peripheral) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (cs *Connections) Add(x ConnectionListItem) bool {
|
||||
cs.Lock()
|
||||
defer cs.Unlock()
|
||||
found := false
|
||||
for _, item := range cs.items {
|
||||
if item.p.Identifier == x.p.Identifier {
|
||||
// are we allowed to have multiple connections to the same peripheral?
|
||||
return false
|
||||
}
|
||||
}
|
||||
if found {
|
||||
return false
|
||||
}
|
||||
cs.items = append(cs.items, x)
|
||||
return true
|
||||
}
|
||||
|
||||
type UpdateStateEvent struct {
|
||||
State string
|
||||
}
|
||||
|
@ -103,6 +142,9 @@ type DiscoverEvent struct {
|
|||
type ConnectEvent struct {
|
||||
Peripheral Peripheral
|
||||
}
|
||||
type ConnectTimeoutEvent struct {
|
||||
Peripheral Peripheral
|
||||
}
|
||||
|
||||
func (b *BLE) Events() chan interface{} {
|
||||
return b.events
|
||||
|
@ -174,6 +216,31 @@ func (b *BLE) StopScan() {
|
|||
b.cm.StopScan()
|
||||
}
|
||||
|
||||
func connectTracker(b *BLE, x ConnectionListItem) {
|
||||
tick := time.NewTicker(time.Second * 30)
|
||||
select {
|
||||
case <-b.connections.close:
|
||||
fmt.Printf("Closing connection to %s\n", x.p.Name)
|
||||
b.cm.CancelPeripheralConnection(x.p.p)
|
||||
case <-tick.C:
|
||||
fmt.Printf("Connection to %s timed out\n", x.p.Name)
|
||||
b.cm.CancelPeripheralConnection(x.p.p)
|
||||
b.connections.Lock()
|
||||
for n, item := range b.connections.items {
|
||||
if item.p.Identifier == x.p.Identifier {
|
||||
b.connections.items = append(b.connections.items[:n], b.connections.items[n+1:]...)
|
||||
}
|
||||
}
|
||||
b.connections.Unlock()
|
||||
b.events <- ConnectTimeoutEvent{x.p}
|
||||
case state := <-x.state:
|
||||
fmt.Printf("connectTracker: state\n")
|
||||
if state == "connected" {
|
||||
fmt.Printf("--connected")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BLE) Connect(p Peripheral) {
|
||||
b.Lock()
|
||||
if !b.ready {
|
||||
|
@ -182,40 +249,56 @@ func (b *BLE) Connect(p Peripheral) {
|
|||
}
|
||||
b.Unlock()
|
||||
if p.p == nil {
|
||||
fmt.Printf("RetrievePeripheralsWithIdentifiers\n")
|
||||
ps := b.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
|
||||
if (int)(ps.Count()) > 0 {
|
||||
if x := (int)(ps.Count()); x > 0 {
|
||||
fmt.Printf("--found %d\n", x)
|
||||
p.p = ps.ObjectAtIndex(0).CBPeripheral()
|
||||
} else {
|
||||
fmt.Printf("--none found\n")
|
||||
return
|
||||
}
|
||||
}
|
||||
item := ConnectionListItem{p, make(chan string)}
|
||||
b.connections.Add(item)
|
||||
go connectTracker(b, item)
|
||||
//time.Sleep(time.Second/10)
|
||||
b.cm.ConnectPeripheral(p.p, nil)
|
||||
fmt.Printf("cm.ConnectPeripheral() returned\n")
|
||||
}
|
||||
|
||||
func updateState(c *ns.CBCentralManager) {
|
||||
ble := cdLookup[c.Ptr()]
|
||||
b := cdLookup[c.Ptr()]
|
||||
fmt.Printf("Go: did update state\n")
|
||||
st := c.CBManager.State()
|
||||
if st == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
|
||||
ble.ready = true
|
||||
b.ready = true
|
||||
b.connections.close = make(chan struct{})
|
||||
} else {
|
||||
ble.ready = false
|
||||
if b.ready {
|
||||
close(b.connections.close)
|
||||
for _, item := range b.connections.items {
|
||||
fmt.Printf("Closing connection to %s\n", item.p.Name)
|
||||
b.cm.CancelPeripheralConnection(item.p.p)
|
||||
}
|
||||
ble.setState(st)
|
||||
ble.events <- UpdateStateEvent{State: stringState(st)}
|
||||
b.connections.items = b.connections.items[:0]
|
||||
b.ready = false
|
||||
}
|
||||
}
|
||||
b.setState(st)
|
||||
b.events <- UpdateStateEvent{State: stringState(st)}
|
||||
}
|
||||
|
||||
func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
|
||||
ble := cdLookup[c.Ptr()]
|
||||
b := cdLookup[c.Ptr()]
|
||||
peripheral := newPeripheral(p)
|
||||
peripheral.RSSI = (int)(rssi.IntValue())
|
||||
if peripheral.Name == "" {
|
||||
return
|
||||
}
|
||||
if ok := ble.peripherals.Add(peripheral); ok {
|
||||
pdLookup[p.Ptr()] = ble
|
||||
ble.events <- DiscoverEvent{Peripheral: peripheral}
|
||||
if ok := b.peripherals.Add(peripheral); ok {
|
||||
pdLookup[p.Ptr()] = b
|
||||
b.events <- DiscoverEvent{Peripheral: peripheral}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -247,6 +330,7 @@ func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
|||
}
|
||||
}
|
||||
|
||||
b.connections.UpdateState(peripheral, "connected")
|
||||
b.events <- ConnectEvent{peripheral}
|
||||
fmt.Printf("Go: connectPeripheral returning\n")
|
||||
}
|
||||
|
@ -260,7 +344,6 @@ func DiscoverServices(x Peripheral) {
|
|||
p.DiscoverServices(nil)
|
||||
}
|
||||
|
||||
|
||||
func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
|
||||
fmt.Printf("Did discover services\n")
|
||||
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||
|
|
Loading…
Reference in New Issue
Block a user