Skip to content

Return the connection state directly when calling function check() #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void loop() {
* object.
*/

conMan.update();
conMan.check();
}

void onNetworkConnect(void *_arg) {
Expand Down
29 changes: 18 additions & 11 deletions src/Arduino_ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,25 @@ 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;
virtual Client &getClient() = 0;
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);
Expand All @@ -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_ */
12 changes: 7 additions & 5 deletions src/Arduino_GSMConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
}
Expand All @@ -130,6 +130,8 @@ void GSMConnectionHandler::update() {
}
lastConnectionTickTime = now;
}

return netConnectionState;
}

/******************************************************************************
Expand Down
11 changes: 5 additions & 6 deletions src/Arduino_GSMConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
};
Expand Down Expand Up @@ -77,6 +74,8 @@ class GSMConnectionHandler : public TcpIpConnectionHandler {

};

typedef GSMConnectionHandler TcpIpConnectionHandler;

#endif /* #ifdef BOARD_HAS_GSM */

#endif /* #ifndef GSM_CONNECTION_MANAGER_H_ */
53 changes: 0 additions & 53 deletions src/Arduino_LPWANConnectionHandler.h

This file was deleted.

4 changes: 3 additions & 1 deletion src/Arduino_LoRaConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool LoRaConnectionHandler::available() {
return modem.available();
}

void LoRaConnectionHandler::update() {
NetworkConnectionState LoRaConnectionHandler::check() {

unsigned long const now = millis();
int networkStatus = 0;
Expand All @@ -121,6 +121,8 @@ void LoRaConnectionHandler::update() {
case NetworkConnectionState::CLOSED: break;
}
}

return netConnectionState;
}

/******************************************************************************
Expand Down
25 changes: 12 additions & 13 deletions src/Arduino_LoRaConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
INCLUDE
******************************************************************************/

#include "Arduino_LPWANConnectionHandler.h"
#include "Arduino_ConnectionHandler.h"

typedef enum {
LORA_ERROR_ACK_NOT_RECEIVED = -1,
Expand All @@ -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:

Expand Down Expand Up @@ -89,4 +86,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler {

};

typedef LoRaConnectionHandler LPWANConnectionHandler;

#endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */
12 changes: 7 additions & 5 deletions src/Arduino_NBConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
}
Expand All @@ -138,6 +138,8 @@ void NBConnectionHandler::update() {
}
lastConnectionTickTime = now;
}

return netConnectionState;
}

/******************************************************************************
Expand Down
9 changes: 4 additions & 5 deletions src/Arduino_NBConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@
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);
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 void check() {
update();
}
virtual void update() __attribute__((deprecated)); /* use 'update()' instead */
virtual NetworkConnectionState check();
virtual Client &getClient() {
return networkClient;
};
Expand Down Expand Up @@ -78,6 +75,8 @@ class NBConnectionHandler : public TcpIpConnectionHandler {

};

typedef NBConnectionHandler TcpIpConnectionHandler;

#endif /* #ifdef BOARD_HAS_NB */

#endif /* #ifndef NB_CONNECTION_MANAGER_H_ */
Loading