Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a22ec4a

Browse files
romansavrulinme-no-dev
authored andcommittedJul 9, 2019
Reduce flash usage up to 214k in one click (#2929)
* std::stringstream -> std::string * Fix small issues * Small fix 2
1 parent 20498cf commit a22ec4a

20 files changed

+238
-214
lines changed
 

‎libraries/BLE/src/BLEAddress.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <iomanip>
1414
#include <string.h>
1515
#include <stdio.h>
16+
#include <malloc.h>
1617
#ifdef ARDUINO_ARCH_ESP32
1718
#include "esp32-hal-log.h"
1819
#endif
@@ -83,13 +84,11 @@ esp_bd_addr_t *BLEAddress::getNative() {
8384
* @return The string representation of the address.
8485
*/
8586
std::string BLEAddress::toString() {
86-
std::stringstream stream;
87-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[0] << ':';
88-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[1] << ':';
89-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[2] << ':';
90-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[3] << ':';
91-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[4] << ':';
92-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[5];
93-
return stream.str();
87+
auto size = 18;
88+
char *res = (char*)malloc(size);
89+
snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
90+
std::string ret(res);
91+
free(res);
92+
return ret;
9493
} // toString
9594
#endif

‎libraries/BLE/src/BLEAdvertisedDevice.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,23 +484,29 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
484484
* @return A string representation of this device.
485485
*/
486486
std::string BLEAdvertisedDevice::toString() {
487-
std::stringstream ss;
488-
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
487+
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
489488
if (haveAppearance()) {
490-
ss << ", appearance: " << getAppearance();
489+
char val[6];
490+
snprintf(val, sizeof(val), "%d", getAppearance());
491+
res += ", appearance: ";
492+
res += val;
491493
}
492494
if (haveManufacturerData()) {
493495
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
494-
ss << ", manufacturer data: " << pHex;
496+
res += ", manufacturer data: ";
497+
res += pHex;
495498
free(pHex);
496499
}
497500
if (haveServiceUUID()) {
498-
ss << ", serviceUUID: " << getServiceUUID().toString();
501+
res += ", serviceUUID: " + getServiceUUID().toString();
499502
}
500503
if (haveTXPower()) {
501-
ss << ", txPower: " << (int)getTXPower();
504+
char val[4];
505+
snprintf(val, sizeof(val), "%d", getTXPower());
506+
res += ", txPower: ";
507+
res += val;
502508
}
503-
return ss.str();
509+
return res;
504510
} // toString
505511

506512
uint8_t* BLEAdvertisedDevice::getPayload() {

‎libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -717,17 +717,18 @@ void BLECharacteristic::setWriteProperty(bool value) {
717717
* @return A string representation of the characteristic.
718718
*/
719719
std::string BLECharacteristic::toString() {
720-
std::stringstream stringstream;
721-
stringstream << std::hex << std::setfill('0');
722-
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle;
723-
stringstream << " " <<
724-
((m_properties & ESP_GATT_CHAR_PROP_BIT_READ) ? "Read " : "") <<
725-
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE) ? "Write " : "") <<
726-
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) ? "WriteNoResponse " : "") <<
727-
((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST) ? "Broadcast " : "") <<
728-
((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY) ? "Notify " : "") <<
729-
((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE) ? "Indicate " : "");
730-
return stringstream.str();
720+
std::string res = "UUID: " + m_bleUUID.toString() + ", handle : 0x";
721+
char hex[5];
722+
snprintf(hex, sizeof(hex), "%04x", m_handle);
723+
res += hex;
724+
res += " ";
725+
if (m_properties & ESP_GATT_CHAR_PROP_BIT_READ) res += "Read ";
726+
if (m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE) res += "Write ";
727+
if (m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) res += "WriteNoResponse ";
728+
if (m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST) res += "Broadcast ";
729+
if (m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY) res += "Notify ";
730+
if (m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE) res += "Indicate ";
731+
return res;
731732
} // toString
732733

