NVS Refactor ** Factory+Squeezelite FLASH required for this branch!

This is a major refactoring of the nvs parameters storage. To support
this, a new partition was added. To start using this branch, one must
flash all binaries and possibly re-configure the system.
This commit is contained in:
Sebastien
2019-10-03 14:15:59 -04:00
parent 725636e0c4
commit b6f1ce9a7a
22 changed files with 168 additions and 196 deletions

View File

@@ -14,3 +14,5 @@ COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../squeezelite-ota
COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/

View File

@@ -50,7 +50,6 @@ static char *s = "\"";
static char *r = "\\\"";
/* @brief task handle for the http server */
static TaskHandle_t task_http_server = NULL;
extern char current_namespace[];
/**
* @brief embedded binary data.
@@ -74,14 +73,14 @@ extern const uint8_t index_html_end[] asm("_binary_index_html_end");
/* const http headers stored in ROM */
const static char http_hdr_template[] = "HTTP/1.1 200 OK\nContent-type: %s\nAccept-Ranges: bytes\nContent-Length: %d\nContent-Encoding: %s\n\n";
const static char http_html_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/html\n\n";
const static char http_css_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/css\nCache-Control: public, max-age=31536000\n\n";
const static char http_js_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\n\n";
const static char http_hdr_template[] = "HTTP/1.1 200 OK\nContent-type: %s\nAccept-Ranges: bytes\nContent-Length: %d\nContent-Encoding: %s\nAccess-Control-Allow-Origin: *\n\n";
const static char http_html_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/html\nAccess-Control-Allow-Origin: *\n\n";
const static char http_css_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/css\nCache-Control: public, max-age=31536000\nAccess-Control-Allow-Origin: *\n\n";
const static char http_js_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\nAccess-Control-Allow-Origin: *\n\n";
const static char http_400_hdr[] = "HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n";
const static char http_404_hdr[] = "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n";
const static char http_503_hdr[] = "HTTP/1.1 503 Service Unavailable\nContent-Length: 0\n\n";
const static char http_ok_json_no_cache_hdr[] = "HTTP/1.1 200 OK\nContent-type: application/json\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\n\n";
const static char http_ok_json_no_cache_hdr[] = "HTTP/1.1 200 OK\nContent-type: application/json\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nAccess-Control-Allow-Origin: *\n\n";
const static char http_redirect_hdr_start[] = "HTTP/1.1 302 Found\nLocation: http://";
const static char http_redirect_hdr_end[] = "/\n\n";
@@ -308,12 +307,18 @@ void http_server_netconn_serve(struct netconn *conn) {
/* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
int lenH = 0;
char *host = http_server_get_header(save_ptr, "Host: ", &lenH);
const char * host_name=NULL;
if((err=tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &host_name )) !=ESP_OK){
ESP_LOGE(TAG,"Unable to get host name. Error: %s",esp_err_to_name(err));
}
/* determine if Host is from the STA IP address */
wifi_manager_lock_sta_ip_string(portMAX_DELAY);
bool access_from_sta_ip = lenH > 0?strstr(host, wifi_manager_get_sta_ip_string()):false;
wifi_manager_unlock_sta_ip_string();
bool access_from_host_name = (host_name!=NULL) && strstr(host, host_name);
if (lenH > 0 && !strstr(host, DEFAULT_AP_IP) && !access_from_sta_ip) {
if (lenH > 0 && !strstr(host, DEFAULT_AP_IP) && !(access_from_sta_ip || access_from_host_name)) {
ESP_LOGI(TAG,"Redirecting to default AP IP Address : %s", DEFAULT_AP_IP);
netconn_write(conn, http_redirect_hdr_start, sizeof(http_redirect_hdr_start) - 1, NETCONN_NOCOPY);
netconn_write(conn, DEFAULT_AP_IP, sizeof(DEFAULT_AP_IP) - 1, NETCONN_NOCOPY);
@@ -402,16 +407,9 @@ void http_server_netconn_serve(struct netconn *conn) {
bOTA=true;
}
else {
if(lenA < MAX_COMMAND_LINE_SIZE ){
ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ Storing parameter");
wifi_manager_save_config(last_parm,last_parm_name,lenA);
}
else
{
ESP_LOGE(TAG,"length is too long : %s = %s", last_parm_name, last_parm);
last_parm=NULL;
bErrorFound=true;
}
err= store_nvs_value(NVS_TYPE_STR, last_parm_name , last_parm);
if(err!=ESP_OK) ESP_LOGE(TAG,"Unable to save nvs value. Error: %s",esp_err_to_name(err));
}
}
if(last_parm_name!=NULL) {

View File

@@ -59,6 +59,7 @@ Contains the freeRTOS task and all necessary support
#include "driver/gpio.h"
#include "driver/adc.h"
#include "cJSON.h"
#include "nvs_utilities.h"
#ifndef RECOVERY_APPLICATION
#define RECOVERY_APPLICATION 0
@@ -84,6 +85,7 @@ uint16_t ap_num = MAX_AP_NUM;
wifi_ap_record_t *accessp_records;
char *accessp_json = NULL;
char *ip_info_json = NULL;
char *host_name = NULL;
cJSON * ip_info_cjson=NULL;
wifi_config_t* wifi_manager_config_sta = NULL;
static update_reason_code_t last_update_reason_code=0;
@@ -113,7 +115,6 @@ struct wifi_settings_t wifi_settings = {
const char wifi_manager_nvs_namespace[] = "config";
extern char current_namespace[];
EventGroupHandle_t wifi_manager_event_group;
@@ -185,78 +186,20 @@ void wifi_manager_start(){
wifi_manager_sta_ip = (char*)malloc(sizeof(char) * IP4ADDR_STRLEN_MAX);
wifi_manager_safe_update_sta_ip_string((uint32_t)0);
host_name = (char * )get_nvs_value_alloc_default(NVS_TYPE_STR, "host_name", "squeezelite-esp32", 0);
char * release_url = (char * )get_nvs_value_alloc_default(NVS_TYPE_STR, "release_url", SQUEEZELITE_ESP32_RELEASE_URL, 0);
if(release_url == NULL){
ESP_LOGE(TAG,"Unable to retrieve the release url from nvs");
}
else {
free(release_url);
}
/* start wifi manager task */
xTaskCreate(&wifi_manager, "wifi_manager", 4096, NULL, WIFI_MANAGER_TASK_PRIORITY, &task_wifi_manager);
}
char * wifi_manager_alloc_get_config(char * name, size_t * l){
size_t len=0;
char * value=NULL;
nvs_handle handle;
ESP_LOGD(TAG, "About to get config value %s from flash",name);
esp_err_t esp_err=nvs_open(current_namespace, NVS_READWRITE, &handle);
if(esp_err==ESP_ERR_NVS_NOT_INITIALIZED){
ESP_LOGE(TAG,"Unable to open nvs namespace %s. nvs is not initialized.",wifi_manager_nvs_namespace);
}
else if( esp_err == ESP_OK) {
if (nvs_get_str(handle, name, NULL, &len)==ESP_OK) {
value=(char *)malloc(len);
memset(value,0x0, len);
nvs_get_str(handle, name, value, &len);
*l=len;
ESP_LOGD(TAG,"Found value %s, length %u = %s",name,*l,value);
}
else
{
ESP_LOGW(TAG, "Value %s does one exist in flash",name);
}
nvs_close(handle);
}
else
{
ESP_LOGE(TAG,"Unable to open nvs namespace %s. Error: %d, %s", wifi_manager_nvs_namespace,esp_err, esp_err_to_name(esp_err));
}
return value;
}
esp_err_t wifi_manager_save_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);
esp_err = nvs_open(current_namespace, NVS_READWRITE, &handle);
if(esp_err==ESP_ERR_NVS_NOT_INITIALIZED){
ESP_LOGE(TAG,"Unable to open nvs namespace %s. nvs is not initialized.",wifi_manager_nvs_namespace);
}
else if (esp_err != ESP_OK) {
ESP_LOGE(TAG,"Unable to open nvs namespace %s. Error: %d, %s", wifi_manager_nvs_namespace, esp_err, esp_err_to_name(esp_err));
return esp_err;
}
esp_err = nvs_set_str(handle, name, value);
if (esp_err != ESP_OK){
ESP_LOGE(TAG,"Unable to save value %s=%s",name,value);
nvs_close(handle);
return esp_err;
}
esp_err = nvs_commit(handle);
if (esp_err != ESP_OK){
ESP_LOGE(TAG,"nvs commit error");
return esp_err;
}
nvs_close(handle);
ESP_LOGD(TAG, "wifi_manager_wrote %s=%s with length %i", name, value, len);
return ESP_OK;
}
esp_err_t wifi_manager_save_sta_config(){
nvs_handle handle;
esp_err_t esp_err;
@@ -626,7 +569,7 @@ char* wifi_manager_get_ip_info_json(){
void wifi_manager_destroy(){
vTaskDelete(task_wifi_manager);
task_wifi_manager = NULL;
free(host_name);
/* heap buffers */
free(accessp_records);
accessp_records = NULL;
@@ -729,7 +672,7 @@ void wifi_manager( void * pvParameters ){
BaseType_t xStatus;
EventBits_t uxBits;
uint8_t retries = 0;
esp_err_t err=ESP_OK;
/* initialize the tcp stack */
tcpip_adapter_init();
@@ -782,7 +725,6 @@ void wifi_manager( void * pvParameters ){
ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, wifi_settings.ap_bandwidth));
ESP_ERROR_CHECK(esp_wifi_set_ps(wifi_settings.sta_power_save));
/* STA - Wifi Station configuration setup */
tcpip_adapter_dhcp_status_t status;
if(wifi_settings.sta_static_ip) {
@@ -802,15 +744,12 @@ void wifi_manager( void * pvParameters ){
ESP_ERROR_CHECK(tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &status));
if (status!=TCPIP_ADAPTER_DHCP_STARTED)
ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
}
}
/* by default the mode is STA because wifi_manager will not start the access point unless it has to! */
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
/* start http server */
http_server_start();
@@ -906,6 +845,10 @@ void wifi_manager( void * pvParameters ){
else{
/* update config to latest and attempt connection */
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, wifi_manager_get_wifi_sta_config()));
ESP_LOGI(TAG,"Setting host name to : %s",host_name);
if((err=tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, host_name)) !=ESP_OK){
ESP_LOGE(TAG,"Unable to set host name. Error: %s",esp_err_to_name(err));
}
ESP_ERROR_CHECK(esp_wifi_connect());
}

View File

@@ -279,19 +279,12 @@ void wifi_manager( void * pvParameters );
char* wifi_manager_get_ap_list_json();
char* wifi_manager_get_ip_info_json();
char * 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();
/**
* @brief saves the current configuration to flash ram storage
*/
esp_err_t wifi_manager_save_config(char * value, char * name, int len);
/**
* @brief fetch a previously STA wifi config in the flash ram storage.