Skip to content

Commit b305d72

Browse files
Added check state into ConnectionHandler
the objective of the check state is to validate the configuration of the connection handler before going into the init state. For the time being the only valid check is to wait for settings to be provided if required. WiFiConnectionHandler splits the init state into check and init
1 parent 0fcdd40 commit b305d72

7 files changed

+42
-18
lines changed

src/ConnectionHandlerDefinitions.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@
163163
******************************************************************************/
164164

165165
enum class NetworkConnectionState : unsigned int {
166-
INIT = 0,
167-
CONNECTING = 1,
168-
CONNECTED = 2,
169-
DISCONNECTING = 3,
170-
DISCONNECTED = 4,
171-
CLOSED = 5,
172-
ERROR = 6
166+
CHECK = 0,
167+
INIT = 1,
168+
CONNECTING = 2,
169+
CONNECTED = 3,
170+
DISCONNECTING = 4,
171+
DISCONNECTED = 5,
172+
CLOSED = 6,
173+
ERROR = 7
173174
};
174175

175176
enum class NetworkConnectionEvent {

src/ConnectionHandlerInterface.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
2323
: _flags{keep_alive, false, settings_required, false}
2424
, _interface{interface}
2525
, _lastConnectionTickTime{millis()}
26-
, _current_net_connection_state{NetworkConnectionState::INIT}
26+
, _current_net_connection_state{NetworkConnectionState::CHECK}
2727
, _timeoutTable(DefaultTimeoutTable)
2828
{
2929

@@ -73,6 +73,7 @@ NetworkConnectionState ConnectionHandler::updateConnectionState() {
7373
*/
7474
switch (_current_net_connection_state)
7575
{
76+
case NetworkConnectionState::CHECK: next_net_connection_state = update_handleCheck (); break;
7677
case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break;
7778
case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break;
7879
case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break;
@@ -139,3 +140,11 @@ void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
139140
void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
140141
_on_error_event_callback = callback;
141142
}
143+
144+
NetworkConnectionState ConnectionHandler::update_handleCheck() {
145+
if(_flags.settings_required && !_flags.settings_provided) {
146+
return NetworkConnectionState::CHECK;
147+
} else {
148+
return NetworkConnectionState::INIT;
149+
}
150+
}

src/ConnectionHandlerInterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ConnectionHandler {
9090
* @return true if the update is successful, false otherwise
9191
*/
9292
virtual bool updateSetting(const models::NetworkSetting& s) {
93-
if(_current_net_connection_state == NetworkConnectionState::INIT && s.type == _interface) {
93+
if(_current_net_connection_state <= NetworkConnectionState::INIT && s.type == _interface) {
9494
memcpy(&_settings, &s, sizeof(s));
9595
_flags.settings_provided = true;
9696
return true;
@@ -125,6 +125,7 @@ class ConnectionHandler {
125125

126126
NetworkAdapter _interface;
127127

128+
virtual NetworkConnectionState update_handleCheck ();
128129
virtual NetworkConnectionState update_handleInit () = 0;
129130
virtual NetworkConnectionState update_handleConnecting () = 0;
130131
virtual NetworkConnectionState update_handleConnected () = 0;

src/GenericConnectionHandler.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ GenericConnectionHandler::GenericConnectionHandler(bool const keep_alive)
2121
: ConnectionHandler(keep_alive, NetworkAdapter::NONE, true), _ch(nullptr) { }
2222

2323
bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) {
24-
if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) {
24+
if(_ch != nullptr && _ch->_current_net_connection_state > NetworkConnectionState::INIT) {
2525
// If the internal connection handler is already being used and not in INIT phase we cannot update the settings
2626
return false;
27-
} else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) {
27+
} else if(_ch != nullptr && _ch->_current_net_connection_state <= NetworkConnectionState::INIT && _interface != s.type) {
2828
// If the internal connection handler is already being used and in INIT phase and the interface type is being changed
2929
// -> we need to deallocate the previously allocated handler
3030

@@ -57,27 +57,31 @@ void GenericConnectionHandler::getSetting(models::NetworkSetting& s) {
5757
}
5858

5959
NetworkConnectionState GenericConnectionHandler::updateConnectionState() {
60-
return _ch != nullptr ? _ch->updateConnectionState() : NetworkConnectionState::INIT;
60+
return _ch != nullptr ? _ch->updateConnectionState() : NetworkConnectionState::CHECK;
61+
}
62+
63+
NetworkConnectionState GenericConnectionHandler::update_handleCheck() {
64+
return _ch != nullptr ? _ch->update_handleCheck() : NetworkConnectionState::CHECK;
6165
}
6266

6367
NetworkConnectionState GenericConnectionHandler::update_handleInit() {
64-
return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT;
68+
return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::CHECK;
6569
}
6670

6771
NetworkConnectionState GenericConnectionHandler::update_handleConnecting() {
68-
return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT;
72+
return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::CHECK;
6973
}
7074

7175
NetworkConnectionState GenericConnectionHandler::update_handleConnected() {
72-
return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT;
76+
return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::CHECK;
7377
}
7478

7579
NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() {
76-
return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT;
80+
return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::CHECK;
7781
}
7882

7983
NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() {
80-
return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT;
84+
return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::CHECK;
8185
}
8286

8387
#if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD))

src/GenericConnectionHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class GenericConnectionHandler : public ConnectionHandler
6060

6161
NetworkConnectionState updateConnectionState() override;
6262

63+
NetworkConnectionState update_handleCheck () override;
6364
NetworkConnectionState update_handleInit () override;
6465
NetworkConnectionState update_handleConnecting () override;
6566
NetworkConnectionState update_handleConnected () override;

src/WiFiConnectionHandler.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ unsigned long WiFiConnectionHandler::getTime()
5757
PROTECTED MEMBER FUNCTIONS
5858
******************************************************************************/
5959

60-
NetworkConnectionState WiFiConnectionHandler::update_handleInit()
60+
NetworkConnectionState WiFiConnectionHandler::update_handleCheck()
6161
{
6262
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
6363
if (WiFi.status() == NETWORK_HARDWARE_ERROR)
@@ -82,7 +82,14 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
8282
WiFi.mode(WIFI_STA);
8383
#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
8484

85+
return ConnectionHandler::update_handleCheck();
86+
}
87+
88+
NetworkConnectionState WiFiConnectionHandler::update_handleInit()
89+
{
8590
DEBUG_INFO(F("WiFi.status(): %d"), WiFi.status());
91+
92+
if (WiFi.status() != WL_CONNECTED && _flags.settings_provided)
8693
{
8794
WiFi.begin(_settings.wifi.ssid, _settings.wifi.pwd);
8895
#if defined(ARDUINO_ARCH_ESP8266)

src/WiFiConnectionHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class WiFiConnectionHandler : public ConnectionHandler
6666

6767
protected:
6868

69+
NetworkConnectionState update_handleCheck () override;
6970
NetworkConnectionState update_handleInit () override;
7071
NetworkConnectionState update_handleConnecting () override;
7172
NetworkConnectionState update_handleConnected () override;

0 commit comments

Comments
 (0)