From b67113fc1f0f2425043893d37ece71a793527c61 Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 10 Oct 2022 20:11:23 +0200 Subject: [PATCH] use const char * where possible --- src/devices/boiler.cpp | 2 +- src/emsdevice.cpp | 24 +++++++++++------------ src/emsdevice.h | 10 +++++----- src/emsdevicevalue.cpp | 4 ++-- src/emsesp.cpp | 5 +++-- src/helpers.cpp | 44 +++++++++++++++--------------------------- src/helpers.h | 4 +--- src/mqtt.cpp | 22 +++++++++++---------- 8 files changed, 52 insertions(+), 63 deletions(-) diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index e35571d5f..938dfd859 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -2227,7 +2227,7 @@ bool Boiler::set_maintenance(const char * value, const int8_t id) { std::string s; if (Helpers::value2string(value, s)) { - if (s == Helpers::translated_word(FL_(reset))) { + if (s == std::string(Helpers::translated_word(FL_(reset)))) { // LOG_INFO("Reset boiler maintenance message"); write_command(0x05, 0x08, 0xFF, 0x1C); return true; diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 7fdb48186..58400dbc1 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -81,7 +81,7 @@ std::string EMSdevice::brand_to_string() const { } // returns the name of the MQTT topic to use for a specific device, without the base -std::string EMSdevice::device_type_2_device_name(const uint8_t device_type) { +const char * EMSdevice::device_type_2_device_name(const uint8_t device_type) { switch (device_type) { case DeviceType::SYSTEM: return (F_(system)); @@ -259,7 +259,7 @@ bool EMSdevice::has_tag(const uint8_t tag) const { // called from the command 'entities' void EMSdevice::list_device_entries(JsonObject & output) const { for (const auto & dv : devicevalues_) { - std::string fullname = dv.get_fullname(); + auto fullname = dv.get_fullname(); if (!dv.has_state(DeviceValueState::DV_WEB_EXCLUDE) && dv.type != DeviceValueType::CMD && !fullname.empty()) { // if we have a tag prefix it char key[50]; @@ -637,9 +637,9 @@ void EMSdevice::publish_value(void * value_p) const { char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; if (Mqtt::publish_single2cmd()) { if (dv.tag >= DeviceValueTAG::TAG_HC1) { - snprintf(topic, sizeof(topic), "%s/%s/%s", device_type_2_device_name(device_type_).c_str(), tag_to_mqtt(dv.tag).c_str(), (dv.short_name)); + snprintf(topic, sizeof(topic), "%s/%s/%s", device_type_2_device_name(device_type_), tag_to_mqtt(dv.tag).c_str(), (dv.short_name)); } else { - snprintf(topic, sizeof(topic), "%s/%s", device_type_2_device_name(device_type_).c_str(), (dv.short_name)); + snprintf(topic, sizeof(topic), "%s/%s", device_type_2_device_name(device_type_), (dv.short_name)); } } else if (Mqtt::is_nested() && dv.tag >= DeviceValueTAG::TAG_HC1) { snprintf(topic, sizeof(topic), "%s/%s/%s", Mqtt::tag_to_topic(device_type_, dv.tag).c_str(), tag_to_mqtt(dv.tag).c_str(), (dv.short_name)); @@ -662,7 +662,7 @@ void EMSdevice::publish_value(void * value_p) const { Helpers::render_value(payload, *(uint8_t *)(value_p), 0); } else { auto enum_str = Helpers::translated_word(dv.options[*(uint8_t *)(value_p)]); - strlcpy(payload, enum_str.c_str(), sizeof(payload)); + strlcpy(payload, enum_str, sizeof(payload)); } } break; @@ -823,7 +823,7 @@ void EMSdevice::generate_values_web(JsonObject & output) { JsonArray l = obj.createNestedArray("l"); for (uint8_t i = 0; i < dv.options_size; i++) { auto enum_str = Helpers::translated_word(dv.options[i]); - if (!enum_str.empty()) { + if (enum_str) { l.add(enum_str); } } @@ -917,12 +917,12 @@ void EMSdevice::generate_values_web_customization(JsonArray & output) { // don't add the fullname if its a command auto fullname = Helpers::translated_word(dv.fullname); if (dv.type != DeviceValueType::CMD) { - if (!fullname.empty()) { + if (fullname) { if ((dv.tag == DeviceValueTAG::TAG_NONE) || tag_to_string(dv.tag).empty()) { obj["n"] = fullname; } else { char name[50]; - snprintf(name, sizeof(name), "%s %s", tag_to_string(dv.tag).c_str(), fullname.c_str()); + snprintf(name, sizeof(name), "%s %s", tag_to_string(dv.tag).c_str(), fullname); obj["n"] = name; } } @@ -933,7 +933,7 @@ void EMSdevice::generate_values_web_customization(JsonArray & output) { obj["cn"] = custom_fullname; } } else { - obj["n"] = "!" + fullname; // prefix commands with a ! + obj["n"] = "!" + std::string(fullname); // prefix commands with a ! } // add the custom name, is optional @@ -1362,11 +1362,11 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c sizeof(time_s), "%d %s %d %s %d %s", (time_value / 1440), - Helpers::translated_word(FL_(days)).c_str(), + Helpers::translated_word(FL_(days)), ((time_value % 1440) / 60), - Helpers::translated_word(FL_(hours)).c_str(), + Helpers::translated_word(FL_(hours)), (time_value % 60), - Helpers::translated_word(FL_(minutes)).c_str()); + Helpers::translated_word(FL_(minutes))); json[name] = time_s; } else { json[name] = time_value; diff --git a/src/emsdevice.h b/src/emsdevice.h index d4ea9ad0d..6a582e982 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -46,11 +46,11 @@ class EMSdevice { std::string device_type_name() const; - static std::string device_type_2_device_name(const uint8_t device_type); - static uint8_t device_name_2_device_type(const char * topic); - static std::string uom_to_string(uint8_t uom); - static std::string tag_to_string(uint8_t tag); - static std::string tag_to_mqtt(uint8_t tag); + static const char * device_type_2_device_name(const uint8_t device_type); + static uint8_t device_name_2_device_type(const char * topic); + static std::string uom_to_string(uint8_t uom); + static std::string tag_to_string(uint8_t tag); + static std::string tag_to_mqtt(uint8_t tag); bool has_tag(const uint8_t tag) const; diff --git a/src/emsdevicevalue.cpp b/src/emsdevicevalue.cpp index 87452232c..538930b70 100644 --- a/src/emsdevicevalue.cpp +++ b/src/emsdevicevalue.cpp @@ -74,7 +74,7 @@ DeviceValue::DeviceValue(uint8_t device_type, Serial.print(custom_fullname.c_str()); Serial.print(COLOR_RESET); } else { - Serial.print(Helpers::translated_word(fullname).c_str()); + Serial.print(Helpers::translated_word(fullname)); } Serial.print(" (#options="); Serial.print(options_size); @@ -88,7 +88,7 @@ DeviceValue::DeviceValue(uint8_t device_type, Serial.print(i + 1); Serial.print(":"); auto str = Helpers::translated_word(options[i]); - Serial.print((str.c_str())); + Serial.print(str); i++; } } else if (options_single != nullptr) { diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 9e12cb41c..e20342e2d 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -1039,7 +1039,7 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const fetch_device_values(device_id); // go and fetch its data // Print to LOG showing we've added a new device - LOG_INFO("Recognized new %s with deviceID 0x%02X", EMSdevice::device_type_2_device_name(device_type).c_str(), device_id); + LOG_INFO("Recognized new %s with deviceID 0x%02X", EMSdevice::device_type_2_device_name(device_type), device_id); // add command commands for all devices, except for connect, controller and gateway if ((device_type == DeviceType::CONNECT) || (device_type == DeviceType::CONTROLLER) || (device_type == DeviceType::GATEWAY)) { @@ -1073,7 +1073,8 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const FL_(entities_cmd)); // MQTT subscribe to the device e.g. "ems-esp/boiler/#" - Mqtt::subscribe(device_type, EMSdevice::device_type_2_device_name(device_type) + "/#", nullptr); + auto topic = std::string(EMSdevice::device_type_2_device_name(device_type)) + "/#"; + Mqtt::subscribe(device_type, topic, nullptr); return true; } diff --git a/src/helpers.cpp b/src/helpers.cpp index 2a248bce2..ce2ea6eb3 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -179,9 +179,9 @@ char * Helpers::render_boolean(char * result, const bool value, const bool dashb uint8_t bool_format_ = dashboard ? EMSESP::system_.bool_dashboard() : EMSESP::system_.bool_format(); if (bool_format_ == BOOL_FORMAT_ONOFF_STR) { - strlcpy(result, value ? translated_word(FL_(on)).c_str() : translated_word(FL_(off)).c_str(), 5); + strlcpy(result, value ? translated_word(FL_(on)) : translated_word(FL_(off)), 5); } else if (bool_format_ == BOOL_FORMAT_ONOFF_STR_CAP) { - strlcpy(result, value ? translated_word(FL_(ON)).c_str() : translated_word(FL_(OFF)).c_str(), 5); + strlcpy(result, value ? translated_word(FL_(ON)) : translated_word(FL_(OFF)), 5); } else if ((bool_format_ == BOOL_FORMAT_10) || (bool_format_ == BOOL_FORMAT_10_STR)) { strlcpy(result, value ? "1" : "0", 2); } else { @@ -593,12 +593,12 @@ bool Helpers::value2bool(const char * value, bool & value_b) { std::string bool_str = toLower(value); // convert to lower case - if ((bool_str == Helpers::translated_word(FL_(on))) || (bool_str == "on") || (bool_str == "1") || (bool_str == "true")) { + if ((bool_str == std::string(Helpers::translated_word(FL_(on)))) || (bool_str == "on") || (bool_str == "1") || (bool_str == "true")) { value_b = true; return true; // is a bool } - if ((bool_str == Helpers::translated_word(FL_(off))) || (bool_str == "off") || (bool_str == "0") || (bool_str == "false")) { + if ((bool_str == std::string(Helpers::translated_word(FL_(off)))) || (bool_str == "off") || (bool_str == "0") || (bool_str == "false")) { value_b = false; return true; // is a bool } @@ -615,7 +615,7 @@ bool Helpers::value2enum(const char * value, uint8_t & value_ui, const char * co std::string str = toLower(value); for (value_ui = 0; strs[value_ui]; value_ui++) { - std::string str1 = toLower(Helpers::translated_word(strs[value_ui])); + std::string str1 = toLower(std::string(Helpers::translated_word(strs[value_ui]))); std::string str2 = toLower((strs[value_ui][0])); // also check for default language if ((str1 != "") && ((str2 == "off" && str == "false") || (str2 == "on" && str == "true") || (str == str1) || (str == str2) @@ -627,19 +627,25 @@ bool Helpers::value2enum(const char * value, uint8_t & value_ui, const char * co return false; } -// checks to see if a string is member of a vector and return the index, also allow true/false for on/off +// finds the string (value) of a list vector (strs) +// returns true if found, and sets the value_ui to the index, else false +// also allow true/false for on/off bool Helpers::value2enum(const char * value, uint8_t & value_ui, const char * const * strs) { if ((value == nullptr) || (strlen(value) == 0)) { return false; } std::string str = toLower(value); + std::string s_on = Helpers::translated_word(FL_(on)); + std::string s_off = Helpers::translated_word(FL_(off)); + + // stops when a nullptr is found, which is the end delimeter of a MAKE_PSTR_LIST() + // could use count_items() to avoid buffer over-run but this works for (value_ui = 0; strs[value_ui]; value_ui++) { std::string enum_str = toLower((strs[value_ui])); if ((enum_str != "") - && ((enum_str == "off" && (str == Helpers::translated_word(FL_(off)) || str == "false")) - || (enum_str == "on" && (str == Helpers::translated_word(FL_(on)) || str == "true")) || (str == enum_str) + && ((enum_str == "off" && (str == s_off || str == "false")) || (enum_str == "on" && (str == s_on || str == "true")) || (str == enum_str) || (value[0] == ('0' + value_ui) && value[1] == '\0'))) { return true; } @@ -702,31 +708,13 @@ uint8_t Helpers::count_items(const char * const ** list) { return list_size; } -// return translated string as a std::string, optionally converting to lowercase (for console commands) -std::string Helpers::translated_word(const char * const * strings, bool to_lower) { - uint8_t language_index = EMSESP::system_.language_index(); - uint8_t index = 0; - - // check for empty - if (!strings) { - return ""; // it's a nullptr with no translations, return empty to prevent unwanted crash - } - - // see how many translations we have for this entity. if there is no translation for this, revert to EN - if (Helpers::count_items(strings) >= language_index + 1 && strlen(strings[language_index])) { - index = language_index; - } - return to_lower ? toLower((strings[index])) : (strings[index]); -} - // returns char pointer to translated description or fullname -const char * Helpers::translated_fullname(const char * const * strings) { +const char * Helpers::translated_word(const char * const * strings) { uint8_t language_index = EMSESP::system_.language_index(); uint8_t index = 0; - // check for empty if (!strings) { - return nullptr; // it's a nullptr with no translations, return empty to prevent unwanted crash + return ""; // no translations } // see how many translations we have for this entity. if there is no translation for this, revert to EN diff --git a/src/helpers.h b/src/helpers.h index 0c164f283..95fa7a5db 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -77,9 +77,7 @@ class Helpers { static uint8_t count_items(const char * const ** list); static uint8_t count_items(const char * const * list); - static std::string translated_word(const char * const * strings, bool to_lower = false); - static const char * translated_fullname(const char * const * strings); - + static const char * translated_word(const char * const * strings); #ifdef EMSESP_STANDALONE static char * ultostr(char * ptr, uint32_t value, const uint8_t base); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index b04ae6cf4..74a6385d8 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -899,15 +899,16 @@ void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const std::string & model, StaticJsonDocument dev_json; // always create the ids - JsonArray ids = dev_json.createNestedArray("ids"); - char ha_device[40]; - std::string device_type_name = EMSdevice::device_type_2_device_name(dv.device_type); - snprintf(ha_device, sizeof(ha_device), "ems-esp-%s", device_type_name.c_str()); + JsonArray ids = dev_json.createNestedArray("ids"); + char ha_device[40]; + auto device_type_name = EMSdevice::device_type_2_device_name(dv.device_type); + snprintf(ha_device, sizeof(ha_device), "ems-esp-%s", device_type_name); ids.add(ha_device); if (create_device_config) { - device_type_name[0] = toupper(device_type_name[0]); // capitalize - dev_json["name"] = "EMS-ESP " + device_type_name; + auto cap_name = strdup(device_type_name); + cap_name[0] = toupper(cap_name[0]); // capitalize + dev_json["name"] = std::string("EMS-ESP ") + cap_name; dev_json["mf"] = brand; dev_json["mdl"] = model; dev_json["via_device"] = "ems-esp"; @@ -972,8 +973,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdevice } // create the device name - char device_name[50]; - strlcpy(device_name, EMSdevice::device_type_2_device_name(device_type).c_str(), sizeof(device_name)); + auto device_name = EMSdevice::device_type_2_device_name(device_type); // create entity by add the hc/wwc tag if present, separating with a . char new_entity[50]; @@ -1341,11 +1341,13 @@ std::string Mqtt::tag_to_topic(uint8_t device_type, uint8_t tag) { return EMSdevice::tag_to_mqtt(tag); } + std::string topic = EMSdevice::device_type_2_device_name(device_type); + // if there is a tag add it if (!EMSdevice::tag_to_mqtt(tag).empty() && ((tag == DeviceValueTAG::TAG_BOILER_DATA_WW) || (!is_nested() && tag >= DeviceValueTAG::TAG_HC1))) { - return EMSdevice::device_type_2_device_name(device_type) + "_data_" + EMSdevice::tag_to_mqtt(tag); + return topic + "_data_" + EMSdevice::tag_to_mqtt(tag); } else { - return EMSdevice::device_type_2_device_name(device_type) + "_data"; + return topic + "_data"; } }