forked from arduino-libraries/Arduino_ConnectionHandler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEthernetConnectionHandler.cpp
More file actions
162 lines (141 loc) · 5.74 KB
/
EthernetConnectionHandler.cpp
File metadata and controls
162 lines (141 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
This file is part of ArduinoIoTCloud.
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to license@arduino.cc.
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "ConnectionHandlerDefinitions.h"
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
#include "EthernetConnectionHandler.h"
/******************************************************************************
CTOR/DTOR
******************************************************************************/
static inline void fromIPAddress(const IPAddress src, models::ip_addr& dst) {
if(src.type() == IPv4) {
dst.dword[IPADDRESS_V4_DWORD_INDEX] = (uint32_t)src;
} else if(src.type() == IPv6) {
for(uint8_t i=0; i<sizeof(dst.bytes); i++) {
dst.bytes[i] = src[i];
}
}
}
EthernetConnectionHandler::EthernetConnectionHandler(
unsigned long const timeout,
unsigned long const responseTimeout,
bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
{
memset(_settings.eth.ip.dword, 0, sizeof(_settings.eth.ip.dword));
memset(_settings.eth.dns.dword, 0, sizeof(_settings.eth.dns.dword));
memset(_settings.eth.gateway.dword, 0, sizeof(_settings.eth.gateway.dword));
memset(_settings.eth.netmask.dword, 0, sizeof(_settings.eth.netmask.dword));
_settings.eth.timeout = timeout;
_settings.eth.response_timeout = responseTimeout;
}
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}
{
fromIPAddress(ip, _settings.eth.ip);
fromIPAddress(dns, _settings.eth.dns);
fromIPAddress(gateway, _settings.eth.gateway);
fromIPAddress(netmask, _settings.eth.netmask);
_settings.eth.timeout = timeout;
_settings.eth.response_timeout = responseTimeout;
}
/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState EthernetConnectionHandler::update_handleInit()
{
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found."));
return NetworkConnectionState::ERROR;
}
IPAddress ip(_settings.eth.ip.type, _settings.eth.ip.bytes);
// An ip address is provided -> static ip configuration
if (ip != INADDR_NONE) {
if (Ethernet.begin(nullptr, ip,
IPAddress(_settings.eth.dns.type, _settings.eth.dns.bytes),
IPAddress(_settings.eth.gateway.type, _settings.eth.gateway.bytes),
IPAddress(_settings.eth.netmask.type, _settings.eth.netmask.bytes),
_settings.eth.timeout,
_settings.eth.response_timeout) == 0) {
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
_settings.eth.timeout, _settings.eth.response_timeout);
return NetworkConnectionState::INIT;
}
// An ip address is not provided -> dhcp configuration
} else {
if (Ethernet.begin(nullptr, _settings.eth.timeout, _settings.eth.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",
_settings.eth.timeout, _settings.eth.response_timeout);
return NetworkConnectionState::INIT;
}
}
return NetworkConnectionState::CONNECTING;
}
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
{
if (Ethernet.linkStatus() == LinkOFF) {
return NetworkConnectionState::INIT;
}
if (!_check_internet_availability) {
return NetworkConnectionState::CONNECTED;
}
int ping_result = Ethernet.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("Ethernet.ping(): %d"), ping_result);
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("Internet check failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
else
{
Debug.print(DBG_INFO, F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;
}
}
NetworkConnectionState EthernetConnectionHandler::update_handleConnected()
{
if (Ethernet.linkStatus() == LinkOFF) {
Debug.print(DBG_ERROR, F("Ethernet link OFF, connection lost."));
if (_keep_alive)
{
Debug.print(DBG_ERROR, F("Attempting reconnection"));
}
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting()
{
Ethernet.disconnect();
return NetworkConnectionState::DISCONNECTED;
}
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected()
{
if (_keep_alive)
{
return NetworkConnectionState::INIT;
}
else
{
return NetworkConnectionState::CLOSED;
}
}
#endif /* #ifdef BOARD_HAS_ETHERNET */