Skip to content

Commit d307e00

Browse files
authored
Merge branch 'h2zero:master' into misc-ctx-cleanup
2 parents f9afdc9 + fa468d3 commit d307e00

10 files changed

+57
-31
lines changed

CHANGELOG.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [Unreleased]
4+
## [2.0.3] 2025-01-05
5+
6+
## Fixed
7+
- Unused variable warning when log level is below info.
8+
- Build error missing definition of CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT in platformio.
9+
- Race condition in `NimBLEScan` that can cause a crash due to heap corruption if `NimBLEScan::stop` is called from the `onResult` callback.
10+
- Advertisement data not set if scan response is enabled after the data is set.
11+
- `NimBLECharacteristic`/`NimBLEDescriptor` not able to update their values in the `onRead` callback.
12+
- Too short of a timeout being requested in NimBLE_Server example leading to frequent disconnects.
13+
14+
## Changed
15+
- `NimBLEHIDDevice` now allows for the same report ID in multiple input/output/feature reports.
516

617
## Added
718
- Config for custom log colors pre level.
19+
- Error logs in the case that NIMBLE_CPP_DEBUG_ASSERT is not defined.
20+
- Error logs when setting advertisement data fails.
21+
- Missing documentation in the migration guide about enabling automatic advertising on disconnect, which was disabled by default in 2.x.
822

923
## [2.0.2] 2024-12-21
1024

docs/1.x_to2.x_migration_guide.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ This returns a pointer to `const ble_addr_t` instead of a pointer to the address
6565
## Server
6666
- `NimBLEServer::disconnect` now returns `bool`, true = success, instead of `int` to be consistent with the rest of the library.
6767
- `NimBLEServerCallbacks::onMTUChanged` renamed to `NimBLEServerCallbacks::onMTUChange` to be consistent with the client callback.
68-
- `NimBLEServer::getPeerIDInfo` renamed to `NimBLEServer::getPeerInfoByHandle` to better describe it's use.
68+
- `NimBLEServer::getPeerIDInfo` renamed to `NimBLEServer::getPeerInfoByHandle` to better describe it's use.
69+
- Advertising is no longer automatically restarted when a peer disconnects, to re-enable this feature either call `NimBLEServer::advertiseOnDisconnect(true);` after creating the server or manually restart advertising in the `onDisconnect` callback.
6970
<br/>
7071

7172
### Services

docs/Doxyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ PROJECT_NAME = esp-nimble-cpp
4848
# could be handy for archiving the generated documentation or if some version
4949
# control system is used.
5050

51-
PROJECT_NUMBER = 2.0.2
51+
PROJECT_NUMBER = 2.0.3
5252

5353
# Using the PROJECT_BRIEF tag one can provide an optional one line description
5454
# for a project that appears at the top of each page and should give viewer a

examples/NimBLE_Server/main/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ServerCallbacks : public NimBLEServerCallbacks {
2626
* Latency: number of intervals allowed to skip.
2727
* Timeout: 10 millisecond increments.
2828
*/
29-
pServer->updateConnParams(connInfo.getConnHandle(), 24, 48, 0, 18);
29+
pServer->updateConnParams(connInfo.getConnHandle(), 24, 48, 0, 180);
3030
}
3131

