- avoid possible buffer overflows by using snprintf
- uniqueId is a field now - optimizes string copy - store long strings in FLASH memory
This commit is contained in:
parent
138c7b5cd8
commit
baa3d2d0b8
@ -17,8 +17,8 @@ namespace HaESP {
|
||||
uint8_t hfrag;
|
||||
ESP.getHeapStats(&hfree, &hmax, &hfrag);
|
||||
|
||||
char value[256];
|
||||
sprintf(value, "{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}", hfrag, hfree, hmax);
|
||||
char value[128];
|
||||
snprintf_P(value, sizeof(value), PSTR("{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}"), hfrag, hfree, hmax);
|
||||
Sensor::mapSensors["heap_fragmentation"]->updateState(value);
|
||||
Sensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str());
|
||||
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str());
|
||||
|
||||
27
src/ha.h
27
src/ha.h
@ -7,6 +7,7 @@ using namespace std;
|
||||
|
||||
#define JSON_SIZE 512
|
||||
#define TOPIC_SIZE 255
|
||||
#define BASE_TOPIC "homeassistant/%s/%s/%s"
|
||||
|
||||
namespace Ha {
|
||||
uint16_t(*publisher)(const char* topic, const char* message);
|
||||
@ -116,12 +117,13 @@ namespace Ha {
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void buildUniqueId(char* uniqueId) {
|
||||
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id);
|
||||
char uniqueId[50];
|
||||
virtual void buildUniqueId() {
|
||||
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
|
||||
}
|
||||
|
||||
virtual void buildConfigTopic() {
|
||||
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
|
||||
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"/config", type, MAIN_DEVICE_ID, id);
|
||||
}
|
||||
|
||||
virtual void buildComponentConfig(JsonDocument& jsonDoc) = 0;
|
||||
@ -131,8 +133,7 @@ namespace Ha {
|
||||
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
||||
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
||||
jsonDoc["name"] = name;
|
||||
char uniqueId[50];
|
||||
buildUniqueId(uniqueId);
|
||||
buildUniqueId();
|
||||
jsonDoc["unique_id"] = uniqueId;
|
||||
buildConfigTopic();
|
||||
|
||||
@ -261,7 +262,7 @@ namespace Ha {
|
||||
static unordered_map<string, Command*> mapCommands;
|
||||
|
||||
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
|
||||
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
|
||||
snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", type, MAIN_DEVICE_ID, id);
|
||||
mapCommands.insert({ string(commandTopic), this });
|
||||
}
|
||||
|
||||
@ -297,7 +298,7 @@ namespace Ha {
|
||||
StateConfig(Component* cmp) : cmp(cmp) {}
|
||||
|
||||
void withStateTopic() {
|
||||
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", cmp->type, MAIN_DEVICE_ID, cmp->id);
|
||||
snprintf(stateTopic, sizeof(stateTopic), BASE_TOPIC"/state", cmp->type, MAIN_DEVICE_ID, cmp->id);
|
||||
}
|
||||
|
||||
void updateState(const char* message) {
|
||||
@ -336,9 +337,7 @@ namespace Ha {
|
||||
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
|
||||
|
||||
void updateState(unsigned int value) {
|
||||
char message[32];
|
||||
sprintf(message, "%u", value);
|
||||
StateConfig::updateState(message);
|
||||
StateConfig::updateState(to_string(value).c_str());
|
||||
}
|
||||
|
||||
void restoreFromState() {
|
||||
@ -366,17 +365,17 @@ namespace Ha {
|
||||
}
|
||||
|
||||
protected:
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
void buildUniqueId() override {
|
||||
if (multiValueComponent && deviceClass) {
|
||||
sprintf(uniqueId, "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
|
||||
snprintf(uniqueId, sizeof(uniqueId), "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
|
||||
} else {
|
||||
Component::buildUniqueId(uniqueId);
|
||||
Component::buildUniqueId();
|
||||
}
|
||||
}
|
||||
|
||||
void buildConfigTopic() override {
|
||||
if (multiValueComponent && deviceClass) {
|
||||
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
||||
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"_%s""/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
||||
} else {
|
||||
Component::buildConfigTopic();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user