Fix broken build under ESP32

Fix broken imports & standardise import style
This commit is contained in:
rjwats 2019-12-26 15:12:27 +00:00 committed by GitHub
commit 14f50c1e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 140 additions and 143 deletions

View File

@ -1,12 +1,12 @@
#ifndef APStatus_h
#define APStatus_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>

View File

@ -21,7 +21,7 @@ String ArduinoJsonJWT::getSecret() {
String ArduinoJsonJWT::sign(String& payload) {
unsigned char hmacResult[32];
{
#if defined(ESP_PLATFORM)
#ifdef ESP32
mbedtls_md_context_t ctx;
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
mbedtls_md_init(&ctx);
@ -30,7 +30,7 @@ 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);
#else
#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;
@ -93,12 +93,12 @@ void ArduinoJsonJWT::parseJWT(String jwt, JsonDocument& jsonDocument) {
String ArduinoJsonJWT::encode(const char* cstr, int inputLen) {
// prepare encoder
base64_encodestate _state;
#if defined(ESP8266)
base64_init_encodestate_nonewlines(&_state);
size_t encodedLength = base64_encode_expected_len_nonewlines(inputLen) + 1;
#elif defined(ESP_PLATFORM)
#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));

View File

@ -5,9 +5,10 @@
#include <ArduinoJson.h>
#include <libb64/cdecode.h>
#include <libb64/cencode.h>
#if defined(ESP_PLATFORM)
#ifdef ESP32
#include <mbedtls/md.h>
#else
#elif defined(ESP8266)
#include <bearssl/bearssl_hmac.h>
#endif

View File

@ -3,19 +3,19 @@
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <FS.h>
#endif
#include <APSettingsService.h>
#include <APStatus.h>
#include <AuthenticationService.h>
#include <FS.h>
#include <NTPSettingsService.h>
#include <NTPStatus.h>
#include <OTASettingsService.h>

View File

@ -2,19 +2,18 @@
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) {
#if defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler =
WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM)
#ifdef ESP32
WiFi.onEvent(
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler =
WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
#endif
NTP.onNTPSyncEvent([this](NTPSyncEvent_t ntpEvent) {
_ntpEvent = ntpEvent;
_syncEventTriggered = true;
@ -68,18 +67,7 @@ void NTPSettingsService::onConfigUpdated() {
_reconfigureNTP = true;
}
#if defined(ESP8266)
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
Serial.printf("Got IP address, starting NTP Synchronization\n");
_reconfigureNTP = true;
}
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
Serial.printf("WiFi connection dropped, stopping NTP.\n");
_reconfigureNTP = false;
NTP.stop();
}
#elif defined(ESP_PLATFORM)
#ifdef ESP32
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.printf("Got IP address, starting NTP Synchronization\n");
_reconfigureNTP = true;
@ -90,6 +78,17 @@ void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventI
_reconfigureNTP = false;
NTP.stop();
}
#elif defined(ESP8266)
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
Serial.printf("Got IP address, starting NTP Synchronization\n");
_reconfigureNTP = true;
}
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
Serial.printf("WiFi connection dropped, stopping NTP.\n");
_reconfigureNTP = false;
NTP.stop();
}
#endif
void NTPSettingsService::configureNTP() {

View File

@ -37,15 +37,15 @@ class NTPSettingsService : public AdminSettingsService {
bool _syncEventTriggered = false;
NTPSyncEvent_t _ntpEvent;
#if defined(ESP8266)
#ifdef ESP32
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
#elif defined(ESP_PLATFORM)
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
void configureNTP();

View File

@ -1,12 +1,12 @@
#ifndef NTPStatus_h
#define NTPStatus_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>

View File

@ -2,12 +2,12 @@
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
AdminSettingsService(server, fs, securityManager, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {
#if defined(ESP8266)
_onStationModeGotIPHandler =
WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM)
#ifdef ESP32
WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeGotIPHandler =
WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
#endif
}
@ -43,7 +43,7 @@ void OTASettingsService::writeToJsonObject(JsonObject& root) {
void OTASettingsService::configureArduinoOTA() {
if (_arduinoOTA) {
#if defined(ESP_PLATFORM)
#ifdef ESP32
_arduinoOTA->end();
#endif
delete _arduinoOTA;
@ -75,13 +75,12 @@ void OTASettingsService::configureArduinoOTA() {
_arduinoOTA->begin();
}
}
#if defined(ESP8266)
void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
configureArduinoOTA();
}
#elif defined(ESP_PLATFORM)
#ifdef ESP32
void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
configureArduinoOTA();
}
#elif defined(ESP8266)
void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
configureArduinoOTA();
}
#endif

View File

@ -3,10 +3,10 @@
#include <AdminSettingsService.h>
#if defined(ESP8266)
#include <ESP8266mDNS.h>
#elif defined(ESP_PLATFORM)
#ifdef ESP32
#include <ESPmDNS.h>
#elif defined(ESP8266)
#include <ESP8266mDNS.h>
#endif
#include <ArduinoOTA.h>
@ -38,12 +38,11 @@ class OTASettingsService : public AdminSettingsService {
String _password;
void configureArduinoOTA();
#if defined(ESP8266)
#ifdef ESP32
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeGotIPHandler;
void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
#elif defined(ESP_PLATFORM)
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
};

View File

@ -9,10 +9,10 @@ RestartService::RestartService(AsyncWebServer* server, SecurityManager* security
void RestartService::restart(AsyncWebServerRequest* request) {
request->onDisconnect([]() {
#if defined(ESP8266)
ESP.reset();
#elif defined(ESP_PLATFORM)
#ifdef ESP32
ESP.restart();
#elif defined(ESP8266)
ESP.reset();
#endif
});
request->send(200);

View File

@ -1,12 +1,12 @@
#ifndef RestartService_h
#define RestartService_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ESPAsyncWebServer.h>

View File

@ -1,12 +1,12 @@
#ifndef SettingsService_h
#define SettingsService_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>

View File

@ -1,12 +1,12 @@
#ifndef Service_h
#define Service_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>

View File

@ -10,10 +10,10 @@ SystemStatus::SystemStatus(AsyncWebServer* server, SecurityManager* securityMana
void SystemStatus::systemStatus(AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_ESP_STATUS_SIZE);
JsonObject root = response->getRoot();
#if defined(ESP8266)
root["esp_platform"] = "esp8266";
#elif defined(ESP_PLATFORM)
#ifdef ESP32
root["esp_platform"] = "esp32";
#elif defined(ESP8266)
root["esp_platform"] = "esp8266";
#endif
root["cpu_freq_mhz"] = ESP.getCpuFreqMHz();
root["free_heap"] = ESP.getFreeHeap();

View File

@ -1,12 +1,12 @@
#ifndef SystemStatus_h
#define SystemStatus_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>

View File

@ -31,10 +31,10 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest* request) {
network["ssid"] = WiFi.SSID(i);
network["bssid"] = WiFi.BSSIDstr(i);
network["channel"] = WiFi.channel(i);
#if defined(ESP8266)
network["encryption_type"] = convertEncryptionType(WiFi.encryptionType(i));
#elif defined(ESP_PLATFORM)
#ifdef ESP32
network["encryption_type"] = (uint8_t)WiFi.encryptionType(i);
#elif defined(ESP8266)
network["encryption_type"] = convertEncryptionType(WiFi.encryptionType(i));
#endif
}
response->setLength();
@ -46,7 +46,7 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest* request) {
}
}
#if defined(ESP8266)
#ifdef ESP8266
/*
* Convert encryption type to standard used by ESP32 rather than the translated form which the esp8266 libaries expose.
*

View File

@ -1,12 +1,12 @@
#ifndef WiFiScanner_h
#define WiFiScanner_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>
@ -28,7 +28,7 @@ class WiFiScanner {
void scanNetworks(AsyncWebServerRequest* request);
void listNetworks(AsyncWebServerRequest* request);
#if defined(ESP8266)
#ifdef ESP8266
uint8_t convertEncryptionType(uint8_t encryptionType);
#endif
};

View File

@ -11,17 +11,16 @@ WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, Securit
// Disable WiFi config persistance and auto reconnect
WiFi.persistent(false);
WiFi.setAutoReconnect(false);
#if defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM)
#ifdef ESP32
// Init the wifi driver on ESP32
WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL);
WiFi.onEvent(
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
#elif defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
#endif
}
@ -120,12 +119,12 @@ void WiFiSettingsService::manageSTA() {
WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2);
} else {
// configure for DHCP
#if defined(ESP8266)
WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
WiFi.hostname(_hostname);
#elif defined(ESP_PLATFORM)
#ifdef ESP32
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(_hostname.c_str());
#elif defined(ESP8266)
WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
WiFi.hostname(_hostname);
#endif
}
// attempt to connect to the network
@ -133,12 +132,12 @@ void WiFiSettingsService::manageSTA() {
}
}
#if defined(ESP8266)
void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
WiFi.disconnect(true);
}
#elif defined(ESP_PLATFORM)
#ifdef ESP32
void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
WiFi.disconnect(true);
}
#elif defined(ESP8266)
void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
WiFi.disconnect(true);
}
#endif

View File

@ -38,11 +38,11 @@ class WiFiSettingsService : public AdminSettingsService {
IPAddress _dnsIP1;
IPAddress _dnsIP2;
#if defined(ESP8266)
#ifdef ESP32
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
#elif defined(ESP_PLATFORM)
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
void readIP(JsonObject& root, String key, IPAddress& _ip);

View File

@ -5,18 +5,34 @@ WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager)
HTTP_GET,
securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
#if defined(ESP8266)
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
#elif defined(ESP_PLATFORM)
#ifdef ESP32
WiFi.onEvent(onStationModeConnected, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
#endif
}
#if defined(ESP8266)
#ifdef ESP32
void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("WiFi Connected.");
}
void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("WiFi Disconnected. Reason code=");
Serial.println(info.disconnected.reason);
}
void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("WiFi Got IP. localIP=");
Serial.print(WiFi.localIP().toString());
Serial.print(", hostName=");
Serial.println(WiFi.getHostname());
}
#elif defined(ESP8266)
void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) {
Serial.print("WiFi Connected. SSID=");
Serial.println(event.ssid);
@ -33,22 +49,6 @@ void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
Serial.print(", hostName=");
Serial.println(WiFi.hostname());
}
#elif defined(ESP_PLATFORM)
void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("WiFi Connected.");
}
void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("WiFi Disconnected. Reason code=");
Serial.println(info.disconnected.reason);
}
void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("WiFi Got IP. localIP=");
Serial.print(WiFi.localIP().toString());
Serial.print(", hostName=");
Serial.println(WiFi.getHostname());
}
#endif
void WiFiStatus::wifiStatus(AsyncWebServerRequest* request) {

View File

@ -1,12 +1,12 @@
#ifndef WiFiStatus_h
#define WiFiStatus_h
#if defined(ESP8266)
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>
@ -23,7 +23,12 @@ class WiFiStatus {
WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager);
private:
#if defined(ESP8266)
#ifdef ESP32
// static functions for logging WiFi events to the UART
static void onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
// handler refrences for logging important WiFi events over serial
WiFiEventHandler _onStationModeConnectedHandler;
WiFiEventHandler _onStationModeDisconnectedHandler;
@ -32,11 +37,6 @@ class WiFiStatus {
static void onStationModeConnected(const WiFiEventStationModeConnected& event);
static void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
static void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
#elif defined(ESP_PLATFORM)
// static functions for logging WiFi events to the UART
static void onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
void wifiStatus(AsyncWebServerRequest* request);