Skip to content

Commit

Permalink
refactor(OTA): use advanced APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jun 19, 2024
1 parent 74388a0 commit 7c4763f
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/OTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,61 @@ namespace ESPAdmin

void OTA::task(void *)
{
esp_http_client_config_t config = {

esp_http_client_config_t httpConfig = {
.url = _downloadURL.c_str(),
.cert_pem = Store::options.httpCert,
.timeout_ms = Store::options.httpTimeoutMs,
};

Update::onChange(UPDATE_STARTED);
esp_https_ota_config_t otaConfig = {
.http_config = &httpConfig,
};

Store::updateRunning = true;
Update::onChange(UPDATE_STARTED);

esp_https_ota_handle_t otaHandle;

esp_err_t ret = esp_https_ota(&config);
esp_err_t ret = esp_https_ota_begin(&otaConfig, &otaHandle);

if (ret == ESP_OK)
if (ret != ESP_OK)
{
Store::updateRunning = false;
Update::onChange(UPDATE_SUCCEDED);
Update::onChange(UPDATE_FAILED);
}
else

while (Store::updateRunning)
{
Store::updateRunning = false;
Update::onChange(UPDATE_FAILED);
esp_err_t ret = esp_https_ota_perform(otaHandle);

if (ret == ESP_ERR_HTTPS_OTA_IN_PROGRESS)
{
continue;
}
else if (ret == ESP_OK)
{
Store::updateRunning = false;

bool completed = esp_https_ota_is_complete_data_received(otaHandle);

if (completed)
{
Update::onChange(UPDATE_SUCCEDED);
}
else
{
Update::onChange(UPDATE_FAILED);
}
}
else
{
Store::updateRunning = false;
Update::onChange(UPDATE_FAILED);
}
}

esp_https_ota_finish(otaHandle);
vTaskDelete(nullptr);
}
}

0 comments on commit 7c4763f

Please sign in to comment.