Skip to content

Commit fea952a

Browse files
fixup! readapting ConnectionHandler implementation to use setting struct
1 parent 9566552 commit fea952a

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

src/EthernetConnectionHandler.cpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,42 @@
2525
CTOR/DTOR
2626
******************************************************************************/
2727

28-
// FIXME
29-
EthernetConnectionHandler::EthernetConnectionHandler(unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
30-
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
31-
// ,_ip{INADDR_NONE}
32-
// ,_dns{INADDR_NONE}
33-
// ,_gateway{INADDR_NONE}
34-
// ,_netmask{INADDR_NONE}
35-
// ,_timeout{timeout}
36-
// ,_response_timeout{responseTimeout}
37-
{
38-
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+
}
3936
}
4037

41-
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+
// FIXME
39+
EthernetConnectionHandler::EthernetConnectionHandler(
40+
unsigned long const timeout,
41+
unsigned long const responseTimeout,
42+
bool const keep_alive)
4243
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
43-
// ,_ip{ip}
44-
// ,_dns{dns}
45-
// ,_gateway{gateway}
46-
// ,_netmask{netmask}
47-
// ,_timeout{timeout}
48-
// ,_response_timeout{responseTimeout}
4944
{
50-
45+
memset(_settings.eth.ip.dword, 0, sizeof(_settings.eth.ip.dword));
46+
memset(_settings.eth.dns.dword, 0, sizeof(_settings.eth.dns.dword));
47+
memset(_settings.eth.gateway.dword, 0, sizeof(_settings.eth.gateway.dword));
48+
memset(_settings.eth.netmask.dword, 0, sizeof(_settings.eth.netmask.dword));
49+
_settings.eth.timeout = timeout;
50+
_settings.eth.response_timeout = responseTimeout;
5151
}
5252

53-
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)
53+
EthernetConnectionHandler::EthernetConnectionHandler(
54+
const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask,
55+
unsigned long const timeout, unsigned long const responseTimeout, bool const keep_alive)
5456
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
55-
// ,_ip{INADDR_NONE}
56-
// ,_dns{INADDR_NONE}
57-
// ,_gateway{INADDR_NONE}
58-
// ,_netmask{INADDR_NONE}
59-
// ,_timeout{timeout}
60-
// ,_response_timeout{responseTimeout}
6157
{
62-
// if(!_ip.fromString(ip)) {
63-
// _ip = INADDR_NONE;
64-
// }
65-
// if(!_dns.fromString(dns)) {
66-
// _dns = INADDR_NONE;
67-
// }
68-
// if(!_gateway.fromString(gateway)) {
69-
// _gateway = INADDR_NONE;
70-
// }
71-
// if(!_netmask.fromString(netmask)) {
72-
// _netmask = INADDR_NONE;
73-
// }
58+
fromIPAddress(ip, _settings.eth.ip);
59+
fromIPAddress(dns, _settings.eth.dns);
60+
fromIPAddress(gateway, _settings.eth.gateway);
61+
fromIPAddress(netmask, _settings.eth.netmask);
62+
_settings.eth.timeout = timeout;
63+
_settings.eth.response_timeout = responseTimeout;
7464
}
7565

7666
/******************************************************************************
@@ -88,12 +78,12 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
8878

8979
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
9080
{
91-
if (_settings.eth.ip != INADDR_NONE) {
81+
if (IPAddress(_settings.eth.ip.type, _settings.eth.ip.bytes) == INADDR_NONE) {
9282
if (Ethernet.begin(nullptr,
93-
_settings.eth.ip,
94-
_settings.eth.dns,
95-
_settings.eth.gateway,
96-
_settings.eth.netmask,
83+
IPAddress(_settings.eth.ip.type, _settings.eth.ip.bytes),
84+
IPAddress(_settings.eth.dns.type, _settings.eth.dns.bytes),
85+
IPAddress(_settings.eth.gateway.type, _settings.eth.gateway.bytes),
86+
IPAddress(_settings.eth.netmask.type, _settings.eth.netmask.bytes),
9787
_settings.eth.timeout,
9888
_settings.eth.response_timeout) == 0) {
9989
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));

0 commit comments

Comments
 (0)