Skip to content

Commit fd2c854

Browse files
committed
Use timed attempt (#37)
* use timedAttempt lib * update test ci
1 parent 8b79789 commit fd2c854

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

.github/workflows/compile-examples.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ jobs:
3434
- source-url: https://github.com/fabik111/ArduinoBLE.git
3535
version: 82e2a28f871e97b313846cee6d9efed8943dca53
3636
- source-url: https://github.com/andreagilardoni/ArduinoIoTCloud.git
37-
version: a51de0e7bfb68b18aea5fbac753f0414991fac7f
37+
version: a4e171a058175f9615017bbf1e103663858a9610
3838
- name: Arduino_SecureElement
39-
- source-url: https://github.com/andreagilardoni/Arduino_CloudUtils.git
40-
version: 5fd19ba84d34ffbc3def4d1035d5a21fd9e2f06b
39+
- source-url: https://github.com/arduino-libraries/Arduino_CloudUtils.git
4140
- source-url: https://github.com/andreagilardoni/ArduinoStorage.git
4241
version: 39f0bd138103967aaafcaa7f5c0e1e237b4ccb4d
4342
- name: ArduinoJson

src/NetworkConfigurator.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#define SERVICE_ID_FOR_AGENTMANAGER 0xB0
2323

24+
#define NC_CONNECTION_RETRY_TIMER_ms 120000
25+
#define NC_CONNECTION_TIMEOUT_ms 15000
26+
#define NC_UPDATE_NETWORK_OPTIONS_TIMER_ms 120000
2427

2528
#if defined(BOARD_HAS_KVSTORE)
2629
KVStore _kvstore;
@@ -30,13 +33,16 @@ constexpr char *STORAGE_KEY{ "NETWORK_CONFIGS" };
3033
NetworkConfiguratorClass::NetworkConfiguratorClass(AgentsManagerClass &agentManager, ConnectionHandler &connectionHandler, bool startConfigurationIfConnectionFails)
3134
: _agentManager{ &agentManager },
3235
_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
3441
}
3542

3643
bool NetworkConfiguratorClass::begin() {
3744
_connectionLostStatus = false;
3845
_state = NetworkConfiguratorStates::READ_STORED_CONFIG;
39-
_startConnectionAttempt = 0;
4046
memset(&_networkSetting, 0x00, sizeof(models::NetworkSetting));
4147

4248
#ifdef BOARD_HAS_WIFI
@@ -67,6 +73,9 @@ bool NetworkConfiguratorClass::begin() {
6773
DEBUG_ERROR("NetworkConfiguratorClass::%s Failed to initialize the AgentsManagerClass", __FUNCTION__);
6874
}
6975

76+
_connectionTimeout.begin(NC_CONNECTION_TIMEOUT_ms);
77+
_connectionRetryTimer.begin(NC_CONNECTION_RETRY_TIMER_ms);
78+
7079
#ifdef BOARD_HAS_ETHERNET
7180
_networkSetting.type = NetworkAdapter::ETHERNET;
7281
_networkSetting.eth.timeout = 250;
@@ -76,24 +85,32 @@ bool NetworkConfiguratorClass::begin() {
7685
return true;
7786
}
7887
_connectionHandlerIstantiated = true;
88+
_connectionTimeout.reload();
7989
_state = NetworkConfiguratorStates::CHECK_ETH;
8090
#endif
8191
return true;
8292
}
8393

