Skip to content

Commit 821180d

Browse files
committed
Perform callbacks in case of state transitions in base class now
1 parent 3a82983 commit 821180d

5 files changed

+42
-43
lines changed

src/Arduino_ConnectionHandler.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,43 @@ NetworkConnectionState ConnectionHandler::check()
4545
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
4646
{
4747
_lastConnectionTickTime = now;
48+
NetworkConnectionState next_net_connection_state = _current_net_connection_state;
4849

50+
/* While the state machine is implemented here, the concrete implementation of the
51+
* states is done in the derived connection handlers.
52+
*/
4953
switch (_current_net_connection_state)
5054
{
51-
case NetworkConnectionState::INIT: _current_net_connection_state = update_handleInit (); break;
52-
case NetworkConnectionState::CONNECTING: _current_net_connection_state = update_handleConnecting (); break;
53-
case NetworkConnectionState::CONNECTED: _current_net_connection_state = update_handleConnected (); break;
54-
case NetworkConnectionState::DISCONNECTING: _current_net_connection_state = update_handleDisconnecting(); break;
55-
case NetworkConnectionState::DISCONNECTED: _current_net_connection_state = update_handleDisconnected (); break;
56-
case NetworkConnectionState::ERROR: break;
57-
case NetworkConnectionState::CLOSED: break;
55+
case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break;
56+
case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break;
57+
case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break;
58+
case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting(); break;
59+
case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break;
60+
case NetworkConnectionState::ERROR: break;
61+
case NetworkConnectionState::CLOSED: break;
62+
}
63+
64+
/* Here we are determining whether a state transition from one state to the next has
65+
* occurred - and if it has, we call eventually registered callbacks.
66+
*/
67+
if(next_net_connection_state != _current_net_connection_state)
68+
{
69+
/* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */
70+
if(next_net_connection_state == NetworkConnectionState::CONNECTED)
71+
{
72+
if(_on_connect_event_callback) _on_connect_event_callback();
73+
}
74+
if(next_net_connection_state == NetworkConnectionState::DISCONNECTED)
75+
{
76+
if(_on_disconnect_event_callback) _on_disconnect_event_callback();
77+
}
78+
if(next_net_connection_state == NetworkConnectionState::ERROR)
79+
{
80+
if(_on_error_event_callback) _on_error_event_callback();
81+
}
82+
83+
/* Assign new state to the member variable holding the state */
84+
_current_net_connection_state = next_net_connection_state;
5885
}
5986
}
6087

@@ -95,17 +122,3 @@ void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
95122
void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
96123
_on_error_event_callback = callback;
97124
}
98-
99-
/******************************************************************************
100-
PRIVATE MEMBER FUNCTIONS
101-
******************************************************************************/
102-
103-
void ConnectionHandler::execCallback(NetworkConnectionEvent const event)
104-
{
105-
switch (event)
106-
{
107-
case NetworkConnectionEvent::CONNECTED: if(_on_connect_event_callback) _on_connect_event_callback (); break;
108-
case NetworkConnectionEvent::DISCONNECTED: if(_on_disconnect_event_callback) _on_disconnect_event_callback(); break;
109-
case NetworkConnectionEvent::ERROR: if(_on_error_event_callback) _on_error_event_callback (); break;
110-
}
111-
}

