Skip to content

Commit a33784d

Browse files
committed
Make function pointer containing connection handler event callbacks private and access them via one generalised execCallback function
1 parent 2c45fa0 commit a33784d

6 files changed

+33
-22
lines changed

src/Arduino_ConnectionHandler.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
4747
_on_error_event_callback = callback;
4848
}
4949

50-
void ConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) {
51-
if (callback) {
52-
(*callback)(callback_arg);
50+
/******************************************************************************
51+
PRIVATE MEMBER FUNCTIONS
52+
******************************************************************************/
53+
54+
void ConnectionHandler::execCallback(NetworkConnectionEvent const event, void * callback_arg) {
55+
switch (event) {
56+
case NetworkConnectionEvent::CONNECTED: if(_on_connect_event_callback) _on_connect_event_callback (callback_arg); break;
57+
case NetworkConnectionEvent::DISCONNECTED: if(_on_disconnect_event_callback) _on_disconnect_event_callback(callback_arg); break;
58+
case NetworkConnectionEvent::ERROR: if(_on_error_event_callback) _on_error_event_callback (callback_arg); break;
59+
case NetworkConnectionEvent::INIT: break;
60+
case NetworkConnectionEvent::CONNECTING: break;
61+
case NetworkConnectionEvent::DISCONNECTING: break;
62+
case NetworkConnectionEvent::CLOSED: break;
5363
}
5464
}
55-

src/Arduino_ConnectionHandler.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ class ConnectionHandler {
162162
void addErrorCallback(OnNetworkEventCallback callback) __attribute__((deprecated));
163163

164164
protected:
165-
OnNetworkEventCallback _on_connect_event_callback = NULL,
166-
_on_disconnect_event_callback = NULL,
167-
_on_error_event_callback = NULL;
168165

169166
NetworkConnectionState netConnectionState = NetworkConnectionState::INIT;
170167

171-
static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg);
168+
void execCallback(NetworkConnectionEvent const event, void * callback_arg);
169+
170+
private:
172171

172+
OnNetworkEventCallback _on_connect_event_callback = NULL,
173+
_on_disconnect_event_callback = NULL,
174+
_on_error_event_callback = NULL;
173175
};
174176

175177
#if defined(BOARD_HAS_WIFI)

src/Arduino_GSMConnectionHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void GSMConnectionHandler::changeConnectionState(NetworkConnectionState _newStat
151151
}
152152
break;
153153
case NetworkConnectionState::CONNECTED: {
154-
execNetworkEventCallback(_on_connect_event_callback, 0);
154+
execCallback(NetworkConnectionEvent::CONNECTED, 0);
155155
newInterval = CHECK_INTERVAL_CONNECTED;
156156
}
157157
break;
@@ -164,7 +164,7 @@ void GSMConnectionHandler::changeConnectionState(NetworkConnectionState _newStat
164164
}
165165
case NetworkConnectionState::DISCONNECTED: {
166166
if (netConnectionState == NetworkConnectionState::CONNECTED) {
167-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
167+
execCallback(NetworkConnectionEvent::DISCONNECTED, 0);
168168
Debug.print(DBG_ERROR, "Disconnected from Cellular Network");
169169
Debug.print(DBG_ERROR, "Attempting reconnection");
170170
if (keepAlive) {
@@ -175,7 +175,7 @@ void GSMConnectionHandler::changeConnectionState(NetworkConnectionState _newStat
175175
}
176176
break;
177177
case NetworkConnectionState::ERROR: {
178-
execNetworkEventCallback(_on_error_event_callback, 0);
178+
execCallback(NetworkConnectionEvent::ERROR, 0);
179179
Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board.");
180180
}
181181
break;

src/Arduino_LoRaConnectionHandler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ NetworkConnectionState LoRaConnectionHandler::update_handleInit() {
133133
Debug.print(DBG_VERBOSE, "::INIT");
134134
if (!modem.begin(band)) {
135135
Debug.print(DBG_VERBOSE, "Failed to start module");
136-
execNetworkEventCallback(_on_error_event_callback, 0);
136+
execCallback(NetworkConnectionEvent::ERROR, 0);
137137
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
138138
};
139139
//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
@@ -149,14 +149,14 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnecting() {
149149
Debug.print(DBG_VERBOSE, "::CONNECTING");
150150
bool networkStatus = modem.joinOTAA(appeui, appkey);
151151
if (networkStatus != true) {
152-
execNetworkEventCallback(_on_error_event_callback, 0);
152+
execCallback(NetworkConnectionEvent::ERROR, 0);
153153
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
154154
return NetworkConnectionState::ERROR;
155155
}
156156

157157
Debug.print(DBG_INFO, "Connected to the network");
158158
connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED;
159-
execNetworkEventCallback(_on_connect_event_callback, 0);
159+
execCallback(NetworkConnectionEvent::CONNECTED, 0);
160160
return NetworkConnectionState::CONNECTED;
161161
}
162162

@@ -165,7 +165,7 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnected() {
165165
bool networkStatus = modem.connected();
166166
Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus);
167167
if (networkStatus != true) {
168-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
168+
execCallback(NetworkConnectionEvent::DISCONNECTED, 0);
169169

170170
Debug.print(DBG_ERROR, "Connection to the network lost.");
171171
if (keepAlive) {
@@ -180,7 +180,7 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnected() {
180180
}
181181

182182
NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() {
183-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
183+
execCallback(NetworkConnectionEvent::DISCONNECTED, 0);
184184

185185
Debug.print(DBG_ERROR, "Connection to the network lost.");
186186
if (keepAlive) {

src/Arduino_NBConnectionHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState
159159
}
160160
break;
161161
case NetworkConnectionState::CONNECTED: {
162-
execNetworkEventCallback(_on_connect_event_callback, 0);
162+
execCallback(NetworkConnectionEvent::CONNECTED, 0);
163163
newInterval = CHECK_INTERVAL_CONNECTED;
164164
}
165165
break;
@@ -172,7 +172,7 @@ void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState
172172
}
173173
case NetworkConnectionState::DISCONNECTED: {
174174
if (netConnectionState == NetworkConnectionState::CONNECTED) {
175-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
175+
execCallback(NetworkConnectionEvent::DISCONNECTED, 0);
176176
Debug.print(DBG_ERROR, "Disconnected from Cellular Network");
177177
Debug.print(DBG_ERROR, "Attempting reconnection");
178178
if (keepAlive) {
@@ -183,7 +183,7 @@ void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState
183183
}
184184
break;
185185
case NetworkConnectionState::ERROR: {
186-
execNetworkEventCallback(_on_error_event_callback, 0);
186+
execCallback(NetworkConnectionEvent::ERROR, 0);
187187
Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board.");
188188
}
189189
break;

src/Arduino_WiFiConnectionHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit() {
9696
#ifndef BOARD_ESP8266
9797
Debug.print(DBG_INFO, "WiFi.status(): %d", WiFi.status());
9898
if (WiFi.status() == NETWORK_HARDWARE_ERROR) {
99-
execNetworkEventCallback(_on_error_event_callback, 0);
99+
execCallback(NetworkConnectionEvent::ERROR, 0);
100100
Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield.");
101101
Debug.print(DBG_ERROR, "Then reset and retry.");
102102
return NetworkConnectionState::ERROR;
@@ -138,7 +138,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() {
138138
}
139139
else {
140140
Debug.print(DBG_INFO, "Connected to \"%s\"", ssid);
141-
execNetworkEventCallback(_on_connect_event_callback, 0);
141+
execCallback(NetworkConnectionEvent::CONNECTED, 0);
142142
connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED;
143143
return NetworkConnectionState::GETTIME;
144144
}
@@ -149,7 +149,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnected() {
149149
Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status());
150150
if (WiFi.status() != WL_CONNECTED)
151151
{
152-
execNetworkEventCallback(_on_disconnect_event_callback, 0);
152+
execCallback(NetworkConnectionEvent::DISCONNECTED, 0);
153153

154154
Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status());
155155
Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid);

0 commit comments

Comments
 (0)