Skip to content

Commit 6f34df8

Browse files
DreidSpbromansavrulin
authored andcommitted
std::stringstream -> std::string
1 parent 7d78247 commit 6f34df8

20 files changed

+237
-214
lines changed

libraries/BLE/src/BLEAddress.cpp

Lines changed: 6 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,10 @@ 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+
char *res = (char*)malloc(18);
88+
snprintf(res, 18, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
89+
std::string ret(res);
90+
free(res);
91+
return ret;
9492
} // toString
9593
#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, 3, "%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: 43 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,48 @@ 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+
float temp = (float)m_eddystoneData.temp;
60+
char val[6];
61+
62+
out += "Version " + m_eddystoneData.version;
63+
out += "\n";
64+
out += "Battery Voltage " + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
65+
out += " mV\n";
66+
67+
out += "Temperature ";
68+
snprintf(val, sizeof(val), "%d", (int)m_eddystoneData.temp);
69+
out += val;
70+
out += ".";
71+
snprintf(val, sizeof(val), "%d", ((int)(m_eddystoneData.temp * 100)) % 100);
72+
out += val;
73+
out += " °C\n";
74+
75+
out += "Adv. Count ";
76+
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
77+
out += val;
78+
out += "\n";
79+
80+
out += "Time ";
81+
82+
snprintf(val, sizeof(val), "%04d", rawsec / 864000);
83+
out += val;
84+
out += ".";
85+
86+
snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24);
87+
out += val;
88+
out += ":";
89+
90+
snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60);
91+
out += val;
92+
out += ":";
93+
94+
snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60);
95+
out += val;
96+
out += "\n";
97+
98+
return out;
11399
} // toString
114100

115101
/**

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: 13 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,7 @@ 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();
404404
}
405405

406406
m_semaphoreReadCharEvt.take("readValue");
@@ -501,11 +501,16 @@ void BLERemoteCharacteristic::removeDescriptors() {
501501
* @return a String representation.
502502
*/
503503
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();
504+
std::string res = "Characteristic: uuid: " + m_uuid.toString();
505+
char val[6];
506+
res += ", handle: ";
507+
snprintf(val, sizeof(val), "%d", getHandle());
508+
res += val;
509+
res += " 0x";
510+
snprintf(val, sizeof(val), "%04x", getHandle());
511+
res += val;
512+
res += ", props: " + BLEUtils::characteristicPropertiesToString(m_charProp);
513+
return res;
509514
} // toString
510515

511516

@@ -546,7 +551,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp
546551
// Check to see that we are connected.
547552
if (!getRemoteService()->getClient()->isConnected()) {
548553
log_e("Disconnected");
549-
throw BLEDisconnectedException();
554+
// throw BLEDisconnectedException();
550555
}
551556

552557
m_semaphoreWriteCharEvt.take("writeValue");

0 commit comments

Comments
 (0)