From f2b100a866875110ed5de9c5dd183d385f8e1c11 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Wed, 5 Jun 2024 13:48:25 +0200 Subject: [PATCH] splitting src/Arduino_ConnectionHandler.h into multiple files to avoid cyclic includes --- src/Arduino_ConnectionHandler.h | 270 +----------------- src/Arduino_ConnectionHandlerDefinitions.h | 185 ++++++++++++ ...=> Arduino_ConnectionHandlerInterface.cpp} | 2 +- src/Arduino_ConnectionHandlerInterface.h | 99 +++++++ 4 files changed, 293 insertions(+), 263 deletions(-) create mode 100644 src/Arduino_ConnectionHandlerDefinitions.h rename src/{Arduino_ConnectionHandler.cpp => Arduino_ConnectionHandlerInterface.cpp} (99%) create mode 100644 src/Arduino_ConnectionHandlerInterface.h diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index b4f8e315..ee79dc3a 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -27,275 +27,21 @@ #endif #include +#include "Arduino_ConnectionHandlerDefinitions.h" -#ifdef ARDUINO_SAMD_MKR1000 - #include - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED - #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED -#endif - -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ - defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined (ARDUINO_NANO_RP2040_CONNECT) - #include - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_MODULE - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED - #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION -#endif - -#if defined(ARDUINO_PORTENTA_H7_M7) - #include - #include - #include - #include - #include - #include - - #define BOARD_HAS_WIFI - #define BOARD_HAS_ETHERNET - #define BOARD_HAS_CATM1_NBIOT - #define BOARD_HAS_CELLULAR - #define BOARD_HAS_PORTENTA_CATM1_NBIOT_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 -#endif - -#if defined(ARDUINO_PORTENTA_C33) - #include - #include - #include - #include - #include - - #define BOARD_HAS_WIFI - #define BOARD_HAS_ETHERNET - #define BOARD_HAS_CELLULAR - #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 - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED -#endif - -#if defined(ARDUINO_OPTA) - #include - #include - #include - #include - - #define BOARD_HAS_WIFI - #define BOARD_HAS_ETHERNET - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED -#endif - -#if defined(ARDUINO_GIGA) - #include - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED -#endif - -#ifdef ARDUINO_SAMD_MKRGSM1400 - #include - #define BOARD_HAS_GSM - #define NETWORK_HARDWARE_ERROR GPRS_PING_ERROR - #define NETWORK_IDLE_STATUS GSM3_NetworkStatus_t::IDLE - #define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY -#endif - -#ifdef ARDUINO_SAMD_MKRNB1500 - #include - #define BOARD_HAS_NB - #define NETWORK_HARDWARE_ERROR - #define NETWORK_IDLE_STATUS NB_NetworkStatus_t::IDLE - #define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY -#endif - -#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) - #include - #define BOARD_HAS_LORA -#endif - -#if defined(ARDUINO_ARCH_ESP8266) - #include - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED - #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED -#endif - -#if defined(ARDUINO_ARCH_ESP32) - #include - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED - #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED -#endif - -#if defined(ARDUINO_UNOR4_WIFI) - #include - - #define BOARD_HAS_WIFI - #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD - #define NETWORK_IDLE_STATUS WL_IDLE_STATUS - #define NETWORK_CONNECTED WL_CONNECTED - #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION +#if defined(BOARD_HAS_WIFI) + #include "Arduino_WiFiConnectionHandler.h" #endif -#ifdef ARDUINO_EDGE_CONTROL - #include - #define BOARD_HAS_CATM1_NBIOT - #define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD - #define NETWORK_HARDWARE_ERROR +#if defined(BOARD_HAS_GSM) + #include "Arduino_GSMConnectionHandler.h" #endif -/****************************************************************************** - TYPEDEFS - ******************************************************************************/ - -enum class NetworkConnectionState : unsigned int { - INIT = 0, - CONNECTING = 1, - CONNECTED = 2, - DISCONNECTING = 3, - DISCONNECTED = 4, - CLOSED = 5, - ERROR = 6 -}; - -enum class NetworkConnectionEvent { - CONNECTED, - DISCONNECTED, - ERROR -}; - -enum class NetworkAdapter { - WIFI, - ETHERNET, - NB, - GSM, - LORA, - CATM1, - CELL -}; - -typedef void (*OnNetworkEventCallback)(); - -/****************************************************************************** - CONSTANTS - ******************************************************************************/ - -static unsigned int const CHECK_INTERVAL_TABLE[] = -{ - /* INIT */ 100, -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) - /* CONNECTING */ 4000, -#else - /* CONNECTING */ 500, +#if defined(BOARD_HAS_NB) + #include "Arduino_NBConnectionHandler.h" #endif - /* CONNECTED */ 10000, - /* DISCONNECTING */ 100, - /* DISCONNECTED */ 1000, - /* CLOSED */ 1000, - /* ERROR */ 1000 -}; - -/****************************************************************************** - CLASS DECLARATION - ******************************************************************************/ - -class ConnectionHandler { - public: - - ConnectionHandler(bool const keep_alive, NetworkAdapter interface); - - NetworkConnectionState check(); - - #if !defined(BOARD_HAS_LORA) - virtual unsigned long getTime() = 0; - virtual Client &getClient() = 0; - virtual UDP &getUDP() = 0; - #else - virtual int write(const uint8_t *buf, size_t size) = 0; - virtual int read() = 0; - virtual bool available() = 0; - #endif - - NetworkConnectionState getStatus() __attribute__((deprecated)) { - return _current_net_connection_state; - } - - NetworkAdapter getInterface() { - return _interface; - } - - void connect(); - void disconnect(); - - void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - void addConnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); - void addDisconnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); - void addErrorCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); - - protected: - - bool _keep_alive; - NetworkAdapter _interface; - - virtual NetworkConnectionState update_handleInit () = 0; - virtual NetworkConnectionState update_handleConnecting () = 0; - virtual NetworkConnectionState update_handleConnected () = 0; - virtual NetworkConnectionState update_handleDisconnecting() = 0; - virtual NetworkConnectionState update_handleDisconnected () = 0; - - - private: - - unsigned long _lastConnectionTickTime; - NetworkConnectionState _current_net_connection_state; - OnNetworkEventCallback _on_connect_event_callback = NULL, - _on_disconnect_event_callback = NULL, - _on_error_event_callback = NULL; -}; - -#if defined(BOARD_HAS_WIFI) - #include "Arduino_WiFiConnectionHandler.h" -#elif defined(BOARD_HAS_GSM) - #include "Arduino_GSMConnectionHandler.h" -#elif defined(BOARD_HAS_NB) - #include "Arduino_NBConnectionHandler.h" -#elif defined(BOARD_HAS_LORA) +#if defined(BOARD_HAS_LORA) #include "Arduino_LoRaConnectionHandler.h" #endif diff --git a/src/Arduino_ConnectionHandlerDefinitions.h b/src/Arduino_ConnectionHandlerDefinitions.h new file mode 100644 index 00000000..74212d3e --- /dev/null +++ b/src/Arduino_ConnectionHandlerDefinitions.h @@ -0,0 +1,185 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2019 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. +*/ + +#pragma once + +/****************************************************************************** + INCLUDES + ******************************************************************************/ + +#include + +#ifdef ARDUINO_SAMD_MKR1000 + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED + #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED +#endif + +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ + defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined (ARDUINO_NANO_RP2040_CONNECT) + + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_MODULE + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED + #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION +#endif + +#if defined(ARDUINO_PORTENTA_H7_M7) + #define BOARD_HAS_WIFI + #define BOARD_HAS_ETHERNET + #define BOARD_HAS_CATM1_NBIOT + #define BOARD_HAS_CELLULAR + #define BOARD_HAS_PORTENTA_CATM1_NBIOT_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 +#endif + +#if defined(ARDUINO_PORTENTA_C33) + #define BOARD_HAS_WIFI + #define BOARD_HAS_ETHERNET + #define BOARD_HAS_CELLULAR + #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) + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED +#endif + +#if defined(ARDUINO_OPTA) + #define BOARD_HAS_WIFI + #define BOARD_HAS_ETHERNET + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED +#endif + +#if defined(ARDUINO_GIGA) + + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED +#endif + +#ifdef ARDUINO_SAMD_MKRGSM1400 + #define BOARD_HAS_GSM + #define NETWORK_HARDWARE_ERROR GPRS_PING_ERROR + #define NETWORK_IDLE_STATUS GSM3_NetworkStatus_t::IDLE + #define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY +#endif + +#ifdef ARDUINO_SAMD_MKRNB1500 + #define BOARD_HAS_NB + #define NETWORK_HARDWARE_ERROR + #define NETWORK_IDLE_STATUS NB_NetworkStatus_t::IDLE + #define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY +#endif + +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) + #define BOARD_HAS_LORA +#endif + +#if defined(ARDUINO_ARCH_ESP8266) + + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED + #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED +#endif + +#if defined(ARDUINO_ARCH_ESP32) + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED + #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED +#endif + +#if defined(ARDUINO_UNOR4_WIFI) + + #define BOARD_HAS_WIFI + #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD + #define NETWORK_IDLE_STATUS WL_IDLE_STATUS + #define NETWORK_CONNECTED WL_CONNECTED + #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION +#endif + +#ifdef ARDUINO_EDGE_CONTROL + #define BOARD_HAS_CATM1_NBIOT + #define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD + #define NETWORK_HARDWARE_ERROR +#endif + +/****************************************************************************** + TYPEDEFS + ******************************************************************************/ + +enum class NetworkConnectionState : unsigned int { + INIT = 0, + CONNECTING = 1, + CONNECTED = 2, + DISCONNECTING = 3, + DISCONNECTED = 4, + CLOSED = 5, + ERROR = 6 +}; + +enum class NetworkConnectionEvent { + CONNECTED, + DISCONNECTED, + ERROR +}; + +enum class NetworkAdapter { + WIFI, + ETHERNET, + NB, + GSM, + LORA, + CATM1 +}; + +/****************************************************************************** + CONSTANTS + ******************************************************************************/ + +static unsigned int const CHECK_INTERVAL_TABLE[] = +{ + /* INIT */ 100, +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + /* CONNECTING */ 4000, +#else + /* CONNECTING */ 500, +#endif + /* CONNECTED */ 10000, + /* DISCONNECTING */ 100, + /* DISCONNECTED */ 1000, + /* CLOSED */ 1000, + /* ERROR */ 1000 +}; diff --git a/src/Arduino_ConnectionHandler.cpp b/src/Arduino_ConnectionHandlerInterface.cpp similarity index 99% rename from src/Arduino_ConnectionHandler.cpp rename to src/Arduino_ConnectionHandlerInterface.cpp index ab83fa08..ccfc4df9 100644 --- a/src/Arduino_ConnectionHandler.cpp +++ b/src/Arduino_ConnectionHandlerInterface.cpp @@ -19,7 +19,7 @@ INCLUDE ******************************************************************************/ -#include "Arduino_ConnectionHandler.h" +#include "Arduino_ConnectionHandlerInterface.h" /****************************************************************************** CONSTRUCTOR/DESTRUCTOR diff --git a/src/Arduino_ConnectionHandlerInterface.h b/src/Arduino_ConnectionHandlerInterface.h new file mode 100644 index 00000000..52a09bd5 --- /dev/null +++ b/src/Arduino_ConnectionHandlerInterface.h @@ -0,0 +1,99 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2019 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. +*/ + +#pragma once + +/****************************************************************************** + INCLUDES + ******************************************************************************/ + +#if !defined(__AVR__) +# include +#endif + +#include +#include +#include +#include "Arduino_ConnectionHandlerDefinitions.h" + +/****************************************************************************** + TYPEDEFS + ******************************************************************************/ + +typedef void (*OnNetworkEventCallback)(); + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class ConnectionHandler { + public: + + ConnectionHandler(bool const keep_alive, NetworkAdapter interface); + + + NetworkConnectionState check(); + + #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT) + virtual unsigned long getTime() = 0; + virtual Client &getClient() = 0; + virtual UDP &getUDP() = 0; + #endif + + #if defined(BOARD_HAS_LORA) + virtual int write(const uint8_t *buf, size_t size) = 0; + virtual int read() = 0; + virtual bool available() = 0; + #endif + + NetworkConnectionState getStatus() __attribute__((deprecated)) { + return _current_net_connection_state; + } + + NetworkAdapter getInterface() { + return _interface; + } + + void connect(); + void disconnect(); + + void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); + void addConnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); + void addDisconnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); + void addErrorCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); + + protected: + + bool _keep_alive; + NetworkAdapter _interface; + + virtual NetworkConnectionState update_handleInit () = 0; + virtual NetworkConnectionState update_handleConnecting () = 0; + virtual NetworkConnectionState update_handleConnected () = 0; + virtual NetworkConnectionState update_handleDisconnecting() = 0; + virtual NetworkConnectionState update_handleDisconnected () = 0; + + + private: + + unsigned long _lastConnectionTickTime; + NetworkConnectionState _current_net_connection_state; + OnNetworkEventCallback _on_connect_event_callback = NULL, + _on_disconnect_event_callback = NULL, + _on_error_event_callback = NULL; +}; +