Reduce flash usage up to 214k in one click (#2929)

* std::stringstream -> std::string

* Fix small issues

* Small fix 2
This commit is contained in:
Roman Savrulin 2019-07-09 19:31:17 +03:00 committed by Me No Dev
parent 20498cf8b1
commit a22ec4a978
20 changed files with 229 additions and 205 deletions

View File

@ -13,6 +13,7 @@
#include <iomanip> #include <iomanip>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <malloc.h>
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h" #include "esp32-hal-log.h"
#endif #endif
@ -83,13 +84,11 @@ esp_bd_addr_t *BLEAddress::getNative() {
* @return The string representation of the address. * @return The string representation of the address.
*/ */
std::string BLEAddress::toString() { std::string BLEAddress::toString() {
std::stringstream stream; auto size = 18;
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[0] << ':'; char *res = (char*)malloc(size);
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[1] << ':'; 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]);
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[2] << ':'; std::string ret(res);
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[3] << ':'; free(res);
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[4] << ':'; return ret;
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[5];
return stream.str();
} // toString } // toString
#endif #endif

View File

@ -484,23 +484,29 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
* @return A string representation of this device. * @return A string representation of this device.
*/ */
std::string BLEAdvertisedDevice::toString() { std::string BLEAdvertisedDevice::toString() {
std::stringstream ss; std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
if (haveAppearance()) { if (haveAppearance()) {
ss << ", appearance: " << getAppearance(); char val[6];
snprintf(val, sizeof(val), "%d", getAppearance());
res += ", appearance: ";
res += val;
} }
if (haveManufacturerData()) { if (haveManufacturerData()) {
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length()); char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
ss << ", manufacturer data: " << pHex; res += ", manufacturer data: ";
res += pHex;
free(pHex); free(pHex);
} }
if (haveServiceUUID()) { if (haveServiceUUID()) {
ss << ", serviceUUID: " << getServiceUUID().toString(); res += ", serviceUUID: " + getServiceUUID().toString();
} }
if (haveTXPower()) { if (haveTXPower()) {
ss << ", txPower: " << (int)getTXPower(); char val[4];
snprintf(val, sizeof(val), "%d", getTXPower());
res += ", txPower: ";
res += val;
} }
return ss.str(); return res;
} // toString } // toString
uint8_t* BLEAdvertisedDevice::getPayload() { uint8_t* BLEAdvertisedDevice::getPayload() {

View File

@ -717,17 +717,18 @@ void BLECharacteristic::setWriteProperty(bool value) {
* @return A string representation of the characteristic. * @return A string representation of the characteristic.
*/ */
std::string BLECharacteristic::toString() { std::string BLECharacteristic::toString() {
std::stringstream stringstream; std::string res = "UUID: " + m_bleUUID.toString() + ", handle : 0x";
stringstream << std::hex << std::setfill('0'); char hex[5];
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle; snprintf(hex, sizeof(hex), "%04x", m_handle);
stringstream << " " << res += hex;
((m_properties & ESP_GATT_CHAR_PROP_BIT_READ) ? "Read " : "") << res += " ";
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE) ? "Write " : "") << if (m_properties & ESP_GATT_CHAR_PROP_BIT_READ) res += "Read ";
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) ? "WriteNoResponse " : "") << if (m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE) res += "Write ";
((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST) ? "Broadcast " : "") << if (m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) res += "WriteNoResponse ";
((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY) ? "Notify " : "") << if (m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST) res += "Broadcast ";
((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE) ? "Indicate " : ""); if (m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY) res += "Notify ";
return stringstream.str(); if (m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE) res += "Indicate ";
return res;
} // toString } // toString

View File

@ -116,17 +116,18 @@ void BLECharacteristicMap::setByUUID(BLECharacteristic* pCharacteristic, BLEUUID
* @return A string representation of the characteristic map. * @return A string representation of the characteristic map.
*/ */
std::string BLECharacteristicMap::toString() { std::string BLECharacteristicMap::toString() {
std::stringstream stringStream; std::string res;
stringStream << std::hex << std::setfill('0');
int count = 0; int count = 0;
char hex[5];
for (auto &myPair: m_uuidMap) { for (auto &myPair: m_uuidMap) {
if (count > 0) { if (count > 0) {res += "\n";}
stringStream << "\n"; snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
}
count++; count++;
stringStream << "handle: 0x" << std::setw(2) << myPair.first->getHandle() << ", uuid: " + myPair.first->getUUID().toString(); res += "handle: 0x";
res += hex;
res += ", uuid: " + myPair.first->getUUID().toString();
} }
return stringStream.str(); return res;
} // toString } // toString

View File

@ -515,14 +515,13 @@ uint16_t BLEClient::getMTU() {
* @return A string representation of this client. * @return A string representation of this client.
*/ */
std::string BLEClient::toString() { std::string BLEClient::toString() {
std::ostringstream ss; std::string res = "peer address: " + m_peerAddress.toString();
ss << "peer address: " << m_peerAddress.toString(); res += "\nServices:\n";
ss << "\nServices:\n";
for (auto &myPair : m_servicesMap) { for (auto &myPair : m_servicesMap) {
ss << myPair.second->toString() << "\n"; res += myPair.second->toString() + "\n";
// myPair.second is the value // myPair.second is the value
} }
return ss.str(); return res;
} // toString } // toString

View File

@ -15,7 +15,7 @@
#include <string.h> #include <string.h>
#include <map> #include <map>
#include <string> #include <string>
#include "BLEExceptions.h" //#include "BLEExceptions.h"
#include "BLERemoteService.h" #include "BLERemoteService.h"
#include "BLEService.h" #include "BLEService.h"
#include "BLEAddress.h" #include "BLEAddress.h"

View File

@ -255,10 +255,10 @@ void BLEDescriptor::setAccessPermissions(esp_gatt_perm_t perm) {
* @return A string representation of the descriptor. * @return A string representation of the descriptor.
*/ */
std::string BLEDescriptor::toString() { std::string BLEDescriptor::toString() {
std::stringstream stringstream; char hex[5];
stringstream << std::hex << std::setfill('0'); snprintf(hex, sizeof(hex), "%04x", m_handle);
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle; std::string res = "UUID: " + m_bleUUID.toString() + ", handle: 0x" + hex;
return stringstream.str(); return res;
} // toString } // toString

View File

@ -90,17 +90,18 @@ void BLEDescriptorMap::setByHandle(uint16_t handle, BLEDescriptor* pDescriptor)
* @return A string representation of the descriptor map. * @return A string representation of the descriptor map.
*/ */
std::string BLEDescriptorMap::toString() { std::string BLEDescriptorMap::toString() {
std::stringstream stringStream; std::string res;
stringStream << std::hex << std::setfill('0'); char hex[5];
int count = 0; int count = 0;
for (auto &myPair : m_uuidMap) { for (auto &myPair : m_uuidMap) {
if (count > 0) { if (count > 0) {res += "\n";}
stringStream << "\n"; snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
}
count++; count++;
stringStream << "handle: 0x" << std::setw(2) << myPair.first->getHandle() << ", uuid: " + myPair.first->getUUID().toString(); res += "handle: 0x";
res += hex;
res += ", uuid: " + myPair.first->getUUID().toString();
} }
return stringStream.str(); return res;
} // toString } // toString

View File

@ -479,9 +479,8 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
* @return A string representation of the nature of this device. * @return A string representation of the nature of this device.
*/ */
/* STATIC */ std::string BLEDevice::toString() { /* STATIC */ std::string BLEDevice::toString() {
std::ostringstream oss; std::string res = "BD Address: " + getAddress().toString();
oss << "BD Address: " << getAddress().toString(); return res;
return oss.str();
} // toString } // toString

View File

@ -7,7 +7,7 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <string.h> #include <string.h>
#include <sstream> #include <stdio.h>
#include "esp32-hal-log.h" #include "esp32-hal-log.h"
#include "BLEEddystoneTLM.h" #include "BLEEddystoneTLM.h"
@ -54,62 +54,44 @@ uint32_t BLEEddystoneTLM::getTime() {
} // getTime } // getTime
std::string BLEEddystoneTLM::toString() { std::string BLEEddystoneTLM::toString() {
std::stringstream ss; std::string out = "";
std::string out = ""; uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
uint32_t rawsec; char val[6];
ss << "Version ";
ss << std::dec << m_eddystoneData.version;
ss << "\n";
ss << "Battery Voltage "; out += "Version " + m_eddystoneData.version;
ss << std::dec << ENDIAN_CHANGE_U16(m_eddystoneData.volt); out += "\n";
ss << " mV\n"; out += "Battery Voltage " + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
out += " mV\n";
ss << "Temperature "; out += "Temperature ";
ss << (float) m_eddystoneData.temp; snprintf(val, sizeof(val), "%d", m_eddystoneData.temp);
ss << " °C\n"; out += val;
out += ".0 °C\n";
ss << "Adv. Count "; out += "Adv. Count ";
ss << std::dec << ENDIAN_CHANGE_U32(m_eddystoneData.advCount); snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
out += val;
out += "\n";
ss << "\n"; out += "Time ";
ss << "Time "; snprintf(val, sizeof(val), "%04d", rawsec / 864000);
out += val;
out += ".";
rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil); snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24);
std::stringstream buffstream; out += val;
buffstream << "0000"; out += ":";
buffstream << std::dec << rawsec / 864000;
std::string buff = buffstream.str();
ss << buff.substr(buff.length() - 4, buff.length()); snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60);
ss << "."; out += val;
out += ":";
buffstream.str(""); snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60);
buffstream.clear(); out += val;
buffstream << "00"; out += "\n";
buffstream << std::dec << (rawsec / 36000) % 24;
buff = buffstream.str();
ss << buff.substr(buff.length()-2, buff.length());
ss << ":";
buffstream.str(""); return out;
buffstream.clear();
buffstream << "00";
buffstream << std::dec << (rawsec / 600) % 60;
buff = buffstream.str();
ss << buff.substr(buff.length() - 2, buff.length());
ss << ":";
buffstream.str("");
buffstream.clear();
buffstream << "00";
buffstream << std::dec << (rawsec / 10) % 60;
buff = buffstream.str();
ss << buff.substr(buff.length() - 2, buff.length());
ss << "\n";
return ss.str();
} // toString } // toString
/** /**

View File

@ -5,5 +5,5 @@
* Author: kolban * Author: kolban
*/ */
#include "BLEExceptions.h" //#include "BLEExceptions.h"

View File

@ -14,7 +14,7 @@
#include <esp_err.h> #include <esp_err.h>
#include <sstream> #include <sstream>
#include "BLEExceptions.h" //#include "BLEExceptions.h"
#include "BLEUtils.h" #include "BLEUtils.h"
#include "GeneralUtils.h" #include "GeneralUtils.h"
#include "BLERemoteDescriptor.h" #include "BLERemoteDescriptor.h"
@ -400,7 +400,8 @@ std::string BLERemoteCharacteristic::readValue() {
// Check to see that we are connected. // Check to see that we are connected.
if (!getRemoteService()->getClient()->isConnected()) { if (!getRemoteService()->getClient()->isConnected()) {
log_e("Disconnected"); log_e("Disconnected");
throw BLEDisconnectedException(); // throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
return std::string();
} }
m_semaphoreReadCharEvt.take("readValue"); m_semaphoreReadCharEvt.take("readValue");
@ -501,11 +502,16 @@ void BLERemoteCharacteristic::removeDescriptors() {
* @return a String representation. * @return a String representation.
*/ */
std::string BLERemoteCharacteristic::toString() { std::string BLERemoteCharacteristic::toString() {
std::ostringstream ss; std::string res = "Characteristic: uuid: " + m_uuid.toString();
ss << "Characteristic: uuid: " << m_uuid.toString() << char val[6];
", handle: " << getHandle() << " 0x" << std::hex << getHandle() << res += ", handle: ";
", props: " << BLEUtils::characteristicPropertiesToString(m_charProp); snprintf(val, sizeof(val), "%d", getHandle());
return ss.str(); res += val;
res += " 0x";
snprintf(val, sizeof(val), "%04x", getHandle());
res += val;
res += ", props: " + BLEUtils::characteristicPropertiesToString(m_charProp);
return res;
} // toString } // toString
@ -546,7 +552,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp
// Check to see that we are connected. // Check to see that we are connected.
if (!getRemoteService()->getClient()->isConnected()) { if (!getRemoteService()->getClient()->isConnected()) {
log_e("Disconnected"); log_e("Disconnected");
throw BLEDisconnectedException(); // throw BLEDisconnectedException();
} }
m_semaphoreWriteCharEvt.take("writeValue"); m_semaphoreWriteCharEvt.take("writeValue");

View File

@ -55,7 +55,8 @@ std::string BLERemoteDescriptor::readValue() {
// Check to see that we are connected. // Check to see that we are connected.
if (!getRemoteCharacteristic()->getRemoteService()->getClient()->isConnected()) { if (!getRemoteCharacteristic()->getRemoteService()->getClient()->isConnected()) {
log_e("Disconnected"); log_e("Disconnected");
throw BLEDisconnectedException(); // throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
return std::string();
} }
m_semaphoreReadDescrEvt.take("readValue"); m_semaphoreReadDescrEvt.take("readValue");
@ -113,9 +114,12 @@ uint32_t BLERemoteDescriptor::readUInt32() {
* @retun A string representation of this BLE Remote Descriptor. * @retun A string representation of this BLE Remote Descriptor.
*/ */
std::string BLERemoteDescriptor::toString() { std::string BLERemoteDescriptor::toString() {
std::stringstream ss; char val[6];
ss << "handle: " << getHandle() << ", uuid: " << getUUID().toString(); snprintf(val, sizeof(val), "%d", getHandle());
return ss.str(); std::string res = "handle: ";
res += val;
res += ", uuid: " + getUUID().toString();
return res;
} // toString } // toString
@ -130,7 +134,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
// Check to see that we are connected. // Check to see that we are connected.
if (!getRemoteCharacteristic()->getRemoteService()->getClient()->isConnected()) { if (!getRemoteCharacteristic()->getRemoteService()->getClient()->isConnected()) {
log_e("Disconnected"); log_e("Disconnected");
throw BLEDisconnectedException(); // throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
return;
} }
esp_err_t errRc = ::esp_ble_gattc_write_char_descr( esp_err_t errRc = ::esp_ble_gattc_write_char_descr(

View File

@ -317,15 +317,25 @@ void BLERemoteService::setValue(BLEUUID characteristicUuid, std::string value) {
* @return A string representation of this remote service. * @return A string representation of this remote service.
*/ */
std::string BLERemoteService::toString() { std::string BLERemoteService::toString() {
std::ostringstream ss; std::string res = "Service: uuid: " + m_uuid.toString();
ss << "Service: uuid: " + m_uuid.toString(); char val[6];
ss << ", start_handle: " << std::dec << m_startHandle << " 0x" << std::hex << m_startHandle << res += ", start_handle: ";
", end_handle: " << std::dec << m_endHandle << " 0x" << std::hex << m_endHandle; snprintf(val, sizeof(val), "%d", m_startHandle);
res += val;
snprintf(val, sizeof(val), "%04x", m_startHandle);
res += " 0x";
res += val;
res += ", end_handle: ";
snprintf(val, sizeof(val), "%d", m_endHandle);
res += val;
snprintf(val, sizeof(val), "%04x", m_endHandle);
res += " 0x";
res += val;
for (auto &myPair : m_characteristicMap) { for (auto &myPair : m_characteristicMap) {
ss << "\n" << myPair.second->toString(); res += "\n" + myPair.second->toString();
// myPair.second is the value // myPair.second is the value
} }
return ss.str(); return res;
} // toString } // toString

View File

@ -381,10 +381,12 @@ BLECharacteristic* BLEService::getCharacteristic(BLEUUID uuid) {
* @return A string representation of this service. * @return A string representation of this service.
*/ */
std::string BLEService::toString() { std::string BLEService::toString() {
std::stringstream stringStream; std::string res = "UUID: " + getUUID().toString();
stringStream << "UUID: " << getUUID().toString() << char hex[5];
", handle: 0x" << std::hex << std::setfill('0') << std::setw(2) << getHandle(); snprintf(hex, sizeof(hex), "%04x", getHandle());
return stringStream.str(); res += ", handle: 0x";
res += hex;
return res;
} // toString } // toString

View File

@ -6,7 +6,7 @@
*/ */
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <sstream> #include <stdio.h>
#include <iomanip> #include <iomanip>
#include "BLEService.h" #include "BLEService.h"
@ -73,12 +73,15 @@ void BLEServiceMap::setByHandle(uint16_t handle, BLEService* service) {
* @return A string representation of the service map. * @return A string representation of the service map.
*/ */
std::string BLEServiceMap::toString() { std::string BLEServiceMap::toString() {
std::stringstream stringStream; std::string res;
stringStream << std::hex << std::setfill('0'); char hex[5];
for (auto &myPair: m_handleMap) { for (auto &myPair: m_handleMap) {
stringStream << "handle: 0x" << std::setw(2) << myPair.first << ", uuid: " + myPair.second->getUUID().toString() << "\n"; res += "handle: 0x";
snprintf(hex, sizeof(hex), "%04x", myPair.first);
res += hex;
res += ", uuid: " + myPair.second->getUUID().toString() + "\n";
} }
return stringStream.str(); return res;
} // toString } // toString
void BLEServiceMap::handleGATTServerEvent( void BLEServiceMap::handleGATTServerEvent(

View File

@ -349,51 +349,38 @@ BLEUUID BLEUUID::to128() {
*/ */
std::string BLEUUID::toString() { std::string BLEUUID::toString() {
if (!m_valueSet) return "<NULL>"; // If we have no value, nothing to format. if (!m_valueSet) return "<NULL>"; // If we have no value, nothing to format.
// If the UUIDs are 16 or 32 bit, pad correctly. // If the UUIDs are 16 or 32 bit, pad correctly.
std::stringstream ss;
if (m_uuid.len == ESP_UUID_LEN_16) { // If the UUID is 16bit, pad correctly. if (m_uuid.len == ESP_UUID_LEN_16) { // If the UUID is 16bit, pad correctly.
ss << "0000" << char hex[5];
std::hex << snprintf(hex, sizeof(hex), "%04x", m_uuid.uuid.uuid16);
std::setfill('0') << return std::string(hex) + "-0000-1000-8000-00805f9b34fb";
std::setw(4) <<
m_uuid.uuid.uuid16 <<
"-0000-1000-8000-00805f9b34fb";
return ss.str(); // Return the string
} // End 16bit UUID } // End 16bit UUID
if (m_uuid.len == ESP_UUID_LEN_32) { // If the UUID is 32bit, pad correctly. if (m_uuid.len == ESP_UUID_LEN_32) { // If the UUID is 32bit, pad correctly.
ss << std::hex << char hex[9];
std::setfill('0') << snprintf(hex, sizeof(hex), "%08x", m_uuid.uuid.uuid32);
std::setw(8) << return std::string(hex) + "-0000-1000-8000-00805f9b34fb";
m_uuid.uuid.uuid32 <<
"-0000-1000-8000-00805f9b34fb";
return ss.str(); // return the string
} // End 32bit UUID } // End 32bit UUID
// The UUID is not 16bit or 32bit which means that it is 128bit. // The UUID is not 16bit or 32bit which means that it is 128bit.
// //
// UUID string format: // UUID string format:
// AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP // AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
ss << std::hex << std::setfill('0') << auto size = 35;
std::setw(2) << (int) m_uuid.uuid.uuid128[15] << char *hex = (char *)malloc(size);
std::setw(2) << (int) m_uuid.uuid.uuid128[14] << snprintf(hex, size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
std::setw(2) << (int) m_uuid.uuid.uuid128[13] << m_uuid.uuid.uuid128[15], m_uuid.uuid.uuid128[14],
std::setw(2) << (int) m_uuid.uuid.uuid128[12] << "-" << m_uuid.uuid.uuid128[13], m_uuid.uuid.uuid128[12],
std::setw(2) << (int) m_uuid.uuid.uuid128[11] << m_uuid.uuid.uuid128[11], m_uuid.uuid.uuid128[10],
std::setw(2) << (int) m_uuid.uuid.uuid128[10] << "-" << m_uuid.uuid.uuid128[9], m_uuid.uuid.uuid128[8],
std::setw(2) << (int) m_uuid.uuid.uuid128[9] << m_uuid.uuid.uuid128[7], m_uuid.uuid.uuid128[6],
std::setw(2) << (int) m_uuid.uuid.uuid128[8] << "-" << m_uuid.uuid.uuid128[5], m_uuid.uuid.uuid128[4],
std::setw(2) << (int) m_uuid.uuid.uuid128[7] << m_uuid.uuid.uuid128[3], m_uuid.uuid.uuid128[2],
std::setw(2) << (int) m_uuid.uuid.uuid128[6] << "-" << m_uuid.uuid.uuid128[1], m_uuid.uuid.uuid128[0]);
std::setw(2) << (int) m_uuid.uuid.uuid128[5] << std::string res(hex);
std::setw(2) << (int) m_uuid.uuid.uuid128[4] << free(hex);
std::setw(2) << (int) m_uuid.uuid.uuid128[3] << return res;
std::setw(2) << (int) m_uuid.uuid.uuid128[2] <<
std::setw(2) << (int) m_uuid.uuid.uuid128[1] <<
std::setw(2) << (int) m_uuid.uuid.uuid128[0];
return ss.str();
} // toString } // toString
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View File

@ -604,26 +604,32 @@ static const gattService_t g_gattServices[] = {
* @return A string representation of characteristic properties. * @return A string representation of characteristic properties.
*/ */
std::string BLEUtils::characteristicPropertiesToString(esp_gatt_char_prop_t prop) { std::string BLEUtils::characteristicPropertiesToString(esp_gatt_char_prop_t prop) {
std::stringstream stream; std::string res = "broadcast: ";
stream << res += ((prop & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?"1":"0");
"broadcast: " << ((prop & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?"1":"0") << res += ", read: ";
", read: " << ((prop & ESP_GATT_CHAR_PROP_BIT_READ)?"1":"0") << res += ((prop & ESP_GATT_CHAR_PROP_BIT_READ)?"1":"0");
", write_nr: " << ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?"1":"0") << res += ", write_nr: ";
", write: " << ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE)?"1":"0") << res += ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?"1":"0");
", notify: " << ((prop & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?"1":"0") << res += ", write: ";
", indicate: " << ((prop & ESP_GATT_CHAR_PROP_BIT_INDICATE)?"1":"0") << res += ((prop & ESP_GATT_CHAR_PROP_BIT_WRITE)?"1":"0");
", auth: " << ((prop & ESP_GATT_CHAR_PROP_BIT_AUTH)?"1":"0"); res += ", notify: ";
return stream.str(); res += ((prop & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?"1":"0");
res += ", indicate: ";
res += ((prop & ESP_GATT_CHAR_PROP_BIT_INDICATE)?"1":"0");
res += ", auth: ";
res += ((prop & ESP_GATT_CHAR_PROP_BIT_AUTH)?"1":"0");
return res;
} // characteristicPropertiesToString } // characteristicPropertiesToString
/** /**
* @brief Convert an esp_gatt_id_t to a string. * @brief Convert an esp_gatt_id_t to a string.
*/ */
static std::string gattIdToString(esp_gatt_id_t gattId) { static std::string gattIdToString(esp_gatt_id_t gattId) {
std::stringstream stream; std::string res = "uuid: " + BLEUUID(gattId.uuid).toString() + ", inst_id: ";
stream << "uuid: " << BLEUUID(gattId.uuid).toString() << ", inst_id: " << (int)gattId.inst_id; char val[8];
//sprintf(buffer, "uuid: %s, inst_id: %d", uuidToString(gattId.uuid).c_str(), gattId.inst_id); snprintf(val, sizeof(val), "%d", (int)gattId.inst_id);
return stream.str(); res += val;
return res;
} // gattIdToString } // gattIdToString
@ -654,23 +660,23 @@ const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
* @return std::string A string representation of the advertising flags. * @return std::string A string representation of the advertising flags.
*/ */
std::string BLEUtils::adFlagsToString(uint8_t adFlags) { std::string BLEUtils::adFlagsToString(uint8_t adFlags) {
std::stringstream ss; std::string res;
if (adFlags & (1 << 0)) { if (adFlags & (1 << 0)) {
ss << "[LE Limited Discoverable Mode] "; res += "[LE Limited Discoverable Mode] ";
} }
if (adFlags & (1 << 1)) { if (adFlags & (1 << 1)) {
ss << "[LE General Discoverable Mode] "; res += "[LE General Discoverable Mode] ";
} }
if (adFlags & (1 << 2)) { if (adFlags & (1 << 2)) {
ss << "[BR/EDR Not Supported] "; res += "[BR/EDR Not Supported] ";
} }
if (adFlags & (1 << 3)) { if (adFlags & (1 << 3)) {
ss << "[Simultaneous LE and BR/EDR to Same Device Capable (Controller)] "; res += "[Simultaneous LE and BR/EDR to Same Device Capable (Controller)] ";
} }
if (adFlags & (1 << 4)) { if (adFlags & (1 << 4)) {
ss << "[Simultaneous LE and BR/EDR to Same Device Capable (Host)] "; res += "[Simultaneous LE and BR/EDR to Same Device Capable (Host)] ";
} }
return ss.str(); return res;
} // adFlagsToString } // adFlagsToString
@ -802,13 +808,13 @@ char* BLEUtils::buildHexData(uint8_t* target, uint8_t* source, uint8_t length) {
* @return A string representation of a piece of memory. * @return A string representation of a piece of memory.
*/ */
std::string BLEUtils::buildPrintData(uint8_t* source, size_t length) { std::string BLEUtils::buildPrintData(uint8_t* source, size_t length) {
std::ostringstream ss; std::string res;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char c = *source; char c = *source;
ss << (isprint(c) ? c : '.'); res += (isprint(c) ? c : '.');
source++; source++;
} }
return ss.str(); return res;
} // buildPrintData } // buildPrintData
@ -1848,14 +1854,22 @@ std::string BLEUtils::gattDescriptorUUIDToString(uint32_t descriptorUUID) {
* @return A string representation of an esp_gattc_service_elem_t. * @return A string representation of an esp_gattc_service_elem_t.
*/ */
std::string BLEUtils::gattcServiceElementToString(esp_gattc_service_elem_t* pGATTCServiceElement) { std::string BLEUtils::gattcServiceElementToString(esp_gattc_service_elem_t* pGATTCServiceElement) {
std::stringstream ss; std::string res;
char val[6];
ss << "[uuid: " << BLEUUID(pGATTCServiceElement->uuid).toString() << res += "[uuid: " + BLEUUID(pGATTCServiceElement->uuid).toString() + ", start_handle: ";
", start_handle: " << pGATTCServiceElement->start_handle << snprintf(val, sizeof(val), "%d", pGATTCServiceElement->start_handle);
" 0x" << std::hex << pGATTCServiceElement->start_handle << res += val;
", end_handle: " << std::dec << pGATTCServiceElement->end_handle << res += " 0x";
" 0x" << std::hex << pGATTCServiceElement->end_handle << "]"; snprintf(val, sizeof(val), "%04x", pGATTCServiceElement->start_handle);
return ss.str(); res += val;
res += ", end_handle: ";
snprintf(val, sizeof(val), "%d", pGATTCServiceElement->end_handle);
res += val;
res += " 0x";
snprintf(val, sizeof(val), "%04x", pGATTCServiceElement->end_handle);
res += val;
res += "]";
return res;
} // gattcServiceElementToString } // gattcServiceElementToString

View File

@ -202,9 +202,12 @@ bool FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
* @return A string representation of the semaphore. * @return A string representation of the semaphore.
*/ */
std::string FreeRTOS::Semaphore::toString() { std::string FreeRTOS::Semaphore::toString() {
std::stringstream stringStream; char hex[9];
stringStream << "name: "<< m_name << " (0x" << std::hex << std::setfill('0') << (uint32_t)m_semaphore << "), owner: " << m_owner; std::string res = "name: " + m_name + " (0x";
return stringStream.str(); snprintf(hex, sizeof(hex), "%08x", (uint32_t)m_semaphore);
res += hex;
res += "), owner: " + m_owner;
return res;
} // toString } // toString

View File

@ -332,9 +332,12 @@ void GeneralUtils::hexDump(const uint8_t* pData, uint32_t length) {
* @return A string representation of the IP address. * @return A string representation of the IP address.
*/ */
std::string GeneralUtils::ipToString(uint8_t *ip) { std::string GeneralUtils::ipToString(uint8_t *ip) {
std::stringstream s; auto size = 16;
s << (int) ip[0] << '.' << (int) ip[1] << '.' << (int) ip[2] << '.' << (int) ip[3]; char *val = (char*)malloc(size);
return s.str(); snprintf(val, size, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
std::string res(val);
free(val);
return res;
} // ipToString } // ipToString
@ -347,11 +350,14 @@ std::string GeneralUtils::ipToString(uint8_t *ip) {
std::vector<std::string> GeneralUtils::split(std::string source, char delimiter) { std::vector<std::string> GeneralUtils::split(std::string source, char delimiter) {
// See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g // See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
std::vector<std::string> strings; std::vector<std::string> strings;
std::istringstream iss(source); std::size_t current, previous = 0;
std::string s; current = source.find(delimiter);
while (std::getline(iss, s, delimiter)) { while (current != std::string::npos) {
strings.push_back(trim(s)); strings.push_back(trim(source.substr(previous, current - previous)));
previous = current + 1;
current = source.find(delimiter, previous);
} }
strings.push_back(trim(source.substr(previous, current - previous)));
return strings; return strings;
} // split } // split