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

New Write mehod for characteristics #368

Closed
wants to merge 2 commits 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
61 changes: 61 additions & 0 deletions src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,67 @@ int BLECharacteristic::readValue(int32_t& value)
return readValue((uint8_t*)&value, sizeof(value));
}

int BLECharacteristic::write(const char* value, bool withResponse)
{
if (_local) {
return _local->write(value);
}

if (_remote) {
return _remote->write(value, withResponse);
}

return 0;
}

int BLECharacteristic::write(const uint8_t value[], int length, bool withResponse)
{
if (_local) {
return _local->write(value, length);
}

if (_remote) {
return _remote->write(value, length, withResponse);
}

return 0;
}

int BLECharacteristic::write(const void* value, int length, bool withResponse)
{
return write((const uint8_t*)value, length, withResponse);
}

int BLECharacteristic::write(uint8_t value, bool withResponse)
{
return write((uint8_t*)&value, sizeof(value), withResponse);
}

int BLECharacteristic::write(int8_t value, bool withResponse)
{
return write((uint8_t*)&value, sizeof(value), withResponse);
}

int BLECharacteristic::write(uint16_t value, bool withResponse)
{
return write((uint8_t*)&value, sizeof(value), withResponse);
}

int BLECharacteristic::write(int16_t value, bool withResponse)
{
return write((uint8_t*)&value, sizeof(value), withResponse);
}

int BLECharacteristic::write(uint32_t value, bool withResponse)
{
return write((uint8_t*)&value, sizeof(value), withResponse);
}

int BLECharacteristic::write(int32_t value, bool withResponse)
{
return write((uint8_t*)&value, sizeof(value), withResponse);
}

