Skip to content

Allow valueSize changes after initialization to adjust to MTU size #391

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
return 0;
}

bool BLECharacteristic::setValueSize(uint16_t valueSize) {
if (_local) {
return _local->setValueSize(valueSize);

Check warning on line 114 in src/BLECharacteristic.cpp

View check run for this annotation

Codecov / codecov/patch

src/BLECharacteristic.cpp#L112-L114

Added lines #L112 - L114 were not covered by tests
}

if (_remote) {

Check warning on line 117 in src/BLECharacteristic.cpp

View check run for this annotation

Codecov / codecov/patch

src/BLECharacteristic.cpp#L117

Added line #L117 was not covered by tests
//_remote->setValueLength();
}

return false;

Check warning on line 121 in src/BLECharacteristic.cpp

View check run for this annotation

Codecov / codecov/patch

src/BLECharacteristic.cpp#L121

Added line #L121 was not covered by tests
}

int BLECharacteristic::valueSize() const
{
if (_local) {
Expand Down
1 change: 1 addition & 0 deletions src/BLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BLECharacteristic {

uint8_t properties() const;

bool setValueSize(uint16_t valueSize);
int valueSize() const;
const uint8_t* value() const;
int valueLength() const;
Expand Down
5 changes: 5 additions & 0 deletions src/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@
return _rssi;
}

uint16_t BLEDevice::getMTU() const

Check warning on line 264 in src/BLEDevice.cpp

View check run for this annotation

Codecov / codecov/patch

src/BLEDevice.cpp#L264

Added line #L264 was not covered by tests
{
return ATT.mtu(ATT.connectionHandle(_addressType, _address));

Check warning on line 266 in src/BLEDevice.cpp

View check run for this annotation

Codecov / codecov/patch

src/BLEDevice.cpp#L266

Added line #L266 was not covered by tests
}

bool BLEDevice::connect()
{
return ATT.connect(_addressType, _address);
Expand Down
2 changes: 2 additions & 0 deletions src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum BLEDeviceEvent {
BLEConnected = 0,
BLEDisconnected = 1,
BLEDiscovered = 2,
BLEMtuChanged = 3,

BLEDeviceLastEvent
};
Expand Down Expand Up @@ -68,6 +69,7 @@ class BLEDevice {
int manufacturerData(uint8_t value[], int length) const;

virtual int rssi();
virtual uint16_t getMTU() const;

bool connect();
bool discoverAttributes();
Expand Down
14 changes: 14 additions & 0 deletions src/local/BLELocalCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@
return _permissions;
}

bool BLELocalCharacteristic::setValueSize(uint16_t valueSize) {
if (valueSize == _valueSize) {
return true;

Check warning on line 94 in src/local/BLELocalCharacteristic.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalCharacteristic.cpp#L92-L94

Added lines #L92 - L94 were not covered by tests
}

if (_valueLength > 0 && valueSize < _valueLength) {
return false;

Check warning on line 98 in src/local/BLELocalCharacteristic.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalCharacteristic.cpp#L97-L98

Added lines #L97 - L98 were not covered by tests
}

_value = (uint8_t*)realloc(_value, valueSize);
_valueSize = valueSize;
return true;

Check warning on line 103 in src/local/BLELocalCharacteristic.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalCharacteristic.cpp#L101-L103

Added lines #L101 - L103 were not covered by tests
}

int BLELocalCharacteristic::valueSize() const
{
return _valueSize;
Expand Down
1 change: 1 addition & 0 deletions src/local/BLELocalCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BLELocalCharacteristic : public BLELocalAttribute {
uint8_t properties() const;
uint8_t permissions() const;

bool setValueSize(uint16_t valueSize);
int valueSize() const;
const uint8_t* value() const;
int valueLength() const;
Expand Down
11 changes: 11 additions & 0 deletions src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@
return 127;
}

uint16_t BLELocalDevice::getMTU()

Check warning on line 286 in src/local/BLELocalDevice.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalDevice.cpp#L286

Added line #L286 was not covered by tests
{
BLEDevice central = ATT.central();

Check warning on line 288 in src/local/BLELocalDevice.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalDevice.cpp#L288

Added line #L288 was not covered by tests

if (central) {
return central.getMTU();

Check warning on line 291 in src/local/BLELocalDevice.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalDevice.cpp#L290-L291

Added lines #L290 - L291 were not covered by tests
}

return -1;
}

Check warning on line 295 in src/local/BLELocalDevice.cpp

View check run for this annotation

Codecov / codecov/patch

src/local/BLELocalDevice.cpp#L294-L295

Added lines #L294 - L295 were not covered by tests

bool BLELocalDevice::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
{
return _advertisingData.setAdvertisedServiceUuid(advertisedServiceUuid);
Expand Down
1 change: 1 addition & 0 deletions src/local/BLELocalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class BLELocalDevice {
virtual String address() const;

virtual int rssi();
virtual uint16_t getMTU();

virtual bool setAdvertisedServiceUuid(const char* advertisedServiceUuid);
virtual bool setAdvertisedService(const BLEService& service);
Expand Down
6 changes: 6 additions & 0 deletions src/utility/ATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@
for (int i = 0; i < ATT_MAX_PEERS; i++) {
if (_peers[i].connectionHandle == connectionHandle) {
_peers[i].mtu = mtu;
if (_eventHandlers[BLEMtuChanged]) {
_eventHandlers[BLEMtuChanged](BLEDevice(_peers[i].addressType, _peers[i].address));

Check warning on line 693 in src/utility/ATT.cpp

View check run for this annotation

Codecov / codecov/patch

src/utility/ATT.cpp#L692-L693

Added lines #L692 - L693 were not covered by tests
}
break;
}
}
Expand Down Expand Up @@ -722,6 +725,9 @@
for (int i = 0; i < ATT_MAX_PEERS; i++) {
if (_peers[i].connectionHandle == connectionHandle) {
_peers[i].mtu = mtu;
// if (_eventHandlers[BLEMtuChanged]) {
// _eventHandlers[BLEMtuChanged](BLEDevice(_peers[i].addressType, _peers[i].address));
// }
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utility/ATT.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ATTClass {
uint8_t length;
} _pendingResp;

BLEDeviceEventHandler _eventHandlers[2];
BLEDeviceEventHandler _eventHandlers[BLEDeviceLastEvent];
};

extern ATTClass& ATT;
Expand Down
Loading