Skip to content

Commit 098d4f3

Browse files
authored
Merge pull request #11751 from Kolcha/ble-cleanup
BLE: `BLECharacteristic::setValue()` cleanup and optimization
2 parents 2578c56 + f801bdd commit 098d4f3

File tree

8 files changed

+35
-50
lines changed

8 files changed

+35
-50
lines changed

libraries/BLE/src/BLE2901.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ BLE2901::BLE2901() : BLEDescriptor(BLEUUID((uint16_t)BLE2901_UUID)) {}
4747
/**
4848
* @brief Set the Characteristic User Description
4949
*/
50-
void BLE2901::setDescription(String userDesc) {
50+
void BLE2901::setDescription(const String &userDesc) {
5151
if (userDesc.length() > ESP_GATT_MAX_ATTR_LEN) {
5252
log_e("Size %d too large, must be no bigger than %d", userDesc.length(), ESP_GATT_MAX_ATTR_LEN);
5353
return;

libraries/BLE/src/BLE2901.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BLE2901 : public BLEDescriptor {
4040
***************************************************************************/
4141

4242
BLE2901();
43-
void setDescription(String desc);
43+
void setDescription(const String &desc);
4444
}; // BLE2901
4545

4646
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void BLECharacteristic::setReadProperty(bool value) {
346346
* @param [in] data The data to set for the characteristic.
347347
* @param [in] length The length of the data in bytes.
348348
*/
349-
void BLECharacteristic::setValue(uint8_t *data, size_t length) {
349+
void BLECharacteristic::setValue(const uint8_t *data, size_t length) {
350350
// The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
351351
// "VERBOSE". As it is quite CPU intensive, it is much better to not call it if not needed.
352352
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
@@ -371,43 +371,28 @@ void BLECharacteristic::setValue(uint8_t *data, size_t length) {
371371
* @param [in] Set the value of the characteristic.
372372
* @return N/A.
373373
*/
374-
void BLECharacteristic::setValue(String value) {
375-
setValue((uint8_t *)(value.c_str()), value.length());
374+
void BLECharacteristic::setValue(const String &value) {
375+
setValue(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
376376
} // setValue
377377

378-
void BLECharacteristic::setValue(uint16_t &data16) {
379-
uint8_t temp[2];
380-
temp[0] = data16;
381-
temp[1] = data16 >> 8;
382-
setValue(temp, 2);
378+
void BLECharacteristic::setValue(uint16_t data16) {
379+
setValue(reinterpret_cast<const uint8_t *>(&data16), sizeof(data16));
383380
} // setValue
384381

385-
void BLECharacteristic::setValue(uint32_t &data32) {
386-
uint8_t temp[4];
387-
temp[0] = data32;
388-
temp[1] = data32 >> 8;
389-
temp[2] = data32 >> 16;
390-
temp[3] = data32 >> 24;
391-
setValue(temp, 4);
382+
void BLECharacteristic::setValue(uint32_t data32) {
383+
setValue(reinterpret_cast<const uint8_t *>(&data32), sizeof(data32));
392384
} // setValue
393385

394-
void BLECharacteristic::setValue(int &data32) {
395-
uint8_t temp[4];
396-
temp[0] = data32;
397-
temp[1] = data32 >> 8;
398-
temp[2] = data32 >> 16;
399-
temp[3] = data32 >> 24;
400-
setValue(temp, 4);
386+
void BLECharacteristic::setValue(int data32) {
387+
setValue(reinterpret_cast<const uint8_t *>(&data32), sizeof(data32));
401388
} // setValue
402389

403-
void BLECharacteristic::setValue(float &data32) {
404-
float temp = data32;
405-
setValue((uint8_t *)&temp, 4);
390+
void BLECharacteristic::setValue(float data32) {
391+
setValue(reinterpret_cast<const uint8_t *>(&data32), sizeof(data32));
406392
} // setValue
407393

408-
void BLECharacteristic::setValue(double &data64) {
409-
double temp = data64;
410-
setValue((uint8_t *)&temp, 8);
394+
void BLECharacteristic::setValue(double data64) {
395+
setValue(reinterpret_cast<const uint8_t *>(&data64), sizeof(data64));
411396
} // setValue
412397

413398
/**

libraries/BLE/src/BLECharacteristic.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ class BLECharacteristic {
184184
void indicate();
185185
void notify(bool is_notification = true);
186186
void setCallbacks(BLECharacteristicCallbacks *pCallbacks);
187-
void setValue(uint8_t *data, size_t size);
188-
void setValue(String value);
189-
void setValue(uint16_t &data16);
190-
void setValue(uint32_t &data32);
191-
void setValue(int &data32);
192-
void setValue(float &data32);
193-
void setValue(double &data64);
187+
void setValue(const uint8_t *data, size_t size);
188+
void setValue(const String &value);
189+
void setValue(uint16_t data16);
190+
void setValue(uint32_t data32);
191+
void setValue(int data32);
192+
void setValue(float data32);
193+
void setValue(double data64);
194194
String toString();
195195
uint16_t getHandle();
196196
void setAccessPermissions(uint8_t perm);

libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void BLEDescriptor::setHandle(uint16_t handle) {
181181
* @param [in] data The data to set for the descriptor.
182182
* @param [in] length The length of the data in bytes.
183183
*/
184-
void BLEDescriptor::setValue(uint8_t *data, size_t length) {
184+
void BLEDescriptor::setValue(const uint8_t *data, size_t length) {
185185
if (length > m_value.attr_max_len) {
186186
log_e("Size %d too large, must be no bigger than %d", length, m_value.attr_max_len);
187187
return;
@@ -203,8 +203,8 @@ void BLEDescriptor::setValue(uint8_t *data, size_t length) {
203203
* @brief Set the value of the descriptor.
204204
* @param [in] value The value of the descriptor in string form.
205205
*/
206-
void BLEDescriptor::setValue(String value) {
207-
setValue((uint8_t *)value.c_str(), value.length());
206+
void BLEDescriptor::setValue(const String &value) {
207+
setValue(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
208208
} // setValue
209209

210210
void BLEDescriptor::setAccessPermissions(uint8_t perm) {

libraries/BLE/src/BLEDescriptor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class BLEDescriptor {
9494

9595
void setAccessPermissions(uint8_t perm); // Set the permissions of the descriptor.
9696
void setCallbacks(BLEDescriptorCallbacks *pCallbacks); // Set callbacks to be invoked for the descriptor.
97-
void setValue(uint8_t *data, size_t size); // Set the value of the descriptor as a pointer to data.
98-
void setValue(String value); // Set the value of the descriptor as a data buffer.
97+
void setValue(const uint8_t *data, size_t size); // Set the value of the descriptor as a pointer to data.
98+
void setValue(const String &value); // Set the value of the descriptor as a data buffer.
9999

100100
String toString(); // Convert the descriptor to a string representation.
101101

libraries/BLE/src/BLEValue.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ BLEValue::BLEValue() {
3737
* The accumulation is a growing set of data that is added to until a commit or cancel.
3838
* @param [in] part A message part being added.
3939
*/
40-
void BLEValue::addPart(String part) {
40+
void BLEValue::addPart(const String &part) {
4141
log_v(">> addPart: length=%d", part.length());
4242
m_accumulation += part;
4343
} // addPart
@@ -48,7 +48,7 @@ void BLEValue::addPart(String part) {
4848
* @param [in] pData A message part being added.
4949
* @param [in] length The number of bytes being added.
5050
*/
51-
void BLEValue::addPart(uint8_t *pData, size_t length) {
51+
void BLEValue::addPart(const uint8_t *pData, size_t length) {
5252
log_v(">> addPart: length=%d", length);
5353
m_accumulation += String((char *)pData, length);
5454
} // addPart
@@ -121,7 +121,7 @@ void BLEValue::setReadOffset(uint16_t readOffset) {
121121
/**
122122
* @brief Set the current value.
123123
*/
124-
void BLEValue::setValue(String value) {
124+
void BLEValue::setValue(const String &value) {
125125
m_value = value;
126126
} // setValue
127127

@@ -130,7 +130,7 @@ void BLEValue::setValue(String value) {
130130
* @param [in] pData The data for the current value.
131131
* @param [in] The length of the new current value.
132132
*/
133-
void BLEValue::setValue(uint8_t *pData, size_t length) {
133+
void BLEValue::setValue(const uint8_t *pData, size_t length) {
134134
m_value = String((char *)pData, length);
135135
} // setValue
136136

libraries/BLE/src/BLEValue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ class BLEValue {
3434
***************************************************************************/
3535

3636
BLEValue();
37-
void addPart(String part);
38-
void addPart(uint8_t *pData, size_t length);
37+
void addPart(const String &part);
38+
void addPart(const uint8_t *pData, size_t length);
3939
void cancel();
4040
void commit();
4141
uint8_t *getData();
4242
size_t getLength();
4343
uint16_t getReadOffset();
4444
String getValue();
4545
void setReadOffset(uint16_t readOffset);
46-
void setValue(String value);
47-
void setValue(uint8_t *pData, size_t length);
46+
void setValue(const String &value);
47+
void setValue(const uint8_t *pData, size_t length);
4848

4949
private:
5050
/***************************************************************************

0 commit comments

Comments
 (0)