From 6e3408c7670ea05b3b990a3c22858d1aa739aa1d Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 3 May 2021 07:54:25 +0200 Subject: [PATCH] Track the number of unsuccessful request-last-values. In the current implementation the number the board would continously attempt to obtain the last values from the cloud. If, however, those values never arrive - then the board would never move on and become operational. It's therefore better to count the number of failed attempts and reset the connection to allow a clean re-connection from the ground up. --- src/AIoTC_Config.h | 1 + src/ArduinoIoTCloudTCP.cpp | 14 ++++++++++++++ src/ArduinoIoTCloudTCP.h | 1 + 3 files changed, 16 insertions(+) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 7c76d4486..7c54d1751 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -141,5 +141,6 @@ #define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL) #define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL) #define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL) +#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL) #endif /* ARDUINO_AIOTC_CONFIG_H_ */ diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 86b4bca3b..54d416596 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -71,6 +71,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() , _next_connection_attempt_tick{0} , _last_connection_attempt_cnt{0} , _last_sync_request_tick{0} +, _last_sync_request_cnt{0} , _mqtt_data_buf{0} , _mqtt_data_len{0} , _mqtt_data_request_retransmit{false} @@ -424,6 +425,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues() DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now); requestLastValue(); _last_sync_request_tick = now; + /* Track the number of times a get-last-values request was sent to the cloud. + * If no data is received within a certain number of retry-requests it's a better + * strategy to disconnect and re-establish connection from the ground up. + */ + _last_sync_request_cnt++; + if (_last_sync_request_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT) + { + _last_sync_request_cnt = 0; + _mqttClient.stop(); + execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT); + return State::ConnectPhy; + } } return State::RequestLastValues; @@ -517,6 +530,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length) CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true); sendPropertiesToCloud(); execCloudEventCallback(ArduinoIoTCloudEvent::SYNC); + _last_sync_request_cnt = 0; _state = State::Connected; } } diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 364b4bf36..f29aafde7 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -99,6 +99,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass unsigned long _next_connection_attempt_tick; unsigned int _last_connection_attempt_cnt; unsigned long _last_sync_request_tick; + unsigned int _last_sync_request_cnt; String _brokerAddress; uint16_t _brokerPort; uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE];