Skip to content

Commit 1bff3e3

Browse files
committed
Adding first draft of a connection handler for Ethernet, e.g. via MKR ETH shield
1 parent 66da3c6 commit 1bff3e3

4 files changed

+161
-1
lines changed

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Datatypes (KEYWORD1)
77
####################################################
88
ConnectionHandler KEYWORD1
9+
EthernetConnectionHandler KEYWORD1
910
WiFiConnectionHandler KEYWORD1
1011
GSMConnectionHandler KEYWORD1
1112
NBConnectionHandler KEYWORD1

src/Arduino_ConnectionHandler.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@
113113
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED
114114
#endif
115115

116+
//#define ARDUINO_ETHERNET_SHIELD /* Uncomment this line if you want to use a ethernet shield via e.g. MKR ETH Shield */
117+
#if defined(ARDUINO_ETHERNET_SHIELD)
118+
#include <SPI.h>
119+
#include <Ethernet.h>
120+
121+
#define BOARD_HAS_ETHERNET
122+
#endif
123+
116124
/******************************************************************************
117125
INCLUDES
118126
******************************************************************************/
@@ -212,7 +220,9 @@ class ConnectionHandler {
212220
_on_error_event_callback = NULL;
213221
};
214222

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

0 commit comments

Comments
 (0)