Skip to content

Commit a463120

Browse files
committed
Moving OTA state machine transitions outside of State::Connected handler - they don't need to be connected to perform the initial steps
1 parent b673b59 commit a463120

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/ArduinoIoTCloudTCP.cpp

+20-17
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
166166

167167
void ArduinoIoTCloudTCP::update()
168168
{
169-
/* Retrieve the latest data from the MQTT Client. */
170-
if (_mqttClient.connected())
171-
_mqttClient.poll();
172-
173169
/* Run through the state machine. */
174170
State next_state = _state;
175171
switch (_state)
@@ -182,6 +178,19 @@ void ArduinoIoTCloudTCP::update()
182178
case State::Connected: next_state = handle_Connected(); break;
183179
}
184180
_state = next_state;
181+
182+
/* Check for new data from the MQTT client. */
183+
if (_mqttClient.connected())
184+
_mqttClient.poll();
185+
186+
#if OTA_ENABLED
187+
/* If a _ota_logic object has been instantiated then we are spinning its
188+
* 'update' method here in order to process incoming data and generally
189+
* to transition to the OTA logic update states.
190+
*/
191+
OTAError const err = _ota_logic.update();
192+
_ota_error = static_cast<int>(err);
193+
#endif /* OTA_ENABLED */
185194
}
186195

187196
int ArduinoIoTCloudTCP::connected()
@@ -231,7 +240,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
231240
return State::SubscribeMqttTopics;
232241

233242
DBG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
234-
return State::ConnectMqttBroker;
243+
return State::ConnectPhy;
235244
}
236245

237246
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
@@ -290,28 +299,22 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
290299
{
291300
if (!_mqttClient.connected())
292301
{
302+
DBG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
303+
304+
/* Forcefully disconnect MQTT client and trigger a reconnection. */
305+
_mqttClient.stop();
306+
293307
/* The last message was definitely lost, trigger a retransmit. */
294308
_mqtt_data_request_retransmit = true;
295309

296310
/* We are not connected anymore, trigger the callback for a disconnected event. */
297311
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
298312

299-
/* Forcefully disconnect MQTT client and trigger a reconnection. */
300-
_mqttClient.stop();
301-
return State::ConnectMqttBroker;
313+
return State::ConnectPhy;
302314
}
303315
/* We are connected so let's to our stuff here. */
304316
else
305317
{
306-
#if OTA_ENABLED
307-
/* If a _ota_logic object has been instantiated then we are spinning its
308-
* 'update' method here in order to process incoming data and generally
309-
* to transition to the OTA logic update states.
310-
*/
311-
OTAError const err = _ota_logic.update();
312-
_ota_error = static_cast<int>(err);
313-
#endif /* OTA_ENABLED */
314-
315318
/* Check if a primitive property wrapper is locally changed.
316319
* This function requires an existing time service which in
317320
* turn requires an established connection. Not having that

0 commit comments

Comments
 (0)