2019-10-23 18:28:19 -04:00
|
|
|
package ble
|
|
|
|
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2019-10-24 08:59:25 -04:00
|
|
|
"unsafe"
|
2019-10-23 18:28:19 -04:00
|
|
|
|
2019-10-24 18:18:27 -04:00
|
|
|
"git.wow.st/gmp/ble/gatt"
|
2019-10-23 18:28:19 -04:00
|
|
|
"git.wow.st/gmp/ble/ns"
|
|
|
|
)
|
|
|
|
|
2019-10-28 11:21:14 -04:00
|
|
|
type bleState ns.CBManagerState
|
2019-10-23 18:28:19 -04:00
|
|
|
|
2019-10-28 11:21:14 -04:00
|
|
|
type bleHandle struct {
|
|
|
|
cd *ns.CBDelegate
|
|
|
|
cm *ns.CBCentralManager
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 17:00:54 -04:00
|
|
|
var cdLookup map[unsafe.Pointer]*BLE
|
|
|
|
var pdLookup map[unsafe.Pointer]*BLE
|
2019-10-24 18:18:27 -04:00
|
|
|
var pcache map[unsafe.Pointer]*Peripheral
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func peripheralLookup(p Peripheral) *BLE {
|
|
|
|
return pdLookup[p.p.Ptr()]
|
|
|
|
}
|
2019-10-23 18:28:19 -04:00
|
|
|
|
|
|
|
type State string
|
|
|
|
|
|
|
|
type Peripheral struct {
|
2019-10-24 17:00:54 -04:00
|
|
|
Name string
|
|
|
|
RSSI int
|
2019-10-24 08:59:25 -04:00
|
|
|
Identifier string
|
2019-10-23 18:28:19 -04:00
|
|
|
|
2019-10-24 17:00:54 -04:00
|
|
|
p *ns.CBPeripheral
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
type Service *ns.CBService
|
|
|
|
type Characteristic *ns.CBCharacteristic
|
|
|
|
|
|
|
|
//Init needs to be called before the BLE library can be used. No setup needed
|
|
|
|
//on Darwin.
|
|
|
|
func Init() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Peripheral) Retain() {
|
|
|
|
//NOTE: ns.ObjectAtIndex() calls SetFinalizer for us, which will be a problem
|
|
|
|
//later when we call GC(), so we always first clear the finalizer before
|
|
|
|
//setting a new one.
|
|
|
|
runtime.SetFinalizer(x.p, nil)
|
|
|
|
x.p.Retain()
|
|
|
|
x.p.GC()
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:59:25 -04:00
|
|
|
func peripheralName(p *ns.CBPeripheral) string {
|
|
|
|
var ret string
|
|
|
|
nsname := p.Name()
|
|
|
|
if nsname.Ptr() != nil {
|
|
|
|
ret = nsname.String()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPeripheral(x *ns.CBPeripheral) Peripheral {
|
2019-10-24 18:18:27 -04:00
|
|
|
if pcache == nil {
|
|
|
|
pcache = make(map[unsafe.Pointer]*Peripheral)
|
|
|
|
} else {
|
|
|
|
if ret,ok := pcache[x.Ptr()]; ok {
|
|
|
|
return *ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := Peripheral{
|
2019-10-24 17:00:54 -04:00
|
|
|
Name: peripheralName(x),
|
2019-10-24 08:59:25 -04:00
|
|
|
Identifier: x.Identifier().UUIDString().String(),
|
2019-10-24 17:00:54 -04:00
|
|
|
p: x,
|
2019-10-24 08:59:25 -04:00
|
|
|
}
|
2019-10-24 18:18:27 -04:00
|
|
|
pcache[x.Ptr()] = &ret
|
|
|
|
return ret
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func (b *BLE) stringState() string {
|
|
|
|
x := b.state
|
2019-10-28 11:21:14 -04:00
|
|
|
switch (ns.NSInteger)(x) {
|
|
|
|
case ns.CBManagerStateResetting:
|
2019-10-23 18:28:19 -04:00
|
|
|
return "resetting"
|
2019-10-28 11:21:14 -04:00
|
|
|
case ns.CBManagerStateUnsupported:
|
2019-10-23 18:28:19 -04:00
|
|
|
return "unsupported"
|
2019-10-28 11:21:14 -04:00
|
|
|
case ns.CBManagerStateUnauthorized:
|
2019-10-23 18:28:19 -04:00
|
|
|
return "unauthorized"
|
2019-10-28 11:21:14 -04:00
|
|
|
case ns.CBManagerStatePoweredOff:
|
2019-10-23 18:28:19 -04:00
|
|
|
return "powered off"
|
2019-10-28 11:21:14 -04:00
|
|
|
case ns.CBManagerStatePoweredOn:
|
2019-10-23 18:28:19 -04:00
|
|
|
return "powered on"
|
2019-10-28 11:21:14 -04:00
|
|
|
case ns.CBManagerStateUnknown:
|
2019-10-23 18:28:19 -04:00
|
|
|
return "unknown"
|
|
|
|
default:
|
|
|
|
return "no state"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func (b *BLE) readyToScan() bool {
|
2019-10-28 11:21:14 -04:00
|
|
|
return b.state == (bleState)(ns.CBManagerStatePoweredOn)
|
2019-10-28 09:25:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BLE) scan() {
|
2019-10-28 11:21:14 -04:00
|
|
|
b.handle.cm.ScanForPeripheralsWithServices(nil, nil)
|
2019-10-28 09:25:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BLE) stopScan() {
|
2019-10-28 11:21:14 -04:00
|
|
|
b.handle.cm.StopScan()
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BLE) setState(x ns.CBManagerState) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
2019-10-28 11:21:14 -04:00
|
|
|
b.state = (bleState)(x)
|
2019-10-23 19:28:43 -04:00
|
|
|
if b.ready && b.wantScan {
|
2019-10-25 09:09:54 -04:00
|
|
|
fmt.Printf("Go: Scanning\n")
|
2019-10-28 09:25:23 -04:00
|
|
|
b.scan()
|
2019-10-25 09:09:54 -04:00
|
|
|
b.wantScan = false
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func (b *BLE) connectPeripheral(x Peripheral) {
|
|
|
|
fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.Ptr())
|
2019-10-28 11:21:14 -04:00
|
|
|
b.handle.cm.ConnectPeripheral(x.p, nil)
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-25 14:03:20 -04:00
|
|
|
func CancelConnection(p Peripheral) {
|
2019-10-28 09:25:23 -04:00
|
|
|
b := peripheralLookup(p)
|
2019-10-25 14:59:13 -04:00
|
|
|
if b == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-10-25 14:03:20 -04:00
|
|
|
b.connections.Lock()
|
|
|
|
var ch chan string;
|
|
|
|
for _, item := range b.connections.items {
|
|
|
|
if item.p.Identifier == p.Identifier {
|
|
|
|
ch = item.state
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.connections.Unlock()
|
|
|
|
if ch != nil {
|
|
|
|
ch <- "cancel"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func (b *BLE) cancelConnection(p Peripheral) {
|
2019-10-28 11:21:14 -04:00
|
|
|
b.handle.cm.CancelPeripheralConnection(p.p)
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func (b *BLE) knownPeripheral(p Peripheral) (Peripheral, bool) {
|
|
|
|
fmt.Printf("RetrievePeripheralsWithIdentifiers\n")
|
2019-10-28 11:21:14 -04:00
|
|
|
ps := b.handle.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
|
2019-10-28 09:25:23 -04:00
|
|
|
if x := (int)(ps.Count()); x > 0 {
|
|
|
|
fmt.Printf("--found %d\n", x)
|
|
|
|
cbp := ps.ObjectAtIndex(0).CBPeripheral()
|
|
|
|
return newPeripheral(cbp), true
|
|
|
|
} else {
|
|
|
|
fmt.Printf("--none found\n")
|
|
|
|
return Peripheral{}, false
|
2019-10-25 14:03:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func didUpdateState(c *ns.CBCentralManager) {
|
2019-10-24 17:00:54 -04:00
|
|
|
b := cdLookup[c.Ptr()]
|
2019-10-24 08:59:25 -04:00
|
|
|
st := c.CBManager.State()
|
2019-10-23 19:28:43 -04:00
|
|
|
if st == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
|
2019-10-24 17:00:54 -04:00
|
|
|
b.ready = true
|
|
|
|
b.connections.close = make(chan struct{})
|
2019-10-23 19:28:43 -04:00
|
|
|
} else {
|
2019-10-24 17:00:54 -04:00
|
|
|
if b.ready {
|
|
|
|
close(b.connections.close)
|
2019-10-25 14:03:20 -04:00
|
|
|
b.connections.Lock()
|
2019-10-24 17:00:54 -04:00
|
|
|
for _, item := range b.connections.items {
|
|
|
|
fmt.Printf("Closing connection to %s\n", item.p.Name)
|
2019-10-28 11:21:14 -04:00
|
|
|
b.handle.cm.CancelPeripheralConnection(item.p.p)
|
2019-10-24 17:00:54 -04:00
|
|
|
}
|
|
|
|
b.connections.items = b.connections.items[:0]
|
2019-10-25 14:03:20 -04:00
|
|
|
b.connections.Unlock()
|
2019-10-24 17:00:54 -04:00
|
|
|
b.ready = false
|
|
|
|
}
|
2019-10-23 19:28:43 -04:00
|
|
|
}
|
2019-10-24 17:00:54 -04:00
|
|
|
b.setState(st)
|
2019-10-28 09:25:23 -04:00
|
|
|
b.events <- UpdateStateEvent{State: b.stringState()}
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func didDiscoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
|
2019-10-24 17:00:54 -04:00
|
|
|
b := cdLookup[c.Ptr()]
|
2019-10-24 08:59:25 -04:00
|
|
|
peripheral := newPeripheral(p)
|
|
|
|
peripheral.RSSI = (int)(rssi.IntValue())
|
2019-10-28 09:25:23 -04:00
|
|
|
_didDiscoverPeripheral(b, peripheral)
|
2019-10-25 13:30:11 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func _didDiscoverPeripheral(b *BLE, peripheral Peripheral) {
|
2019-10-23 18:28:19 -04:00
|
|
|
if peripheral.Name == "" {
|
|
|
|
return
|
|
|
|
}
|
2019-10-24 17:00:54 -04:00
|
|
|
if ok := b.peripherals.Add(peripheral); ok {
|
2019-10-25 13:30:11 -04:00
|
|
|
pdLookup[peripheral.p.Ptr()] = b
|
2019-10-24 18:18:27 -04:00
|
|
|
b.events <- DiscoverPeripheralEvent{Peripheral: peripheral}
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func didConnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
2019-10-23 18:28:19 -04:00
|
|
|
fmt.Printf("Did connect peripheral\n")
|
2019-10-24 08:59:25 -04:00
|
|
|
b := cdLookup[c.Ptr()]
|
2019-10-23 18:28:19 -04:00
|
|
|
|
|
|
|
// set ourselves up as a peripheral delegate
|
2019-10-28 11:21:14 -04:00
|
|
|
p.SetDelegate(b.handle.cd)
|
2019-10-23 18:28:19 -04:00
|
|
|
|
2019-10-24 08:59:25 -04:00
|
|
|
uuidstring := p.Identifier().UUIDString().String()
|
|
|
|
peripheral := newPeripheral(p)
|
2019-10-23 18:28:19 -04:00
|
|
|
found := false
|
2019-10-23 19:28:43 -04:00
|
|
|
|
2019-10-24 08:59:25 -04:00
|
|
|
b.peripherals.Lock()
|
|
|
|
for _, item := range b.peripherals.items {
|
|
|
|
if item.p.Identifier == uuidstring {
|
2019-10-23 18:28:19 -04:00
|
|
|
peripheral = item.p
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 08:59:25 -04:00
|
|
|
b.peripherals.Unlock()
|
2019-10-23 19:28:43 -04:00
|
|
|
|
2019-10-23 18:28:19 -04:00
|
|
|
if !found {
|
|
|
|
peripheral.Name = peripheralName(p)
|
2019-10-24 08:59:25 -04:00
|
|
|
if ok := b.peripherals.Add(peripheral); ok {
|
|
|
|
pdLookup[p.Ptr()] = b
|
|
|
|
}
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
2019-10-23 19:28:43 -04:00
|
|
|
|
2019-10-24 17:00:54 -04:00
|
|
|
b.connections.UpdateState(peripheral, "connected")
|
2019-10-24 08:59:25 -04:00
|
|
|
b.events <- ConnectEvent{peripheral}
|
2019-10-28 09:25:23 -04:00
|
|
|
fmt.Printf("Go: didConnectPeripheral returning\n")
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 18:18:27 -04:00
|
|
|
func (x Peripheral) DiscoverServices() {
|
2019-10-23 19:28:43 -04:00
|
|
|
fmt.Printf("Discovering services on %s\n", x.Name)
|
|
|
|
p := x.p
|
|
|
|
|
|
|
|
// discover all services on this device
|
|
|
|
|
|
|
|
p.DiscoverServices(nil)
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func didDiscoverServices(p *ns.CBPeripheral, e *ns.NSError) {
|
2019-10-24 18:18:27 -04:00
|
|
|
b := pdLookup[p.Ptr()]
|
2019-10-23 18:28:19 -04:00
|
|
|
fmt.Printf("Did discover services\n")
|
|
|
|
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
|
|
|
serv := o.CBService()
|
2019-10-24 18:18:27 -04:00
|
|
|
uuid := serv.UUID().UUIDString().String()
|
|
|
|
b.events <-DiscoverServiceEvent{
|
|
|
|
Peripheral: newPeripheral(p),
|
|
|
|
Gatt: gatt.Service{uuid},
|
|
|
|
Service: serv,
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-10-28 09:25:23 -04:00
|
|
|
fmt.Printf("Go: didDiscoverServices returning\n")
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func hr(d *ns.NSData) int {
|
|
|
|
if l := int(d.Length()); l < 4 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
x := C.GoBytes(d.Bytes(), 4)
|
|
|
|
flags := x[0]
|
|
|
|
if flags&0x80 != 0 { // uint16 format
|
|
|
|
return int(binary.BigEndian.Uint16(x[1:2]))
|
|
|
|
} else {
|
|
|
|
return int(x[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:18:27 -04:00
|
|
|
func (p Peripheral) DiscoverCharacteristics(serv *ns.CBService) {
|
|
|
|
p.p.DiscoverCharacteristics(nil, serv)
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func didDiscoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
|
2019-10-24 18:18:27 -04:00
|
|
|
b := pdLookup[p.Ptr()]
|
2019-10-23 18:28:19 -04:00
|
|
|
fmt.Printf("Did discover characteristics\n")
|
2019-10-24 18:18:27 -04:00
|
|
|
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
|
|
|
chr := o.CBCharacteristic()
|
|
|
|
chuuid := chr.UUID()
|
|
|
|
fmt.Printf("------%s\n", chuuid.UUIDString())
|
|
|
|
b.events <-DiscoverCharacteristicEvent{
|
|
|
|
Peripheral: newPeripheral(p),
|
|
|
|
Service: s,
|
|
|
|
Characteristic: chr,
|
|
|
|
Gatt: gatt.Characteristic{chuuid.UUIDString().String()},
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-10-28 09:25:23 -04:00
|
|
|
fmt.Printf("Go: didDiscoverCharacteristics returning\n")
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 18:18:27 -04:00
|
|
|
func (p Peripheral) SetNotifyValue(c *ns.CBCharacteristic) {
|
|
|
|
p.p.SetNotifyValue(1, c)
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
func didUpdateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
|
2019-10-24 18:30:02 -04:00
|
|
|
b := pdLookup[p.Ptr()]
|
|
|
|
v := chr.Value()
|
|
|
|
b.events <-UpdateValueEvent{
|
2019-10-25 14:59:13 -04:00
|
|
|
Peripheral: newPeripheral(p),
|
|
|
|
Characteristic: chr,
|
2019-10-24 18:30:02 -04:00
|
|
|
Data: C.GoBytes(v.Bytes(), (C.int)(v.Length())),
|
2019-10-23 18:28:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBLE() *BLE {
|
2019-10-24 17:00:54 -04:00
|
|
|
ps := Peripherals{items: make([]PeripheralListItem, 0)}
|
2019-10-24 08:59:25 -04:00
|
|
|
ble := &BLE{events: make(chan interface{}), peripherals: ps}
|
2019-10-23 18:28:19 -04:00
|
|
|
|
|
|
|
queue := ns.DispatchQueueCreate(ns.CharWithGoString("go_hrm_queue"), nil)
|
|
|
|
|
2019-10-24 08:59:25 -04:00
|
|
|
cd := ns.CBDelegateAlloc()
|
2019-10-23 18:28:19 -04:00
|
|
|
|
2019-10-28 09:25:23 -04:00
|
|
|
cd.CentralManagerDidUpdateStateCallback(didUpdateState)
|
|
|
|
cd.CentralManagerDidDiscoverPeripheralCallback(didDiscoverPeripheral)
|
|
|
|
cd.CentralManagerDidConnectPeripheralCallback(didConnectPeripheral)
|
|
|
|
cd.PeripheralDidDiscoverServicesCallback(didDiscoverServices)
|
|
|
|
cd.PeripheralDidDiscoverCharacteristicsForServiceCallback(didDiscoverCharacteristics)
|
|
|
|
cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue)
|
2019-10-23 18:28:19 -04:00
|
|
|
|
|
|
|
|
2019-10-28 11:21:14 -04:00
|
|
|
ble.handle.cd = cd
|
2019-10-24 08:59:25 -04:00
|
|
|
if cdLookup == nil {
|
2019-10-24 17:00:54 -04:00
|
|
|
cdLookup = make(map[unsafe.Pointer]*BLE, 0)
|
2019-10-24 08:59:25 -04:00
|
|
|
}
|
|
|
|
if pdLookup == nil {
|
2019-10-24 17:00:54 -04:00
|
|
|
pdLookup = make(map[unsafe.Pointer]*BLE, 0)
|
2019-10-24 08:59:25 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:28:19 -04:00
|
|
|
// We defined our own queue because this won't work on the main queue.
|
2019-10-28 11:21:14 -04:00
|
|
|
ble.handle.cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue)
|
|
|
|
cdLookup[ble.handle.cm.Ptr()] = ble
|
2019-10-23 18:28:19 -04:00
|
|
|
|
|
|
|
// For debugging purposes, run GC every second to make sure things are
|
|
|
|
// not over-released.
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
runtime.GC()
|
|
|
|
time.Sleep(time.Second * 30)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ble
|
|
|
|
}
|