darwin: add Write command to the gattc implementation
This commit is contained in:
parent
5746ccfb60
commit
190c4be423
2 changed files with 39 additions and 0 deletions
|
@ -211,3 +211,20 @@ func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripher
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DidWriteValueForCharacteristic is called after the characteristic for a Service
|
||||||
|
// for a Peripheral trigger a write with response. It contains the returned error or nil.
|
||||||
|
func (pd *peripheralDelegate) DidWriteValueForCharacteristic(_ cbgo.Peripheral, chr cbgo.Characteristic, err error) {
|
||||||
|
uuid, _ := ParseUUID(chr.UUID().String())
|
||||||
|
svcuuid, _ := ParseUUID(chr.Service().UUID().String())
|
||||||
|
|
||||||
|
if svc, ok := pd.d.services[svcuuid]; ok {
|
||||||
|
for _, char := range svc.characteristics {
|
||||||
|
if char.characteristic == chr && uuid == char.UUID() { // compare pointers
|
||||||
|
if char.writeChan != nil {
|
||||||
|
char.writeChan <- err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -168,6 +168,7 @@ type deviceCharacteristic struct {
|
||||||
characteristic cbgo.Characteristic
|
characteristic cbgo.Characteristic
|
||||||
callback func(buf []byte)
|
callback func(buf []byte)
|
||||||
readChan chan error
|
readChan chan error
|
||||||
|
writeChan chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
// UUID returns the UUID for this DeviceCharacteristic.
|
// UUID returns the UUID for this DeviceCharacteristic.
|
||||||
|
@ -175,6 +176,27 @@ func (c DeviceCharacteristic) UUID() UUID {
|
||||||
return c.uuidWrapper
|
return c.uuidWrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write replaces the characteristic value with a new value. The
|
||||||
|
// call will return after all data has been written.
|
||||||
|
func (c DeviceCharacteristic) Write(p []byte) (n int, err error) {
|
||||||
|
c.writeChan = make(chan error)
|
||||||
|
c.service.device.prph.WriteCharacteristic(p, c.characteristic, true)
|
||||||
|
|
||||||
|
// wait for result
|
||||||
|
select {
|
||||||
|
case <-time.NewTimer(10 * time.Second).C:
|
||||||
|
err = errors.New("timeout on Write()")
|
||||||
|
case err = <-c.writeChan:
|
||||||
|
}
|
||||||
|
|
||||||
|
c.writeChan = nil
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
// WriteWithoutResponse replaces the characteristic value with a new value. The
|
// WriteWithoutResponse replaces the characteristic value with a new value. The
|
||||||
// call will return before all data has been written. A limited number of such
|
// call will return before all data has been written. A limited number of such
|
||||||
// writes can be in flight at any given time. This call is also known as a
|
// writes can be in flight at any given time. This call is also known as a
|
||||||
|
|
Loading…
Reference in a new issue