733734

‎libraries/BLE/src/BLECharacteristicMap.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,18 @@ void BLECharacteristicMap::setByUUID(BLECharacteristic* pCharacteristic, BLEUUID
116116
* @return A string representation of the characteristic map.
117117
*/
118118
std::string BLECharacteristicMap::toString() {
119-
std::stringstream stringStream;
120-
stringStream << std::hex << std::setfill('0');
119+
std::string res;
121120
int count = 0;
121+
char hex[5];
122122
for (auto &myPair: m_uuidMap) {
123-
if (count > 0) {
124-
stringStream << "\n";
125-
}
123+
if (count > 0) {res += "\n";}
124+
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
126125
count++;
127-
stringStream << "handle: 0x" << std::setw(2) << myPair.first->getHandle() << ", uuid: " + myPair.first->getUUID().toString();
126+
res += "handle: 0x";
127+
res += hex;
128+
res += ", uuid: " + myPair.first->getUUID().toString();
128129
}
129-
return stringStream.str();
130+
return res;
130131
} // toString
131132

132133

‎libraries/BLE/src/BLEClient.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,13 @@ uint16_t BLEClient::getMTU() {
515515
* @return A string representation of this client.
516516
*/
517517
std::string BLEClient::toString() {
518-
std::ostringstream ss;
519-
ss << "peer address: " << m_peerAddress.toString();
520-
ss << "\nServices:\n";
518+
std::string res = "peer address: " + m_peerAddress.toString();
519+
res += "\nServices:\n";
521520
for (auto &myPair : m_servicesMap) {
522-
ss << myPair.second->toString() << "\n";
521+
res += myPair.second->toString() + "\n";
523522
// myPair.second is the value
524523
}
525-
return ss.str();
524+
return res;
526525
} // toString
527526

528527

‎libraries/BLE/src/BLEClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <string.h>
1616
#include <map>
1717
#include <string>
18-
#include "BLEExceptions.h"
18+
//#include "BLEExceptions.h"
1919
#include "BLERemoteService.h"
2020
#include "BLEService.h"
2121
#include "BLEAddress.h"

‎libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ void BLEDescriptor::setAccessPermissions(esp_gatt_perm_t perm) {
255255
* @return A string representation of the descriptor.
256256
*/
257257
std::string BLEDescriptor::toString() {
258-
std::stringstream stringstream;
259-
stringstream << std::hex << std::setfill('0');
260-
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle;
261-
return stringstream.str();
258+
char hex[5];
259+
snprintf(hex, sizeof(hex), "%04x", m_handle);
260+
std::string res = "UUID: " + m_bleUUID.toString() + ", handle: 0x" + hex;
261+
return res;
262262
} // toString
263263

264264

‎libraries/BLE/src/BLEDescriptorMap.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,18 @@ void BLEDescriptorMap::setByHandle(uint16_t handle, BLEDescriptor* pDescriptor)
9090
* @return A string representation of the descriptor map.
9191
*/
9292
std::string BLEDescriptorMap::toString() {
93-
std::stringstream stringStream;
94-
stringStream << std::hex << std::setfill('0');
93+
std::string res;
94+
char hex[5];
9595
int count = 0;
9696
for (auto &myPair : m_uuidMap) {
97-
if (count > 0) {
98-
stringStream << "\n";
99-
}
97+
if (count > 0) {res += "\n";}
98+
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
10099
count++;
101-
stringStream << "handle: 0x" << std::setw(2) << myPair.first->getHandle() << ", uuid: " + myPair.first->getUUID().toString();
100+
res += "handle: 0x";
101+
res += hex;
102+
res += ", uuid: " + myPair.first->getUUID().toString();
102103
}
103-
return stringStream.str();
104+
return res;
104105
} // toString
105106

106107

‎libraries/BLE/src/BLEDevice.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,8 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
479479
* @return A string representation of the nature of this device.
480480
*/
481481
/* STATIC */ std::string BLEDevice::toString() {
482-
std::ostringstream oss;
483-
oss << "BD Address: " << getAddress().toString();
484-
return oss.str();
482+
std::string res = "BD Address: " + getAddress().toString();
483+
return res;
485484
} // toString
486485

487486

‎libraries/BLE/src/BLEEddystoneTLM.cpp

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
99
#include <string.h>
10-
#include <sstream>
10+
#include <stdio.h>
1111
#include "esp32-hal-log.h"
1212
#include "BLEEddystoneTLM.h"
1313

@@ -54,62 +54,44 @@ uint32_t BLEEddystoneTLM::getTime() {
5454
} // getTime
5555

5656
std::string BLEEddystoneTLM::toString() {
57-
std::stringstream ss;
58-
std::string out = "";
59-
uint32_t rawsec;
60-
ss << "Version ";
61-
ss << std::dec << m_eddystoneData.version;
62-
ss << "\n";
63-
64-
ss << "Battery Voltage ";
65-
ss << std::dec << ENDIAN_CHANGE_U16(m_eddystoneData.volt);
66-
ss << " mV\n";
67-
68-
ss << "Temperature ";
69-
ss << (float) m_eddystoneData.temp;
70-
ss << " °C\n";
71-
72-
ss << "Adv. Count ";
73-
ss << std::dec << ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
74-
75-
ss << "\n";
76-
77-
ss << "Time ";
78-
79-
rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
80-
std::stringstream buffstream;
81-
buffstream << "0000";
82-
buffstream << std::dec << rawsec / 864000;
83-
std::string buff = buffstream.str();
84-
85-
ss << buff.substr(buff.length() - 4, buff.length());
86-
ss << ".";
87-
88-
buffstream.str("");
89-
buffstream.clear();
90-
buffstream << "00";
91-
buffstream << std::dec << (rawsec / 36000) % 24;
92-
buff = buffstream.str();
93-
ss << buff.substr(buff.length()-2, buff.length());
94-
ss << ":";
95-
96-
buffstream.str("");
97-
buffstream.clear();
98-
buffstream << "00";
99-
buffstream << std::dec << (rawsec / 600) % 60;
100-
buff = buffstream.str();
101-
ss << buff.substr(buff.length() - 2, buff.length());
102-
ss << ":";
103-
104-
buffstream.str("");
105-
buffstream.clear();
106-
buffstream << "00";
107-
buffstream << std::dec << (rawsec / 10) % 60;
108-
buff = buffstream.str();
109-
ss << buff.substr(buff.length() - 2, buff.length());
110-
ss << "\n";
111-
112-
return ss.str();
57+
std::string out = "";
58+
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
59+
char val[6];
60+
61+
out += "Version " + m_eddystoneData.version;
62+
out += "\n";
63+
out += "Battery Voltage " + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
64+
out += " mV\n";
65+
66+
out += "Temperature ";
67+
snprintf(val, sizeof(val), "%d", m_eddystoneData.temp);
68+
out += val;
69+
out += ".0 °C\n";
70+
71+
out += "Adv. Count ";
72+
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
73+
out += val;
74+
out += "\n";
75+
76+
out += "Time ";
77+
78+
snprintf(val, sizeof(val), "%04d", rawsec / 864000);
79+
out += val;
80+
out += ".";
81+
82+
snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24);
83+
out += val;
84+
out += ":";
85+
86+
snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60);
87+
out += val;
88+
out += ":";
89+
90+
snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60);
91+
out += val;
92+
out += "\n";
93+
94+
return out;
11395
} // toString
11496

