Skip to content

Commit

Permalink
checking _ota_req when the FSM state is between SubscribeDeviceTopic …
Browse files Browse the repository at this point in the history
…and Connected
  • Loading branch information
andreagilardoni committed Feb 5, 2024
1 parent d436b80 commit 4fc74e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
67 changes: 37 additions & 30 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ void ArduinoIoTCloudTCP::update()
}
_state = next_state;

#if OTA_ENABLED
if(_state > State::SubscribeDeviceTopic && _state <= State::Connected) {
checkOTAreq();
}
#endif /* OTA_ENABLED */

/* This watchdog feed is actually needed only by the RP2040 Connect because its
* maximum watchdog window is 8389 ms; despite this we feed it for all
* supported ARCH to keep code aligned.
Expand Down Expand Up @@ -568,36 +574,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
_mqtt_data_request_retransmit = false;
}

#if OTA_ENABLED
/* Request a OTA download if the hidden property
* OTA request has been set.
*/

if (_ota_req)
{
bool const ota_execution_allowed_by_user = (_get_ota_confirmation != nullptr && _get_ota_confirmation());
bool const perform_ota_now = ota_execution_allowed_by_user || !_ask_user_before_executing_ota;
if (perform_ota_now) {
/* Clear the error flag. */
_ota_error = static_cast<int>(OTAError::None);
/* Clear the request flag. */
_ota_req = false;
/* Transmit the cleared request flags to the cloud. */
sendDevicePropertyToCloud("OTA_REQ");
/* Call member function to handle OTA request. */
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
/* If something fails send the OTA error to the cloud */
sendDevicePropertyToCloud("OTA_ERROR");
}
}

/* Check if we have received the OTA_URL property and provide
* echo to the cloud.
*/
sendDevicePropertyToCloud("OTA_URL");

#endif /* OTA_ENABLED */

/* Check if any properties need encoding and send them to
* the cloud if necessary.
*/
Expand All @@ -612,6 +588,37 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
}
}

#if OTA_ENABLED
void ArduinoIoTCloudTCP::checkOTAreq() {
/* Request a OTA download if the hidden property
* OTA request has been set.
*/

if (_ota_req)
{
bool const ota_execution_allowed_by_user = (_get_ota_confirmation != nullptr && _get_ota_confirmation());
bool const perform_ota_now = ota_execution_allowed_by_user || !_ask_user_before_executing_ota;
if (perform_ota_now) {
/* Clear the error flag. */
_ota_error = static_cast<int>(OTAError::None);
/* Clear the request flag. */
_ota_req = false;
/* Transmit the cleared request flags to the cloud. */
sendDevicePropertyToCloud("OTA_REQ");
/* Call member function to handle OTA request. */
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
/* If something fails send the OTA error to the cloud */
sendDevicePropertyToCloud("OTA_ERROR");
}
}

/* Check if we have received the OTA_URL property and provide
* echo to the cloud.
*/
sendDevicePropertyToCloud("OTA_URL");
}
#endif /* OTA_ENABLED */

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
Expand Down
2 changes: 2 additions & 0 deletions src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
_get_ota_confirmation = cb;
_ask_user_before_executing_ota = true;
}

void checkOTAreq();
#endif

private:
Expand Down

0 comments on commit 4fc74e9

Please sign in to comment.