From 7ab21acea7a014a71f875e75b685bfa89f20164c Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 14 Jul 2022 12:03:14 +0200 Subject: [PATCH 01/16] Add EthernetConnectionHandler --- keywords.txt | 1 + src/Arduino_ConnectionHandler.h | 6 +- src/Arduino_EthernetConnectionHandler.cpp | 82 +++++++++++++++++++++++ src/Arduino_EthernetConnectionHandler.h | 61 +++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/Arduino_EthernetConnectionHandler.cpp create mode 100644 src/Arduino_EthernetConnectionHandler.h diff --git a/keywords.txt b/keywords.txt index 8d9e9434..4ac67251 100644 --- a/keywords.txt +++ b/keywords.txt @@ -10,6 +10,7 @@ WiFiConnectionHandler KEYWORD1 GSMConnectionHandler KEYWORD1 NBConnectionHandler KEYWORD1 LoRaConnectionHandler KEYWORD1 +EthernetConnectionHandler KEYWORD1 #################################################### # Methods and Functions (KEYWORD2) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index cc70f5fe..abe3f9ea 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -153,7 +153,7 @@ class ConnectionHandler { 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; @@ -207,4 +207,8 @@ class ConnectionHandler { #include "Arduino_LoRaConnectionHandler.h" #endif +#if defined(BOARD_HAS_ETHERNET) + #include "Arduino_EthernetConnectionHandler.h" +#endif + #endif /* CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp new file mode 100644 index 00000000..fff9d052 --- /dev/null +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -0,0 +1,82 @@ +/* + 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 "Arduino_EthernetConnectionHandler.h" + +#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */ + +/****************************************************************************** + CTOR/DTOR + ******************************************************************************/ + +EthernetConnectionHandler::EthernetConnectionHandler(uint8_t * mac, bool const keep_alive) +: ConnectionHandler{keep_alive} +, _mac{mac} +{ + +} + +/****************************************************************************** + PROTECTED MEMBER FUNCTIONS + ******************************************************************************/ + +NetworkConnectionState EthernetConnectionHandler::update_handleInit() +{ + if (Ethernet.begin(const_cast(_mac)) == 0) { + Debug.print(DBG_ERROR, F("Failed to configure Ethernet using DHCP")); + + if (Ethernet.hardwareStatus() == EthernetNoHardware) { + Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); + return NetworkConnectionState::ERROR; + } + + if (Ethernet.linkStatus() == LinkOFF) { + Debug.print(DBG_ERROR, F("Error, ethernet cable is not connected.")); + return NetworkConnectionState::ERROR; + } + + return NetworkConnectionState::ERROR; + } + + return NetworkConnectionState::CONNECTING; +} + +NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() +{ + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState EthernetConnectionHandler::update_handleConnected() +{ + if (Ethernet.linkStatus() == LinkON) + return NetworkConnectionState::CONNECTED; + else + return NetworkConnectionState::DISCONNECTED; +} + +NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() +{ + return NetworkConnectionState::DISCONNECTED; +} + +NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() +{ + return NetworkConnectionState::INIT; +} + +#endif /* #ifdef BOARD_HAS_ETHERNET */ diff --git a/src/Arduino_EthernetConnectionHandler.h b/src/Arduino_EthernetConnectionHandler.h new file mode 100644 index 00000000..3c36caea --- /dev/null +++ b/src/Arduino_EthernetConnectionHandler.h @@ -0,0 +1,61 @@ +/* + 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. +*/ + +#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(uint8_t * mac, 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: + + uint8_t const * _mac; + + EthernetUDP _eth_udp; + EthernetClient _eth_client; + +}; + +#endif /* #ifdef BOARD_HAS_ETHERNET */ + +#endif /* ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ */ From d47a687fde51a6e714934f44aeb1363631531f2f Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 14 Jul 2022 12:07:19 +0200 Subject: [PATCH 02/16] Enable Ethernet support for Portenta H7. Needs Vision shield --- src/Arduino_ConnectionHandler.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index abe3f9ea..8f028684 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -43,7 +43,18 @@ #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 + #include + + #define BOARD_HAS_WIFI + #define BOARD_HAS_PORTENTA_VISION_SHIELD + #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 #include @@ -96,6 +107,12 @@ #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED #endif +#if defined(BOARD_HAS_PORTENTA_VISION_SHIELD) + #define BOARD_HAS_ETHERNET + + #include +#endif + /****************************************************************************** INCLUDES ******************************************************************************/ From 9cb7f992163cea2ee32c2f02c84004f6f7e6de61 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 21 Jul 2022 11:19:58 +0200 Subject: [PATCH 03/16] Remove MAC address configuration --- src/Arduino_EthernetConnectionHandler.cpp | 5 ++--- src/Arduino_EthernetConnectionHandler.h | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index fff9d052..cdb6b2de 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -24,9 +24,8 @@ CTOR/DTOR ******************************************************************************/ -EthernetConnectionHandler::EthernetConnectionHandler(uint8_t * mac, bool const keep_alive) +EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) : ConnectionHandler{keep_alive} -, _mac{mac} { } @@ -37,7 +36,7 @@ EthernetConnectionHandler::EthernetConnectionHandler(uint8_t * mac, bool const k NetworkConnectionState EthernetConnectionHandler::update_handleInit() { - if (Ethernet.begin(const_cast(_mac)) == 0) { + if (Ethernet.begin() == 0) { Debug.print(DBG_ERROR, F("Failed to configure Ethernet using DHCP")); if (Ethernet.hardwareStatus() == EthernetNoHardware) { diff --git a/src/Arduino_EthernetConnectionHandler.h b/src/Arduino_EthernetConnectionHandler.h index 3c36caea..92b44179 100644 --- a/src/Arduino_EthernetConnectionHandler.h +++ b/src/Arduino_EthernetConnectionHandler.h @@ -31,7 +31,7 @@ class EthernetConnectionHandler : public ConnectionHandler { public: - EthernetConnectionHandler(uint8_t * mac, bool const keep_alive = true); + EthernetConnectionHandler(bool const keep_alive = true); virtual unsigned long getTime() override { return 0; } @@ -49,8 +49,6 @@ class EthernetConnectionHandler : public ConnectionHandler private: - uint8_t const * _mac; - EthernetUDP _eth_udp; EthernetClient _eth_client; From 37758d37af1b21ef8c54e3041dc039b1dba13cf8 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 22 Jul 2022 15:57:13 +0200 Subject: [PATCH 04/16] Add function to get the hardware network interface type used for connection --- src/Arduino_ConnectionHandler.cpp | 3 ++- src/Arduino_ConnectionHandler.h | 15 ++++++++++++++- src/Arduino_EthernetConnectionHandler.cpp | 2 +- src/Arduino_GSMConnectionHandler.cpp | 2 +- src/Arduino_LoRaConnectionHandler.cpp | 2 +- src/Arduino_NBConnectionHandler.cpp | 2 +- src/Arduino_WiFiConnectionHandler.cpp | 2 +- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Arduino_ConnectionHandler.cpp b/src/Arduino_ConnectionHandler.cpp index 943fa1d9..ab83fa08 100644 --- a/src/Arduino_ConnectionHandler.cpp +++ b/src/Arduino_ConnectionHandler.cpp @@ -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} { diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 8f028684..b55954f4 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -141,6 +141,14 @@ enum class NetworkConnectionEvent { ERROR }; +enum class NetworkAdapter { + WIFI, + ETHERNET, + NB, + GSM, + LORA +}; + typedef void (*OnNetworkEventCallback)(); /****************************************************************************** @@ -165,7 +173,7 @@ static unsigned int const CHECK_INTERVAL_TABLE[] = class ConnectionHandler { public: - ConnectionHandler(bool const keep_alive); + ConnectionHandler(bool const keep_alive, NetworkAdapter interface); NetworkConnectionState check(); @@ -186,6 +194,10 @@ class ConnectionHandler { return _current_net_connection_state; } + NetworkAdapter getInterface() { + return _interface; + } + void connect(); void disconnect(); @@ -197,6 +209,7 @@ class ConnectionHandler { protected: bool _keep_alive; + NetworkAdapter _interface; virtual NetworkConnectionState update_handleInit () = 0; virtual NetworkConnectionState update_handleConnecting () = 0; diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index cdb6b2de..c9c205c8 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -25,7 +25,7 @@ ******************************************************************************/ EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) -: ConnectionHandler{keep_alive} +: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} { } diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index 3bf8b908..e57a7201 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -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) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index e34092b0..886c7e3f 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -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) diff --git a/src/Arduino_NBConnectionHandler.cpp b/src/Arduino_NBConnectionHandler.cpp index 8c747edf..ae74a238 100644 --- a/src/Arduino_NBConnectionHandler.cpp +++ b/src/Arduino_NBConnectionHandler.cpp @@ -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) diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index 45a23b99..eb897450 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -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} { From a3af901c5b83e5c93318e93697aa4f50d1e4d8d5 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 25 Jul 2022 13:45:27 +0200 Subject: [PATCH 05/16] Handle automatic retries on connect/disconnect --- src/Arduino_EthernetConnectionHandler.cpp | 51 +++++++++++++++-------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index c9c205c8..374b9c5c 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -36,46 +36,61 @@ EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) NetworkConnectionState EthernetConnectionHandler::update_handleInit() { - if (Ethernet.begin() == 0) { - Debug.print(DBG_ERROR, F("Failed to configure Ethernet using DHCP")); - - if (Ethernet.hardwareStatus() == EthernetNoHardware) { - Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); - return NetworkConnectionState::ERROR; - } - - if (Ethernet.linkStatus() == LinkOFF) { - Debug.print(DBG_ERROR, F("Error, ethernet cable is not connected.")); - return NetworkConnectionState::ERROR; - } - + if (Ethernet.hardwareStatus() == EthernetNoHardware) { +#if !defined(__AVR__) + Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); +#endif return NetworkConnectionState::ERROR; } - return NetworkConnectionState::CONNECTING; } NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() { + if (Ethernet.begin(nullptr, 15000, 4000) == 0) { +#if !defined(__AVR__) + Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection")); +#endif + return NetworkConnectionState::CONNECTING; + } return NetworkConnectionState::CONNECTED; } NetworkConnectionState EthernetConnectionHandler::update_handleConnected() { - if (Ethernet.linkStatus() == LinkON) - return NetworkConnectionState::CONNECTED; - else + if (Ethernet.linkStatus() == LinkOFF) { +#if !defined(__AVR__) + Debug.print(DBG_VERBOSE, F("Ethernet.status(): %d"), Ethernet.status()); + Debug.print(DBG_ERROR, F("Connection lost.")); +#endif + if (_keep_alive) + { +#if !defined(__AVR__) + Debug.print(DBG_ERROR, F("Attempting reconnection")); +#endif + } return NetworkConnectionState::DISCONNECTED; + } + return NetworkConnectionState::CONNECTED; } NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() { + Ethernet.disconnect(); return NetworkConnectionState::DISCONNECTED; } NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() { - return NetworkConnectionState::INIT; + Ethernet.end(); + if (_keep_alive) + { + return NetworkConnectionState::INIT; + } + else + { + return NetworkConnectionState::CLOSED; + } } #endif /* #ifdef BOARD_HAS_ETHERNET */ From 349cee21261c50a95414c980d5190c38b6910791 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 26 Jul 2022 14:41:13 +0200 Subject: [PATCH 06/16] Add CTOR and logic for static IP configuration --- src/Arduino_EthernetConnectionHandler.cpp | 29 ++++++++++++++++++++--- src/Arduino_EthernetConnectionHandler.h | 6 +++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index 374b9c5c..71382603 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -26,6 +26,20 @@ EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) : ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} +,_ip{INADDR_NONE} +,_dns{INADDR_NONE} +,_gateway{INADDR_NONE} +,_subnet{INADDR_NONE} +{ + +} + +EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive) +: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} +,_ip{ip} +,_dns{dns} +,_gateway{gateway} +,_subnet{subnet} { } @@ -47,11 +61,20 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit() NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() { - if (Ethernet.begin(nullptr, 15000, 4000) == 0) { + if (_ip != INADDR_NONE) { + if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _subnet, 15000, 4000) == 0) { #if !defined(__AVR__) - Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection")); + Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection")); #endif - return NetworkConnectionState::CONNECTING; + return NetworkConnectionState::CONNECTING; + } + } else { + if (Ethernet.begin(nullptr, 15000, 4000) == 0) { +#if !defined(__AVR__) + Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection")); +#endif + return NetworkConnectionState::CONNECTING; + } } return NetworkConnectionState::CONNECTED; } diff --git a/src/Arduino_EthernetConnectionHandler.h b/src/Arduino_EthernetConnectionHandler.h index 92b44179..f9074e74 100644 --- a/src/Arduino_EthernetConnectionHandler.h +++ b/src/Arduino_EthernetConnectionHandler.h @@ -32,6 +32,7 @@ class EthernetConnectionHandler : public ConnectionHandler public: EthernetConnectionHandler(bool const keep_alive = true); + EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive = true); virtual unsigned long getTime() override { return 0; } @@ -49,6 +50,11 @@ class EthernetConnectionHandler : public ConnectionHandler private: + IPAddress _ip; + IPAddress _dns; + IPAddress _gateway; + IPAddress _subnet; + EthernetUDP _eth_udp; EthernetClient _eth_client; From 328bd83be8424f2093ab6936a7f13db7825c87e2 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 3 Aug 2022 08:44:57 +0200 Subject: [PATCH 07/16] Use more detailed define to enable ethernet support Otherwise we cannot distinguish between LoRa/Ethernet Vision Shield --- src/Arduino_ConnectionHandler.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index b55954f4..aaade80e 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -48,7 +48,7 @@ #include #define BOARD_HAS_WIFI - #define BOARD_HAS_PORTENTA_VISION_SHIELD + #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 @@ -107,7 +107,7 @@ #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED #endif -#if defined(BOARD_HAS_PORTENTA_VISION_SHIELD) +#if defined(BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET) #define BOARD_HAS_ETHERNET #include From 9b74bb698d91b51d51a42dfe564e0126a6f938c6 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 14 Sep 2022 09:38:48 +0200 Subject: [PATCH 08/16] Add missing include file --- src/Arduino_ConnectionHandler.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index aaade80e..f622834b 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -54,6 +54,13 @@ #define NETWORK_CONNECTED WL_CONNECTED #endif +#if defined(BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET) + #include + #include + + #define BOARD_HAS_ETHERNET +#endif + #if defined(ARDUINO_NICLA_VISION) #include #include @@ -107,12 +114,6 @@ #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED #endif -#if defined(BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET) - #define BOARD_HAS_ETHERNET - - #include -#endif - /****************************************************************************** INCLUDES ******************************************************************************/ From dafc31b36a03b30c6d2106d16791d5581fe0b246 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 14 Sep 2022 09:43:01 +0200 Subject: [PATCH 09/16] Remove useless call to Ethernet.end() --- src/Arduino_EthernetConnectionHandler.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index 71382603..2b71319d 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -105,7 +105,6 @@ NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() { - Ethernet.end(); if (_keep_alive) { return NetworkConnectionState::INIT; From 8f0b5946e78e85241b96fb706723634d47073386 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 14 Sep 2022 10:12:02 +0200 Subject: [PATCH 10/16] Simplify debug prints --- src/Arduino_EthernetConnectionHandler.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index 2b71319d..75450fe8 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -83,8 +83,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnected() { if (Ethernet.linkStatus() == LinkOFF) { #if !defined(__AVR__) - Debug.print(DBG_VERBOSE, F("Ethernet.status(): %d"), Ethernet.status()); - Debug.print(DBG_ERROR, F("Connection lost.")); + Debug.print(DBG_ERROR, F("Ethernet link OFF, connection lost.")); #endif if (_keep_alive) { From a3fee7ea58599c77e7639857e7b9ee9cf831666f Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 14 Sep 2022 16:43:14 +0200 Subject: [PATCH 11/16] Drop #if !defined(__AVR__) statements --- src/Arduino_EthernetConnectionHandler.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index 75450fe8..8c129c7e 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -51,9 +51,7 @@ EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns NetworkConnectionState EthernetConnectionHandler::update_handleInit() { if (Ethernet.hardwareStatus() == EthernetNoHardware) { -#if !defined(__AVR__) Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); -#endif return NetworkConnectionState::ERROR; } return NetworkConnectionState::CONNECTING; @@ -63,33 +61,26 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() { if (_ip != INADDR_NONE) { if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _subnet, 15000, 4000) == 0) { -#if !defined(__AVR__) Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection")); -#endif return NetworkConnectionState::CONNECTING; } } else { if (Ethernet.begin(nullptr, 15000, 4000) == 0) { -#if !defined(__AVR__) Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection")); -#endif return NetworkConnectionState::CONNECTING; } } + return NetworkConnectionState::CONNECTED; } NetworkConnectionState EthernetConnectionHandler::update_handleConnected() { if (Ethernet.linkStatus() == LinkOFF) { -#if !defined(__AVR__) Debug.print(DBG_ERROR, F("Ethernet link OFF, connection lost.")); -#endif if (_keep_alive) { -#if !defined(__AVR__) Debug.print(DBG_ERROR, F("Attempting reconnection")); -#endif } return NetworkConnectionState::DISCONNECTED; } From 1026b98034d2dc5b22ca317ab327a9483ae4babd Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 20 Oct 2022 13:33:26 +0200 Subject: [PATCH 12/16] Simplify ethernet support for portenta based boards --- src/Arduino_ConnectionHandler.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index f622834b..9a5d6961 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -46,21 +46,17 @@ #if defined(ARDUINO_PORTENTA_H7_M7) #include #include + #include + #include #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(BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET) - #include - #include - - #define BOARD_HAS_ETHERNET -#endif - #if defined(ARDUINO_NICLA_VISION) #include #include From 1f3b9a54d695960021a4b91528f0dd30a7868db5 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 20 Oct 2022 13:36:28 +0200 Subject: [PATCH 13/16] Rename parameter and internal variables from subnet to netmask --- src/Arduino_EthernetConnectionHandler.cpp | 8 ++++---- src/Arduino_EthernetConnectionHandler.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index 8c129c7e..1e85894b 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -29,17 +29,17 @@ EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) ,_ip{INADDR_NONE} ,_dns{INADDR_NONE} ,_gateway{INADDR_NONE} -,_subnet{INADDR_NONE} +,_netmask{INADDR_NONE} { } -EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive) +EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress netmask, bool const keep_alive) : ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} ,_ip{ip} ,_dns{dns} ,_gateway{gateway} -,_subnet{subnet} +,_netmask{netmask} { } @@ -60,7 +60,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit() NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() { if (_ip != INADDR_NONE) { - if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _subnet, 15000, 4000) == 0) { + 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; } diff --git a/src/Arduino_EthernetConnectionHandler.h b/src/Arduino_EthernetConnectionHandler.h index f9074e74..ed4ab1dc 100644 --- a/src/Arduino_EthernetConnectionHandler.h +++ b/src/Arduino_EthernetConnectionHandler.h @@ -32,7 +32,7 @@ class EthernetConnectionHandler : public ConnectionHandler public: EthernetConnectionHandler(bool const keep_alive = true); - EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive = true); + EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress netmask, bool const keep_alive = true); virtual unsigned long getTime() override { return 0; } @@ -53,7 +53,7 @@ class EthernetConnectionHandler : public ConnectionHandler IPAddress _ip; IPAddress _dns; IPAddress _gateway; - IPAddress _subnet; + IPAddress _netmask; EthernetUDP _eth_udp; EthernetClient _eth_client; From b53dd950dd92e1a8a176f473662d254cec62cf76 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 20 Oct 2022 13:40:07 +0200 Subject: [PATCH 14/16] Add constructor to handle string parameters --- src/Arduino_EthernetConnectionHandler.cpp | 21 +++++++++++++++++++++ src/Arduino_EthernetConnectionHandler.h | 1 + 2 files changed, 22 insertions(+) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index 1e85894b..cf0c6f6c 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -44,6 +44,27 @@ EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns } +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 ******************************************************************************/ diff --git a/src/Arduino_EthernetConnectionHandler.h b/src/Arduino_EthernetConnectionHandler.h index ed4ab1dc..6110a3bf 100644 --- a/src/Arduino_EthernetConnectionHandler.h +++ b/src/Arduino_EthernetConnectionHandler.h @@ -33,6 +33,7 @@ class EthernetConnectionHandler : public ConnectionHandler EthernetConnectionHandler(bool const keep_alive = true); EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, 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; } From 2462ec0f8ea84058e9db9c1549ad8384670ca123 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 20 Oct 2022 13:43:17 +0200 Subject: [PATCH 15/16] Use const attribute for IPAddress parameters --- src/Arduino_EthernetConnectionHandler.cpp | 2 +- src/Arduino_EthernetConnectionHandler.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Arduino_EthernetConnectionHandler.cpp b/src/Arduino_EthernetConnectionHandler.cpp index cf0c6f6c..0753eb23 100644 --- a/src/Arduino_EthernetConnectionHandler.cpp +++ b/src/Arduino_EthernetConnectionHandler.cpp @@ -34,7 +34,7 @@ EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) } -EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress netmask, bool const keep_alive) +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} diff --git a/src/Arduino_EthernetConnectionHandler.h b/src/Arduino_EthernetConnectionHandler.h index 6110a3bf..bac7bf45 100644 --- a/src/Arduino_EthernetConnectionHandler.h +++ b/src/Arduino_EthernetConnectionHandler.h @@ -32,7 +32,7 @@ class EthernetConnectionHandler : public ConnectionHandler public: EthernetConnectionHandler(bool const keep_alive = true); - EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress netmask, 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); From 4e68980d80c5c17a0068c10fa62ac107de7a0dde Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 2 Nov 2022 15:43:33 +0100 Subject: [PATCH 16/16] Extend example adding ethernet connection handler --- .../ConnectionHandlerDemo.ino | 15 ++++++++++++++- examples/ConnectionHandlerDemo/arduino_secrets.h | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index b106eed6..4c364a51 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -14,13 +14,26 @@ * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows * * NBConnectionHandler conMan(SECRET_PIN); + * + * If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows: + * + * DHCP mode + * EthernetConnectionHandler conMan(); + * + * Manual configuration + * EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); + * + * Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE. + * */ #include "arduino_secrets.h" #include -#if defined(BOARD_HAS_WIFI) +#if defined(BOARD_HAS_ETHERNET) +EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); +#elif defined(BOARD_HAS_WIFI) WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS); #elif defined(BOARD_HAS_GSM) GSMConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS); diff --git a/examples/ConnectionHandlerDemo/arduino_secrets.h b/examples/ConnectionHandlerDemo/arduino_secrets.h index 0a2a2fe6..5836270e 100644 --- a/examples/ConnectionHandlerDemo/arduino_secrets.h +++ b/examples/ConnectionHandlerDemo/arduino_secrets.h @@ -8,3 +8,8 @@ const char SECRET_GSM_PASS[] = "GSM PASSWORD"; const char SECRET_APP_EUI[] = "APP_EUI"; const char SECRET_APP_KEY[] = "APP_KEY"; + +const char SECRET_IP[] = "IP ADDRESS"; +const char SECRET_DNS[] = "DNS ADDRESS"; +const char SECRET_GATEWAY[] = "GATEWAY ADDRESS"; +const char SECRET_NETMASK[] = "NETWORK MASK";