From de9549936d534b50e6553bd8666956a5793a013b Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 18 Oct 2019 17:31:32 +0530 Subject: [PATCH 1/5] examples: fix handle passed to `esp_https_ota_is_complete_data_received` --- .../ota/advanced_https_ota/main/advanced_https_ota_example.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c index a207a91304..3f05a3f37a 100644 --- a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c +++ b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c @@ -87,7 +87,7 @@ void advanced_ota_example_task(void *pvParameter) ESP_LOGD(TAG, "Image bytes read: %d", esp_https_ota_get_image_len_read(https_ota_handle)); } - if (esp_https_ota_is_complete_data_received(&https_ota_handle) != true) { + if (esp_https_ota_is_complete_data_received(https_ota_handle) != true) { // the OTA image was not completely received and user can customise the response to this situation. ESP_LOGE(TAG, "Complete data was not received."); } @@ -99,7 +99,7 @@ ota_end: vTaskDelay(1000 / portTICK_PERIOD_MS); esp_restart(); } else { - ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed..."); + ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed %d", ota_finish_err); } while (1) { From 917a406c0a6b9bcfd5bd462323937d76591c3abe Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 18 Oct 2019 17:35:13 +0530 Subject: [PATCH 2/5] Logging improvements in OTA example and component --- components/esp_https_ota/src/esp_https_ota.c | 2 +- .../ota/native_ota_example/main/native_ota_example.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/components/esp_https_ota/src/esp_https_ota.c b/components/esp_https_ota/src/esp_https_ota.c index d2ca501039..d5a69783ad 100644 --- a/components/esp_https_ota/src/esp_https_ota.c +++ b/components/esp_https_ota/src/esp_https_ota.c @@ -263,7 +263,7 @@ 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) { - ESP_LOGI(TAG, "Connection closed, all data received"); + ESP_LOGI(TAG, "Connection closed"); } else if (data_read < 0) { ESP_LOGE(TAG, "Error: SSL data read error"); return ESP_FAIL; diff --git a/examples/system/ota/native_ota_example/main/native_ota_example.c b/examples/system/ota/native_ota_example/main/native_ota_example.c index 1c8ea033f7..ae885b1676 100644 --- a/examples/system/ota/native_ota_example/main/native_ota_example.c +++ b/examples/system/ota/native_ota_example/main/native_ota_example.c @@ -176,19 +176,20 @@ static void ota_example_task(void *pvParameter) binary_file_length += data_read; ESP_LOGD(TAG, "Written image length %d", binary_file_length); } else if (data_read == 0) { - ESP_LOGI(TAG, "Connection closed,all data received"); + ESP_LOGI(TAG, "Connection closed"); break; } } - ESP_LOGI(TAG, "Total Write binary data length : %d", binary_file_length); + ESP_LOGI(TAG, "Total Write binary data length: %d", binary_file_length); if (esp_http_client_is_complete_data_received(client) != true) { ESP_LOGE(TAG, "Error in receiving complete file"); http_cleanup(client); task_fatal_error(); } - if (esp_ota_end(update_handle) != ESP_OK) { - ESP_LOGE(TAG, "esp_ota_end failed!"); + err = esp_ota_end(update_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err)); http_cleanup(client); task_fatal_error(); } From 4dcffdb0a9ae6e91dcd5d5ac3a1677376a7bf158 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 18 Oct 2019 17:35:57 +0530 Subject: [PATCH 3/5] esp_http_client: fix issue where http parser was not invoking `message_complete` callback https://github.com/espressif/esp-idf/issues/2625 https://github.com/espressif/esp-idf/issues/4209 --- components/esp_http_client/esp_http_client.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 7a186f74b1..517e95b962 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -849,7 +849,16 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) if (rlen <= 0) { if (errno != 0) { - ESP_LOGW(TAG, "esp_transport_read returned : %d and errno : %d ", rlen, errno); + esp_log_level_t sev = ESP_LOG_WARN; + /* On connection close from server, recv should ideally return 0 but we have error conversion + * in `tcp_transport` SSL layer which translates it `-1` and hence below additional checks */ + if (rlen == -1 && errno == ENOTCONN && client->response->is_chunked) { + /* Explicit call to parser for invoking `message_complete` callback */ + http_parser_execute(client->parser, client->parser_settings, res_buffer->data, 0); + /* ...and lowering the message severity, as closed connection from server side is expected in chunked transport */ + sev = ESP_LOG_DEBUG; + } + ESP_LOG_LEVEL(sev, TAG, "esp_transport_read returned:%d and errno:%d ", rlen, errno); } return ridx; } From a51fe89778a4f006e520ada267acfe40d8b4b095 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Mon, 21 Oct 2019 23:41:05 -0500 Subject: [PATCH 4/5] Update OTA Documentation to clarify grammar. The sentence about CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE was worded strangely. I updated it to better explain the functionality. Closes https://github.com/espressif/esp-idf/pull/4228 --- docs/en/api-reference/system/ota.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/system/ota.rst b/docs/en/api-reference/system/ota.rst index ff9251c2cc..b3e3b08fd2 100644 --- a/docs/en/api-reference/system/ota.rst +++ b/docs/en/api-reference/system/ota.rst @@ -41,7 +41,7 @@ The main purpose of the application rollback is to keep the device working after * The application works fine, :cpp:func:`esp_ota_mark_app_valid_cancel_rollback` marks the running application with the state ``ESP_OTA_IMG_VALID``. There are no restrictions on booting this application. * The application has critical errors and further work is not possible, a rollback to the previous application is required, :cpp:func:`esp_ota_mark_app_invalid_rollback_and_reboot` marks the running application with the state ``ESP_OTA_IMG_INVALID`` and reset. This application will not be selected by the bootloader for boot and will boot the previously working application. -* If the :ref:`CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE` option is set, and occur a reset without calling either function then happend and is rolled back. +* If the :ref:`CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE` option is set, and a reset occurs without calling either function then the application is rolled back. Note: The state is not written to the binary image of the application it is written to the ``otadata`` partition. The partition contains a ``ota_seq`` counter which is a pointer to the slot (ota_0, ota_1, ...) from which the application will be selected for boot. From e1a6846ac381a5988b6e9ce580346374954e0754 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 22 Oct 2019 14:44:05 +0530 Subject: [PATCH 5/5] examples/ota: disable WiFi power save mode for optimal performance --- .../main/advanced_https_ota_example.c | 11 +++++++++++ .../ota/native_ota_example/main/native_ota_example.c | 11 +++++++++++ .../ota/simple_ota_example/main/simple_ota_example.c | 11 +++++++++++ 3 files changed, 33 insertions(+) diff --git a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c index 3f05a3f37a..e6b4bd15c8 100644 --- a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c +++ b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c @@ -20,6 +20,10 @@ #include "nvs_flash.h" #include "protocol_examples_common.h" +#if CONFIG_EXAMPLE_CONNECT_WIFI +#include "esp_wifi.h" +#endif + static const char *TAG = "advanced_https_ota_example"; extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end"); @@ -130,6 +134,13 @@ void app_main(void) */ ESP_ERROR_CHECK(example_connect()); +#if CONFIG_EXAMPLE_CONNECT_WIFI + /* Ensure to disable any WiFi power save mode, this allows best throughput + * and hence timings for overall OTA operation. + */ + esp_wifi_set_ps(WIFI_PS_NONE); +#endif // CONFIG_EXAMPLE_CONNECT_WIFI + xTaskCreate(&advanced_ota_example_task, "advanced_ota_example_task", 1024 * 8, NULL, 5, NULL); } diff --git a/examples/system/ota/native_ota_example/main/native_ota_example.c b/examples/system/ota/native_ota_example/main/native_ota_example.c index ae885b1676..08b6c9bb62 100644 --- a/examples/system/ota/native_ota_example/main/native_ota_example.c +++ b/examples/system/ota/native_ota_example/main/native_ota_example.c @@ -21,6 +21,10 @@ #include "driver/gpio.h" #include "protocol_examples_common.h" +#if CONFIG_EXAMPLE_CONNECT_WIFI +#include "esp_wifi.h" +#endif + #define BUFFSIZE 1024 #define HASH_LEN 32 /* SHA-256 digest length */ @@ -283,5 +287,12 @@ void app_main(void) */ ESP_ERROR_CHECK(example_connect()); +#if CONFIG_EXAMPLE_CONNECT_WIFI + /* Ensure to disable any WiFi power save mode, this allows best throughput + * and hence timings for overall OTA operation. + */ + esp_wifi_set_ps(WIFI_PS_NONE); +#endif // CONFIG_EXAMPLE_CONNECT_WIFI + xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); } diff --git a/examples/system/ota/simple_ota_example/main/simple_ota_example.c b/examples/system/ota/simple_ota_example/main/simple_ota_example.c index f85186e2b6..494c85f54f 100644 --- a/examples/system/ota/simple_ota_example/main/simple_ota_example.c +++ b/examples/system/ota/simple_ota_example/main/simple_ota_example.c @@ -21,6 +21,10 @@ #include "nvs_flash.h" #include "protocol_examples_common.h" +#if CONFIG_EXAMPLE_CONNECT_WIFI +#include "esp_wifi.h" +#endif + static const char *TAG = "simple_ota_example"; extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end"); @@ -117,5 +121,12 @@ void app_main(void) */ ESP_ERROR_CHECK(example_connect()); +#if CONFIG_EXAMPLE_CONNECT_WIFI + /* Ensure to disable any WiFi power save mode, this allows best throughput + * and hence timings for overall OTA operation. + */ + esp_wifi_set_ps(WIFI_PS_NONE); +#endif // CONFIG_EXAMPLE_CONNECT_WIFI + xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); }