Compare commits

..

No commits in common. "5955e369e4eb378cd51b39e01225e2e03a0e0440" and "ec02bc7bcc2b1a53e0a69d255000d5d7eb76c460" have entirely different histories.

View File

@ -119,7 +119,6 @@ func (ps *Peripherals) Add(x Peripheral) bool {
}
//take ownership of this Objective-C object
x.p.Retain()
runtime.SetFinalizer(x.p, nil)
x.p.GC()
item := PeripheralListItem{p: x, seen: time.Now()}
ps.items = append(ps.items, item)
@ -165,8 +164,6 @@ type DiscoverCharacteristicEvent struct {
}
type UpdateValueEvent struct {
Peripheral Peripheral
Characteristic *ns.CBCharacteristic
Data []byte
}
@ -228,9 +225,6 @@ func (b *BLE) Scan() {
if b.state != (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
b.wantScan = true
} else {
b.peripherals.Lock()
b.peripherals.items = b.peripherals.items[:0]
b.peripherals.Unlock()
fmt.Printf("Go: Scanning\n")
b.cm.ScanForPeripheralsWithServices(nil, nil)
}
@ -247,41 +241,15 @@ func (b *BLE) StopScan() {
b.cm.StopScan()
}
func CancelConnection(p Peripheral) {
b := pdLookup[p.p.Ptr()]
if b == nil {
return
}
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"
}
}
func connectTracker(b *BLE, x ConnectionListItem) bool {
func connectTracker(b *BLE, x ConnectionListItem) {
fmt.Printf("connectTracker(): %s\n", x.p.Name)
b.connections.Lock()
for _, item := range b.connections.items {
if item.p.Identifier == x.p.Identifier {
fmt.Printf("connectTracker(): already connecting to %s\n", x.p.Name)
b.connections.Unlock()
return true
}
}
b.connections.items = append(b.connections.items, x)
b.connections.Unlock()
fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.p.Ptr())
b.cm.ConnectPeripheral(x.p.p, nil)
cancel := func() {
tick := time.NewTicker(time.Second * 5)
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 {
@ -290,34 +258,20 @@ func connectTracker(b *BLE, x ConnectionListItem) bool {
}
}
b.connections.Unlock()
}
go func() {
tick := time.NewTicker(time.Second * 5)
select {
case <-b.connections.close:
fmt.Printf("connectTracker(): Closing connection to %s\n", x.p.Name)
b.cm.CancelPeripheralConnection(x.p.p)
case <-tick.C:
fmt.Printf("connectTracker(): Connection to %s timed out\n", x.p.Name)
cancel()
b.events <- ConnectTimeoutEvent{x.p}
case state := <-x.state:
if state == "cancel" {
cancel()
fmt.Printf("connectTracker: state\n")
if state == "connected" {
fmt.Printf("--connected")
}
fmt.Printf("connectTracker(): state %s\n", state)
}
}()
return true
}
func (b *BLE) Connect(p Peripheral) bool {
func (b *BLE) Connect(p Peripheral) {
b.Lock()
if !b.ready {
b.Unlock()
fmt.Printf("--BLE not ready\n")
return false
return
}
b.Unlock()
if p.p == nil {
@ -325,37 +279,19 @@ func (b *BLE) Connect(p Peripheral) bool {
ps := b.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
if x := (int)(ps.Count()); x > 0 {
fmt.Printf("--found %d\n", x)
cbp := ps.ObjectAtIndex(0).CBPeripheral()
//NOTE: ns.ObjectAtIndex() calls SetFinalizer for us, which will be a problem
//later when we call GC(), so we first clear the finalizer here.
runtime.SetFinalizer(cbp, nil)
p = newPeripheral(cbp)
p.p = ps.ObjectAtIndex(0).CBPeripheral()
} else {
fmt.Printf("--none found\n")
return false
return
}
}
item := ConnectionListItem{p, make(chan string)}
fmt.Printf("BLE.Connect() calling connectTracker\n")
return connectTracker(b, item)
}
func Disconnect(p Peripheral) {
b := pdLookup[p.p.Ptr()]
found := false
b.connections.Lock()
for i, item := range b.connections.items {
if item.p.Identifier == p.Identifier {
found = true
b.connections.items = append(b.connections.items[:i], b.connections.items[i+1:]...)
break
}
}
b.connections.Unlock()
if !found {
return
}
b.cm.CancelPeripheralConnection(p.p)
b.connections.Add(item)
go connectTracker(b, item)
//time.Sleep(time.Second/10)
fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", p.p.Ptr())
b.cm.ConnectPeripheral(p.p, nil)
fmt.Printf("BLE.Connect() returned\n")
}
func updateState(c *ns.CBCentralManager) {
@ -367,13 +303,11 @@ func updateState(c *ns.CBCentralManager) {
} else {
if b.ready {
close(b.connections.close)
b.connections.Lock()
for _, item := range b.connections.items {
fmt.Printf("Closing connection to %s\n", item.p.Name)
b.cm.CancelPeripheralConnection(item.p.p)
}
b.connections.items = b.connections.items[:0]
b.connections.Unlock()
b.ready = false
}
}
@ -385,15 +319,11 @@ func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDict
b := cdLookup[c.Ptr()]
peripheral := newPeripheral(p)
peripheral.RSSI = (int)(rssi.IntValue())
_discoverPeripheral(b, peripheral)
}
func _discoverPeripheral(b *BLE, peripheral Peripheral) {
if peripheral.Name == "" {
return
}
if ok := b.peripherals.Add(peripheral); ok {
pdLookup[peripheral.p.Ptr()] = b
pdLookup[p.Ptr()] = b
b.events <- DiscoverPeripheralEvent{Peripheral: peripheral}
}
}
@ -499,8 +429,6 @@ func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
b := pdLookup[p.Ptr()]
v := chr.Value()
b.events <-UpdateValueEvent{
Peripheral: newPeripheral(p),
Characteristic: chr,
Data: C.GoBytes(v.Bytes(), (C.int)(v.Length())),
}
}