Skip to content

Commit 349cee2

Browse files
committed
Add CTOR and logic for static IP configuration
1 parent a3af901 commit 349cee2

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/Arduino_EthernetConnectionHandler.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626

2727
EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive)
2828
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
29+
,_ip{INADDR_NONE}
30+
,_dns{INADDR_NONE}
31+
,_gateway{INADDR_NONE}
32+
,_subnet{INADDR_NONE}
33+
{
34+
35+
}
36+
37+
EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive)
38+
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
39+
,_ip{ip}
40+
,_dns{dns}
41+
,_gateway{gateway}
42+
,_subnet{subnet}
2943
{
3044

3145
}
@@ -47,11 +61,20 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
4761

4862
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
4963
{
50-
if (Ethernet.begin(nullptr, 15000, 4000) == 0) {
64+
if (_ip != INADDR_NONE) {
65+
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _subnet, 15000, 4000) == 0) {
5166
#if !defined(__AVR__)
52-
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection"));
67+
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
5368
#endif
54-
return NetworkConnectionState::CONNECTING;
69+
return NetworkConnectionState::CONNECTING;
70+
}
71+
} else {
72+
if (Ethernet.begin(nullptr, 15000, 4000) == 0) {
73+
#if !defined(__AVR__)
74+
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection"));
75+
#endif
76+
return NetworkConnectionState::CONNECTING;
77+
}
5578
}
5679
return NetworkConnectionState::CONNECTED;
5780
}

src/Arduino_EthernetConnectionHandler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class EthernetConnectionHandler : public ConnectionHandler
3232
public:
3333

3434
EthernetConnectionHandler(bool const keep_alive = true);
35+
EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive = true);
3536

3637

3738
virtual unsigned long getTime() override { return 0; }
@@ -49,6 +50,11 @@ class EthernetConnectionHandler : public ConnectionHandler
4950

5051
private:
5152

53+
IPAddress _ip;
54+
IPAddress _dns;
55+
IPAddress _gateway;
56+
IPAddress _subnet;
57+
5258
EthernetUDP _eth_udp;
5359
EthernetClient _eth_client;
5460

0 commit comments

Comments
 (0)