src/Arduino_ConnectionHandler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ class ConnectionHandler {
186186

187187
bool _keep_alive;
188188

189-
void execCallback(NetworkConnectionEvent const event);
190-
191189
virtual NetworkConnectionState update_handleInit () = 0;
192190
virtual NetworkConnectionState update_handleConnecting () = 0;
193191
virtual NetworkConnectionState update_handleConnected () = 0;

src/Arduino_GSMConnectionHandler.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ NetworkConnectionState GSMConnectionHandler::update_handleInit()
6767
else
6868
{
6969
Debug.print(DBG_ERROR, "SIM not present or wrong PIN");
70-
execCallback(NetworkConnectionEvent::ERROR);
7170
return NetworkConnectionState::ERROR;
7271
}
7372
}
@@ -80,7 +79,6 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
8079
{
8180
Debug.print(DBG_ERROR, "GPRS attach failed");
8281
Debug.print(DBG_ERROR, "Make sure the antenna is connected and reset your board.");
83-
execCallback(NetworkConnectionEvent::ERROR);
8482
return NetworkConnectionState::ERROR;
8583
}
8684
Debug.print(DBG_INFO, "Sending PING to outer space...");
@@ -95,7 +93,6 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
9593
else
9694
{
9795
Debug.print(DBG_INFO, "Connected to GPRS Network");
98-
execCallback(NetworkConnectionEvent::CONNECTED);
9996
return NetworkConnectionState::CONNECTED;
10097
}
10198
}
@@ -105,7 +102,6 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnected()
105102
int const is_gsm_access_alive = _gsm.isAccessAlive();
106103
if (is_gsm_access_alive != 1)
107104
{
108-
execCallback(NetworkConnectionEvent::DISCONNECTED);
109105
return NetworkConnectionState::DISCONNECTED;
110106
}
111107
return NetworkConnectionState::CONNECTED;
@@ -114,7 +110,6 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnected()
114110
NetworkConnectionState GSMConnectionHandler::update_handleDisconnecting()
115111
{
116112
_gsm.shutdown();
117-
execCallback(NetworkConnectionEvent::DISCONNECTED);
118113
return NetworkConnectionState::DISCONNECTED;
119114
}
120115

src/Arduino_LoRaConnectionHandler.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ NetworkConnectionState LoRaConnectionHandler::update_handleInit()
104104
{
105105
if (!_modem.begin(_band))
106106
{
107-
execCallback(NetworkConnectionEvent::ERROR);
108107
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
108+
return NetworkConnectionEvent::ERROR;
109109
}
110110
//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
111111
delay(100);
@@ -120,22 +120,21 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnecting()
120120
bool const network_status = _modem.joinOTAA(_appeui, _appkey);
121121
if (network_status != true)
122122
{
123-
execCallback(NetworkConnectionEvent::ERROR);
124123
Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry.");
125124
return NetworkConnectionState::ERROR;
126125
}
127-
128-
Debug.print(DBG_INFO, "Connected to the network");
129-
execCallback(NetworkConnectionEvent::CONNECTED);
130-
return NetworkConnectionState::CONNECTED;
126+
else
127+
{
128+
Debug.print(DBG_INFO, "Connected to the network");
129+
return NetworkConnectionState::CONNECTED;
130+
}
131131
}
132132

133133
NetworkConnectionState LoRaConnectionHandler::update_handleConnected()
134134
{
135135
bool const network_status = _modem.connected();
136-
if (network_status != true) {
137-
execCallback(NetworkConnectionEvent::DISCONNECTED);
138-
136+
if (network_status != true)
137+
{
139138
Debug.print(DBG_ERROR, "Connection to the network lost.");
140139
if (_keep_alive)
141140
{
@@ -148,8 +147,6 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnected()
148147

149148
NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting()
150149
{
151-
execCallback(NetworkConnectionEvent::DISCONNECTED);
152-
153150
Debug.print(DBG_ERROR, "Connection to the network lost.");
154151
if (_keep_alive)
155152
{

src/Arduino_WiFiConnectionHandler.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
5858
Debug.print(DBG_INFO, "WiFi.status(): %d", WiFi.status());
5959
if (WiFi.status() == NETWORK_HARDWARE_ERROR)
6060
{
61-
execCallback(NetworkConnectionEvent::ERROR);
6261
Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield.");
6362
Debug.print(DBG_ERROR, "Then reset and retry.");
6463
return NetworkConnectionState::ERROR;
@@ -101,7 +100,6 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
101100
else
102101
{
103102
Debug.print(DBG_INFO, "Connected to \"%s\"", _ssid);
104-
execCallback(NetworkConnectionEvent::CONNECTED);
105103
#ifdef BOARD_ESP8266
106104
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
107105
#endif
@@ -113,8 +111,6 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnected()
113111
{
114112
if (WiFi.status() != WL_CONNECTED)
115113
{
116-
execCallback(NetworkConnectionEvent::DISCONNECTED);
117-
118114
Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status());
119115
Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", _ssid);
120116

0 commit comments

Comments
 (0)