21
21
22
22
#define SERVICE_ID_FOR_AGENTMANAGER 0xB0
23
23
24
+ #define NC_CONNECTION_RETRY_TIMER_ms 120000
25
+ #define NC_CONNECTION_TIMEOUT_ms 15000
26
+ #define NC_UPDATE_NETWORK_OPTIONS_TIMER_ms 120000
24
27
25
28
#if defined(BOARD_HAS_KVSTORE)
26
29
KVStore _kvstore;
@@ -30,13 +33,16 @@ constexpr char *STORAGE_KEY{ "NETWORK_CONFIGS" };
30
33
NetworkConfiguratorClass::NetworkConfiguratorClass (AgentsManagerClass &agentManager, ConnectionHandler &connectionHandler, bool startConfigurationIfConnectionFails)
31
34
: _agentManager{ &agentManager },
32
35
_connectionHandler{ &connectionHandler },
33
- _startBLEIfConnectionFails{ startConfigurationIfConnectionFails } {
36
+ _startBLEIfConnectionFails{ startConfigurationIfConnectionFails },
37
+ _connectionTimeout{ NC_CONNECTION_TIMEOUT_ms, NC_CONNECTION_TIMEOUT_ms },
38
+ _connectionRetryTimer{ NC_CONNECTION_RETRY_TIMER_ms, NC_CONNECTION_RETRY_TIMER_ms },
39
+ _optionUpdateTimer{ NC_UPDATE_NETWORK_OPTIONS_TIMER_ms, NC_UPDATE_NETWORK_OPTIONS_TIMER_ms } {
40
+ _optionUpdateTimer.begin (NC_UPDATE_NETWORK_OPTIONS_TIMER_ms); // initialize the timer before calling begin
34
41
}
35
42
36
43
bool NetworkConfiguratorClass::begin () {
37
44
_connectionLostStatus = false ;
38
45
_state = NetworkConfiguratorStates::READ_STORED_CONFIG;
39
- _startConnectionAttempt = 0 ;
40
46
memset (&_networkSetting, 0x00 , sizeof (models::NetworkSetting));
41
47
42
48
#ifdef BOARD_HAS_WIFI
@@ -67,6 +73,9 @@ bool NetworkConfiguratorClass::begin() {
67
73
DEBUG_ERROR (" NetworkConfiguratorClass::%s Failed to initialize the AgentsManagerClass" , __FUNCTION__);
68
74
}
69
75
76
+ _connectionTimeout.begin (NC_CONNECTION_TIMEOUT_ms);
77
+ _connectionRetryTimer.begin (NC_CONNECTION_RETRY_TIMER_ms);
78
+
70
79
#ifdef BOARD_HAS_ETHERNET
71
80
_networkSetting.type = NetworkAdapter::ETHERNET;
72
81
_networkSetting.eth .timeout = 250 ;
@@ -76,24 +85,32 @@ bool NetworkConfiguratorClass::begin() {
76
85
return true ;
77
86
}
78
87
_connectionHandlerIstantiated = true ;
88
+ _connectionTimeout.reload ();
79
89
_state = NetworkConfiguratorStates::CHECK_ETH;
80
90
#endif
81
91
return true ;
82
92
}
83
93
84
94
NetworkConfiguratorStates NetworkConfiguratorClass::poll () {
85
-
95
+ NetworkConfiguratorStates nextState = _state;
86
96
switch (_state) {
87
97
#ifdef BOARD_HAS_ETHERNET
88
- case NetworkConfiguratorStates::CHECK_ETH: _state = handleCheckEth (); break ;
98
+ case NetworkConfiguratorStates::CHECK_ETH: nextState = handleCheckEth (); break ;
89
99
#endif
90
- case NetworkConfiguratorStates::READ_STORED_CONFIG: _state = handleReadStorage (); break ;
91
- case NetworkConfiguratorStates::TEST_STORED_CONFIG: _state = handleTestStoredConfig (); break ;
92
- case NetworkConfiguratorStates::WAITING_FOR_CONFIG: _state = handleWaitingForConf (); break ;
93
- case NetworkConfiguratorStates::CONNECTING: _state = handleConnecting (); break ;
94
- case NetworkConfiguratorStates::CONFIGURED: _state = handleConfigured (); break ;
95
- case NetworkConfiguratorStates::UPDATING_CONFIG: _state = handleUpdatingConfig (); break ;
96
- case NetworkConfiguratorStates::END: break ;
100
+ case NetworkConfiguratorStates::READ_STORED_CONFIG: nextState = handleReadStorage (); break ;
101
+ case NetworkConfiguratorStates::TEST_STORED_CONFIG: nextState = handleTestStoredConfig (); break ;
102
+ case NetworkConfiguratorStates::WAITING_FOR_CONFIG: nextState = handleWaitingForConf (); break ;
103
+ case NetworkConfiguratorStates::CONNECTING: nextState = handleConnecting (); break ;
104
+ case NetworkConfiguratorStates::CONFIGURED: nextState = handleConfigured (); break ;
105
+ case NetworkConfiguratorStates::UPDATING_CONFIG: nextState = handleUpdatingConfig (); break ;
106
+ case NetworkConfiguratorStates::END: break ;
107
+ }
108
+
109
+ if (_state != nextState){
110
+ if (nextState == NetworkConfiguratorStates::CONNECTING){
111
+ _connectionTimeout.reload ();
112
+ }
113
+ _state = nextState;
97
114
}
98
115
99
116
return _state;
@@ -126,8 +143,6 @@ bool NetworkConfiguratorClass::resetStoredConfiguration() {
126
143
}
127
144
128
145
bool NetworkConfiguratorClass::end () {
129
- _lastConnectionAttempt = 0 ;
130
- _lastOptionUpdate = 0 ;
131
146
_agentManager->removeReturnNetworkSettingsCallback ();
132
147
_agentManager->removeRequestHandler (RequestType::SCAN);
133
148
_agentManager->removeRequestHandler (RequestType::CONNECT);
@@ -139,32 +154,25 @@ bool NetworkConfiguratorClass::end() {
139
154
NetworkConfiguratorClass::ConnectionResult NetworkConfiguratorClass::connectToNetwork (StatusMessage *err) {
140
155
ConnectionResult res = ConnectionResult::IN_PROGRESS;
141
156
142
- if (_startConnectionAttempt == 0 ) {
143
- _startConnectionAttempt = millis ();
144
- }
145
-
146
157
NetworkConnectionState connectionRes = NetworkConnectionState::DISCONNECTED;
147
158
connectionRes = _connectionHandler->check ();
148
159
if (connectionRes == NetworkConnectionState::CONNECTED) {
149
- _startConnectionAttempt = 0 ;
150
160
DEBUG_INFO (" Connected to network" );
151
161
sendStatus (StatusMessage::CONNECTED);
152
162
res = ConnectionResult::SUCCESS;
153
- } else if (connectionRes != NetworkConnectionState::CONNECTED && millis () - _startConnectionAttempt > NC_CONNECTION_TIMEOUT ) // connection attempt failed
163
+ } else if (connectionRes != NetworkConnectionState::CONNECTED && _connectionTimeout. isExpired () ) // connection attempt failed
154
164
{
155
165
#ifdef BOARD_HAS_WIFI
156
166
// Need for restoring the scan after a failed connection attempt
157
167
#if defined(ARDUINO_UNOR4_WIFI)
158
168
WiFi.end ();
159
169
#endif
160
170
#endif
161
- _startConnectionAttempt = 0 ;
162
171
int errorCode;
163
172
String errorMsg = decodeConnectionErrorMessage (connectionRes, &errorCode);
164
173
DEBUG_INFO (" Connection fail: %s" , errorMsg.c_str ());
165
174
*err = (StatusMessage)errorCode;
166
-
167
- _lastConnectionAttempt = millis ();
175
+ _connectionRetryTimer.reload ();
168
176
res = ConnectionResult::FAILED;
169
177
}
170
178
@@ -212,8 +220,7 @@ bool NetworkConfiguratorClass::updateNetworkOptions() {
212
220
netOptionMsg.m .netOptions = &netOption;
213
221
_agentManager->sendMsg (netOptionMsg);
214
222
215
- _lastOptionUpdate = millis ();
216
-
223
+ _optionUpdateTimer.reload ();
217
224
return true ;
218
225
}
219
226
@@ -425,7 +432,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleReadStorage() {
425
432
nextState = NetworkConfiguratorStates::CONFIGURED;
426
433
#endif
427
434
428
- if (nextState == NetworkConfiguratorStates::WAITING_FOR_CONFIG && _lastOptionUpdate == 0 ) {
435
+ if (nextState == NetworkConfiguratorStates::WAITING_FOR_CONFIG && _optionUpdateTimer. getWaitTime () == 0 ) {
429
436
updateNetworkOptions ();
430
437
}
431
438
return nextState;
@@ -443,7 +450,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleTestStoredConfig() {
443
450
if (_startBLEIfConnectionFails) {
444
451
_agentManager->enableBLEAgent (true );
445
452
}
446
- if (_lastOptionUpdate == 0 ) {
453
+ if (_optionUpdateTimer. getWaitTime () == 0 ) {
447
454
updateNetworkOptions ();
448
455
}
449
456
nextState = NetworkConfiguratorStates::WAITING_FOR_CONFIG;
@@ -465,13 +472,11 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleWaitingForConf() {
465
472
_receivedEvent = NetworkConfiguratorEvents::NONE;
466
473
if (nextState == _state) {
467
474
// Check if update the network options
468
- if (millis () - _lastOptionUpdate > 120000 ) {
469
- #ifdef BOARD_HAS_WIFI
470
- updateNetworkOptions ();
471
- #endif
475
+ if (_optionUpdateTimer.isExpired ()) {
476
+ updateNetworkOptions ();
472
477
}
473
478
474
- if (_connectionHandlerIstantiated && _agentManager->isConfigInProgress () != true && ( millis () - _lastConnectionAttempt > 120000 )) {
479
+ if (_connectionHandlerIstantiated && _agentManager->isConfigInProgress () != true && _connectionRetryTimer. isExpired ( )) {
475
480
sendStatus (StatusMessage::CONNECTING);
476
481
nextState = NetworkConfiguratorStates::CONNECTING;
477
482
}
0 commit comments