Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ethernet support for Portenta + Ethernet Shield #81

Merged
merged 16 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ WiFiConnectionHandler KEYWORD1
GSMConnectionHandler KEYWORD1
NBConnectionHandler KEYWORD1
LoRaConnectionHandler KEYWORD1
EthernetConnectionHandler KEYWORD1

####################################################
# Methods and Functions (KEYWORD2)
Expand Down
3 changes: 2 additions & 1 deletion src/Arduino_ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
CONSTRUCTOR/DESTRUCTOR
******************************************************************************/

ConnectionHandler::ConnectionHandler(bool const keep_alive)
ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter interface)
: _keep_alive{keep_alive}
, _interface{interface}
, _lastConnectionTickTime{millis()}
, _current_net_connection_state{NetworkConnectionState::INIT}
{
Expand Down
37 changes: 34 additions & 3 deletions src/Arduino_ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION
#endif

#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION)
#if defined(ARDUINO_PORTENTA_H7_M7)
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Ethernet.h>
#include <PortentaEthernet.h>

#define BOARD_HAS_WIFI
#define BOARD_HAS_ETHERNET
#define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
#define NETWORK_CONNECTED WL_CONNECTED
#endif

#if defined(ARDUINO_NICLA_VISION)
#include <WiFi.h>
#include <WiFiUdp.h>

Expand Down Expand Up @@ -124,6 +138,14 @@ enum class NetworkConnectionEvent {
ERROR
};

enum class NetworkAdapter {
WIFI,
ETHERNET,
NB,
GSM,
LORA
};

typedef void (*OnNetworkEventCallback)();

/******************************************************************************
Expand All @@ -148,12 +170,12 @@ static unsigned int const CHECK_INTERVAL_TABLE[] =
class ConnectionHandler {
public:

ConnectionHandler(bool const keep_alive);
ConnectionHandler(bool const keep_alive, NetworkAdapter interface);


NetworkConnectionState check();

#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET)
virtual unsigned long getTime() = 0;
virtual Client &getClient() = 0;
virtual UDP &getUDP() = 0;
Expand All @@ -169,6 +191,10 @@ class ConnectionHandler {
return _current_net_connection_state;
}

NetworkAdapter getInterface() {
return _interface;
}

void connect();
void disconnect();

Expand All @@ -180,6 +206,7 @@ class ConnectionHandler {
protected:

bool _keep_alive;
NetworkAdapter _interface;

virtual NetworkConnectionState update_handleInit () = 0;
virtual NetworkConnectionState update_handleConnecting () = 0;
Expand Down Expand Up @@ -207,4 +234,8 @@ class ConnectionHandler {
#include "Arduino_LoRaConnectionHandler.h"
#endif

#if defined(BOARD_HAS_ETHERNET)
#include "Arduino_EthernetConnectionHandler.h"
#endif

#endif /* CONNECTION_HANDLER_H_ */
129 changes: 129 additions & 0 deletions src/Arduino_EthernetConnectionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
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 [email protected].
*/

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_EthernetConnectionHandler.h"

#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */

/******************************************************************************
CTOR/DTOR
******************************************************************************/

EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
,_ip{INADDR_NONE}
,_dns{INADDR_NONE}
,_gateway{INADDR_NONE}
,_netmask{INADDR_NONE}
{

}

EthernetConnectionHandler::EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
,_ip{ip}
,_dns{dns}
,_gateway{gateway}
,_netmask{netmask}
{

}

EthernetConnectionHandler::EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
,_ip{INADDR_NONE}
,_dns{INADDR_NONE}
,_gateway{INADDR_NONE}
,_netmask{INADDR_NONE}
{
if(!_ip.fromString(ip)) {
_ip = INADDR_NONE;
}
if(!_dns.fromString(dns)) {
_dns = INADDR_NONE;
}
if(!_gateway.fromString(gateway)) {
_gateway = INADDR_NONE;
}
if(!_netmask.fromString(netmask)) {
_netmask = INADDR_NONE;
}
}

/******************************************************************************
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;
}
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
{
if (_ip != INADDR_NONE) {
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _netmask, 15000, 4000) == 0) {
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
return NetworkConnectionState::CONNECTING;
}
} else {
if (Ethernet.begin(nullptr, 15000, 4000) == 0) {
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection"));
return NetworkConnectionState::CONNECTING;
}
}

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 */
66 changes: 66 additions & 0 deletions src/Arduino_EthernetConnectionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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 [email protected].
*/

#ifndef ARDUINO_ETHERNET_CONNECTION_HANDLER_H_
#define ARDUINO_ETHERNET_CONNECTION_HANDLER_H_

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_ConnectionHandler.h"

#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */

/******************************************************************************
CLASS DECLARATION
******************************************************************************/

class EthernetConnectionHandler : public ConnectionHandler
{
public:

EthernetConnectionHandler(bool const keep_alive = true);
EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive = true);
EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive = true);


virtual unsigned long getTime() override { return 0; }
virtual Client & getClient() override{ return _eth_client; }
virtual UDP & getUDP() override { return _eth_udp; }


protected:

virtual NetworkConnectionState update_handleInit () override;
virtual NetworkConnectionState update_handleConnecting () override;
virtual NetworkConnectionState update_handleConnected () override;
virtual NetworkConnectionState update_handleDisconnecting() override;
virtual NetworkConnectionState update_handleDisconnected () override;

private:

IPAddress _ip;
IPAddress _dns;
IPAddress _gateway;
IPAddress _netmask;

EthernetUDP _eth_udp;
EthernetClient _eth_client;

};

#endif /* #ifdef BOARD_HAS_ETHERNET */

#endif /* ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ */
2 changes: 1 addition & 1 deletion src/Arduino_GSMConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ __attribute__((weak)) void mkr_gsm_feed_watchdog()
******************************************************************************/

GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
: ConnectionHandler{keep_alive}
: ConnectionHandler{keep_alive, NetworkAdapter::GSM}
, _pin(pin)
, _apn(apn)
, _login(login)
Expand Down
2 changes: 1 addition & 1 deletion src/Arduino_LoRaConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef enum
CTOR/DTOR
******************************************************************************/
LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band, char const * channelMask, _lora_class const device_class)
: ConnectionHandler{false}
: ConnectionHandler{false, NetworkAdapter::LORA}
, _appeui(appeui)
, _appkey(appkey)
, _band(band)
Expand Down
2 changes: 1 addition & 1 deletion src/Arduino_NBConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, boo
}

NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, char const * login, char const * pass, bool const keep_alive)
: ConnectionHandler{keep_alive}
: ConnectionHandler{keep_alive, NetworkAdapter::NB}
, _pin(pin)
, _apn(apn)
, _login(login)
Expand Down
2 changes: 1 addition & 1 deletion src/Arduino_WiFiConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
******************************************************************************/

WiFiConnectionHandler::WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive)
: ConnectionHandler{keep_alive}
: ConnectionHandler{keep_alive, NetworkAdapter::WIFI}
, _ssid{ssid}
, _pass{pass}
{
Expand Down