11597
/**

‎libraries/BLE/src/BLEExceptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
* Author: kolban
66
*/
77

8-
#include "BLEExceptions.h"
8+
//#include "BLEExceptions.h"
99

‎libraries/BLE/src/BLERemoteCharacteristic.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <esp_err.h>
1515

1616
#include <sstream>
17-
#include "BLEExceptions.h"
17+
//#include "BLEExceptions.h"
1818
#include "BLEUtils.h"
1919
#include "GeneralUtils.h"
2020
#include "BLERemoteDescriptor.h"
@@ -400,7 +400,8 @@ std::string BLERemoteCharacteristic::readValue() {
400400
// Check to see that we are connected.
401401
if (!getRemoteService()->getClient()->isConnected()) {
402402
log_e("Disconnected");
403-
throw BLEDisconnectedException();
403+
// throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
404+
return std::string();
404405
}
405406

406407
m_semaphoreReadCharEvt.take("readValue");
@@ -501,11 +502,16 @@ void BLERemoteCharacteristic::removeDescriptors() {
501502
* @return a String representation.
502503
*/
503504
std::string BLERemoteCharacteristic::toString() {
504-
std::ostringstream ss;
505-
ss << "Characteristic: uuid: " << m_uuid.toString() <<
506-
", handle: " << getHandle() << " 0x" << std::hex << getHandle() <<
507-
", props: " << BLEUtils::characteristicPropertiesToString(m_charProp);
508-
return ss.str();
505+
std::string res = "Characteristic: uuid: " + m_uuid.toString();
506+
char val[6];
507+
res += ", handle: ";
508+
snprintf(val, sizeof(val), "%d", getHandle());
509+
res += val;
510+
res += " 0x";
511+
snprintf(val, sizeof(val), "%04x", getHandle());
512+
res += val;
513+
res += ", props: " + BLEUtils::characteristicPropertiesToString(m_charProp);
514+
return res;
509515
} // toString
510516

511517

@@ -546,7 +552,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp
546552
// Check to see that we are connected.
547553
if (!getRemoteService()->getClient()->isConnected()) {
548554
log_e("Disconnected");
549-
throw BLEDisconnectedException();
555+
// throw BLEDisconnectedException();
550556
}
551557

552558
m_semaphoreWriteCharEvt.take("writeValue");

‎libraries/BLE/src/BLERemoteDescriptor.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ std::string BLERemoteDescriptor::readValue() {
5555
// Check to see that we are connected.
5656
if (!getRemoteCharacteristic()->getRemoteService()->getClient()->isConnected()) {
5757
log_e("Disconnected");
58-
throw BLEDisconnectedException();
58+
// throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
59+
return std::string();
5960
}
6061

6162
m_semaphoreReadDescrEvt.take("readValue");
@@ -113,9 +114,12 @@ uint32_t BLERemoteDescriptor::readUInt32() {
113114
* @retun A string representation of this BLE Remote Descriptor.
114115
*/
115116
std::string BLERemoteDescriptor::toString() {
116-
std::stringstream ss;
117-
ss << "handle: " << getHandle() << ", uuid: " << getUUID().toString();
118-
return ss.str();
117+
char val[6];
118+
snprintf(val, sizeof(val), "%d", getHandle());
119+
std::string res = "handle: ";
120+
res += val;
121+
res += ", uuid: " + getUUID().toString();
122+
return res;
119123
} // toString
120124

121125

@@ -130,7 +134,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
130134
// Check to see that we are connected.
131135
if (!getRemoteCharacteristic()->getRemoteService()->getClient()->isConnected()) {
132136
log_e("Disconnected");
133-
throw BLEDisconnectedException();
137+
// throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
138+
return;
134139
}
135140

136141
esp_err_t errRc = ::esp_ble_gattc_write_char_descr(

‎libraries/BLE/src/BLERemoteService.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,25 @@ void BLERemoteService::setValue(BLEUUID characteristicUuid, std::string value) {
317317
* @return A string representation of this remote service.
318318
*/
319319
std::string BLERemoteService::toString() {
320-
std::ostringstream ss;
321-
ss << "Service: uuid: " + m_uuid.toString();
322-
ss << ", start_handle: " << std::dec << m_startHandle << " 0x" << std::hex << m_startHandle <<
323-
", end_handle: " << std::dec << m_endHandle << " 0x" << std::hex << m_endHandle;
320+
std::string res = "Service: uuid: " + m_uuid.toString();
321+
char val[6];
322+
res += ", start_handle: ";
323+
snprintf(val, sizeof(val), "%d", m_startHandle);
324+
res += val;
325+
snprintf(val, sizeof(val), "%04x", m_startHandle);
326+
res += " 0x";
327+
res += val;
328+
res += ", end_handle: ";
329+
snprintf(val, sizeof(val), "%d", m_endHandle);
330+
res += val;
331+
snprintf(val, sizeof(val), "%04x", m_endHandle);
332+
res += " 0x";
333+
res += val;
324334
for (auto &myPair : m_characteristicMap) {
325-
ss << "\n" << myPair.second->toString();
335+
res += "\n" + myPair.second->toString();
326336
// myPair.second is the value
327337
}
328-
return ss.str();
338+
return res;
329339
} // toString
330340

331341

‎libraries/BLE/src/BLEService.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,12 @@ BLECharacteristic* BLEService::getCharacteristic(BLEUUID uuid) {
381381
* @return A string representation of this service.
382382
*/
383383
std::string BLEService::toString() {
384-
std::stringstream stringStream;
385-
stringStream << "UUID: " << getUUID().toString() <<
386-
", handle: 0x" << std::hex << std::setfill('0') << std::setw(2) << getHandle();
387-
return stringStream.str();
384+
std::string res = "UUID: " + getUUID().toString();
385+
char hex[5];
386+
snprintf(hex, sizeof(hex), "%04x", getHandle());
387+
res += ", handle: 0x";
388+
res += hex;
389+
return res;
388390
} // toString
389391

390392

‎libraries/BLE/src/BLEServiceMap.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
9-
#include <sstream>
9+
#include <stdio.h>
1010
#include <iomanip>
1111
#include "BLEService.h"
1212

@@ -73,12 +73,15 @@ void BLEServiceMap::setByHandle(uint16_t handle, BLEService* service) {
7373
* @return A string representation of the service map.
7474
*/
7575
std::string BLEServiceMap::toString() {
76-
std::stringstream stringStream;
77-
stringStream << std::hex << std::setfill('0');
76+
std::string res;
77+
char hex[5];
7878
for (auto &myPair: m_handleMap) {
79-
stringStream << "handle: 0x" << std::setw(2) << myPair.first << ", uuid: " + myPair.second->getUUID().toString() << "\n";
79+
res += "handle: 0x";
80+
snprintf(hex, sizeof(hex), "%04x", myPair.first);
81+
res += hex;
82+
res += ", uuid: " + myPair.second->getUUID().toString() + "\n";
8083
}
81-
return stringStream.str();
84+
return res;
8285
} // toString
8386

8487
void BLEServiceMap::handleGATTServerEvent(

‎libraries/BLE/src/BLEUUID.cpp

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -349,51 +349,38 @@ BLEUUID BLEUUID::to128() {
349349
*/
350350
std::string BLEUUID::toString() {
351351
if (!m_valueSet) return "<NULL>"; // If we have no value, nothing to format.
352-
353352
// If the UUIDs are 16 or 32 bit, pad correctly.
354-
std::stringstream ss;
355353

356354
if (m_uuid.len == ESP_UUID_LEN_16) { // If the UUID is 16bit, pad correctly.
357-
ss << "0000" <<
358-
std::hex <<
359-
std::setfill('0') <<
360-
std::setw(4) <<
361-
m_uuid.uuid.uuid16 <<
362-
"-0000-1000-8000-00805f9b34fb";
363-
return ss.str(); // Return the string
355+
char hex[5];
356+
snprintf(hex, sizeof(hex), "%04x", m_uuid.uuid.uuid16);
357+
return std::string(hex) + "-0000-1000-8000-00805f9b34fb";
364358
} // End 16bit UUID
365359

366360
if (m_uuid.len == ESP_UUID_LEN_32) { // If the UUID is 32bit, pad correctly.
367-
ss << std::hex <<
368-
std::setfill('0') <<
369-
std::setw(8) <<
370-
m_uuid.uuid.uuid32 <<
371-
"-0000-1000-8000-00805f9b34fb";
372-
return ss.str(); // return the string
361+
char hex[9];
362+
snprintf(hex, sizeof(hex), "%08x", m_uuid.uuid.uuid32);
363+
return std::string(hex) + "-0000-1000-8000-00805f9b34fb";
373364
} // End 32bit UUID
374365

375366
// The UUID is not 16bit or 32bit which means that it is 128bit.
376367
//
377368
// UUID string format:
378369
// AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
379-
ss << std::hex << std::setfill('0') <<
380-
std::setw(2) << (int) m_uuid.uuid.uuid128[15] <<
381-
std::setw(2) << (int) m_uuid.uuid.uuid128[14] <<
382-
std::setw(2) << (int) m_uuid.uuid.uuid128[13] <<
383-
std::setw(2) << (int) m_uuid.uuid.uuid128[12] << "-" <<
384-
std::setw(2) << (int) m_uuid.uuid.uuid128[11] <<
385-
std::setw(2) << (int) m_uuid.uuid.uuid128[10] << "-" <<
386-
std::setw(2) << (int) m_uuid.uuid.uuid128[9] <<
387-
std::setw(2) << (int) m_uuid.uuid.uuid128[8] << "-" <<
388-
std::setw(2) << (int) m_uuid.uuid.uuid128[7] <<
389-
std::setw(2) << (int) m_uuid.uuid.uuid128[6] << "-" <<
390-
std::setw(2) << (int) m_uuid.uuid.uuid128[5] <<
391-
std::setw(2) << (int) m_uuid.uuid.uuid128[4] <<
392-
std::setw(2) << (int) m_uuid.uuid.uuid128[3] <<
393-
std::setw(2) << (int) m_uuid.uuid.uuid128[2] <<
394-
std::setw(2) << (int) m_uuid.uuid.uuid128[1] <<
395-
std::setw(2) << (int) m_uuid.uuid.uuid128[0];
396-
return ss.str();
370+
auto size = 35;
371+
char *hex = (char *)malloc(size);
372+
snprintf(hex, size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
373+
m_uuid.uuid.uuid128[15], m_uuid.uuid.uuid128[14],
374+
m_uuid.uuid.uuid128[13], m_uuid.uuid.uuid128[12],
375+
m_uuid.uuid.uuid128[11], m_uuid.uuid.uuid128[10],
376+
m_uuid.uuid.uuid128[9], m_uuid.uuid.uuid128[8],
377+
m_uuid.uuid.uuid128[7], m_uuid.uuid.uuid128[6],
378+
m_uuid.uuid.uuid128[5], m_uuid.uuid.uuid128[4],
379+
m_uuid.uuid.uuid128[3], m_uuid.uuid.uuid128[2],
380+
m_uuid.uuid.uuid128[1], m_uuid.uuid.uuid128[0]);
381+
std::string res(hex);
382+
free(hex);
383+
return res;
397384
} // toString
398385

399386
#endif /* CONFIG_BT_ENABLED */

‎libraries/BLE/src/BLEUtils.cpp

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -604,26 +604,32 @@ static const gattService_t g_gattServices[] = {
604604
* @return A string representation of characteristic properties.
605605
*/
606606
std::string BLEUtils::characteristicPropertiesToString(esp_gatt_char_prop_t prop) {
607-
std::stringstream stream;
608-
stream <<
609-
"broadcast: " << ((prop & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?"1":"0") <<
610-
", read: " << ((prop & ESP_GATT_CHAR_PROP_BIT_READ)?"1":"0") <<
611-
", write_nr: " << ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?"1":"0") <<
612-
", write: " << ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE)?"1":"0") <<
613-
", notify: " << ((prop & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?"1":"0") <<
614-
", indicate: " << ((prop & ESP_GATT_CHAR_PROP_BIT_INDICATE)?"1":"0") <<
615-
", auth: " << ((prop & ESP_GATT_CHAR_PROP_BIT_AUTH)?"1":"0");
616-
return stream.str();
607+
std::string res = "broadcast: ";
608+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?"1":"0");
609+
res += ", read: ";
610+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_READ)?"1":"0");
611+
res += ", write_nr: ";
612+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?"1":"0");
613+
res += ", write: ";
614+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE)?"1":"0");
615+
res += ", notify: ";
616+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?"1":"0");
617+
res += ", indicate: ";
618+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_INDICATE)?"1":"0");
619+
res += ", auth: ";
620+
res += ((prop & ESP_GATT_CHAR_PROP_BIT_AUTH)?"1":"0");
621+
return res;
617622
} // characteristicPropertiesToString
618623

619624
/**
620625
* @brief Convert an esp_gatt_id_t to a string.
621626
*/
622627
static std::string gattIdToString(esp_gatt_id_t gattId) {
623-
std::stringstream stream;
624-
stream << "uuid: " << BLEUUID(gattId.uuid).toString() << ", inst_id: " << (int)gattId.inst_id;
625-
//sprintf(buffer, "uuid: %s, inst_id: %d", uuidToString(gattId.uuid).c_str(), gattId.inst_id);
626-
return stream.str();
628+
std::string res = "uuid: " + BLEUUID(gattId.uuid).toString() + ", inst_id: ";
629+
char val[8];
630+
snprintf(val, sizeof(val), "%d", (int)gattId.inst_id);
631+
res += val;
632+
return res;
627633
} // gattIdToString
628634

629635

@@ -654,23 +660,23 @@ const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
654660
* @return std::string A string representation of the advertising flags.
655661
*/
656662
std::string BLEUtils::adFlagsToString(uint8_t adFlags) {
657-
std::stringstream ss;
663+
std::string res;
658664
if (adFlags & (1 << 0)) {
659-
ss << "[LE Limited Discoverable Mode] ";
665+
res += "[LE Limited Discoverable Mode] ";
660666
}
661667
if (adFlags & (1 << 1)) {
662-
ss << "[LE General Discoverable Mode] ";
668+
res += "[LE General Discoverable Mode] ";
663669
}
664670
if (adFlags & (1 << 2)) {
665-
ss << "[BR/EDR Not Supported] ";
671+
res += "[BR/EDR Not Supported] ";
666672
}
667673
if (adFlags & (1 << 3)) {
668-
ss << "[Simultaneous LE and BR/EDR to Same Device Capable (Controller)] ";
674+
res += "[Simultaneous LE and BR/EDR to Same Device Capable (Controller)] ";
669675
}
670676
if (adFlags & (1 << 4)) {
671-
ss << "[Simultaneous LE and BR/EDR to Same Device Capable (Host)] ";
677+
res += "[Simultaneous LE and BR/EDR to Same Device Capable (Host)] ";
672678
}
673-
return ss.str();
679+
return res;
674680
} // adFlagsToString
675681

676682

@@ -802,13 +808,13 @@ char* BLEUtils::buildHexData(uint8_t* target, uint8_t* source, uint8_t length) {
802808
* @return A string representation of a piece of memory.
803809
*/
804810
std::string BLEUtils::buildPrintData(uint8_t* source, size_t length) {
805-
std::ostringstream ss;
811+
std::string res;
806812
for (int i = 0; i < length; i++) {
807813
char c = *source;
808-
ss << (isprint(c) ? c : '.');
814+
res += (isprint(c) ? c : '.');
809815
source++;
810816
}
811-
return ss.str();
817+
return res;
812818
} // buildPrintData
813819

814820

@@ -1848,14 +1854,22 @@ std::string BLEUtils::gattDescriptorUUIDToString(uint32_t descriptorUUID) {
18481854
* @return A string representation of an esp_gattc_service_elem_t.
18491855
*/
18501856
std::string BLEUtils::gattcServiceElementToString(esp_gattc_service_elem_t* pGATTCServiceElement) {
1851-
std::stringstream ss;
1852-
1853-
ss << "[uuid: " << BLEUUID(pGATTCServiceElement->uuid).toString() <<
1854-
", start_handle: " << pGATTCServiceElement->start_handle <<
1855-
" 0x" << std::hex << pGATTCServiceElement->start_handle <<
1856-
", end_handle: " << std::dec << pGATTCServiceElement->end_handle <<
1857-
" 0x" << std::hex << pGATTCServiceElement->end_handle << "]";
1858-
return ss.str();
1857+
std::string res;
1858+
char val[6];
1859+
res += "[uuid: " + BLEUUID(pGATTCServiceElement->uuid).toString() + ", start_handle: ";
1860+
snprintf(val, sizeof(val), "%d", pGATTCServiceElement->start_handle);
1861+
res += val;
1862+
res += " 0x";
1863+
snprintf(val, sizeof(val), "%04x", pGATTCServiceElement->start_handle);
1864+
res += val;
1865+
res += ", end_handle: ";
1866+
snprintf(val, sizeof(val), "%d", pGATTCServiceElement->end_handle);
1867+
res += val;
1868+
res += " 0x";
1869+
snprintf(val, sizeof(val), "%04x", pGATTCServiceElement->end_handle);
1870+
res += val;
1871+
res += "]";
1872+
return res;
18591873
} // gattcServiceElementToString
18601874

18611875

‎libraries/BLE/src/FreeRTOS.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ bool FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
202202
* @return A string representation of the semaphore.
203203
*/
204204
std::string FreeRTOS::Semaphore::toString() {
205-
std::stringstream stringStream;
206-
stringStream << "name: "<< m_name << " (0x" << std::hex << std::setfill('0') << (uint32_t)m_semaphore << "), owner: " << m_owner;
207-
return stringStream.str();
205+
char hex[9];
206+
std::string res = "name: " + m_name + " (0x";
207+
snprintf(hex, sizeof(hex), "%08x", (uint32_t)m_semaphore);
208+
res += hex;
209+
res += "), owner: " + m_owner;
210+
return res;
208211
} // toString
209212

210213

‎libraries/BLE/src/GeneralUtils.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,12 @@ void GeneralUtils::hexDump(const uint8_t* pData, uint32_t length) {
332332
* @return A string representation of the IP address.
333333
*/
334334
std::string GeneralUtils::ipToString(uint8_t *ip) {
335-
std::stringstream s;
336-
s << (int) ip[0] << '.' << (int) ip[1] << '.' << (int) ip[2] << '.' << (int) ip[3];
337-
return s.str();
335+
auto size = 16;
336+
char *val = (char*)malloc(size);
337+
snprintf(val, size, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
338+
std::string res(val);
339+
free(val);
340+
return res;
338341
} // ipToString
339342

340343

@@ -347,11 +350,14 @@ std::string GeneralUtils::ipToString(uint8_t *ip) {
347350
std::vector<std::string> GeneralUtils::split(std::string source, char delimiter) {
348351
// See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
349352
std::vector<std::string> strings;
350-
std::istringstream iss(source);
351-
std::string s;
352-
while (std::getline(iss, s, delimiter)) {
353-
strings.push_back(trim(s));
353+
std::size_t current, previous = 0;
354+
current = source.find(delimiter);
355+
while (current != std::string::npos) {
356+
strings.push_back(trim(source.substr(previous, current - previous)));
357+
previous = current + 1;
358+
current = source.find(delimiter, previous);
354359
}
360+
strings.push_back(trim(source.substr(previous, current - previous)));
355361
return strings;
356362
} // split
357363

0 commit comments

Comments
 (0)
Please sign in to comment.