Skip to content

Commit 5ebc8cd

Browse files
committed
- aStyle formatting of source and example
1 parent 2f1164f commit 5ebc8cd

6 files changed

+305
-293
lines changed

examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

+14-8
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,24 @@ void setup() {
4040
// the following methods allow the sketch to be notified when connected or
4141
// disconnected to the network
4242

43-
conMan.addConnectCallback(onNetworkConnect); // look at function onNetworkConnect towards the end of this sketch
44-
conMan.addDisconnectCallback(onNetworkDisconnect); // look at function onNetworkDisconnect towards the end of this sketch
43+
conMan.addConnectCallback(
44+
onNetworkConnect); // look at function onNetworkConnect towards the end of
45+
// this sketch
46+
conMan.addDisconnectCallback(
47+
onNetworkDisconnect); // look at function onNetworkDisconnect towards the
48+
// end of this sketch
4549
}
4650

4751
void loop() {
48-
// the following code keeps on running connection workflows on our ConnectionHandler object,
49-
// hence allowing reconnection in case of failure and notification of connect/disconnect event if enabled (see addConnectCallback/addDisconnectCallback)
50-
// NOTE: any use of delay() within the loop or methods called from it will delay the execution of .update(),
51-
// which might not guarantee the correct functioning of the ConnectionHandler object.
52-
53-
conMan.update();
52+
// the following code keeps on running connection workflows on our
53+
// ConnectionHandler object, hence allowing reconnection in case of failure
54+
// and notification of connect/disconnect event if enabled (see
55+
// addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within
56+
// the loop or methods called from it will delay the execution of .update(),
57+
// which might not guarantee the correct functioning of the ConnectionHandler
58+
// object.
5459

60+
conMan.update();
5561
}
5662

