Merge branch 'Over_The_Air_Update' of github.com:sle118/squeezelite-esp32 into Over_The_Air_Update

This commit is contained in:
Christian Herzog
2019-10-02 18:36:13 +02:00
6 changed files with 90 additions and 37 deletions

View File

@@ -41,10 +41,10 @@
static const char *TAG = "squeezelite-ota"; 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_start[] asm("_binary_github_pem_start");
extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end"); extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
extern bool wait_for_wifi(); char * cert=NULL;
extern char current_namespace[];
static struct { static struct {
char status_text[31]; char status_text[81];
uint32_t ota_actual_len; uint32_t ota_actual_len;
uint32_t ota_total_len; uint32_t ota_total_len;
char * redirected_url; char * redirected_url;
@@ -64,19 +64,28 @@ static esp_http_client_config_t ota_config;
extern void wifi_manager_refresh_ota_json(); extern void wifi_manager_refresh_ota_json();
void _printMemStats(){ 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_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_minimum_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_free_size(MALLOC_CAP_SPIRAM),
heap_caps_get_minimum_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_list args;
va_start(args, status); 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); va_end(args);
_printMemStats(); _printMemStats();
wifi_manager_refresh_ota_json(); wifi_manager_refresh_ota_json();
if(bDelay){
ESP_LOGD(TAG,"Holding task...");
vTaskDelay(700 / 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(){ const char * ota_get_status(){
if(!ota_status.bInitialized) 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: case HTTP_EVENT_ON_CONNECTED:
ESP_LOGD(TAG, "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_total_len=0;
ota_status.ota_actual_len=0; ota_status.ota_actual_len=0;
ota_status.lastpct=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){ esp_err_t init_config(esp_http_client_config_t * conf, const char * url){
memset(conf, 0x00, sizeof(esp_http_client_config_t)); 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->event_handler = _http_event_handler;
conf->buffer_size = 2048; conf->buffer_size = 2048;
conf->disable_auto_redirect=true; conf->disable_auto_redirect=true;
@@ -198,7 +208,6 @@ esp_err_t _erase_last_boot_app_partition(void)
uint16_t remain_size=0; uint16_t remain_size=0;
const esp_partition_t *ota_partition=NULL; const esp_partition_t *ota_partition=NULL;
const esp_partition_t *ota_data_partition=NULL; const esp_partition_t *ota_data_partition=NULL;
float erase_percent=0;
esp_err_t err=ESP_OK; esp_err_t err=ESP_OK;
ESP_LOGI(TAG, "Looking for OTA partition."); 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){ if(ota_data_partition==NULL || ota_partition==NULL){
return ESP_FAIL; return ESP_FAIL;
} }
ESP_LOGI(TAG,"Erasing OTA partition"); ESP_LOGI(TAG,"Erasing flash ");
num_passes=ota_partition->size/OTA_FLASH_ERASE_BLOCK; num_passes=ota_partition->size/OTA_FLASH_ERASE_BLOCK;
remain_size=ota_partition->size-(num_passes*OTA_FLASH_ERASE_BLOCK); remain_size=ota_partition->size-(num_passes*OTA_FLASH_ERASE_BLOCK);
for(uint16_t i=0;i<num_passes;i++){ for(uint16_t i=0;i<num_passes;i++){
err=esp_partition_erase_range(ota_partition, 0, ota_partition->size); err=esp_partition_erase_range(ota_partition, 0, ota_partition->size);
erase_percent=100.0f*(float)i/num_passes; ESP_LOGD(TAG,"Erasing flash (%u%%)",i/num_passes);
triggerStatusJsonRefresh("Erasing OTA partition. %.1f%%",erase_percent); triggerStatusJsonRefresh(i%5==0?true:false,"Erasing flash (%u/%u)",i,num_passes);
taskYIELD(); taskYIELD();
if(err!=ESP_OK) return err; 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); err=esp_partition_erase_range(ota_partition, ota_partition->size-remain_size, remain_size);
if(err!=ESP_OK) return err; if(err!=ESP_OK) return err;
} }
triggerStatusJsonRefresh("Erasing OTA partition. 100%%"); triggerStatusJsonRefresh(false,"Erasing flash (100%%)");
taskYIELD(); taskYIELD();
return ESP_OK; return ESP_OK;
} }
@@ -262,7 +271,7 @@ void ota_task(void *pvParameter)
ota_status.bInitialized = true; ota_status.bInitialized = true;
ESP_LOGD(TAG, "HTTP ota Thread started"); ESP_LOGD(TAG, "HTTP ota Thread started");
triggerStatusJsonRefresh("Initializing..."); triggerStatusJsonRefresh(true,"Initializing...");
ota_status.bRedirectFound=false; ota_status.bRedirectFound=false;
if(passedURL==NULL || strlen(passedURL)==0){ if(passedURL==NULL || strlen(passedURL)==0){
ESP_LOGE(TAG,"HTTP OTA called without a url"); ESP_LOGE(TAG,"HTTP OTA called without a url");
@@ -274,7 +283,7 @@ void ota_task(void *pvParameter)
FREE_RESET(pvParameter); FREE_RESET(pvParameter);
ESP_LOGW(TAG,"**************** Expecting WATCHDOG errors below during flash erase. This is OK and not to worry about **************** "); 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(); esp_err_t err=_erase_last_boot_app_partition();
if(err!=ESP_OK){ if(err!=ESP_OK){
ESP_LOGE(TAG,"Unable to erase last APP partition. Error: %s",esp_err_to_name(err)); 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"); ESP_LOGI(TAG,"Calling esp_https_ota");
init_config(&ota_config,ota_status.bRedirectFound?ota_status.redirected_url:ota_status.current_url); init_config(&ota_config,ota_status.bRedirectFound?ota_status.redirected_url:ota_status.current_url);
ota_status.bOTAStarted = true; ota_status.bOTAStarted = true;
triggerStatusJsonRefresh("Starting OTA..."); triggerStatusJsonRefresh(true,"Starting OTA...");
err = esp_https_ota(&ota_config); err = esp_https_ota(&ota_config);
if (err == ESP_OK) { if (err == ESP_OK) {
triggerStatusJsonRefresh("Success!"); triggerStatusJsonRefresh(true,"Success!");
esp_restart(); esp_restart();
} else { } else {
triggerStatusJsonRefresh("Error: %s",esp_err_to_name(err)); triggerStatusJsonRefresh(true,"Error: %s",esp_err_to_name(err));
wifi_manager_refresh_ota_json(); wifi_manager_refresh_ota_json();
ESP_LOGE(TAG, "Firmware upgrade failed with error : %s", esp_err_to_name(err)); ESP_LOGE(TAG, "Firmware upgrade failed with error : %s", esp_err_to_name(err));
ota_status.bOTAThreadStarted=false; ota_status.bOTAThreadStarted=false;
@@ -305,11 +314,10 @@ void ota_task(void *pvParameter)
} }
esp_err_t process_recovery_ota(const char * bin_url){ esp_err_t process_recovery_ota(const char * bin_url){
#if RECOVERY_APPLICATION
// Initialize NVS. // Initialize NVS.
int ret=0; int ret=0;
esp_err_t err = nvs_flash_init(); esp_err_t err = nvs_flash_init();
if(ota_status.bOTAThreadStarted){ if(ota_status.bOTAThreadStarted){
ESP_LOGE(TAG,"OTA Already started. "); ESP_LOGE(TAG,"OTA Already started. ");
return ESP_FAIL; return ESP_FAIL;
@@ -341,17 +349,18 @@ esp_err_t process_recovery_ota(const char * bin_url){
#endif #endif
ESP_LOGI(TAG, "Starting ota on core %u for : %s", OTA_CORE,urlPtr); 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) { if (ret != pdPASS) {
ESP_LOGI(TAG, "create thread %s failed", "ota_task"); ESP_LOGI(TAG, "create thread %s failed", "ota_task");
return ESP_FAIL; return ESP_FAIL;
} }
#endif
return ESP_OK; return ESP_OK;
} }
esp_err_t start_ota(const char * bin_url, bool bFromAppMain) 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 #if RECOVERY_APPLICATION
return process_recovery_ota(bin_url); return process_recovery_ota(bin_url);
#else #else

View File

@@ -92,7 +92,7 @@ const static char http_redirect_hdr_end[] = "/\n\n";
void http_server_start(){ void http_server_start(){
if(task_http_server == NULL){ 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) { void http_server(void *pvParameters) {
@@ -386,6 +386,8 @@ void http_server_netconn_serve(struct netconn *conn) {
bool bErrorFound=false; bool bErrorFound=false;
bool bOTA=false; bool bOTA=false;
char * otaURL=NULL; char * otaURL=NULL;
// make sure we terminate the netconn string
save_ptr[buflen-1]='\0';
while(last_parm!=NULL){ while(last_parm!=NULL){
// Search will return // Search will return

View File

@@ -56,10 +56,18 @@ Contains the freeRTOS task and all necessary support
#include "lwip/ip4_addr.h" #include "lwip/ip4_addr.h"
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_app_format.h" #include "esp_app_format.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#ifndef SQUEEZELITE_ESP32_RELEASE_URL #ifndef SQUEEZELITE_ESP32_RELEASE_URL
#define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases" #define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
#endif #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 */ /* objects used to manipulate the main queue of events */
QueueHandle_t wifi_manager_queue; 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(config){
#if RECOVERY_APPLICATION #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 #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 #endif
memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE); memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE);
const esp_app_desc_t* desc = esp_ota_get_app_description(); 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, gw,
(int)update_reason_code, (int)update_reason_code,
desc->project_name, desc->project_name,
desc->version desc->version,
#if RECOVERY_APPLICATION #if RECOVERY_APPLICATION
,ota_get_status(), ota_get_status(),
ota_get_pct_complete() ota_get_pct_complete(),
#endif #endif
JACK_LEVEL,
adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1
); );
} }
else{ else{
@@ -421,31 +431,39 @@ void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code)
"0", "0",
(int)update_reason_code, (int)update_reason_code,
desc->project_name, desc->project_name,
desc->version desc->version,
#if RECOVERY_APPLICATION #if RECOVERY_APPLICATION
,"", "",
0 0,
#endif #endif
JACK_LEVEL,
adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1
); );
} }
} }
else{ else{
#if RECOVERY_APPLICATION #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 #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 #endif
memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE); memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE);
const esp_app_desc_t* desc = esp_ota_get_app_description(); 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 */ /* 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, snprintf( (ip_info_json + strlen(ip_info_json)), JSON_IP_INFO_SIZE, ip_info_json_format,
desc->project_name, desc->project_name,
desc->version desc->version,
#if RECOVERY_APPLICATION #if RECOVERY_APPLICATION
,ota_get_status(), ota_get_status(),
ota_get_pct_complete() ota_get_pct_complete(),
#endif #endif
JACK_LEVEL,
adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1
); );
} }
} }

View File

@@ -59,7 +59,7 @@ void process_autoexec(){
if(str_flag !=NULL ){ if(str_flag !=NULL ){
autoexec_flag=atoi(str_flag); 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) { if(autoexec_flag == 1) {
do { do {
snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++); snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);

View File

@@ -99,6 +99,29 @@ esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data,
nvs_close(nvs); nvs_close(nvs);
return err; 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) { void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
nvs_handle nvs; nvs_handle nvs;
esp_err_t err; esp_err_t err;

View File

@@ -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); 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); void * get_nvs_value_alloc(nvs_type_t type, const char *key);
esp_err_t erase_nvs(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 #ifdef __cplusplus
} }
#endif #endif