2022-07-15 18:05:58 +02:00
// SPDX-License-Identifier: GPL-2.0-or-later
/*
2024-01-20 11:24:57 +01:00
* Copyright ( C ) 2022 - 2024 Thomas Basler and others
2022-07-15 18:05:58 +02:00
*/
2022-07-13 21:30:53 +02:00
# include "NetworkSettings.h"
2022-04-10 17:37:54 +02:00
# include "Configuration.h"
2022-12-19 21:01:29 +01:00
# include "MessageOutput.h"
2023-01-16 22:26:35 +01:00
# include "PinMapping.h"
2022-10-20 19:35:17 +02:00
# include "Utils.h"
2024-09-26 19:22:30 +02:00
# include "__compiled_constants.h"
2022-04-10 17:37:54 +02:00
# include "defaults.h"
2023-10-30 20:53:45 +01:00
# include <ESPmDNS.h>
2022-07-13 21:30:53 +02:00
# include <ETH.h>
2022-04-10 17:37:54 +02:00
2022-07-13 21:30:53 +02:00
NetworkSettingsClass : : NetworkSettingsClass ( )
2024-01-20 11:24:57 +01:00
: _loopTask ( TASK_IMMEDIATE , TASK_FOREVER , std : : bind ( & NetworkSettingsClass : : loop , this ) )
, _apIp ( 192 , 168 , 4 , 1 )
2023-12-12 23:54:06 +01:00
, _apNetmask ( 255 , 255 , 255 , 0 )
2025-04-03 19:06:08 +02:00
, _dnsServer ( std : : make_unique < DNSServer > ( ) )
2022-04-10 17:37:54 +02:00
{
}
2023-12-12 13:26:07 +01:00
void NetworkSettingsClass : : init ( Scheduler & scheduler )
2022-04-10 17:37:54 +02:00
{
2022-09-13 21:35:51 +02:00
using std : : placeholders : : _1 ;
2024-09-02 20:16:03 +02:00
using std : : placeholders : : _2 ;
2022-07-13 21:30:53 +02:00
2023-07-20 23:44:09 +02:00
WiFi . setScanMethod ( WIFI_ALL_CHANNEL_SCAN ) ;
WiFi . setSortMethod ( WIFI_CONNECT_AP_BY_SIGNAL ) ;
2023-08-02 17:21:03 +02:00
WiFi . disconnect ( true , true ) ;
2024-09-02 20:16:03 +02:00
WiFi . onEvent ( std : : bind ( & NetworkSettingsClass : : NetworkEvent , this , _1 , _2 ) ) ;
2023-07-21 16:57:00 +02:00
if ( PinMapping . isValidW5500Config ( ) ) {
2025-02-08 00:34:23 +01:00
const PinMapping_t & pin = PinMapping . get ( ) ;
2024-09-27 00:31:15 +02:00
_w5500 = W5500 : : setup ( pin . w5500_mosi , pin . w5500_miso , pin . w5500_sclk , pin . w5500_cs , pin . w5500_int , pin . w5500_rst ) ;
if ( _w5500 )
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " W5500: Connection successful \r \n " ) ;
2024-09-27 00:31:15 +02:00
else
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " W5500: Connection error!! \r \n " ) ;
2024-10-04 01:15:24 +02:00
}
# if CONFIG_ETH_USE_ESP32_EMAC
else if ( PinMapping . isValidEthConfig ( ) ) {
2025-02-08 00:34:23 +01:00
const PinMapping_t & pin = PinMapping . get ( ) ;
2024-09-27 18:27:26 +02:00
# if ESP_ARDUINO_VERSION_MAJOR < 3
2023-07-21 16:57:00 +02:00
ETH . begin ( pin . eth_phy_addr , pin . eth_power , pin . eth_mdc , pin . eth_mdio , pin . eth_type , pin . eth_clk_mode ) ;
2024-09-27 18:27:26 +02:00
# else
ETH . begin ( pin . eth_type , pin . eth_phy_addr , pin . eth_mdc , pin . eth_mdio , pin . eth_power , pin . eth_clk_mode ) ;
# endif
2023-07-21 16:57:00 +02:00
}
2024-10-04 01:15:24 +02:00
# endif
2023-07-21 16:57:00 +02:00
2022-04-10 17:37:54 +02:00
setupMode ( ) ;
2023-11-20 21:51:51 +01:00
2023-12-12 13:26:07 +01:00
scheduler . addTask ( _loopTask ) ;
2023-11-20 21:51:51 +01:00
_loopTask . enable ( ) ;
2022-04-10 17:37:54 +02:00
}
2024-09-02 20:16:03 +02:00
void NetworkSettingsClass : : NetworkEvent ( const WiFiEvent_t event , WiFiEventInfo_t info )
2022-07-13 21:30:53 +02:00
{
switch ( event ) {
case ARDUINO_EVENT_ETH_START :
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " ETH start \r \n " ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : Ethernet ) {
raiseEvent ( network_event : : NETWORK_START ) ;
}
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_ETH_STOP :
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " ETH stop \r \n " ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : Ethernet ) {
raiseEvent ( network_event : : NETWORK_STOP ) ;
}
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_ETH_CONNECTED :
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " ETH connected \r \n " ) ;
2022-07-19 21:53:04 +02:00
_ethConnected = true ;
raiseEvent ( network_event : : NETWORK_CONNECTED ) ;
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_ETH_GOT_IP :
2023-01-04 18:48:33 +01:00
MessageOutput . printf ( " ETH got IP: %s \r \n " , ETH . localIP ( ) . toString ( ) . c_str ( ) ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : Ethernet ) {
raiseEvent ( network_event : : NETWORK_GOT_IP ) ;
}
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_ETH_DISCONNECTED :
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " ETH disconnected \r \n " ) ;
2022-07-19 21:53:04 +02:00
_ethConnected = false ;
if ( _networkMode = = network_mode : : Ethernet ) {
raiseEvent ( network_event : : NETWORK_DISCONNECTED ) ;
}
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_WIFI_STA_CONNECTED :
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " WiFi connected \r \n " ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : WiFi ) {
raiseEvent ( network_event : : NETWORK_CONNECTED ) ;
}
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED :
2024-09-02 20:16:03 +02:00
// 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
2024-10-12 21:16:42 +02:00
MessageOutput . printf ( " WiFi disconnected: % " PRIu8 " \r \n " , info . wifi_sta_disconnected . reason ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : WiFi ) {
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " Try reconnecting \r \n " ) ;
2024-08-05 18:20:55 +02:00
WiFi . disconnect ( true , false ) ;
WiFi . begin ( ) ;
2022-07-19 21:53:04 +02:00
raiseEvent ( network_event : : NETWORK_DISCONNECTED ) ;
}
2022-07-13 21:30:53 +02:00
break ;
case ARDUINO_EVENT_WIFI_STA_GOT_IP :
2023-01-04 18:48:33 +01:00
MessageOutput . printf ( " WiFi got ip: %s \r \n " , WiFi . localIP ( ) . toString ( ) . c_str ( ) ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : WiFi ) {
raiseEvent ( network_event : : NETWORK_GOT_IP ) ;
}
2022-07-13 21:30:53 +02:00
break ;
default :
2022-07-19 22:12:21 +02:00
break ;
2022-07-13 21:30:53 +02:00
}
}
2024-09-27 17:32:28 +02:00
bool NetworkSettingsClass : : onEvent ( DtuNetworkEventCb cbEvent , const network_event event )
2022-07-19 21:53:04 +02:00
{
if ( ! cbEvent ) {
return pdFALSE ;
}
2024-10-04 23:02:12 +02:00
DtuNetworkEventCbList_t newEventHandler ;
2022-07-19 21:53:04 +02:00
newEventHandler . cb = cbEvent ;
newEventHandler . event = event ;
_cbEventList . push_back ( newEventHandler ) ;
return true ;
}
2023-12-12 00:21:14 +01:00
void NetworkSettingsClass : : raiseEvent ( const network_event event )
2022-07-19 21:53:04 +02:00
{
2024-09-26 18:08:48 +02:00
for ( auto & entry : _cbEventList ) {
2022-07-19 21:53:04 +02:00
if ( entry . cb ) {
if ( entry . event = = event | | entry . event = = network_event : : NETWORK_EVENT_MAX ) {
entry . cb ( event ) ;
}
}
}
}
2023-10-30 20:53:45 +01:00
void NetworkSettingsClass : : handleMDNS ( )
{
2023-12-11 19:54:57 +01:00
const bool mdnsEnabled = Configuration . get ( ) . Mdns . Enabled ;
2023-10-30 20:53:45 +01:00
2025-04-12 12:45:41 +02:00
// Return if no state change
2023-12-12 23:54:06 +01:00
if ( _lastMdnsEnabled = = mdnsEnabled ) {
2023-10-30 20:53:45 +01:00
return ;
}
2023-12-12 23:54:06 +01:00
_lastMdnsEnabled = mdnsEnabled ;
2023-10-30 20:53:45 +01:00
MDNS . end ( ) ;
if ( ! mdnsEnabled ) {
2025-04-12 12:45:41 +02:00
MessageOutput . printf ( " MDNS disabled \r \n " ) ;
2023-10-30 20:53:45 +01:00
return ;
}
2025-04-12 12:45:41 +02:00
MessageOutput . printf ( " Starting MDNS responder... \r \n " ) ;
2023-10-30 20:53:45 +01:00
2025-04-12 12:45:41 +02:00
if ( ! MDNS . begin ( getHostname ( ) ) ) {
MessageOutput . printf ( " Error setting up MDNS responder! \r \n " ) ;
return ;
2023-10-30 20:53:45 +01:00
}
2025-04-12 12:45:41 +02:00
MDNS . addService ( " http " , " tcp " , 80 ) ;
MDNS . addService ( " opendtu " , " tcp " , 80 ) ;
MDNS . addServiceTxt ( " opendtu " , " tcp " , " git_hash " , __COMPILED_GIT_HASH__ ) ;
MessageOutput . printf ( " MDNS started \r \n " ) ;
2023-10-30 20:53:45 +01:00
}
2022-07-13 21:30:53 +02:00
void NetworkSettingsClass : : setupMode ( )
2022-04-10 17:37:54 +02:00
{
2023-12-12 23:54:06 +01:00
if ( _adminEnabled ) {
2022-04-10 17:37:54 +02:00
WiFi . mode ( WIFI_AP_STA ) ;
String ssidString = getApName ( ) ;
2023-12-12 23:54:06 +01:00
WiFi . softAPConfig ( _apIp , _apIp , _apNetmask ) ;
2023-12-11 18:44:02 +01:00
WiFi . softAP ( ssidString . c_str ( ) , Configuration . get ( ) . Security . Password ) ;
2023-12-12 23:54:06 +01:00
_dnsServer - > setErrorReplyCode ( DNSReplyCode : : NoError ) ;
_dnsServer - > start ( DNS_PORT , " * " , WiFi . softAPIP ( ) ) ;
_dnsServerStatus = true ;
2022-04-10 17:37:54 +02:00
} else {
2023-12-12 23:54:06 +01:00
_dnsServerStatus = false ;
_dnsServer - > stop ( ) ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : WiFi ) {
WiFi . mode ( WIFI_STA ) ;
} else {
WiFi . mode ( WIFI_MODE_NULL ) ;
}
2022-04-10 17:37:54 +02:00
}
}
2022-07-13 21:30:53 +02:00
void NetworkSettingsClass : : enableAdminMode ( )
2022-04-10 17:37:54 +02:00
{
2025-04-15 20:38:07 +02:00
// This prevents a immediate "Disabling search for AP" when
// the network connection persists for a long time and the
// credentials gets changed.
_connectTimeoutTimer = 0 ;
_connectRedoTimer = 0 ;
2023-12-12 23:54:06 +01:00
_adminTimeoutCounter = 0 ;
_adminTimeoutCounterMax = Configuration . get ( ) . WiFi . ApTimeout * 60 ;
2025-04-15 18:36:51 +02:00
_adminEnabled = true ;
2022-04-10 17:37:54 +02:00
setupMode ( ) ;
}
2025-04-15 20:57:13 +02:00
void NetworkSettingsClass : : disableAdminMode ( )
{
_adminEnabled = false ;
MessageOutput . printf ( " Admin mode disabled \r \n " ) ;
setupMode ( ) ;
}
2023-12-12 01:30:40 +01:00
String NetworkSettingsClass : : getApName ( ) const
2022-04-10 17:37:54 +02:00
{
2022-10-20 19:35:17 +02:00
return String ( ACCESS_POINT_NAME + String ( Utils : : getChipId ( ) ) ) ;
2022-04-10 17:37:54 +02:00
}
2022-07-13 21:30:53 +02:00
void NetworkSettingsClass : : loop ( )
2022-04-10 17:37:54 +02:00
{
2022-07-19 21:53:04 +02:00
if ( _ethConnected ) {
if ( _networkMode ! = network_mode : : Ethernet ) {
// Do stuff when switching to Ethernet mode
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " Switch to Ethernet mode \r \n " ) ;
2022-07-19 21:53:04 +02:00
_networkMode = network_mode : : Ethernet ;
WiFi . mode ( WIFI_MODE_NULL ) ;
setStaticIp ( ) ;
setHostname ( ) ;
}
2023-07-22 18:00:41 +02:00
} else if ( _networkMode ! = network_mode : : WiFi ) {
2022-07-19 21:53:04 +02:00
// Do stuff when switching to Ethernet mode
2025-04-12 10:25:28 +02:00
MessageOutput . printf ( " Switch to WiFi mode \r \n " ) ;
2022-07-19 21:53:04 +02:00
_networkMode = network_mode : : WiFi ;
enableAdminMode ( ) ;
applyConfig ( ) ;
}
2023-12-12 23:54:06 +01:00
if ( millis ( ) - _lastTimerCall > 1000 ) {
if ( _adminEnabled & & _adminTimeoutCounterMax > 0 ) {
_adminTimeoutCounter + + ;
if ( _adminTimeoutCounter % 10 = = 0 ) {
2025-04-09 18:33:47 +02:00
MessageOutput . printf ( " Admin AP remaining seconds: % " PRIu32 " / % " PRIu32 " \r \n " , _adminTimeoutCounter , _adminTimeoutCounterMax ) ;
2023-04-16 11:00:17 +02:00
}
}
2023-12-12 23:54:06 +01:00
_connectTimeoutTimer + + ;
_connectRedoTimer + + ;
_lastTimerCall = millis ( ) ;
2022-04-10 17:37:54 +02:00
}
2023-12-12 23:54:06 +01:00
if ( _adminEnabled ) {
2022-07-19 21:53:04 +02:00
// Don't disable the admin mode when network is not available
if ( ! isConnected ( ) ) {
2023-12-12 23:54:06 +01:00
_adminTimeoutCounter = 0 ;
2022-04-10 17:37:54 +02:00
}
2023-04-16 11:00:17 +02:00
// If WiFi is connected to AP for more than adminTimeoutCounterMax
2022-04-10 17:37:54 +02:00
// seconds, disable the internal Access Point
2023-12-12 23:54:06 +01:00
if ( _adminTimeoutCounter > _adminTimeoutCounterMax ) {
2025-04-15 20:57:13 +02:00
disableAdminMode ( ) ;
2022-04-10 17:37:54 +02:00
}
// It's nearly not possible to use the internal AP if the
// WiFi is searching for an AP. So disable searching afer
// WIFI_RECONNECT_TIMEOUT and repeat after WIFI_RECONNECT_REDO_TIMEOUT
2022-07-19 21:53:04 +02:00
if ( isConnected ( ) ) {
2023-12-12 23:54:06 +01:00
_connectTimeoutTimer = 0 ;
_connectRedoTimer = 0 ;
2022-04-10 17:37:54 +02:00
} else {
2023-12-12 23:54:06 +01:00
if ( _connectTimeoutTimer > WIFI_RECONNECT_TIMEOUT & & ! _forceDisconnection ) {
2025-04-12 13:21:46 +02:00
MessageOutput . printf ( " Disabling search for AP... \r \n " ) ;
2022-04-10 17:37:54 +02:00
WiFi . mode ( WIFI_AP ) ;
2023-12-12 23:54:06 +01:00
_connectRedoTimer = 0 ;
_forceDisconnection = true ;
2022-04-10 17:37:54 +02:00
}
2023-12-12 23:54:06 +01:00
if ( _connectRedoTimer > WIFI_RECONNECT_REDO_TIMEOUT & & _forceDisconnection ) {
2025-04-12 13:21:46 +02:00
MessageOutput . printf ( " Enable search for AP... \r \n " ) ;
2022-04-10 17:37:54 +02:00
WiFi . mode ( WIFI_AP_STA ) ;
applyConfig ( ) ;
2023-12-12 23:54:06 +01:00
_connectTimeoutTimer = 0 ;
_forceDisconnection = false ;
2022-04-10 17:37:54 +02:00
}
}
}
2023-12-12 23:54:06 +01:00
if ( _dnsServerStatus ) {
_dnsServer - > processNextRequest ( ) ;
2022-04-17 22:07:08 +02:00
}
2023-10-30 20:53:45 +01:00
handleMDNS ( ) ;
2022-04-10 17:37:54 +02:00
}
2022-07-13 21:30:53 +02:00
void NetworkSettingsClass : : applyConfig ( )
2022-04-10 17:37:54 +02:00
{
setHostname ( ) ;
2025-04-12 11:00:28 +02:00
const auto & config = Configuration . get ( ) . WiFi ;
// Check if SSID is empty
if ( ! strcmp ( config . Ssid , " " ) ) {
2022-04-10 17:37:54 +02:00
return ;
}
2025-04-12 11:00:28 +02:00
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 \r \n " ,
newCredentials ? " new " : " existing " ) ;
bool success = false ;
if ( newCredentials ) {
success = WiFi . begin (
config . Ssid ,
config . Password ) ! = WL_CONNECT_FAILED ;
2022-04-10 17:37:54 +02:00
} else {
2025-04-12 11:00:28 +02:00
success = WiFi . begin ( ) ! = WL_CONNECT_FAILED ;
2022-04-10 17:37:54 +02:00
}
2025-04-12 11:00:28 +02:00
MessageOutput . printf ( " Configuring WiFi %s \r \n " , success ? " done " : " failed " ) ;
2022-04-10 17:37:54 +02:00
setStaticIp ( ) ;
}
2022-07-13 21:30:53 +02:00
void NetworkSettingsClass : : setHostname ( )
2022-04-10 17:37:54 +02:00
{
2025-04-12 10:45:01 +02:00
if ( _networkMode = = network_mode : : Undefined ) {
return ;
}
const String hostname = getHostname ( ) ;
bool success = false ;
MessageOutput . printf ( " Start setting hostname... \r \n " ) ;
2022-09-06 18:44:26 +02:00
if ( _networkMode = = network_mode : : WiFi ) {
2025-04-12 10:45:01 +02:00
success = WiFi . hostname ( hostname ) ;
2022-09-06 18:44:26 +02:00
// Evil bad hack to get the hostname set up correctly
WiFi . mode ( WIFI_MODE_APSTA ) ;
WiFi . mode ( WIFI_MODE_STA ) ;
setupMode ( ) ;
2023-07-22 18:00:41 +02:00
} else if ( _networkMode = = network_mode : : Ethernet ) {
2025-04-12 10:45:01 +02:00
success = ETH . setHostname ( hostname . c_str ( ) ) ;
2022-04-10 17:37:54 +02:00
}
2025-04-12 10:45:01 +02:00
MessageOutput . printf ( " Setting hostname %s \r \n " , success ? " done " : " failed " ) ;
2022-04-10 17:37:54 +02:00
}
2022-07-13 21:30:53 +02:00
void NetworkSettingsClass : : setStaticIp ( )
2022-04-10 17:37:54 +02:00
{
2025-04-12 11:13:56 +02:00
if ( _networkMode = = network_mode : : Undefined ) {
return ;
}
const auto & config = Configuration . get ( ) . WiFi ;
const char * mode = ( _networkMode = = network_mode : : WiFi ) ? " WiFi " : " Ethernet " ;
const char * ipType = config . Dhcp ? " DHCP " : " static " ;
MessageOutput . printf ( " Start configuring %s %s IP... \r \n " , mode , ipType ) ;
bool success = false ;
2022-07-19 21:53:04 +02:00
if ( _networkMode = = network_mode : : WiFi ) {
2025-04-12 11:13:56 +02:00
if ( config . Dhcp ) {
success = WiFi . config ( INADDR_NONE , INADDR_NONE , INADDR_NONE ) ;
2022-09-06 18:44:26 +02:00
} else {
2025-04-12 11:13:56 +02:00
success = WiFi . config (
IPAddress ( config . Ip ) ,
IPAddress ( config . Gateway ) ,
IPAddress ( config . Netmask ) ,
IPAddress ( config . Dns1 ) ,
IPAddress ( config . Dns2 ) ) ;
2022-07-19 21:53:04 +02:00
}
2023-07-22 18:00:41 +02:00
} else if ( _networkMode = = network_mode : : Ethernet ) {
2025-04-12 11:13:56 +02:00
if ( config . Dhcp ) {
success = ETH . config ( INADDR_NONE , INADDR_NONE , INADDR_NONE , INADDR_NONE ) ;
2022-07-19 21:53:04 +02:00
} else {
2025-04-12 11:13:56 +02:00
success = ETH . config (
IPAddress ( config . Ip ) ,
IPAddress ( config . Gateway ) ,
IPAddress ( config . Netmask ) ,
IPAddress ( config . Dns1 ) ,
IPAddress ( config . Dns2 ) ) ;
2022-07-19 21:53:04 +02:00
}
}
2025-04-12 11:13:56 +02:00
MessageOutput . printf ( " Configure IP %s \r \n " , success ? " done " : " failed " ) ;
2022-07-19 21:53:04 +02:00
}
2023-12-12 01:30:40 +01:00
IPAddress NetworkSettingsClass : : localIP ( ) const
2022-07-19 21:53:04 +02:00
{
switch ( _networkMode ) {
case network_mode : : Ethernet :
return ETH . localIP ( ) ;
break ;
case network_mode : : WiFi :
return WiFi . localIP ( ) ;
break ;
default :
return INADDR_NONE ;
2022-04-10 17:37:54 +02:00
}
}
2023-12-12 01:30:40 +01:00
IPAddress NetworkSettingsClass : : subnetMask ( ) const
2022-07-19 21:53:04 +02:00
{
switch ( _networkMode ) {
case network_mode : : Ethernet :
return ETH . subnetMask ( ) ;
break ;
case network_mode : : WiFi :
return WiFi . subnetMask ( ) ;
break ;
default :
return IPAddress ( 255 , 255 , 255 , 0 ) ;
}
}
2023-12-12 01:30:40 +01:00
IPAddress NetworkSettingsClass : : gatewayIP ( ) const
2022-07-19 21:53:04 +02:00
{
switch ( _networkMode ) {
case network_mode : : Ethernet :
return ETH . gatewayIP ( ) ;
break ;
case network_mode : : WiFi :
return WiFi . gatewayIP ( ) ;
break ;
default :
return INADDR_NONE ;
}
}
2023-12-12 01:30:40 +01:00
IPAddress NetworkSettingsClass : : dnsIP ( const uint8_t dns_no ) const
2022-07-19 21:53:04 +02:00
{
switch ( _networkMode ) {
case network_mode : : Ethernet :
return ETH . dnsIP ( dns_no ) ;
break ;
case network_mode : : WiFi :
return WiFi . dnsIP ( dns_no ) ;
break ;
default :
return INADDR_NONE ;
}
}
2023-12-12 01:30:40 +01:00
String NetworkSettingsClass : : macAddress ( ) const
2022-07-19 21:53:04 +02:00
{
switch ( _networkMode ) {
case network_mode : : Ethernet :
2024-09-26 19:22:30 +02:00
if ( _w5500 ) {
2023-07-21 16:57:00 +02:00
return _w5500 - > macAddress ( ) ;
2024-09-26 19:22:30 +02:00
}
2022-07-19 21:53:04 +02:00
return ETH . macAddress ( ) ;
break ;
case network_mode : : WiFi :
return WiFi . macAddress ( ) ;
break ;
default :
2022-10-28 23:39:32 +02:00
return " " ;
2022-07-19 21:53:04 +02:00
}
}
2022-09-06 18:44:26 +02:00
String NetworkSettingsClass : : getHostname ( )
2022-07-19 21:53:04 +02:00
{
2022-09-06 18:44:26 +02:00
const CONFIG_T & config = Configuration . get ( ) ;
char preparedHostname [ WIFI_MAX_HOSTNAME_STRLEN + 1 ] ;
char resultHostname [ WIFI_MAX_HOSTNAME_STRLEN + 1 ] ;
uint8_t pos = 0 ;
2023-12-11 19:54:57 +01:00
const uint32_t chipId = Utils : : getChipId ( ) ;
2023-11-19 14:53:26 +01:00
snprintf ( preparedHostname , WIFI_MAX_HOSTNAME_STRLEN + 1 , config . WiFi . Hostname , chipId ) ;
2022-09-06 18:44:26 +02:00
const char * pC = preparedHostname ;
while ( * pC & & pos < WIFI_MAX_HOSTNAME_STRLEN ) { // while !null and not over length
if ( isalnum ( * pC ) ) { // if the current char is alpha-numeric append it to the hostname
resultHostname [ pos ] = * pC ;
pos + + ;
} else if ( * pC = = ' ' | | * pC = = ' _ ' | | * pC = = ' - ' | | * pC = = ' + ' | | * pC = = ' ! ' | | * pC = = ' ? ' | | * pC = = ' * ' ) {
resultHostname [ pos ] = ' - ' ;
pos + + ;
}
// else do nothing - no leading hyphens and do not include hyphens for all other characters.
pC + + ;
2022-07-19 21:53:04 +02:00
}
2022-09-06 18:44:26 +02:00
resultHostname [ pos ] = ' \0 ' ; // terminate string
// last character must not be hyphen
while ( pos > 0 & & resultHostname [ pos - 1 ] = = ' - ' ) {
resultHostname [ pos - 1 ] = ' \0 ' ;
pos - - ;
}
// Fallback if no other rule applied
if ( strlen ( resultHostname ) = = 0 ) {
snprintf ( resultHostname , WIFI_MAX_HOSTNAME_STRLEN + 1 , APP_HOSTNAME , chipId ) ;
}
return resultHostname ;
2022-07-19 21:53:04 +02:00
}
2023-12-12 01:30:40 +01:00
bool NetworkSettingsClass : : isConnected ( ) const
2022-07-19 21:53:04 +02:00
{
2025-04-15 19:58:32 +02:00
return ( WiFi . localIP ( ) [ 0 ] ! = 0 & & WiFi . isConnected ( ) ) | | ETH . localIP ( ) [ 0 ] ! = 0 ;
2022-07-19 21:53:04 +02:00
}
2023-12-12 01:30:40 +01:00
network_mode NetworkSettingsClass : : NetworkMode ( ) const
2022-07-19 21:53:04 +02:00
{
return _networkMode ;
}
2023-10-30 20:53:45 +01:00
NetworkSettingsClass NetworkSettings ;