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"
"unsafe"
"git.wow.st/gmp/ble/gatt"
"git.wow.st/gmp/ble/ns"
)
@ -29,6 +30,8 @@ type BLE struct {
var cdLookup map[unsafe.Pointer]*BLE
var pdLookup map[unsafe.Pointer]*BLE
var pcache map[unsafe.Pointer]*Peripheral
type State string
@ -50,11 +53,20 @@ func peripheralName(p *ns.CBPeripheral) string {
}
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),
Identifier: x.Identifier().UUIDString().String(),
p: x,
}
pcache[x.Ptr()] = &ret
return ret
}
type PeripheralListItem struct {
@ -135,10 +147,23 @@ type UpdateStateEvent struct {
State string
}
type DiscoverEvent struct {
type DiscoverPeripheralEvent struct {
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 {
Peripheral Peripheral
}
@ -146,6 +171,11 @@ type ConnectTimeoutEvent struct {
Peripheral Peripheral
}
type UpdageValueEvent struct {
Peripheral Peripheral
Characteristic gatt.Characteristic
}
func (b *BLE) Events() chan interface{} {
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 {
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")
}
func DiscoverServices(x Peripheral) {
func (x Peripheral) DiscoverServices() {
fmt.Printf("Discovering services on %s\n", x.Name)
p := x.p
@ -345,19 +375,15 @@ func DiscoverServices(x Peripheral) {
}
func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
b := pdLookup[p.Ptr()]
fmt.Printf("Did discover services\n")
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
serv := o.CBService()
uuid := serv.UUID()
switch {
case uuid.IsEqualTo(hrm_uuid):
fmt.Printf("--heart rate monitor service\n")
p.DiscoverCharacteristics(nil, serv)
case uuid.IsEqualTo(info_uuid):
fmt.Printf("--device information service\n")
p.DiscoverCharacteristics(nil, serv)
default:
fmt.Printf("--unknown service\n")
uuid := serv.UUID().UUIDString().String()
b.events <-DiscoverServiceEvent{
Peripheral: newPeripheral(p),
Gatt: gatt.Service{uuid},
Service: serv,
}
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) {
b := pdLookup[p.Ptr()]
fmt.Printf("Did discover characteristics\n")
uuid := s.UUID()
fmt.Printf("----%s\n", uuid.UUIDString())
if uuid.IsEqualTo(hrm_uuid) {
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
chr := o.CBCharacteristic()
chuuid := chr.UUID()
fmt.Printf("------%s\n", chuuid.UUIDString())
if chuuid.IsEqualTo(hrv_uuid) {
p.SetNotifyValue(1, chr)
v := chr.Value()
fmt.Println(hr(v))
}
return true
})
}
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
})
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) {
//ble := pdLookup[p]
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
}