Move BLE type into ble.go.

This commit is contained in:
Greg 2019-10-28 11:21:14 -04:00
parent 2d4f106dd7
commit c911bbb547
2 changed files with 35 additions and 30 deletions

12
ble.go
View File

@ -8,6 +8,18 @@ import (
"git.wow.st/gmp/ble/gatt" "git.wow.st/gmp/ble/gatt"
) )
type BLE struct {
state bleState
events chan interface{}
peripherals Peripherals
handle bleHandle
ready, wantScan bool
sync.Mutex
connections Connections
}
type PeripheralListItem struct { type PeripheralListItem struct {
p Peripheral p Peripheral
seen time.Time seen time.Time

View File

@ -6,7 +6,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"runtime" "runtime"
"sync"
"time" "time"
"unsafe" "unsafe"
@ -14,17 +13,11 @@ import (
"git.wow.st/gmp/ble/ns" "git.wow.st/gmp/ble/ns"
) )
type BLE struct { type bleState ns.CBManagerState
state ns.CBManagerState
events chan interface{}
peripherals Peripherals
cd *ns.CBDelegate
cm *ns.CBCentralManager
ready, wantScan bool type bleHandle struct {
sync.Mutex cd *ns.CBDelegate
cm *ns.CBCentralManager
connections Connections
} }
var cdLookup map[unsafe.Pointer]*BLE var cdLookup map[unsafe.Pointer]*BLE
@ -91,18 +84,18 @@ func newPeripheral(x *ns.CBPeripheral) Peripheral {
func (b *BLE) stringState() string { func (b *BLE) stringState() string {
x := b.state x := b.state
switch x { switch (ns.NSInteger)(x) {
case (ns.CBManagerState)(ns.CBManagerStateResetting): case ns.CBManagerStateResetting:
return "resetting" return "resetting"
case (ns.CBManagerState)(ns.CBManagerStateUnsupported): case ns.CBManagerStateUnsupported:
return "unsupported" return "unsupported"
case (ns.CBManagerState)(ns.CBManagerStateUnauthorized): case ns.CBManagerStateUnauthorized:
return "unauthorized" return "unauthorized"
case (ns.CBManagerState)(ns.CBManagerStatePoweredOff): case ns.CBManagerStatePoweredOff:
return "powered off" return "powered off"
case (ns.CBManagerState)(ns.CBManagerStatePoweredOn): case ns.CBManagerStatePoweredOn:
return "powered on" return "powered on"
case (ns.CBManagerState)(ns.CBManagerStateUnknown): case ns.CBManagerStateUnknown:
return "unknown" return "unknown"
default: default:
return "no state" return "no state"
@ -110,21 +103,21 @@ func (b *BLE) stringState() string {
} }
func (b *BLE) readyToScan() bool { func (b *BLE) readyToScan() bool {
return b.state == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) return b.state == (bleState)(ns.CBManagerStatePoweredOn)
} }
func (b *BLE) scan() { func (b *BLE) scan() {
b.cm.ScanForPeripheralsWithServices(nil, nil) b.handle.cm.ScanForPeripheralsWithServices(nil, nil)
} }
func (b *BLE) stopScan() { func (b *BLE) stopScan() {
b.cm.StopScan() b.handle.cm.StopScan()
} }
func (b *BLE) setState(x ns.CBManagerState) { func (b *BLE) setState(x ns.CBManagerState) {
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
b.state = x b.state = (bleState)(x)
if b.ready && b.wantScan { if b.ready && b.wantScan {
fmt.Printf("Go: Scanning\n") fmt.Printf("Go: Scanning\n")
b.scan() b.scan()
@ -134,7 +127,7 @@ func (b *BLE) setState(x ns.CBManagerState) {
func (b *BLE) connectPeripheral(x Peripheral) { func (b *BLE) connectPeripheral(x Peripheral) {
fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.Ptr()) fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.Ptr())
b.cm.ConnectPeripheral(x.p, nil) b.handle.cm.ConnectPeripheral(x.p, nil)
} }
func CancelConnection(p Peripheral) { func CancelConnection(p Peripheral) {
@ -157,12 +150,12 @@ func CancelConnection(p Peripheral) {
} }
func (b *BLE) cancelConnection(p Peripheral) { func (b *BLE) cancelConnection(p Peripheral) {
b.cm.CancelPeripheralConnection(p.p) b.handle.cm.CancelPeripheralConnection(p.p)
} }
func (b *BLE) knownPeripheral(p Peripheral) (Peripheral, bool) { func (b *BLE) knownPeripheral(p Peripheral) (Peripheral, bool) {
fmt.Printf("RetrievePeripheralsWithIdentifiers\n") fmt.Printf("RetrievePeripheralsWithIdentifiers\n")
ps := b.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier)))) ps := b.handle.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
if x := (int)(ps.Count()); x > 0 { if x := (int)(ps.Count()); x > 0 {
fmt.Printf("--found %d\n", x) fmt.Printf("--found %d\n", x)
cbp := ps.ObjectAtIndex(0).CBPeripheral() cbp := ps.ObjectAtIndex(0).CBPeripheral()
@ -185,7 +178,7 @@ func didUpdateState(c *ns.CBCentralManager) {
b.connections.Lock() b.connections.Lock()
for _, item := range b.connections.items { for _, item := range b.connections.items {
fmt.Printf("Closing connection to %s\n", item.p.Name) fmt.Printf("Closing connection to %s\n", item.p.Name)
b.cm.CancelPeripheralConnection(item.p.p) b.handle.cm.CancelPeripheralConnection(item.p.p)
} }
b.connections.items = b.connections.items[:0] b.connections.items = b.connections.items[:0]
b.connections.Unlock() b.connections.Unlock()
@ -218,7 +211,7 @@ func didConnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
b := cdLookup[c.Ptr()] b := cdLookup[c.Ptr()]
// set ourselves up as a peripheral delegate // set ourselves up as a peripheral delegate
p.SetDelegate(b.cd) p.SetDelegate(b.handle.cd)
uuidstring := p.Identifier().UUIDString().String() uuidstring := p.Identifier().UUIDString().String()
peripheral := newPeripheral(p) peripheral := newPeripheral(p)
@ -336,7 +329,7 @@ func NewBLE() *BLE {
cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue) cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue)
ble.cd = cd ble.handle.cd = cd
if cdLookup == nil { if cdLookup == nil {
cdLookup = make(map[unsafe.Pointer]*BLE, 0) cdLookup = make(map[unsafe.Pointer]*BLE, 0)
} }
@ -345,8 +338,8 @@ func NewBLE() *BLE {
} }
// We defined our own queue because this won't work on the main queue. // We defined our own queue because this won't work on the main queue.
ble.cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue) ble.handle.cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue)
cdLookup[ble.cm.Ptr()] = ble cdLookup[ble.handle.cm.Ptr()] = ble
// For debugging purposes, run GC every second to make sure things are // For debugging purposes, run GC every second to make sure things are
// not over-released. // not over-released.