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-09-29 19:13:56 +02:00
16 changed files with 2493 additions and 39 deletions

View File

@@ -24,6 +24,8 @@
#include "esp32/rom/uart.h"
#include "cmd_system.h"
#include "sdkconfig.h"
#include "esp_partition.h"
#include "esp_ota_ops.h"
#ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
#define WITH_TASKS_INFO 1
@@ -102,9 +104,14 @@ static int restart(int argc, char **argv)
void guided_factory()
{
ESP_LOGI(TAG, "Rebooting to factory.");
uint32_t *p_force_factory_magic = (uint32_t *)LWS_MAGIC_REBOOT_TYPE_ADS;
*p_force_factory_magic = LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY;
const esp_partition_t *partition;
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, "factory");
partition = (esp_partition_t *) esp_partition_get(it);
if(partition != NULL){
esp_ota_set_boot_partition(partition);
}
esp_partition_iterator_release(it);
esp_restart();
}

View File

@@ -6,5 +6,5 @@
# todo: add support for https
COMPONENT_ADD_INCLUDEDIRS := .
COMPONENT_ADD_INCLUDEDIRS += include
CFLAGS += -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCONFIG_OTA_ALLOW_HTTP=1
CFLAGS += -DLOG_LOCAL_LEVEL=ESP_LOG_INFO -DCONFIG_OTA_ALLOW_HTTP=1
COMPONENT_EMBED_TXTFILES := ${PROJECT_PATH}/server_certs/github.pem

View File

@@ -98,12 +98,11 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
ota_status.ota_actual_len=0;
lastpct=0;
newpct=0;
wifi_manager_refresh_ota_json();
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));
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_HEADER_SENT:
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
@@ -122,17 +121,18 @@ 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()%5 == 0) newpct = ota_get_pct_complete();
if(ota_get_pct_complete()%2 == 0) newpct = ota_get_pct_complete();
if(lastpct!=newpct )
{
wifi_manager_refresh_ota_json();
lastpct=newpct;
ESP_LOGD(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);
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);
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),
@@ -184,7 +184,7 @@ 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 = 1024*2;
conf->buffer_size = 2048;
conf->disable_auto_redirect=true;
conf->skip_cert_common_name_check = false;
conf->url = strdup(url);
@@ -207,26 +207,26 @@ void ota_task(void *pvParameter)
return ;
}
ota_status.current_url= strdup(passedURL);
init_config(&config,ota_status.current_url);
// 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);
}
//
// 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(1500/ portTICK_RATE_MS);
esp_err_t err = esp_https_ota(&config);
vTaskDelay(500/ portTICK_RATE_MS);
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();
@@ -277,9 +277,18 @@ void start_ota(const char * bin_url)
nvs_close(nvs);
}
ESP_LOGI(TAG, "Waiting for other processes to start");
vTaskDelay(2500/ portTICK_RATE_MS);
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"
#else
#define OTA_CORE 1
#endif
ESP_LOGI(TAG, "Starting ota: %s", urlPtr);
ret=xTaskCreate(&ota_task, "ota_task", 1024*10,(void *) urlPtr, 4, NULL);
ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*40, (void *)urlPtr, tskIDLE_PRIORITY+3, NULL, OTA_CORE);
if (ret != pdPASS) {
ESP_LOGI(TAG, "create thread %s failed", "ota_task");
}

View File

@@ -38,7 +38,7 @@ function to process requests, decode URLs, serve files, etc. etc.
/* @brief tag used for ESP serial console messages */
static const char TAG[] = "http_server";
static const char json_start[] = "{ \"recovery\": %u, \"autoexec\": %u, \"list\": [";
static const char json_start[] = "{ \"autoexec\": %u, \"list\": [";
static const char json_end[] = "]}";
static const char template[] = "{ \"%s\": \"%s\" }";
static const char array_separator[]=",";
@@ -312,7 +312,7 @@ void http_server_netconn_serve(struct netconn *conn) {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
autoexec_flag = wifi_manager_get_flag();
snprintf(config_buffer,locbuflen-1, json_start, RECOVERY_APPLICATION, autoexec_flag);
snprintf(config_buffer,locbuflen-1, json_start, autoexec_flag);
netconn_write(conn, config_buffer, strlen(config_buffer), NETCONN_NOCOPY);
ESP_LOGI(TAG, "About to get config from flash");

View File

@@ -73,6 +73,8 @@ wifi_ap_record_t *accessp_records;
char *accessp_json = NULL;
char *ip_info_json = NULL;
wifi_config_t* wifi_manager_config_sta = NULL;
static update_reason_code_t last_update_reason_code=0;
void (**cb_ptr_arr)(void*) = NULL;
@@ -414,11 +416,18 @@ void wifi_manager_clear_ip_info_json(){
void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code){
wifi_config_t *config = wifi_manager_get_wifi_sta_config();
if(update_reason_code == UPDATE_OTA) {
update_reason_code = last_update_reason_code;
}
else
{
last_update_reason_code = 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}\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}\n";
#else
const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\"}\n";
const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\",\"recovery\": 0}\n";
#endif
memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE);
const esp_app_desc_t* desc = esp_ota_get_app_description();
@@ -900,6 +909,12 @@ void wifi_manager( void * pvParameters ){
if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
break;
case EVENT_REFRESH_OTA:
if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
wifi_manager_generate_ip_info_json( UPDATE_OTA );
wifi_manager_unlock_json_buffer();
}
break;
case ORDER_START_WIFI_SCAN:
ESP_LOGD(TAG, "MESSAGE: ORDER_START_WIFI_SCAN");
@@ -1155,7 +1170,6 @@ void wifi_manager( void * pvParameters ){
if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
break;
default:
break;

View File

@@ -226,7 +226,8 @@ typedef enum update_reason_code_t {
UPDATE_CONNECTION_OK = 0,
UPDATE_FAILED_ATTEMPT = 1,
UPDATE_USER_DISCONNECT = 2,
UPDATE_LOST_CONNECTION = 3
UPDATE_LOST_CONNECTION = 3,
UPDATE_OTA=4
}update_reason_code_t;
typedef enum connection_request_made_by_code_t{