More nvs parameter work

This commit is contained in:
Sebastien
2019-09-22 10:40:37 -04:00
parent daeac0a073
commit aa6366a51f
7 changed files with 372 additions and 208 deletions

View File

@@ -33,7 +33,7 @@ function to process requests, decode URLs, serve files, etc. etc.
#include "http_server.h"
#include "cmd_system.h"
#define NVS_PARTITION_NAME "nvs"
/* @brief tag used for ESP serial console messages */
static const char TAG[] = "http_server";
@@ -134,6 +134,38 @@ 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, int parm_name_max_len, char ** next_position ) {
*len = 0;
char *ret = NULL;
char *ptr = NULL;
ptr = strstr(request, header_name);
if (ptr) {
ret = ptr + strlen(header_name);
ptr = ret;
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r' && *ptr != ':' ) {
ptr++;
}
if(*ptr==':'){
// save the parameter name: the string between header name and ":"
strncpy(parm_name,ret,(ptr-ret-1>parm_name_max_len?parm_name_max_len:ptr-ret-1));
ptr++;
while (*ptr == ' ' ) {
ptr++;
}
}
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') {
(*len)++;
ptr++;
}
// Terminate value inside its actual buffer so we can treat it as individual string
*ptr='\0';
*next_position=ptr;
return ret;
}
return NULL;
}
void http_server_netconn_serve(struct netconn *conn) {
@@ -239,40 +271,43 @@ void http_server_netconn_serve(struct netconn *conn) {
{
int i=1;
size_t l = 0;
esp_err_t esp_err;
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(buff,buflen-1, json_start, RECOVERY_APPLICATION, autoexec_flag);
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
do {
snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i);
ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
autoexec_value = wifi_manager_alloc_get_config(autoexec_name, &l);
if(autoexec_value!=NULL ){
ESP_LOGI(TAG, "About to get config from flash");
nvs_iterator_t it = nvs_entry_find(NVS_PARTITION_NAME, NULL, NVS_TYPE_STR);
if (it == NULL) {
ESP_LOGW(TAG, "No nvs entry found in %s",NVS_PARTITION_NAME );
}
while (it != NULL){
nvs_entry_info_t info;
nvs_entry_info(it, &info);
it = nvs_entry_next(it);
if(strstr(info.namespace_name, current_namespace)){
if(i>1)
{
netconn_write(conn, array_separator, strlen(array_separator), NETCONN_NOCOPY);
ESP_LOGD(TAG,"%s", array_separator);
}
ESP_LOGI(TAG,"found command %s = %s", autoexec_name, autoexec_value);
strreplace(autoexec_value, s, r);
snprintf(buff, buflen-1, template, autoexec_name, autoexec_value);
autoexec_value = wifi_manager_alloc_get_config(info.key, &l);
strreplace(autoexec_value, s, r);
ESP_LOGI(TAG,"Namespace %s, key=%s, value=%s", info.namespace_name, info.key,autoexec_value );
snprintf(buff, buflen-1, template, info.key, autoexec_value);
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
ESP_LOGD(TAG,"%s", buff);
ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name);
free(autoexec_value);
free(autoexec_value );
i++;
}
else {
ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
break;
}
i++;
} while(1);
}
free(buff);
netconn_write(conn, json_end, strlen(json_end), NETCONN_NOCOPY);
ESP_LOGD(TAG,"%s", json_end);
}
}
else if(strstr(line, "POST /config.json ")){
@@ -280,56 +315,47 @@ void http_server_netconn_serve(struct netconn *conn) {
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
int i=1;
int lenS = 0, lenA=0;
char autoexec_name[22]={0};
char autoexec_key[12]={0};
char * autoexec_value=NULL;
char * autoexec_flag_s=NULL;
int lenA=0;
char * last_parm=save_ptr;
char * next_parm=save_ptr;
char * last_parm_name[41]={0};
uint8_t autoexec_flag=0;
autoexec_flag_s = http_server_get_header(save_ptr, "X-Custom-autoexec: ", &lenA);
if(autoexec_flag_s!=NULL && lenA > 0)
{
autoexec_flag = atoi(autoexec_flag_s);
wifi_manager_save_autoexec_flag(autoexec_flag);
bool bErrorFound=false;
while(last_parm!=NULL){
// Search will return
memset(last_parm_name,0x00,sizeof(last_parm_name));
last_parm = http_server_search_header(next_parm, "X-Custom- ", &lenA, last_parm_name, sizeof(last_parm_name)-1,&next_parm);
ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ call, found parameter %s=%s, length %i", last_parm_name, last_parm, lenA);
if(last_parm!=NULL){
if(strstr(last_parm_name, "autoexec")){
autoexec_flag = atoi(last_parm);
wifi_manager_save_autoexec_flag(autoexec_flag);
}
else {
if(lenA < MAX_COMMAND_LINE_SIZE ){
ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ Storing parameter");
wifi_manager_save_autoexec_config(last_parm,last_parm_name,lenA);
}
else
{
char szErrorPrefix[]="{ status: \"value length is too long for ";
char szErrorSuffix[]="{ status: \"value length is too long for ";
netconn_write(conn, szErrorPrefix, strlen(szErrorPrefix), NETCONN_NOCOPY);
ESP_LOGE(TAG,"length is too long : %s = %s", last_parm_name, last_parm);
last_parm=NULL;
}
}
}
}
if(bErrorFound){
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
}
do {
if(snprintf(autoexec_name,sizeof(autoexec_name)-1,"X-Custom-autoexec%u: ",i)<0)
{
ESP_LOGE(TAG,"Unable to process autoexec%u. Name length overflow.",i);
break;
}
if(snprintf(autoexec_key,sizeof(autoexec_key)-1,"autoexec%u",i++)<0)
{
ESP_LOGE(TAG,"Unable to process autoexec%u. Name length overflow.",i);
break;
}
ESP_LOGD(TAG,"Looking for command name %s.", autoexec_name);
autoexec_value = http_server_get_header(save_ptr, autoexec_name, &lenS);
if(autoexec_value ){
if(lenS < MAX_COMMAND_LINE_SIZE ){
ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ call, with %s: %s, length %i", autoexec_key, autoexec_value, lenS);
wifi_manager_save_autoexec_config(autoexec_value,autoexec_key,lenS);
}
else
{
ESP_LOGE(TAG,"command line length is too long : %s = %s", autoexec_name, autoexec_value);
}
}
else {
ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
break;
}
} while(1);
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
//reboot esp if autoexec1 was modified
if (i > 1) {
ESP_LOGD(TAG,"autoexec1 changed, triggering reboot");
esp_restart();
}
}
else{
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);

View File

@@ -54,7 +54,7 @@ Contains the freeRTOS task and all necessary support
#include "lwip/err.h"
#include "lwip/netdb.h"
#include "lwip/ip4_addr.h"
#include "app_update/include/esp_ota_ops.h"
#ifndef SQUEEZELITE_ESP32_RELEASE_URL
#define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
@@ -98,6 +98,7 @@ struct wifi_settings_t wifi_settings = {
};
const char wifi_manager_nvs_namespace[] = "espwifimgr";
char current_namespace[16] = "squeezelite-esp32";
EventGroupHandle_t wifi_manager_event_group;
@@ -177,7 +178,7 @@ uint8_t wifi_manager_get_flag(){
esp_err_t esp_err;
ESP_LOGI(TAG, "About to get config from flash");
esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
esp_err = nvs_open(current_namespace, NVS_READWRITE, &handle);
if (esp_err != ESP_OK) return 0;
esp_err= nvs_get_u8(handle, "autoexec", &value);
@@ -194,7 +195,7 @@ char * wifi_manager_alloc_get_config(char * name, size_t * l){
nvs_handle handle;
ESP_LOGD(TAG, "About to get config value %s from flash",name);
if (nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle) == ESP_OK) {
if (nvs_open(current_namespace, NVS_READWRITE, &handle) == ESP_OK) {
if (nvs_get_str(handle, name, NULL, &len)==ESP_OK) {
value=(char *)malloc(len);
memset(value,0x0, len);
@@ -220,7 +221,7 @@ esp_err_t wifi_manager_save_autoexec_flag(uint8_t flag){
nvs_handle handle;
esp_err_t esp_err;
ESP_LOGI(TAG, "About to save config to flash");
esp_err=nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
esp_err=nvs_open(current_namespace, NVS_READWRITE, &handle);
if (esp_err != ESP_OK) {
ESP_LOGE(TAG,"Unable to open nvs namespace %s",wifi_manager_nvs_namespace);
return esp_err;
@@ -248,17 +249,15 @@ esp_err_t wifi_manager_save_autoexec_flag(uint8_t flag){
esp_err_t wifi_manager_save_autoexec_config(char * value, char * name, int len){
nvs_handle handle;
char val[len+1];
esp_err_t esp_err;
if (len) { *val = '\0'; strncat(val, value, len); }
ESP_LOGI(TAG, "About to save config to flash");
esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
esp_err_t esp_err;
ESP_LOGI(TAG, "About to save config to flash");
esp_err = nvs_open(current_namespace, NVS_READWRITE, &handle);
if (esp_err != ESP_OK) {
ESP_LOGE(TAG,"Unable to open nvs namespace %s",wifi_manager_nvs_namespace);
ESP_LOGE(TAG,"Unable to open nvs namespace %s",current_namespace);
return esp_err;
}
esp_err = nvs_set_str(handle, name, val);
esp_err = nvs_set_str(handle, name, value);
if (esp_err != ESP_OK){
ESP_LOGE(TAG,"Unable to save value %s=%s",name,val);
nvs_close(handle);
@@ -406,13 +405,14 @@ 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(config){
#if !RECOVERY_APPLICATION
const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d}\n";
#else
const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d, \"ota_dsc\":\"%s\", \"ota_pct\":%d}\n";
const char ip_info_json_format[] = ",\"ip\":\"%s\",\"netmask\":\"%s\",\"gw\":\"%s\",\"urc\":%d,\"project_name\":\"%s\",\"version\":\"%s\"";
#if RECOVERY_APPLICATION
"\"ota_dsc\":\"%s\", \"ota_pct\":%d";
#endif
"}\n";
memset(ip_info_json, 0x00, JSON_IP_INFO_SIZE);
app_desc_t* app_desc=esp_ota_get_app_description(void);
/* to avoid declaring a new buffer we copy the data directly into the buffer at its correct address */
strcpy(ip_info_json, "{\"ssid\":");
@@ -433,7 +433,11 @@ void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code)
ip,
netmask,
gw,
(int)update_reason_code
(int)update_reason_code,
app_desc->project_name,
app_desc->version
#if RECOVERY_APPLICATION
,ota_get_status(),
ota_get_pct_complete()

View File

@@ -176,7 +176,8 @@ extern "C" {
// recovery has more resources available. Let's use them to include more details about the OTA process
#define JSON_IP_INFO_SIZE 150+255
#else
#define JSON_IP_INFO_SIZE 150
// 40 chars for appname and version
#define JSON_IP_INFO_SIZE 150+40
#endif