Browse Source

web: revert to per-file include checks

minimal & single-source builds should coexist yet again
network/test
Maxim Prokhorov 1 year ago
parent
commit
b64e7f91c1
16 changed files with 307 additions and 78 deletions
  1. +12
    -8
      code/espurna/api.cpp
  2. +3
    -9
      code/espurna/api.h
  3. +17
    -0
      code/espurna/api_async_server.h
  4. +100
    -34
      code/espurna/api_common.cpp
  5. +1
    -2
      code/espurna/api_impl.h
  6. +3
    -2
      code/espurna/button.cpp
  7. +3
    -0
      code/espurna/gpio.cpp
  8. +9
    -6
      code/espurna/led.cpp
  9. +4
    -3
      code/espurna/main.cpp
  10. +120
    -3
      code/espurna/main.h
  11. +3
    -0
      code/espurna/ota.cpp
  12. +7
    -1
      code/espurna/relay.cpp
  13. +4
    -1
      code/espurna/system.cpp
  14. +4
    -1
      code/espurna/telnet.cpp
  15. +7
    -1
      code/espurna/terminal.cpp
  16. +10
    -7
      code/espurna/wifi.cpp

+ 12
- 8
code/espurna/api.cpp View File

@ -7,17 +7,19 @@ Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
*/
#include "api.h"
// -----------------------------------------------------------------------------
#include "system.h"
#include "rpc.h"
#include "espurna.h"
#if API_SUPPORT
#include "api.h"
#endif
#if WEB_SUPPORT
#include "web.h"
#include <ESPAsyncTCP.h>
#include <ArduinoJson.h>
#include "web.h"
#endif
#include <algorithm>
@ -26,6 +28,11 @@ Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include <forward_list>
#include <vector>
#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 {


+ 3
- 9
code/espurna/api.h View File

@ -9,11 +9,9 @@ Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#pragma once
#include "espurna.h"
#include <functional>
#include "web.h"
#include <ArduinoJson.h>
#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();

+ 17
- 0
code/espurna/api_async_server.h View File

@ -0,0 +1,17 @@
/*
API MODULE
Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
*/
#pragma once
#include <ESPAsyncWebServer.h>
bool apiAuthenticateHeader(AsyncWebServerRequest*, const String& key);
bool apiAuthenticateParam(AsyncWebServerRequest*, const String& key);
bool apiAuthenticate(AsyncWebServerRequest*);

+ 100
- 34
code/espurna/api_common.cpp View File

@ -9,52 +9,87 @@ Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
#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
}

+ 1
- 2
code/espurna/api_impl.h View File

@ -10,12 +10,11 @@ Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <algorithm>
#include <memory>
#include <vector>
#include "api_async_server.h"
#include "api_path.h"
namespace espurna {


+ 3
- 2
code/espurna/button.cpp View File

@ -20,7 +20,10 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include "mqtt.h"
#include "relay.h"
#include "system.h"
#if WEB_SUPPORT
#include "ws.h"
#endif
#include "mcp23s08_pin.h"
@ -997,7 +1000,6 @@ void _buttonRelayAction(size_t id, ButtonAction action) {
// -----------------------------------------------------------------------------
#if WEB_SUPPORT
namespace {
void _buttonWebSocketOnVisible(JsonObject& root) {
@ -1015,7 +1017,6 @@ bool _buttonWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant&) {
}
} // namespace
#endif // WEB_SUPPORT
//------------------------------------------------------------------------------


+ 3
- 0
code/espurna/gpio.cpp View File

@ -17,7 +17,10 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "rtcmem.h"
#include "terminal.h"
#if WEB_SUPPORT
#include "ws.h"
#endif
namespace espurna {
namespace peripherals {


+ 9
- 6
code/espurna/led.cpp View File

@ -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 <algorithm>
#include <cstring>
#include <ctime>
@ -30,6 +24,15 @@ $ pio run -e ... -t espurna/led_pattern.re.ipp
#include <forward_list>
#include <vector>
#include "led.h"
#include "mqtt.h"
#include "relay.h"
#include "rpc.h"
#if WEB_SUPPORT
#include "ws.h"
#endif
namespace espurna {
namespace led {
namespace {


+ 4
- 3
code/espurna/main.cpp View File

@ -20,12 +20,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "espurna.h"
#include "main.h"
#include <algorithm>
#include <utility>
#include "main.h"
#include "ota.h"
#include "rtcmem.h"
// -----------------------------------------------------------------------------
// GENERAL CALLBACKS
// -----------------------------------------------------------------------------


+ 120
- 3
code/espurna/main.h View File

@ -20,46 +20,163 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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

+ 3
- 0
code/espurna/ota.cpp View File

@ -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"


+ 7
- 1
code/espurna/relay.cpp View File

@ -11,7 +11,14 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#if RELAY_SUPPORT
#if API_SUPPORT
#include "api.h"
#endif
#if WEB_SUPPORT
#include "ws.h"
#endif
#include "mqtt.h"
#include "relay.h"
#include "tuya.h"
@ -22,7 +29,6 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include "storage_eeprom.h"
#include "terminal.h"
#include "utils.h"
#include "ws.h"
#include <ArduinoJson.h>


+ 4
- 1
code/espurna/system.cpp View File

@ -8,8 +8,11 @@ Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "espurna.h"
#include "rtcmem.h"
#if WEB_SUPPORT
#include "ws.h"
#endif
#include "rtcmem.h"
#include "ntp.h"
#include <cstdint>


+ 4
- 1
code/espurna/telnet.cpp View File

@ -28,11 +28,14 @@ Updated to use WiFiServer and support reverse connections by Niek van der Maas <
#include <ESP8266WiFi.h>
#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"


+ 7
- 1
code/espurna/terminal.cpp View File

@ -11,7 +11,14 @@ Copyright (C) 2020-2022 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#if TERMINAL_SUPPORT
#if API_SUPPORT
#include "api.h"
#endif
#if WEB_SUPPORT
#include "ws.h"
#endif
#include "crash.h"
#include "mqtt.h"
#include "settings.h"
@ -21,7 +28,6 @@ Copyright (C) 2020-2022 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include "terminal.h"
#include "utils.h"
#include "wifi.h"
#include "ws.h"
#include "libs/PrintString.h"


+ 10
- 7
code/espurna/wifi.cpp View File

@ -10,24 +10,27 @@ Copyright (C) 2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
*/
#include "wifi.h"
#include "espurna.h"
#include "telnet.h"
#include "ws.h"
#include "wifi.h"
#include <IPAddress.h>
#include <AddrList.h>
#if WIFI_AP_CAPTIVE_SUPPORT
#include <DNSServer.h>
#endif
#include <algorithm>
#include <array>
#include <list>
#include <queue>
#include <vector>
#if WEB_SUPPORT
#include "ws.h"
#endif
#if WIFI_AP_CAPTIVE_SUPPORT
#include <DNSServer.h>
#endif
// ref.
// https://github.com/d-a-v/esp82xx-nonos-linklayer/blob/master/README.md#how-it-works
//


Loading…
Cancel
Save