mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[ETH] Fix setting static IP for Ethernet network interfaces
This commit is contained in:
@@ -126,7 +126,7 @@ void NWPluginData_static_runtime::clear(networkIndex_t networkIndex)
|
||||
|
||||
_connectionFailures = 0;
|
||||
|
||||
// FIXME TD-er: Should also clear dns cache?
|
||||
// FIXME TD-er: Should also clear dns cache and/or static IP?
|
||||
}
|
||||
|
||||
void NWPluginData_static_runtime::processEvent_and_clear()
|
||||
@@ -304,6 +304,15 @@ void NWPluginData_static_runtime::processEvents()
|
||||
}
|
||||
}
|
||||
|
||||
void NWPluginData_static_runtime::setStaticIP(const IPAddress & ip, const IPAddress & gateway, const IPAddress & subnetmask, const IPAddress & dns)
|
||||
{
|
||||
_useStaticIP = IPAddressSet(ip) && IPAddressSet(gateway) && IPAddressSet(subnetmask);
|
||||
_ip = ip;
|
||||
_gateway = gateway;
|
||||
_sn = subnetmask;
|
||||
_dns = dns;
|
||||
}
|
||||
|
||||
String NWPluginData_static_runtime::statusToString() const
|
||||
{
|
||||
String log;
|
||||
@@ -314,6 +323,7 @@ String NWPluginData_static_runtime::statusToString() const
|
||||
|
||||
if (hasIP()) {
|
||||
log += F("IP ");
|
||||
if (_useStaticIP) log += F("(static) ");
|
||||
}
|
||||
|
||||
if (operational()) {
|
||||
|
||||
@@ -127,6 +127,8 @@ struct NWPluginData_static_runtime {
|
||||
|
||||
void processEvents();
|
||||
|
||||
void setStaticIP(const IPAddress & ip, const IPAddress & gateway, const IPAddress & subnetmask, const IPAddress & dns);
|
||||
|
||||
// =============================================
|
||||
// OnOffTimers for keeping track of:
|
||||
// - start/stop of interface
|
||||
@@ -157,6 +159,9 @@ struct NWPluginData_static_runtime {
|
||||
bool _enableIPv6{}; // Cached enableIPv6 flag as it is being used from callbacks
|
||||
#endif
|
||||
|
||||
IPAddress _ip, _gateway, _sn, _dns;
|
||||
bool _useStaticIP{}; // Added to make sure we don't have to re-check IP from a callback
|
||||
|
||||
private:
|
||||
|
||||
#ifdef ESP32
|
||||
|
||||
@@ -147,7 +147,6 @@ bool NetworkConnected(bool force) {
|
||||
|
||||
if (force || (timePassedSince(last_check_millis) > 50) || (last_check_millis == 0)) {
|
||||
last_check_millis = millis();
|
||||
last_result = Network.isOnline();
|
||||
|
||||
// FIXME TD-er: This is checking for NonAP interfaces, however we also have
|
||||
// ESPEasy::net::NWPluginCall(NWPlugin::Function::NWPLUGIN_WEBSERVER_SHOULD_RUN);
|
||||
@@ -161,6 +160,7 @@ bool NetworkConnected(bool force) {
|
||||
// - Webserver
|
||||
|
||||
processNetworkEvents();
|
||||
last_result = Network.isOnline();
|
||||
}
|
||||
return last_result;
|
||||
}
|
||||
|
||||
@@ -553,6 +553,12 @@ bool NW003_data_struct_ETH_RMII::ETHConnectRelaxed() {
|
||||
|
||||
if (!(data && iface)) { return false; }
|
||||
|
||||
{
|
||||
IPAddress ip, gateway, sn, dns;
|
||||
getStaticIPAddresses(ip, gateway, sn, dns);
|
||||
data->setStaticIP(ip, gateway, sn, dns);
|
||||
}
|
||||
|
||||
if (data->started() && data->connected()) {
|
||||
if (EthLinkUp()) { return true; }
|
||||
data->mark_connect_failed();
|
||||
@@ -595,6 +601,12 @@ bool NW003_data_struct_ETH_RMII::ETHConnectRelaxed() {
|
||||
|
||||
IPAddress ip, gateway, sn, dns;
|
||||
if (getStaticIPAddresses(ip, gateway, sn, dns)) {
|
||||
addLog(LOG_LEVEL_INFO, strformat(
|
||||
F("ETH: static IP: %s, GW: %s, SN: %s, DNS:%s"),
|
||||
ip.toString().c_str(),
|
||||
gateway.toString().c_str(),
|
||||
sn.toString().c_str(),
|
||||
dns.toString().c_str()));
|
||||
iface->config(ip, gateway, sn, dns);
|
||||
}
|
||||
|
||||
|
||||
@@ -441,6 +441,12 @@ bool NW004_data_struct_ETH_SPI::ETHConnectRelaxed() {
|
||||
|
||||
if (!(data && iface)) { return false; }
|
||||
|
||||
{
|
||||
IPAddress ip, gateway, sn, dns;
|
||||
getStaticIPAddresses(ip, gateway, sn, dns);
|
||||
data->setStaticIP(ip, gateway, sn, dns);
|
||||
}
|
||||
|
||||
if (data->started() && data->connected()) {
|
||||
if (EthLinkUp()) { return true; }
|
||||
data->mark_connect_failed();
|
||||
|
||||
@@ -39,6 +39,13 @@ struct ETH_stats_and_cache_t {
|
||||
|
||||
// _eth.enableIPv6(_stats_and_cache._enableIPv6);
|
||||
}
|
||||
if (_stats_and_cache._useStaticIP) {
|
||||
_eth.config(
|
||||
_stats_and_cache._ip,
|
||||
_stats_and_cache._gateway,
|
||||
_stats_and_cache._sn,
|
||||
_stats_and_cache._dns);
|
||||
}
|
||||
}
|
||||
|
||||
void mark_stop(esp_eth_handle_t handle)
|
||||
|
||||
@@ -411,7 +411,31 @@ void addFormIPBox(const String & label,
|
||||
const IPAddress& ip)
|
||||
{
|
||||
const bool empty_IP = !IPAddressSet(ip);
|
||||
|
||||
addFormTextBox(label, id, (empty_IP) ? EMPTY_STRING : formatIP(ip));
|
||||
|
||||
/*
|
||||
// TODO TD-er: Option to validate IP in HTML
|
||||
//
|
||||
// IP input HTML validation from:
|
||||
// Source - https://stackoverflow.com/a/54796814
|
||||
// Posted by ptay, modified by community. See post 'Timeline' for change history
|
||||
// Retrieved 2026-04-30, License - CC BY-SA 4.0
|
||||
//
|
||||
// <input type="text" minlength="7" maxlength="15" size="15" pattern="^(?>(\d|[1-9]\d{2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?1)$">
|
||||
|
||||
const String value = (empty_IP) ? EMPTY_STRING : formatIP(ip);
|
||||
addRowLabel_tr_id(label, id);
|
||||
|
||||
// Validation only for IPv4 address
|
||||
addHtml(strformat(
|
||||
F("<input class='wide' type='text' minlength='7' maxlength='15' size='15'
|
||||
pattern='^(?>(\\d|[1-9]\\d{2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?1)$' name='%s' id='%s' value='%s'>"),
|
||||
id.c_str(),
|
||||
id.c_str(),
|
||||
value.c_str()
|
||||
));
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user