Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to allow writeValue() without response #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/BLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions src/remote/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/remote/BLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down