From 7e62763f2738642cc6205ff0119e4ec1fe568317 Mon Sep 17 00:00:00 2001 From: "fabian.gaertner" Date: Sun, 10 Dec 2023 11:23:26 +0100 Subject: [PATCH] Add Write command to the gattc implementaitons (windows hat already one) --- gattc_darwin.go | 8 ++++++++ gattc_linux.go | 10 ++++++++++ gattc_sd.go | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/gattc_darwin.go b/gattc_darwin.go index 4fb1f301..3abd8e1f 100644 --- a/gattc_darwin.go +++ b/gattc_darwin.go @@ -175,6 +175,14 @@ 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.service.device.prph.WriteCharacteristic(p, c.characteristic, true) + + 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 diff --git a/gattc_linux.go b/gattc_linux.go index 462040d1..b3bb19dd 100644 --- a/gattc_linux.go +++ b/gattc_linux.go @@ -226,6 +226,16 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter return chars, nil } +// 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) { + err = c.characteristic.WriteValue(p, map[string]interface{}{"type": "request"}) + 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 diff --git a/gattc_sd.go b/gattc_sd.go index 018d398e..050964d8 100644 --- a/gattc_sd.go +++ b/gattc_sd.go @@ -311,6 +311,26 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter return characteristics, nil } +// 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) { + if len(p) == 0 { + return 0, nil + } + + errCode := C.sd_ble_gattc_write(c.connectionHandle, &C.ble_gattc_write_params_t{ + write_op: C.BLE_GATT_OP_WRITE_REQ, + handle: c.valueHandle, + offset: 0, + len: uint16(len(p)), + p_value: &p[0], + }) + if errCode != 0 { + return 0, Error(errCode) + } + 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