diff --git a/library.properties b/library.properties index 333b6417..035be2ad 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Originally part of ArduinoIoTCloud category=Communication url=https://github.com/arduino-libraries/Arduino_ConnectionHandler architectures=samd,esp32,esp8266 -depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB +depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN diff --git a/src/Arduino_ConnectionHandler.cpp b/src/Arduino_ConnectionHandler.cpp new file mode 100644 index 00000000..cfc6b707 --- /dev/null +++ b/src/Arduino_ConnectionHandler.cpp @@ -0,0 +1,55 @@ +/* + 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. +*/ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "Arduino_ConnectionHandler.h" + +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +void ConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) { + switch (event) { + case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break; + case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break; + case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break; + case NetworkConnectionEvent::INIT: ; break; + case NetworkConnectionEvent::CONNECTING: ; break; + case NetworkConnectionEvent::DISCONNECTING: ; break; + case NetworkConnectionEvent::CLOSED: ; break; + } +} + +void ConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { + _on_connect_event_callback = callback; +} +void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { + _on_disconnect_event_callback = callback; +} +void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { + _on_error_event_callback = callback; +} + +void ConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { + if (callback) { + (*callback)(callback_arg); + } +} + diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 6db47859..352c495a 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -22,9 +22,6 @@ INCLUDES ******************************************************************************/ -#include -#include - #include /****************************************************************************** @@ -57,30 +54,27 @@ class ConnectionHandler { virtual void init() = 0; virtual void check() = 0; virtual void update() = 0; - virtual unsigned long getTime() = 0; - virtual Client &getClient(); - virtual UDP &getUDP(); virtual NetworkConnectionState getStatus() { return netConnectionState; } virtual void connect(); virtual void disconnect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - - private: - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; + void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); + void addConnectCallback(OnNetworkEventCallback callback); + void addDisconnectCallback(OnNetworkEventCallback callback); + void addErrorCallback(OnNetworkEventCallback callback); protected: + OnNetworkEventCallback _on_connect_event_callback = NULL, + _on_disconnect_event_callback = NULL, + _on_error_event_callback = NULL; unsigned long lastValidTimestamp = 0; /* UNUSED */ NetworkConnectionState netConnectionState = NetworkConnectionState::DISCONNECTED; + static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); + }; #ifdef ARDUINO_SAMD_MKR1000 @@ -121,6 +115,11 @@ class ConnectionHandler { #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_ESP8266_ESP12) \ || defined(ESP8266) \ || defined(ESP8266_ESP01) \ @@ -163,12 +162,13 @@ class ConnectionHandler { #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED #endif -#ifdef 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" + +#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" #endif #endif /* CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index 95c4403e..acadcf0e 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -49,10 +49,7 @@ GSMConnectionHandler::GSMConnectionHandler(const char *pin, const char *apn, con pass(pass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(_keepAlive) { } /****************************************************************************** @@ -71,34 +68,6 @@ void GSMConnectionHandler::init() { } } -void GSMConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) { - switch (event) { - case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break; - case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break; - case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break; - case NetworkConnectionEvent::INIT: ; break; - case NetworkConnectionEvent::CONNECTING: ; break; - case NetworkConnectionEvent::DISCONNECTING: ; break; - case NetworkConnectionEvent::CLOSED: ; break; - } -} - -void GSMConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { - _on_connect_event_callback = callback; -} -void GSMConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { - _on_disconnect_event_callback = callback; -} -void GSMConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { - _on_error_event_callback = callback; -} - -void GSMConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { - if (callback) { - (*callback)(callback_arg); - } -} - unsigned long GSMConnectionHandler::getTime() { return gsmAccess.getTime(); } diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index 260fc0ca..e8bbf240 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -22,7 +22,8 @@ INCLUDE ******************************************************************************/ -#include "Arduino_ConnectionHandler.h" +#include "Arduino_TcpIpConnectionHandler.h" + #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ @@ -30,7 +31,7 @@ CLASS DECLARATION ******************************************************************************/ -class GSMConnectionHandler : public ConnectionHandler { +class GSMConnectionHandler : public TcpIpConnectionHandler { public: GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true); @@ -55,11 +56,6 @@ class GSMConnectionHandler : public ConnectionHandler { virtual void disconnect(); virtual void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - private: void changeConnectionState(NetworkConnectionState _newState); @@ -79,11 +75,6 @@ class GSMConnectionHandler : public ConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); }; #endif /* #ifdef BOARD_HAS_GSM */ diff --git a/src/Arduino_LPWANConnectionHandler.h b/src/Arduino_LPWANConnectionHandler.h new file mode 100644 index 00000000..7f16d8d8 --- /dev/null +++ b/src/Arduino_LPWANConnectionHandler.h @@ -0,0 +1,53 @@ +/* + 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() = 0; + 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 new file mode 100644 index 00000000..e5dbf579 --- /dev/null +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -0,0 +1,216 @@ +/* + 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. +*/ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) /* Only compile if the board has LoRa */ + +#include "Arduino_LoRaConnectionHandler.h" + +/****************************************************************************** + CONSTANTS + ******************************************************************************/ + +static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ + +/****************************************************************************** + CTOR/DTOR + ******************************************************************************/ + +LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band band, _lora_class deviceClass) : + appeui(_appeui), + appkey(_appkey), + band(band), + deviceClass(deviceClass), + lastConnectionTickTime(millis()), + connectionTickTimeInterval(CHECK_INTERVAL_IDLE), + keepAlive(false) { + netConnectionState = NetworkConnectionState::INIT; +} + +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +void LoRaConnectionHandler::init() { +} + +unsigned long LoRaConnectionHandler::getTime() { + return 0; +} + +int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { + int err; + modem.beginPacket(); + modem.write(buf, size); + err = modem.endPacket(true); + if (err != size) { + switch (err) { + case LoRaCommunicationError::LORA_ERROR_ACK_NOT_RECEIVED: { + Debug.print(DBG_ERROR, "Message ack was not received, the message could not be delivered"); + } break; + case LoRaCommunicationError::LORA_ERROR_GENERIC: { + Debug.print(DBG_ERROR, "LoRa generic error (LORA_ERROR)"); + } break; + case LoRaCommunicationError::LORA_ERROR_WRONG_PARAM: { + Debug.print(DBG_ERROR, "LoRa malformed param error (LORA_ERROR_PARAM"); + } break; + case LoRaCommunicationError::LORA_ERROR_COMMUNICATION_BUSY: { + Debug.print(DBG_ERROR, "LoRa chip is busy (LORA_ERROR_BUSY)"); + } break; + case LoRaCommunicationError::LORA_ERROR_MESSAGE_OVERFLOW: { + Debug.print(DBG_ERROR, "LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); + } break; + case LoRaCommunicationError::LORA_ERROR_NO_NETWORK_AVAILABLE: { + Debug.print(DBG_ERROR, "LoRa no network error (LORA_ERROR_NO_NETWORK)"); + } break; + case LoRaCommunicationError::LORA_ERROR_RX_PACKET: { + Debug.print(DBG_ERROR, "LoRa rx error (LORA_ERROR_RX)"); + } break; + case LoRaCommunicationError::LORA_ERROR_REASON_UNKNOWN: { + Debug.print(DBG_ERROR, "LoRa unknown error (LORA_ERROR_UNKNOWN)"); + } break; + case LoRaCommunicationError::LORA_ERROR_MAX_PACKET_SIZE: { + Debug.print(DBG_ERROR, "Message length is bigger than max LoRa packet!"); + } break; + } + } else { + Debug.print(DBG_INFO, "Message sent correctly!"); + } + return err; +} + +int LoRaConnectionHandler::read() { + return modem.read(); +} + +bool LoRaConnectionHandler::available() { + return modem.available(); +} + +void LoRaConnectionHandler::update() { + + unsigned long const now = millis(); + int networkStatus = 0; + if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ + + lastConnectionTickTime = now; + switch (netConnectionState) { + case NetworkConnectionState::INIT: netConnectionState = update_handleInit(); break; + case NetworkConnectionState::CONNECTING: netConnectionState = update_handleConnecting(); break; + case NetworkConnectionState::CONNECTED: netConnectionState = update_handleConnected(); break; + case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break; + case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected(); break; + case NetworkConnectionState::ERROR: break; + case NetworkConnectionState::CLOSED: break; + } + } +} + +/****************************************************************************** + PRIVATE MEMBER FUNCTIONS + ******************************************************************************/ + +NetworkConnectionState LoRaConnectionHandler::update_handleInit() { + Debug.print(DBG_VERBOSE, "::INIT"); + if (!modem.begin(band)) { + Debug.print(DBG_VERBOSE, "Failed to start module"); + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); + }; + //A delay is required between modem.begin(band) and modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized before the connection attempt + delay(100); + modem.configureClass(deviceClass); + delay(100); + Debug.print(DBG_INFO, "Connecting to the network"); + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; + return NetworkConnectionState::CONNECTING; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleConnecting() { + Debug.print(DBG_VERBOSE, "::CONNECTING"); + bool networkStatus = modem.joinOTAA(appeui, appkey); + if (networkStatus != true) { + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); + return NetworkConnectionState::ERROR; + } + + Debug.print(DBG_INFO, "Connected to the network"); + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; + execNetworkEventCallback(_on_connect_event_callback, 0); + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleConnected() { + + bool networkStatus = modem.connected(); + Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus); + if (networkStatus != true) { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + + Debug.print(DBG_ERROR, "Connection to the network lost."); + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); + } + connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; + return NetworkConnectionState::DISCONNECTED; + } + Debug.print(DBG_VERBOSE, "Connected to the network"); + + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + + Debug.print(DBG_ERROR, "Connection to the network lost."); + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); + } + connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; + return NetworkConnectionState::DISCONNECTED; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleDisconnected() { + if (keepAlive) { + Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); + connectionTickTimeInterval = CHECK_INTERVAL_INIT; + return NetworkConnectionState::INIT; + } else { + Debug.print(DBG_VERBOSE, "Connection to the network terminated"); + return NetworkConnectionState::CLOSED; + } + +} + +void LoRaConnectionHandler::connect() { + if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { + return; + } + keepAlive = true; + connectionTickTimeInterval = CHECK_INTERVAL_INIT; + netConnectionState = NetworkConnectionState::INIT; + +} +void LoRaConnectionHandler::disconnect() { + // do nothing + return; +} +#endif diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h new file mode 100644 index 00000000..ecb31e89 --- /dev/null +++ b/src/Arduino_LoRaConnectionHandler.h @@ -0,0 +1,92 @@ +/* + 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_LORA_CONNECTION_HANDLER_H_ +#define ARDUINO_LORA_CONNECTION_HANDLER_H_ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "Arduino_LPWANConnectionHandler.h" + +typedef enum { + LORA_ERROR_ACK_NOT_RECEIVED = -1, + LORA_ERROR_GENERIC = -2, + LORA_ERROR_WRONG_PARAM = -3, + LORA_ERROR_COMMUNICATION_BUSY = -4, + LORA_ERROR_MESSAGE_OVERFLOW = -5, + LORA_ERROR_NO_NETWORK_AVAILABLE = -6, + LORA_ERROR_RX_PACKET = -7, + LORA_ERROR_REASON_UNKNOWN = -8, + LORA_ERROR_MAX_PACKET_SIZE = -20 +} LoRaCommunicationError; + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class LoRaConnectionHandler : public LPWANConnectionHandler { + 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(); + + int write(const uint8_t *buf, size_t size); + int read(); + bool available(); + + void disconnect(); + void connect(); + + private: + + const int CHECK_INTERVAL_IDLE = 100; + const int CHECK_INTERVAL_INIT = 100; + const int CHECK_INTERVAL_CONNECTING = 500; + const int CHECK_INTERVAL_CONNECTED = 10000; + const int CHECK_INTERVAL_RETRYING = 5000; + const int CHECK_INTERVAL_DISCONNECTING = 500; + const int CHECK_INTERVAL_DISCONNECTED = 1000; + const int CHECK_INTERVAL_ERROR = 500; + + LoRaModem modem; + const char *appeui, *appkey; + _lora_band band; + _lora_class deviceClass; + unsigned long lastConnectionTickTime; + + int connectionTickTimeInterval; + + bool keepAlive; + + NetworkConnectionState update_handleInit(); + NetworkConnectionState update_handleConnecting(); + NetworkConnectionState update_handleConnected(); + + NetworkConnectionState update_handleDisconnecting(); + NetworkConnectionState update_handleDisconnected(); + + +}; + +#endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_NBConnectionHandler.cpp b/src/Arduino_NBConnectionHandler.cpp index e24045a4..2c46be46 100644 --- a/src/Arduino_NBConnectionHandler.cpp +++ b/src/Arduino_NBConnectionHandler.cpp @@ -56,10 +56,7 @@ NBConnectionHandler::NBConnectionHandler(const char *pin, const char *apn, const pass(pass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(_keepAlive) { } /****************************************************************************** @@ -78,34 +75,6 @@ void NBConnectionHandler::init() { } } -void NBConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) { - switch (event) { - case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break; - case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break; - case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break; - case NetworkConnectionEvent::INIT: ; break; - case NetworkConnectionEvent::CONNECTING: ; break; - case NetworkConnectionEvent::DISCONNECTING: ; break; - case NetworkConnectionEvent::CLOSED: ; break; - } -} - -void NBConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { - _on_connect_event_callback = callback; -} -void NBConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { - _on_disconnect_event_callback = callback; -} -void NBConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { - _on_error_event_callback = callback; -} - -void NBConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { - if (callback) { - (*callback)(callback_arg); - } -} - unsigned long NBConnectionHandler::getTime() { return nbAccess.getTime(); } diff --git a/src/Arduino_NBConnectionHandler.h b/src/Arduino_NBConnectionHandler.h index 02a733d3..ab3822dc 100644 --- a/src/Arduino_NBConnectionHandler.h +++ b/src/Arduino_NBConnectionHandler.h @@ -30,7 +30,7 @@ CLASS DECLARATION ******************************************************************************/ -class NBConnectionHandler : public ConnectionHandler { +class NBConnectionHandler : public TcpIpConnectionHandler { public: NBConnectionHandler(const char *pin, const bool keepAlive = true); NBConnectionHandler(const char *pin, const char *apn, const bool keepAlive = true); @@ -57,11 +57,6 @@ class NBConnectionHandler : public ConnectionHandler { virtual void disconnect(); virtual void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - private: void changeConnectionState(NetworkConnectionState _newState); @@ -81,11 +76,6 @@ class NBConnectionHandler : public ConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); }; #endif /* #ifdef BOARD_HAS_NB */ diff --git a/src/Arduino_TcpIpConnectionHandler.h b/src/Arduino_TcpIpConnectionHandler.h new file mode 100644 index 00000000..018f46f9 --- /dev/null +++ b/src/Arduino_TcpIpConnectionHandler.h @@ -0,0 +1,57 @@ +/* + 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() = 0; + virtual unsigned long getTime() = 0; + virtual Client &getClient() = 0; + virtual UDP &getUDP() = 0; + + virtual void connect() = 0; + virtual void disconnect() = 0; + +}; + +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT) + #include "Arduino_WiFiConnectionHandler.h" +#elif defined(ARDUINO_SAMD_MKRGSM1400) + #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 4f60b862..f7b948c4 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -32,10 +32,7 @@ WiFiConnectionHandler::WiFiConnectionHandler(const char *_ssid, const char *_pas pass(_pass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(_keepAlive) { } /****************************************************************************** @@ -45,35 +42,6 @@ WiFiConnectionHandler::WiFiConnectionHandler(const char *_ssid, const char *_pas void WiFiConnectionHandler::init() { } -// INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED, CLOSED, ERROR -void WiFiConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) { - switch (event) { - case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break; - case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break; - case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break; - case NetworkConnectionEvent::INIT: ; break; - case NetworkConnectionEvent::CONNECTING: ; break; - case NetworkConnectionEvent::DISCONNECTING: ; break; - case NetworkConnectionEvent::CLOSED: ; break; - } -} - -void WiFiConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { - _on_connect_event_callback = callback; -} -void WiFiConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { - _on_disconnect_event_callback = callback; -} -void WiFiConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { - _on_error_event_callback = callback; -} - -void WiFiConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { - if (callback) { - (*callback)(callback_arg); - } -} - unsigned long WiFiConnectionHandler::getTime() { #if !defined(BOARD_ESP8266) return WiFi.getTime(); diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index 6b117029..37d9ee38 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -22,7 +22,7 @@ INCLUDE ******************************************************************************/ -#include "Arduino_ConnectionHandler.h" +#include "Arduino_TcpIpConnectionHandler.h" #ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ @@ -30,7 +30,7 @@ CLASS DECLARATION ******************************************************************************/ -class WiFiConnectionHandler : public ConnectionHandler { +class WiFiConnectionHandler : public TcpIpConnectionHandler { public: WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive = true); @@ -49,10 +49,6 @@ class WiFiConnectionHandler : public ConnectionHandler { virtual void disconnect(); virtual void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); WiFiUDP udp; private: @@ -71,12 +67,6 @@ class WiFiConnectionHandler : public ConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); - NetworkConnectionState update_handleInit (); NetworkConnectionState update_handleConnecting (); NetworkConnectionState update_handleConnected ();