109 lines
3.6 KiB
C
109 lines
3.6 KiB
C
#include "esp_ota.h"
|
|
static const char *TAG = "OTA";
|
|
|
|
static void __attribute__((noreturn)) task_fatal_error(void)
|
|
{
|
|
ESP_LOGE(TAG, "Exiting task due to fatal error...");
|
|
(void)vTaskDelete(NULL);
|
|
|
|
while (1) {
|
|
;
|
|
}
|
|
}
|
|
|
|
void Update_App_error(esp_ota_handle_t update_handle)
|
|
{
|
|
esp_ota_abort(update_handle);
|
|
task_fatal_error();
|
|
}
|
|
|
|
void Update_App(uint8_t * buffer)
|
|
{
|
|
|
|
uint32_t binsize = 0;
|
|
binsize = (buffer[0] << 24) | (buffer[1]<<16) | (buffer[2]<<8) | (buffer[3]<<0);
|
|
ESP_LOGI(TAG, "buffer: %d %d %d %ld",buffer[0],buffer[1],buffer[2],binsize);
|
|
esp_err_t err;
|
|
/* 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;
|
|
|
|
ESP_LOGI(TAG, "Starting OTA example");
|
|
|
|
const esp_partition_t *configured = esp_ota_get_boot_partition();
|
|
const esp_partition_t *running = esp_ota_get_running_partition();
|
|
|
|
if (configured != running) {
|
|
ESP_LOGI(TAG, "Configured OTA boot partition at offset 0x%lx, but running from offset 0x%lx",
|
|
configured->address, running->address);
|
|
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
|
|
}
|
|
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%ld)",
|
|
running->type, running->subtype, running->address);
|
|
|
|
update_partition = esp_ota_get_next_update_partition(NULL);
|
|
assert(update_partition != NULL);
|
|
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%lx",
|
|
update_partition->subtype, update_partition->address);
|
|
|
|
//esp_app_desc_t new_app_info;
|
|
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);
|
|
}
|
|
|
|
uint8_t* code;
|
|
uint8_t buf[256];
|
|
uint8_t index = 0;
|
|
uint32_t max_size = 0;
|
|
uint32_t checksum = 0;
|
|
uint32_t Rem_mem = 0;
|
|
uint16_t read_len = 256;
|
|
uint16_t i =0;
|
|
|
|
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
|
|
|
max_size = binsize;
|
|
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));
|
|
esp_ota_abort(update_handle);
|
|
task_fatal_error();
|
|
}
|
|
ESP_LOGI(TAG, "esp_ota_begin succeeded");
|
|
|
|
{
|
|
err = esp_ota_write( update_handle, (const void *)&buffer[4], max_size);
|
|
if (err != ESP_OK) {
|
|
esp_ota_abort(update_handle);
|
|
task_fatal_error();
|
|
}
|
|
}
|
|
ESP_LOGI(TAG, "%ld %ld",binsize,max_size);
|
|
//free(code);
|
|
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
|
err = esp_ota_end(update_handle);
|
|
if (err != ESP_OK) {
|
|
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
|
|
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
|
|
} else {
|
|
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
|
|
}
|
|
task_fatal_error();
|
|
}
|
|
|
|
err = esp_ota_set_boot_partition(update_partition);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
|
|
|
|
task_fatal_error();
|
|
}
|
|
ESP_LOGI(TAG, "Prepare to restart system!");
|
|
esp_restart();
|
|
} |