8494
NetworkConfiguratorStates NetworkConfiguratorClass::poll() {
85-
95+
NetworkConfiguratorStates nextState = _state;
8696
switch (_state) {
8797
#ifdef BOARD_HAS_ETHERNET
88-
case NetworkConfiguratorStates::CHECK_ETH: _state = handleCheckEth (); break;
98+
case NetworkConfiguratorStates::CHECK_ETH: nextState = handleCheckEth (); break;
8999
#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;
97114
}
98115

99116
return _state;
@@ -126,8 +143,6 @@ bool NetworkConfiguratorClass::resetStoredConfiguration() {
126143
}
127144

128145
bool NetworkConfiguratorClass::end() {
129-
_lastConnectionAttempt = 0;
130-
_lastOptionUpdate = 0;
131146
_agentManager->removeReturnNetworkSettingsCallback();
132147
_agentManager->removeRequestHandler(RequestType::SCAN);
133148
_agentManager->removeRequestHandler(RequestType::CONNECT);
@@ -139,32 +154,25 @@ bool NetworkConfiguratorClass::end() {
139154
NetworkConfiguratorClass::ConnectionResult NetworkConfiguratorClass::connectToNetwork(StatusMessage *err) {
140155
ConnectionResult res = ConnectionResult::IN_PROGRESS;
141156

142-
if (_startConnectionAttempt == 0) {
143-
_startConnectionAttempt = millis();
144-
}
145-
146157
NetworkConnectionState connectionRes = NetworkConnectionState::DISCONNECTED;
147158
connectionRes = _connectionHandler->check();
148159
if (connectionRes == NetworkConnectionState::CONNECTED) {
149-
_startConnectionAttempt = 0;
150160
DEBUG_INFO("Connected to network");
151161
sendStatus(StatusMessage::CONNECTED);
152162
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
154164
{
155165
#ifdef BOARD_HAS_WIFI
156166
// Need for restoring the scan after a failed connection attempt
157167
#if defined(ARDUINO_UNOR4_WIFI)
158168
WiFi.end();
159169
#endif
160170
#endif
161-
_startConnectionAttempt = 0;
162171
int errorCode;
163172
String errorMsg = decodeConnectionErrorMessage(connectionRes, &errorCode);
164173
DEBUG_INFO("Connection fail: %s", errorMsg.c_str());
165174
*err = (StatusMessage)errorCode;
166-
167-
_lastConnectionAttempt = millis();
175+
_connectionRetryTimer.reload();
168176
res = ConnectionResult::FAILED;
169177
}
170178

@@ -212,8 +220,7 @@ bool NetworkConfiguratorClass::updateNetworkOptions() {
212220
netOptionMsg.m.netOptions = &netOption;
213221
_agentManager->sendMsg(netOptionMsg);
214222

215-
_lastOptionUpdate = millis();
216-
223+
_optionUpdateTimer.reload();
217224
return true;
218225
}
219226

@@ -425,7 +432,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleReadStorage() {
425432
nextState = NetworkConfiguratorStates::CONFIGURED;
426433
#endif
427434

428-
if (nextState == NetworkConfiguratorStates::WAITING_FOR_CONFIG && _lastOptionUpdate == 0) {
435+
if (nextState == NetworkConfiguratorStates::WAITING_FOR_CONFIG && _optionUpdateTimer.getWaitTime() == 0) {
429436
updateNetworkOptions();
430437
}
431438
return nextState;
@@ -443,7 +450,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleTestStoredConfig() {
443450
if (_startBLEIfConnectionFails) {
444451
_agentManager->enableBLEAgent(true);
445452
}
446-
if(_lastOptionUpdate == 0) {
453+
if(_optionUpdateTimer.getWaitTime() == 0) {
447454
updateNetworkOptions();
448455
}
449456
nextState = NetworkConfiguratorStates::WAITING_FOR_CONFIG;
@@ -465,13 +472,11 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleWaitingForConf() {
465472
_receivedEvent = NetworkConfiguratorEvents::NONE;
466473
if (nextState == _state) {
467474
//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();
472477
}
473478

474-
if (_connectionHandlerIstantiated && _agentManager->isConfigInProgress() != true && (millis() - _lastConnectionAttempt > 120000)) {
479+
if (_connectionHandlerIstantiated && _agentManager->isConfigInProgress() != true && _connectionRetryTimer.isExpired()) {
475480
sendStatus(StatusMessage::CONNECTING);
476481
nextState = NetworkConfiguratorStates::CONNECTING;
477482
}

src/NetworkConfigurator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "GenericConnectionHandler.h"
1212
#include "ConfiguratorAgents/AgentsManager.h"
1313
#include <settings/settings.h>
14-
#define NC_CONNECTION_TIMEOUT 15000
14+
#include <Arduino_TimedAttempt.h>
1515

1616
enum class NetworkConfiguratorStates { CHECK_ETH,
1717
READ_STORED_CONFIG,
@@ -44,10 +44,10 @@ class NetworkConfiguratorClass {
4444
bool _startBLEIfConnectionFails;
4545
bool _connectionHandlerIstantiated = false;
4646
bool _checkStoredCred = true;
47-
uint32_t _lastConnectionAttempt = 0;
48-
uint32_t _startConnectionAttempt;
47+
TimedAttempt _connectionTimeout;
48+
TimedAttempt _connectionRetryTimer;
49+
TimedAttempt _optionUpdateTimer;
4950
bool _connectionLostStatus = false;
50-
uint32_t _lastOptionUpdate = 0;
5151
enum class NetworkConfiguratorEvents { NONE,
5252
SCAN_REQ,
5353
CONNECT_REQ,

0 commit comments

Comments
 (0)