Add gatt package. Add DiscoverServiceEvent and DiscoverCharacteristic

event.
This commit is contained in:
Greg 2019-10-24 18:18:27 -04:00
parent 64b684e419
commit 822b3529c0
2 changed files with 78 additions and 29 deletions

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"unsafe" "unsafe"
"git.wow.st/gmp/ble/gatt"
"git.wow.st/gmp/ble/ns" "git.wow.st/gmp/ble/ns"
) )
@ -29,6 +30,8 @@ type BLE struct {
var cdLookup map[unsafe.Pointer]*BLE var cdLookup map[unsafe.Pointer]*BLE
var pdLookup map[unsafe.Pointer]*BLE var pdLookup map[unsafe.Pointer]*BLE
var pcache map[unsafe.Pointer]*Peripheral
type State string type State string
@ -50,11 +53,20 @@ func peripheralName(p *ns.CBPeripheral) string {
} }
func newPeripheral(x *ns.CBPeripheral) Peripheral { func newPeripheral(x *ns.CBPeripheral) Peripheral {
return Peripheral{ if pcache == nil {
pcache = make(map[unsafe.Pointer]*Peripheral)
} else {
if ret,ok := pcache[x.Ptr()]; ok {
return *ret
}
}
ret := Peripheral{
Name: peripheralName(x), Name: peripheralName(x),
Identifier: x.Identifier().UUIDString().String(), Identifier: x.Identifier().UUIDString().String(),
p: x, p: x,
} }
pcache[x.Ptr()] = &ret
return ret
} }
type PeripheralListItem struct { type PeripheralListItem struct {
@ -135,10 +147,23 @@ type UpdateStateEvent struct {
State string State string
} }
type DiscoverEvent struct { type DiscoverPeripheralEvent struct {
Peripheral Peripheral Peripheral Peripheral
} }
type DiscoverServiceEvent struct {
Peripheral Peripheral
Gatt gatt.Service
Service *ns.CBService
}
type DiscoverCharacteristicEvent struct {
Peripheral Peripheral
Gatt gatt.Characteristic
Service *ns.CBService
Characteristic *ns.CBCharacteristic
}
type ConnectEvent struct { type ConnectEvent struct {
Peripheral Peripheral Peripheral Peripheral
} }
@ -146,6 +171,11 @@ type ConnectTimeoutEvent struct {
Peripheral Peripheral Peripheral Peripheral
} }
type UpdageValueEvent struct {
Peripheral Peripheral
Characteristic gatt.Characteristic
}
func (b *BLE) Events() chan interface{} { func (b *BLE) Events() chan interface{} {
return b.events return b.events
} }
@ -298,7 +328,7 @@ func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDict
} }
if ok := b.peripherals.Add(peripheral); ok { if ok := b.peripherals.Add(peripheral); ok {
pdLookup[p.Ptr()] = b pdLookup[p.Ptr()] = b
b.events <- DiscoverEvent{Peripheral: peripheral} b.events <- DiscoverPeripheralEvent{Peripheral: peripheral}
} }
} }
@ -335,7 +365,7 @@ func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
fmt.Printf("Go: connectPeripheral returning\n") fmt.Printf("Go: connectPeripheral returning\n")
} }
func DiscoverServices(x Peripheral) { func (x Peripheral) DiscoverServices() {
fmt.Printf("Discovering services on %s\n", x.Name) fmt.Printf("Discovering services on %s\n", x.Name)
p := x.p p := x.p
@ -345,19 +375,15 @@ func DiscoverServices(x Peripheral) {
} }
func discoverServices(p *ns.CBPeripheral, e *ns.NSError) { func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
b := pdLookup[p.Ptr()]
fmt.Printf("Did discover services\n") fmt.Printf("Did discover services\n")
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool { p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
serv := o.CBService() serv := o.CBService()
uuid := serv.UUID() uuid := serv.UUID().UUIDString().String()
switch { b.events <-DiscoverServiceEvent{
case uuid.IsEqualTo(hrm_uuid): Peripheral: newPeripheral(p),
fmt.Printf("--heart rate monitor service\n") Gatt: gatt.Service{uuid},
p.DiscoverCharacteristics(nil, serv) Service: serv,
case uuid.IsEqualTo(info_uuid):
fmt.Printf("--device information service\n")
p.DiscoverCharacteristics(nil, serv)
default:
fmt.Printf("--unknown service\n")
} }
return true return true
}) })
@ -377,26 +403,32 @@ func hr(d *ns.NSData) int {
} }
} }
func (p Peripheral) DiscoverCharacteristics(serv *ns.CBService) {
p.p.DiscoverCharacteristics(nil, serv)
}
func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) { func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
b := pdLookup[p.Ptr()]
fmt.Printf("Did discover characteristics\n") fmt.Printf("Did discover characteristics\n")
uuid := s.UUID() s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
fmt.Printf("----%s\n", uuid.UUIDString()) chr := o.CBCharacteristic()
if uuid.IsEqualTo(hrm_uuid) { chuuid := chr.UUID()
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool { fmt.Printf("------%s\n", chuuid.UUIDString())
chr := o.CBCharacteristic() b.events <-DiscoverCharacteristicEvent{
chuuid := chr.UUID() Peripheral: newPeripheral(p),
fmt.Printf("------%s\n", chuuid.UUIDString()) Service: s,
if chuuid.IsEqualTo(hrv_uuid) { Characteristic: chr,
p.SetNotifyValue(1, chr) Gatt: gatt.Characteristic{chuuid.UUIDString().String()},
v := chr.Value() }
fmt.Println(hr(v)) return true
} })
return true
})
}
fmt.Printf("Go: discoverCharacteristics returning\n") fmt.Printf("Go: discoverCharacteristics returning\n")
} }
func (p Peripheral) SetNotifyValue(c *ns.CBCharacteristic) {
p.p.SetNotifyValue(1, c)
}
func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) { func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
//ble := pdLookup[p] //ble := pdLookup[p]
ble := gble ble := gble

17
gatt/main.go Normal file
View File

@ -0,0 +1,17 @@
package gatt
import (
)
type Service struct {
UUID string
}
type Descriptor struct {
UUID string
}
type Characteristic struct {
UUID string
}