Major refactoring. OTA Works now (yes, yes... really works! )

This commit is contained in:
Sebastien
2019-09-30 16:22:58 -04:00
parent 5e6fc4d20c
commit 4a79a39d3c
8 changed files with 277 additions and 226 deletions

View File

@@ -26,6 +26,18 @@
#include <sys/time.h>
#include <stdarg.h>
#include "esp_image_format.h"
#include "esp_secure_boot.h"
#include "esp_flash_encrypt.h"
#include "esp_spi_flash.h"
#include "sdkconfig.h"
#include "esp_ota_ops.h"
#define OTA_FLASH_ERASE_BLOCK (1024*100)
static const char *TAG = "squeezelite-ota";
extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
@@ -49,10 +61,10 @@ static struct {
struct timeval tv;
static esp_http_client_config_t ota_config;
extern void CODE_RAM_LOCATION wifi_manager_refresh_ota_json();
extern void wifi_manager_refresh_ota_json();
void CODE_RAM_LOCATION _printMemStats(){
ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)\n",
void _printMemStats(){
ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
@@ -66,7 +78,7 @@ void triggerStatusJsonRefresh(const char * status, ...){
_printMemStats();
wifi_manager_refresh_ota_json();
}
const char * CODE_RAM_LOCATION ota_get_status(){
const char * ota_get_status(){
if(!ota_status.bInitialized)
{
memset(ota_status.status_text, 0x00,sizeof(ota_status.status_text));
@@ -74,7 +86,7 @@ const char * CODE_RAM_LOCATION ota_get_status(){
}
return ota_status.status_text;
}
uint8_t CODE_RAM_LOCATION ota_get_pct_complete(){
uint8_t ota_get_pct_complete(){
return ota_status.ota_total_len==0?0:
(uint8_t)((float)ota_status.ota_actual_len/(float)ota_status.ota_total_len*100.0f);
}
@@ -89,7 +101,7 @@ static void __attribute__((noreturn)) task_fatal_error(void)
}
}
#define FREE_RESET(p) if(p!=NULL) { free(p); p=NULL; }
esp_err_t CODE_RAM_LOCATION _http_event_handler(esp_http_client_event_t *evt)
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
// --------------
// Received parameters
@@ -151,27 +163,15 @@ esp_err_t CODE_RAM_LOCATION _http_event_handler(esp_http_client_event_t *evt)
if(ota_status.lastpct!=ota_status.newpct )
{
gettimeofday(&tv, NULL);
uint32_t elapsed_ms= (tv.tv_sec-ota_status.OTA_start.tv_sec )*1000+(tv.tv_usec-ota_status.OTA_start.tv_usec)/1000;
ESP_LOGI(TAG,"OTA chunk : %d bytes (%d of %d) (%d pct), %d KB/s", evt->data_len, ota_status.ota_actual_len, ota_status.ota_total_len, ota_status.newpct, elapsed_ms>0?ota_status.ota_actual_len*1000/elapsed_ms/1024:0);
wifi_manager_refresh_ota_json();
ota_status.lastpct=ota_status.newpct;
ESP_LOGI(TAG,"OTA chunk : %d bytes (%d of %d) (%d pct), %d KB/s", evt->data_len, ota_status.ota_actual_len, ota_status.ota_total_len, ota_status.newpct, elapsed_ms>0?ota_status.ota_actual_len*1000/elapsed_ms/1024:0);
ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)\n",
heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
}
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)\n",
heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
@@ -180,11 +180,11 @@ esp_err_t CODE_RAM_LOCATION _http_event_handler(esp_http_client_event_t *evt)
return ESP_OK;
}
esp_err_t CODE_RAM_LOCATION init_config(esp_http_client_config_t * conf, const char * url){
esp_err_t init_config(esp_http_client_config_t * conf, const char * url){
memset(conf, 0x00, sizeof(esp_http_client_config_t));
conf->cert_pem = (char *)server_cert_pem_start;
conf->event_handler = _http_event_handler;
conf->buffer_size = 4096;
conf->buffer_size = 2048;
conf->disable_auto_redirect=true;
conf->skip_cert_common_name_check = false;
conf->url = strdup(url);
@@ -192,7 +192,71 @@ esp_err_t CODE_RAM_LOCATION init_config(esp_http_client_config_t * conf, const c
return ESP_OK;
}
void CODE_RAM_LOCATION ota_task(void *pvParameter)
esp_err_t _erase_last_boot_app_partition(void)
{
uint16_t num_passes=0;
uint16_t remain_size=0;
const esp_partition_t *ota_partition=NULL;
const esp_partition_t *ota_data_partition=NULL;
float erase_percent=0;
esp_err_t err=ESP_OK;
ESP_LOGI(TAG, "Looking for OTA partition.");
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0 , NULL);
if(it == NULL){
ESP_LOGE(TAG,"Unable initialize partition iterator!");
}
else {
ota_partition = (esp_partition_t *) esp_partition_get(it);
if(ota_partition != NULL){
ESP_LOGI(TAG, "Found OTA partition.");
}
else {
ESP_LOGE(TAG,"OTA partition not found! Unable update application.");
}
esp_partition_iterator_release(it);
}
it = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA , NULL);
if(it == NULL){
ESP_LOGE(TAG,"Unable initialize partition iterator!");
}
else {
ota_data_partition = (esp_partition_t *) esp_partition_get(it);
if(ota_data_partition != NULL){
ESP_LOGI(TAG, "Found OTA data partition.");
}
else {
ESP_LOGE(TAG,"OTA data partition not found! Unable update application.");
}
esp_partition_iterator_release(it);
}
if(ota_data_partition==NULL || ota_partition==NULL){
return ESP_FAIL;
}
ESP_LOGI(TAG,"Erasing OTA partition");
num_passes=ota_partition->size/OTA_FLASH_ERASE_BLOCK;
remain_size=ota_partition->size-(num_passes*OTA_FLASH_ERASE_BLOCK);
for(uint16_t i=0;i<num_passes;i++){
err=esp_partition_erase_range(ota_partition, 0, ota_partition->size);
erase_percent=100.0f*(float)i/num_passes;
triggerStatusJsonRefresh("Erasing OTA partition. %.1f%%",erase_percent);
taskYIELD();
if(err!=ESP_OK) return err;
}
if(remain_size>0){
err=esp_partition_erase_range(ota_partition, ota_partition->size-remain_size, remain_size);
if(err!=ESP_OK) return err;
}
triggerStatusJsonRefresh("Erasing OTA partition. 100%%");
taskYIELD();
return ESP_OK;
}
void ota_task(void *pvParameter)
{
char * passedURL=(char *)pvParameter;
@@ -208,11 +272,23 @@ void CODE_RAM_LOCATION ota_task(void *pvParameter)
}
ota_status.current_url= strdup(passedURL);
FREE_RESET(pvParameter);
ESP_LOGD(TAG,"Calling esp_https_ota");
ESP_LOGW(TAG,"**************** Expecting WATCHDOG errors below during flash erase. This is OK and not to worry about **************** ");
triggerStatusJsonRefresh("Erasing OTA partition");
esp_err_t err=_erase_last_boot_app_partition();
if(err!=ESP_OK){
ESP_LOGE(TAG,"Unable to erase last APP partition. Error: %s",esp_err_to_name(err));
FREE_RESET(ota_status.current_url);
FREE_RESET(ota_status.redirected_url);
vTaskDelete(NULL);
}
ESP_LOGI(TAG,"Calling esp_https_ota");
init_config(&ota_config,ota_status.bRedirectFound?ota_status.redirected_url:ota_status.current_url);
ota_status.bOTAStarted = true;
triggerStatusJsonRefresh("Starting OTA...");
esp_err_t err = esp_https_ota(&ota_config);
err = esp_https_ota(&ota_config);
if (err == ESP_OK) {
triggerStatusJsonRefresh("Success!");
esp_restart();
@@ -232,7 +308,6 @@ esp_err_t process_recovery_ota(const char * bin_url){
#if RECOVERY_APPLICATION
// Initialize NVS.
int ret=0;
nvs_handle nvs;
esp_err_t err = nvs_flash_init();
if(ota_status.bOTAThreadStarted){
@@ -266,7 +341,7 @@ esp_err_t process_recovery_ota(const char * bin_url){
#endif
ESP_LOGI(TAG, "Starting ota on core %u for : %s", OTA_CORE,urlPtr);
ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*20, (void *)urlPtr, ESP_TASK_MAIN_PRIO+4, NULL, OTA_CORE);
ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*20, (void *)urlPtr, ESP_TASK_MAIN_PRIO+1, NULL, OTA_CORE);
if (ret != pdPASS) {
ESP_LOGI(TAG, "create thread %s failed", "ota_task");
return ESP_FAIL;

View File

@@ -13,8 +13,8 @@
#define CODE_RAM_LOCATION
#endif
esp_err_t CODE_RAM_LOCATION start_ota(const char * bin_url, bool bFromAppMain);
const char * CODE_RAM_LOCATION ota_get_status();
uint8_t CODE_RAM_LOCATION ota_get_pct_complete();
esp_err_t start_ota(const char * bin_url, bool bFromAppMain);
const char * ota_get_status();
uint8_t ota_get_pct_complete();