3232
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override {

idf_component.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## IDF Component Manager Manifest File
2-
version: "2.0.2"
2+
version: "2.0.3"
33
license: "Apache-2.0"
44
description: "C++ wrapper for the NimBLE BLE stack"
55
url: "https://github.com/h2zero/esp-nimble-cpp"

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esp-nimble-cpp",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "C++ wrapper for the NimBLE BLE stack",
55
"keywords": [
66
"BLE",

src/NimBLEAdvertisementData.cpp

+19-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static const char* LOG_TAG = "NimBLEAdvertisementData";
3939
* @param [in] length The size of data to be added to the payload.
4040
*/
4141
bool NimBLEAdvertisementData::addData(const uint8_t* data, size_t length) {
42-
if ((m_payload.size() + length) > BLE_HS_ADV_MAX_SZ) {
42+
if (m_payload.size() + length > BLE_HS_ADV_MAX_SZ) {
4343
NIMBLE_LOGE(LOG_TAG, "Data length exceeded");
4444
return false;
4545
}
@@ -159,6 +159,7 @@ bool NimBLEAdvertisementData::addServiceUUID(const NimBLEUUID& serviceUUID) {
159159
type = BLE_HS_ADV_TYPE_COMP_UUIDS128;
160160
break;
161161
default:
162+
NIMBLE_LOGE(LOG_TAG, "Cannot add UUID, invalid size!");
162163
return false;
163164
}
164165

@@ -169,10 +170,11 @@ bool NimBLEAdvertisementData::addServiceUUID(const NimBLEUUID& serviceUUID) {
169170
}
170171

171172
if (length + getPayload().size() > BLE_HS_ADV_MAX_SZ) {
173+
NIMBLE_LOGE(LOG_TAG, "Cannot add UUID, data length exceeded!");
172174
return false;
173175
}
174176

175-
uint8_t data[31];
177+
uint8_t data[BLE_HS_ADV_MAX_SZ];
176178
const uint8_t* uuid = serviceUUID.getValue();
177179
if (dataLoc == -1) {
178180
data[0] = 1 + bytes;
@@ -214,6 +216,7 @@ bool NimBLEAdvertisementData::removeServiceUUID(const NimBLEUUID& serviceUUID) {
214216
type = BLE_HS_ADV_TYPE_COMP_UUIDS128;
215217
break;
216218
default:
219+
NIMBLE_LOGE(LOG_TAG, "Cannot remove UUID, invalid size!");
217220
return false;
218221
}
219222

@@ -266,12 +269,12 @@ bool NimBLEAdvertisementData::removeServices() {
266269
* @return True if successful.
267270
*/
268271
bool NimBLEAdvertisementData::setManufacturerData(const uint8_t* data, size_t length) {
269-
if (length > 29) {
272+
if (length > BLE_HS_ADV_MAX_FIELD_SZ) {
270273
NIMBLE_LOGE(LOG_TAG, "MFG data too long");
271274
return false;
272275
}
273276

274-
uint8_t mdata[31];
277+
uint8_t mdata[BLE_HS_ADV_MAX_SZ];
275278
mdata[0] = length + 1;
276279
mdata[1] = BLE_HS_ADV_TYPE_MFG_DATA;
277280
memcpy(&mdata[2], data, length);
@@ -302,12 +305,12 @@ bool NimBLEAdvertisementData::setManufacturerData(const std::vector<uint8_t>& da
302305
* @return True if successful.
303306
*/
304307
bool NimBLEAdvertisementData::setURI(const std::string& uri) {
305-
if (uri.length() > 29) {
308+
if (uri.length() > BLE_HS_ADV_MAX_FIELD_SZ) {
306309
NIMBLE_LOGE(LOG_TAG, "URI too long");
307310
return false;
308311
}
309312

310-
uint8_t data[31];
313+
uint8_t data[BLE_HS_ADV_MAX_SZ];
311314
uint8_t length = 2 + uri.length();
312315
data[0] = length - 1;
313316
data[1] = BLE_HS_ADV_TYPE_URI;
@@ -324,16 +327,16 @@ bool NimBLEAdvertisementData::setURI(const std::string& uri) {
324327
* @return True if successful.
325328
*/
326329
bool NimBLEAdvertisementData::setName(const std::string& name, bool isComplete) {
327-
if (name.length() > 29) {
330+
if (name.length() > BLE_HS_ADV_MAX_FIELD_SZ) {
328331
NIMBLE_LOGE(LOG_TAG, "Name too long - truncating");
329332
isComplete = false;
330333
}
331334

332-
uint8_t data[31];
333-
uint8_t length = 2 + std::min<uint8_t>(name.length(), 29);
335+
uint8_t data[BLE_HS_ADV_MAX_SZ];
336+
uint8_t length = 2 + std::min<uint8_t>(name.length(), BLE_HS_ADV_MAX_FIELD_SZ);
334337
data[0] = length - 1;
335338
data[1] = isComplete ? BLE_HS_ADV_TYPE_COMP_NAME : BLE_HS_ADV_TYPE_INCOMP_NAME;
336-
memcpy(&data[2], name.c_str(), std::min<uint8_t>(name.length(), 29));
339+
memcpy(&data[2], name.c_str(), std::min<uint8_t>(name.length(), BLE_HS_ADV_MAX_FIELD_SZ));
337340
return addData(data, length);
338341
} // setName
339342

@@ -411,14 +414,14 @@ bool NimBLEAdvertisementData::setPartialServices32(const std::vector<NimBLEUUID>
411414
bool NimBLEAdvertisementData::setServices(bool complete, uint8_t size, const std::vector<NimBLEUUID>& uuids) {
412415
uint8_t bytes = size / 8;
413416
uint8_t length = 2; // start with 2 for length + type bytes
414-
uint8_t data[31];
417+
uint8_t data[BLE_HS_ADV_MAX_SZ];
415418

416419
for (const auto& uuid : uuids) {
417420
if (uuid.bitSize() != size) {
418421
NIMBLE_LOGE(LOG_TAG, "Service UUID(%d) invalid", size);
419422
continue;
420423
} else {
421-
if (length + bytes >= 31) {
424+
if (length + bytes >= BLE_HS_ADV_MAX_SZ) {
422425
NIMBLE_LOGW(LOG_TAG, "Too many services - truncating");
423426
complete = false;
424427
break;
@@ -441,6 +444,7 @@ bool NimBLEAdvertisementData::setServices(bool complete, uint8_t size, const std
441444
data[1] = (complete ? BLE_HS_ADV_TYPE_COMP_UUIDS128 : BLE_HS_ADV_TYPE_INCOMP_UUIDS128);
442445
break;
443446
default:
447+
NIMBLE_LOGE(LOG_TAG, "Cannot set services, invalid size!");
444448
return false;
445449
}
446450

@@ -458,7 +462,7 @@ bool NimBLEAdvertisementData::setServices(bool complete, uint8_t size, const std
458462
bool NimBLEAdvertisementData::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length) {
459463
uint8_t uuidBytes = uuid.bitSize() / 8;
460464
uint8_t sDataLen = 2 + uuidBytes + length;
461-
if (sDataLen > 31) {
465+
if (sDataLen > BLE_HS_ADV_MAX_SZ) {
462466
NIMBLE_LOGE(LOG_TAG, "Service Data too long");
463467
return false;
464468
}
@@ -475,6 +479,7 @@ bool NimBLEAdvertisementData::setServiceData(const NimBLEUUID& uuid, const uint8
475479
type = BLE_HS_ADV_TYPE_SVC_DATA_UUID128;
476480
break;
477481
default:
482+
NIMBLE_LOGE(LOG_TAG, "Cannot set service data, invalid size!");
478483
return false;
479484
}
480485

@@ -483,7 +488,7 @@ bool NimBLEAdvertisementData::setServiceData(const NimBLEUUID& uuid, const uint8
483488
return true;
484489
}
485490

486-
uint8_t sData[31];
491+
uint8_t sData[BLE_HS_ADV_MAX_SZ];
487492
sData[0] = uuidBytes + length + 1;
488493
sData[1] = type;
489494
memcpy(&sData[2], uuid.getValue(), uuidBytes);

src/NimBLEAdvertising.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ bool NimBLEAdvertising::refreshAdvertisingData() {
408408
* @return True if the service was added successfully.
409409
*/
410410
bool NimBLEAdvertising::addServiceUUID(const NimBLEUUID& serviceUUID) {
411-
if (!m_advData.addServiceUUID(serviceUUID) && m_scanResp) {
411+
if (!m_advData.addServiceUUID(serviceUUID)) {
412412
if (!m_scanData.addServiceUUID(serviceUUID)) {
413413
return false;
414414
}
@@ -466,7 +466,7 @@ bool NimBLEAdvertising::removeServices() {
466466
* @return True if the appearance was set successfully.
467467
*/
468468
bool NimBLEAdvertising::setAppearance(uint16_t appearance) {
469-
if (!m_advData.setAppearance(appearance) && m_scanResp) {
469+
if (!m_advData.setAppearance(appearance)) {
470470
if (!m_scanData.setAppearance(appearance)) {
471471
return false;
472472
}
@@ -484,7 +484,7 @@ bool NimBLEAdvertising::setAppearance(uint16_t appearance) {
484484
* @details Range = 0x0006(7.5ms) to 0x0C80(4000ms), values not within the range will be limited to this range.
485485
*/
486486
bool NimBLEAdvertising::setPreferredParams(uint16_t minInterval, uint16_t maxInterval) {
487-
if (!m_advData.setPreferredParams(minInterval, maxInterval) && m_scanResp) {
487+
if (!m_advData.setPreferredParams(minInterval, maxInterval)) {
488488
if (!m_scanData.setPreferredParams(minInterval, maxInterval)) {
489489
return false;
490490
}
@@ -499,7 +499,7 @@ bool NimBLEAdvertising::setPreferredParams(uint16_t minInterval, uint16_t maxInt
499499
* @return True if the transmission power level was added successfully.
500500
*/
501501
bool NimBLEAdvertising::addTxPower() {
502-
if (!m_advData.addTxPower() && m_scanResp) {
502+
if (!m_advData.addTxPower()) {
503503
if (!m_scanData.addTxPower()) {
504504
return false;
505505
}
@@ -537,7 +537,7 @@ bool NimBLEAdvertising::setName(const std::string& name) {
537537
* @return True if the manufacturer data was set successfully.
538538
*/
539539
bool NimBLEAdvertising::setManufacturerData(const uint8_t* data, size_t length) {
540-
if (!m_advData.setManufacturerData(data, length) && m_scanResp) {
540+
if (!m_advData.setManufacturerData(data, length)) {
541541
if (!m_scanData.setManufacturerData(data, length)) {
542542
return false;
543543
}
@@ -571,7 +571,7 @@ bool NimBLEAdvertising::setManufacturerData(const std::vector<uint8_t>& data) {
571571
* @return True if the URI was set successfully.
572572
*/
573573
bool NimBLEAdvertising::setURI(const std::string& uri) {
574-
if (!m_advData.setURI(uri) && m_scanResp) {
574+
if (!m_advData.setURI(uri)) {
575575
if (!m_scanData.setURI(uri)) {
576576
return false;
577577
}
@@ -590,7 +590,7 @@ bool NimBLEAdvertising::setURI(const std::string& uri) {
590590
* @note If data length is 0 the service data will not be advertised.
591591
*/
592592
bool NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length) {
593-
if (!m_advData.setServiceData(uuid, data, length) && m_scanResp) {
593+
if (!m_advData.setServiceData(uuid, data, length)) {
594594
if (!m_scanData.setServiceData(uuid, data, length)) {
595595
return false;
596596
}

src/NimBLEExtAdvertising.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ bool NimBLEExtAdvertisement::addServiceUUID(const NimBLEUUID& serviceUUID) {
701701
type = BLE_HS_ADV_TYPE_COMP_UUIDS128;
702702
break;
703703
default:
704+
NIMBLE_LOGE(LOG_TAG, "Cannot add UUID, invalid size!");
704705
return false;
705706
}
706707

@@ -711,10 +712,11 @@ bool NimBLEExtAdvertisement::addServiceUUID(const NimBLEUUID& serviceUUID) {
711712
}
712713

713714
if (length + getDataSize() > CONFIG_BT_NIMBLE_MAX_EXT_ADV_DATA_LEN) {
715+
NIMBLE_LOGE(LOG_TAG, "Cannot add UUID, data length exceeded!");
714716
return false;
715717
}
716718

717-
uint8_t data[31];
719+
uint8_t data[BLE_HS_ADV_MAX_SZ];
718720
const uint8_t* uuid = serviceUUID.getValue();
719721
if (dataLoc == -1) {
720722
data[0] = 1 + bytes;
@@ -756,6 +758,7 @@ bool NimBLEExtAdvertisement::removeServiceUUID(const NimBLEUUID& serviceUUID) {
756758
type = BLE_HS_ADV_TYPE_COMP_UUIDS128;
757759
break;
758760
default:
761+
NIMBLE_LOGE(LOG_TAG, "Cannot remove UUID, invalid size!");
759762
return false;
760763
}
761764

@@ -878,6 +881,7 @@ bool NimBLEExtAdvertisement::setServices(bool complete, uint8_t size, const std:
878881
header[1] = complete ? BLE_HS_ADV_TYPE_COMP_UUIDS128 : BLE_HS_ADV_TYPE_INCOMP_UUIDS128;
879882
break;
880883
default:
884+
NIMBLE_LOGE(LOG_TAG, "Cannot set services, invalid size!");
881885
return false;
882886
}
883887

@@ -932,6 +936,7 @@ bool NimBLEExtAdvertisement::setServiceData(const NimBLEUUID& uuid, const uint8_
932936
type = BLE_HS_ADV_TYPE_SVC_DATA_UUID128;
933937
break;
934938
default:
939+
NIMBLE_LOGE(LOG_TAG, "Cannot set service data, invalid size!");
935940
return false;
936941
}
937942

src/NimBLEServer.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,9 @@ int NimBLEServer::handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_
611611
NIMBLE_LOGD(LOG_TAG,
612612
"Gatt %s event",
613613
(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR || ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) ? "Read" : "Write");
614-
auto pAtt = static_cast<NimBLELocalValueAttribute*>(arg);
615-
auto val = pAtt->getAttVal();
614+
auto pAtt = static_cast<NimBLELocalValueAttribute*>(arg);
615+
const NimBLEAttValue& val = pAtt->getAttVal();
616+
616617
NimBLEConnInfo peerInfo{};
617618
ble_gap_conn_find(connHandle, &peerInfo.m_desc);
618619

@@ -623,7 +624,7 @@ int NimBLEServer::handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_
623624
if (connHandle != BLE_HS_CONN_HANDLE_NONE) {
624625
// If the packet header is only 8 bytes then this is a follow up of a long read
625626
// so we don't want to call the onRead() callback again.
626-
if (ctxt->om->om_pkthdr_len > 8 || pAtt->getAttVal().size() <= (ble_att_mtu(connHandle) - 3)) {
627+
if (ctxt->om->om_pkthdr_len > 8 || val.size() <= (ble_att_mtu(connHandle) - 3)) {
627628
pAtt->readEvent(peerInfo);
628629
}
629630
}

0 commit comments

Comments
 (0)