From f97f8ab5a6f9a73a880a50c455e66447e49cf6f8 Mon Sep 17 00:00:00 2001 From: Larry Bank Date: Sat, 2 May 2020 07:47:25 -0400 Subject: [PATCH] Added option to allow writeValue() without response --- src/BLECharacteristic.cpp | 17 ++++++++++ src/BLECharacteristic.h | 2 ++ src/remote/BLERemoteCharacteristic.cpp | 47 ++++++++++++++++++++++++++ src/remote/BLERemoteCharacteristic.h | 1 + 4 files changed, 67 insertions(+) diff --git a/src/BLECharacteristic.cpp b/src/BLECharacteristic.cpp index 34f761fa..de8419a4 100644 --- a/src/BLECharacteristic.cpp +++ b/src/BLECharacteristic.cpp @@ -234,6 +234,18 @@ int BLECharacteristic::readValue(int32_t& value) return readValue((uint8_t*)&value, sizeof(value)); } +int BLECharacteristic::writeValue(const uint8_t value[], int length, bool bWithResponse) +{ + if (_local) { + return _local->writeValue(value, length); + } + + if (_remote) { + return _remote->writeValue(value, length, bWithResponse); + } + return 0; +} + int BLECharacteristic::writeValue(const uint8_t value[], int length) { if (_local) { @@ -247,6 +259,11 @@ int BLECharacteristic::writeValue(const uint8_t value[], int length) return 0; } +int BLECharacteristic::writeValue(const void* value, int length, bool bWithResponse) +{ + return writeValue((const uint8_t*)value, length, bWithResponse); +} + int BLECharacteristic::writeValue(const void* value, int length) { return writeValue((const uint8_t*)value, length); diff --git a/src/BLECharacteristic.h b/src/BLECharacteristic.h index 03e1b61e..f562665e 100644 --- a/src/BLECharacteristic.h +++ b/src/BLECharacteristic.h @@ -69,7 +69,9 @@ class BLECharacteristic { int readValue(int32_t& value); int writeValue(const uint8_t value[], int length); + int writeValue(const uint8_t value[], int length, bool bWithResponse); int writeValue(const void* value, int length); + int writeValue(const void* value, int length, bool bWithResponse); int writeValue(const char* value); int writeValue(uint8_t value); int writeValue(int8_t value); diff --git a/src/remote/BLERemoteCharacteristic.cpp b/src/remote/BLERemoteCharacteristic.cpp index ecad6229..e58ba987 100644 --- a/src/remote/BLERemoteCharacteristic.cpp +++ b/src/remote/BLERemoteCharacteristic.cpp @@ -133,6 +133,53 @@ int BLERemoteCharacteristic::writeValue(const uint8_t value[], int length) return 0; } +int BLERemoteCharacteristic::writeValue(const uint8_t value[], int length, bool bWithResponse) +{ + if (!ATT.connected(_connectionHandle)) { + return false; + } + + uint16_t maxLength = ATT.mtu(_connectionHandle) - 3; + + if (length > (int)maxLength) { + // cap to MTU max length + length = maxLength; + } + + _value = (uint8_t*)realloc(_value, length); + if (_value == NULL) { + // realloc failed + return 0; + } + + if ((_properties & BLEWrite) && bWithResponse) { + uint8_t resp[4]; + int respLength = ATT.writeReq(_connectionHandle, _valueHandle, value, length, resp); + + if (!respLength) { + return 0; + } + + if (resp[0] == 0x01) { + // error + return 0; + } + + memcpy(_value, value, length); + _valueLength = length; + + return 1; + } else if (_properties & BLEWriteWithoutResponse) { + ATT.writeCmd(_connectionHandle, _valueHandle, value, length); + + memcpy(_value, value, length); + _valueLength = length; + + return 1; + } + + return 0; +} int BLERemoteCharacteristic::writeValue(const char* value) { return writeValue((uint8_t*)value, strlen(value)); diff --git a/src/remote/BLERemoteCharacteristic.h b/src/remote/BLERemoteCharacteristic.h index c4bc140d..09b5b7c5 100644 --- a/src/remote/BLERemoteCharacteristic.h +++ b/src/remote/BLERemoteCharacteristic.h @@ -39,6 +39,7 @@ class BLERemoteCharacteristic : public BLERemoteAttribute { uint8_t operator[] (int offset) const; int writeValue(const uint8_t value[], int length); + int writeValue(const uint8_t value[], int length, bool bWithResponse); int writeValue(const char* value); bool valueUpdated(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy