Skip to content

Commit c692433

Browse files
committed
Dynamically allocate characteristic and descriptor data buffers
1 parent 040b63b commit c692433

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

libraries/CurieBle/src/BLECharacteristic.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ BLECharacteristic::BLECharacteristic(const char* uuid,
2828
const unsigned short maxLength) :
2929
BLEAttribute(uuid, BLETypeCharacteristic),
3030
_properties(properties),
31-
_data_len(0),
31+
_value_length(0),
3232
_written(false),
3333
_cccd_value(0),
3434
_value_handle(0),
3535
_cccd_handle(0),
3636
_user_description(NULL),
3737
_presentation_format(NULL)
3838
{
39-
_max_len = maxLength > BLE_MAX_ATTR_DATA_LEN ? BLE_MAX_ATTR_DATA_LEN : maxLength;
39+
_value_size = maxLength > BLE_MAX_ATTR_DATA_LEN ? BLE_MAX_ATTR_DATA_LEN : maxLength;
40+
_value = (unsigned char*)malloc(_value_length);
4041

4142
memset(_event_handlers, 0, sizeof(_event_handlers));
4243
}
@@ -49,6 +50,14 @@ BLECharacteristic::BLECharacteristic(const char* uuid,
4950
setValue((const uint8_t*)value, strlen(value));
5051
}
5152

