From 25f6b8c751113aff55afaea895ed4adff621898f Mon Sep 17 00:00:00 2001 From: Markus Frey Date: Thu, 19 Dec 2019 10:59:49 +0100 Subject: [PATCH 1/3] Fix the ability to provide `null` for `wifi.password` for open WiFi networks Additionally, this commit will now explicitly allow to omit `wifi.password` from the configuration. Leaving it out will be treated as setting it to `null`. --- src/Homie/Boot/BootConfig.cpp | 9 +++++++-- src/Homie/Config.cpp | 2 +- src/Homie/Utils/Validation.cpp | 16 +++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Homie/Boot/BootConfig.cpp b/src/Homie/Boot/BootConfig.cpp index d3cc1f53..6e39152f 100644 --- a/src/Homie/Boot/BootConfig.cpp +++ b/src/Homie/Boot/BootConfig.cpp @@ -131,8 +131,13 @@ void BootConfig::_onWifiConnectRequest(AsyncWebServerRequest *request) { JsonObject parsedJson = parseJsonDoc.as(); JsonVariant wifiSsid = parsedJson["ssid"]; JsonVariant wifiPassword = parsedJson["password"]; - if (!wifiSsid.as() || !wifiPassword.is()) { - __SendJSONError(request, F("✖ SSID and password required")); + if (!wifiSsid.as()) { + __SendJSONError(request, F("✖ SSID required")); + return; + } + + if (!wifiPassword.isNull() && !wifiPassword.is()) { + __SendJSONError(request, F("✖ Password is not a string")); return; } diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index ce98fdde..b3ff84e6 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -68,7 +68,6 @@ bool Config::load() { const char* reqName = parsedJson["name"]; const char* reqWifiSsid = reqWifi["ssid"]; - const char* reqWifiPassword = reqWifi["password"]; const char* reqMqttHost = reqMqtt["host"]; /* Optional config items */ @@ -78,6 +77,7 @@ bool Config::load() { uint16_t reqWifiChannel = reqWifi["channel"] | 0; const char* reqWifiBssid = reqWifi["bssid"] | ""; + const char* reqWifiPassword = reqWifi["password"]; // implicit | nullptr; const char* reqWifiIp = reqWifi["ip"] | ""; const char* reqWifiMask = reqWifi["mask"] | ""; const char* reqWifiGw = reqWifi["gw"] | ""; diff --git a/src/Homie/Utils/Validation.cpp b/src/Homie/Utils/Validation.cpp index 9feac1d7..b603491d 100644 --- a/src/Homie/Utils/Validation.cpp +++ b/src/Homie/Utils/Validation.cpp @@ -102,13 +102,15 @@ ConfigValidationResult Validation::_validateConfigWifi(const JsonObject object) { JsonVariant wifiPassword = wifi["password"]; - if (!wifiPassword.is()) { - result.reason = F("wifi.password is not a string"); - return result; - } - if (wifiPassword.as() && strlen(wifiPassword.as()) + 1 > MAX_WIFI_PASSWORD_LENGTH) { - result.reason = F("wifi.password is too long"); - return result; + if (!wifiPassword.isNull()) { + if (!wifiPassword.as()) { + result.reason = F("wifi.password is not a string"); + return result; + } + if (strlen(wifiPassword.as()) + 1 > MAX_WIFI_PASSWORD_LENGTH) { + result.reason = F("wifi.password is too long"); + return result; + } } } From 64ca0bad9d412d8a0e5bcab292d3f996f743eb78 Mon Sep 17 00:00:00 2001 From: Markus Frey Date: Thu, 19 Dec 2019 11:12:41 +0100 Subject: [PATCH 2/3] Make `ota.enabled` configuration optional * OTA will be disabled by default if `ota.enabled` is not provided --- src/Homie/Utils/Validation.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Homie/Utils/Validation.cpp b/src/Homie/Utils/Validation.cpp index b603491d..372f3ff2 100644 --- a/src/Homie/Utils/Validation.cpp +++ b/src/Homie/Utils/Validation.cpp @@ -345,17 +345,19 @@ ConfigValidationResult Validation::_validateConfigOta(const JsonObject object) { JsonVariant ota = object["ota"]; - if (!ota.is()) { - result.reason = F("ota is not an object"); - return result; - } + if (!ota.isNull()) { + if (!ota.is()) { + result.reason = F("ota is not an object"); + return result; + } - { - JsonVariant otaEnabled = ota["enabled"]; + { + JsonVariant otaEnabled = ota["enabled"]; - if (!otaEnabled.is()) { - result.reason = F("ota.enabled is not a boolean"); - return result; + if (!otaEnabled.isNull() && !otaEnabled.is()) { + result.reason = F("ota.enabled is not a boolean"); + return result; + } } } From 60ca263c1c39051bc62dd4e96942067062f4fd3e Mon Sep 17 00:00:00 2001 From: Markus Frey Date: Thu, 19 Dec 2019 11:50:28 +0100 Subject: [PATCH 3/3] Avoid unnecessary copying in Config::getSafeConfigFile() --- src/Homie/Boot/BootNormal.cpp | 2 +- src/Homie/Config.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index 15a2a7a2..b97b158f 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -472,7 +472,7 @@ void BootNormal::_advertise() { { char* safeConfigFile = Interface::get().getConfig().getSafeConfigFile(); packetId = Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation/config")), 1, true, safeConfigFile); - free(safeConfigFile); + delete safeConfigFile; if (packetId != 0) _advertisementProgress.globalStep = AdvertisementProgress::GlobalStep::PUB_IMPLEMENTATION_VERSION; break; } diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index b3ff84e6..2c41893a 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -164,9 +164,9 @@ char* Config::getSafeConfigFile() const { parsedJson["mqtt"].as().remove("password"); size_t jsonBufferLength = measureJson(jsonDoc) + 1; - std::unique_ptr jsonString(new char[jsonBufferLength]); - serializeJson(jsonDoc, jsonString.get(), jsonBufferLength); - return strdup(jsonString.get()); + char* jsonString = new char[jsonBufferLength]; + serializeJson(jsonDoc, jsonString, jsonBufferLength); + return jsonString; } void Config::erase() {