From 2dcf1b2bd80ea2474275254bec722aa8905bf6c5 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 8 Apr 2020 17:52:01 +0300 Subject: [PATCH] cfg: move DEVICE and MANUFACTURER to functions Avoids useless logs like this one: https://travis-ci.com/github/mcspr/espurna-nightly-builder/jobs/316891026#L449 --- code/espurna/board.h | 3 ++- code/espurna/board.ino | 23 +++++++++++++++++++---- code/espurna/homeassistant.ino | 9 +++++---- code/espurna/ssdp.ino | 6 ++++-- code/espurna/utils.h | 4 ++-- code/espurna/utils.ino | 24 +++++++++++++++++------- code/espurna/ws.ino | 10 ++++------ code/platformio.ini | 6 +++--- 8 files changed, 56 insertions(+), 29 deletions(-) diff --git a/code/espurna/board.h b/code/espurna/board.h index 222c6a41..a8274e57 100644 --- a/code/espurna/board.h +++ b/code/espurna/board.h @@ -6,7 +6,8 @@ BOARD MODULE #pragma once -String getIdentifier(); +const String& getChipId(); +const String& getIdentifier(); String getEspurnaModules(); String getEspurnaOTAModules(); diff --git a/code/espurna/board.ino b/code/espurna/board.ino index fa4b2d0c..0c6b1805 100644 --- a/code/espurna/board.ino +++ b/code/espurna/board.ino @@ -303,10 +303,25 @@ PROGMEM const char espurna_sensors[] = //-------------------------------------------------------------------------------- -String getIdentifier() { - char buffer[20]; - snprintf_P(buffer, sizeof(buffer), PSTR("%s-%06X"), APP_NAME, ESP.getChipId()); - return String(buffer); +const String& getChipId() { + static String value; + if (!value.length()) { + char buffer[7]; + value.reserve(sizeof(buffer)); + snprintf_P(buffer, sizeof(buffer), PSTR("%06X"), ESP.getChipId()); + value = buffer; + } + return value; +} + +const String& getIdentifier() { + static String value; + if (!value.length()) { + value += APP_NAME; + value += '-'; + value += getChipId(); + } + return value; } String getEspurnaModules() { diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index b7fc8fc7..551d0b9a 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -15,6 +15,7 @@ Copyright (C) 2017-2019 by Xose Pérez #include "mqtt.h" #include "relay.h" #include "rpc.h" +#include "utils.h" #include "ws.h" bool _ha_enabled = false; @@ -78,8 +79,8 @@ struct ha_config_t { deviceConfig.createNestedArray("identifiers").add(identifier.c_str()); deviceConfig["name"] = name.c_str(); deviceConfig["sw_version"] = version.c_str(); - deviceConfig["manufacturer"] = MANUFACTURER; - deviceConfig["model"] = DEVICE; + deviceConfig["manufacturer"] = getDevice().c_str(); + deviceConfig["model"] = getManufacturer().c_str(); } ha_config_t() : ha_config_t(DEFAULT_BUFFER_SIZE) {} @@ -394,8 +395,8 @@ void _haSensorYaml(unsigned char index, JsonObject& root) { void _haGetDeviceConfig(JsonObject& config) { config.createNestedArray("identifiers").add(getIdentifier()); config["name"] = getSetting("desc", getSetting("hostname")); - config["manufacturer"] = MANUFACTURER; - config["model"] = DEVICE; + config["manufacturer"] = getManufacturer().c_str(); + config["model"] = getDevice().c_str(); config["sw_version"] = String(APP_NAME) + " " + APP_VERSION + " (" + getCoreVersion() + ")"; } diff --git a/code/espurna/ssdp.ino b/code/espurna/ssdp.ino index b29573c9..bd65e61e 100644 --- a/code/espurna/ssdp.ino +++ b/code/espurna/ssdp.ino @@ -12,6 +12,8 @@ https://github.com/esp8266/Arduino/issues/2283#issuecomment-299635604 #include +#include "utils.h" + const char _ssdp_template[] PROGMEM = "" "" @@ -54,7 +56,7 @@ void ssdpSetup() { APP_NAME, // modelName APP_VERSION, // modelNumber APP_WEBSITE, // modelURL - DEVICE_NAME, // manufacturer + getBoardName().c_str(), // manufacturer "", // manufacturerURL chipId // UUID ); @@ -71,7 +73,7 @@ void ssdpSetup() { SSDP.setModelName(APP_NAME); SSDP.setModelNumber(APP_VERSION); SSDP.setModelURL(APP_WEBSITE); - SSDP.setManufacturer(DEVICE_NAME); + SSDP.setManufacturer(getBoardName()); SSDP.setManufacturerURL(""); SSDP.setURL("/"); SSDP.begin(); diff --git a/code/espurna/utils.h b/code/espurna/utils.h index 03197ff8..35b84273 100644 --- a/code/espurna/utils.h +++ b/code/espurna/utils.h @@ -18,9 +18,9 @@ extern "C" uint32_t _SPIFFS_end; void setDefaultHostname(); void setBoardName(); -String getBoardName(); -String getAdminPass(); +const String& getDevice(); +const String& getManufacturer(); const String& getCoreVersion(); const String& getCoreRevision(); diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index c1bd63ae..ab91423c 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -40,25 +40,35 @@ PROGMEM const char* const custom_reset_string[] = { void setDefaultHostname() { if (strlen(HOSTNAME) > 0) { - setSetting("hostname", HOSTNAME); + setSetting("hostname", F(HOSTNAME)); } else { setSetting("hostname", getIdentifier()); } } -void setBoardName() { - if (!isEspurnaCore()) { - setSetting("boardName", DEVICE_NAME); - } +const String& getDevice() { + static const String value(F(DEVICE)); + return value; +} + +const String& getManufacturer() { + static const String value(F(MANUFACTURER)); + return value; } String getBoardName() { - static const String defaultValue(DEVICE_NAME); + static const String defaultValue(F(DEVICE_NAME)); return getSetting("boardName", defaultValue); } +void setBoardName() { + if (!isEspurnaCore()) { + setSetting("boardName", F(DEVICE_NAME)); + } +} + String getAdminPass() { - static const String defaultValue(ADMIN_PASS); + static const String defaultValue(F(ADMIN_PASS)); return getSetting("adminPass", defaultValue); } diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 49a4a0de..a3e2d567 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -12,6 +12,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include "system.h" #include "web.h" +#include "utils.h" #include "ws.h" #include "ws_internal.h" @@ -387,9 +388,6 @@ bool _wsOnKeyCheck(const char * key, JsonVariant& value) { } void _wsOnConnected(JsonObject& root) { - char chipid[7]; - snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId()); - root["webMode"] = WEB_MODE_NORMAL; root["app_name"] = APP_NAME; @@ -398,12 +396,12 @@ void _wsOnConnected(JsonObject& root) { #if defined(APP_REVISION) root["app_revision"] = APP_REVISION; #endif - root["manufacturer"] = MANUFACTURER; - root["chipid"] = String(chipid); + root["device"] = getDevice().c_str(); + root["manufacturer"] = getManufacturer().c_str(); + root["chipid"] = getChipId().c_str(); root["mac"] = WiFi.macAddress(); root["bssid"] = WiFi.BSSIDstr(); root["channel"] = WiFi.channel(); - root["device"] = DEVICE; root["hostname"] = getSetting("hostname"); root["desc"] = getSetting("desc"); root["network"] = getNetwork(); diff --git a/code/platformio.ini b/code/platformio.ini index e82b286e..b2d34c82 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -280,15 +280,15 @@ build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_WPS=1 [env:espurna-core-webui-1MB] extends = env:esp8266-1m-base -src_build_flags = -DESPURNA_CORE_WEB +src_build_flags = -DESPURNA_CORE_WEBUI [env:espurna-core-webui-2MB] extends = env:esp8266-2m-base -src_build_flags = -DESPURNA_CORE_WEB +src_build_flags = -DESPURNA_CORE_WEBUI [env:espurna-core-webui-4MB] extends = env:esp8266-4m-base -src_build_flags = -DESPURNA_CORE_WEB +src_build_flags = -DESPURNA_CORE_WEBUI # ------------------------------------------------------------------------------ # DEVELOPMENT BOARDS