mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 04:02:27 +00:00
Bugfix for failing OTA example
example_test.py is added to test advanced_https_ota_example and native ota_example. Closes https://github.com/espressif/esp-idf/issues/4394
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <esp_https_ota.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_ota_ops.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define IMAGE_HEADER_SIZE sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1
|
||||
#define DEFAULT_OTA_BUF_SIZE IMAGE_HEADER_SIZE
|
||||
@@ -70,15 +71,27 @@ static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client
|
||||
}
|
||||
|
||||
char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE];
|
||||
/*
|
||||
* `data_read_size` holds number of bytes to be read.
|
||||
* `bytes_read` holds number of bytes read.
|
||||
*/
|
||||
int bytes_read = 0, data_read_size = DEFAULT_OTA_BUF_SIZE;
|
||||
if (process_again(status_code)) {
|
||||
while (1) {
|
||||
int data_read = esp_http_client_read(http_client, upgrade_data_buf, DEFAULT_OTA_BUF_SIZE);
|
||||
if (data_read < 0) {
|
||||
ESP_LOGE(TAG, "Error: SSL data read error");
|
||||
return ESP_FAIL;
|
||||
} else if (data_read == 0) {
|
||||
return ESP_OK;
|
||||
while (data_read_size > 0) {
|
||||
int data_read = esp_http_client_read(http_client, (upgrade_data_buf + bytes_read), data_read_size);
|
||||
/*
|
||||
* As esp_http_client_read never returns negative error code, we rely on
|
||||
* `errno` to check for underlying transport connectivity closure if any
|
||||
*/
|
||||
if (errno == ENOTCONN || errno == ECONNRESET) {
|
||||
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
||||
break;
|
||||
}
|
||||
bytes_read += data_read;
|
||||
data_read_size -= data_read;
|
||||
}
|
||||
if (data_read_size > 0) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
@@ -215,19 +228,37 @@ esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, es
|
||||
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
/*
|
||||
* `data_read_size` holds number of bytes needed to read complete header.
|
||||
* `bytes_read` holds number of bytes read.
|
||||
*/
|
||||
int data_read_size = IMAGE_HEADER_SIZE;
|
||||
int data_read = esp_http_client_read(handle->http_client,
|
||||
handle->ota_upgrade_buf,
|
||||
data_read_size);
|
||||
if (data_read < 0) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (data_read >= sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
|
||||
memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
||||
handle->binary_file_len += data_read;
|
||||
} else {
|
||||
int data_read = 0, bytes_read = 0;
|
||||
/*
|
||||
* while loop is added to download complete image headers, even if the headers
|
||||
* are not sent in a single packet.
|
||||
*/
|
||||
while (data_read_size > 0 && !esp_https_ota_is_complete_data_received(https_ota_handle)) {
|
||||
data_read = esp_http_client_read(handle->http_client,
|
||||
(handle->ota_upgrade_buf + bytes_read),
|
||||
data_read_size);
|
||||
/*
|
||||
* As esp_http_client_read never returns negative error code, we rely on
|
||||
* `errno` to check for underlying transport connectivity closure if any
|
||||
*/
|
||||
if (errno == ENOTCONN || errno == ECONNRESET) {
|
||||
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
||||
break;
|
||||
}
|
||||
data_read_size -= data_read;
|
||||
bytes_read += data_read;
|
||||
}
|
||||
if (data_read_size > 0) {
|
||||
ESP_LOGE(TAG, "Complete headers were not received");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
handle->binary_file_len = bytes_read;
|
||||
memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -265,10 +296,21 @@ esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
|
||||
handle->ota_upgrade_buf,
|
||||
handle->ota_upgrade_buf_size);
|
||||
if (data_read == 0) {
|
||||
/*
|
||||
* As esp_http_client_read never returns negative error code, we rely on
|
||||
* `errno` to check for underlying transport connectivity closure if any
|
||||
*/
|
||||
if (errno == ENOTCONN || errno == ECONNRESET) {
|
||||
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
/* esp_https_ota_is_complete_data_received is added to check whether
|
||||
complete image is received.
|
||||
*/
|
||||
if (!esp_https_ota_is_complete_data_received(https_ota_handle)) {
|
||||
return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
|
||||
}
|
||||
ESP_LOGI(TAG, "Connection closed");
|
||||
} else if (data_read < 0) {
|
||||
ESP_LOGE(TAG, "Error: SSL data read error");
|
||||
return ESP_FAIL;
|
||||
} else if (data_read > 0) {
|
||||
return _ota_write(handle, (const void *)handle->ota_upgrade_buf, data_read);
|
||||
}
|
||||
|
Reference in New Issue
Block a user