25
25
CTOR/DTOR
26
26
******************************************************************************/
27
27
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
+ }
38
36
}
39
37
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)
41
42
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
42
- ,_ip{ip}
43
- ,_dns{dns}
44
- ,_gateway{gateway}
45
- ,_netmask{netmask}
46
- ,_timeout{timeout}
47
- ,_response_timeout{responseTimeout}
48
43
{
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;
50
50
}
51
51
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)
53
55
: 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}
60
56
{
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;
73
63
}
74
64
75
65
/* *****************************************************************************
@@ -87,16 +77,29 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
87
77
88
78
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting ()
89
79
{
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
+
92
91
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 );
94
94
return NetworkConnectionState::CONNECTING;
95
95
}
96
+ // An ip address is not provided -> dhcp configuration
96
97
} else {
97
- if (Ethernet.begin (nullptr , _timeout, _response_timeout ) == 0 ) {
98
+ if (Ethernet.begin (nullptr , _settings. eth . timeout , _settings. eth . response_timeout ) == 0 ) {
98
99
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
+
100
103
return NetworkConnectionState::CONNECTING;
101
104
}
102
105
}
0 commit comments