diff --git a/code/espurna/alexa.cpp b/code/espurna/alexa.cpp index ab833907..3215238c 100644 --- a/code/espurna/alexa.cpp +++ b/code/espurna/alexa.cpp @@ -84,7 +84,7 @@ bool enabled() { String hostname() { auto out = getSetting("alexaName", build::hostname()); if (!out.length()) { - out = getSetting("hostname", getIdentifier()); + out = getHostname(); } return out; diff --git a/code/espurna/debug.cpp b/code/espurna/debug.cpp index 2e37c06b..6cc45238 100644 --- a/code/espurna/debug.cpp +++ b/code/espurna/debug.cpp @@ -429,13 +429,6 @@ constexpr bool enabled() { } } // namespace build -namespace settings { - -String hostname() { - return getSetting("hostname", getIdentifier()); -} - -} // namespace settings namespace internal { @@ -453,7 +446,7 @@ void configure() { snprintf_P( internal::header, sizeof(internal::header), PSTR("<%u>1 - %.31s ESPurna - - - "), DEBUG_UDP_FAC_PRI, - settings::hostname().c_str()); + getHostname().c_str()); } bool output(const char* message, size_t len) { diff --git a/code/espurna/homeassistant.cpp b/code/espurna/homeassistant.cpp index ff9bc0f4..2c4cf853 100644 --- a/code/espurna/homeassistant.cpp +++ b/code/espurna/homeassistant.cpp @@ -27,6 +27,37 @@ Copyright (C) 2019-2021 by Maxim Prokhorov ; - static constexpr size_t BufferSize { JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(5) }; using Buffer = StaticJsonBuffer; @@ -94,8 +101,10 @@ public: Device(const Device&) = delete; Device(Device&&) = default; - Device(String&& prefix, String&& name, const String& identifier, const char* version, const char* manufacturer, const char* device) : - _strings(std::make_unique(std::move(prefix), std::move(name), identifier, version, manufacturer, device)), + + template + Device(Args&&... args) : + _strings(make_strings(std::forward(args)...)), _buffer(std::make_unique()), _root(_buffer->createObject()) { @@ -125,6 +134,29 @@ public: } private: + struct Strings { + String prefix; + String name; + String identifier; + const char* version; + const char* manufacturer; + const char* device; + }; + + using StringsPtr = std::unique_ptr; + + StringsPtr make_strings(String prefix, String name, String identifier, const char* version, const char* manufacturer, const char* device) { + return std::make_unique( + Strings{ + std::move(prefix), + normalize_ascii(std::move(name), false), + normalize_ascii(std::move(identifier), true), + version, + manufacturer, + device + }); + } + StringsPtr _strings; BufferPtr _buffer; JsonObject& _root; @@ -191,8 +223,8 @@ private: Context makeContext() { auto device = std::make_unique( - getSetting("haPrefix", HOMEASSISTANT_PREFIX), - getSetting("hostname", getIdentifier()), + settings::prefix(), + getHostname(), getIdentifier(), getVersion(), getManufacturer(), @@ -891,8 +923,8 @@ void publishDiscovery() { void configure() { bool current = internal::enabled; - internal::enabled = getSetting("haEnabled", 1 == HOMEASSISTANT_ENABLED); - internal::retain = getSetting("haRetain", 1 == HOMEASSISTANT_RETAIN); + internal::enabled = settings::enabled(); + internal::retain = settings::retain(); if (internal::enabled != current) { internal::state = internal::State::Pending; @@ -938,9 +970,9 @@ void onVisible(JsonObject& root) { } void onConnected(JsonObject& root) { - root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX); - root["haEnabled"] = getSetting("haEnabled", 1 == HOMEASSISTANT_ENABLED); - root["haRetain"] = getSetting("haRetain", 1 == HOMEASSISTANT_RETAIN); + root["haPrefix"] = settings::prefix(); + root["haEnabled"] = settings::enabled(); + root["haRetain"] = settings::retain(); } bool onKeyCheck(const char* key, JsonVariant& value) { diff --git a/code/espurna/influxdb.cpp b/code/espurna/influxdb.cpp index 6e3e06c1..500ff64d 100644 --- a/code/espurna/influxdb.cpp +++ b/code/espurna/influxdb.cpp @@ -215,7 +215,7 @@ void _idbFlush() { // TODO: should we always store specific pairs like tspk keeps relay / sensor readings? // note that we also send heartbeat data, persistent values should be flagged - const String device = getSetting("hostname"); + const String device = getHostname(); _idb_client->payload = ""; for (auto& pair : _idb_client->values) { diff --git a/code/espurna/llmnr.cpp b/code/espurna/llmnr.cpp index 4d8aef85..cced26a4 100644 --- a/code/espurna/llmnr.cpp +++ b/code/espurna/llmnr.cpp @@ -6,14 +6,16 @@ Copyright (C) 2017-2019 by Xose Pérez */ -#include "llmnr.h" +#include "espurna.h" #if LLMNR_SUPPORT #include +#include "llmnr.h" + void llmnrSetup() { - auto hostname = getSetting("hostname", getIdentifier()); + auto hostname = getHostname(); LLMNR.begin(hostname.c_str()); DEBUG_MSG_P(PSTR("[LLMNR] Configured for %s\n"), hostname.c_str()); } diff --git a/code/espurna/llmnr.h b/code/espurna/llmnr.h index ba170503..e81b5710 100644 --- a/code/espurna/llmnr.h +++ b/code/espurna/llmnr.h @@ -6,6 +6,4 @@ Copyright (C) 2017-2019 by Xose Pérez */ -#include "espurna.h" - void llmnrSetup(); diff --git a/code/espurna/main.cpp b/code/espurna/main.cpp index d4e23479..bea5dba2 100644 --- a/code/espurna/main.cpp +++ b/code/espurna/main.cpp @@ -119,9 +119,7 @@ void setup() { #endif // Hostname & board name initialization - if (getSetting("hostname").length() == 0) { - setDefaultHostname(); - } + setDefaultHostname(); setBoardName(); boardSetup(); diff --git a/code/espurna/mdns.cpp b/code/espurna/mdns.cpp index 14000e72..6f61ca49 100644 --- a/code/espurna/mdns.cpp +++ b/code/espurna/mdns.cpp @@ -25,13 +25,6 @@ Copyright (C) 2017-2019 by Xose Pérez namespace mdns { namespace { -namespace settings { - -String hostname() { - return getSetting("hostname", getIdentifier()); -} - -} // namespace settings void addServices() { #if WEB_SUPPORT @@ -61,7 +54,7 @@ void addServices() { } void start() { - auto hostname = settings::hostname(); + auto hostname = getHostname(); if (MDNS.begin(hostname)) { DEBUG_MSG_P(PSTR("[MDNS] Started with hostname %s\n"), hostname.c_str()); addServices(); diff --git a/code/espurna/mqtt.cpp b/code/espurna/mqtt.cpp index 4270c060..d84da63e 100644 --- a/code/espurna/mqtt.cpp +++ b/code/espurna/mqtt.cpp @@ -507,7 +507,7 @@ bool _mqttConnectSyncClient(bool secure = false) { #endif // (MQTT_LIBRARY == MQTT_LIBRARY_ARDUINOMQTT) || (MQTT_LIBRARY == MQTT_LIBRARY_PUBSUBCLIENT) String _mqttPlaceholders(String&& text) { - text.replace("{hostname}", getSetting("hostname", getIdentifier())); + text.replace("{hostname}", getHostname()); text.replace("{magnitude}", "#"); text.replace("{mac}", getFullChipId()); return text; @@ -875,10 +875,10 @@ bool _mqttHeartbeat(heartbeat::Mask mask) { mqttSend(MQTT_TOPIC_BOARD, getBoardName().c_str()); if (mask & heartbeat::Report::Hostname) - mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname", getIdentifier()).c_str()); + mqttSend(MQTT_TOPIC_HOSTNAME, getHostname().c_str()); if (mask & heartbeat::Report::Description) { - auto desc = getSetting("desc"); + auto desc = getDescription(); if (desc.length()) { mqttSend(MQTT_TOPIC_DESCRIPTION, desc.c_str()); } @@ -1246,7 +1246,7 @@ void mqttFlush() { root[MQTT_TOPIC_MAC] = WiFi.macAddress(); #endif #if MQTT_ENQUEUE_HOSTNAME - root[MQTT_TOPIC_HOSTNAME] = getSetting("hostname", getIdentifier()); + root[MQTT_TOPIC_HOSTNAME] = getHostname(); #endif #if MQTT_ENQUEUE_IP root[MQTT_TOPIC_IP] = wifiStaIp().toString(); diff --git a/code/espurna/netbios.cpp b/code/espurna/netbios.cpp index 28bcdb85..ecf72c7f 100644 --- a/code/espurna/netbios.cpp +++ b/code/espurna/netbios.cpp @@ -6,15 +6,17 @@ Copyright (C) 2017-2019 by Xose Pérez */ -#include "netbios.h" +#include "espurna.h" #if NETBIOS_SUPPORT #include +#include "netbios.h" + void netbiosSetup() { static WiFiEventHandler _netbios_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) { - auto hostname = getSetting("hostname", getIdentifier()); + auto hostname = getHostname(); NBNS.begin(hostname.c_str()); DEBUG_MSG_P(PSTR("[NETBIOS] Configured for %s\n"), hostname.c_str()); }); diff --git a/code/espurna/netbios.h b/code/espurna/netbios.h index 31b197bc..fc40829d 100644 --- a/code/espurna/netbios.h +++ b/code/espurna/netbios.h @@ -6,6 +6,4 @@ Copyright (C) 2017-2019 by Xose Pérez */ -#include "espurna.h" - void netbiosSetup(); diff --git a/code/espurna/ota_web.cpp b/code/espurna/ota_web.cpp index fcc32caa..d5b54555 100644 --- a/code/espurna/ota_web.cpp +++ b/code/espurna/ota_web.cpp @@ -56,7 +56,7 @@ void setStatus(AsyncWebServerRequest *request, int code, const String& payload = void onUpgrade(AsyncWebServerRequest *request) { if (!webAuthenticate(request)) { - return request->requestAuthentication(getSetting("hostname").c_str()); + return request->requestAuthentication(getHostname().c_str()); } if (request->_tempObject) { @@ -68,7 +68,7 @@ void onUpgrade(AsyncWebServerRequest *request) { void onFile(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { if (!webAuthenticate(request)) { - return request->requestAuthentication(getSetting("hostname").c_str()); + return request->requestAuthentication(getHostname().c_str()); } // We set this after we are done with the request diff --git a/code/espurna/ssdp.cpp b/code/espurna/ssdp.cpp index 9589cdb4..e03e397f 100644 --- a/code/espurna/ssdp.cpp +++ b/code/espurna/ssdp.cpp @@ -15,18 +15,13 @@ https://github.com/esp8266/Arduino/issues/2283#issuecomment-299635604 #include #include "web.h" -#include "utils.h" namespace ssdp { namespace { namespace settings { -String hostname() { - return getSetting("hostname", getIdentifier()); -} - String name() { - return getSetting("ssdpName", hostname()); + return getSetting("ssdpName", getHostname()); } // needs to be in the response diff --git a/code/espurna/utils.cpp b/code/espurna/utils.cpp index d592808f..22ac85dd 100644 --- a/code/espurna/utils.cpp +++ b/code/espurna/utils.cpp @@ -23,11 +23,25 @@ bool tryParseId(const char* p, TryParseIdFunc limit, size_t& out) { return true; } -void setDefaultHostname() { +String getDescription() { + return getSetting("desc"); +} + +String getHostname() { if (strlen(HOSTNAME) > 0) { - setSetting("hostname", F(HOSTNAME)); - } else { - setSetting("hostname", getIdentifier()); + return getSetting("hostname", F(HOSTNAME)); + } + + return getSetting("hostname", getIdentifier()); +} + +void setDefaultHostname() { + if (!getSetting("hostname").length()) { + if (strlen(HOSTNAME) > 0) { + setSetting("hostname", F(HOSTNAME)); + } else { + setSetting("hostname", getIdentifier()); + } } } diff --git a/code/espurna/utils.h b/code/espurna/utils.h index f6c22b36..621e9327 100644 --- a/code/espurna/utils.h +++ b/code/espurna/utils.h @@ -29,6 +29,8 @@ const char* getAppWebsite(); const char* getDevice(); const char* getManufacturer(); +String getDescription(); +String getHostname(); String getAdminPass(); String getBoardName(); String buildTime(); diff --git a/code/espurna/web.cpp b/code/espurna/web.cpp index 84dd8633..8c0e21a8 100644 --- a/code/espurna/web.cpp +++ b/code/espurna/web.cpp @@ -230,7 +230,7 @@ constexpr unsigned long WebConfigBufferMax { 4096ul }; namespace { void _webRequestAuth(AsyncWebServerRequest* request) { - request->requestAuthentication(getSetting("hostname", getIdentifier()).c_str(), true); + request->requestAuthentication(getHostname().c_str(), true); } void _onReset(AsyncWebServerRequest *request) { @@ -249,7 +249,7 @@ void _onDiscover(AsyncWebServerRequest *request) { webLog(request); const String device = getBoardName(); - const String hostname = getSetting("hostname"); + const String hostname = getHostname(); StaticJsonBuffer jsonBuffer; JsonObject &root = jsonBuffer.createObject(); @@ -297,7 +297,7 @@ void _onGetConfig(AsyncWebServerRequest *request) { }); *out += "\n}"; - auto hostname = getSetting("hostname", getIdentifier()); + auto hostname = getHostname(); auto timestamp = String(millis()); #if NTP_SUPPORT if (ntpSynced()) { @@ -499,7 +499,7 @@ int _onCertificate(void * arg, const char *filename, uint8_t **buf) { bool _onAPModeRequest(AsyncWebServerRequest *request) { if ((WiFi.getMode() & WIFI_AP) > 0) { - const String domain = getSetting("hostname") + "."; + const String domain = getHostname() + "."; const String host = request->header("Host"); const String ip = WiFi.softAPIP().toString(); diff --git a/code/espurna/wifi.cpp b/code/espurna/wifi.cpp index 86e60129..fc1589d9 100644 --- a/code/espurna/wifi.cpp +++ b/code/espurna/wifi.cpp @@ -604,10 +604,6 @@ int8_t scanRssiThreshold() { return getSetting("wifiScanRssi", wifi::build::scanRssiThreshold()); } -String hostname() { - return getSetting("hostname", getIdentifier()); -} - wifi::StaMode staMode() { return getSetting("wifiStaMode", wifi::build::staMode()); } @@ -664,7 +660,7 @@ String softApDefaultSsid() { String softApSsid() { return getSetting("wifiApSsid", wifi::build::hasSoftApSsid() ? wifi::build::softApSsid() - : hostname()); + : getHostname()); } String softApPassphrase() { @@ -2521,7 +2517,7 @@ void loop() { case State::Connect: { if (!wifi::sta::connecting()) { - if (!wifi::sta::connection::start(wifi::settings::hostname())) { + if (!wifi::sta::connection::start(getHostname())) { state = State::Timeout; break; } diff --git a/code/espurna/ws.cpp b/code/espurna/ws.cpp index 4bf4299e..d963640a 100644 --- a/code/espurna/ws.cpp +++ b/code/espurna/ws.cpp @@ -594,8 +594,8 @@ void _wsOnConnected(JsonObject& root) { root["mac"] = getFullChipId().c_str(); root["bssid"] = WiFi.BSSIDstr(); root["channel"] = WiFi.channel(); - root["hostname"] = getSetting("hostname", getIdentifier()); - root["desc"] = getSetting("desc"); + root["hostname"] = getHostname(); + root["desc"] = getDescription(); root["network"] = wifiStaSsid(); root["deviceip"] = wifiStaIp().toString(); root["sketch_size"] = ESP.getSketchSize();