From 97f17e93f1dd5fedcf290effb7d37bccfa9c087f Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 3 Oct 2020 23:24:38 +0200 Subject: [PATCH] gatt/darwin: macOS implementation and ensure pointer usage Signed-off-by: deadprogram --- gap_darwin.go | 4 ++++ gattc_darwin.go | 17 +++++++++++++++-- gattc_linux.go | 2 +- gattc_sd.go | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/gap_darwin.go b/gap_darwin.go index ae6c8fb..4c52deb 100644 --- a/gap_darwin.go +++ b/gap_darwin.go @@ -162,5 +162,9 @@ func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripher if char != nil && char.callback != nil { go char.callback(chr.Value()) } + + if char.readChan != nil { + char.readChan <- nil + } } } diff --git a/gattc_darwin.go b/gattc_darwin.go index e1e224d..d661147 100644 --- a/gattc_darwin.go +++ b/gattc_darwin.go @@ -114,6 +114,7 @@ type DeviceCharacteristic struct { characteristic cbgo.Characteristic callback func(buf []byte) + readChan chan error } // UUID returns the UUID for this DeviceCharacteristic. @@ -147,6 +148,18 @@ func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) er } // Read reads the current characteristic value. -func (c DeviceCharacteristic) Read() (data []byte, err error) { - return nil, nil +func (c *DeviceCharacteristic) Read() (data []byte, err error) { + c.readChan = make(chan error) + c.service.device.prph.ReadCharacteristic(c.characteristic) + + // wait for result + select { + case <-c.readChan: + c.readChan = nil + case <-time.NewTimer(10 * time.Second).C: + c.readChan = nil + return nil, errors.New("timeout on Read()") + } + + return c.characteristic.Value(), nil } diff --git a/gattc_linux.go b/gattc_linux.go index c353ebf..65f5994 100644 --- a/gattc_linux.go +++ b/gattc_linux.go @@ -234,7 +234,7 @@ func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) er } // Read reads the current characteristic value. -func (c DeviceCharacteristic) Read() ([]byte, error) { +func (c *DeviceCharacteristic) Read() ([]byte, error) { options := make(map[string]interface{}) return c.characteristic.ReadValue(options) } diff --git a/gattc_sd.go b/gattc_sd.go index 43ce47a..078edee 100644 --- a/gattc_sd.go +++ b/gattc_sd.go @@ -348,7 +348,7 @@ var readingCharacteristic struct { // Read reads the current characteristic value up to MTU length. // A future enhancement would be to be able to retrieve a longer // value by making multiple calls. -func (c DeviceCharacteristic) Read() ([]byte, error) { +func (c *DeviceCharacteristic) Read() ([]byte, error) { errCode := C.sd_ble_gattc_read(c.connectionHandle, c.valueHandle, 0) if errCode != 0 { return nil, Error(errCode)