Skip to content

Commit 4bffc65

Browse files
committed
add check for characteristic max_len
1 parent 36cfbe0 commit 4bffc65

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

libraries/Bluefruit52Lib/examples/custom_hrm/custom_hrm.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ void setupHRM(void)
130130
// B6:7 = UINT16 - RR Internal (1/1024 second resolution)
131131
hrmc.setProperties(CHR_PROPS_NOTIFY);
132132
hrmc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
133-
hrmc.setMaxLen(8);
133+
hrmc.setMaxLen(2);
134134
hrmc.start();
135135

136136
// Set the characteristic to use 8-bit values, with the sensor connected and detected
137-
uint8_t hrmdata[2] = { 0b00000110, 0x40 };
137+
uint8_t hrmdata[2] = { 0b00000000, 0x40 };
138138
hrmc.write(hrmdata, 2);
139139

140140
// Configure the Body Sensor Location characteristic

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,12 @@ err_t BLECharacteristic::write(const char * str)
355355

356356
err_t BLECharacteristic::write(const uint8_t* data, int len, uint16_t offset)
357357
{
358+
// could not exceed max len
359+
uint16_t actual_len = min16(len, _max_len);
360+
358361
ble_gatts_value_t value =
359362
{
360-
.len = (uint16_t) len,
363+
.len = (uint16_t) actual_len,
361364
.offset = offset,
362365
.p_value = (uint8_t*) data
363366
};
@@ -388,7 +391,7 @@ err_t BLECharacteristic::write(uint8_t num)
388391

389392
bool BLECharacteristic::notifyEnabled(void)
390393
{
391-
VERIFY( _properties.notify );
394+
VERIFY( _properties.notify && Bluefruit.connected() );
392395
uint16_t cccd;
393396
ble_gatts_value_t value =
394397
{
@@ -409,14 +412,15 @@ err_t BLECharacteristic::notify(const uint8_t* data, int len, uint16_t offset)
409412
// Whether Txbuf available or not we still update the chars'value
410413
(void) Bluefruit.txbuf_get(100);
411414

412-
uint16_t len16 = (uint16_t) len;
415+
// could not exceed max len
416+
uint16_t actual_len = min16(len, _max_len);
413417

414418
ble_gatts_hvx_params_t hvx_params =
415419
{
416420
.handle = _handles.value_handle,
417421
.type = BLE_GATT_HVX_NOTIFICATION,
418422
.offset = offset,
419-
.p_len = &len16,
423+
.p_len = &actual_len,
420424
.p_data = (uint8_t*) data,
421425
};
422426

0 commit comments

Comments
 (0)