Skip to content

Commit a22ec4a

Browse files
romansavrulinme-no-dev
authored andcommitted
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

+7-8
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

+13-7
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

+12-11
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

+8-7
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

+4-5
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

+1-1
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

+4-4
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

+8-7
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

+2-3
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

+39-57
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

+1-1
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

+14-8
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");

0 commit comments

Comments
 (0)