diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index 0077db20..a2522834 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -51,7 +51,7 @@ void loop() { * object. */ - conMan.update(); + conMan.check(); } void onNetworkConnect(void *_arg) { diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 854f3841..55d2ae1a 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -137,8 +137,7 @@ typedef void (*OnNetworkEventCallback)(void * /* arg */); class ConnectionHandler { public: virtual void init() = 0; - virtual void check() = 0; - virtual void update() __attribute__((deprecated)) = 0; /* use 'check()' instead */ + virtual NetworkConnectionState check() = 0; #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) virtual unsigned long getTime() = 0; @@ -146,11 +145,17 @@ class ConnectionHandler { virtual UDP &getUDP() = 0; #endif - virtual NetworkConnectionState getStatus() { + #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 + + virtual NetworkConnectionState getStatus() __attribute__((deprecated)) { return netConnectionState; } - virtual void connect(); - virtual void disconnect(); + virtual void connect() = 0; + virtual void disconnect() = 0; void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); void addConnectCallback(OnNetworkEventCallback callback); void addDisconnectCallback(OnNetworkEventCallback callback); @@ -168,12 +173,14 @@ class ConnectionHandler { }; -#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) - #include "Arduino_TcpIpConnectionHandler.h" -#endif - -#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) - #include "Arduino_LPWANConnectionHandler.h" +#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) + #include "Arduino_LoRaConnectionHandler.h" #endif #endif /* CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index d514a38b..ceb14933 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -71,7 +71,7 @@ unsigned long GSMConnectionHandler::getTime() { return gsmAccess.getTime(); } -void GSMConnectionHandler::update() { +NetworkConnectionState GSMConnectionHandler::check() { unsigned long const now = millis(); int gsmAccessAlive; if (now - lastConnectionTickTime > connectionTickTimeInterval) { @@ -89,7 +89,7 @@ void GSMConnectionHandler::update() { if (networkStatus == GSM3_NetworkStatus_t::ERROR) { // NO FURTHER ACTION WILL FOLLOW THIS changeConnectionState(NetworkConnectionState::ERROR); - return; + return netConnectionState; } Debug.print(DBG_INFO, "Sending PING to outer space..."); int pingResult; @@ -98,11 +98,11 @@ void GSMConnectionHandler::update() { if (pingResult < 0) { Debug.print(DBG_ERROR, "PING failed"); Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); - return; + return netConnectionState; } else { Debug.print(DBG_INFO, "Connected to GPRS Network"); changeConnectionState(NetworkConnectionState::CONNECTED); - return; + return netConnectionState; } } break; @@ -111,7 +111,7 @@ void GSMConnectionHandler::update() { Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", gsmAccessAlive); if (gsmAccessAlive != 1) { changeConnectionState(NetworkConnectionState::DISCONNECTED); - return; + return netConnectionState; } Debug.print(DBG_VERBOSE, "Connected to Cellular Network"); } @@ -130,6 +130,8 @@ void GSMConnectionHandler::update() { } lastConnectionTickTime = now; } + + return netConnectionState; } /****************************************************************************** diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index 98adedc2..f4c18ad1 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -22,7 +22,7 @@ INCLUDE ******************************************************************************/ -#include "Arduino_TcpIpConnectionHandler.h" +#include "Arduino_ConnectionHandler.h" #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ @@ -31,16 +31,13 @@ CLASS DECLARATION ******************************************************************************/ -class GSMConnectionHandler : public TcpIpConnectionHandler { +class GSMConnectionHandler : public ConnectionHandler { public: GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true); virtual void init(); virtual unsigned long getTime(); - virtual void check() { - update(); - } - virtual void update() __attribute__((deprecated)); /* use 'check()' instead */ + virtual NetworkConnectionState check(); virtual Client &getClient() { return networkClient; }; @@ -77,6 +74,8 @@ class GSMConnectionHandler : public TcpIpConnectionHandler { }; +typedef GSMConnectionHandler TcpIpConnectionHandler; + #endif /* #ifdef BOARD_HAS_GSM */ #endif /* #ifndef GSM_CONNECTION_MANAGER_H_ */ diff --git a/src/Arduino_LPWANConnectionHandler.h b/src/Arduino_LPWANConnectionHandler.h deleted file mode 100644 index 879e96d2..00000000 --- a/src/Arduino_LPWANConnectionHandler.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - 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. -*/ - -#ifndef ARDUINO_LPWAN_CONNECTION_HANDLER_H_ -#define ARDUINO_LPWAN_CONNECTION_HANDLER_H_ - -/****************************************************************************** - INCLUDES - ******************************************************************************/ - -#include -#include - -/****************************************************************************** - CLASS DECLARATION - ******************************************************************************/ - -class LPWANConnectionHandler : public ConnectionHandler { - public: - virtual void init() = 0; - virtual void check() = 0; - virtual void update() __attribute__((deprecated)) = 0; /* use 'check()' instead */ - virtual unsigned long getTime() = 0; - - virtual int write(const uint8_t *buf, size_t size) = 0; - virtual int read() = 0; - virtual bool available() = 0; - - virtual void connect() = 0; - virtual void disconnect() = 0; - -}; - -#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) - #include "Arduino_LoRaConnectionHandler.h" -#endif - - -#endif /* LPWAN_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index e5dbf579..4919ce04 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -104,7 +104,7 @@ bool LoRaConnectionHandler::available() { return modem.available(); } -void LoRaConnectionHandler::update() { +NetworkConnectionState LoRaConnectionHandler::check() { unsigned long const now = millis(); int networkStatus = 0; @@ -121,6 +121,8 @@ void LoRaConnectionHandler::update() { case NetworkConnectionState::CLOSED: break; } } + + return netConnectionState; } /****************************************************************************** diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index 7cc2a5cb..ed885172 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -22,7 +22,7 @@ INCLUDE ******************************************************************************/ -#include "Arduino_LPWANConnectionHandler.h" +#include "Arduino_ConnectionHandler.h" typedef enum { LORA_ERROR_ACK_NOT_RECEIVED = -1, @@ -40,23 +40,20 @@ typedef enum { CLASS DECLARATION ******************************************************************************/ -class LoRaConnectionHandler : public LPWANConnectionHandler { +class LoRaConnectionHandler : public ConnectionHandler { public: LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = _lora_band::EU868, _lora_class = _lora_class::CLASS_A); - void init(); - unsigned long getTime(); - void check() { - update(); - } - void update() __attribute__((deprecated)); /* use 'check()' instead */ + virtual void init(); + virtual unsigned long getTime(); + virtual NetworkConnectionState check(); - int write(const uint8_t *buf, size_t size); - int read(); - bool available(); + virtual int write(const uint8_t *buf, size_t size); + virtual int read(); + virtual bool available(); - void disconnect(); - void connect(); + virtual void disconnect(); + virtual void connect(); private: @@ -89,4 +86,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { }; +typedef LoRaConnectionHandler LPWANConnectionHandler; + #endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_NBConnectionHandler.cpp b/src/Arduino_NBConnectionHandler.cpp index 75da631b..7e493617 100644 --- a/src/Arduino_NBConnectionHandler.cpp +++ b/src/Arduino_NBConnectionHandler.cpp @@ -78,7 +78,7 @@ unsigned long NBConnectionHandler::getTime() { return nbAccess.getTime(); } -void NBConnectionHandler::update() { +NetworkConnectionState NBConnectionHandler::check() { unsigned long const now = millis(); int nbAccessAlive; if (now - lastConnectionTickTime > connectionTickTimeInterval) { @@ -96,7 +96,7 @@ void NBConnectionHandler::update() { if (networkStatus == NB_NetworkStatus_t::ERROR) { // NO FURTHER ACTION WILL FOLLOW THIS changeConnectionState(NetworkConnectionState::ERROR); - return; + return netConnectionState; } Debug.print(DBG_INFO, "Sending PING to outer space..."); int pingResult; @@ -106,11 +106,11 @@ void NBConnectionHandler::update() { if (pingResult < 0) { Debug.print(DBG_ERROR, "PING failed"); Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); - return; + return netConnectionState; } else { Debug.print(DBG_INFO, "Connected to GPRS Network"); changeConnectionState(NetworkConnectionState::CONNECTED); - return; + return netConnectionState; } } break; @@ -119,7 +119,7 @@ void NBConnectionHandler::update() { Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nbAccessAlive); if (nbAccessAlive != 1) { changeConnectionState(NetworkConnectionState::DISCONNECTED); - return; + return netConnectionState; } Debug.print(DBG_VERBOSE, "Connected to Cellular Network"); } @@ -138,6 +138,8 @@ void NBConnectionHandler::update() { } lastConnectionTickTime = now; } + + return netConnectionState; } /****************************************************************************** diff --git a/src/Arduino_NBConnectionHandler.h b/src/Arduino_NBConnectionHandler.h index 680fb583..dfd1dae0 100644 --- a/src/Arduino_NBConnectionHandler.h +++ b/src/Arduino_NBConnectionHandler.h @@ -30,7 +30,7 @@ CLASS DECLARATION ******************************************************************************/ -class NBConnectionHandler : public TcpIpConnectionHandler { +class NBConnectionHandler : public ConnectionHandler { public: NBConnectionHandler(const char *pin, const bool keepAlive = true); NBConnectionHandler(const char *pin, const char *apn, const bool keepAlive = true); @@ -38,10 +38,7 @@ class NBConnectionHandler : public TcpIpConnectionHandler { virtual void init(); virtual unsigned long getTime(); - virtual void check() { - update(); - } - virtual void update() __attribute__((deprecated)); /* use 'update()' instead */ + virtual NetworkConnectionState check(); virtual Client &getClient() { return networkClient; }; @@ -78,6 +75,8 @@ class NBConnectionHandler : public TcpIpConnectionHandler { }; +typedef NBConnectionHandler TcpIpConnectionHandler; + #endif /* #ifdef BOARD_HAS_NB */ #endif /* #ifndef NB_CONNECTION_MANAGER_H_ */ diff --git a/src/Arduino_TcpIpConnectionHandler.h b/src/Arduino_TcpIpConnectionHandler.h deleted file mode 100644 index 387940d3..00000000 --- a/src/Arduino_TcpIpConnectionHandler.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - 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. -*/ - -#ifndef ARDUINO_TCPIP_CONNECTION_HANDLER_H_ -#define ARDUINO_TCPIP_CONNECTION_HANDLER_H_ - -/****************************************************************************** - INCLUDES - ******************************************************************************/ - -#include -#include - -#include -#include - -/****************************************************************************** - CLASS DECLARATION - ******************************************************************************/ - -class TcpIpConnectionHandler : public ConnectionHandler { - public: - virtual void init() = 0; - virtual void check() = 0; - virtual void update() __attribute__((deprecated)) = 0; /* use 'update()' instead */ - virtual unsigned long getTime() = 0; - virtual Client &getClient() = 0; - virtual UDP &getUDP() = 0; - - virtual void connect() = 0; - virtual void disconnect() = 0; - -}; - -#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" -#endif - -#endif /* TCPIP_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index f7b948c4..7b5a5efe 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -50,7 +50,7 @@ unsigned long WiFiConnectionHandler::getTime() { #endif } -void WiFiConnectionHandler::update() { +NetworkConnectionState WiFiConnectionHandler::check() { unsigned long const now = millis(); if((now - lastConnectionTickTime) > connectionTickTimeInterval) @@ -68,6 +68,8 @@ void WiFiConnectionHandler::update() { case NetworkConnectionState::CLOSED: break; } } + + return netConnectionState; } /****************************************************************************** diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index 7201253f..08b40132 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -22,7 +22,7 @@ INCLUDE ******************************************************************************/ -#include "Arduino_TcpIpConnectionHandler.h" +#include "Arduino_ConnectionHandler.h" #ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ @@ -30,16 +30,13 @@ CLASS DECLARATION ******************************************************************************/ -class WiFiConnectionHandler : public TcpIpConnectionHandler { +class WiFiConnectionHandler : public ConnectionHandler { public: WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive = true); virtual void init(); virtual unsigned long getTime(); - virtual void check() { - update(); - } - virtual void update() __attribute__((deprecated)); /* use 'update()' instead */ + virtual NetworkConnectionState check(); virtual Client &getClient() { return wifiClient; }; @@ -76,6 +73,8 @@ class WiFiConnectionHandler : public TcpIpConnectionHandler { }; +typedef WiFiConnectionHandler TcpIpConnectionHandler; + #endif /* #ifdef BOARD_HAS_WIFI */ #endif /* ARDUINO_WIFI_CONNECTION_HANDLER_H_ */