2018-07-12 12:15:44 +00:00
|
|
|
// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <esp_https_ota.h>
|
|
|
|
#include <esp_log.h>
|
2019-10-14 20:56:21 +00:00
|
|
|
#include <esp_ota_ops.h>
|
2019-12-09 14:23:05 +00:00
|
|
|
#include <errno.h>
|
2018-07-12 12:15:44 +00:00
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
#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
|
2018-07-12 12:15:44 +00:00
|
|
|
static const char *TAG = "esp_https_ota";
|
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
typedef enum {
|
|
|
|
ESP_HTTPS_OTA_INIT,
|
|
|
|
ESP_HTTPS_OTA_BEGIN,
|
|
|
|
ESP_HTTPS_OTA_IN_PROGRESS,
|
|
|
|
ESP_HTTPS_OTA_SUCCESS,
|
|
|
|
} esp_https_ota_state;
|
|
|
|
|
|
|
|
struct esp_https_ota_handle {
|
|
|
|
esp_ota_handle_t update_handle;
|
|
|
|
const esp_partition_t *update_partition;
|
|
|
|
esp_http_client_handle_t http_client;
|
|
|
|
char *ota_upgrade_buf;
|
|
|
|
size_t ota_upgrade_buf_size;
|
|
|
|
int binary_file_len;
|
|
|
|
esp_https_ota_state state;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct esp_https_ota_handle esp_https_ota_t;
|
|
|
|
|
|
|
|
static bool process_again(int status_code)
|
|
|
|
{
|
|
|
|
switch (status_code) {
|
|
|
|
case HttpStatus_MovedPermanently:
|
|
|
|
case HttpStatus_Found:
|
|
|
|
case HttpStatus_Unauthorized:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code)
|
|
|
|
{
|
|
|
|
esp_err_t err;
|
|
|
|
if (status_code == HttpStatus_MovedPermanently || status_code == HttpStatus_Found) {
|
|
|
|
err = esp_http_client_set_redirection(http_client);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "URL redirection Failed");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else if (status_code == HttpStatus_Unauthorized) {
|
|
|
|
esp_http_client_add_auth(http_client);
|
|
|
|
}
|
|
|
|
|
|
|
|
char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE];
|
2020-02-13 08:13:58 +00:00
|
|
|
// process_again() returns true only in case of redirection.
|
2019-04-03 14:25:27 +00:00
|
|
|
if (process_again(status_code)) {
|
2020-02-13 08:13:58 +00:00
|
|
|
while (1) {
|
2019-12-09 14:23:05 +00:00
|
|
|
/*
|
2020-02-13 08:13:58 +00:00
|
|
|
* In case of redirection, esp_http_client_read() is called
|
|
|
|
* to clear the response buffer of http_client.
|
2019-12-09 14:23:05 +00:00
|
|
|
*/
|
2020-02-13 08:13:58 +00:00
|
|
|
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;
|
2019-04-03 14:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t _http_connect(esp_http_client_handle_t http_client)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_FAIL;
|
2019-08-15 13:10:00 +00:00
|
|
|
int status_code, header_ret;
|
2019-04-03 14:25:27 +00:00
|
|
|
do {
|
|
|
|
err = esp_http_client_open(http_client, 0);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
|
|
|
return err;
|
|
|
|
}
|
2019-08-15 13:10:00 +00:00
|
|
|
header_ret = esp_http_client_fetch_headers(http_client);
|
|
|
|
if (header_ret < 0) {
|
|
|
|
return header_ret;
|
|
|
|
}
|
2019-04-03 14:25:27 +00:00
|
|
|
status_code = esp_http_client_get_status_code(http_client);
|
2019-08-15 13:10:00 +00:00
|
|
|
err = _http_handle_response_code(http_client, status_code);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
2019-04-03 14:25:27 +00:00
|
|
|
}
|
|
|
|
} while (process_again(status_code));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _http_cleanup(esp_http_client_handle_t client)
|
2018-07-12 12:15:44 +00:00
|
|
|
{
|
|
|
|
esp_http_client_close(client);
|
|
|
|
esp_http_client_cleanup(client);
|
|
|
|
}
|
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
static esp_err_t _ota_write(esp_https_ota_t *https_ota_handle, const void *buffer, size_t buf_len)
|
2018-07-12 12:15:44 +00:00
|
|
|
{
|
2019-04-03 14:25:27 +00:00
|
|
|
if (buffer == NULL || https_ota_handle == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
esp_err_t err = esp_ota_write(https_ota_handle->update_handle, buffer, buf_len);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%d", err);
|
|
|
|
} else {
|
|
|
|
https_ota_handle->binary_file_len += buf_len;
|
|
|
|
ESP_LOGD(TAG, "Written image length %d", https_ota_handle->binary_file_len);
|
|
|
|
err = ESP_ERR_HTTPS_OTA_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle)
|
|
|
|
{
|
|
|
|
esp_err_t err;
|
2019-05-21 17:42:18 +00:00
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
if (handle == NULL || ota_config == NULL || ota_config->http_config == NULL) {
|
|
|
|
ESP_LOGE(TAG, "esp_https_ota_begin: Invalid argument");
|
2019-05-21 17:42:18 +00:00
|
|
|
if (handle) {
|
|
|
|
*handle = NULL;
|
|
|
|
}
|
2018-07-12 12:15:44 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:02:35 +00:00
|
|
|
#if !CONFIG_OTA_ALLOW_HTTP
|
2019-04-03 14:25:27 +00:00
|
|
|
if (!ota_config->http_config->cert_pem) {
|
|
|
|
ESP_LOGE(TAG, "Server certificate not found in esp_http_client config");
|
2019-05-21 17:42:18 +00:00
|
|
|
*handle = NULL;
|
2018-12-31 09:02:35 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
2018-12-31 09:02:35 +00:00
|
|
|
#endif
|
2018-07-12 12:15:44 +00:00
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
esp_https_ota_t *https_ota_handle = calloc(1, sizeof(esp_https_ota_t));
|
|
|
|
if (!https_ota_handle) {
|
|
|
|
ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
|
2019-05-21 17:42:18 +00:00
|
|
|
*handle = NULL;
|
2019-04-03 14:25:27 +00:00
|
|
|
return ESP_ERR_NO_MEM;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
2019-04-03 14:25:27 +00:00
|
|
|
|
|
|
|
/* Initiate HTTP Connection */
|
|
|
|
https_ota_handle->http_client = esp_http_client_init(ota_config->http_config);
|
|
|
|
if (https_ota_handle->http_client == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialise HTTP connection");
|
|
|
|
err = ESP_FAIL;
|
|
|
|
goto failure;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
err = _http_connect(https_ota_handle->http_client);
|
2018-07-12 12:15:44 +00:00
|
|
|
if (err != ESP_OK) {
|
2019-04-03 14:25:27 +00:00
|
|
|
ESP_LOGE(TAG, "Failed to establish HTTP connection");
|
|
|
|
goto http_cleanup;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
https_ota_handle->update_partition = NULL;
|
2018-07-12 12:15:44 +00:00
|
|
|
ESP_LOGI(TAG, "Starting OTA...");
|
2019-04-03 14:25:27 +00:00
|
|
|
https_ota_handle->update_partition = esp_ota_get_next_update_partition(NULL);
|
|
|
|
if (https_ota_handle->update_partition == NULL) {
|
2018-07-12 12:15:44 +00:00
|
|
|
ESP_LOGE(TAG, "Passive OTA partition not found");
|
2019-04-03 14:25:27 +00:00
|
|
|
err = ESP_FAIL;
|
|
|
|
goto http_cleanup;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
|
2019-04-03 14:25:27 +00:00
|
|
|
https_ota_handle->update_partition->subtype, https_ota_handle->update_partition->address);
|
2018-07-12 12:15:44 +00:00
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
const int alloc_size = (ota_config->http_config->buffer_size > DEFAULT_OTA_BUF_SIZE) ?
|
|
|
|
ota_config->http_config->buffer_size : DEFAULT_OTA_BUF_SIZE;
|
|
|
|
https_ota_handle->ota_upgrade_buf = (char *)malloc(alloc_size);
|
|
|
|
if (!https_ota_handle->ota_upgrade_buf) {
|
|
|
|
ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
|
|
|
|
err = ESP_ERR_NO_MEM;
|
|
|
|
goto http_cleanup;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
2019-04-03 14:25:27 +00:00
|
|
|
https_ota_handle->ota_upgrade_buf_size = alloc_size;
|
2018-07-12 12:15:44 +00:00
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
https_ota_handle->binary_file_len = 0;
|
|
|
|
*handle = (esp_https_ota_handle_t)https_ota_handle;
|
|
|
|
https_ota_handle->state = ESP_HTTPS_OTA_BEGIN;
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
http_cleanup:
|
|
|
|
_http_cleanup(https_ota_handle->http_client);
|
|
|
|
failure:
|
|
|
|
free(https_ota_handle);
|
2019-05-21 17:42:18 +00:00
|
|
|
*handle = NULL;
|
2019-04-03 14:25:27 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
|
|
|
|
{
|
|
|
|
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
|
|
|
|
if (handle == NULL || new_app_info == NULL) {
|
|
|
|
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (handle->state < ESP_HTTPS_OTA_BEGIN) {
|
|
|
|
ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2019-12-09 14:23:05 +00:00
|
|
|
/*
|
|
|
|
* `data_read_size` holds number of bytes needed to read complete header.
|
|
|
|
* `bytes_read` holds number of bytes read.
|
|
|
|
*/
|
2019-04-03 14:25:27 +00:00
|
|
|
int data_read_size = IMAGE_HEADER_SIZE;
|
2019-12-09 14:23:05 +00:00
|
|
|
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
|
|
|
|
*/
|
2020-02-19 10:53:56 +00:00
|
|
|
if (errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) {
|
2019-12-09 14:23:05 +00:00
|
|
|
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data_read_size -= data_read;
|
|
|
|
bytes_read += data_read;
|
2019-04-03 14:25:27 +00:00
|
|
|
}
|
2019-12-09 14:23:05 +00:00
|
|
|
if (data_read_size > 0) {
|
|
|
|
ESP_LOGE(TAG, "Complete headers were not received");
|
2019-04-03 14:25:27 +00:00
|
|
|
return ESP_FAIL;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
2019-12-09 14:23:05 +00:00
|
|
|
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));
|
2019-04-03 14:25:27 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
2019-01-30 08:54:20 +00:00
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
|
|
|
|
{
|
|
|
|
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
|
|
|
|
if (handle == NULL) {
|
|
|
|
ESP_LOGE(TAG, "esp_https_ota_perform: Invalid argument");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (handle->state < ESP_HTTPS_OTA_BEGIN) {
|
|
|
|
ESP_LOGE(TAG, "esp_https_ota_perform: Invalid state");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t err;
|
|
|
|
int data_read;
|
|
|
|
switch (handle->state) {
|
|
|
|
case ESP_HTTPS_OTA_BEGIN:
|
|
|
|
err = esp_ota_begin(handle->update_partition, OTA_SIZE_UNKNOWN, &handle->update_handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
handle->state = ESP_HTTPS_OTA_IN_PROGRESS;
|
|
|
|
/* In case `esp_https_ota_read_img_desc` was invoked first,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
/* falls through */
|
|
|
|
case ESP_HTTPS_OTA_IN_PROGRESS:
|
|
|
|
data_read = esp_http_client_read(handle->http_client,
|
|
|
|
handle->ota_upgrade_buf,
|
|
|
|
handle->ota_upgrade_buf_size);
|
|
|
|
if (data_read == 0) {
|
2020-01-27 12:03:36 +00:00
|
|
|
/*
|
|
|
|
* esp_https_ota_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);
|
2019-12-09 14:23:05 +00:00
|
|
|
/*
|
|
|
|
* As esp_http_client_read never returns negative error code, we rely on
|
2020-01-27 12:03:36 +00:00
|
|
|
* `errno` to check for underlying transport connectivity closure if any.
|
|
|
|
* Incase the complete data has not been received but the server has sent
|
|
|
|
* an ENOTCONN or ECONNRESET, failure is returned. We close with success
|
|
|
|
* if complete data has been received.
|
2019-12-09 14:23:05 +00:00
|
|
|
*/
|
2020-02-19 10:53:56 +00:00
|
|
|
if ((errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) && !is_recv_complete) {
|
2019-12-09 14:23:05 +00:00
|
|
|
ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
|
|
|
|
return ESP_FAIL;
|
2020-01-27 12:03:36 +00:00
|
|
|
} else if (!is_recv_complete) {
|
2019-12-09 14:23:05 +00:00
|
|
|
return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Connection closed");
|
2019-04-03 14:25:27 +00:00
|
|
|
} else if (data_read > 0) {
|
|
|
|
return _ota_write(handle, (const void *)handle->ota_upgrade_buf, data_read);
|
|
|
|
}
|
|
|
|
handle->state = ESP_HTTPS_OTA_SUCCESS;
|
2018-07-12 12:15:44 +00:00
|
|
|
break;
|
2019-04-03 14:25:27 +00:00
|
|
|
default:
|
|
|
|
ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
|
|
|
|
return ESP_FAIL;
|
2018-07-12 12:15:44 +00:00
|
|
|
break;
|
2019-04-03 14:25:27 +00:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-15 13:10:00 +00:00
|
|
|
bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle)
|
|
|
|
{
|
|
|
|
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
|
|
|
|
return esp_http_client_is_complete_data_received(handle->http_client);
|
|
|
|
}
|
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
|
|
|
|
{
|
|
|
|
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
|
|
|
|
if (handle == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (handle->state < ESP_HTTPS_OTA_BEGIN) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
switch (handle->state) {
|
|
|
|
case ESP_HTTPS_OTA_SUCCESS:
|
|
|
|
case ESP_HTTPS_OTA_IN_PROGRESS:
|
|
|
|
err = esp_ota_end(handle->update_handle);
|
|
|
|
/* falls through */
|
|
|
|
case ESP_HTTPS_OTA_BEGIN:
|
2019-05-21 17:42:18 +00:00
|
|
|
if (handle->ota_upgrade_buf) {
|
|
|
|
free(handle->ota_upgrade_buf);
|
|
|
|
}
|
|
|
|
if (handle->http_client) {
|
|
|
|
_http_cleanup(handle->http_client);
|
|
|
|
}
|
2019-04-03 14:25:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err == ESP_OK) && (handle->state == ESP_HTTPS_OTA_SUCCESS)) {
|
|
|
|
esp_err_t err = esp_ota_set_boot_partition(handle->update_partition);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%d", err);
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 17:42:18 +00:00
|
|
|
free(handle);
|
2019-04-03 14:25:27 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)
|
|
|
|
{
|
|
|
|
esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
|
|
|
|
if (handle == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (handle->state < ESP_HTTPS_OTA_IN_PROGRESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return handle->binary_file_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_https_ota(const esp_http_client_config_t *config)
|
|
|
|
{
|
|
|
|
if (!config) {
|
|
|
|
ESP_LOGE(TAG, "esp_http_client config not found");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_https_ota_config_t ota_config = {
|
|
|
|
.http_config = config,
|
|
|
|
};
|
|
|
|
|
|
|
|
esp_https_ota_handle_t https_ota_handle = NULL;
|
|
|
|
esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle);
|
|
|
|
if (https_ota_handle == NULL) {
|
|
|
|
return ESP_FAIL;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 14:25:27 +00:00
|
|
|
while (1) {
|
|
|
|
err = esp_https_ota_perform(https_ota_handle);
|
|
|
|
if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
|
2018-07-12 12:15:44 +00:00
|
|
|
if (err != ESP_OK) {
|
2019-04-03 14:25:27 +00:00
|
|
|
/* If there was an error in esp_https_ota_perform(),
|
|
|
|
then it is given more precedence than error in esp_https_ota_finish()
|
|
|
|
*/
|
2018-07-12 12:15:44 +00:00
|
|
|
return err;
|
2019-04-03 14:25:27 +00:00
|
|
|
} else if (ota_finish_err != ESP_OK) {
|
|
|
|
return ota_finish_err;
|
2018-07-12 12:15:44 +00:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
2019-08-15 13:10:00 +00:00
|
|
|
}
|