Skip to content

OTA: making it possible to perform ota without the need of the thing ID #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ void ArduinoIoTCloudTCP::update()
}
_state = next_state;

#if OTA_ENABLED
if (_state > State::SubscribeDeviceTopic && _state <= State::Connected) {
handle_OTARequest();
}
#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 @@ -569,36 +575,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 @@ -613,6 +589,37 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
}
}

#if OTA_ENABLED
void ArduinoIoTCloudTCP::handle_OTARequest() {
/* 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 handle_OTARequest();
#endif

private:
Expand Down