2525 CTOR/DTOR
2626 ******************************************************************************/
2727
28- EthernetConnectionHandler::EthernetConnectionHandler (unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
29- : ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
30- ,_ip{INADDR_NONE}
31- ,_dns{INADDR_NONE}
32- ,_gateway{INADDR_NONE}
33- ,_netmask{INADDR_NONE}
34- ,_timeout{timeout}
35- ,_response_timeout{responseTimeout}
36- {
37-
28+ static inline void fromIPAddress (const IPAddress src, models::ip_addr& dst) {
29+ if (src.type () == IPv4) {
30+ dst.dword [IPADDRESS_V4_DWORD_INDEX] = (uint32_t )src;
31+ } else if (src.type () == IPv6) {
32+ for (uint8_t i=0 ; i<sizeof (dst.bytes ); i++) {
33+ dst.bytes [i] = src[i];
34+ }
35+ }
3836}
3937
40- 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)
38+ EthernetConnectionHandler::EthernetConnectionHandler (
39+ unsigned long const timeout,
40+ unsigned long const responseTimeout,
41+ bool const keep_alive)
4142: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
42- ,_ip{ip}
43- ,_dns{dns}
44- ,_gateway{gateway}
45- ,_netmask{netmask}
46- ,_timeout{timeout}
47- ,_response_timeout{responseTimeout}
4843{
49-
44+ memset (_settings.eth .ip .dword , 0 , sizeof (_settings.eth .ip .dword ));
45+ memset (_settings.eth .dns .dword , 0 , sizeof (_settings.eth .dns .dword ));
46+ memset (_settings.eth .gateway .dword , 0 , sizeof (_settings.eth .gateway .dword ));
47+ memset (_settings.eth .netmask .dword , 0 , sizeof (_settings.eth .netmask .dword ));
48+ _settings.eth .timeout = timeout;
49+ _settings.eth .response_timeout = responseTimeout;
5050}
5151
52- 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)
52+ EthernetConnectionHandler::EthernetConnectionHandler (
53+ const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask,
54+ unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
5355: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
54- ,_ip{INADDR_NONE}
55- ,_dns{INADDR_NONE}
56- ,_gateway{INADDR_NONE}
57- ,_netmask{INADDR_NONE}
58- ,_timeout{timeout}
59- ,_response_timeout{responseTimeout}
6056{
61- if (!_ip.fromString (ip)) {
62- _ip = INADDR_NONE;
63- }
64- if (!_dns.fromString (dns)) {
65- _dns = INADDR_NONE;
66- }
67- if (!_gateway.fromString (gateway)) {
68- _gateway = INADDR_NONE;
69- }
70- if (!_netmask.fromString (netmask)) {
71- _netmask = INADDR_NONE;
72- }
57+ fromIPAddress (ip, _settings.eth .ip );
58+ fromIPAddress (dns, _settings.eth .dns );
59+ fromIPAddress (gateway, _settings.eth .gateway );
60+ fromIPAddress (netmask, _settings.eth .netmask );
61+ _settings.eth .timeout = timeout;
62+ _settings.eth .response_timeout = responseTimeout;
7363}
7464
7565/* *****************************************************************************
@@ -87,16 +77,29 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
8777
8878NetworkConnectionState EthernetConnectionHandler::update_handleConnecting ()
8979{
90- if (_ip != INADDR_NONE) {
91- if (Ethernet.begin (nullptr , _ip, _dns, _gateway, _netmask, _timeout, _response_timeout) == 0 ) {
80+ IPAddress ip (_settings.eth .ip .type , _settings.eth .ip .bytes );
81+
82+ // An ip address is provided -> static ip configuration
83+ if (ip != INADDR_NONE) {
84+ if (Ethernet.begin (nullptr , ip,
85+ IPAddress (_settings.eth .dns .type , _settings.eth .dns .bytes ),
86+ IPAddress (_settings.eth .gateway .type , _settings.eth .gateway .bytes ),
87+ IPAddress (_settings.eth .netmask .type , _settings.eth .netmask .bytes ),
88+ _settings.eth .timeout ,
89+ _settings.eth .response_timeout ) == 0 ) {
90+
9291 Debug.print (DBG_ERROR, F (" Failed to configure Ethernet, check cable connection" ));
93- Debug.print (DBG_VERBOSE, " timeout: %d, response timeout: %d" , _timeout, _response_timeout);
92+ Debug.print (DBG_VERBOSE, " timeout: %d, response timeout: %d" ,
93+ _settings.eth .timeout , _settings.eth .response_timeout );
9494 return NetworkConnectionState::CONNECTING;
9595 }
96+ // An ip address is not provided -> dhcp configuration
9697 } else {
97- if (Ethernet.begin (nullptr , _timeout, _response_timeout ) == 0 ) {
98+ if (Ethernet.begin (nullptr , _settings. eth . timeout , _settings. eth . response_timeout ) == 0 ) {
9899 Debug.print (DBG_ERROR, F (" Waiting Ethernet configuration from DHCP server, check cable connection" ));
99- Debug.print (DBG_VERBOSE, " timeout: %d, response timeout: %d" , _timeout, _response_timeout);
100+ Debug.print (DBG_VERBOSE, " timeout: %d, response timeout: %d" ,
101+ _settings.eth .timeout , _settings.eth .response_timeout );
102+
100103 return NetworkConnectionState::CONNECTING;
101104 }
102105 }
0 commit comments