Skip to content

Commit

Permalink
Add support for custom Ethernet timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
manchoz committed Dec 13, 2023
1 parent 1c5fb9b commit 36aa008
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/Arduino_EthernetConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,38 @@
CTOR/DTOR
******************************************************************************/

EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive)
EthernetConnectionHandler::EthernetConnectionHandler(unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
,_ip{INADDR_NONE}
,_dns{INADDR_NONE}
,_gateway{INADDR_NONE}
,_netmask{INADDR_NONE}
,_timeout{timeout}
,_response_timeout{responseTimeout}
{

}

EthernetConnectionHandler::EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive)
EthernetConnectionHandler::EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
,_ip{ip}
,_dns{dns}
,_gateway{gateway}
,_netmask{netmask}
,_timeout{timeout}
,_response_timeout{responseTimeout}
{

}

EthernetConnectionHandler::EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive)
EthernetConnectionHandler::EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
,_ip{INADDR_NONE}
,_dns{INADDR_NONE}
,_gateway{INADDR_NONE}
,_netmask{INADDR_NONE}
,_timeout{timeout}
,_response_timeout{responseTimeout}
{
if(!_ip.fromString(ip)) {
_ip = INADDR_NONE;
Expand Down Expand Up @@ -81,13 +87,15 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
{
if (_ip != INADDR_NONE) {
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _netmask, 15000, 4000) == 0) {
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _netmask, _timeout, _response_timeout) == 0) {
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d", _timeout, _response_timeout);
return NetworkConnectionState::CONNECTING;
}
} else {
if (Ethernet.begin(nullptr, 15000, 4000) == 0) {
if (Ethernet.begin(nullptr, _timeout, _response_timeout) == 0) {
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection"));
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d", _timeout, _response_timeout);
return NetworkConnectionState::CONNECTING;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Arduino_EthernetConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class EthernetConnectionHandler : public ConnectionHandler
{
public:

EthernetConnectionHandler(bool const keep_alive = true);
EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive = true);
EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive = true);
EthernetConnectionHandler(unsigned long const timeout = 15000, unsigned long const responseTimeout = 4000, bool const keep_alive = true);
EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, unsigned long const timeout = 15000, unsigned long const responseTimeout = 4000, bool const keep_alive = true);
EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, unsigned long const timeout = 15000, unsigned long const responseTimeout = 4000, bool const keep_alive = true);


virtual unsigned long getTime() override { return 0; }
Expand All @@ -56,6 +56,9 @@ class EthernetConnectionHandler : public ConnectionHandler
IPAddress _gateway;
IPAddress _netmask;

unsigned long _timeout;
unsigned long _response_timeout;

EthernetUDP _eth_udp;
EthernetClient _eth_client;

Expand Down

0 comments on commit 36aa008

Please sign in to comment.