Skip to content

Commit 541a918

Browse files
committed
chore: Fix back-off logic
1 parent 62dd486 commit 541a918

File tree

3 files changed

+12
-42
lines changed

3 files changed

+12
-42
lines changed

src/AIoTC_Config.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,19 @@
160160
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
161161
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
162162

163-
#define AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms (2000UL)
164-
#define AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms (32000UL)
163+
#if defined(USE_NOTECARD)
164+
// 10x the standard delays for Notecard
165+
#define AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms (20000UL)
166+
#define AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms (320000UL)
167+
#else
168+
#define AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms (2000UL)
169+
#define AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms (32000UL)
170+
#endif
171+
165172
#define AIOT_CONFIG_THING_ID_REQUEST_MAX_RETRY_CNT (10UL)
166173

167174
#define AIOT_CONFIG_DEVICE_REGISTERED_RETRY_DELAY_k (10UL)
168-
#define AIOT_CONFIG_MAX_DEVICE_REGISTERED_RETRY_DELAY_k (4UL)
175+
#define AIOT_CONFIG_MAX_DEVICE_REGISTERED_RETRY_DELAY_k (40UL)
169176

170177
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
171178
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)

src/ArduinoIoTCloudNotecard.cpp

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@
4343
* CONSTANTS
4444
******************************************************************************/
4545

46-
#ifdef BACKOFF
47-
static size_t const BACKOFF_BASE_MS = 60000; // 1 minute
48-
static uint32_t const BACKOFF_MAX_MS = 3600000; // 1 hour
49-
#endif
5046
static size_t const CBOR_NOTE_MSG_MAX_SIZE = 255;
5147
static size_t const DEFAULT_READ_INTERVAL_MS = 1000; // 1 second
52-
static size_t const FAILSAFE_READ_INTERVAL_MS = 10000; // 10 seconds
48+
static size_t const FAILSAFE_READ_INTERVAL_MS = 15000; // 15 seconds
5349

5450
/******************************************************************************
5551
* LOCAL MODULE FUNCTIONS
@@ -76,10 +72,6 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
7672
,_message_stream(std::bind(&ArduinoIoTCloudNotecard::sendMessage, this, std::placeholders::_1))
7773
,_thing(&_message_stream)
7874
,_device(&_message_stream)
79-
#ifdef BACKOFF
80-
,_backoff_multiplier{1}
81-
,_last_failed_attach_request_ms{0}
82-
#endif
8375
,_notecard_last_read_ms{static_cast<uint32_t>(-DEFAULT_READ_INTERVAL_MS)}
8476
,_notecard_read_interval_ms{DEFAULT_READ_INTERVAL_MS}
8577
,_interrupt_pin{-1}
@@ -120,7 +112,6 @@ int ArduinoIoTCloudNotecard::begin(ConnectionHandler &connection_, int interrupt
120112
return 0; // (false -> failure)
121113
}
122114

123-
//TODO: Remove if not needed
124115
// Pull the Arduino IoT Cloud Device ID from the Notecard
125116
setDeviceId(notecard_connection->getDeviceId());
126117

@@ -153,7 +144,6 @@ void ArduinoIoTCloudNotecard::printDebugInfo()
153144
NetworkConnectionState conn_state = _connection->check();
154145
DEBUG_INFO("***** Arduino IoT Cloud Notecard - configuration info *****");
155146
DEBUG_INFO("Notecard UID: %s", reinterpret_cast<NotecardConnectionHandler *>(_connection)->getNotecardUid().c_str());
156-
//TODO: Remove if not needed
157147
DEBUG_INFO("Arduino Device ID: %s", getDeviceId().c_str());
158148
if (NetworkConnectionState::CONNECTED == conn_state)
159149
{
@@ -221,20 +211,6 @@ ArduinoIoTCloudNotecard::State ArduinoIoTCloudNotecard::handle_Connected()
221211
/* Poll Notecard for new messages */
222212
pollNotecard();
223213

224-
#ifdef BACKOFF
225-
/* Respect back-off */
226-
if (_last_failed_attach_request_ms) {
227-
const uint32_t backoff_ms = (BACKOFF_BASE_MS * _backoff_multiplier);
228-
if ((::millis() - _last_failed_attach_request_ms) < backoff_ms) {
229-
return State::Connected;
230-
} else {
231-
DEBUG_DEBUG("ArduinoIoTCloudNotecard::%s back-off expired.", __FUNCTION__);
232-
_backoff_multiplier <<= (BACKOFF_MAX_MS > backoff_ms);
233-
_last_failed_attach_request_ms = 0;
234-
}
235-
}
236-
#endif
237-
238214
/* Call CloudDevice process to get configuration */
239215
_device.update();
240216

@@ -401,23 +377,14 @@ void ArduinoIoTCloudNotecard::processCommand(const uint8_t *buf, size_t len)
401377
String new_thing_id = String(command.thingUpdateCmd.params.thing_id);
402378

403379
if (!new_thing_id.length()) {
404-
#ifdef BACKOFF
405-
_last_failed_attach_request_ms = ::millis();
406-
DEBUG_DEBUG("ArduinoIoTCloudNotecard::%s received null Thing ID (current back-off: %u minute%s).", __FUNCTION__, _backoff_multiplier, ((_backoff_multiplier == 1) ? "" : "s"));
407-
#endif
380+
DEBUG_DEBUG("ArduinoIoTCloudNotecard::%s received null Thing ID.", __FUNCTION__);
408381
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
409382

410383
/* Send message to device state machine to inform we have received a null thing-id */
411384
Message message;
412385
message = { DeviceRegisteredCmdId };
413386
_device.handleMessage(&message);
414387
} else {
415-
#ifdef BACKOFF
416-
DEBUG_VERBOSE("ArduinoIoTCloudNotecard::%s resetting back-off variables", __FUNCTION__);
417-
/* Reset back-off variables */
418-
_backoff_multiplier = 1;
419-
_last_failed_attach_request_ms = 0;
420-
#endif
421388
if (_device.isAttached() && _thing_id != new_thing_id) {
422389
DEBUG_DEBUG("ArduinoIoTCloudNotecard::%s detaching Thing ID: %s", __FUNCTION__, _thing_id.c_str());
423390
detachThing();

src/ArduinoIoTCloudNotecard.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ class ArduinoIoTCloudNotecard : public ArduinoIoTCloudClass
107107
ArduinoCloudDevice _device;
108108

109109
// Notecard member variables
110-
#ifdef BACKOFF
111-
uint32_t _backoff_multiplier;
112-
uint32_t _last_failed_attach_request_ms;
113-
#endif // BACKOFF
114110
uint32_t _notecard_last_read_ms;
115111
uint32_t _notecard_read_interval_ms;
116112
int _interrupt_pin;

0 commit comments

Comments
 (0)