diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 6e7fc53e..a3de50dc 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest env: - LIBRARIES: Arduino_DebugUtils WiFi101 WiFiNINA MKRGSM MKRNB + LIBRARIES: Arduino_DebugUtils WiFi101 WiFiNINA MKRGSM MKRNB MKRWAN strategy: matrix: fqbn: [ @@ -14,6 +14,8 @@ jobs: "arduino:samd:nano_33_iot", "arduino:samd:mkrgsm1400", "arduino:samd:mkrnb1500", + "arduino:samd:mkrwan1300", + "arduino:samd:mkrwan1310", '"esp8266:esp8266:huzzah" "https://arduino.esp8266.com/stable/package_esp8266com_index.json"' ] diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index a2522834..2246fc10 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -26,6 +26,8 @@ WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS); GSMConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS); #elif defined(BOARD_HAS_NB) NBConnectionHandler conMan(SECRET_PIN); +#elif defined(BOARD_HAS_LORA) +LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); #endif void setup() { @@ -35,10 +37,9 @@ void setup() { setDebugMessageLevel(DBG_INFO); - /* Register a function to be called upon connection to a network */ - conMan.addConnectCallback(onNetworkConnect); - /* Register a function to be called upon disconnection from a network */ - conMan.addDisconnectCallback(onNetworkDisconnect); + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); } void loop() { @@ -54,10 +55,14 @@ void loop() { conMan.check(); } -void onNetworkConnect(void *_arg) { +void onNetworkConnect() { Serial.println(">>>> CONNECTED to network"); } -void onNetworkDisconnect(void *_arg) { +void onNetworkDisconnect() { Serial.println(">>>> DISCONNECTED from network"); } + +void onNetworkError() { + Serial.println(">>>> ERROR"); +} diff --git a/examples/ConnectionHandlerDemo/arduino_secrets.h b/examples/ConnectionHandlerDemo/arduino_secrets.h index 2cc38da9..0a2a2fe6 100644 --- a/examples/ConnectionHandlerDemo/arduino_secrets.h +++ b/examples/ConnectionHandlerDemo/arduino_secrets.h @@ -4,4 +4,7 @@ const char SECRET_PASS[] = "NETWORK PASSWORD"; const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS"; const char SECRET_PIN[] = "0000"; const char SECRET_GSM_USER[] = "GSM USERNAME"; -const char SECRET_GSM_PASS[] = "GSM PASSWORD"; \ No newline at end of file +const char SECRET_GSM_PASS[] = "GSM PASSWORD"; + +const char SECRET_APP_EUI[] = "APP_EUI"; +const char SECRET_APP_KEY[] = "APP_KEY"; diff --git a/keywords.txt b/keywords.txt index 2e63dae4..8d9e9434 100644 --- a/keywords.txt +++ b/keywords.txt @@ -5,19 +5,24 @@ #################################################### # Datatypes (KEYWORD1) #################################################### -Client KEYWORD1 ConnectionHandler KEYWORD1 WiFiConnectionHandler KEYWORD1 GSMConnectionHandler KEYWORD1 NBConnectionHandler KEYWORD1 -EthernetConnectionHandler KEYWORD1 +LoRaConnectionHandler KEYWORD1 #################################################### # Methods and Functions (KEYWORD2) #################################################### ConnectionHandler KEYWORD2 -begin KEYWORD2 +check KEYWORD2 +connect KEYWORD2 +disconnect KEYWORD2 +addCallback KEYWORD2 +getTime KEYWORD2 +getClient KEYWORD2 +getUDP KEYWORD2 #################################################### # Constants (LITERAL1) diff --git a/src/Arduino_ConnectionHandler.cpp b/src/Arduino_ConnectionHandler.cpp index cfc6b707..51f63fc8 100644 --- a/src/Arduino_ConnectionHandler.cpp +++ b/src/Arduino_ConnectionHandler.cpp @@ -21,19 +21,95 @@ #include "Arduino_ConnectionHandler.h" +/****************************************************************************** + CONSTRUCTOR/DESTRUCTOR + ******************************************************************************/ + +ConnectionHandler::ConnectionHandler(bool const keep_alive) +: _keep_alive{keep_alive} +, _current_net_connection_state{NetworkConnectionState::INIT} +, _lastConnectionTickTime{millis()} +{ + +} + /****************************************************************************** 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; +NetworkConnectionState ConnectionHandler::check() +{ + unsigned long const now = millis(); + unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast(_current_net_connection_state)]; + + if((now - _lastConnectionTickTime) > connectionTickTimeInterval) + { + _lastConnectionTickTime = now; + NetworkConnectionState next_net_connection_state = _current_net_connection_state; + + /* While the state machine is implemented here, the concrete implementation of the + * states is done in the derived connection handlers. + */ + switch (_current_net_connection_state) + { + case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break; + case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break; + case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break; + case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting(); break; + case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break; + case NetworkConnectionState::ERROR: break; + case NetworkConnectionState::CLOSED: break; + } + + /* Here we are determining whether a state transition from one state to the next has + * occurred - and if it has, we call eventually registered callbacks. + */ + if(next_net_connection_state != _current_net_connection_state) + { + /* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */ + if(next_net_connection_state == NetworkConnectionState::CONNECTED) + { + if(_on_connect_event_callback) _on_connect_event_callback(); + } + if(next_net_connection_state == NetworkConnectionState::DISCONNECTED) + { + if(_on_disconnect_event_callback) _on_disconnect_event_callback(); + } + if(next_net_connection_state == NetworkConnectionState::ERROR) + { + if(_on_error_event_callback) _on_error_event_callback(); + } + + /* Assign new state to the member variable holding the state */ + _current_net_connection_state = next_net_connection_state; + } + } + + return _current_net_connection_state; +} + +void ConnectionHandler::connect() +{ + if (_current_net_connection_state != NetworkConnectionState::INIT && _current_net_connection_state != NetworkConnectionState::CONNECTING) + { + _keep_alive = true; + _current_net_connection_state = NetworkConnectionState::INIT; + } +} + +void ConnectionHandler::disconnect() +{ + _keep_alive = false; + _current_net_connection_state = NetworkConnectionState::DISCONNECTING; +} + +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; } } @@ -46,10 +122,3 @@ void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback 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 55d2ae1a..64520189 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -113,22 +113,38 @@ TYPEDEFS ******************************************************************************/ -enum class NetworkConnectionState { - INIT, - CONNECTING, +enum class NetworkConnectionState : unsigned int { + INIT = 0, + CONNECTING = 1, + CONNECTED = 2, + DISCONNECTING = 3, + DISCONNECTED = 4, + CLOSED = 5, + ERROR = 6 +}; + +enum class NetworkConnectionEvent { CONNECTED, - GETTIME, - DISCONNECTING, DISCONNECTED, - CLOSED, ERROR }; -enum class NetworkConnectionEvent { - INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED, CLOSED, ERROR -}; +typedef void (*OnNetworkEventCallback)(); -typedef void (*OnNetworkEventCallback)(void * /* arg */); +/****************************************************************************** + CONSTANTS + ******************************************************************************/ + +static unsigned int const CHECK_INTERVAL_TABLE[] = +{ + /* INIT */ 100, + /* CONNECTING */ 500, + /* CONNECTED */ 10000, + /* DISCONNECTING */ 100, + /* DISCONNECTED */ 1000, + /* CLOSED */ 1000, + /* ERROR */ 1000 +}; /****************************************************************************** CLASS DECLARATION @@ -136,8 +152,11 @@ typedef void (*OnNetworkEventCallback)(void * /* arg */); class ConnectionHandler { public: - virtual void init() = 0; - virtual NetworkConnectionState check() = 0; + + ConnectionHandler(bool const keep_alive); + + + NetworkConnectionState check(); #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) virtual unsigned long getTime() = 0; @@ -151,26 +170,36 @@ class ConnectionHandler { virtual bool available() = 0; #endif - virtual NetworkConnectionState getStatus() __attribute__((deprecated)) { - return netConnectionState; + NetworkConnectionState getStatus() __attribute__((deprecated)) { + return _current_net_connection_state; } - virtual void connect() = 0; - virtual void disconnect() = 0; + + void connect(); + void disconnect(); + void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - void addConnectCallback(OnNetworkEventCallback callback); - void addDisconnectCallback(OnNetworkEventCallback callback); - void addErrorCallback(OnNetworkEventCallback callback); + void addConnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); + void addDisconnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); + void addErrorCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); 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::INIT; + bool _keep_alive; - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); + 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) diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index ceb14933..951038bd 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -19,15 +19,6 @@ INCLUDE ******************************************************************************/ -/* - static int const DBG_NONE = -1; - static int const DBG_ERROR = 0; - static int const DBG_WARNING = 1; - static int const DBG_INFO = 2; - static int const DBG_DEBUG = 3; - static int const DBG_VERBOSE = 4; -*/ - #include "Arduino_GSMConnectionHandler.h" #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ @@ -36,169 +27,102 @@ CONSTANTS ******************************************************************************/ -static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; +static int const GSM_TIMEOUT = 30000; /****************************************************************************** CTOR/DTOR ******************************************************************************/ -GSMConnectionHandler::GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, bool _keepAlive) : - pin(pin), - apn(apn), - login(login), - pass(pass), - lastConnectionTickTime(millis()), - connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive) { +GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive) +: ConnectionHandler{keep_alive} +, _pin(pin) +, _apn(apn) +, _login(login) +, _pass(pass) +{ + } /****************************************************************************** PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -void GSMConnectionHandler::init() { - char msgBuffer[120]; - if (gsmAccess.begin(pin) == GSM_READY) { - Debug.print(DBG_INFO, "SIM card ok"); - gsmAccess.setTimeout(CHECK_INTERVAL_RETRYING); - changeConnectionState(NetworkConnectionState::CONNECTING); - } else { - Debug.print(DBG_ERROR, "SIM not present or wrong PIN"); - } -} - -unsigned long GSMConnectionHandler::getTime() { - return gsmAccess.getTime(); -} - -NetworkConnectionState GSMConnectionHandler::check() { - unsigned long const now = millis(); - int gsmAccessAlive; - if (now - lastConnectionTickTime > connectionTickTimeInterval) { - switch (netConnectionState) { - case NetworkConnectionState::INIT: { - init(); - } - break; - - case NetworkConnectionState::CONNECTING: { - // NOTE: Blocking Call when 4th parameter == true - GSM3_NetworkStatus_t networkStatus; - networkStatus = gprs.attachGPRS(apn, login, pass, true); - Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", networkStatus); - if (networkStatus == GSM3_NetworkStatus_t::ERROR) { - // NO FURTHER ACTION WILL FOLLOW THIS - changeConnectionState(NetworkConnectionState::ERROR); - return netConnectionState; - } - Debug.print(DBG_INFO, "Sending PING to outer space..."); - int pingResult; - pingResult = gprs.ping("time.arduino.cc"); - Debug.print(DBG_INFO, "GSM.ping(): %d", pingResult); - if (pingResult < 0) { - Debug.print(DBG_ERROR, "PING failed"); - Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); - return netConnectionState; - } else { - Debug.print(DBG_INFO, "Connected to GPRS Network"); - changeConnectionState(NetworkConnectionState::CONNECTED); - return netConnectionState; - } - } - break; - case NetworkConnectionState::CONNECTED: { - gsmAccessAlive = gsmAccess.isAccessAlive(); - Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", gsmAccessAlive); - if (gsmAccessAlive != 1) { - changeConnectionState(NetworkConnectionState::DISCONNECTED); - return netConnectionState; - } - Debug.print(DBG_VERBOSE, "Connected to Cellular Network"); - } - break; - case NetworkConnectionState::DISCONNECTED: { - //gprs.detachGPRS(); - if (keepAlive) { - Debug.print(DBG_VERBOSE, "keep alive > INIT"); - changeConnectionState(NetworkConnectionState::INIT); - } else { - changeConnectionState(NetworkConnectionState::CLOSED); - } - //changeConnectionState(NetworkConnectionState::CONNECTING); - } - break; - } - lastConnectionTickTime = now; - } - - return netConnectionState; +unsigned long GSMConnectionHandler::getTime() +{ + return _gsm.getTime(); } /****************************************************************************** - PRIVATE MEMBER FUNCTIONS + PROTECTED MEMBER FUNCTIONS ******************************************************************************/ -void GSMConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { - int newInterval = CHECK_INTERVAL_IDLE; - switch (_newState) { - case NetworkConnectionState::INIT: { - newInterval = CHECK_INTERVAL_INIT; - } - break; - case NetworkConnectionState::CONNECTING: { - Debug.print(DBG_INFO, "Connecting to Cellular Network"); - newInterval = CHECK_INTERVAL_CONNECTING; - } - break; - case NetworkConnectionState::CONNECTED: { - execNetworkEventCallback(_on_connect_event_callback, 0); - newInterval = CHECK_INTERVAL_CONNECTED; - } - break; - case NetworkConnectionState::GETTIME: { - } - break; - case NetworkConnectionState::DISCONNECTING: { - Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network"); - gsmAccess.shutdown(); - } - case NetworkConnectionState::DISCONNECTED: { - if (netConnectionState == NetworkConnectionState::CONNECTED) { - execNetworkEventCallback(_on_disconnect_event_callback, 0); - Debug.print(DBG_ERROR, "Disconnected from Cellular Network"); - Debug.print(DBG_ERROR, "Attempting reconnection"); - if (keepAlive) { - Debug.print(DBG_ERROR, "Attempting reconnection"); - } - } - newInterval = CHECK_INTERVAL_DISCONNECTED; - } - break; - case NetworkConnectionState::ERROR: { - execNetworkEventCallback(_on_error_event_callback, 0); - Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board."); - } - break; +NetworkConnectionState GSMConnectionHandler::update_handleInit() +{ + if (_gsm.begin(_pin) == GSM_READY) + { + Debug.print(DBG_INFO, "SIM card ok"); + _gsm.setTimeout(GSM_TIMEOUT); + return NetworkConnectionState::CONNECTING; + } + else + { + Debug.print(DBG_ERROR, "SIM not present or wrong PIN"); + return NetworkConnectionState::ERROR; } - connectionTickTimeInterval = newInterval; - lastConnectionTickTime = millis(); - netConnectionState = _newState; } +NetworkConnectionState GSMConnectionHandler::update_handleConnecting() +{ + GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true); + Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", network_status); + if (network_status == GSM3_NetworkStatus_t::ERROR) + { + Debug.print(DBG_ERROR, "GPRS attach failed"); + Debug.print(DBG_ERROR, "Make sure the antenna is connected and reset your board."); + return NetworkConnectionState::ERROR; + } + Debug.print(DBG_INFO, "Sending PING to outer space..."); + int const ping_result = _gprs.ping("time.arduino.cc"); + Debug.print(DBG_INFO, "GPRS.ping(): %d", ping_result); + if (ping_result < 0) + { + Debug.print(DBG_ERROR, "PING failed"); + Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", CHECK_INTERVAL_TABLE[static_cast(NetworkConnectionState::CONNECTING)]); + return NetworkConnectionState::CONNECTING; + } + else + { + Debug.print(DBG_INFO, "Connected to GPRS Network"); + return NetworkConnectionState::CONNECTED; + } +} -void GSMConnectionHandler::connect() { - if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { - return; +NetworkConnectionState GSMConnectionHandler::update_handleConnected() +{ + int const is_gsm_access_alive = _gsm.isAccessAlive(); + if (is_gsm_access_alive != 1) + { + return NetworkConnectionState::DISCONNECTED; } - keepAlive = true; - changeConnectionState(NetworkConnectionState::INIT); + return NetworkConnectionState::CONNECTED; +} +NetworkConnectionState GSMConnectionHandler::update_handleDisconnecting() +{ + _gsm.shutdown(); + return NetworkConnectionState::DISCONNECTED; } -void GSMConnectionHandler::disconnect() { - //WiFi.end(); - changeConnectionState(NetworkConnectionState::DISCONNECTING); - keepAlive = false; +NetworkConnectionState GSMConnectionHandler::update_handleDisconnected() +{ + if (_keep_alive) + { + return NetworkConnectionState::INIT; + } + else + { + return NetworkConnectionState::CLOSED; + } } -#endif /* #ifdef BOARD_HAS_GSM */ \ No newline at end of file +#endif /* #ifdef BOARD_HAS_GSM */ diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index f4c18ad1..714ed8c5 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -31,50 +31,39 @@ CLASS DECLARATION ******************************************************************************/ -class GSMConnectionHandler : public ConnectionHandler { +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 NetworkConnectionState check(); - virtual Client &getClient() { - return networkClient; - }; - virtual UDP &getUDP() { - return udp; - }; - - GSMClient networkClient; - GSM gsmAccess; - GPRS gprs; - GSMUDP udp; - - virtual void disconnect(); - virtual void connect(); - private: + GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true); - void changeConnectionState(NetworkConnectionState _newState); - 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 = 30000; - const int CHECK_INTERVAL_DISCONNECTED = 1000; - const int CHECK_INTERVAL_ERROR = 500; + virtual unsigned long getTime() override; + virtual Client & getClient() override { return _gsm_client; }; + virtual UDP & getUDP() override { return _gsm_udp; }; - const char *pin, *apn, *login, *pass; - unsigned long lastConnectionTickTime; - int connectionTickTimeInterval; + protected: - bool keepAlive; + 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; -}; -typedef GSMConnectionHandler TcpIpConnectionHandler; + private: + + const char * _pin; + const char * _apn; + const char * _login; + const char * _pass; + + GSM _gsm; + GPRS _gprs; + GSMUDP _gsm_udp; + GSMClient _gsm_client; +}; #endif /* #ifdef BOARD_HAS_GSM */ diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index 4919ce04..d3b86c97 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -24,195 +24,147 @@ #include "Arduino_LoRaConnectionHandler.h" /****************************************************************************** - CONSTANTS + TYPEDEF ******************************************************************************/ -static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ +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; /****************************************************************************** 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; +LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band, _lora_class const device_class) +: ConnectionHandler{false} +, _appeui(appeui) +, _appkey(appkey) +, _band(band) +, _device_class(device_class) +{ + } /****************************************************************************** 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; +int LoRaConnectionHandler::write(const uint8_t * buf, size_t size) +{ + _modem.beginPacket(); + _modem.write(buf, size); + int const 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 { + } + else + { Debug.print(DBG_INFO, "Message sent correctly!"); } return err; } -int LoRaConnectionHandler::read() { - return modem.read(); +int LoRaConnectionHandler::read() +{ + return _modem.read(); } -bool LoRaConnectionHandler::available() { - return modem.available(); -} - -NetworkConnectionState LoRaConnectionHandler::check() { - - 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; - } - } - - return netConnectionState; +bool LoRaConnectionHandler::available() +{ + return _modem.available(); } /****************************************************************************** - PRIVATE MEMBER FUNCTIONS + PROTECTED 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); +NetworkConnectionState LoRaConnectionHandler::update_handleInit() +{ + if (!_modem.begin(_band)) + { 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 + return NetworkConnectionState::ERROR; + } + //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); + _modem.configureClass(_device_class); 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); +NetworkConnectionState LoRaConnectionHandler::update_handleConnecting() +{ + bool const network_status = _modem.joinOTAA(_appeui, _appkey); + if (network_status != true) + { 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; + else + { + Debug.print(DBG_INFO, "Connected to the network"); + 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); - +NetworkConnectionState LoRaConnectionHandler::update_handleConnected() +{ + bool const network_status = _modem.connected(); + if (network_status != true) + { Debug.print(DBG_ERROR, "Connection to the network lost."); - if (keepAlive) { + if (_keep_alive) + { 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); - +NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() +{ Debug.print(DBG_ERROR, "Connection to the network lost."); - if (keepAlive) { + if (_keep_alive) + { 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; +NetworkConnectionState LoRaConnectionHandler::update_handleDisconnected() +{ + if (_keep_alive) + { return NetworkConnectionState::INIT; - } else { - Debug.print(DBG_VERBOSE, "Connection to the network terminated"); + } + else + { 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 index ed885172..a0cff734 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -24,68 +24,38 @@ #include "Arduino_ConnectionHandler.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 ConnectionHandler { +class LoRaConnectionHandler : public ConnectionHandler +{ public: - LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = _lora_band::EU868, _lora_class = _lora_class::CLASS_A); - - virtual void init(); - virtual unsigned long getTime(); - virtual NetworkConnectionState check(); - virtual int write(const uint8_t *buf, size_t size); - virtual int read(); - virtual bool available(); + LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band = _lora_band::EU868, _lora_class const device_class = _lora_class::CLASS_A); - virtual void disconnect(); - virtual 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 = 30000; - const int CHECK_INTERVAL_DISCONNECTING = 500; - const int CHECK_INTERVAL_DISCONNECTED = 1000; - const int CHECK_INTERVAL_ERROR = 500; + virtual int write(const uint8_t *buf, size_t size) override; + virtual int read() override; + virtual bool available() override; - LoRaModem modem; - const char *appeui, *appkey; - _lora_band band; - _lora_class deviceClass; - unsigned long lastConnectionTickTime; - int connectionTickTimeInterval; + protected: - bool keepAlive; + 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; - NetworkConnectionState update_handleInit(); - NetworkConnectionState update_handleConnecting(); - NetworkConnectionState update_handleConnected(); - - NetworkConnectionState update_handleDisconnecting(); - NetworkConnectionState update_handleDisconnected(); + private: + char const * _appeui; + char const * _appkey; + _lora_band _band; + _lora_class _device_class; + LoRaModem _modem; }; -typedef LoRaConnectionHandler LPWANConnectionHandler; - #endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_NBConnectionHandler.cpp b/src/Arduino_NBConnectionHandler.cpp index 7e493617..b3c719dd 100644 --- a/src/Arduino_NBConnectionHandler.cpp +++ b/src/Arduino_NBConnectionHandler.cpp @@ -19,15 +19,6 @@ INCLUDE ******************************************************************************/ -/* - static int const DBG_NONE = -1; - static int const DBG_ERROR = 0; - static int const DBG_WARNING = 1; - static int const DBG_INFO = 2; - static int const DBG_DEBUG = 3; - static int const DBG_VERBOSE = 4; -*/ - #include "Arduino_NBConnectionHandler.h" #ifdef BOARD_HAS_NB /* Only compile if this is a board with NB */ @@ -36,177 +27,110 @@ CONSTANTS ******************************************************************************/ -static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; +static int const NB_TIMEOUT = 30000; /****************************************************************************** CTOR/DTOR ******************************************************************************/ -NBConnectionHandler::NBConnectionHandler(const char *pin, bool _keepAlive) : - NBConnectionHandler(pin, "", _keepAlive) { +NBConnectionHandler::NBConnectionHandler(char const * pin, bool const keep_alive) +: NBConnectionHandler(pin, "", keep_alive) +{ + } -NBConnectionHandler::NBConnectionHandler(const char *pin, const char *apn, bool _keepAlive) : - NBConnectionHandler(pin, apn, "", "", _keepAlive) { +NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, bool const keep_alive) +: NBConnectionHandler(pin, apn, "", "", keep_alive) +{ + } -NBConnectionHandler::NBConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, bool _keepAlive) : - pin(pin), - apn(apn), - login(login), - pass(pass), - lastConnectionTickTime(millis()), - connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive) { +NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, char const * login, char const * pass, bool const keep_alive) +: ConnectionHandler{keep_alive} +, _pin(pin) +, _apn(apn) +, _login(login) +, _pass(pass) +{ + } /****************************************************************************** PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -void NBConnectionHandler::init() { - char msgBuffer[120]; - if (nbAccess.begin(pin, apn, login, pass) == NB_READY) { - Debug.print(DBG_INFO, "SIM card ok"); - nbAccess.setTimeout(CHECK_INTERVAL_RETRYING); - changeConnectionState(NetworkConnectionState::CONNECTING); - } else { - Debug.print(DBG_ERROR, "SIM not present or wrong PIN"); - } -} - -unsigned long NBConnectionHandler::getTime() { - return nbAccess.getTime(); -} - -NetworkConnectionState NBConnectionHandler::check() { - unsigned long const now = millis(); - int nbAccessAlive; - if (now - lastConnectionTickTime > connectionTickTimeInterval) { - switch (netConnectionState) { - case NetworkConnectionState::INIT: { - init(); - } - break; - - case NetworkConnectionState::CONNECTING: { - // NOTE: Blocking Call when 4th parameter == true - NB_NetworkStatus_t networkStatus; - networkStatus = gprs.attachGPRS(true); - Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", networkStatus); - if (networkStatus == NB_NetworkStatus_t::ERROR) { - // NO FURTHER ACTION WILL FOLLOW THIS - changeConnectionState(NetworkConnectionState::ERROR); - return netConnectionState; - } - Debug.print(DBG_INFO, "Sending PING to outer space..."); - int pingResult; - // pingResult = gprs.ping("time.arduino.cc"); - // Debug.print(DBG_INFO, "NB.ping(): %d", pingResult); - // if (pingResult < 0) { - if (pingResult < 0) { - Debug.print(DBG_ERROR, "PING failed"); - Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); - return netConnectionState; - } else { - Debug.print(DBG_INFO, "Connected to GPRS Network"); - changeConnectionState(NetworkConnectionState::CONNECTED); - return netConnectionState; - } - } - break; - case NetworkConnectionState::CONNECTED: { - nbAccessAlive = nbAccess.isAccessAlive(); - Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nbAccessAlive); - if (nbAccessAlive != 1) { - changeConnectionState(NetworkConnectionState::DISCONNECTED); - return netConnectionState; - } - Debug.print(DBG_VERBOSE, "Connected to Cellular Network"); - } - break; - case NetworkConnectionState::DISCONNECTED: { - //gprs.detachGPRS(); - if (keepAlive) { - Debug.print(DBG_VERBOSE, "keep alive > INIT"); - changeConnectionState(NetworkConnectionState::INIT); - } else { - changeConnectionState(NetworkConnectionState::CLOSED); - } - //changeConnectionState(NetworkConnectionState::CONNECTING); - } - break; - } - lastConnectionTickTime = now; - } - - return netConnectionState; +unsigned long NBConnectionHandler::getTime() +{ + return _nb.getTime(); } /****************************************************************************** PRIVATE MEMBER FUNCTIONS ******************************************************************************/ -void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { - int newInterval = CHECK_INTERVAL_IDLE; - switch (_newState) { - case NetworkConnectionState::INIT: { - newInterval = CHECK_INTERVAL_INIT; - } - break; - case NetworkConnectionState::CONNECTING: { - Debug.print(DBG_INFO, "Connecting to Cellular Network"); - newInterval = CHECK_INTERVAL_CONNECTING; - } - break; - case NetworkConnectionState::CONNECTED: { - execNetworkEventCallback(_on_connect_event_callback, 0); - newInterval = CHECK_INTERVAL_CONNECTED; - } - break; - case NetworkConnectionState::GETTIME: { - } - break; - case NetworkConnectionState::DISCONNECTING: { - Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network"); - nbAccess.shutdown(); - } - case NetworkConnectionState::DISCONNECTED: { - if (netConnectionState == NetworkConnectionState::CONNECTED) { - execNetworkEventCallback(_on_disconnect_event_callback, 0); - Debug.print(DBG_ERROR, "Disconnected from Cellular Network"); - Debug.print(DBG_ERROR, "Attempting reconnection"); - if (keepAlive) { - Debug.print(DBG_ERROR, "Attempting reconnection"); - } - } - newInterval = CHECK_INTERVAL_DISCONNECTED; - } - break; - case NetworkConnectionState::ERROR: { - execNetworkEventCallback(_on_error_event_callback, 0); - Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board."); - } - break; +NetworkConnectionState NBConnectionHandler::update_handleInit() +{ + if (_nb.begin(_pin, _apn, _login, _pass) == NB_READY) + { + Debug.print(DBG_INFO, "SIM card ok"); + _nb.setTimeout(NB_TIMEOUT); + return NetworkConnectionState::CONNECTING; + } + else + { + Debug.print(DBG_ERROR, "SIM not present or wrong PIN"); + return NetworkConnectionState::ERROR; } - connectionTickTimeInterval = newInterval; - lastConnectionTickTime = millis(); - netConnectionState = _newState; } +NetworkConnectionState NBConnectionHandler::update_handleConnecting() +{ + NB_NetworkStatus_t const network_status = _nb_gprs.attachGPRS(true); + Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", network_status); + if (network_status == NB_NetworkStatus_t::ERROR) + { + Debug.print(DBG_ERROR, "GPRS.attachGPRS() failed"); + return NetworkConnectionState::ERROR; + } + else + { + Debug.print(DBG_INFO, "Connected to GPRS Network"); + return NetworkConnectionState::CONNECTED; + } +} -void NBConnectionHandler::connect() { - if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { - return; +NetworkConnectionState NBConnectionHandler::update_handleConnected() +{ + int const nb_is_access_alive = _nb.isAccessAlive(); + Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nb_is_access_alive); + if (nb_is_access_alive != 1) + { + Debug.print(DBG_INFO, "Disconnected from cellular network"); + return NetworkConnectionState::DISCONNECTED; + } + else + { + Debug.print(DBG_VERBOSE, "Connected to Cellular Network"); + return NetworkConnectionState::CONNECTED; } - keepAlive = true; - changeConnectionState(NetworkConnectionState::INIT); +} +NetworkConnectionState NBConnectionHandler::update_handleDisconnecting() +{ + Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network"); + _nb.shutdown(); + return NetworkConnectionState::DISCONNECTED; } -void NBConnectionHandler::disconnect() { - //WiFi.end(); - changeConnectionState(NetworkConnectionState::DISCONNECTING); - keepAlive = false; +NetworkConnectionState NBConnectionHandler::update_handleDisconnected() +{ + if (_keep_alive) + { + return NetworkConnectionState::INIT; + } + else + { + return NetworkConnectionState::CLOSED; + } } -#endif /* #ifdef BOARD_HAS_NB */ \ No newline at end of file +#endif /* #ifdef BOARD_HAS_NB */ diff --git a/src/Arduino_NBConnectionHandler.h b/src/Arduino_NBConnectionHandler.h index dfd1dae0..53d2174e 100644 --- a/src/Arduino_NBConnectionHandler.h +++ b/src/Arduino_NBConnectionHandler.h @@ -30,52 +30,43 @@ CLASS DECLARATION ******************************************************************************/ -class NBConnectionHandler : public ConnectionHandler { +class NBConnectionHandler : public ConnectionHandler +{ public: - NBConnectionHandler(const char *pin, const bool keepAlive = true); - NBConnectionHandler(const char *pin, const char *apn, const bool keepAlive = true); - NBConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true); - - virtual void init(); - virtual unsigned long getTime(); - virtual NetworkConnectionState check(); - virtual Client &getClient() { - return networkClient; - }; - virtual UDP &getUDP() { - return udp; - }; - - NBClient networkClient; - NB nbAccess; - GPRS gprs; - NBUDP udp; - - virtual void disconnect(); - virtual void connect(); - private: + NBConnectionHandler(char const * pin, bool const keep_alive = true); + NBConnectionHandler(char const * pin, char const * apn, bool const keep_alive = true); + NBConnectionHandler(char const * pin, char const * apn, char const * login, char const * pass, bool const keep_alive = true); - void changeConnectionState(NetworkConnectionState _newState); - 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 = 30000; - const int CHECK_INTERVAL_DISCONNECTED = 1000; - const int CHECK_INTERVAL_ERROR = 500; + virtual unsigned long getTime() override; + virtual Client & getClient() override { return _nb_client; }; + virtual UDP & getUDP() override { return _nb_udp; }; - const char *pin, *apn, *login, *pass; - unsigned long lastConnectionTickTime; - int connectionTickTimeInterval; + protected: - bool keepAlive; + 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; -}; -typedef NBConnectionHandler TcpIpConnectionHandler; + private: + + void changeConnectionState(NetworkConnectionState _newState); + + char const * _pin; + char const * _apn; + char const * _login; + char const * _pass; + + NB _nb; + GPRS _nb_gprs; + NBUDP _nb_udp; + NBClient _nb_client; +}; #endif /* #ifdef BOARD_HAS_NB */ diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index 7b5a5efe..24bbceb0 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -27,22 +27,20 @@ CTOR/DTOR ******************************************************************************/ -WiFiConnectionHandler::WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive) : - ssid(_ssid), - pass(_pass), - lastConnectionTickTime(millis()), - connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive) { +WiFiConnectionHandler::WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive) +: ConnectionHandler{keep_alive} +, _ssid{ssid} +, _pass{pass} +{ + } /****************************************************************************** PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -void WiFiConnectionHandler::init() { -} - -unsigned long WiFiConnectionHandler::getTime() { +unsigned long WiFiConnectionHandler::getTime() +{ #if !defined(BOARD_ESP8266) return WiFi.getTime(); #else @@ -50,53 +48,16 @@ unsigned long WiFiConnectionHandler::getTime() { #endif } -NetworkConnectionState WiFiConnectionHandler::check() { - - unsigned long const now = millis(); - if((now - lastConnectionTickTime) > connectionTickTimeInterval) - { - 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::GETTIME: netConnectionState = update_handleGetTime (); break; - case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break; - case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected (); break; - case NetworkConnectionState::ERROR: break; - case NetworkConnectionState::CLOSED: break; - } - } - - return netConnectionState; -} - /****************************************************************************** - PRIVATE MEMBER FUNCTIONS + PROTECTED MEMBER FUNCTIONS ******************************************************************************/ -void WiFiConnectionHandler::connect() { - if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { - return; - } - keepAlive = true; - connectionTickTimeInterval = CHECK_INTERVAL_INIT; - netConnectionState = NetworkConnectionState::INIT; -} - -void WiFiConnectionHandler::disconnect() { - keepAlive = false; - netConnectionState = NetworkConnectionState::DISCONNECTING; -} - -NetworkConnectionState WiFiConnectionHandler::update_handleInit() { - Debug.print(DBG_VERBOSE, "::INIT"); - +NetworkConnectionState WiFiConnectionHandler::update_handleInit() +{ #ifndef BOARD_ESP8266 Debug.print(DBG_INFO, "WiFi.status(): %d", WiFi.status()); - if (WiFi.status() == NETWORK_HARDWARE_ERROR) { - execNetworkEventCallback(_on_error_event_callback, 0); + if (WiFi.status() == NETWORK_HARDWARE_ERROR) + { Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); Debug.print(DBG_ERROR, "Then reset and retry."); return NetworkConnectionState::ERROR; @@ -104,7 +65,8 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit() { Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); - if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { + if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) + { Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); delay(5000); @@ -113,82 +75,72 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit() { Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); WiFi.disconnect(); delay(300); - WiFi.begin(ssid, pass); + WiFi.begin(_ssid, _pass); delay(1000); #endif /* ifndef BOARD_ESP8266 */ - connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; return NetworkConnectionState::CONNECTING; } -NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() { - Debug.print(DBG_VERBOSE, "::CONNECTING"); - +NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() +{ #ifndef BOARD_ESP8266 - if (WiFi.status() != WL_CONNECTED) { - WiFi.begin(ssid, pass); + if (WiFi.status() != WL_CONNECTED) + { + WiFi.begin(_ssid, _pass); } #endif /* ifndef BOARD_ESP8266 */ - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - if (WiFi.status() != NETWORK_CONNECTED) { - Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); - Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); + if (WiFi.status() != NETWORK_CONNECTED) + { + Debug.print(DBG_ERROR, "Connection to \"%s\" failed", _ssid); + Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", CHECK_INTERVAL_TABLE[static_cast(NetworkConnectionState::CONNECTING)]); return NetworkConnectionState::CONNECTING; } - else { - Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); - execNetworkEventCallback(_on_connect_event_callback, 0); - connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; - return NetworkConnectionState::GETTIME; + else + { + Debug.print(DBG_INFO, "Connected to \"%s\"", _ssid); +#ifdef BOARD_ESP8266 + configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); +#endif + return NetworkConnectionState::CONNECTED; } } -NetworkConnectionState WiFiConnectionHandler::update_handleConnected() { - - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); +NetworkConnectionState WiFiConnectionHandler::update_handleConnected() +{ if (WiFi.status() != WL_CONNECTED) { - execNetworkEventCallback(_on_disconnect_event_callback, 0); - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); + Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", _ssid); - if (keepAlive) { + if (_keep_alive) + { Debug.print(DBG_ERROR, "Attempting reconnection"); } - connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; return NetworkConnectionState::DISCONNECTED; } - Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); return NetworkConnectionState::CONNECTED; } -NetworkConnectionState WiFiConnectionHandler::update_handleGetTime() { - Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); -#ifdef BOARD_ESP8266 - configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); -#endif - return NetworkConnectionState::CONNECTED; -} - -NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting() { - Debug.print(DBG_VERBOSE, "Disconnecting from \"%s\"", ssid); +NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting() +{ WiFi.disconnect(); return NetworkConnectionState::DISCONNECTED; } -NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected() { +NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected() +{ #ifndef BOARD_ESP8266 WiFi.end(); #endif /* ifndef BOARD_ESP8266 */ - if (keepAlive) { - connectionTickTimeInterval = CHECK_INTERVAL_INIT; + if (_keep_alive) + { return NetworkConnectionState::INIT; } - else { - Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); + else + { return NetworkConnectionState::CLOSED; } } diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index 08b40132..56670793 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -30,50 +30,34 @@ CLASS DECLARATION ******************************************************************************/ -class WiFiConnectionHandler : public ConnectionHandler { +class WiFiConnectionHandler : public ConnectionHandler +{ public: - WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive = true); - - virtual void init(); - virtual unsigned long getTime(); - virtual NetworkConnectionState check(); - virtual Client &getClient() { - return wifiClient; - }; - virtual UDP &getUDP() { - return udp; - }; - virtual void disconnect(); - virtual void connect(); - - WiFiUDP udp; - private: + WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive = true); - 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_DISCONNECTED = 1000; - const char *ssid, *pass; - unsigned long lastConnectionTickTime; + virtual unsigned long getTime() override; + virtual Client & getClient() override{ return _wifi_client; } + virtual UDP & getUDP() override { return _wifi_udp; } - WiFiClient wifiClient; - int connectionTickTimeInterval; - bool keepAlive; + protected: - NetworkConnectionState update_handleInit (); - NetworkConnectionState update_handleConnecting (); - NetworkConnectionState update_handleConnected (); - NetworkConnectionState update_handleGetTime (); - NetworkConnectionState update_handleDisconnecting(); - NetworkConnectionState update_handleDisconnected (); + 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: -typedef WiFiConnectionHandler TcpIpConnectionHandler; + char const * _ssid; + char const * _pass; + + WiFiUDP _wifi_udp; + WiFiClient _wifi_client; +}; #endif /* #ifdef BOARD_HAS_WIFI */