Full OTA refactor and other stability improvement

This commit is contained in:
Sebastien
2019-09-29 21:12:02 -04:00
parent 828aaf4760
commit 0ab1cd438b
14 changed files with 305 additions and 352 deletions

View File

@@ -59,11 +59,11 @@ static const char TAG[] = "dns_server";
static TaskHandle_t task_dns_server = NULL;
int socket_fd;
void dns_server_start() {
void CODE_RAM_LOCATION dns_server_start() {
xTaskCreate(&dns_server, "dns_server", 3072, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_dns_server);
}
void dns_server_stop(){
void CODE_RAM_LOCATION dns_server_stop(){
if(task_dns_server){
vTaskDelete(task_dns_server);
close(socket_fd);
@@ -74,7 +74,7 @@ void dns_server_stop(){
void dns_server(void *pvParameters) {
void CODE_RAM_LOCATION dns_server(void *pvParameters) {

View File

@@ -34,7 +34,7 @@ Contains the freeRTOS task for the DNS server that processes the requests.
#define MAIN_DNS_SERVER_H_
#include <esp_system.h>
#include <stdbool.h>
#include "squeezelite-ota.h"
#ifdef __cplusplus
extern "C" {
@@ -126,9 +126,9 @@ typedef struct __attribute__((__packed__)) dns_answer_t{
uint32_t RDATA; /* For the sake of simplicity only ipv4 is supported, and as such it's a unsigned 32 bit */
}dns_answer_t;
void dns_server(void *pvParameters);
void dns_server_start();
void dns_server_stop();
void CODE_RAM_LOCATION dns_server(void *pvParameters);
void CODE_RAM_LOCATION dns_server_start();
void CODE_RAM_LOCATION dns_server_stop();

View File

@@ -33,6 +33,7 @@ function to process requests, decode URLs, serve files, etc. etc.
#include "http_server.h"
#include "cmd_system.h"
#include "../squeezelite-ota/squeezelite-ota.h"
#define NVS_PARTITION_NAME "nvs"
@@ -48,7 +49,6 @@ static char *r = "\\\"";
static TaskHandle_t task_http_server = NULL;
extern char current_namespace[];
extern void start_ota(const char * bin_url);
/**
* @brief embedded binary data.
* @see file "component.mk"
@@ -86,14 +86,14 @@ const static char http_redirect_hdr_end[] = "/\n\n";
void http_server_start(){
void CODE_RAM_LOCATION http_server_start(){
if(task_http_server == NULL){
xTaskCreate(&http_server, "http_server", 1024*5, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_http_server);
}
}
void http_server(void *pvParameters) {
void CODE_RAM_LOCATION http_server(void *pvParameters) {
struct netconn *conn, *newconn;
err_t err;
@@ -121,7 +121,7 @@ void http_server(void *pvParameters) {
}
char* http_server_get_header(char *request, char *header_name, int *len) {
char* CODE_RAM_LOCATION http_server_get_header(char *request, char *header_name, int *len) {
*len = 0;
char *ret = NULL;
char *ptr = NULL;
@@ -138,7 +138,7 @@ char* http_server_get_header(char *request, char *header_name, int *len) {
}
return NULL;
}
char* http_server_search_header(char *request, char *header_name, int *len, char ** parm_name, char ** next_position, char * bufEnd) {
char* CODE_RAM_LOCATION http_server_search_header(char *request, char *header_name, int *len, char ** parm_name, char ** next_position, char * bufEnd) {
*len = 0;
char *ret = NULL;
char *ptr = NULL;
@@ -191,7 +191,7 @@ char* http_server_search_header(char *request, char *header_name, int *len, char
ESP_LOGD(TAG, "No more match for : %s", header_name);
return NULL;
}
void http_server_send_resource_file(struct netconn *conn,const uint8_t * start, const uint8_t * end, char * content_type,char * encoding){
void CODE_RAM_LOCATION http_server_send_resource_file(struct netconn *conn,const uint8_t * start, const uint8_t * end, char * content_type,char * encoding){
uint16_t len=end - start;
size_t buff_length= sizeof(http_hdr_template)+strlen(content_type)+strlen(encoding);
char * http_hdr=malloc(buff_length);
@@ -210,7 +210,7 @@ void http_server_send_resource_file(struct netconn *conn,const uint8_t * start,
}
}
void http_server_netconn_serve(struct netconn *conn) {
void CODE_RAM_LOCATION http_server_netconn_serve(struct netconn *conn) {
struct netbuf *inbuf;
char *buf = NULL;
@@ -395,10 +395,10 @@ void http_server_netconn_serve(struct netconn *conn) {
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY); //400 invalid request
}
else{
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
if(bOTA){
ESP_LOGI(TAG, "Initiating OTA for url %s",otaURL);
start_ota(otaURL);
ESP_LOGI(TAG, "Restarting to process OTA for url %s",otaURL);
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
esp_restart();
}
}
@@ -475,7 +475,7 @@ void http_server_netconn_serve(struct netconn *conn) {
netbuf_delete(inbuf);
}
void strreplace(char *src, char *str, char *rep)
void CODE_RAM_LOCATION strreplace(char *src, char *str, char *rep)
{
char *p = strstr(src, str);
if (p)

View File

@@ -67,13 +67,13 @@ extern "C" {
* @brief RTOS task for the HTTP server. Do not start manually.
* @see void http_server_start()
*/
void http_server(void *pvParameters);
void CODE_RAM_LOCATION http_server(void *pvParameters);
/* @brief helper function that processes one HTTP request at a time */
void http_server_netconn_serve(struct netconn *conn);
void CODE_RAM_LOCATION http_server_netconn_serve(struct netconn *conn);
/* @brief create the task for the http server */
void http_server_start();
void CODE_RAM_LOCATION http_server_start();
/**
* @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
@@ -86,9 +86,9 @@ void http_server_start();
* @param len the size of the header value if found.
* @return pointer to the beginning of the header value.
*/
char* http_server_get_header(char *request, char *header_name, int *len);
char* CODE_RAM_LOCATION http_server_get_header(char *request, char *header_name, int *len);
void strreplace(char *src, char *str, char *rep);
void CODE_RAM_LOCATION strreplace(char *src, char *str, char *rep);
#ifdef __cplusplus

View File

@@ -56,7 +56,6 @@ Contains the freeRTOS task and all necessary support
#include "lwip/ip4_addr.h"
#include "esp_ota_ops.h"
#include "esp_app_format.h"
#include "squeezelite-ota.h"
#ifndef SQUEEZELITE_ESP32_RELEASE_URL
#define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
@@ -131,21 +130,21 @@ const int WIFI_MANAGER_SCAN_BIT = BIT7;
const int WIFI_MANAGER_REQUEST_DISCONNECT_BIT = BIT8;
void wifi_manager_refresh_ota_json(){
void CODE_RAM_LOCATION wifi_manager_refresh_ota_json(){
wifi_manager_send_message(EVENT_REFRESH_OTA, NULL);
}
void wifi_manager_scan_async(){
void CODE_RAM_LOCATION wifi_manager_scan_async(){
wifi_manager_send_message(ORDER_START_WIFI_SCAN, NULL);
}
void wifi_manager_disconnect_async(){
void CODE_RAM_LOCATION wifi_manager_disconnect_async(){
wifi_manager_send_message(ORDER_DISCONNECT_STA, NULL);
//xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT); TODO: delete
}
void wifi_manager_start(){
void CODE_RAM_LOCATION wifi_manager_start(){
/* disable the default wifi logging */
esp_log_level_set("wifi", ESP_LOG_NONE);
@@ -176,7 +175,7 @@ void wifi_manager_start(){
xTaskCreate(&wifi_manager, "wifi_manager", 4096, NULL, WIFI_MANAGER_TASK_PRIORITY, &task_wifi_manager);
}
uint8_t wifi_manager_get_flag(){
uint8_t CODE_RAM_LOCATION wifi_manager_get_flag(){
uint8_t value=0;
nvs_handle handle;
esp_err_t esp_err;
@@ -191,7 +190,7 @@ uint8_t wifi_manager_get_flag(){
}
char * wifi_manager_alloc_get_config(char * name, size_t * l){
char * CODE_RAM_LOCATION wifi_manager_alloc_get_config(char * name, size_t * l){
size_t len=0;
char * value=NULL;
@@ -224,7 +223,7 @@ char * wifi_manager_alloc_get_config(char * name, size_t * l){
}
esp_err_t wifi_manager_save_autoexec_flag(uint8_t flag){
esp_err_t CODE_RAM_LOCATION wifi_manager_save_autoexec_flag(uint8_t flag){
nvs_handle handle;
esp_err_t esp_err;
ESP_LOGI(TAG, "About to save autoexec flag to flash");
@@ -257,7 +256,7 @@ esp_err_t wifi_manager_save_autoexec_flag(uint8_t flag){
return ESP_OK;
}
esp_err_t wifi_manager_save_autoexec_config(char * value, char * name, int len){
esp_err_t CODE_RAM_LOCATION wifi_manager_save_autoexec_config(char * value, char * name, int len){
nvs_handle handle;
esp_err_t esp_err;
ESP_LOGI(TAG, "About to save config to flash. Name: %s, value: %s", name,value);
@@ -290,7 +289,7 @@ esp_err_t wifi_manager_save_autoexec_config(char * value, char * name, int len){
}
esp_err_t wifi_manager_save_sta_config(){
esp_err_t CODE_RAM_LOCATION wifi_manager_save_sta_config(){
nvs_handle handle;
esp_err_t esp_err;
ESP_LOGI(TAG, "About to save config to flash");
@@ -332,7 +331,7 @@ esp_err_t wifi_manager_save_sta_config(){
return ESP_OK;
}
bool wifi_manager_fetch_wifi_sta_config(){
bool CODE_RAM_LOCATION wifi_manager_fetch_wifi_sta_config(){
nvs_handle handle;
esp_err_t esp_err;
@@ -409,12 +408,12 @@ bool wifi_manager_fetch_wifi_sta_config(){
}
void wifi_manager_clear_ip_info_json(){
void CODE_RAM_LOCATION wifi_manager_clear_ip_info_json(){
strcpy(ip_info_json, "{}\n");
}
void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code){
void CODE_RAM_LOCATION 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;
@@ -498,11 +497,11 @@ void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code)
}
void wifi_manager_clear_access_points_json(){
void CODE_RAM_LOCATION wifi_manager_clear_access_points_json(){
strcpy(accessp_json, "[]\n");
}
void wifi_manager_generate_acess_points_json(){
void CODE_RAM_LOCATION wifi_manager_generate_acess_points_json(){
strcpy(accessp_json, "[");
@@ -531,7 +530,7 @@ void wifi_manager_generate_acess_points_json(){
}
bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait){
bool CODE_RAM_LOCATION wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait){
if(wifi_manager_sta_ip_mutex){
if( xSemaphoreTake( wifi_manager_sta_ip_mutex, xTicksToWait ) == pdTRUE ) {
return true;
@@ -546,11 +545,11 @@ bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait){
}
void wifi_manager_unlock_sta_ip_string(){
void CODE_RAM_LOCATION wifi_manager_unlock_sta_ip_string(){
xSemaphoreGive( wifi_manager_sta_ip_mutex );
}
void wifi_manager_safe_update_sta_ip_string(uint32_t ip){
void CODE_RAM_LOCATION wifi_manager_safe_update_sta_ip_string(uint32_t ip){
if(wifi_manager_lock_sta_ip_string(portMAX_DELAY)){
struct ip4_addr ip4;
@@ -566,11 +565,11 @@ void wifi_manager_safe_update_sta_ip_string(uint32_t ip){
}
}
char* wifi_manager_get_sta_ip_string(){
char* CODE_RAM_LOCATION wifi_manager_get_sta_ip_string(){
return wifi_manager_sta_ip;
}
bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait){
bool CODE_RAM_LOCATION wifi_manager_lock_json_buffer(TickType_t xTicksToWait){
if(wifi_manager_json_mutex){
if( xSemaphoreTake( wifi_manager_json_mutex, xTicksToWait ) == pdTRUE ) {
return true;
@@ -585,15 +584,15 @@ bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait){
}
void wifi_manager_unlock_json_buffer(){
void CODE_RAM_LOCATION wifi_manager_unlock_json_buffer(){
xSemaphoreGive( wifi_manager_json_mutex );
}
char* wifi_manager_get_ap_list_json(){
char* CODE_RAM_LOCATION wifi_manager_get_ap_list_json(){
return accessp_json;
}
esp_err_t wifi_manager_event_handler(void *ctx, system_event_t *event)
esp_err_t CODE_RAM_LOCATION wifi_manager_event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
@@ -667,11 +666,11 @@ esp_err_t wifi_manager_event_handler(void *ctx, system_event_t *event)
return ESP_OK;
}
wifi_config_t* wifi_manager_get_wifi_sta_config(){
wifi_config_t* CODE_RAM_LOCATION wifi_manager_get_wifi_sta_config(){
return wifi_manager_config_sta;
}
void wifi_manager_connect_async(){
void CODE_RAM_LOCATION wifi_manager_connect_async(){
/* in order to avoid a false positive on the front end app we need to quickly flush the ip json
* There'se a risk the front end sees an IP or a password error when in fact
* it's a remnant from a previous connection
@@ -684,11 +683,11 @@ void wifi_manager_connect_async(){
}
char* wifi_manager_get_ip_info_json(){
char* CODE_RAM_LOCATION wifi_manager_get_ip_info_json(){
return ip_info_json;
}
void wifi_manager_destroy(){
void CODE_RAM_LOCATION wifi_manager_destroy(){
vTaskDelete(task_wifi_manager);
task_wifi_manager = NULL;
@@ -717,7 +716,7 @@ void wifi_manager_destroy(){
wifi_manager_queue = NULL;
}
void wifi_manager_filter_unique( wifi_ap_record_t * aplist, uint16_t * aps) {
void CODE_RAM_LOCATION wifi_manager_filter_unique( wifi_ap_record_t * aplist, uint16_t * aps) {
int total_unique;
wifi_ap_record_t * first_free;
total_unique=*aps;
@@ -768,27 +767,27 @@ void wifi_manager_filter_unique( wifi_ap_record_t * aplist, uint16_t * aps) {
*aps = total_unique;
}
BaseType_t wifi_manager_send_message_to_front(message_code_t code, void *param){
BaseType_t CODE_RAM_LOCATION wifi_manager_send_message_to_front(message_code_t code, void *param){
queue_message msg;
msg.code = code;
msg.param = param;
return xQueueSendToFront( wifi_manager_queue, &msg, portMAX_DELAY);
}
BaseType_t wifi_manager_send_message(message_code_t code, void *param){
BaseType_t CODE_RAM_LOCATION wifi_manager_send_message(message_code_t code, void *param){
queue_message msg;
msg.code = code;
msg.param = param;
return xQueueSend( wifi_manager_queue, &msg, portMAX_DELAY);
}
void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) ){
void CODE_RAM_LOCATION wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) ){
if(cb_ptr_arr && message_code < MESSAGE_CODE_COUNT){
cb_ptr_arr[message_code] = func_ptr;
}
}
void wifi_manager( void * pvParameters ){
void CODE_RAM_LOCATION wifi_manager( void * pvParameters ){
queue_message msg;
BaseType_t xStatus;
EventBits_t uxBits;

View File

@@ -39,13 +39,13 @@ extern "C" {
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "squeezelite-ota.h"
#ifndef RECOVERY_APPLICATION
#error "RECOVERY_APPLICATION not defined. Defaulting to squeezelite"
#endif
#if RECOVERY_APPLICATION==1
#warning "compiling for recovery."
#elif RECOVERY_APPLICATION==0
#warning "compiling for squeezelite."
#else
@@ -268,71 +268,71 @@ typedef struct{
/**
* Allocate heap memory for the wifi manager and start the wifi_manager RTOS task
*/
void wifi_manager_start();
void CODE_RAM_LOCATION wifi_manager_start();
/**
* Frees up all memory allocated by the wifi_manager and kill the task.
*/
void wifi_manager_destroy();
void CODE_RAM_LOCATION wifi_manager_destroy();
/**
* Filters the AP scan list to unique SSIDs
*/
void filter_unique( wifi_ap_record_t * aplist, uint16_t * ap_num);
void CODE_RAM_LOCATION filter_unique( wifi_ap_record_t * aplist, uint16_t * ap_num);
/**
* Main task for the wifi_manager
*/
void wifi_manager( void * pvParameters );
void CODE_RAM_LOCATION wifi_manager( void * pvParameters );
char* wifi_manager_get_ap_list_json();
char* wifi_manager_get_ip_info_json();
char* CODE_RAM_LOCATION wifi_manager_get_ap_list_json();
char* CODE_RAM_LOCATION wifi_manager_get_ip_info_json();
uint8_t wifi_manager_get_flag();
char * wifi_manager_alloc_get_config(char * name, size_t * l);
uint8_t CODE_RAM_LOCATION wifi_manager_get_flag();
char * CODE_RAM_LOCATION wifi_manager_alloc_get_config(char * name, size_t * l);
/**
* @brief saves the current STA wifi config to flash ram storage.
*/
esp_err_t wifi_manager_save_sta_config();
esp_err_t CODE_RAM_LOCATION wifi_manager_save_sta_config();
/**
* @brief saves the current configuration to flash ram storage
*/
esp_err_t wifi_manager_save_autoexec_config(char * value, char * name, int len);
esp_err_t wifi_manager_save_autoexec_flag(uint8_t flag);
esp_err_t CODE_RAM_LOCATION wifi_manager_save_autoexec_config(char * value, char * name, int len);
esp_err_t CODE_RAM_LOCATION wifi_manager_save_autoexec_flag(uint8_t flag);
/**
* @brief fetch a previously STA wifi config in the flash ram storage.
* @return true if a previously saved config was found, false otherwise.
*/
bool wifi_manager_fetch_wifi_sta_config();
bool CODE_RAM_LOCATION wifi_manager_fetch_wifi_sta_config();
wifi_config_t* wifi_manager_get_wifi_sta_config();
wifi_config_t* CODE_RAM_LOCATION wifi_manager_get_wifi_sta_config();
/**
* @brief A standard wifi event handler as recommended by Espressif
*/
esp_err_t wifi_manager_event_handler(void *ctx, system_event_t *event);
esp_err_t CODE_RAM_LOCATION wifi_manager_event_handler(void *ctx, system_event_t *event);
/**
* @brief requests a connection to an access point that will be process in the main task thread.
*/
void wifi_manager_connect_async();
void CODE_RAM_LOCATION wifi_manager_connect_async();
/**
* @brief requests a wifi scan
*/
void wifi_manager_scan_async();
void CODE_RAM_LOCATION wifi_manager_scan_async();
/**
* @brief requests to disconnect and forget about the access point.
*/
void wifi_manager_disconnect_async();
void CODE_RAM_LOCATION wifi_manager_disconnect_async();
/**
* @brief Tries to get access to json buffer mutex.
@@ -349,65 +349,65 @@ void wifi_manager_disconnect_async();
* @param xTicksToWait The time in ticks to wait for the semaphore to become available.
* @return true in success, false otherwise.
*/
bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait);
bool CODE_RAM_LOCATION wifi_manager_lock_json_buffer(TickType_t xTicksToWait);
/**
* @brief Releases the json buffer mutex.
*/
void wifi_manager_unlock_json_buffer();
void CODE_RAM_LOCATION wifi_manager_unlock_json_buffer();
/**
* @brief Generates the connection status json: ssid and IP addresses.
* @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
*/
void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code);
void CODE_RAM_LOCATION wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code);
/**
* @brief Clears the connection status json.
* @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
*/
void wifi_manager_clear_ip_info_json();
void CODE_RAM_LOCATION wifi_manager_clear_ip_info_json();
/**
* @brief Generates the list of access points after a wifi scan.
* @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
*/
void wifi_manager_generate_acess_points_json();
void CODE_RAM_LOCATION wifi_manager_generate_acess_points_json();
/**
* @brief Clear the list of access points.
* @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
*/
void wifi_manager_clear_access_points_json();
void CODE_RAM_LOCATION wifi_manager_clear_access_points_json();
/**
* @brief Start the mDNS service
*/
void wifi_manager_initialise_mdns();
void CODE_RAM_LOCATION wifi_manager_initialise_mdns();
bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait);
void wifi_manager_unlock_sta_ip_string();
bool CODE_RAM_LOCATION wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait);
void CODE_RAM_LOCATION wifi_manager_unlock_sta_ip_string();
/**
* @brief gets the string representation of the STA IP address, e.g.: "192.168.1.69"
*/
char* wifi_manager_get_sta_ip_string();
char* CODE_RAM_LOCATION wifi_manager_get_sta_ip_string();
/**
* @brief thread safe char representation of the STA IP update
*/
void wifi_manager_safe_update_sta_ip_string(uint32_t ip);
void CODE_RAM_LOCATION wifi_manager_safe_update_sta_ip_string(uint32_t ip);
/**
* @brief Register a callback to a custom function when specific event message_code happens.
*/
void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) );
void CODE_RAM_LOCATION wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) );
BaseType_t wifi_manager_send_message(message_code_t code, void *param);
BaseType_t wifi_manager_send_message_to_front(message_code_t code, void *param);
BaseType_t CODE_RAM_LOCATION wifi_manager_send_message(message_code_t code, void *param);
BaseType_t CODE_RAM_LOCATION wifi_manager_send_message_to_front(message_code_t code, void *param);
#ifdef __cplusplus
}