diff --git a/code/espurna/api.cpp b/code/espurna/api.cpp index 87bb204c..b8118270 100644 --- a/code/espurna/api.cpp +++ b/code/espurna/api.cpp @@ -7,17 +7,19 @@ Copyright (C) 2020-2021 by Maxim Prokhorov #include + +#include "web.h" #endif #include @@ -26,6 +28,11 @@ Copyright (C) 2020-2021 by Maxim Prokhorov #include +#include "system.h" +#include "rpc.h" + +#include "api_path.h" + // ----------------------------------------------------------------------------- PathParts::PathParts(espurna::StringView path) : @@ -256,7 +263,6 @@ size_t PathParts::wildcards(const PathParts& pattern) { } #if WEB_SUPPORT - String ApiRequest::wildcard(int index) const { return PathParts::wildcard(_pattern, _parts, index).toString(); } @@ -264,13 +270,11 @@ String ApiRequest::wildcard(int index) const { size_t ApiRequest::wildcards() const { return PathParts::wildcards(_pattern); } - #endif // ----------------------------------------------------------------------------- #if API_SUPPORT - namespace espurna { namespace api { namespace content_type { diff --git a/code/espurna/api.h b/code/espurna/api.h index 8eb98d39..9fbc5ebc 100644 --- a/code/espurna/api.h +++ b/code/espurna/api.h @@ -9,11 +9,9 @@ Copyright (C) 2020-2021 by Maxim Prokhorov -#include "web.h" +#include #include "api_path.h" #include "api_impl.h" @@ -42,13 +40,9 @@ void apiRegister(String path, bool apiError(espurna::api::Request&); bool apiOk(espurna::api::Request&); -bool apiAuthenticateHeader(AsyncWebServerRequest*, const String& key); -bool apiAuthenticateParam(AsyncWebServerRequest*, const String& key); -bool apiAuthenticate(AsyncWebServerRequest*); - -void apiCommonSetup(); +String apiKey(); bool apiEnabled(); bool apiRestFul(); -String apiKey(); +void apiCommonSetup(); void apiSetup(); diff --git a/code/espurna/api_async_server.h b/code/espurna/api_async_server.h new file mode 100644 index 00000000..35da7dd4 --- /dev/null +++ b/code/espurna/api_async_server.h @@ -0,0 +1,17 @@ +/* + +API MODULE + +Copyright (C) 2016-2019 by Xose Pérez +Copyright (C) 2020-2021 by Maxim Prokhorov + +*/ + +#pragma once + +#include + +bool apiAuthenticateHeader(AsyncWebServerRequest*, const String& key); +bool apiAuthenticateParam(AsyncWebServerRequest*, const String& key); +bool apiAuthenticate(AsyncWebServerRequest*); + diff --git a/code/espurna/api_common.cpp b/code/espurna/api_common.cpp index 57af66ab..5cf81c2d 100644 --- a/code/espurna/api_common.cpp +++ b/code/espurna/api_common.cpp @@ -9,52 +9,87 @@ Copyright (C) 2020 by Maxim Prokhorov #include "espurna.h" +#if WEB_SUPPORT #include "api.h" - -#include "ws.h" #include "web.h" +#include "ws.h" +#endif // ----------------------------------------------------------------------------- -#if WEB_SUPPORT +namespace espurna { +namespace api { namespace { +namespace build { -bool _apiWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant&) { - return espurna::settings::query::samePrefix(key, STRING_VIEW("api")); +constexpr bool enabled() { + return 1 == API_ENABLED; } -void _apiWebSocketOnVisible(JsonObject& root) { - wsPayloadModule(root, PSTR("api")); +constexpr bool restful() { + return 1 == API_RESTFUL; } -void _apiWebSocketOnConnected(JsonObject& root) { - root["apiEnabled"] = apiEnabled(); - root["apiKey"] = apiKey(); - root["apiRestFul"] = apiRestFul(); +STRING_VIEW_INLINE(Key, API_KEY); + +constexpr StringView key() { + return Key; } +} // namespace build + +namespace settings { +namespace keys { + +STRING_VIEW_INLINE(Enabled, "apiEnabled"); +STRING_VIEW_INLINE(Restful, "apiRestFul"); +STRING_VIEW_INLINE(Key, "apiKey"); + +} // namespace keys + +bool enabled() { + return getSetting(keys::Enabled, build::enabled()); } -// ----------------------------------------------------------------------------- -// Public API -// ----------------------------------------------------------------------------- +bool restful() { + return getSetting(keys::Restful, build::restful()); +} -bool apiEnabled() { - return getSetting("apiEnabled", 1 == API_ENABLED); +String key() { + return getSetting(keys::Key, build::key()); } -bool apiRestFul() { - return getSetting("apiRestFul", 1 == API_RESTFUL); +} // namespace settings + +namespace web { +#if WEB_SUPPORT + +bool onKeyCheck(espurna::StringView key, const JsonVariant&) { + return espurna::settings::query::samePrefix(key, STRING_VIEW("api")); } -String apiKey() { - return getSetting("apiKey", API_KEY); +void onVisible(JsonObject& root) { + wsPayloadModule(root, PSTR("api")); } -bool apiAuthenticateHeader(AsyncWebServerRequest* request, const String& key) { - if (apiEnabled() && key.length()) { - auto* header = request->getHeader(F("Api-Key")); +void onConnected(JsonObject& root) { + root["apiEnabled"] = apiEnabled(); + root["apiKey"] = apiKey(); + root["apiRestFul"] = apiRestFul(); +} + +void setup() { + wsRegister() + .onVisible(onVisible) + .onConnected(onConnected) + .onKeyCheck(onKeyCheck); +} + +bool authenticate_header(AsyncWebServerRequest* request, const String& key) { + STRING_VIEW_INLINE(Header, "Api-Key"); + if (settings::enabled() && key.length()) { + auto* header = request->getHeader(Header.toString()); if (header && (key == header->value())) { return true; } @@ -63,8 +98,10 @@ bool apiAuthenticateHeader(AsyncWebServerRequest* request, const String& key) { return false; } -bool apiAuthenticateParam(AsyncWebServerRequest* request, const String& key) { - auto* param = request->getParam("apikey", (request->method() == HTTP_PUT)); +bool authenticate_param(AsyncWebServerRequest* request, const String& key) { + STRING_VIEW_INLINE(Param, "apikey"); + + auto* param = request->getParam(Param.toString(), (request->method() == HTTP_PUT)); if (param && (key == param->value())) { return true; } @@ -72,28 +109,57 @@ bool apiAuthenticateParam(AsyncWebServerRequest* request, const String& key) { return false; } -bool apiAuthenticate(AsyncWebServerRequest* request) { +bool authenticate(AsyncWebServerRequest* request) { const auto key = apiKey(); if (!key.length()) { return false; } - if (apiAuthenticateHeader(request, key)) { + if (authenticate_header(request, key)) { return true; } - if (apiAuthenticateParam(request, key)) { + if (authenticate_param(request, key)) { return true; } return false; } -void apiCommonSetup() { - wsRegister() - .onVisible(_apiWebSocketOnVisible) - .onConnected(_apiWebSocketOnConnected) - .onKeyCheck(_apiWebSocketOnKeyCheck); +#endif +} // namespace web +} // namespace +} // namespace api +} // namespace espurna + +#if WEB_SUPPORT +bool apiAuthenticateHeader(AsyncWebServerRequest* request, const String& key) { + return espurna::api::web::authenticate_header(request, key); +} + +bool apiAuthenticateParam(AsyncWebServerRequest* request, const String& key) { + return espurna::api::web::authenticate_param(request, key); } -#endif // WEB_SUPPORT == 1 +bool apiAuthenticate(AsyncWebServerRequest* request) { + return espurna::api::web::authenticate(request); +} +#endif + +String apiKey() { + return espurna::api::settings::key(); +} + +bool apiEnabled() { + return espurna::api::settings::enabled(); +} + +bool apiRestFul() { + return espurna::api::settings::restful(); +} + +void apiCommonSetup() { +#if WEB_SUPPORT + espurna::api::web::setup(); +#endif +} diff --git a/code/espurna/api_impl.h b/code/espurna/api_impl.h index a4be587e..38bc430d 100644 --- a/code/espurna/api_impl.h +++ b/code/espurna/api_impl.h @@ -10,12 +10,11 @@ Copyright (C) 2020 by Maxim Prokhorov #include -#include - #include #include #include +#include "api_async_server.h" #include "api_path.h" namespace espurna { diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp index 525d9c51..7e59196c 100644 --- a/code/espurna/button.cpp +++ b/code/espurna/button.cpp @@ -20,7 +20,10 @@ Copyright (C) 2019-2021 by Maxim Prokhorov #include "rtcmem.h" #include "terminal.h" + +#if WEB_SUPPORT #include "ws.h" +#endif namespace espurna { namespace peripherals { diff --git a/code/espurna/led.cpp b/code/espurna/led.cpp index 3f07ae65..d95cc524 100644 --- a/code/espurna/led.cpp +++ b/code/espurna/led.cpp @@ -17,12 +17,6 @@ $ pio run -e ... -t espurna/led_pattern.re.ipp #if LED_SUPPORT -#include "led.h" -#include "mqtt.h" -#include "relay.h" -#include "rpc.h" -#include "ws.h" - #include #include #include @@ -30,6 +24,15 @@ $ pio run -e ... -t espurna/led_pattern.re.ipp #include #include +#include "led.h" +#include "mqtt.h" +#include "relay.h" +#include "rpc.h" + +#if WEB_SUPPORT +#include "ws.h" +#endif + namespace espurna { namespace led { namespace { diff --git a/code/espurna/main.cpp b/code/espurna/main.cpp index 4cb4312a..76066a83 100644 --- a/code/espurna/main.cpp +++ b/code/espurna/main.cpp @@ -20,12 +20,13 @@ along with this program. If not, see . */ -#include "espurna.h" -#include "main.h" - #include #include +#include "main.h" +#include "ota.h" +#include "rtcmem.h" + // ----------------------------------------------------------------------------- // GENERAL CALLBACKS // ----------------------------------------------------------------------------- diff --git a/code/espurna/main.h b/code/espurna/main.h index 3a184853..71f6dd0c 100644 --- a/code/espurna/main.h +++ b/code/espurna/main.h @@ -20,46 +20,163 @@ along with this program. If not, see . */ +#pragma once + +#include "espurna.h" + +#if ALEXA_SUPPORT #include "alexa.h" +#endif + +#if API_SUPPORT #include "api.h" +#endif + +#if BUTTON_SUPPORT #include "button.h" +#endif + +#if DEBUG_SUPPORT #include "crash.h" +#endif + +#if KINGART_CURTAIN_SUPPORT #include "curtain_kingart.h" +#endif + +#if DEBUG_SUPPORT #include "debug.h" +#endif + +#if DOMOTICZ_SUPPORT #include "domoticz.h" +#endif + +#if ENCODER_SUPPORT #include "encoder.h" +#endif + +#if HOMEASSISTANT_SUPPORT #include "homeassistant.h" +#endif + +#if GARLAND_SUPPORT #include "garland.h" +#endif + +#if I2C_SUPPORT #include "i2c.h" +#endif + +#if INFLUXDB_SUPPORT #include "influxdb.h" +#endif + +#if FAN_SUPPORT #include "fan.h" +#endif + +#if IR_SUPPORT #include "ir.h" +#endif + +#if LED_SUPPORT #include "led.h" +#endif + +#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE #include "light.h" +#endif + +#ifdef FOXEL_LIGHTFOX_DUAL #include "lightfox.h" +#endif + +#if LLMNR_SUPPORT #include "llmnr.h" +#endif + +#if MDNS_SERVER_SUPPORT #include "mdns.h" +#endif + +#if MQTT_SUPPORT #include "mqtt.h" +#endif + +#if NETBIOS_SUPPORT #include "netbios.h" +#endif + +#if NOFUSS_SUPPORT #include "nofuss.h" +#endif + +#if NTP_SUPPORT #include "ntp.h" -#include "ota.h" +#endif + +#if RELAY_SUPPORT #include "relay.h" +#endif + +#if RFB_SUPPORT #include "rfbridge.h" +#endif + +#if RFM69_SUPPORT #include "rfm69.h" -#include "rpc.h" +#endif + +#if RPN_RULES_SUPPORT #include "rpnrules.h" -#include "rtcmem.h" +#endif + +#if SCHEDULER_SUPPORT #include "scheduler.h" +#endif + +#if SENSOR_SUPPORT #include "sensor.h" +#endif + +#if SSDP_SUPPORT #include "ssdp.h" +#endif + +#if TELNET_SUPPORT #include "telnet.h" +#endif + +#if THERMOSTAT_SUPPORT #include "thermostat.h" +#endif + +#if THINGSPEAK_SUPPORT #include "thingspeak.h" +#endif + +#if TUYA_SUPPORT #include "tuya.h" +#endif + +#if UART_MQTT_SUPPORT #include "uartmqtt.h" +#endif + +#if WEB_SUPPORT #include "web.h" #include "ws.h" +#endif + +#if MCP23S08_SUPPORT #include "mcp23s08.h" +#endif + +#if PROMETHEUS_SUPPORT #include "prometheus.h" +#endif + +#if PWM_SUPPORT #include "pwm.h" +#endif diff --git a/code/espurna/ota.cpp b/code/espurna/ota.cpp index d2b7abd5..f7600699 100644 --- a/code/espurna/ota.cpp +++ b/code/espurna/ota.cpp @@ -9,7 +9,10 @@ OTA MODULE COMMON FUNCTIONS #include "system.h" #include "terminal.h" #include "utils.h" + +#if WEB_SUPPORT #include "ws.h" +#endif #include "libs/PrintString.h" diff --git a/code/espurna/relay.cpp b/code/espurna/relay.cpp index 00f3b304..64e453a1 100644 --- a/code/espurna/relay.cpp +++ b/code/espurna/relay.cpp @@ -11,7 +11,14 @@ Copyright (C) 2019-2021 by Maxim Prokhorov diff --git a/code/espurna/system.cpp b/code/espurna/system.cpp index aa4a9e52..e9075d71 100644 --- a/code/espurna/system.cpp +++ b/code/espurna/system.cpp @@ -8,8 +8,11 @@ Copyright (C) 2019 by Xose Pérez #include "espurna.h" -#include "rtcmem.h" +#if WEB_SUPPORT #include "ws.h" +#endif + +#include "rtcmem.h" #include "ntp.h" #include diff --git a/code/espurna/telnet.cpp b/code/espurna/telnet.cpp index cdbdab30..e9860fa2 100644 --- a/code/espurna/telnet.cpp +++ b/code/espurna/telnet.cpp @@ -28,11 +28,14 @@ Updated to use WiFiServer and support reverse connections by Niek van der Maas < #include +#if WEB_SUPPORT +#include "ws.h" +#endif + #include "mqtt.h" #include "telnet.h" #include "terminal.h" #include "wifi.h" -#include "ws.h" #include "libs/URL.h" diff --git a/code/espurna/terminal.cpp b/code/espurna/terminal.cpp index e625d3b8..839be487 100644 --- a/code/espurna/terminal.cpp +++ b/code/espurna/terminal.cpp @@ -11,7 +11,14 @@ Copyright (C) 2020-2022 by Maxim Prokhorov */ -#include "wifi.h" +#include "espurna.h" -#include "telnet.h" -#include "ws.h" +#include "wifi.h" #include #include -#if WIFI_AP_CAPTIVE_SUPPORT -#include -#endif - #include #include #include #include #include +#if WEB_SUPPORT +#include "ws.h" +#endif + +#if WIFI_AP_CAPTIVE_SUPPORT +#include +#endif + // ref. // https://github.com/d-a-v/esp82xx-nonos-linklayer/blob/master/README.md#how-it-works //