Skip to content

Commit 6a87f58

Browse files
authored
Merge pull request #25 from arduino-libraries/return-network-state-in-check-method
Return the connection state directly when calling function check()
2 parents a498cd0 + f3ebad3 commit 6a87f58

12 files changed

+65
-164
lines changed

examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void loop() {
5151
* object.
5252
*/
5353

54-
conMan.update();
54+
conMan.check();
5555
}
5656

5757
void onNetworkConnect(void *_arg) {

src/Arduino_ConnectionHandler.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,25 @@ typedef void (*OnNetworkEventCallback)(void * /* arg */);
137137
class ConnectionHandler {
138138
public:
139139
virtual void init() = 0;
140-
virtual void check() = 0;
141-
virtual void update() __attribute__((deprecated)) = 0; /* use 'check()' instead */
140+
virtual NetworkConnectionState check() = 0;
142141

143142
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
144143
virtual unsigned long getTime() = 0;
145144
virtual Client &getClient() = 0;
146145
virtual UDP &getUDP() = 0;
147146
#endif
148147

149-
virtual NetworkConnectionState getStatus() {
148+
#if defined(BOARD_HAS_LORA)
149+
virtual int write(const uint8_t *buf, size_t size) = 0;
150+
virtual int read() = 0;
151+
virtual bool available() = 0;
152+
#endif
153+
154+
virtual NetworkConnectionState getStatus() __attribute__((deprecated)) {
150155
return netConnectionState;
151156
}
152-
virtual void connect();
153-
virtual void disconnect();
157+
virtual void connect() = 0;
158+
virtual void disconnect() = 0;
154159
void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback);
155160
void addConnectCallback(OnNetworkEventCallback callback);
156161
void addDisconnectCallback(OnNetworkEventCallback callback);
@@ -168,12 +173,14 @@ class ConnectionHandler {
168173

169174
};
170175

171-
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
172-
#include "Arduino_TcpIpConnectionHandler.h"
173-
#endif
174-
175-
#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310)
176-
#include "Arduino_LPWANConnectionHandler.h"
176+
#if defined(BOARD_HAS_WIFI)
177+
#include "Arduino_WiFiConnectionHandler.h"
178+
#elif defined(BOARD_HAS_GSM)
179+
#include "Arduino_GSMConnectionHandler.h"
180+
#elif defined(BOARD_HAS_NB)
181+
#include "Arduino_NBConnectionHandler.h"
182+
#elif defined(BOARD_HAS_LORA)
183+
#include "Arduino_LoRaConnectionHandler.h"
177184
#endif
178185

179186
#endif /* CONNECTION_HANDLER_H_ */

src/Arduino_GSMConnectionHandler.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ unsigned long GSMConnectionHandler::getTime() {
7171
return gsmAccess.getTime();
7272
}
7373

74-
void GSMConnectionHandler::update() {
74+
NetworkConnectionState GSMConnectionHandler::check() {
7575
unsigned long const now = millis();
7676
int gsmAccessAlive;
7777
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
@@ -89,7 +89,7 @@ void GSMConnectionHandler::update() {
8989
if (networkStatus == GSM3_NetworkStatus_t::ERROR) {
9090
// NO FURTHER ACTION WILL FOLLOW THIS
9191
changeConnectionState(NetworkConnectionState::ERROR);
92-
return;
92+
return netConnectionState;
9393
}
9494
Debug.print(DBG_INFO, "Sending PING to outer space...");
9595
int pingResult;
@@ -98,11 +98,11 @@ void GSMConnectionHandler::update() {
9898
if (pingResult < 0) {
9999
Debug.print(DBG_ERROR, "PING failed");
100100
Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
101-
return;
101+
return netConnectionState;
102102
} else {
103103
Debug.print(DBG_INFO, "Connected to GPRS Network");
104104
changeConnectionState(NetworkConnectionState::CONNECTED);
105-
return;
105+
return netConnectionState;
106106
}
107107
}
108108
break;
@@ -111,7 +111,7 @@ void GSMConnectionHandler::update() {
111111
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", gsmAccessAlive);
112112
if (gsmAccessAlive != 1) {
113113
changeConnectionState(NetworkConnectionState::DISCONNECTED);
114-
return;
114+
return netConnectionState;
115115
}
116116
Debug.print(DBG_VERBOSE, "Connected to Cellular Network");
117117
}
@@ -130,6 +130,8 @@ void GSMConnectionHandler::update() {
130130
}
131131
lastConnectionTickTime = now;
132132
}
133+
134+
return netConnectionState;
133135
}
134136

135137
/******************************************************************************

src/Arduino_GSMConnectionHandler.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
INCLUDE
2323
******************************************************************************/
2424

25-
#include "Arduino_TcpIpConnectionHandler.h"
25+
#include "Arduino_ConnectionHandler.h"
2626

2727

2828
#ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */
@@ -31,16 +31,13 @@
3131
CLASS DECLARATION
3232
******************************************************************************/
3333

34-
class GSMConnectionHandler : public TcpIpConnectionHandler {
34+
class GSMConnectionHandler : public ConnectionHandler {
3535
public:
3636
GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true);
3737

3838
virtual void init();
3939
virtual unsigned long getTime();
40-
virtual void check() {
41-
update();
42-
}
43-
virtual void update() __attribute__((deprecated)); /* use 'check()' instead */
40+
virtual NetworkConnectionState check();
4441
virtual Client &getClient() {
4542
return networkClient;
4643
};
@@ -77,6 +74,8 @@ class GSMConnectionHandler : public TcpIpConnectionHandler {
7774

7875
};
7976

77+
typedef GSMConnectionHandler TcpIpConnectionHandler;
78+
8079
#endif /* #ifdef BOARD_HAS_GSM */
8180

8281
#endif /* #ifndef GSM_CONNECTION_MANAGER_H_ */

src/Arduino_LPWANConnectionHandler.h

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/Arduino_LoRaConnectionHandler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool LoRaConnectionHandler::available() {
104104
return modem.available();
105105
}
106106

107-
void LoRaConnectionHandler::update() {
107+
NetworkConnectionState LoRaConnectionHandler::check() {
108108

109109
unsigned long const now = millis();
110110
int networkStatus = 0;
@@ -121,6 +121,8 @@ void LoRaConnectionHandler::update() {
121121
case NetworkConnectionState::CLOSED: break;
122122
}
123123
}
124+
125+
return netConnectionState;
124126
}
125127

126128
/******************************************************************************

src/Arduino_LoRaConnectionHandler.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
INCLUDE
2323
******************************************************************************/
2424

25-
#include "Arduino_LPWANConnectionHandler.h"
25+
#include "Arduino_ConnectionHandler.h"
2626

2727
typedef enum {
2828
LORA_ERROR_ACK_NOT_RECEIVED = -1,
@@ -40,23 +40,20 @@ typedef enum {
4040
CLASS DECLARATION
4141
******************************************************************************/
4242

43-
class LoRaConnectionHandler : public LPWANConnectionHandler {
43+
class LoRaConnectionHandler : public ConnectionHandler {
4444
public:
4545
LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = _lora_band::EU868, _lora_class = _lora_class::CLASS_A);
4646

47-
void init();
48-
unsigned long getTime();
49-
void check() {
50-
update();
51-
}
52-
void update() __attribute__((deprecated)); /* use 'check()' instead */
47+
virtual void init();
48+
virtual unsigned long getTime();
49+
virtual NetworkConnectionState check();
5350

54-
int write(const uint8_t *buf, size_t size);
55-
int read();
56-
bool available();
51+
virtual int write(const uint8_t *buf, size_t size);
52+
virtual int read();
53+
virtual bool available();
5754

58-
void disconnect();
59-
void connect();
55+
virtual void disconnect();
56+
virtual void connect();
6057

6158
private:
6259

@@ -89,4 +86,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler {
8986

9087
};
9188

89+
typedef LoRaConnectionHandler LPWANConnectionHandler;
90+
9291
#endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */

src/Arduino_NBConnectionHandler.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ unsigned long NBConnectionHandler::getTime() {
7878
return nbAccess.getTime();
7979
}
8080

81-
void NBConnectionHandler::update() {
81+
NetworkConnectionState NBConnectionHandler::check() {
8282
unsigned long const now = millis();
8383
int nbAccessAlive;
8484
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
@@ -96,7 +96,7 @@ void NBConnectionHandler::update() {
9696
if (networkStatus == NB_NetworkStatus_t::ERROR) {
9797
// NO FURTHER ACTION WILL FOLLOW THIS
9898
changeConnectionState(NetworkConnectionState::ERROR);
99-
return;
99+
return netConnectionState;
100100
}
101101
Debug.print(DBG_INFO, "Sending PING to outer space...");
102102
int pingResult;
@@ -106,11 +106,11 @@ void NBConnectionHandler::update() {
106106
if (pingResult < 0) {
107107
Debug.print(DBG_ERROR, "PING failed");
108108
Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
109-
return;
109+
return netConnectionState;
110110
} else {
111111
Debug.print(DBG_INFO, "Connected to GPRS Network");
112112
changeConnectionState(NetworkConnectionState::CONNECTED);
113-
return;
113+
return netConnectionState;
114114
}
115115
}
116116
break;
@@ -119,7 +119,7 @@ void NBConnectionHandler::update() {
119119
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nbAccessAlive);
120120
if (nbAccessAlive != 1) {
121121
changeConnectionState(NetworkConnectionState::DISCONNECTED);
122-
return;
122+
return netConnectionState;
123123
}
124124
Debug.print(DBG_VERBOSE, "Connected to Cellular Network");
125125
}
@@ -138,6 +138,8 @@ void NBConnectionHandler::update() {
138138
}
139139
lastConnectionTickTime = now;
140140
}
141+
142+
return netConnectionState;
141143
}
142144

143145
/******************************************************************************

src/Arduino_NBConnectionHandler.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@
3030
CLASS DECLARATION
3131
******************************************************************************/
3232

33-
class NBConnectionHandler : public TcpIpConnectionHandler {
33+
class NBConnectionHandler : public ConnectionHandler {
3434
public:
3535
NBConnectionHandler(const char *pin, const bool keepAlive = true);
3636
NBConnectionHandler(const char *pin, const char *apn, const bool keepAlive = true);
3737
NBConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true);
3838

3939
virtual void init();
4040
virtual unsigned long getTime();
41-
virtual void check() {
42-
update();
43-
}
44-
virtual void update() __attribute__((deprecated)); /* use 'update()' instead */
41+
virtual NetworkConnectionState check();
4542
virtual Client &getClient() {
4643
return networkClient;
4744
};
@@ -78,6 +75,8 @@ class NBConnectionHandler : public TcpIpConnectionHandler {
7875

7976
};
8077

78+
typedef NBConnectionHandler TcpIpConnectionHandler;
79+
8180
#endif /* #ifdef BOARD_HAS_NB */
8281

8382
#endif /* #ifndef NB_CONNECTION_MANAGER_H_ */

0 commit comments

Comments
 (0)