53+
BLECharacteristic::~BLECharacteristic()
54+
{
55+
if (_value) {
56+
free(_value);
57+
_value = NULL;
58+
}
59+
}
60+
5261
unsigned char
5362
BLECharacteristic::properties() const
5463
{
@@ -63,15 +72,15 @@ BLECharacteristic::setValue(const unsigned char value[], uint16_t length)
6372
_setValue(value, length);
6473

6574
if (_value_handle) {
66-
status = ble_client_gatts_set_attribute_value(_value_handle, _data_len, _data, 0);
75+
status = ble_client_gatts_set_attribute_value(_value_handle, _value_length, _value, 0);
6776
if (BLE_STATUS_SUCCESS != status) {
6877
return false;
6978
}
7079

7180
if (subscribed()) {
7281
boolean_t indication = (_cccd_value & BLE_CCCD_INDICATE_EN_MASK);
7382

74-
status = ble_client_gatts_send_notif_ind(_value_handle, _data_len, _data, 0, indication);
83+
status = ble_client_gatts_send_notif_ind(_value_handle, _value_length, _value, 0, indication);
7584
if (BLE_STATUS_SUCCESS != status) {
7685
return false;
7786
}
@@ -96,25 +105,25 @@ BLECharacteristic::setValue(BLECentral& central, const unsigned char* value, uns
96105
unsigned short
97106
BLECharacteristic::valueSize() const
98107
{
99-
return _max_len;
108+
return _value_size;
100109
}
101110

102111
const unsigned char*
103112
BLECharacteristic::value() const
104113
{
105-
return _data;
114+
return _value;
106115
}
107116

108117
unsigned short
109118
BLECharacteristic::valueLength() const
110119
{
111-
return _data_len;
120+
return _value_length;
112121
}
113122

114123
unsigned char
115124
BLECharacteristic::operator[] (int offset) const
116125
{
117-
return _data[offset];
126+
return _value[offset];
118127
}
119128

120129
bool
@@ -170,9 +179,9 @@ BLECharacteristic::add(uint16_t serviceHandle)
170179
char_data.perms.rd = GAP_SEC_NO_PERMISSION;
171180
}
172181

173-
char_data.init_len = _data_len;
174-
char_data.max_len = _max_len;
175-
char_data.p_value = _data;
182+
char_data.init_len = _value_length;
183+
char_data.max_len = _value_size;
184+
char_data.p_value = _value;
176185

177186
if (_user_description) {
178187
user_desc.buffer = (uint8_t*)_user_description->value();
@@ -247,10 +256,10 @@ BLECharacteristic::setPresentationFormat(BLEDescriptor *descriptor)
247256
void
248257
BLECharacteristic::_setValue(const uint8_t value[], uint16_t length)
249258
{
250-
if (length > _max_len) {
251-
length = _max_len;
259+
if (length > _value_size) {
260+
length = _value_size;
252261
}
253262

254-
memcpy(_data, value, length);
255-
_data_len = length;
263+
memcpy(_value, value, length);
264+
_value_length = length;
256265
}

libraries/CurieBle/src/BLECharacteristic.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class BLECharacteristic : public BLEAttribute {
8181
const unsigned char properties,
8282
const char* value);
8383

84+
virtual ~BLECharacteristic();
85+
8486
/**
8587
* Set the current value of the Characteristic
8688
*
@@ -100,7 +102,7 @@ class BLECharacteristic : public BLEAttribute {
100102
unsigned char properties(void) const;
101103

102104
/**
103-
* Get the size of the Characteristic
105+
* Get the (maximum) size of the Characteristic
104106
*
105107
* @return unsigned size of characateristic in bytes
106108
*/
@@ -114,7 +116,7 @@ class BLECharacteristic : public BLEAttribute {
114116
const unsigned char* value(void) const;
115117

116118
/**
117-
* Get the length of the value of the Characteristic
119+
* Get the current length of the value of the Characteristic
118120
*
119121
* @return unsigned short size of characateristic value in bytes
120122
*/
@@ -164,9 +166,9 @@ class BLECharacteristic : public BLEAttribute {
164166

165167
private:
166168
unsigned char _properties;
167-
unsigned short _max_len;
168-
unsigned char _data[BLE_MAX_ATTR_DATA_LEN];
169-
unsigned short _data_len;
169+
unsigned short _value_size;
170+
unsigned short _value_length;
171+
unsigned char* _value;
170172
bool _written;
171173

172174
uint16_t _cccd_value;

libraries/CurieBle/src/BLEDescriptor.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ BLEDescriptor::BLEDescriptor(const char* uuid, const unsigned char value[], unsi
2727
if (valueLength > BLE_MAX_ATTR_DATA_LEN) {
2828
valueLength = BLE_MAX_ATTR_DATA_LEN;
2929
}
30-
_data_len = valueLength;
30+
_value_length = valueLength;
31+
_value = (unsigned char*)malloc(_value_length);
3132

32-
memcpy(_data, value, _data_len);
33+
memcpy(_value, value, _value_length);
34+
}
35+
36+
BLEDescriptor::~BLEDescriptor() {
37+
if (_value) {
38+
free(_value);
39+
_value = NULL;
40+
}
3341
}
3442

3543
BLEDescriptor::BLEDescriptor(const char* uuid, const char* value) :
@@ -40,19 +48,19 @@ BLEDescriptor::BLEDescriptor(const char* uuid, const char* value) :
4048
const unsigned char*
4149
BLEDescriptor::BLEDescriptor::value() const
4250
{
43-
return _data;
51+
return _value;
4452
}
4553

4654
unsigned short
4755
BLEDescriptor::valueLength() const
4856
{
49-
return _data_len;
57+
return _value_length;
5058
}
5159

5260
unsigned char
5361
BLEDescriptor::operator[] (int offset) const
5462
{
55-
return _data[offset];
63+
return _value[offset];
5664
}
5765

5866
bool
@@ -66,8 +74,8 @@ BLEDescriptor::add(uint16_t serviceHandle)
6674

6775
desc.p_uuid = &uuid;
6876

69-
desc.p_value = _data;
70-
desc.length = _data_len;
77+
desc.p_value = _value;
78+
desc.length = _value_length;
7179

7280
// this class only supports read-only descriptors
7381
desc.perms.rd = GAP_SEC_MODE_1 | GAP_SEC_LEVEL_1;

libraries/CurieBle/src/BLEDescriptor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class BLEDescriptor : public BLEAttribute {
3636
*/
3737
BLEDescriptor(const char* uuid, const unsigned char value[], unsigned short valueLength);
3838

39+
virtual ~BLEDescriptor();
40+
3941
/**
4042
* Constructor for BLE Descriptor
4143
*
@@ -68,8 +70,8 @@ class BLEDescriptor : public BLEAttribute {
6870
friend BLEPeripheral;
6971

7072
private:
71-
uint8_t _data[BLE_MAX_ATTR_DATA_LEN];
72-
uint16_t _data_len;
73+
unsigned short _value_length;
74+
unsigned char* _value;
7375
};
7476

7577
#endif // _BLE_DESCRIPTOR_H_INCLUDED

0 commit comments

Comments
 (0)