Skip to content

Commit 94aa399

Browse files
authored
Merge pull request #433 from pennam/ota_req_new
OTA: making it possible to perform ota without the need of the thing ID
2 parents b0e8700 + dca9968 commit 94aa399

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

src/ArduinoIoTCloudTCP.cpp

+37-30
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ void ArduinoIoTCloudTCP::update()
271271
}
272272
_state = next_state;
273273

274+
#if OTA_ENABLED
275+
if (_state > State::SubscribeDeviceTopic && _state <= State::Connected) {
276+
handle_OTARequest();
277+
}
278+
#endif /* OTA_ENABLED */
279+
274280
/* This watchdog feed is actually needed only by the RP2040 Connect because its
275281
* maximum watchdog window is 8389 ms; despite this we feed it for all
276282
* supported ARCH to keep code aligned.
@@ -569,36 +575,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
569575
_mqtt_data_request_retransmit = false;
570576
}
571577

572-
#if OTA_ENABLED
573-
/* Request a OTA download if the hidden property
574-
* OTA request has been set.
575-
*/
576-
577-
if (_ota_req)
578-
{
579-
bool const ota_execution_allowed_by_user = (_get_ota_confirmation != nullptr && _get_ota_confirmation());
580-
bool const perform_ota_now = ota_execution_allowed_by_user || !_ask_user_before_executing_ota;
581-
if (perform_ota_now) {
582-
/* Clear the error flag. */
583-
_ota_error = static_cast<int>(OTAError::None);
584-
/* Clear the request flag. */
585-
_ota_req = false;
586-
/* Transmit the cleared request flags to the cloud. */
587-
sendDevicePropertyToCloud("OTA_REQ");
588-
/* Call member function to handle OTA request. */
589-
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
590-
/* If something fails send the OTA error to the cloud */
591-
sendDevicePropertyToCloud("OTA_ERROR");
592-
}
593-
}
594-
595-
/* Check if we have received the OTA_URL property and provide
596-
* echo to the cloud.
597-
*/
598-
sendDevicePropertyToCloud("OTA_URL");
599-
600-
#endif /* OTA_ENABLED */
601-
602578
/* Check if any properties need encoding and send them to
603579
* the cloud if necessary.
604580
*/
@@ -613,6 +589,37 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
613589
}
614590
}
615591

592+
#if OTA_ENABLED
593+
void ArduinoIoTCloudTCP::handle_OTARequest() {
594+
/* Request a OTA download if the hidden property
595+
* OTA request has been set.
596+
*/
597+
598+
if (_ota_req)
599+
{
600+
bool const ota_execution_allowed_by_user = (_get_ota_confirmation != nullptr && _get_ota_confirmation());
601+
bool const perform_ota_now = ota_execution_allowed_by_user || !_ask_user_before_executing_ota;
602+
if (perform_ota_now) {
603+
/* Clear the error flag. */
604+
_ota_error = static_cast<int>(OTAError::None);
605+
/* Clear the request flag. */
606+
_ota_req = false;
607+
/* Transmit the cleared request flags to the cloud. */
608+
sendDevicePropertyToCloud("OTA_REQ");
609+
/* Call member function to handle OTA request. */
610+
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
611+
/* If something fails send the OTA error to the cloud */
612+
sendDevicePropertyToCloud("OTA_ERROR");
613+
}
614+
}
615+
616+
/* Check if we have received the OTA_URL property and provide
617+
* echo to the cloud.
618+
*/
619+
sendDevicePropertyToCloud("OTA_URL");
620+
}
621+
#endif /* OTA_ENABLED */
622+
616623
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
617624
{
618625
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);

src/ArduinoIoTCloudTCP.h

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
103103
_get_ota_confirmation = cb;
104104
_ask_user_before_executing_ota = true;
105105
}
106+
107+
void handle_OTARequest();
106108
#endif
107109

108110
private:

0 commit comments

Comments
 (0)