Add gatt package. Add DiscoverServiceEvent and DiscoverCharacteristic
event.
This commit is contained in:
parent
64b684e419
commit
822b3529c0
|
@ -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()
|
|
||||||
fmt.Printf("----%s\n", uuid.UUIDString())
|
|
||||||
if uuid.IsEqualTo(hrm_uuid) {
|
|
||||||
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
||||||
chr := o.CBCharacteristic()
|
chr := o.CBCharacteristic()
|
||||||
chuuid := chr.UUID()
|
chuuid := chr.UUID()
|
||||||
fmt.Printf("------%s\n", chuuid.UUIDString())
|
fmt.Printf("------%s\n", chuuid.UUIDString())
|
||||||
if chuuid.IsEqualTo(hrv_uuid) {
|
b.events <-DiscoverCharacteristicEvent{
|
||||||
p.SetNotifyValue(1, chr)
|
Peripheral: newPeripheral(p),
|
||||||
v := chr.Value()
|
Service: s,
|
||||||
fmt.Println(hr(v))
|
Characteristic: chr,
|
||||||
|
Gatt: gatt.Characteristic{chuuid.UUIDString().String()},
|
||||||
}
|
}
|
||||||
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
17
gatt/main.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package gatt
|
||||||
|
|
||||||
|
import (
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
UUID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Descriptor struct {
|
||||||
|
UUID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Characteristic struct {
|
||||||
|
UUID string
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user