Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions libraries/BLE/src/BLEAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <iomanip>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
Expand Down Expand Up @@ -83,13 +84,11 @@ esp_bd_addr_t *BLEAddress::getNative() {
* @return The string representation of the address.
*/
std::string BLEAddress::toString() {
std::stringstream stream;
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[0] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[1] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[2] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[3] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[4] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[5];
return stream.str();
auto size = 18;
char *res = (char*)malloc(size);
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]);
std::string ret(res);
free(res);
return ret;
} // toString
#endif
20 changes: 13 additions & 7 deletions libraries/BLE/src/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,23 +484,29 @@ void BLEAdvertisedDevice::setTXPower(int8_t txPower) {
* @return A string representation of this device.
*/
std::string BLEAdvertisedDevice::toString() {
std::stringstream ss;
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
if (haveAppearance()) {
ss << ", appearance: " << getAppearance();
char val[6];
snprintf(val, sizeof(val), "%d", getAppearance());
res += ", appearance: ";
res += val;
}
if (haveManufacturerData()) {
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
ss << ", manufacturer data: " << pHex;
res += ", manufacturer data: ";
res += pHex;
free(pHex);
}
if (haveServiceUUID()) {
ss << ", serviceUUID: " << getServiceUUID().toString();
res += ", serviceUUID: " + getServiceUUID().toString();
}
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

uint8_t* BLEAdvertisedDevice::getPayload() {
Expand Down
23 changes: 12 additions & 11 deletions libraries/BLE/src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,17 +717,18 @@ void BLECharacteristic::setWriteProperty(bool value) {
* @return A string representation of the characteristic.
*/
std::string BLECharacteristic::toString() {
std::stringstream stringstream;
stringstream << std::hex << std::setfill('0');
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle;
stringstream << " " <<
((m_properties & ESP_GATT_CHAR_PROP_BIT_READ) ? "Read " : "") <<
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE) ? "Write " : "") <<
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) ? "WriteNoResponse " : "") <<
((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST) ? "Broadcast " : "") <<
((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY) ? "Notify " : "") <<
((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE) ? "Indicate " : "");
return stringstream.str();
std::string res = "UUID: " + m_bleUUID.toString() + ", handle : 0x";
char hex[5];
snprintf(hex, sizeof(hex), "%04x", m_handle);
res += hex;
res += " ";
if (m_properties & ESP_GATT_CHAR_PROP_BIT_READ) res += "Read ";
if (m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE) res += "Write ";
if (m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) res += "WriteNoResponse ";
if (m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST) res += "Broadcast ";
if (m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY) res += "Notify ";
if (m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE) res += "Indicate ";
return res;
} // toString


