Skip to content

Commit 26001ee

Browse files
committed
Allow changing value size after initialization
1 parent c32ec18 commit 26001ee

7 files changed

+36
-1
lines changed

src/BLECharacteristic.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ uint8_t BLECharacteristic::properties() const
109109
return 0;
110110
}
111111

112+
bool BLECharacteristic::setValueSize(uint16_t valueSize) {
113+
if (_local) {
114+
return _local->setValueSize(valueSize);
115+
}
116+
117+
if (_remote) {
118+
//_remote->setValueLength();
119+
}
120+
121+
return false;
122+
}
123+
112124
int BLECharacteristic::valueSize() const
113125
{
114126
if (_local) {

src/BLECharacteristic.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class BLECharacteristic {
5454

5555
uint8_t properties() const;
5656

57+
bool setValueSize(uint16_t valueSize);
5758
int valueSize() const;
5859
const uint8_t* value() const;
5960
int valueLength() const;

src/BLEDevice.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum BLEDeviceEvent {
2828
BLEConnected = 0,
2929
BLEDisconnected = 1,
3030
BLEDiscovered = 2,
31+
BLEMtuChanged = 3,
3132

3233
BLEDeviceLastEvent
3334
};

src/local/BLELocalCharacteristic.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ uint8_t BLELocalCharacteristic::permissions() const {
8989
return _permissions;
9090
}
9191

92+
bool BLELocalCharacteristic::setValueSize(uint16_t valueSize) {
93+
if (valueSize == _valueSize) {
94+
return true;
95+
}
96+
97+
if (_valueLength > 0 && valueSize < _valueLength) {
98+
return false;
99+
}
100+
101+
_value = (uint8_t*)realloc(_value, valueSize);
102+
_valueSize = valueSize;
103+
return true;
104+
}
105+
92106
int BLELocalCharacteristic::valueSize() const
93107
{
94108
return _valueSize;

src/local/BLELocalCharacteristic.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class BLELocalCharacteristic : public BLELocalAttribute {
4242
uint8_t properties() const;
4343
uint8_t permissions() const;
4444

45+
bool setValueSize(uint16_t valueSize);
4546
int valueSize() const;
4647
const uint8_t* value() const;
4748
int valueLength() const;

src/utility/ATT.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,9 @@ void ATTClass::mtuReq(uint16_t connectionHandle, uint8_t dlen, uint8_t data[])
689689
for (int i = 0; i < ATT_MAX_PEERS; i++) {
690690
if (_peers[i].connectionHandle == connectionHandle) {
691691
_peers[i].mtu = mtu;
692+
if (_eventHandlers[BLEMtuChanged]) {
693+
_eventHandlers[BLEMtuChanged](BLEDevice(_peers[i].addressType, _peers[i].address));
694+
}
692695
break;
693696
}
694697
}
@@ -722,6 +725,9 @@ void ATTClass::mtuResp(uint16_t connectionHandle, uint8_t dlen, uint8_t data[])
722725
for (int i = 0; i < ATT_MAX_PEERS; i++) {
723726
if (_peers[i].connectionHandle == connectionHandle) {
724727
_peers[i].mtu = mtu;
728+
// if (_eventHandlers[BLEMtuChanged]) {
729+
// _eventHandlers[BLEMtuChanged](BLEDevice(_peers[i].addressType, _peers[i].address));
730+
// }
725731
break;
726732
}
727733
}

src/utility/ATT.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class ATTClass {
169169
uint8_t length;
170170
} _pendingResp;
171171

172-
BLEDeviceEventHandler _eventHandlers[2];
172+
BLEDeviceEventHandler _eventHandlers[BLEDeviceLastEvent];
173173
};
174174

175175
extern ATTClass& ATT;

0 commit comments

Comments
 (0)