darwin: add Write command to the gattc implementation

This commit is contained in:
Fabianexe 2024-01-05 14:03:30 +01:00 committed by GitHub
parent 5746ccfb60
commit 190c4be423
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View file

@ -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
}
}
}
}
}

View file

@ -168,6 +168,7 @@ type deviceCharacteristic struct {
characteristic cbgo.Characteristic
callback func(buf []byte)
readChan chan error
writeChan chan error
}
// UUID returns the UUID for this DeviceCharacteristic.
@ -175,6 +176,27 @@ func (c DeviceCharacteristic) UUID() UUID {
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
// 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