Skip to content

Commit 68e3d97

Browse files
committed
Simplify Attach/Detach timeout logic
1 parent 16ad787 commit 68e3d97

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/AIoTC_Config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@
139139

140140
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
141141
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
142-
#define AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (5*1000UL)
142+
#define AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (2000UL)
143143
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (32000UL)
144144
#define AIOT_CONFIG_THING_TOPICS_SUBSCRIBE_RETRY_DELAY_ms (1000UL)
145145
#define AIOT_CONFIG_THING_TOPICS_SUBSCRIBE_MAX_RETRY_CNT (10UL)
146+
#define AIOT_CONFIG_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms (20000UL)
146147
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms (1280000UL)
147148
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
148149
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)

src/ArduinoIoTCloudTCP.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
362362
}
363363

364364
sendDevicePropertiesToCloud();
365-
return State::SubscribeDeviceTopic;
365+
return State::WaitDeviceConfig;
366366
}
367367

368368
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
@@ -384,11 +384,10 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
384384
return State::Disconnect;
385385
}
386386

387-
/* No device configuration reply. Wait: 5s -> 10s -> 20s -> 30s */
387+
_last_device_subscribe_cnt++;
388388
unsigned long subscribe_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
389389
subscribe_retry_delay = min(subscribe_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms));
390390
_next_device_subscribe_attempt_tick = millis() + subscribe_retry_delay;
391-
_last_device_subscribe_cnt++;
392391

393392
return State::WaitDeviceConfig;
394393
}
@@ -400,20 +399,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
400399
return State::Disconnect;
401400
}
402401

403-
if (_thing_id_property->isDifferentFromCloud())
404-
{
405-
return State::CheckDeviceConfig;
406-
}
407-
408-
if (millis() > _next_device_subscribe_attempt_tick)
402+
bool const is_retry_attempt = (_last_device_subscribe_cnt > 0);
403+
if (is_retry_attempt && (millis() > _next_device_subscribe_attempt_tick))
409404
{
410405
/* Configuration not received or device not attached to a valid thing. Try to resubscribe */
411406
if (_mqttClient.unsubscribe(_deviceTopicIn))
412407
{
413408
DEBUG_ERROR("ArduinoIoTCloudTCP::%s device waiting for valid thing_id", __FUNCTION__);
414-
return State::SubscribeDeviceTopic;
415409
}
416410
}
411+
412+
if (!is_retry_attempt || (is_retry_attempt && (millis() > _next_device_subscribe_attempt_tick)))
413+
return State::SubscribeDeviceTopic;
414+
417415
return State::WaitDeviceConfig;
418416
}
419417

@@ -428,16 +426,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
428426

429427
if (_thing_id.length() == 0)
430428
{
431-
/* Configuration received but device not attached. Wait: 40s */
432-
unsigned long attach_retry_delay = (1 << _last_device_attach_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
429+
/* Device configuration received, but invalid thing_id. Do not increase counter, but recompute delay.
430+
* Device not attached. Wait: 40s -> 80s -> 160s -> 320s -> 640s -> 1280s -> 1280s ...
431+
*/
432+
unsigned long attach_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms;
433433
attach_retry_delay = min(attach_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms));
434434
_next_device_subscribe_attempt_tick = millis() + attach_retry_delay;
435-
_last_device_attach_cnt++;
436435
return State::WaitDeviceConfig;
437436
}
438437

439438
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s Device attached to a new valid Thing %s", __FUNCTION__, getThingId().c_str());
440-
_last_device_attach_cnt = 0;
439+
440+
/* Received valid thing_id reset counters and go on */
441+
_last_device_subscribe_cnt = 0;
441442

442443
return State::SubscribeThingTopics;
443444
}
@@ -652,8 +653,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
652653
/* Topic for OTA properties and device configuration */
653654
if (_deviceTopicIn == topic) {
654655
CBORDecoder::decode(_device_property_container, (uint8_t*)bytes, length);
655-
_last_device_subscribe_cnt = 0;
656-
_next_device_subscribe_attempt_tick = 0;
656+
_state = State::CheckDeviceConfig;
657657
}
658658

659659
/* Topic for user input data */

src/ArduinoIoTCloudTCP.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
132132
unsigned int _last_connection_attempt_cnt;
133133
unsigned long _next_device_subscribe_attempt_tick;
134134
unsigned int _last_device_subscribe_cnt;
135-
unsigned int _last_device_attach_cnt;
136135
unsigned long _last_sync_request_tick;
137136
unsigned int _last_sync_request_cnt;
138137
unsigned long _last_subscribe_request_tick;

0 commit comments

Comments
 (0)