42
42
* CONSTANTS
43
43
******************************************************************************/
44
44
45
+ static size_t const BACKOFF_BASE_MS = 60000 ; // 1 minute
46
+ static uint32_t const BACKOFF_MAX_MS = 3600000 ; // 1 hour
45
47
static size_t const CBOR_NOTE_MSG_MAX_SIZE = 255 ;
46
- static size_t const DEFAULT_READ_INTERVAL_MS = 1000 ;
47
- static size_t const FAILSAFE_READ_INTERVAL_MS = 10000 ;
48
+ static size_t const DEFAULT_READ_INTERVAL_MS = 1000 ; // 1 second
49
+ static size_t const FAILSAFE_READ_INTERVAL_MS = 10000 ; // 10 seconds
48
50
49
51
/* *****************************************************************************
50
52
* LOCAL MODULE FUNCTIONS
@@ -71,6 +73,8 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
71
73
,_message_stream(std::bind(&ArduinoIoTCloudNotecard::sendMessage, this , std::placeholders::_1))
72
74
,_thing(&_message_stream)
73
75
,_device(&_message_stream)
76
+ ,_backoff_multiplier{1 }
77
+ ,_last_failed_attach_request_ms{0 }
74
78
,_notecard_last_read_ms{static_cast <uint32_t >(-DEFAULT_READ_INTERVAL_MS)}
75
79
,_notecard_read_interval_ms{DEFAULT_READ_INTERVAL_MS}
76
80
,_interrupt_pin{-1 }
@@ -231,6 +235,18 @@ ArduinoIoTCloudNotecard::State ArduinoIoTCloudNotecard::handle_Connected()
231
235
/* Poll Notecard for new messages */
232
236
pollNotecard ();
233
237
238
+ /* Respect back-off */
239
+ if (_last_failed_attach_request_ms) {
240
+ const uint32_t backoff_ms = (BACKOFF_BASE_MS * _backoff_multiplier);
241
+ if ((::millis () - _last_failed_attach_request_ms) < backoff_ms) {
242
+ return State::Connected;
243
+ } else {
244
+ DEBUG_DEBUG (" ArduinoIoTCloudNotecard::%s back-off expired." , __FUNCTION__);
245
+ _backoff_multiplier <<= (BACKOFF_MAX_MS > backoff_ms);
246
+ _last_failed_attach_request_ms = 0 ;
247
+ }
248
+ }
249
+
234
250
/* Call CloudDevice process to get configuration */
235
251
_device.update ();
236
252
@@ -397,16 +413,26 @@ void ArduinoIoTCloudNotecard::processCommand(const uint8_t *buf, size_t len)
397
413
String new_thing_id = String (command.thingUpdateCmd .params .thing_id );
398
414
399
415
if (!new_thing_id.length ()) {
400
- /* Send message to device state machine to inform we have received a null thing-id */
416
+ _last_failed_attach_request_ms = ::millis ();
417
+ DEBUG_DEBUG (" ArduinoIoTCloudNotecard::%s received null Thing ID (current back-off: %u minute%s)." , __FUNCTION__, _backoff_multiplier, ((_backoff_multiplier == 1 ) ? " " : " s" ));
401
418
_thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
419
+
420
+ /* Send message to device state machine to inform we have received a null thing-id */
402
421
Message message;
403
422
message = { DeviceRegisteredCmdId };
404
423
_device.handleMessage (&message);
405
424
} else {
425
+ DEBUG_VERBOSE (" ArduinoIoTCloudNotecard::%s resetting back-off variables" , __FUNCTION__);
426
+ /* Reset back-off variables */
427
+ _backoff_multiplier = 1 ;
428
+ _last_failed_attach_request_ms = 0 ;
429
+
406
430
if (_device.isAttached () && _thing_id != new_thing_id) {
431
+ DEBUG_DEBUG (" ArduinoIoTCloudNotecard::%s detaching Thing ID: %s" , __FUNCTION__, _thing_id.c_str ());
407
432
detachThing ();
408
433
}
409
434
if (!_device.isAttached ()) {
435
+ DEBUG_DEBUG (" ArduinoIoTCloudNotecard::%s attaching Thing ID: %s" , __FUNCTION__, new_thing_id.c_str ());
410
436
attachThing (new_thing_id);
411
437
}
412
438
}
@@ -441,10 +467,11 @@ void ArduinoIoTCloudNotecard::processCommand(const uint8_t *buf, size_t len)
441
467
execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
442
468
443
469
/*
444
- * NOTE: in this current version properties are not properly integrated with the new paradigm of
445
- * modeling the messages with C structs. The current CBOR library allocates an array in the heap
446
- * thus we need to delete it after decoding it with the old CBORDecoder
447
- */
470
+ * NOTE: In this current version properties are not properly integrated
471
+ * with the new paradigm of modeling the messages with C structs. The
472
+ * current CBOR library allocates an array in the heap thus we need to
473
+ * delete it after decoding it with the old CBORDecoder
474
+ */
448
475
free (command.lastValuesUpdateCmd .params .last_values );
449
476
}
450
477
break ;
0 commit comments