Expand Down
15 changes: 8 additions & 7 deletions libraries/BLE/src/BLECharacteristicMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,18 @@ void BLECharacteristicMap::setByUUID(BLECharacteristic* pCharacteristic, BLEUUID
* @return A string representation of the characteristic map.
*/
std::string BLECharacteristicMap::toString() {
std::stringstream stringStream;
stringStream << std::hex << std::setfill('0');
std::string res;
int count = 0;
char hex[5];
for (auto &myPair: m_uuidMap) {
if (count > 0) {
stringStream << "\n";
}
if (count > 0) {res += "\n";}
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
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


Expand Down
9 changes: 4 additions & 5 deletions libraries/BLE/src/BLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,13 @@ uint16_t BLEClient::getMTU() {
* @return A string representation of this client.
*/
std::string BLEClient::toString() {
std::ostringstream ss;
ss << "peer address: " << m_peerAddress.toString();
ss << "\nServices:\n";
std::string res = "peer address: " + m_peerAddress.toString();
res += "\nServices:\n";
for (auto &myPair : m_servicesMap) {
ss << myPair.second->toString() << "\n";
res += myPair.second->toString() + "\n";
// myPair.second is the value
}
return ss.str();
return res;
} // toString


Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <string.h>
#include <map>
#include <string>
#include "BLEExceptions.h"
//#include "BLEExceptions.h"
#include "BLERemoteService.h"
#include "BLEService.h"
#include "BLEAddress.h"
Expand Down
8 changes: 4 additions & 4 deletions libraries/BLE/src/BLEDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ void BLEDescriptor::setAccessPermissions(esp_gatt_perm_t perm) {
* @return A string representation of the descriptor.
*/
std::string BLEDescriptor::toString() {
std::stringstream stringstream;
stringstream << std::hex << std::setfill('0');
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle;
return stringstream.str();
char hex[5];
snprintf(hex, sizeof(hex), "%04x", m_handle);
std::string res = "UUID: " + m_bleUUID.toString() + ", handle: 0x" + hex;
return res;
} // toString


Expand Down
15 changes: 8 additions & 7 deletions libraries/BLE/src/BLEDescriptorMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,18 @@ void BLEDescriptorMap::setByHandle(uint16_t handle, BLEDescriptor* pDescriptor)
* @return A string representation of the descriptor map.
*/
std::string BLEDescriptorMap::toString() {
std::stringstream stringStream;
stringStream << std::hex << std::setfill('0');
std::string res;
char hex[5];
int count = 0;
for (auto &myPair : m_uuidMap) {
if (count > 0) {
stringStream << "\n";
}
if (count > 0) {res += "\n";}
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
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


Expand Down
5 changes: 2 additions & 3 deletions libraries/BLE/src/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,8 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
* @return A string representation of the nature of this device.
*/
/* STATIC */ std::string BLEDevice::toString() {
std::ostringstream oss;
oss << "BD Address: " << getAddress().toString();
return oss.str();
std::string res = "BD Address: " + getAddress().toString();
return res;
} // toString


Expand Down
96 changes: 39 additions & 57 deletions libraries/BLE/src/BLEEddystoneTLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <string.h>
#include <sstream>
#include <stdio.h>
#include "esp32-hal-log.h"
#include "BLEEddystoneTLM.h"

Expand Down Expand Up @@ -54,62 +54,44 @@ uint32_t BLEEddystoneTLM::getTime() {
} // getTime

std::string BLEEddystoneTLM::toString() {
std::stringstream ss;
std::string out = "";
uint32_t rawsec;
ss << "Version ";
ss << std::dec << m_eddystoneData.version;
ss << "\n";

ss << "Battery Voltage ";
ss << std::dec << ENDIAN_CHANGE_U16(m_eddystoneData.volt);
ss << " mV\n";

ss << "Temperature ";
ss << (float) m_eddystoneData.temp;
ss << " °C\n";

ss << "Adv. Count ";
ss << std::dec << ENDIAN_CHANGE_U32(m_eddystoneData.advCount);

ss << "\n";

ss << "Time ";

rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
std::stringstream buffstream;
buffstream << "0000";
buffstream << std::dec << rawsec / 864000;
std::string buff = buffstream.str();

ss << buff.substr(buff.length() - 4, buff.length());
ss << ".";

buffstream.str("");
buffstream.clear();
buffstream << "00";
buffstream << std::dec << (rawsec / 36000) % 24;
buff = buffstream.str();
ss << buff.substr(buff.length()-2, buff.length());
ss << ":";

buffstream.str("");
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();
std::string out = "";
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
char val[6];

out += "Version " + m_eddystoneData.version;
out += "\n";
out += "Battery Voltage " + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
out += " mV\n";

out += "Temperature ";
snprintf(val, sizeof(val), "%d", m_eddystoneData.temp);
out += val;
out += ".0 °C\n";

out += "Adv. Count ";
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
out += val;
out += "\n";

out += "Time ";

snprintf(val, sizeof(val), "%04d", rawsec / 864000);
out += val;
out += ".";

snprintf(val, sizeof(val), "%02d", (rawsec / 36000) % 24);
out += val;
out += ":";

snprintf(val, sizeof(val), "%02d", (rawsec / 600) % 60);
out += val;
out += ":";

snprintf(val, sizeof(val), "%02d", (rawsec / 10) % 60);
out += val;
out += "\n";

return out;
} // toString

/**
Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEExceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* Author: kolban
*/

#include "BLEExceptions.h"
//#include "BLEExceptions.h"

22 changes: 14 additions & 8 deletions libraries/BLE/src/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <esp_err.h>

#include <sstream>
#include "BLEExceptions.h"
//#include "BLEExceptions.h"
#include "BLEUtils.h"
#include "GeneralUtils.h"
#include "BLERemoteDescriptor.h"
Expand Down Expand Up @@ -400,7 +400,8 @@ std::string BLERemoteCharacteristic::readValue() {
// Check to see that we are connected.
if (!getRemoteService()->getClient()->isConnected()) {
log_e("Disconnected");
throw BLEDisconnectedException();
// throw BLEDisconnectedException(); TODO:: think about error reporting mechanism
return std::string();
}

m_semaphoreReadCharEvt.take("readValue");
Expand Down Expand Up @@ -501,11 +502,16 @@ void BLERemoteCharacteristic::removeDescriptors() {
* @return a String representation.
*/
std::string BLERemoteCharacteristic::toString() {
std::ostringstream ss;
ss << "Characteristic: uuid: " << m_uuid.toString() <<
", handle: " << getHandle() << " 0x" << std::hex << getHandle() <<
", props: " << BLEUtils::characteristicPropertiesToString(m_charProp);
return ss.str();
std::string res = "Characteristic: uuid: " + m_uuid.toString();
char val[6];
res += ", handle: ";
snprintf(val, sizeof(val), "%d", getHandle());
res += val;
res += " 0x";
snprintf(val, sizeof(val), "%04x", getHandle());
res += val;
res += ", props: " + BLEUtils::characteristicPropertiesToString(m_charProp);
return res;
} // toString


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

m_semaphoreWriteCharEvt.take("writeValue");
Expand Down
Loading