From 18f36b543329262a3211cfdd14d5ecb2d57e1add Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 2 Oct 2019 12:16:10 -0400 Subject: [PATCH] Improve UI status handling during OTA --- components/squeezelite-ota/squeezelite-ota.c | 53 ++++++++++++-------- components/wifi-manager/http_server.c | 4 +- components/wifi-manager/wifi_manager.c | 44 +++++++++++----- main/console.c | 2 +- main/nvs_utilities.c | 23 +++++++++ main/nvs_utilities.h | 1 + 6 files changed, 90 insertions(+), 37 deletions(-) diff --git a/components/squeezelite-ota/squeezelite-ota.c b/components/squeezelite-ota/squeezelite-ota.c index 77618d30..a227f729 100644 --- a/components/squeezelite-ota/squeezelite-ota.c +++ b/components/squeezelite-ota/squeezelite-ota.c @@ -41,10 +41,10 @@ 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"); -extern bool wait_for_wifi(); -extern char current_namespace[]; +char * cert=NULL; + static struct { - char status_text[31]; + char status_text[81]; uint32_t ota_actual_len; uint32_t ota_total_len; char * redirected_url; @@ -64,19 +64,28 @@ static esp_http_client_config_t ota_config; extern void wifi_manager_refresh_ota_json(); void _printMemStats(){ - ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)", + ESP_LOGD(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), heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM)); } -void triggerStatusJsonRefresh(const char * status, ...){ +void triggerStatusJsonRefresh(bool bDelay,const char * status, ...){ va_list args; va_start(args, status); - snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,status, args); + vsnprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,status, args); va_end(args); _printMemStats(); wifi_manager_refresh_ota_json(); + if(bDelay){ + ESP_LOGD(TAG,"Holding task..."); + vTaskDelay(1500 / portTICK_PERIOD_MS); // wait here for a short amount of time. This will help with refreshing the UI status + ESP_LOGD(TAG,"Done holding task..."); + } + else + { + taskYIELD(); + } } const char * ota_get_status(){ if(!ota_status.bInitialized) @@ -127,7 +136,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) case HTTP_EVENT_ON_CONNECTED: ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); - if(ota_status.bOTAStarted) triggerStatusJsonRefresh("Installing..."); + if(ota_status.bOTAStarted) triggerStatusJsonRefresh(true,"Installing..."); ota_status.ota_total_len=0; ota_status.ota_actual_len=0; ota_status.lastpct=0; @@ -182,7 +191,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) 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->cert_pem =cert==NULL?(char *)server_cert_pem_start:cert; conf->event_handler = _http_event_handler; conf->buffer_size = 2048; conf->disable_auto_redirect=true; @@ -198,7 +208,6 @@ esp_err_t _erase_last_boot_app_partition(void) 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."); @@ -236,14 +245,14 @@ esp_err_t _erase_last_boot_app_partition(void) if(ota_data_partition==NULL || ota_partition==NULL){ return ESP_FAIL; } - ESP_LOGI(TAG,"Erasing OTA partition"); + ESP_LOGI(TAG,"Erasing flash "); 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;isize); - erase_percent=100.0f*(float)i/num_passes; - triggerStatusJsonRefresh("Erasing OTA partition. %.1f%%",erase_percent); + ESP_LOGD(TAG,"Erasing flash (%u%%)",i/num_passes); + triggerStatusJsonRefresh(i%5==0?true:false,"Erasing flash (%u/%u)",i,num_passes); taskYIELD(); if(err!=ESP_OK) return err; } @@ -251,7 +260,7 @@ esp_err_t _erase_last_boot_app_partition(void) 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%%"); + triggerStatusJsonRefresh(false,"Erasing flash (100%%)"); taskYIELD(); return ESP_OK; } @@ -262,7 +271,7 @@ void ota_task(void *pvParameter) ota_status.bInitialized = true; ESP_LOGD(TAG, "HTTP ota Thread started"); - triggerStatusJsonRefresh("Initializing..."); + triggerStatusJsonRefresh(true,"Initializing..."); ota_status.bRedirectFound=false; if(passedURL==NULL || strlen(passedURL)==0){ ESP_LOGE(TAG,"HTTP OTA called without a url"); @@ -274,7 +283,7 @@ void ota_task(void *pvParameter) FREE_RESET(pvParameter); ESP_LOGW(TAG,"**************** Expecting WATCHDOG errors below during flash erase. This is OK and not to worry about **************** "); - triggerStatusJsonRefresh("Erasing OTA partition"); + triggerStatusJsonRefresh(true,"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)); @@ -287,13 +296,13 @@ void ota_task(void *pvParameter) 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..."); + triggerStatusJsonRefresh(true,"Starting OTA..."); err = esp_https_ota(&ota_config); if (err == ESP_OK) { - triggerStatusJsonRefresh("Success!"); + triggerStatusJsonRefresh(true,"Success!"); esp_restart(); } else { - triggerStatusJsonRefresh("Error: %s",esp_err_to_name(err)); + triggerStatusJsonRefresh(true,"Error: %s",esp_err_to_name(err)); wifi_manager_refresh_ota_json(); ESP_LOGE(TAG, "Firmware upgrade failed with error : %s", esp_err_to_name(err)); ota_status.bOTAThreadStarted=false; @@ -305,11 +314,10 @@ void ota_task(void *pvParameter) } esp_err_t process_recovery_ota(const char * bin_url){ -#if RECOVERY_APPLICATION + // Initialize NVS. int ret=0; esp_err_t err = nvs_flash_init(); - if(ota_status.bOTAThreadStarted){ ESP_LOGE(TAG,"OTA Already started. "); return ESP_FAIL; @@ -341,17 +349,18 @@ 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+1, 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; } -#endif + return ESP_OK; } esp_err_t start_ota(const char * bin_url, bool bFromAppMain) { +// uint8_t * get_nvs_value_alloc_default(NVS_TYPE_BLOB, "certs", server_cert_pem_start , server_cert_pem_end-server_cert_pem_start); #if RECOVERY_APPLICATION return process_recovery_ota(bin_url); #else diff --git a/components/wifi-manager/http_server.c b/components/wifi-manager/http_server.c index e46e31a9..1a47e7e3 100644 --- a/components/wifi-manager/http_server.c +++ b/components/wifi-manager/http_server.c @@ -92,7 +92,7 @@ const static char http_redirect_hdr_end[] = "/\n\n"; void http_server_start(){ if(task_http_server == NULL){ - xTaskCreate(&http_server, "http_server", 1024*5, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_http_server); + xTaskCreate(&http_server, "http_server", 1024*5, NULL, WIFI_MANAGER_TASK_PRIORITY, &task_http_server); } } void http_server(void *pvParameters) { @@ -386,6 +386,8 @@ void http_server_netconn_serve(struct netconn *conn) { bool bErrorFound=false; bool bOTA=false; char * otaURL=NULL; + // make sure we terminate the netconn string + save_ptr[buflen-1]='\0'; while(last_parm!=NULL){ // Search will return diff --git a/components/wifi-manager/wifi_manager.c b/components/wifi-manager/wifi_manager.c index 72cb45df..963e29d6 100644 --- a/components/wifi-manager/wifi_manager.c +++ b/components/wifi-manager/wifi_manager.c @@ -56,10 +56,18 @@ Contains the freeRTOS task and all necessary support #include "lwip/ip4_addr.h" #include "esp_ota_ops.h" #include "esp_app_format.h" +#include "driver/gpio.h" +#include "driver/adc.h" #ifndef SQUEEZELITE_ESP32_RELEASE_URL #define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases" #endif +#ifdef TAS575x +#define JACK_GPIO 34 +#define JACK_LEVEL !gpio_get_level(JACK_GPIO)?"1":"0"; +#else +#define JACK_LEVEL "N/A" +#endif /* objects used to manipulate the main queue of events */ QueueHandle_t wifi_manager_queue; @@ -378,9 +386,9 @@ void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code) } if(config){ #if RECOVERY_APPLICATION - const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\", \"ota_dsc\":\"%s\", \"ota_pct\":%u,\"recovery\": 1}\n"; + const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\", \"ota_dsc\":\"%s\", \"ota_pct\":%u,\"recovery\": 1,\"Jack\" : \"%s\", \"Voltage\" : %.2f }\n"; #else - const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\",\"recovery\": 0}\n"; + const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\",\"recovery\": 0,\"Jack\" : \"%s\", \"Voltage\" : %.2f }\n"; #endif memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE); const esp_app_desc_t* desc = esp_ota_get_app_description(); @@ -404,13 +412,15 @@ void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code) gw, (int)update_reason_code, desc->project_name, - desc->version + desc->version, #if RECOVERY_APPLICATION - ,ota_get_status(), - ota_get_pct_complete() + ota_get_status(), + ota_get_pct_complete(), #endif + JACK_LEVEL, + adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1 ); } else{ @@ -421,31 +431,39 @@ void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code) "0", (int)update_reason_code, desc->project_name, - desc->version + desc->version, #if RECOVERY_APPLICATION - ,"", - 0 + "", + 0, #endif + JACK_LEVEL, + adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1 ); } } else{ + #if RECOVERY_APPLICATION - const char ip_info_json_format[] = ",\"project_name\":\"%s\",\"version\":\"%s\", \"ota_dsc\":\"%s\", \"ota_pct\":%d}\n"; + const char ip_info_json_format[] = ",\"project_name\":\"%s\",\"version\":\"%s\", \"ota_dsc\":\"%s\", \"ota_pct\":%d,\"Jack\" : \"%s\", \"Voltage\" : %.2f }\n"; + #else - const char ip_info_json_format[] = ",\"project_name\":\"%s\",\"version\":\"%s\"}\n"; + const char ip_info_json_format[] = ",\"project_name\":\"%s\",\"version\":\"%s\",\"Jack\" : \"%s\", \"Voltage\" : %.2f }\n"; #endif + + memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE); const esp_app_desc_t* desc = esp_ota_get_app_description(); /* to avoid declaring a new buffer we copy the data directly into the buffer at its correct address */ snprintf( (ip_info_json + strlen(ip_info_json)), JSON_IP_INFO_SIZE, ip_info_json_format, desc->project_name, - desc->version + desc->version, #if RECOVERY_APPLICATION - ,ota_get_status(), - ota_get_pct_complete() + ota_get_status(), + ota_get_pct_complete(), #endif + JACK_LEVEL, + adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1 ); } } diff --git a/main/console.c b/main/console.c index 4aa75169..68ad2b98 100644 --- a/main/console.c +++ b/main/console.c @@ -59,7 +59,7 @@ void process_autoexec(){ if(str_flag !=NULL ){ autoexec_flag=atoi(str_flag); - ESP_LOGI(TAG,"autoexec flag value found with value %u", autoexec_flag); + ESP_LOGI(TAG,"autoexec flag value found with value %u, from string value: %s", autoexec_flag, str_flag); if(autoexec_flag == 1) { do { snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++); diff --git a/main/nvs_utilities.c b/main/nvs_utilities.c index bea6df34..d17f75d1 100644 --- a/main/nvs_utilities.c +++ b/main/nvs_utilities.c @@ -99,6 +99,29 @@ esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data, nvs_close(nvs); return err; } + +void * get_nvs_value_alloc_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size) { + + void * current_value = get_nvs_value_alloc(type, key); + if(current_value == NULL && default_value != NULL){ + if(type == NVS_TYPE_BLOB && blob_size == 0){ + ESP_LOGE(TAG,"Unable to store default value for BLOB object' blob size was not specified"); + return NULL; + } + else { + esp_err_t err = store_nvs_value_len(type, key, default_value, blob_size); + if(err!=ESP_OK){ + ESP_LOGE(TAG,"Unable to store default nvs value. Error: %s",esp_err_to_name(err)); + return NULL; + } + } + } + if(current_value == NULL){ + current_value = get_nvs_value_alloc(type, key); + } + return current_value; +} + void * get_nvs_value_alloc(nvs_type_t type, const char *key) { nvs_handle nvs; esp_err_t err; diff --git a/main/nvs_utilities.h b/main/nvs_utilities.h index c6a363d7..8610e8db 100644 --- a/main/nvs_utilities.h +++ b/main/nvs_utilities.h @@ -10,6 +10,7 @@ esp_err_t store_nvs_value(nvs_type_t type, const char *key, void * data); esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint8_t buf_size); void * get_nvs_value_alloc(nvs_type_t type, const char *key); esp_err_t erase_nvs(const char *key); +void * get_nvs_value_alloc_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size); #ifdef __cplusplus } #endif