Use ESP Logging Macros for network

This commit is contained in:
Thomas Basler
2025-04-18 17:58:07 +02:00
parent 24b0dae659
commit d0e8dbe61f

View File

@@ -4,7 +4,6 @@
*/ */
#include "NetworkSettings.h" #include "NetworkSettings.h"
#include "Configuration.h" #include "Configuration.h"
#include "MessageOutput.h"
#include "PinMapping.h" #include "PinMapping.h"
#include "Utils.h" #include "Utils.h"
#include "__compiled_constants.h" #include "__compiled_constants.h"
@@ -12,6 +11,9 @@
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <ETH.h> #include <ETH.h>
#undef TAG
static const char* TAG = "network";
NetworkSettingsClass::NetworkSettingsClass() NetworkSettingsClass::NetworkSettingsClass()
: _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&NetworkSettingsClass::loop, this)) : _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&NetworkSettingsClass::loop, this))
, _apIp(192, 168, 4, 1) , _apIp(192, 168, 4, 1)
@@ -36,9 +38,9 @@ void NetworkSettingsClass::init(Scheduler& scheduler)
const PinMapping_t& pin = PinMapping.get(); const PinMapping_t& pin = PinMapping.get();
_w5500 = W5500::setup(pin.w5500_mosi, pin.w5500_miso, pin.w5500_sclk, pin.w5500_cs, pin.w5500_int, pin.w5500_rst); _w5500 = W5500::setup(pin.w5500_mosi, pin.w5500_miso, pin.w5500_sclk, pin.w5500_cs, pin.w5500_int, pin.w5500_rst);
if (_w5500) if (_w5500)
MessageOutput.printf("W5500: Connection successful\n"); ESP_LOGI(TAG, "W5500: Connection successful");
else else
MessageOutput.printf("W5500: Connection error!!\n"); ESP_LOGE(TAG, "W5500: Connection error!!");
} }
#if CONFIG_ETH_USE_ESP32_EMAC #if CONFIG_ETH_USE_ESP32_EMAC
else if (PinMapping.isValidEthConfig()) { else if (PinMapping.isValidEthConfig()) {
@@ -61,46 +63,46 @@ void NetworkSettingsClass::NetworkEvent(const WiFiEvent_t event, WiFiEventInfo_t
{ {
switch (event) { switch (event) {
case ARDUINO_EVENT_ETH_START: case ARDUINO_EVENT_ETH_START:
MessageOutput.printf("ETH start\n"); ESP_LOGI(TAG, "ETH start");
if (_networkMode == network_mode::Ethernet) { if (_networkMode == network_mode::Ethernet) {
raiseEvent(network_event::NETWORK_START); raiseEvent(network_event::NETWORK_START);
} }
break; break;
case ARDUINO_EVENT_ETH_STOP: case ARDUINO_EVENT_ETH_STOP:
MessageOutput.printf("ETH stop\n"); ESP_LOGI(TAG, "ETH stop");
if (_networkMode == network_mode::Ethernet) { if (_networkMode == network_mode::Ethernet) {
raiseEvent(network_event::NETWORK_STOP); raiseEvent(network_event::NETWORK_STOP);
} }
break; break;
case ARDUINO_EVENT_ETH_CONNECTED: case ARDUINO_EVENT_ETH_CONNECTED:
MessageOutput.printf("ETH connected\n"); ESP_LOGI(TAG, "ETH connected");
_ethConnected = true; _ethConnected = true;
raiseEvent(network_event::NETWORK_CONNECTED); raiseEvent(network_event::NETWORK_CONNECTED);
break; break;
case ARDUINO_EVENT_ETH_GOT_IP: case ARDUINO_EVENT_ETH_GOT_IP:
MessageOutput.printf("ETH got IP: %s\n", ETH.localIP().toString().c_str()); ESP_LOGI(TAG, "ETH got IP: %s", ETH.localIP().toString().c_str());
if (_networkMode == network_mode::Ethernet) { if (_networkMode == network_mode::Ethernet) {
raiseEvent(network_event::NETWORK_GOT_IP); raiseEvent(network_event::NETWORK_GOT_IP);
} }
break; break;
case ARDUINO_EVENT_ETH_DISCONNECTED: case ARDUINO_EVENT_ETH_DISCONNECTED:
MessageOutput.printf("ETH disconnected\n"); ESP_LOGI(TAG, "ETH disconnected");
_ethConnected = false; _ethConnected = false;
if (_networkMode == network_mode::Ethernet) { if (_networkMode == network_mode::Ethernet) {
raiseEvent(network_event::NETWORK_DISCONNECTED); raiseEvent(network_event::NETWORK_DISCONNECTED);
} }
break; break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: case ARDUINO_EVENT_WIFI_STA_CONNECTED:
MessageOutput.printf("WiFi connected\n"); ESP_LOGI(TAG, "WiFi connected");
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
raiseEvent(network_event::NETWORK_CONNECTED); raiseEvent(network_event::NETWORK_CONNECTED);
} }
break; break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
// Reason codes can be found here: https://github.com/espressif/esp-idf/blob/5454d37d496a8c58542eb450467471404c606501/components/esp_wifi/include/esp_wifi_types_generic.h#L79-L141 // Reason codes can be found here: https://github.com/espressif/esp-idf/blob/5454d37d496a8c58542eb450467471404c606501/components/esp_wifi/include/esp_wifi_types_generic.h#L79-L141
MessageOutput.printf("WiFi disconnected: %" PRIu8 "\n", info.wifi_sta_disconnected.reason); ESP_LOGW(TAG, "WiFi disconnected: %" PRIu8 "", info.wifi_sta_disconnected.reason);
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
MessageOutput.printf("Try reconnecting\n"); ESP_LOGI(TAG, "Try reconnecting");
_lastReconnectAttempt = millis(); _lastReconnectAttempt = millis();
WiFi.disconnect(true, false); WiFi.disconnect(true, false);
WiFi.begin(); WiFi.begin();
@@ -108,7 +110,7 @@ void NetworkSettingsClass::NetworkEvent(const WiFiEvent_t event, WiFiEventInfo_t
} }
break; break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP: case ARDUINO_EVENT_WIFI_STA_GOT_IP:
MessageOutput.printf("WiFi got ip: %s\n", WiFi.localIP().toString().c_str()); ESP_LOGI(TAG, "WiFi got ip: %s", WiFi.localIP().toString().c_str());
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
raiseEvent(network_event::NETWORK_GOT_IP); raiseEvent(network_event::NETWORK_GOT_IP);
} }
@@ -154,14 +156,14 @@ void NetworkSettingsClass::handleMDNS()
MDNS.end(); MDNS.end();
if (!mdnsEnabled) { if (!mdnsEnabled) {
MessageOutput.printf("MDNS disabled\n"); ESP_LOGI(TAG, "MDNS disabled");
return; return;
} }
MessageOutput.printf("Starting MDNS responder...\n"); ESP_LOGI(TAG, "Starting MDNS responder...");
if (!MDNS.begin(getHostname())) { if (!MDNS.begin(getHostname())) {
MessageOutput.printf("Error setting up MDNS responder!\n"); ESP_LOGE(TAG, "Error setting up MDNS responder!");
return; return;
} }
@@ -169,7 +171,7 @@ void NetworkSettingsClass::handleMDNS()
MDNS.addService("opendtu", "tcp", 80); MDNS.addService("opendtu", "tcp", 80);
MDNS.addServiceTxt("opendtu", "tcp", "git_hash", __COMPILED_GIT_HASH__); MDNS.addServiceTxt("opendtu", "tcp", "git_hash", __COMPILED_GIT_HASH__);
MessageOutput.printf("MDNS started\n"); ESP_LOGI(TAG, "MDNS started");
} }
void NetworkSettingsClass::setupMode() void NetworkSettingsClass::setupMode()
@@ -210,7 +212,7 @@ void NetworkSettingsClass::enableAdminMode()
void NetworkSettingsClass::disableAdminMode() void NetworkSettingsClass::disableAdminMode()
{ {
_adminEnabled = false; _adminEnabled = false;
MessageOutput.printf("Admin mode disabled\n"); ESP_LOGI(TAG, "Admin mode disabled");
setupMode(); setupMode();
} }
@@ -230,7 +232,7 @@ void NetworkSettingsClass::loop()
if (_ethConnected) { if (_ethConnected) {
if (_networkMode != network_mode::Ethernet) { if (_networkMode != network_mode::Ethernet) {
// Do stuff when switching to Ethernet mode // Do stuff when switching to Ethernet mode
MessageOutput.printf("Switch to Ethernet mode\n"); ESP_LOGI(TAG, "Switch to Ethernet mode");
_networkMode = network_mode::Ethernet; _networkMode = network_mode::Ethernet;
WiFi.mode(WIFI_MODE_NULL); WiFi.mode(WIFI_MODE_NULL);
setStaticIp(); setStaticIp();
@@ -238,7 +240,7 @@ void NetworkSettingsClass::loop()
} }
} else if (_networkMode != network_mode::WiFi) { } else if (_networkMode != network_mode::WiFi) {
// Do stuff when switching to Ethernet mode // Do stuff when switching to Ethernet mode
MessageOutput.printf("Switch to WiFi mode\n"); ESP_LOGI(TAG, "Switch to WiFi mode");
_networkMode = network_mode::WiFi; _networkMode = network_mode::WiFi;
enableAdminMode(); enableAdminMode();
applyConfig(); applyConfig();
@@ -248,11 +250,11 @@ void NetworkSettingsClass::loop()
if (_adminEnabled && _adminTimeoutCounterMax > 0) { if (_adminEnabled && _adminTimeoutCounterMax > 0) {
_adminTimeoutCounter++; _adminTimeoutCounter++;
if (_adminTimeoutCounter % 10 == 0) { if (_adminTimeoutCounter % 10 == 0) {
MessageOutput.printf("Admin AP remaining seconds: %" PRIu32 " / %" PRIu32 "\n", _adminTimeoutCounter, _adminTimeoutCounterMax); ESP_LOGI(TAG, "Admin AP remaining seconds: %" PRIu32 " / %" PRIu32 "", _adminTimeoutCounter, _adminTimeoutCounterMax);
} }
} }
if (_performConnection && !isConnected() && wifiConfigured() && millis() - _lastReconnectAttempt > 60000) { if (_performConnection && !isConnected() && wifiConfigured() && millis() - _lastReconnectAttempt > 60000) {
MessageOutput.printf("Wifi reconnect watchdog triggered... Resetting Wifi hardware\n"); ESP_LOGW(TAG, "Wifi reconnect watchdog triggered... Resetting Wifi hardware");
WiFi.disconnect(true, false); WiFi.disconnect(true, false);
WiFi.mode(WIFI_MODE_NULL); WiFi.mode(WIFI_MODE_NULL);
if (_adminEnabled) { if (_adminEnabled) {
@@ -285,13 +287,13 @@ void NetworkSettingsClass::loop()
_connectRedoTimer = 0; _connectRedoTimer = 0;
} else { } else {
if (_connectTimeoutTimer > WIFI_RECONNECT_TIMEOUT && _performConnection) { if (_connectTimeoutTimer > WIFI_RECONNECT_TIMEOUT && _performConnection) {
MessageOutput.printf("Disabling search for AP...\n"); ESP_LOGI(TAG, "Disabling search for AP...");
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
_connectRedoTimer = 0; _connectRedoTimer = 0;
_performConnection = false; _performConnection = false;
} }
if (_connectRedoTimer > WIFI_RECONNECT_REDO_TIMEOUT && !_performConnection) { if (_connectRedoTimer > WIFI_RECONNECT_REDO_TIMEOUT && !_performConnection) {
MessageOutput.printf("Enable search for AP...\n"); ESP_LOGI(TAG, "Enable search for AP...");
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
applyConfig(); applyConfig();
_connectTimeoutTimer = 0; _connectTimeoutTimer = 0;
@@ -318,7 +320,7 @@ void NetworkSettingsClass::applyConfig()
const bool newCredentials = strcmp(WiFi.SSID().c_str(), config.Ssid) || strcmp(WiFi.psk().c_str(), config.Password); const bool newCredentials = strcmp(WiFi.SSID().c_str(), config.Ssid) || strcmp(WiFi.psk().c_str(), config.Password);
MessageOutput.printf("Start configuring WiFi STA using %s credentials\n", ESP_LOGI(TAG, "Start configuring WiFi STA using %s credentials",
newCredentials ? "new" : "existing"); newCredentials ? "new" : "existing");
bool success = false; bool success = false;
@@ -330,7 +332,7 @@ void NetworkSettingsClass::applyConfig()
success = WiFi.begin() != WL_CONNECT_FAILED; success = WiFi.begin() != WL_CONNECT_FAILED;
} }
MessageOutput.printf("Configuring WiFi %s\n", success ? "done" : "failed"); ESP_LOG_LEVEL_LOCAL((success ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "Configuring WiFi %s", success ? "done" : "failed");
setStaticIp(); setStaticIp();
} }
@@ -344,7 +346,7 @@ void NetworkSettingsClass::setHostname()
const String hostname = getHostname(); const String hostname = getHostname();
bool success = false; bool success = false;
MessageOutput.printf("Start setting hostname...\n"); ESP_LOGI(TAG, "Start setting hostname...");
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
success = WiFi.hostname(hostname); success = WiFi.hostname(hostname);
@@ -356,7 +358,7 @@ void NetworkSettingsClass::setHostname()
success = ETH.setHostname(hostname.c_str()); success = ETH.setHostname(hostname.c_str());
} }
MessageOutput.printf("Setting hostname %s\n", success ? "done" : "failed"); ESP_LOG_LEVEL_LOCAL((success ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "Setting hostname %s", success ? "done" : "failed");
} }
void NetworkSettingsClass::setStaticIp() void NetworkSettingsClass::setStaticIp()
@@ -369,7 +371,7 @@ void NetworkSettingsClass::setStaticIp()
const char* mode = (_networkMode == network_mode::WiFi) ? "WiFi" : "Ethernet"; const char* mode = (_networkMode == network_mode::WiFi) ? "WiFi" : "Ethernet";
const char* ipType = config.Dhcp ? "DHCP" : "static"; const char* ipType = config.Dhcp ? "DHCP" : "static";
MessageOutput.printf("Start configuring %s %s IP...\n", mode, ipType); ESP_LOGI(TAG, "Start configuring %s %s IP...", mode, ipType);
bool success = false; bool success = false;
if (_networkMode == network_mode::WiFi) { if (_networkMode == network_mode::WiFi) {
@@ -396,7 +398,7 @@ void NetworkSettingsClass::setStaticIp()
} }
} }
MessageOutput.printf("Configure IP %s\n", success ? "done" : "failed"); ESP_LOG_LEVEL_LOCAL((success ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "Configure IP %s", success ? "done" : "failed");
} }
IPAddress NetworkSettingsClass::localIP() const IPAddress NetworkSettingsClass::localIP() const