Skip to content

Commit 5e4f376

Browse files
committed
NimBLEAdvertisementData emit error on failure, remove magic numbers.
1 parent acf9a34 commit 5e4f376

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/NimBLEAdvertisementData.cpp

Lines changed: 23 additions & 18 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 (length + getPayload().size() > BLE_HS_ADV_MAX_SZ) {
4343
NIMBLE_LOGE(LOG_TAG, "Data length exceeded");
4444
return false;
4545
}
@@ -72,7 +72,7 @@ bool NimBLEAdvertisementData::setAppearance(uint16_t appearance) {
7272
data[1] = BLE_HS_ADV_TYPE_APPEARANCE;
7373
data[2] = appearance;
7474
data[3] = (appearance >> 8) & 0xFF;
75-
return addData(data, 4);
75+
return addData(data, sizeof(data));
7676
} // setAppearance
7777

7878
/**
@@ -98,7 +98,7 @@ bool NimBLEAdvertisementData::setFlags(uint8_t flag) {
9898
data[0] = 2;
9999
data[1] = BLE_HS_ADV_TYPE_FLAGS;
100100
data[2] = flag | BLE_HS_ADV_F_BREDR_UNSUP;
101-
return addData(data, 3);
101+
return addData(data, sizeof(data));
102102
} // setFlags
103103

104104
/**
@@ -114,7 +114,7 @@ bool NimBLEAdvertisementData::addTxPower() {
114114
# else
115115
data[2] = 0;
116116
# endif
117-
return addData(data, 3);
117+
return addData(data, sizeof(data));
118118
} // addTxPower
119119

120120
/**
@@ -138,7 +138,7 @@ bool NimBLEAdvertisementData::setPreferredParams(uint16_t minInterval, uint16_t
138138
data[3] = minInterval >> 8;
139139
data[4] = maxInterval;
140140
data[5] = maxInterval >> 8;
141-
return addData(data, 6);
141+
return addData(data, sizeof(data));
142142
} // setPreferredParams
143143

144144
/**
@@ -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);

0 commit comments

Comments
 (0)