mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 04:02:27 +00:00
esp_https_ota: Add support for partial image download
Fix issue where binary_file_length field is greater than original image length
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
|
||||
#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
|
||||
#define DEFAULT_REQUEST_SIZE (64 * 1024)
|
||||
static const char *TAG = "esp_https_ota";
|
||||
|
||||
typedef enum {
|
||||
@@ -39,8 +40,11 @@ struct esp_https_ota_handle {
|
||||
char *ota_upgrade_buf;
|
||||
size_t ota_upgrade_buf_size;
|
||||
int binary_file_len;
|
||||
int image_length;
|
||||
int max_http_request_size;
|
||||
esp_https_ota_state state;
|
||||
bool bulk_flash_erase;
|
||||
bool partial_http_download;
|
||||
};
|
||||
|
||||
typedef struct esp_https_ota_handle esp_https_ota_t;
|
||||
@@ -167,6 +171,9 @@ esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
https_ota_handle->partial_http_download = ota_config->partial_http_download;
|
||||
https_ota_handle->max_http_request_size = (ota_config->max_http_request_size == 0) ? DEFAULT_REQUEST_SIZE : ota_config->max_http_request_size;
|
||||
|
||||
/* Initiate HTTP Connection */
|
||||
https_ota_handle->http_client = esp_http_client_init(ota_config->http_config);
|
||||
if (https_ota_handle->http_client == NULL) {
|
||||
@@ -175,6 +182,30 @@ esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_
|
||||
goto failure;
|
||||
}
|
||||
|
||||
if (https_ota_handle->partial_http_download) {
|
||||
esp_http_client_set_method(https_ota_handle->http_client, HTTP_METHOD_HEAD);
|
||||
err = esp_http_client_perform(https_ota_handle->http_client);
|
||||
if (err != ESP_OK || esp_http_client_get_status_code(https_ota_handle->http_client) != HttpStatus_Ok) {
|
||||
ESP_LOGE(TAG, "Failed to get image length");
|
||||
goto http_cleanup;
|
||||
}
|
||||
https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client);
|
||||
esp_http_client_close(https_ota_handle->http_client);
|
||||
|
||||
if (https_ota_handle->image_length > https_ota_handle->max_http_request_size) {
|
||||
char *header_val = NULL;
|
||||
asprintf(&header_val, "bytes=0-%d", https_ota_handle->max_http_request_size - 1);
|
||||
if (header_val == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for HTTP header");
|
||||
err = ESP_ERR_NO_MEM;
|
||||
goto http_cleanup;
|
||||
}
|
||||
esp_http_client_set_header(https_ota_handle->http_client, "Range", header_val);
|
||||
free(header_val);
|
||||
}
|
||||
esp_http_client_set_method(https_ota_handle->http_client, HTTP_METHOD_GET);
|
||||
}
|
||||
|
||||
if (ota_config->http_client_init_cb) {
|
||||
err = ota_config->http_client_init_cb(https_ota_handle->http_client);
|
||||
if (err != ESP_OK) {
|
||||
@@ -243,7 +274,7 @@ esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, es
|
||||
* 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)) {
|
||||
while (data_read_size > 0 && !esp_http_client_is_complete_data_received(handle->http_client)) {
|
||||
data_read = esp_http_client_read(handle->http_client,
|
||||
(handle->ota_upgrade_buf + bytes_read),
|
||||
data_read_size);
|
||||
@@ -294,7 +325,13 @@ esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
|
||||
then the image data read there should be written to OTA partition
|
||||
*/
|
||||
if (handle->binary_file_len) {
|
||||
return _ota_write(handle, (const void *)handle->ota_upgrade_buf, handle->binary_file_len);
|
||||
/*
|
||||
* Header length gets added to handle->binary_file_len in _ota_write
|
||||
* Clear handle->binary_file_len to avoid additional 289 bytes in binary_file_len
|
||||
*/
|
||||
int binary_file_len = handle->binary_file_len;
|
||||
handle->binary_file_len = 0;
|
||||
return _ota_write(handle, (const void *)handle->ota_upgrade_buf, binary_file_len);
|
||||
}
|
||||
/* falls through */
|
||||
case ESP_HTTPS_OTA_IN_PROGRESS:
|
||||
@@ -303,10 +340,10 @@ esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
|
||||
handle->ota_upgrade_buf_size);
|
||||
if (data_read == 0) {
|
||||
/*
|
||||
* esp_https_ota_is_complete_data_received is added to check whether
|
||||
* esp_http_client_is_complete_data_received is added to check whether
|
||||
* complete image is received.
|
||||
*/
|
||||
bool is_recv_complete = esp_https_ota_is_complete_data_received(https_ota_handle);
|
||||
bool is_recv_complete = esp_http_client_is_complete_data_received(handle->http_client);
|
||||
/*
|
||||
* As esp_http_client_read doesn't return negative error code if select fails, we rely on
|
||||
* `errno` to check for underlying transport connectivity closure if any.
|
||||
@@ -326,20 +363,51 @@ esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
|
||||
} else {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
handle->state = ESP_HTTPS_OTA_SUCCESS;
|
||||
if (!handle->partial_http_download || (handle->partial_http_download && handle->image_length == handle->binary_file_len)) {
|
||||
handle->state = ESP_HTTPS_OTA_SUCCESS;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
|
||||
return ESP_FAIL;
|
||||
break;
|
||||
}
|
||||
if (handle->partial_http_download) {
|
||||
if (handle->state == ESP_HTTPS_OTA_IN_PROGRESS && handle->image_length > handle->binary_file_len) {
|
||||
esp_http_client_close(handle->http_client);
|
||||
char *header_val = NULL;
|
||||
if ((handle->image_length - handle->binary_file_len) > handle->max_http_request_size) {
|
||||
asprintf(&header_val, "bytes=%d-%d", handle->binary_file_len, (handle->binary_file_len + handle->max_http_request_size - 1));
|
||||
} else {
|
||||
asprintf(&header_val, "bytes=%d-", handle->binary_file_len);
|
||||
}
|
||||
if (header_val == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for HTTP header");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
esp_http_client_set_header(handle->http_client, "Range", header_val);
|
||||
free(header_val);
|
||||
err = _http_connect(handle->http_client);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to establish HTTP connection");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle)
|
||||
{
|
||||
bool ret = false;
|
||||
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
|
||||
return esp_http_client_is_complete_data_received(handle->http_client);
|
||||
if (handle->partial_http_download) {
|
||||
ret = (handle->image_length == handle->binary_file_len);
|
||||
} else {
|
||||
ret = esp_http_client_is_complete_data_received(handle->http_client);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
|
||||
|
Reference in New Issue
Block a user