int BLECharacteristic::writeValue(const uint8_t value[], int length, bool withResponse)
{
if (_local) {
Expand Down
10 changes: 10 additions & 0 deletions src/BLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ class BLECharacteristic {
int writeValue(int16_t value, bool withResponse = true);
int writeValue(uint32_t value, bool withResponse = true);
int writeValue(int32_t value, bool withResponse = true);
int write(const uint8_t value[], int length, bool withResponse = true);
int write(const char* value, bool withResponse = true);
int write(const void* value, int length, bool withResponse = true);
int write(uint8_t value, bool withResponse = true);
int write(int8_t value, bool withResponse = true);
int write(uint16_t value, bool withResponse = true);
int write(int16_t value, bool withResponse = true);
int write(uint32_t value, bool withResponse = true);
int write(int32_t value, bool withResponse = true);


// deprecated, use writeValue(...)
int setValue(const uint8_t value[], int length) { return writeValue(value, length); }
Expand Down
10 changes: 10 additions & 0 deletions src/BLEStringCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ BLEStringCharacteristic::BLEStringCharacteristic(const char* uuid, unsigned char
{
}

int BLEStringCharacteristic::write(const char *value)
{
return BLECharacteristic::write(value);
}

int BLEStringCharacteristic::write(const String &value)
{
return BLECharacteristic::write(value.c_str());
}

int BLEStringCharacteristic::writeValue(const String& value)
{
return BLECharacteristic::writeValue(value.c_str());
Expand Down
2 changes: 2 additions & 0 deletions src/BLEStringCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BLEStringCharacteristic : public BLECharacteristic
public:
BLEStringCharacteristic(const char* uuid, unsigned char properties, int valueSize);

int write(const char * value);
int write(const String& value);
int writeValue(const String& value);
int setValue(const String& value) { return writeValue(value); }
String value(void);
Expand Down
6 changes: 6 additions & 0 deletions src/BLETypedCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ template<typename T> class BLETypedCharacteristic : public BLECharacteristic
public:
BLETypedCharacteristic(const char* uuid, unsigned int permissions);

int write(T value);
int writeValue(T value);
int setValue(T value) { return writeValue(value); }
T value(void);
Expand All @@ -52,6 +53,11 @@ template<typename T> BLETypedCharacteristic<T>::BLETypedCharacteristic(const cha
writeValue(value);
}

template<typename T> int BLETypedCharacteristic<T>::write(T value)
{
return BLECharacteristic::write((uint8_t*)&value, sizeof(T));
}

template<typename T> int BLETypedCharacteristic<T>::writeValue(T value)
{
return BLECharacteristic::writeValue((uint8_t*)&value, sizeof(T));
Expand Down
45 changes: 43 additions & 2 deletions src/local/BLELocalCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ int BLELocalCharacteristic::writeValue(const uint8_t value[], int length)
}

if ((_properties & BLEIndicate) && (_cccdValue & 0x0002)) {
return ATT.handleInd(valueHandle(), _value, _valueLength);
return ATT.handleInd(valueHandle(), _value,&_valueLength);
} else if ((_properties & BLENotify) && (_cccdValue & 0x0001)) {
return ATT.handleNotify(valueHandle(), _value, _valueLength);
return ATT.handleNotify(valueHandle(), _value, &_valueLength);
}

if (_broadcast) {
Expand All @@ -142,6 +142,47 @@ int BLELocalCharacteristic::writeValue(const char* value)
return writeValue((uint8_t*)value, strlen(value));
}

int BLELocalCharacteristic::write(const uint8_t value[], int length)
{
_valueLength = min(length, _valueSize);
memcpy(_value, value, _valueLength);

if (_fixedLength) {
_valueLength = _valueSize;
}

if ((_properties & BLEIndicate) && (_cccdValue & 0x0002)) {
uint8_t res = ATT.handleInd(valueHandle(), _value, &_valueLength);
if (res != 1){
return res;
}
return _valueLength;
} else if ((_properties & BLENotify) && (_cccdValue & 0x0001)) {
uint8_t res = ATT.handleNotify(valueHandle(), _value, &_valueLength);
if (res != 1){
return res;
}
return _valueLength;
}

if (_broadcast) {
uint16_t serviceUuid = GATT.serviceUuidForCharacteristic(this);

BLE.setAdvertisedServiceData(serviceUuid, value, length);

if (!ATT.connected() && GAP.advertising()) {
BLE.advertise();
}
}

return _valueLength;
}

int BLELocalCharacteristic::write(const char* value)
{
return write((uint8_t*)value, strlen(value));
}

int BLELocalCharacteristic::broadcast()
{
if (_properties & BLEBroadcast) {
Expand Down
2 changes: 2 additions & 0 deletions src/local/BLELocalCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class BLELocalCharacteristic : public BLELocalAttribute {

int writeValue(const uint8_t value[], int length);
int writeValue(const char* value);
int write(const uint8_t value[], int length);
int write(const char* value);

int broadcast();

Expand Down
53 changes: 53 additions & 0 deletions src/remote/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,59 @@ uint8_t BLERemoteCharacteristic::operator[] (int offset) const
return 0;
}

int BLERemoteCharacteristic::write(const uint8_t value[], int length, bool withResponse)
{
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) && withResponse) {
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 length;
} else if (_properties & BLEWriteWithoutResponse) {
ATT.writeCmd(_connectionHandle, _valueHandle, value, length);

memcpy(_value, value, length);
_valueLength = length;

return length;
}

return 0;
}

int BLERemoteCharacteristic::write(const char* value, bool withResponse)
{
return write((uint8_t*)value, strlen(value), withResponse);
}

int BLERemoteCharacteristic::writeValue(const uint8_t value[], int length, bool withResponse)
{
if (!ATT.connected(_connectionHandle)) {
Expand Down
2 changes: 2 additions & 0 deletions src/remote/BLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class BLERemoteCharacteristic : public BLERemoteAttribute {

int writeValue(const uint8_t value[], int length, bool withResponse = true);
int writeValue(const char* value, bool withResponse = true);
int write(const uint8_t value[], int length, bool withResponse = true);
int write(const char* value, bool withResponse = true);

bool valueUpdated();
bool updatedValueRead();
Expand Down
17 changes: 9 additions & 8 deletions src/utility/ATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ BLEDevice ATTClass::central()
return BLEDevice();
}

bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, uint16_t *length)
{
int numNotifications = 0;

Expand All @@ -600,9 +600,9 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
memcpy(&notification[1], &handle, sizeof(handle));
notificationLength += sizeof(handle);

length = min((uint16_t)(_peers[i].mtu - notificationLength), (uint16_t)length);
memcpy(&notification[notificationLength], value, length);
notificationLength += length;
*length = min((uint16_t)(_peers[i].mtu - notificationLength), *length);
memcpy(&notification[notificationLength], value, *length);
notificationLength += *length;

/// TODO: Set encryption requirement on notify.
HCI.sendAclPkt(_peers[i].connectionHandle, ATT_CID, notificationLength, notification);
Expand All @@ -613,7 +613,7 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
return (numNotifications > 0);
}

bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, int length)
bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, uint16_t *length)
{
int numIndications = 0;

Expand All @@ -631,9 +631,10 @@ bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, int length)
memcpy(&indication[1], &handle, sizeof(handle));
indicationLength += sizeof(handle);

length = min((uint16_t)(_peers[i].mtu - indicationLength), (uint16_t)length);
memcpy(&indication[indicationLength], value, length);
indicationLength += length;
*length = min((uint16_t)(_peers[i].mtu - indicationLength), *length);

memcpy(&indication[indicationLength], value, *length);
indicationLength += *length;

_cnf = false;

Expand Down
4 changes: 2 additions & 2 deletions src/utility/ATT.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class ATTClass {

virtual BLEDevice central();

virtual bool handleNotify(uint16_t handle, const uint8_t* value, int length);
virtual bool handleInd(uint16_t handle, const uint8_t* value, int length);
virtual bool handleNotify(uint16_t handle, const uint8_t* value, uint16_t *length);
virtual bool handleInd(uint16_t handle, const uint8_t* value, uint16_t *length);

virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler);

Expand Down
Loading