Skip to content

Commit 7ab21ac

Browse files
committed
Add EthernetConnectionHandler
1 parent 95de8e7 commit 7ab21ac

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ WiFiConnectionHandler KEYWORD1
1010
GSMConnectionHandler KEYWORD1
1111
NBConnectionHandler KEYWORD1
1212
LoRaConnectionHandler KEYWORD1
13+
EthernetConnectionHandler KEYWORD1
1314

1415
####################################################
1516
# Methods and Functions (KEYWORD2)

src/Arduino_ConnectionHandler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class ConnectionHandler {
153153

154154
NetworkConnectionState check();
155155

156-
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
156+
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET)
157157
virtual unsigned long getTime() = 0;
158158
virtual Client &getClient() = 0;
159159
virtual UDP &getUDP() = 0;
@@ -207,4 +207,8 @@ class ConnectionHandler {
207207
#include "Arduino_LoRaConnectionHandler.h"
208208
#endif
209209

210+
#if defined(BOARD_HAS_ETHERNET)
211+
#include "Arduino_EthernetConnectionHandler.h"
212+
#endif
213+
210214
#endif /* CONNECTION_HANDLER_H_ */
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
This software is released under the GNU General Public License version 3,
5+
which covers the main part of arduino-cli.
6+
The terms of this license can be found at:
7+
https://www.gnu.org/licenses/gpl-3.0.en.html
8+
You can be released from the requirements of the above licenses by purchasing
9+
a commercial license. Buying such a license is mandatory if you want to modify or
10+
otherwise use the software for commercial activities involving the Arduino
11+
software without disclosing the source code of your own applications. To purchase
12+
a commercial license, send an email to license@arduino.cc.
13+
*/
14+
15+
/******************************************************************************
16+
INCLUDE
17+
******************************************************************************/
18+
19+
#include "Arduino_EthernetConnectionHandler.h"
20+
21+
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
22+
23+
/******************************************************************************
24+
CTOR/DTOR
25+
******************************************************************************/
26+
27+
EthernetConnectionHandler::EthernetConnectionHandler(uint8_t * mac, bool const keep_alive)
28+
: ConnectionHandler{keep_alive}
29+
, _mac{mac}
30+
{
31+
32+
}
33+
34+
/******************************************************************************
35+
PROTECTED MEMBER FUNCTIONS
36+
******************************************************************************/
37+
38+
NetworkConnectionState EthernetConnectionHandler::update_handleInit()
39+
{
40+
if (Ethernet.begin(const_cast<uint8_t *>(_mac)) == 0) {
41+
Debug.print(DBG_ERROR, F("Failed to configure Ethernet using DHCP"));
42+
43+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
44+
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found."));
45+
return NetworkConnectionState::ERROR;
46+
}
47+
48+
if (Ethernet.linkStatus() == LinkOFF) {
49+
Debug.print(DBG_ERROR, F("Error, ethernet cable is not connected."));
50+
return NetworkConnectionState::ERROR;
51+
}
52+
53+
return NetworkConnectionState::ERROR;
54+
}
55+
56+
return NetworkConnectionState::CONNECTING;
57+
}
58+
59+
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
60+
{
61+
return NetworkConnectionState::CONNECTED;
62+
}
63+
64+
NetworkConnectionState EthernetConnectionHandler::update_handleConnected()
65+
{
66+
if (Ethernet.linkStatus() == LinkON)
67+
return NetworkConnectionState::CONNECTED;
68+
else
69+
return NetworkConnectionState::DISCONNECTED;
70+
}
71+
72+
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting()
73+
{
74+
return NetworkConnectionState::DISCONNECTED;
75+
}
76+
77+
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected()
78+
{
79+
return NetworkConnectionState::INIT;
80+
}
81+
82+
#endif /* #ifdef BOARD_HAS_ETHERNET */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
This software is released under the GNU General Public License version 3,
5+
which covers the main part of arduino-cli.
6+
The terms of this license can be found at:
7+
https://www.gnu.org/licenses/gpl-3.0.en.html
8+
You can be released from the requirements of the above licenses by purchasing
9+
a commercial license. Buying such a license is mandatory if you want to modify or
10+
otherwise use the software for commercial activities involving the Arduino
11+
software without disclosing the source code of your own applications. To purchase
12+
a commercial license, send an email to license@arduino.cc.
13+
*/
14+
15+
#ifndef ARDUINO_ETHERNET_CONNECTION_HANDLER_H_
16+
#define ARDUINO_ETHERNET_CONNECTION_HANDLER_H_
17+
18+
/******************************************************************************
19+
INCLUDE
20+
******************************************************************************/
21+
22+
#include "Arduino_ConnectionHandler.h"
23+
24+
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
25+
26+
/******************************************************************************
27+
CLASS DECLARATION
28+
******************************************************************************/
29+
30+
class EthernetConnectionHandler : public ConnectionHandler
31+
{
32+
public:
33+
34+
EthernetConnectionHandler(uint8_t * mac, bool const keep_alive = true);
35+
36+
37+
virtual unsigned long getTime() override { return 0; }
38+
virtual Client & getClient() override{ return _eth_client; }
39+
virtual UDP & getUDP() override { return _eth_udp; }
40+
41+
42+
protected:
43+
44+
virtual NetworkConnectionState update_handleInit () override;
45+
virtual NetworkConnectionState update_handleConnecting () override;
46+
virtual NetworkConnectionState update_handleConnected () override;
47+
virtual NetworkConnectionState update_handleDisconnecting() override;
48+
virtual NetworkConnectionState update_handleDisconnected () override;
49+
50+
private:
51+
52+
uint8_t const * _mac;
53+
54+
EthernetUDP _eth_udp;
55+
EthernetClient _eth_client;
56+
57+
};
58+
59+
#endif /* #ifdef BOARD_HAS_ETHERNET */
60+
61+
#endif /* ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)