2016-12-19 07:40:21 +00:00
|
|
|
/* OTA example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2019-05-22 09:56:10 +00:00
|
|
|
#include <string.h>
|
2016-12-19 07:40:21 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
2019-05-22 09:56:10 +00:00
|
|
|
#include "esp_event.h"
|
2016-12-19 07:40:21 +00:00
|
|
|
#include "esp_event_loop.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_ota_ops.h"
|
2018-07-12 12:15:44 +00:00
|
|
|
#include "esp_http_client.h"
|
2018-05-30 09:08:00 +00:00
|
|
|
#include "esp_flash_partitions.h"
|
|
|
|
#include "esp_partition.h"
|
2017-03-14 13:39:44 +00:00
|
|
|
#include "nvs.h"
|
2016-12-19 07:40:21 +00:00
|
|
|
#include "nvs_flash.h"
|
2019-02-13 09:32:23 +00:00
|
|
|
#include "driver/gpio.h"
|
2019-05-22 09:56:10 +00:00
|
|
|
#include "protocol_examples_common.h"
|
2016-12-19 07:40:21 +00:00
|
|
|
|
|
|
|
#define BUFFSIZE 1024
|
2018-05-30 09:08:00 +00:00
|
|
|
#define HASH_LEN 32 /* SHA-256 digest length */
|
2016-12-19 07:40:21 +00:00
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
static const char *TAG = "native_ota_example";
|
2016-12-19 07:40:21 +00:00
|
|
|
/*an ota data write buffer ready to write to the flash*/
|
2017-02-20 03:02:39 +00:00
|
|
|
static char ota_write_data[BUFFSIZE + 1] = { 0 };
|
2018-07-12 12:15:44 +00:00
|
|
|
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");
|
2016-12-19 07:40:21 +00:00
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
static void http_cleanup(esp_http_client_handle_t client)
|
2016-12-19 07:40:21 +00:00
|
|
|
{
|
2018-07-12 12:15:44 +00:00
|
|
|
esp_http_client_close(client);
|
|
|
|
esp_http_client_cleanup(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 04:36:11 +00:00
|
|
|
static void __attribute__((noreturn)) task_fatal_error()
|
2016-12-19 07:40:21 +00:00
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "Exiting task due to fatal error...");
|
|
|
|
(void)vTaskDelete(NULL);
|
2017-01-06 10:52:58 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
;
|
|
|
|
}
|
2016-12-19 07:40:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 03:44:16 +00:00
|
|
|
static void print_sha256 (const uint8_t *image_hash, const char *label)
|
2018-05-30 09:08:00 +00:00
|
|
|
{
|
|
|
|
char hash_print[HASH_LEN * 2 + 1];
|
|
|
|
hash_print[HASH_LEN * 2] = 0;
|
|
|
|
for (int i = 0; i < HASH_LEN; ++i) {
|
|
|
|
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "%s: %s", label, hash_print);
|
|
|
|
}
|
|
|
|
|
2018-10-26 03:44:16 +00:00
|
|
|
static void infinite_loop(void)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
ESP_LOGI(TAG, "When a new firmware is available on the server, press the reset button to download it");
|
|
|
|
while(1) {
|
|
|
|
ESP_LOGI(TAG, "Waiting for a new firmware ... %d", ++i);
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 04:36:11 +00:00
|
|
|
static void ota_example_task(void *pvParameter)
|
2016-12-19 07:40:21 +00:00
|
|
|
{
|
|
|
|
esp_err_t err;
|
2017-02-20 03:02:39 +00:00
|
|
|
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
|
|
|
|
esp_ota_handle_t update_handle = 0 ;
|
|
|
|
const esp_partition_t *update_partition = NULL;
|
|
|
|
|
2019-05-22 09:56:10 +00:00
|
|
|
ESP_LOGI(TAG, "Starting OTA example");
|
2017-02-20 03:02:39 +00:00
|
|
|
|
|
|
|
const esp_partition_t *configured = esp_ota_get_boot_partition();
|
|
|
|
const esp_partition_t *running = esp_ota_get_running_partition();
|
|
|
|
|
2017-05-18 05:31:19 +00:00
|
|
|
if (configured != running) {
|
|
|
|
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
|
|
|
|
configured->address, running->address);
|
|
|
|
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
|
|
|
|
}
|
2017-02-20 03:02:39 +00:00
|
|
|
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
|
2017-05-18 05:31:19 +00:00
|
|
|
running->type, running->subtype, running->address);
|
2017-02-20 03:02:39 +00:00
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
esp_http_client_config_t config = {
|
2019-05-22 09:56:10 +00:00
|
|
|
.url = CONFIG_EXAMPLE_FIRMWARE_UPG_URL,
|
2018-07-12 12:15:44 +00:00
|
|
|
.cert_pem = (char *)server_cert_pem_start,
|
|
|
|
};
|
|
|
|
esp_http_client_handle_t client = esp_http_client_init(&config);
|
|
|
|
if (client == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialise HTTP connection");
|
2017-11-20 06:21:50 +00:00
|
|
|
task_fatal_error();
|
|
|
|
}
|
2018-07-12 12:15:44 +00:00
|
|
|
err = esp_http_client_open(client, 0);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
|
|
|
esp_http_client_cleanup(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
task_fatal_error();
|
|
|
|
}
|
2018-07-12 12:15:44 +00:00
|
|
|
esp_http_client_fetch_headers(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
|
2017-02-20 03:02:39 +00:00
|
|
|
update_partition = esp_ota_get_next_update_partition(NULL);
|
|
|
|
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
|
|
|
|
update_partition->subtype, update_partition->address);
|
|
|
|
assert(update_partition != NULL);
|
|
|
|
|
2018-07-12 12:15:44 +00:00
|
|
|
int binary_file_length = 0;
|
2016-12-19 07:40:21 +00:00
|
|
|
/*deal with all receive packet*/
|
2018-10-26 03:44:16 +00:00
|
|
|
bool image_header_was_checked = false;
|
2018-07-12 12:15:44 +00:00
|
|
|
while (1) {
|
|
|
|
int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
|
|
|
|
if (data_read < 0) {
|
|
|
|
ESP_LOGE(TAG, "Error: SSL data read error");
|
|
|
|
http_cleanup(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
task_fatal_error();
|
2018-07-12 12:15:44 +00:00
|
|
|
} else if (data_read > 0) {
|
2018-10-26 03:44:16 +00:00
|
|
|
if (image_header_was_checked == false) {
|
|
|
|
esp_app_desc_t new_app_info;
|
|
|
|
if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
|
|
|
|
// check current version with downloading
|
|
|
|
memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
|
|
|
|
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
|
|
|
|
|
|
|
|
esp_app_desc_t running_app_info;
|
|
|
|
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
|
|
|
|
esp_app_desc_t invalid_app_info;
|
|
|
|
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
|
|
|
|
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check current version with last invalid partition
|
|
|
|
if (last_invalid_app != NULL) {
|
|
|
|
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
|
|
|
|
ESP_LOGW(TAG, "New version is the same as invalid version.");
|
|
|
|
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
|
|
|
|
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
|
|
|
|
http_cleanup(client);
|
|
|
|
infinite_loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
|
|
|
|
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
|
|
|
|
http_cleanup(client);
|
|
|
|
infinite_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
image_header_was_checked = true;
|
|
|
|
|
|
|
|
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
|
|
|
http_cleanup(client);
|
|
|
|
task_fatal_error();
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "received package is not fit len");
|
|
|
|
http_cleanup(client);
|
|
|
|
task_fatal_error();
|
|
|
|
}
|
|
|
|
}
|
2018-07-12 12:15:44 +00:00
|
|
|
err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
|
2016-12-19 07:40:21 +00:00
|
|
|
if (err != ESP_OK) {
|
2018-07-12 12:15:44 +00:00
|
|
|
http_cleanup(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
task_fatal_error();
|
|
|
|
}
|
2018-07-12 12:15:44 +00:00
|
|
|
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");
|
|
|
|
break;
|
2016-12-19 07:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Total Write binary data length : %d", binary_file_length);
|
|
|
|
|
2017-02-20 03:02:39 +00:00
|
|
|
if (esp_ota_end(update_handle) != ESP_OK) {
|
2016-12-19 07:40:21 +00:00
|
|
|
ESP_LOGE(TAG, "esp_ota_end failed!");
|
2018-07-12 12:15:44 +00:00
|
|
|
http_cleanup(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
task_fatal_error();
|
|
|
|
}
|
2018-05-30 09:08:00 +00:00
|
|
|
|
2017-02-20 03:02:39 +00:00
|
|
|
err = esp_ota_set_boot_partition(update_partition);
|
2016-12-19 07:40:21 +00:00
|
|
|
if (err != ESP_OK) {
|
2018-02-22 13:48:53 +00:00
|
|
|
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
|
2018-07-12 12:15:44 +00:00
|
|
|
http_cleanup(client);
|
2016-12-19 07:40:21 +00:00
|
|
|
task_fatal_error();
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Prepare to restart system!");
|
|
|
|
esp_restart();
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2019-02-13 09:32:23 +00:00
|
|
|
static bool diagnostic(void)
|
|
|
|
{
|
|
|
|
gpio_config_t io_conf;
|
|
|
|
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
|
|
|
|
io_conf.mode = GPIO_MODE_INPUT;
|
2019-05-09 14:43:06 +00:00
|
|
|
io_conf.pin_bit_mask = (1ULL << CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
2019-02-13 09:32:23 +00:00
|
|
|
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
|
|
|
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
|
|
|
gpio_config(&io_conf);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Diagnostics (5 sec)...");
|
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
|
|
|
2019-05-09 14:43:06 +00:00
|
|
|
bool diagnostic_is_ok = gpio_get_level(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
2019-02-13 09:32:23 +00:00
|
|
|
|
2019-05-09 14:43:06 +00:00
|
|
|
gpio_reset_pin(CONFIG_EXAMPLE_GPIO_DIAGNOSTIC);
|
2019-02-13 09:32:23 +00:00
|
|
|
return diagnostic_is_ok;
|
|
|
|
}
|
|
|
|
|
2016-12-19 07:40:21 +00:00
|
|
|
void app_main()
|
|
|
|
{
|
2018-05-30 09:08:00 +00:00
|
|
|
uint8_t sha_256[HASH_LEN] = { 0 };
|
|
|
|
esp_partition_t partition;
|
|
|
|
|
|
|
|
// get sha256 digest for the partition table
|
|
|
|
partition.address = ESP_PARTITION_TABLE_OFFSET;
|
|
|
|
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
|
|
|
|
partition.type = ESP_PARTITION_TYPE_DATA;
|
|
|
|
esp_partition_get_sha256(&partition, sha_256);
|
|
|
|
print_sha256(sha_256, "SHA-256 for the partition table: ");
|
|
|
|
|
|
|
|
// get sha256 digest for bootloader
|
|
|
|
partition.address = ESP_BOOTLOADER_OFFSET;
|
|
|
|
partition.size = ESP_PARTITION_TABLE_OFFSET;
|
|
|
|
partition.type = ESP_PARTITION_TYPE_APP;
|
|
|
|
esp_partition_get_sha256(&partition, sha_256);
|
|
|
|
print_sha256(sha_256, "SHA-256 for bootloader: ");
|
|
|
|
|
|
|
|
// get sha256 digest for running partition
|
|
|
|
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
|
|
|
|
print_sha256(sha_256, "SHA-256 for current firmware: ");
|
|
|
|
|
2018-10-26 03:44:16 +00:00
|
|
|
const esp_partition_t *running = esp_ota_get_running_partition();
|
|
|
|
esp_ota_img_states_t ota_state;
|
|
|
|
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
|
|
|
|
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
|
|
|
|
// run diagnostic function ...
|
2019-02-13 09:32:23 +00:00
|
|
|
bool diagnostic_is_ok = diagnostic();
|
2018-10-26 03:44:16 +00:00
|
|
|
if (diagnostic_is_ok) {
|
|
|
|
ESP_LOGI(TAG, "Diagnostics completed successfully! Continuing execution ...");
|
|
|
|
esp_ota_mark_app_valid_cancel_rollback();
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Diagnostics failed! Start rollback to the previous version ...");
|
|
|
|
esp_ota_mark_app_invalid_rollback_and_reboot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 13:39:44 +00:00
|
|
|
// Initialize NVS.
|
|
|
|
esp_err_t err = nvs_flash_init();
|
2018-07-25 15:11:09 +00:00
|
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-03-14 13:39:44 +00:00
|
|
|
// OTA app partition table has a smaller NVS partition size than the non-OTA
|
|
|
|
// partition table. This size mismatch may cause NVS initialization to fail.
|
|
|
|
// If this happens, we erase NVS partition and initialize NVS again.
|
2017-07-13 15:46:19 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
2017-03-14 13:39:44 +00:00
|
|
|
err = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( err );
|
|
|
|
|
2019-05-22 09:56:10 +00:00
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
2017-03-22 04:36:11 +00:00
|
|
|
xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
|
2016-12-19 07:40:21 +00:00
|
|
|
}
|