diff --git a/lib/framework/APStatus.cpp b/lib/framework/APStatus.cpp index 76a4eb16c..620d94384 100644 --- a/lib/framework/APStatus.cpp +++ b/lib/framework/APStatus.cpp @@ -1,10 +1,10 @@ #include +using namespace std::placeholders; // for `_1` etc + APStatus::APStatus(AsyncWebServer * server, SecurityManager * securityManager, APSettingsService * apSettingsService) : _apSettingsService(apSettingsService) { - server->on(AP_STATUS_SERVICE_PATH, - HTTP_GET, - securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); + server->on(AP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); } void APStatus::apStatus(AsyncWebServerRequest * request) { diff --git a/lib/framework/APStatus.h b/lib/framework/APStatus.h index e5164b7ac..defe5f530 100644 --- a/lib/framework/APStatus.h +++ b/lib/framework/APStatus.h @@ -1,13 +1,8 @@ #ifndef APStatus_h #define APStatus_h -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif #include #include diff --git a/lib/framework/ArduinoJsonJWT.cpp b/lib/framework/ArduinoJsonJWT.cpp index 577948cb4..adca88f3e 100644 --- a/lib/framework/ArduinoJsonJWT.cpp +++ b/lib/framework/ArduinoJsonJWT.cpp @@ -15,14 +15,13 @@ String ArduinoJsonJWT::getSecret() { /* * ESP32 uses mbedtls, ESP2866 uses bearssl. * - * Both come with decent HMAC implmentations supporting sha256, as well as others. + * Both come with decent HMAC implementations supporting sha256, as well as others. * * No need to pull in additional crypto libraries - lets use what we already have. */ String ArduinoJsonJWT::sign(String & payload) { unsigned char hmacResult[32]; { -#ifdef ESP32 mbedtls_md_context_t ctx; mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256; mbedtls_md_init(&ctx); @@ -31,14 +30,6 @@ String ArduinoJsonJWT::sign(String & payload) { mbedtls_md_hmac_update(&ctx, (unsigned char *)payload.c_str(), payload.length()); mbedtls_md_hmac_finish(&ctx, hmacResult); mbedtls_md_free(&ctx); -#elif defined(ESP8266) - br_hmac_key_context keyCtx; - br_hmac_key_init(&keyCtx, &br_sha256_vtable, _secret.c_str(), _secret.length()); - br_hmac_context hmacCtx; - br_hmac_init(&hmacCtx, &keyCtx, 0); - br_hmac_update(&hmacCtx, payload.c_str(), payload.length()); - br_hmac_out(&hmacCtx, hmacResult); -#endif } return encode((char *)hmacResult, 32); } @@ -94,13 +85,8 @@ void ArduinoJsonJWT::parseJWT(String jwt, JsonDocument & jsonDocument) { String ArduinoJsonJWT::encode(const char * cstr, int inputLen) { // prepare encoder base64_encodestate _state; -#ifdef ESP32 base64_init_encodestate(&_state); size_t encodedLength = base64_encode_expected_len(inputLen) + 1; -#elif defined(ESP8266) - base64_init_encodestate_nonewlines(&_state); - size_t encodedLength = base64_encode_expected_len_nonewlines(inputLen) + 1; -#endif // prepare buffer of correct length, returning an empty string on failure char * buffer = (char *)malloc(encodedLength * sizeof(char)); if (buffer == nullptr) { diff --git a/lib/framework/ArduinoJsonJWT.h b/lib/framework/ArduinoJsonJWT.h index edad6e92e..3a76f71f6 100644 --- a/lib/framework/ArduinoJsonJWT.h +++ b/lib/framework/ArduinoJsonJWT.h @@ -5,12 +5,7 @@ #include #include #include - -#ifdef ESP32 #include -#elif defined(ESP8266) -#include -#endif class ArduinoJsonJWT { private: diff --git a/lib/framework/AuthenticationService.cpp b/lib/framework/AuthenticationService.cpp index c0a4ab601..3886e1c58 100644 --- a/lib/framework/AuthenticationService.cpp +++ b/lib/framework/AuthenticationService.cpp @@ -1,11 +1,13 @@ #include +using namespace std::placeholders; // for `_1` etc + #if FT_ENABLED(FT_SECURITY) AuthenticationService::AuthenticationService(AsyncWebServer * server, SecurityManager * securityManager) : _securityManager(securityManager) - , _signInHandler(SIGN_IN_PATH, std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2)) { - server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1)); + , _signInHandler(SIGN_IN_PATH, std::bind(&AuthenticationService::signIn, this, _1, _2)) { + server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, std::bind(&AuthenticationService::verifyAuthorization, this, _1)); _signInHandler.setMethod(HTTP_POST); _signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE); server->addHandler(&_signInHandler); diff --git a/lib/framework/ESPUtils.h b/lib/framework/ESPUtils.h index 5e6170729..8674aba94 100644 --- a/lib/framework/ESPUtils.h +++ b/lib/framework/ESPUtils.h @@ -6,11 +6,7 @@ class ESPUtils { public: static String defaultDeviceValue(String prefix = "") { -#ifdef ESP32 return prefix + String((uint32_t)ESP.getEfuseMac(), HEX); -#elif defined(ESP8266) - return prefix + String(ESP.getChipId(), HEX); -#endif } }; diff --git a/lib/framework/FactoryResetService.cpp b/lib/framework/FactoryResetService.cpp index e97fa7367..4e79c8ffb 100644 --- a/lib/framework/FactoryResetService.cpp +++ b/lib/framework/FactoryResetService.cpp @@ -16,7 +16,6 @@ void FactoryResetService::handleRequest(AsyncWebServerRequest * request) { * Delete function assumes that all files are stored flat, within the config directory. */ void FactoryResetService::factoryReset() { -#ifdef ESP32 /* * Based on LITTLEFS. Modified by proddy * Could be replaced with fs.rmdir(FS_CONFIG_DIRECTORY) in IDF 4.2 @@ -28,14 +27,5 @@ void FactoryResetService::factoryReset() { file.close(); fs->remove(pathStr); } -#elif defined(ESP8266) - Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY); - while (configDirectory.next()) { - String path = FS_CONFIG_DIRECTORY; - path.concat("/"); - path.concat(configDirectory.fileName()); - fs->remove(path); - } -#endif RestartService::restartNow(); } diff --git a/lib/framework/FactoryResetService.h b/lib/framework/FactoryResetService.h index bf065a073..b30ca1c6c 100644 --- a/lib/framework/FactoryResetService.h +++ b/lib/framework/FactoryResetService.h @@ -1,14 +1,8 @@ #ifndef FactoryResetService_h #define FactoryResetService_h -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif - #include #include #include diff --git a/lib/framework/FeaturesService.cpp b/lib/framework/FeaturesService.cpp index 98953048a..67a97cd36 100644 --- a/lib/framework/FeaturesService.cpp +++ b/lib/framework/FeaturesService.cpp @@ -1,7 +1,9 @@ #include +using namespace std::placeholders; // for `_1` etc + FeaturesService::FeaturesService(AsyncWebServer * server) { - server->on(FEATURES_SERVICE_PATH, HTTP_GET, std::bind(&FeaturesService::features, this, std::placeholders::_1)); + server->on(FEATURES_SERVICE_PATH, HTTP_GET, std::bind(&FeaturesService::features, this, _1)); } void FeaturesService::features(AsyncWebServerRequest * request) { diff --git a/lib/framework/FeaturesService.h b/lib/framework/FeaturesService.h index 7caa42d94..9c446fd47 100644 --- a/lib/framework/FeaturesService.h +++ b/lib/framework/FeaturesService.h @@ -3,14 +3,8 @@ #include -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif - #include #include #include diff --git a/lib/framework/HttpEndpoint.h b/lib/framework/HttpEndpoint.h index 851e5350b..3951c459f 100644 --- a/lib/framework/HttpEndpoint.h +++ b/lib/framework/HttpEndpoint.h @@ -11,6 +11,8 @@ #define HTTP_ENDPOINT_ORIGIN_ID "http" +using namespace std::placeholders; // for `_1` etc + template class HttpGetEndpoint { public: @@ -24,20 +26,14 @@ class HttpGetEndpoint { : _stateReader(stateReader) , _statefulService(statefulService) , _bufferSize(bufferSize) { - server->on(servicePath.c_str(), - HTTP_GET, - securityManager->wrapRequest(std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1), authenticationPredicate)); + server->on(servicePath.c_str(), HTTP_GET, securityManager->wrapRequest(std::bind(&HttpGetEndpoint::fetchSettings, this, _1), authenticationPredicate)); } - HttpGetEndpoint(JsonStateReader stateReader, - StatefulService * statefulService, - AsyncWebServer * server, - const String & servicePath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + HttpGetEndpoint(JsonStateReader stateReader, StatefulService * statefulService, AsyncWebServer * server, const String & servicePath, size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader) , _statefulService(statefulService) , _bufferSize(bufferSize) { - server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1)); + server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, _1)); } protected: @@ -69,25 +65,17 @@ class HttpPostEndpoint { : _stateReader(stateReader) , _stateUpdater(stateUpdater) , _statefulService(statefulService) - , _updateHandler(servicePath, - securityManager->wrapCallback(std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2), - authenticationPredicate), - bufferSize) + , _updateHandler(servicePath, securityManager->wrapCallback(std::bind(&HttpPostEndpoint::updateSettings, this, _1, _2), authenticationPredicate), bufferSize) , _bufferSize(bufferSize) { _updateHandler.setMethod(HTTP_POST); server->addHandler(&_updateHandler); } - HttpPostEndpoint(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncWebServer * server, - const String & servicePath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + HttpPostEndpoint(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService * statefulService, AsyncWebServer * server, const String & servicePath, size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader) , _stateUpdater(stateUpdater) , _statefulService(statefulService) - , _updateHandler(servicePath, std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2), bufferSize) + , _updateHandler(servicePath, std::bind(&HttpPostEndpoint::updateSettings, this, _1, _2), bufferSize) , _bufferSize(bufferSize) { _updateHandler.setMethod(HTTP_POST); server->addHandler(&_updateHandler); @@ -137,12 +125,7 @@ class HttpEndpoint : public HttpGetEndpoint, public HttpPostEndpoint { , HttpPostEndpoint(stateReader, stateUpdater, statefulService, server, servicePath, securityManager, authenticationPredicate, bufferSize) { } - HttpEndpoint(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncWebServer * server, - const String & servicePath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + HttpEndpoint(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService * statefulService, AsyncWebServer * server, const String & servicePath, size_t bufferSize = DEFAULT_BUFFER_SIZE) : HttpGetEndpoint(stateReader, statefulService, server, servicePath, bufferSize) , HttpPostEndpoint(stateReader, stateUpdater, statefulService, server, servicePath, bufferSize) { } diff --git a/lib/framework/MqttPubSub.h b/lib/framework/MqttPubSub.h index f28c0c7b3..d15152abf 100644 --- a/lib/framework/MqttPubSub.h +++ b/lib/framework/MqttPubSub.h @@ -4,6 +4,8 @@ #include #include +using namespace std::placeholders; // for `_1` etc + #define MQTT_ORIGIN_ID "mqtt" template @@ -31,11 +33,7 @@ class MqttConnector { template class MqttPub : virtual public MqttConnector { public: - MqttPub(JsonStateReader stateReader, - StatefulService * statefulService, - AsyncMqttClient * mqttClient, - const String & pubTopic = "", - size_t bufferSize = DEFAULT_BUFFER_SIZE) + MqttPub(JsonStateReader stateReader, StatefulService * statefulService, AsyncMqttClient * mqttClient, const String & pubTopic = "", size_t bufferSize = DEFAULT_BUFFER_SIZE) : MqttConnector(statefulService, mqttClient, bufferSize) , _stateReader(stateReader) , _pubTopic(pubTopic) { @@ -76,22 +74,11 @@ class MqttPub : virtual public MqttConnector { template class MqttSub : virtual public MqttConnector { public: - MqttSub(JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncMqttClient * mqttClient, - const String & subTopic = "", - size_t bufferSize = DEFAULT_BUFFER_SIZE) + MqttSub(JsonStateUpdater stateUpdater, StatefulService * statefulService, AsyncMqttClient * mqttClient, const String & subTopic = "", size_t bufferSize = DEFAULT_BUFFER_SIZE) : MqttConnector(statefulService, mqttClient, bufferSize) , _stateUpdater(stateUpdater) , _subTopic(subTopic) { - MqttConnector::_mqttClient->onMessage(std::bind(&MqttSub::onMqttMessage, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4, - std::placeholders::_5, - std::placeholders::_6)); + MqttConnector::_mqttClient->onMessage(std::bind(&MqttSub::onMqttMessage, this, _1, _2, _3, _4, _5, _6)); } void setSubTopic(const String & subTopic) { diff --git a/lib/framework/MqttStatus.cpp b/lib/framework/MqttStatus.cpp index b1bd0257e..dc75cc9fc 100644 --- a/lib/framework/MqttStatus.cpp +++ b/lib/framework/MqttStatus.cpp @@ -2,11 +2,13 @@ #include "../../src/emsesp_stub.hpp" // proddy added +using namespace std::placeholders; // for `_1` etc + MqttStatus::MqttStatus(AsyncWebServer * server, MqttSettingsService * mqttSettingsService, SecurityManager * securityManager) : _mqttSettingsService(mqttSettingsService) { server->on(MQTT_STATUS_SERVICE_PATH, HTTP_GET, - securityManager->wrapRequest(std::bind(&MqttStatus::mqttStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); + securityManager->wrapRequest(std::bind(&MqttStatus::mqttStatus, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); } void MqttStatus::mqttStatus(AsyncWebServerRequest * request) { diff --git a/lib/framework/MqttStatus.h b/lib/framework/MqttStatus.h index 8e19013e8..04ef6f1d3 100644 --- a/lib/framework/MqttStatus.h +++ b/lib/framework/MqttStatus.h @@ -1,14 +1,8 @@ #ifndef MqttStatus_h #define MqttStatus_h -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif - #include #include #include diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index 3dc0311a0..f3f9ff2f1 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -2,15 +2,17 @@ #include "../../src/emsesp_stub.hpp" // proddy added +using namespace std::placeholders; // for `_1` etc + NTPSettingsService::NTPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) : _httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager) , _fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE) - , _timeHandler(TIME_PATH, securityManager->wrapCallback(std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2), AuthenticationPredicates::IS_ADMIN)) { + , _timeHandler(TIME_PATH, securityManager->wrapCallback(std::bind(&NTPSettingsService::configureTime, this, _1, _2), AuthenticationPredicates::IS_ADMIN)) { _timeHandler.setMethod(HTTP_POST); _timeHandler.setMaxContentLength(MAX_TIME_SIZE); server->addHandler(&_timeHandler); - WiFi.onEvent(std::bind(&NTPSettingsService::WiFiEvent, this, std::placeholders::_1)); + WiFi.onEvent(std::bind(&NTPSettingsService::WiFiEvent, this, _1)); addUpdateHandler([&](const String & originId) { configureNTP(); }, false); } @@ -24,6 +26,7 @@ void NTPSettingsService::begin() { void NTPSettingsService::WiFiEvent(WiFiEvent_t event) { switch (event) { case SYSTEM_EVENT_STA_DISCONNECTED: + case SYSTEM_EVENT_ETH_DISCONNECTED: emsesp::EMSESP::logger().info(F("WiFi connection dropped, stopping NTP")); connected_ = false; configureNTP(); diff --git a/lib/framework/NTPStatus.cpp b/lib/framework/NTPStatus.cpp index 19c8449df..e3f68b1b1 100644 --- a/lib/framework/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -1,7 +1,9 @@ #include +using namespace std::placeholders; // for `_1` etc + NTPStatus::NTPStatus(AsyncWebServer * server, SecurityManager * securityManager) { - server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); + server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); } /* diff --git a/lib/framework/NTPStatus.h b/lib/framework/NTPStatus.h index ad599b76b..757d5a239 100644 --- a/lib/framework/NTPStatus.h +++ b/lib/framework/NTPStatus.h @@ -2,15 +2,10 @@ #define NTPStatus_h #include -#ifdef ESP32 + #include #include #include -#elif defined(ESP8266) -#include -#include -#include -#endif #include #include diff --git a/lib/framework/NetworkSettingsService.cpp b/lib/framework/NetworkSettingsService.cpp index 9deb1d6d8..587bf3906 100644 --- a/lib/framework/NetworkSettingsService.cpp +++ b/lib/framework/NetworkSettingsService.cpp @@ -1,5 +1,7 @@ #include +using namespace std::placeholders; // for `_1` etc + NetworkSettingsService::NetworkSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) : _httpEndpoint(NetworkSettings::read, NetworkSettings::update, this, server, NETWORK_SETTINGS_SERVICE_PATH, securityManager) , _fsPersistence(NetworkSettings::read, NetworkSettings::update, this, fs, NETWORK_SETTINGS_FILE) @@ -17,7 +19,7 @@ NetworkSettingsService::NetworkSettingsService(AsyncWebServer * server, FS * fs, WiFi.mode(WIFI_MODE_MAX); WiFi.mode(WIFI_MODE_NULL); - WiFi.onEvent(std::bind(&NetworkSettingsService::WiFiEvent, this, std::placeholders::_1)); + WiFi.onEvent(std::bind(&NetworkSettingsService::WiFiEvent, this, _1)); addUpdateHandler([&](const String & originId) { reconfigureWiFiConnection(); }, false); } diff --git a/lib/framework/NetworkStatus.cpp b/lib/framework/NetworkStatus.cpp index 90c05df6e..cb0369d0e 100644 --- a/lib/framework/NetworkStatus.cpp +++ b/lib/framework/NetworkStatus.cpp @@ -2,8 +2,10 @@ #include "../../src/emsesp_stub.hpp" // proddy added +using namespace std::placeholders; // for `_1` etc + NetworkStatus::NetworkStatus(AsyncWebServer * server, SecurityManager * securityManager) { - server->on(NETWORK_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&NetworkStatus::networkStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); + server->on(NETWORK_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&NetworkStatus::networkStatus, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); } void NetworkStatus::networkStatus(AsyncWebServerRequest * request) { diff --git a/lib/framework/OTASettingsService.cpp b/lib/framework/OTASettingsService.cpp index ec10038eb..f0662507c 100644 --- a/lib/framework/OTASettingsService.cpp +++ b/lib/framework/OTASettingsService.cpp @@ -2,11 +2,13 @@ #include "../../src/emsesp_stub.hpp" // proddy added +using namespace std::placeholders; // for `_1` etc + OTASettingsService::OTASettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) : _httpEndpoint(OTASettings::read, OTASettings::update, this, server, OTA_SETTINGS_SERVICE_PATH, securityManager) , _fsPersistence(OTASettings::read, OTASettings::update, this, fs, OTA_SETTINGS_FILE) , _arduinoOTA(nullptr) { - WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); + WiFi.onEvent(std::bind(&OTASettingsService::WiFiEvent, this, _1, _2)); addUpdateHandler([&](const String & originId) { configureArduinoOTA(); }, false); } @@ -23,12 +25,11 @@ void OTASettingsService::loop() { void OTASettingsService::configureArduinoOTA() { if (_arduinoOTA) { -#ifdef ESP32 _arduinoOTA->end(); -#endif delete _arduinoOTA; _arduinoOTA = nullptr; } + if (_state.enabled) { _arduinoOTA = new ArduinoOTAClass; _arduinoOTA->setPort(_state.port); @@ -62,6 +63,13 @@ void OTASettingsService::configureArduinoOTA() { } } -void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { - configureArduinoOTA(); +void OTASettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { + switch (event) { + case SYSTEM_EVENT_STA_GOT_IP: + case SYSTEM_EVENT_ETH_GOT_IP: + configureArduinoOTA(); + break; + default: + break; + } } diff --git a/lib/framework/OTASettingsService.h b/lib/framework/OTASettingsService.h index f18b4dea0..d7fa86c10 100644 --- a/lib/framework/OTASettingsService.h +++ b/lib/framework/OTASettingsService.h @@ -57,7 +57,7 @@ class OTASettingsService : public StatefulService { ArduinoOTAClass * _arduinoOTA; void configureArduinoOTA(); - void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); + void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info); }; #endif // end OTASettingsService_h diff --git a/lib/framework/RestartService.cpp b/lib/framework/RestartService.cpp index 67669040f..d5c37e3c4 100644 --- a/lib/framework/RestartService.cpp +++ b/lib/framework/RestartService.cpp @@ -1,9 +1,9 @@ #include +using namespace std::placeholders; // for `_1` etc + RestartService::RestartService(AsyncWebServer * server, SecurityManager * securityManager) { - server->on(RESTART_SERVICE_PATH, - HTTP_POST, - securityManager->wrapRequest(std::bind(&RestartService::restart, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)); + server->on(RESTART_SERVICE_PATH, HTTP_POST, securityManager->wrapRequest(std::bind(&RestartService::restart, this, _1), AuthenticationPredicates::IS_ADMIN)); } void RestartService::restart(AsyncWebServerRequest * request) { diff --git a/lib/framework/RestartService.h b/lib/framework/RestartService.h index beae4a0dc..e6473c0fe 100644 --- a/lib/framework/RestartService.h +++ b/lib/framework/RestartService.h @@ -1,13 +1,8 @@ #ifndef RestartService_h #define RestartService_h -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif #include #include diff --git a/lib/framework/StatefulService.h b/lib/framework/StatefulService.h index ba5a3c5e6..541488c62 100644 --- a/lib/framework/StatefulService.h +++ b/lib/framework/StatefulService.h @@ -6,10 +6,8 @@ #include #include -#ifdef ESP32 #include #include -#endif #ifndef DEFAULT_BUFFER_SIZE #define DEFAULT_BUFFER_SIZE 1024 @@ -45,16 +43,10 @@ template class StatefulService { public: template -#ifdef ESP32 StatefulService(Args &&... args) : _state(std::forward(args)...) , _accessMutex(xSemaphoreCreateRecursiveMutex()) { } -#else - StatefulService(Args &&... args) - : _state(std::forward(args)...) { - } -#endif update_handler_id_t addUpdateHandler(StateUpdateCallback cb, bool allowRemove = true) { if (!cb) { @@ -131,21 +123,15 @@ class StatefulService { T _state; inline void beginTransaction() { -#ifdef ESP32 xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); -#endif } inline void endTransaction() { -#ifdef ESP32 xSemaphoreGiveRecursive(_accessMutex); -#endif } private: -#ifdef ESP32 - SemaphoreHandle_t _accessMutex; -#endif + SemaphoreHandle_t _accessMutex; std::list _updateHandlers; }; diff --git a/lib/framework/UploadFirmwareService.cpp b/lib/framework/UploadFirmwareService.cpp index d7b547a3b..5f1a19966 100644 --- a/lib/framework/UploadFirmwareService.cpp +++ b/lib/framework/UploadFirmwareService.cpp @@ -1,21 +1,10 @@ #include +using namespace std::placeholders; // for `_1` etc + UploadFirmwareService::UploadFirmwareService(AsyncWebServer * server, SecurityManager * securityManager) : _securityManager(securityManager) { - server->on(UPLOAD_FIRMWARE_PATH, - HTTP_POST, - std::bind(&UploadFirmwareService::uploadComplete, this, std::placeholders::_1), - std::bind(&UploadFirmwareService::handleUpload, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4, - std::placeholders::_5, - std::placeholders::_6)); -#ifdef ESP8266 - Update.runAsync(true); -#endif + server->on(UPLOAD_FIRMWARE_PATH, HTTP_POST, std::bind(&UploadFirmwareService::uploadComplete, this, _1), std::bind(&UploadFirmwareService::handleUpload, this, _1, _2, _3, _4, _5, _6)); } void UploadFirmwareService::handleUpload(AsyncWebServerRequest * request, const String & filename, size_t index, uint8_t * data, size_t len, bool final) { @@ -72,9 +61,5 @@ void UploadFirmwareService::handleError(AsyncWebServerRequest * request, int cod } void UploadFirmwareService::handleEarlyDisconnect() { -#ifdef ESP32 Update.abort(); -#elif defined(ESP8266) - Update.end(); -#endif } diff --git a/lib/framework/UploadFirmwareService.h b/lib/framework/UploadFirmwareService.h index f24b7aa38..1753f37d3 100644 --- a/lib/framework/UploadFirmwareService.h +++ b/lib/framework/UploadFirmwareService.h @@ -3,14 +3,9 @@ #include -#ifdef ESP32 #include #include #include -#elif defined(ESP8266) -#include -#include -#endif #include #include diff --git a/lib/framework/WebSocketTxRx.h b/lib/framework/WebSocketTxRx.h index 07aecc306..d4b5a46c7 100644 --- a/lib/framework/WebSocketTxRx.h +++ b/lib/framework/WebSocketTxRx.h @@ -10,6 +10,8 @@ #define WEB_SOCKET_ORIGIN "websocket" #define WEB_SOCKET_ORIGIN_CLIENT_ID_PREFIX "websocket:" +using namespace std::placeholders; // for `_1` etc + template class WebSocketConnector { protected: @@ -18,27 +20,15 @@ class WebSocketConnector { AsyncWebSocket _webSocket; size_t _bufferSize; - WebSocketConnector(StatefulService * statefulService, - AsyncWebServer * server, - const char * webSocketPath, - SecurityManager * securityManager, - AuthenticationPredicate authenticationPredicate, - size_t bufferSize) + WebSocketConnector(StatefulService * statefulService, AsyncWebServer * server, const char * webSocketPath, SecurityManager * securityManager, AuthenticationPredicate authenticationPredicate, size_t bufferSize) : _statefulService(statefulService) , _server(server) , _webSocket(webSocketPath) , _bufferSize(bufferSize) { _webSocket.setFilter(securityManager->filterRequest(authenticationPredicate)); - _webSocket.onEvent(std::bind(&WebSocketConnector::onWSEvent, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4, - std::placeholders::_5, - std::placeholders::_6)); + _webSocket.onEvent(std::bind(&WebSocketConnector::onWSEvent, this, _1, _2, _3, _4, _5, _6)); _server->addHandler(&_webSocket); - _server->on(webSocketPath, HTTP_GET, std::bind(&WebSocketConnector::forbidden, this, std::placeholders::_1)); + _server->on(webSocketPath, HTTP_GET, std::bind(&WebSocketConnector::forbidden, this, _1)); } WebSocketConnector(StatefulService * statefulService, AsyncWebServer * server, const char * webSocketPath, size_t bufferSize) @@ -46,14 +36,7 @@ class WebSocketConnector { , _server(server) , _webSocket(webSocketPath) , _bufferSize(bufferSize) { - _webSocket.onEvent(std::bind(&WebSocketConnector::onWSEvent, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4, - std::placeholders::_5, - std::placeholders::_6)); + _webSocket.onEvent(std::bind(&WebSocketConnector::onWSEvent, this, _1, _2, _3, _4, _5, _6)); _server->addHandler(&_webSocket); } @@ -84,11 +67,7 @@ class WebSocketTx : virtual public WebSocketConnector { WebSocketConnector::_statefulService->addUpdateHandler([&](const String & originId) { transmitData(nullptr, originId); }, false); } - WebSocketTx(JsonStateReader stateReader, - StatefulService * statefulService, - AsyncWebServer * server, - const char * webSocketPath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + WebSocketTx(JsonStateReader stateReader, StatefulService * statefulService, AsyncWebServer * server, const char * webSocketPath, size_t bufferSize = DEFAULT_BUFFER_SIZE) : WebSocketConnector(statefulService, server, webSocketPath, bufferSize) , _stateReader(stateReader) { WebSocketConnector::_statefulService->addUpdateHandler([&](const String & originId) { transmitData(nullptr, originId); }, false); @@ -161,11 +140,7 @@ class WebSocketRx : virtual public WebSocketConnector { , _stateUpdater(stateUpdater) { } - WebSocketRx(JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncWebServer * server, - const char * webSocketPath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + WebSocketRx(JsonStateUpdater stateUpdater, StatefulService * statefulService, AsyncWebServer * server, const char * webSocketPath, size_t bufferSize = DEFAULT_BUFFER_SIZE) : WebSocketConnector(statefulService, server, webSocketPath, bufferSize) , _stateUpdater(stateUpdater) { } @@ -207,12 +182,7 @@ class WebSocketTxRx : public WebSocketTx, public WebSocketRx { , WebSocketRx(stateUpdater, statefulService, server, webSocketPath, securityManager, authenticationPredicate, bufferSize) { } - WebSocketTxRx(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService * statefulService, - AsyncWebServer * server, - const char * webSocketPath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + WebSocketTxRx(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService * statefulService, AsyncWebServer * server, const char * webSocketPath, size_t bufferSize = DEFAULT_BUFFER_SIZE) : WebSocketConnector(statefulService, server, webSocketPath, bufferSize) , WebSocketTx(stateReader, statefulService, server, webSocketPath, bufferSize) , WebSocketRx(stateUpdater, statefulService, server, webSocketPath, bufferSize) { diff --git a/lib/framework/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp index 764db6fef..29507f84b 100644 --- a/lib/framework/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -1,8 +1,10 @@ #include +using namespace std::placeholders; // for `_1` etc + WiFiScanner::WiFiScanner(AsyncWebServer * server, SecurityManager * securityManager) { - server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)); - server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN)); + server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, _1), AuthenticationPredicates::IS_ADMIN)); + server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, _1), AuthenticationPredicates::IS_ADMIN)); }; void WiFiScanner::scanNetworks(AsyncWebServerRequest * request) { diff --git a/lib/framework/WiFiScanner.h b/lib/framework/WiFiScanner.h index 601e998de..99098e6ea 100644 --- a/lib/framework/WiFiScanner.h +++ b/lib/framework/WiFiScanner.h @@ -1,13 +1,8 @@ #ifndef WiFiScanner_h #define WiFiScanner_h -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif #include #include @@ -26,10 +21,6 @@ class WiFiScanner { private: void scanNetworks(AsyncWebServerRequest * request); void listNetworks(AsyncWebServerRequest * request); - -#ifdef ESP8266 - uint8_t convertEncryptionType(uint8_t encryptionType); -#endif }; #endif // end WiFiScanner_h diff --git a/src/WebAPIService.cpp b/src/WebAPIService.cpp index fdd1a6582..5be154979 100644 --- a/src/WebAPIService.cpp +++ b/src/WebAPIService.cpp @@ -18,10 +18,12 @@ #include "emsesp.h" +using namespace std::placeholders; // for `_1` etc + namespace emsesp { WebAPIService::WebAPIService(AsyncWebServer * server) { - server->on(EMSESP_API_SERVICE_PATH, HTTP_GET, std::bind(&WebAPIService::webAPIService, this, std::placeholders::_1)); + server->on(EMSESP_API_SERVICE_PATH, HTTP_GET, std::bind(&WebAPIService::webAPIService, this, _1)); } // e.g. http://ems-esp/api?device=boiler&cmd=wwtemp&data=20&id=1 @@ -68,8 +70,8 @@ void WebAPIService::webAPIService(AsyncWebServerRequest * request) { } DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE_DYN); - JsonObject json = doc.to(); - bool ok = false; + JsonObject json = doc.to(); + bool ok = false; // execute the command if (data.isEmpty()) { diff --git a/src/WebDevicesService.cpp b/src/WebDevicesService.cpp index 77bb2f115..25e4c9dcd 100644 --- a/src/WebDevicesService.cpp +++ b/src/WebDevicesService.cpp @@ -23,15 +23,10 @@ namespace emsesp { using namespace std::placeholders; // for `_1` etc WebDevicesService::WebDevicesService(AsyncWebServer * server, SecurityManager * securityManager) - : _device_dataHandler(DEVICE_DATA_SERVICE_PATH, - securityManager->wrapCallback(std::bind(&WebDevicesService::device_data, this, _1, _2), AuthenticationPredicates::IS_AUTHENTICATED)) { - server->on(EMSESP_DEVICES_SERVICE_PATH, - HTTP_GET, - securityManager->wrapRequest(std::bind(&WebDevicesService::all_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); + : _device_dataHandler(DEVICE_DATA_SERVICE_PATH, securityManager->wrapCallback(std::bind(&WebDevicesService::device_data, this, _1, _2), AuthenticationPredicates::IS_AUTHENTICATED)) { + server->on(EMSESP_DEVICES_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WebDevicesService::all_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); - server->on(SCAN_DEVICES_SERVICE_PATH, - HTTP_GET, - securityManager->wrapRequest(std::bind(&WebDevicesService::scan_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); + server->on(SCAN_DEVICES_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WebDevicesService::scan_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); _device_dataHandler.setMethod(HTTP_POST); _device_dataHandler.setMaxContentLength(256); diff --git a/src/WebStatusService.cpp b/src/WebStatusService.cpp index 1c20c2a2e..a212830bd 100644 --- a/src/WebStatusService.cpp +++ b/src/WebStatusService.cpp @@ -18,12 +18,14 @@ #include "emsesp.h" +using namespace std::placeholders; // for `_1` etc + namespace emsesp { WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * securityManager) { // rest endpoint for web page - server->on(EMSESP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); - WiFi.onEvent(std::bind(&WebStatusService::WiFiEvent, this, std::placeholders::_1, std::placeholders::_2)); + server->on(EMSESP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, _1), AuthenticationPredicates::IS_AUTHENTICATED)); + WiFi.onEvent(std::bind(&WebStatusService::WiFiEvent, this, _1, _2)); } // handles both WiFI and Ethernet