Skip to content

Commit 6a2e725

Browse files
thekurtovich2zero
authored andcommitted
NimBLEAdvertisementData emit error on failure, remove magic numbers.
1 parent ec4e64c commit 6a2e725

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/NimBLEAdvertisementData.cpp

Lines changed: 19 additions & 14 deletions
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/NimBLEExtAdvertising.cpp

Lines changed: 6 additions & 1 deletion
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

0 commit comments

Comments
 (0)