|
@@ -21,7 +21,9 @@
|
|
|
#include "esp_err.h"
|
|
|
#include "tcpip_adapter.h"
|
|
|
#include "squeezelite-ota.h"
|
|
|
-
|
|
|
+#include <time.h>
|
|
|
+#include <sys/time.h>
|
|
|
+#include <stdarg.h>
|
|
|
|
|
|
static const char *TAG = "squeezelite-ota";
|
|
|
extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
|
|
@@ -37,15 +39,33 @@ static struct {
|
|
|
bool bRedirectFound;
|
|
|
bool bOTAStarted;
|
|
|
bool bInitialized;
|
|
|
-} ota_status;
|
|
|
-uint8_t lastpct=0;
|
|
|
-uint8_t newpct=0;
|
|
|
+ uint8_t lastpct;
|
|
|
+ uint8_t newpct;
|
|
|
+ struct timeval OTA_start;
|
|
|
+ bool bOTAThreadStarted;
|
|
|
|
|
|
-static esp_http_client_config_t config;
|
|
|
+} ota_status;
|
|
|
+struct timeval tv;
|
|
|
static esp_http_client_config_t ota_config;
|
|
|
-static esp_http_client_handle_t client;
|
|
|
-extern void wifi_manager_refresh_ota_json();
|
|
|
-const char * ota_get_status(){
|
|
|
+
|
|
|
+extern void CODE_RAM_LOCATION wifi_manager_refresh_ota_json();
|
|
|
+
|
|
|
+void CODE_RAM_LOCATION _printMemStats(){
|
|
|
+ ESP_LOGI(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));
|
|
|
+}
|
|
|
+void triggerStatusJsonRefresh(const char * status, ...){
|
|
|
+ va_list args;
|
|
|
+ va_start(args, status);
|
|
|
+ snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,status, args);
|
|
|
+ va_end(args);
|
|
|
+ _printMemStats();
|
|
|
+ wifi_manager_refresh_ota_json();
|
|
|
+}
|
|
|
+const char * CODE_RAM_LOCATION ota_get_status(){
|
|
|
if(!ota_status.bInitialized)
|
|
|
{
|
|
|
memset(ota_status.status_text, 0x00,sizeof(ota_status.status_text));
|
|
@@ -53,7 +73,7 @@ const char * ota_get_status(){
|
|
|
}
|
|
|
return ota_status.status_text;
|
|
|
}
|
|
|
-uint8_t ota_get_pct_complete(){
|
|
|
+uint8_t CODE_RAM_LOCATION 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);
|
|
|
}
|
|
@@ -68,7 +88,7 @@ static void __attribute__((noreturn)) task_fatal_error(void)
|
|
|
}
|
|
|
}
|
|
|
#define FREE_RESET(p) if(p!=NULL) { free(p); p=NULL; }
|
|
|
-esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
+esp_err_t CODE_RAM_LOCATION _http_event_handler(esp_http_client_event_t *evt)
|
|
|
{
|
|
|
// --------------
|
|
|
// Received parameters
|
|
@@ -88,21 +108,19 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
switch (evt->event_id) {
|
|
|
case HTTP_EVENT_ERROR:
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
|
|
+ _printMemStats();
|
|
|
//strncpy(ota_status,"HTTP_EVENT_ERROR",sizeof(ota_status)-1);
|
|
|
break;
|
|
|
case HTTP_EVENT_ON_CONNECTED:
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
|
|
|
- if(ota_status.bOTAStarted) snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,"Installing...");
|
|
|
- wifi_manager_refresh_ota_json();
|
|
|
- ota_status.ota_total_len=0;
|
|
|
+
|
|
|
+ if(ota_status.bOTAStarted) triggerStatusJsonRefresh("Installing...");
|
|
|
+ ota_status.ota_total_len=0;
|
|
|
ota_status.ota_actual_len=0;
|
|
|
- lastpct=0;
|
|
|
- newpct=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));
|
|
|
+ ota_status.lastpct=0;
|
|
|
+ ota_status.newpct=0;
|
|
|
+ gettimeofday(&ota_status.OTA_start, NULL);
|
|
|
+
|
|
|
break;
|
|
|
case HTTP_EVENT_HEADER_SENT:
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
|
|
@@ -121,18 +139,23 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
}
|
|
|
break;
|
|
|
case HTTP_EVENT_ON_DATA:
|
|
|
+
|
|
|
vTaskDelay(5/ portTICK_RATE_MS);
|
|
|
if(!ota_status.bOTAStarted) {
|
|
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, status_code=%d, len=%d",esp_http_client_get_status_code(evt->client), evt->data_len);
|
|
|
}
|
|
|
else if(ota_status.bOTAStarted && esp_http_client_get_status_code(evt->client) == 200 ){
|
|
|
ota_status.ota_actual_len+=evt->data_len;
|
|
|
- if(ota_get_pct_complete()%2 == 0) newpct = ota_get_pct_complete();
|
|
|
- if(lastpct!=newpct )
|
|
|
+ if(ota_get_pct_complete()%2 == 0) ota_status.newpct = ota_get_pct_complete();
|
|
|
+ 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;
|
|
|
+
|
|
|
wifi_manager_refresh_ota_json();
|
|
|
- lastpct=newpct;
|
|
|
- ESP_LOGI(TAG,"Receiving OTA data chunk len: %d, %d of %d (%d pct)", evt->data_len, ota_status.ota_actual_len, ota_status.ota_total_len, newpct);
|
|
|
+ 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),
|
|
@@ -156,35 +179,11 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-static void check_http_redirect(void)
|
|
|
-{
|
|
|
- esp_err_t err=ESP_OK;
|
|
|
- ESP_LOGD(TAG, "Checking for http redirects. initializing http client");
|
|
|
- client = esp_http_client_init(&config);
|
|
|
- if (client == NULL) {
|
|
|
- ESP_LOGE(TAG, "Failed to initialise HTTP connection");
|
|
|
- task_fatal_error();
|
|
|
- }
|
|
|
- ESP_LOGD(TAG, "opening http connection");
|
|
|
- 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);
|
|
|
- task_fatal_error();
|
|
|
- }
|
|
|
- ESP_LOGD(TAG, "fetching headers");
|
|
|
- esp_http_client_fetch_headers(client);
|
|
|
- if (err == ESP_OK) {
|
|
|
- ESP_LOGI(TAG, "redirect check retrieved headers");
|
|
|
- } else {
|
|
|
- ESP_LOGE(TAG, "redirect check returned %s", esp_err_to_name(err));
|
|
|
- }
|
|
|
-}
|
|
|
-esp_err_t init_config(esp_http_client_config_t * conf, const char * url){
|
|
|
+esp_err_t CODE_RAM_LOCATION 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 = 2048;
|
|
|
+ conf->buffer_size = 1024*2;
|
|
|
conf->disable_auto_redirect=true;
|
|
|
conf->skip_cert_common_name_check = false;
|
|
|
conf->url = strdup(url);
|
|
@@ -192,14 +191,13 @@ esp_err_t init_config(esp_http_client_config_t * conf, const char * url){
|
|
|
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
-void ota_task(void *pvParameter)
|
|
|
+void CODE_RAM_LOCATION ota_task(void *pvParameter)
|
|
|
{
|
|
|
char * passedURL=(char *)pvParameter;
|
|
|
- memset(&ota_status, 0x00, sizeof(ota_status));
|
|
|
+
|
|
|
ota_status.bInitialized = true;
|
|
|
ESP_LOGD(TAG, "HTTP ota Thread started");
|
|
|
- snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,"Initializing...");
|
|
|
- wifi_manager_refresh_ota_json();
|
|
|
+ triggerStatusJsonRefresh("Initializing...");
|
|
|
ota_status.bRedirectFound=false;
|
|
|
if(passedURL==NULL || strlen(passedURL)==0){
|
|
|
ESP_LOGE(TAG,"HTTP OTA called without a url");
|
|
@@ -207,32 +205,17 @@ void ota_task(void *pvParameter)
|
|
|
return ;
|
|
|
}
|
|
|
ota_status.current_url= strdup(passedURL);
|
|
|
-// init_config(&config,ota_status.current_url);
|
|
|
-
|
|
|
FREE_RESET(pvParameter);
|
|
|
-//
|
|
|
-// snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,"Checking for redirect...");
|
|
|
-// wifi_manager_refresh_ota_json();
|
|
|
-// check_http_redirect();
|
|
|
-// if(ota_status.bRedirectFound && ota_status.redirected_url== NULL){
|
|
|
-// // OTA Failed miserably. Errors would have been logged somewhere
|
|
|
-// ESP_LOGE(TAG,"Redirect check failed to identify target URL. Bailing out");
|
|
|
-// vTaskDelete(NULL);
|
|
|
-// }
|
|
|
ESP_LOGD(TAG,"Calling esp_https_ota");
|
|
|
init_config(&ota_config,ota_status.bRedirectFound?ota_status.redirected_url:ota_status.current_url);
|
|
|
ota_status.bOTAStarted = true;
|
|
|
- snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,"Starting OTA...");
|
|
|
- wifi_manager_refresh_ota_json();
|
|
|
- // pause to let the system catch up
|
|
|
- vTaskDelay(500/ portTICK_RATE_MS);
|
|
|
+ triggerStatusJsonRefresh("Starting OTA...");
|
|
|
esp_err_t err = esp_https_ota(&ota_config);
|
|
|
if (err == ESP_OK) {
|
|
|
- snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,"Success!");
|
|
|
- wifi_manager_refresh_ota_json();
|
|
|
+ triggerStatusJsonRefresh("Success!");
|
|
|
esp_restart();
|
|
|
} else {
|
|
|
- snprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,"Error: %s",esp_err_to_name(err));
|
|
|
+ triggerStatusJsonRefresh("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));
|
|
|
}
|
|
@@ -242,97 +225,60 @@ void ota_task(void *pvParameter)
|
|
|
vTaskDelete(NULL);
|
|
|
}
|
|
|
|
|
|
-void start_ota(const char * bin_url)
|
|
|
-{
|
|
|
- ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
|
|
|
+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();
|
|
|
+ nvs_handle nvs;
|
|
|
+ esp_err_t err = nvs_flash_init();
|
|
|
+
|
|
|
+ if(ota_status.bOTAThreadStarted){
|
|
|
+ ESP_LOGE(TAG,"OTA Already started. ");
|
|
|
+ return ESP_FAIL;
|
|
|
+ }
|
|
|
+ memset(&ota_status, 0x00, sizeof(ota_status));
|
|
|
+ ota_status.bOTAThreadStarted=true;
|
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
|
// todo: If we ever change the size of the nvs partition, we need to figure out a mechanism to enlarge the nvs.
|
|
|
// 1.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.
|
|
|
// 2.NVS partition contains data in new format and cannot be recognized by this version of code.
|
|
|
// If this happens, we erase NVS partition and initialize NVS again.
|
|
|
- ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
+ ESP_LOGW(TAG,"NVS flash size has changed. Formatting nvs");
|
|
|
+ ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
err = nvs_flash_init();
|
|
|
}
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
- char * urlPtr=malloc((strlen(bin_url)+1)*sizeof(char));
|
|
|
- strcpy(urlPtr,bin_url);
|
|
|
+ char * urlPtr=strdup(bin_url);
|
|
|
// the first thing we need to do here is to erase the firmware url
|
|
|
// to avoid a boot loop
|
|
|
- nvs_handle nvs;
|
|
|
|
|
|
- err = nvs_open(current_namespace, NVS_READWRITE, &nvs);
|
|
|
- if (err == ESP_OK) {
|
|
|
- err = nvs_erase_key(nvs, "fwurl");
|
|
|
- if (err == ESP_OK) {
|
|
|
- err = nvs_commit(nvs);
|
|
|
- if (err == ESP_OK) {
|
|
|
- ESP_LOGI(TAG, "Value with key '%s' erased", "fwurl");
|
|
|
- }
|
|
|
- }
|
|
|
- nvs_close(nvs);
|
|
|
- }
|
|
|
- ESP_LOGI(TAG, "Waiting for other processes to start");
|
|
|
- for(int i=0;i<10;i++){
|
|
|
- vTaskDelay(1000/ portTICK_RATE_MS);
|
|
|
- }
|
|
|
+
|
|
|
#ifdef CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1
|
|
|
#define OTA_CORE 0
|
|
|
-#warning "Wifi running on core 1"
|
|
|
+#warning "OTA will run on core 0"
|
|
|
#else
|
|
|
+#warning "OTA will run on core 1"
|
|
|
#define OTA_CORE 1
|
|
|
- #endif
|
|
|
- ESP_LOGI(TAG, "Starting ota: %s", urlPtr);
|
|
|
- ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*40, (void *)urlPtr, tskIDLE_PRIORITY+3, NULL, OTA_CORE);
|
|
|
-
|
|
|
+#endif
|
|
|
+ ESP_LOGI(TAG, "Starting ota on core %u for : %s", OTA_CORE,urlPtr);
|
|
|
+ ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*40, (void *)urlPtr, ESP_TASK_MAIN_PRIO+3, NULL, OTA_CORE);
|
|
|
if (ret != pdPASS) {
|
|
|
ESP_LOGI(TAG, "create thread %s failed", "ota_task");
|
|
|
- }
|
|
|
-#else
|
|
|
- ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
|
|
|
- guided_factory();
|
|
|
+ return ESP_FAIL;
|
|
|
+ }
|
|
|
#endif
|
|
|
-
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+esp_err_t start_ota(const char * bin_url, bool bFromAppMain)
|
|
|
+{
|
|
|
+#if RECOVERY_APPLICATION
|
|
|
+ return process_recovery_ota(bin_url);
|
|
|
+#else
|
|
|
+ ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
|
|
|
+ ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
|
|
|
+ return guided_factory();
|
|
|
+ return ESP_OK;
|
|
|
+#endif
|
|
|
+}
|