5763
void onNetworkConnect(void *_arg) {

src/Arduino_ConnectionHandler.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ class ConnectionHandler {
7070
virtual void addConnectCallback(OnNetworkEventCallback callback);
7171
virtual void addDisconnectCallback(OnNetworkEventCallback callback);
7272
virtual void addErrorCallback(OnNetworkEventCallback callback);
73-
73+
7474
private:
7575
OnNetworkEventCallback _on_connect_event_callback,
7676
_on_disconnect_event_callback,
7777
_on_error_event_callback;
7878

7979
protected:
80-
80+
8181
unsigned long lastValidTimestamp = 0; /* UNUSED */
8282
NetworkConnectionState netConnectionState = NetworkConnectionState::DISCONNECTED;
83-
83+
8484
};
8585

8686
#ifdef ARDUINO_SAMD_MKR1000
@@ -114,13 +114,13 @@ class ConnectionHandler {
114114
#endif
115115

116116
#if defined(ARDUINO_ESP8266_ESP12) || defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
117-
#ifdef ARDUINO_ESP8266_ESP12
118-
#include <ESP8266WiFi.h>
119-
120-
#else
121-
#include <WiFi.h>
122-
#endif
123-
117+
#ifdef ARDUINO_ESP8266_ESP12
118+
#include <ESP8266WiFi.h>
119+
120+
#else
121+
#include <WiFi.h>
122+
#endif
123+
124124
#include <WiFiUdp.h>
125125
#define BOARD_HAS_WIFI
126126
#define GETTIME_MISSING
@@ -131,9 +131,9 @@ class ConnectionHandler {
131131
#endif
132132

133133
#ifdef BOARD_HAS_WIFI
134-
#include "Arduino_WiFiConnectionHandler.h"
134+
#include "Arduino_WiFiConnectionHandler.h"
135135
#elif defined(BOARD_HAS_GSM)
136-
#include "Arduino_GSMConnectionHandler.h"
136+
#include "Arduino_GSMConnectionHandler.h"
137137
#endif
138138

139139
#endif /* CONNECTION_HANDLER_H_ */

src/Arduino_GSMConnectionHandler.cpp

+101-101
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
******************************************************************************/
2121

2222
/*
23-
static int const DBG_NONE = -1;
24-
static int const DBG_ERROR = 0;
25-
static int const DBG_WARNING = 1;
26-
static int const DBG_INFO = 2;
27-
static int const DBG_DEBUG = 3;
28-
static int const DBG_VERBOSE = 4;
23+
static int const DBG_NONE = -1;
24+
static int const DBG_ERROR = 0;
25+
static int const DBG_WARNING = 1;
26+
static int const DBG_INFO = 2;
27+
static int const DBG_DEBUG = 3;
28+
static int const DBG_VERBOSE = 4;
2929
*/
3030

3131
#include "Arduino_GSMConnectionHandler.h"
@@ -73,13 +73,13 @@ void GSMConnectionHandler::init() {
7373

7474
void GSMConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
7575
switch (event) {
76-
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
77-
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
78-
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
79-
case NetworkConnectionEvent::INIT: ; break;
80-
case NetworkConnectionEvent::CONNECTING: ; break;
81-
case NetworkConnectionEvent::DISCONNECTING: ; break;
82-
case NetworkConnectionEvent::CLOSED: ; break;
76+
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
77+
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
78+
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
79+
case NetworkConnectionEvent::INIT: ; break;
80+
case NetworkConnectionEvent::CONNECTING: ; break;
81+
case NetworkConnectionEvent::DISCONNECTING: ; break;
82+
case NetworkConnectionEvent::CLOSED: ; break;
8383
}
8484
}
8585

@@ -108,57 +108,57 @@ void GSMConnectionHandler::update() {
108108
int gsmAccessAlive;
109109
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
110110
switch (netConnectionState) {
111-
case NetworkConnectionState::INIT: {
112-
init();
113-
}
114-
break;
115-
116-
case NetworkConnectionState::CONNECTING: {
117-
// NOTE: Blocking Call when 4th parameter == true
118-
GSM3_NetworkStatus_t networkStatus;
119-
networkStatus = gprs.attachGPRS(apn, login, pass, true);
120-
Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", networkStatus);
121-
if (networkStatus == GSM3_NetworkStatus_t::ERROR) {
122-
// NO FURTHER ACTION WILL FOLLOW THIS
123-
changeConnectionState(NetworkConnectionState::ERROR);
124-
return;
125-
}
126-
Debug.print(DBG_INFO, "Sending PING to outer space...");
127-
int pingResult;
128-
pingResult = gprs.ping("time.arduino.cc");
129-
Debug.print(DBG_INFO, "GSM.ping(): %d", pingResult);
130-
if (pingResult < 0) {
131-
Debug.print(DBG_ERROR, "PING failed");
132-
Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
133-
return;
134-
} else {
135-
Debug.print(DBG_INFO, "Connected to GPRS Network");
136-
changeConnectionState(NetworkConnectionState::CONNECTED);
137-
return;
138-
}
139-
}
140-
break;
141-
case NetworkConnectionState::CONNECTED: {
142-
gsmAccessAlive = gsmAccess.isAccessAlive();
143-
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", gsmAccessAlive);
144-
if (gsmAccessAlive != 1) {
145-
changeConnectionState(NetworkConnectionState::DISCONNECTED);
146-
return;
147-
}
148-
Debug.print(DBG_VERBOSE, "Connected to Cellular Network");
149-
}
150-
break;
151-
case NetworkConnectionState::DISCONNECTED: {
152-
//gprs.detachGPRS();
153-
if (keepAlive) {
154-
Debug.print(DBG_VERBOSE, "keep alive > INIT");
155-
changeConnectionState(NetworkConnectionState::INIT);
156-
} else {
157-
changeConnectionState(NetworkConnectionState::CLOSED);
158-
}
159-
//changeConnectionState(NetworkConnectionState::CONNECTING);
160-
}
161-
break;
111+
case NetworkConnectionState::INIT: {
112+
init();
113+
}
114+
break;
115+
116+
case NetworkConnectionState::CONNECTING: {
117+
// NOTE: Blocking Call when 4th parameter == true
118+
GSM3_NetworkStatus_t networkStatus;
119+
networkStatus = gprs.attachGPRS(apn, login, pass, true);
120+
Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", networkStatus);
121+
if (networkStatus == GSM3_NetworkStatus_t::ERROR) {
122+
// NO FURTHER ACTION WILL FOLLOW THIS
123+
changeConnectionState(NetworkConnectionState::ERROR);
124+
return;
125+
}
126+
Debug.print(DBG_INFO, "Sending PING to outer space...");
127+
int pingResult;
128+
pingResult = gprs.ping("time.arduino.cc");
129+
Debug.print(DBG_INFO, "GSM.ping(): %d", pingResult);
130+
if (pingResult < 0) {
131+
Debug.print(DBG_ERROR, "PING failed");
132+
Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
133+
return;
134+
} else {
135+
Debug.print(DBG_INFO, "Connected to GPRS Network");
136+
changeConnectionState(NetworkConnectionState::CONNECTED);
137+
return;
138+
}
139+
}
140+
break;
141+
case NetworkConnectionState::CONNECTED: {
142+
gsmAccessAlive = gsmAccess.isAccessAlive();
143+
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", gsmAccessAlive);
144+
if (gsmAccessAlive != 1) {
145+
changeConnectionState(NetworkConnectionState::DISCONNECTED);
146+
return;
147+
}
148+
Debug.print(DBG_VERBOSE, "Connected to Cellular Network");
149+
}
150+
break;
151+
case NetworkConnectionState::DISCONNECTED: {
152+
//gprs.detachGPRS();
153+
if (keepAlive) {
154+
Debug.print(DBG_VERBOSE, "keep alive > INIT");
155+
changeConnectionState(NetworkConnectionState::INIT);
156+
} else {
157+
changeConnectionState(NetworkConnectionState::CLOSED);
158+
}
159+
//changeConnectionState(NetworkConnectionState::CONNECTING);
160+
}
161+
break;
162162
}
163163
lastConnectionTickTime = now;
164164
}
@@ -171,44 +171,44 @@ void GSMConnectionHandler::update() {
171171
void GSMConnectionHandler::changeConnectionState(NetworkConnectionState _newState) {
172172
int newInterval = CHECK_INTERVAL_IDLE;
173173
switch (_newState) {
174-
case NetworkConnectionState::INIT: {
175-
newInterval = CHECK_INTERVAL_INIT;
176-
}
177-
break;
178-
case NetworkConnectionState::CONNECTING: {
179-
Debug.print(DBG_INFO, "Connecting to Cellular Network");
180-
newInterval = CHECK_INTERVAL_CONNECTING;
181-
}
182-
break;
183-
case NetworkConnectionState::CONNECTED: {
184-
execNetworkEventCallback(_on_connect_event_callback, 0);
185-
newInterval = CHECK_INTERVAL_CONNECTED;
186-
}
187-
break;
188-
case NetworkConnectionState::GETTIME: {
189-
}
190-
break;
191-
case NetworkConnectionState::DISCONNECTING: {
192-
Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network");
193-
gsmAccess.shutdown();
194-
}
195-
case NetworkConnectionState::DISCONNECTED: {
196-
if (netConnectionState == NetworkConnectionState::CONNECTED) {
197-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
198-
Debug.print(DBG_ERROR, "Disconnected from Cellular Network");
199-
Debug.print(DBG_ERROR, "Attempting reconnection");
200-
if (keepAlive) {
201-
Debug.print(DBG_ERROR, "Attempting reconnection");
174+
case NetworkConnectionState::INIT: {
175+
newInterval = CHECK_INTERVAL_INIT;
202176
}
203-
}
204-
newInterval = CHECK_INTERVAL_DISCONNECTED;
205-
}
206-
break;
207-
case NetworkConnectionState::ERROR: {
208-
execNetworkEventCallback(_on_error_event_callback, 0);
209-
Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board.");
210-
}
211-
break;
177+
break;
178+
case NetworkConnectionState::CONNECTING: {
179+
Debug.print(DBG_INFO, "Connecting to Cellular Network");
180+
newInterval = CHECK_INTERVAL_CONNECTING;
181+
}
182+
break;
183+
case NetworkConnectionState::CONNECTED: {
184+
execNetworkEventCallback(_on_connect_event_callback, 0);
185+
newInterval = CHECK_INTERVAL_CONNECTED;
186+
}
187+
break;
188+
case NetworkConnectionState::GETTIME: {
189+
}
190+
break;
191+
case NetworkConnectionState::DISCONNECTING: {
192+
Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network");
193+
gsmAccess.shutdown();
194+
}
195+
case NetworkConnectionState::DISCONNECTED: {
196+
if (netConnectionState == NetworkConnectionState::CONNECTED) {
197+
execNetworkEventCallback(_on_disconnect_event_callback, 0);
198+
Debug.print(DBG_ERROR, "Disconnected from Cellular Network");
199+
Debug.print(DBG_ERROR, "Attempting reconnection");
200+
if (keepAlive) {
201+
Debug.print(DBG_ERROR, "Attempting reconnection");
202+
}
203+
}
204+
newInterval = CHECK_INTERVAL_DISCONNECTED;
205+
}
206+
break;
207+
case NetworkConnectionState::ERROR: {
208+
execNetworkEventCallback(_on_error_event_callback, 0);
209+
Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board.");
210+
}
211+
break;
212212
}
213213
connectionTickTimeInterval = newInterval;
214214
lastConnectionTickTime = millis();

src/Arduino_GSMConnectionHandler.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class GSMConnectionHandler : public ConnectionHandler {
3636

3737
virtual void init();
3838
virtual unsigned long getTime();
39-
virtual void check() { update();}
39+
virtual void check() {
40+
update();
41+
}
4042
virtual void update();
4143
virtual Client &getClient() {
4244
return networkClient;
@@ -74,7 +76,7 @@ class GSMConnectionHandler : public ConnectionHandler {
7476
unsigned long lastConnectionTickTime;
7577
int connectionTickTimeInterval;
7678

77-
79+
7880
bool keepAlive;
7981

8082
OnNetworkEventCallback _on_connect_event_callback,

0 commit comments

Comments
 (0)