Skip to content

Commit

Permalink
esp-idf OTA: fix for invalid HTML header data in GET request. Log
Browse files Browse the repository at this point in the history
improvement.
  • Loading branch information
klew committed Dec 13, 2024
1 parent 36f1117 commit 39df646
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions extras/porting/esp-idf/esp_idf_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,27 @@ void Supla::EspIdfOta::iterate() {
configCheckUpdate.url = url;
configCheckUpdate.timeout_ms = 5000;

if (client) {
esp_http_client_cleanup(client);
}
client = esp_http_client_init(&configCheckUpdate);
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client,
"Content-Type",
"application/x-www-form-urlencoded");
if (client == NULL) {
fail("SW update: failed initialize connection with update server");
return;
}

esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client,
"Content-Type",
"application/x-www-form-urlencoded");
esp_err_t err;
err = esp_http_client_open(client, querySize);
if (err != ESP_OK) {
fail("SW update: failed to open connection with update server");
return;
}
esp_http_client_write(client, queryParams, querySize);

esp_http_client_write(client, queryParams, querySize);
esp_http_client_fetch_headers(client);

// Start fetching bin file and perform update
Expand Down Expand Up @@ -256,6 +260,10 @@ void Supla::EspIdfOta::iterate() {
SUPLA_LOG_INFO("%s", buf);
log(buf);
}

esp_http_client_cleanup(client);
client = 0;

if (cJSON_IsObject(latestUpdate)) {
cJSON *version = cJSON_GetObjectItemCaseSensitive(latestUpdate, "version");
cJSON *url = cJSON_GetObjectItemCaseSensitive(latestUpdate, "updateUrl");
Expand All @@ -268,7 +276,16 @@ void Supla::EspIdfOta::iterate() {
snprintf(buf, BUF_SIZE, "SW update url: \"%s\"", url->valuestring);
SUPLA_LOG_INFO("%s", buf);
log(buf);
esp_http_client_set_url(client, url->valuestring);

esp_http_client_config_t configGet = {};
configGet.url = url->valuestring;
configGet.timeout_ms = 10000;
client = esp_http_client_init(&configGet);
if (client == NULL) {
fail("SW update: failed initialize GET connection with update server");
return;
}
esp_http_client_set_method(client, HTTP_METHOD_GET);
} else {
fail("SW update: no url and version received - finishing");
return;
Expand All @@ -278,7 +295,6 @@ void Supla::EspIdfOta::iterate() {
return;
}

esp_http_client_set_method(client, HTTP_METHOD_GET);
err = esp_http_client_open(client, 0);
cJSON_Delete(json);
if (err != ESP_OK) {
Expand Down Expand Up @@ -320,13 +336,20 @@ void Supla::EspIdfOta::iterate() {
}

SUPLA_LOG_DEBUG("Getting file from server...");
int bytesRead = 0;
int bytesReadPrinted = 0;
while (true) {
int dataRead = esp_http_client_read(
client, reinterpret_cast<char *>(otaBuffer), BUFFER_SIZE);
if (dataRead < 0) {
fail("SW update: data read error");
return;
} else if (dataRead > 0) {
bytesRead += dataRead;
if (bytesRead - bytesReadPrinted > 1024 * 100) {
bytesReadPrinted = bytesRead;
SUPLA_LOG_DEBUG("SW update: downloaded %d bytes...", bytesRead);
}
err = esp_ota_write(updateHandle, (const void *)otaBuffer, dataRead);
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
fail("SW update: image corrupted - invalid magic byte");
Expand Down

0 comments on commit 39df646

Please sign in to comment.