From 6dfcaa85b6778f81cf5f2ce1fe9fa697b421fcf4 Mon Sep 17 00:00:00 2001 From: btsimonh Date: Sun, 13 Feb 2022 22:16:21 +0000 Subject: [PATCH] convert to new flash_config functions. add webapp config. --- src/flash_config/flash_config.c | 184 ++++++++++++++++++++++++++------ src/flash_config/flash_config.h | 72 +++++++++++-- src/httpserver/new_http.c | 30 ++++++ src/httpserver/rest_interface.c | 44 ++++---- src/new_cfg.c | 43 +++++--- src/new_cfg.h | 3 + src/new_pins.c | 60 +++++++---- src/new_pins.h | 1 + src/user_main.c | 15 +++ 9 files changed, 348 insertions(+), 104 deletions(-) diff --git a/src/flash_config/flash_config.c b/src/flash_config/flash_config.c index 5fee3749a..0e22d7b7d 100644 --- a/src/flash_config/flash_config.c +++ b/src/flash_config/flash_config.c @@ -21,12 +21,19 @@ static TLV_HEADER_ST *g_table = NULL; static UINT32 flashaddr; static UINT32 flashlen; -static int compress_table(); -static int save_table(); +static int changes = 0; +extern int g_savecfg; + +static int config_compress_table(); +static int config_save_table(); + +static INFO_ITEM_ST *_search_item(INFO_ITEM_ST *item, UINT32 *p_usedlen); + +static SemaphoreHandle_t config_mutex = 0; char *hex = "0123456789ABCDEF"; -int dump(unsigned char *addr, int len){ +int config_dump(unsigned char *addr, int len){ char tmp[40]; int i; ADDLOG_DEBUG(LOG_FEATURE_CFG, "dump of 0x%08X", addr); @@ -45,7 +52,31 @@ int dump(unsigned char *addr, int len){ -int get_tbl(int readit){ +int config_get_item(void *container) { + INFO_ITEM_ST *res; + INFO_ITEM_ST *p_item = (INFO_ITEM_ST *)container; + int ret = 0; + BaseType_t taken; + if (!config_mutex) { + config_mutex = xSemaphoreCreateMutex( ); + } + taken = xSemaphoreTake( config_mutex, 100 ); + + res = _search_item(p_item, NULL); + if (res){ + os_memcpy(p_item, res, sizeof(INFO_ITEM_ST) + p_item->len); + ret = 1; + } + + if (taken == pdTRUE){ + xSemaphoreGive( config_mutex ); + } + return ret; +} + + + +int config_get_tbl(int readit){ UINT32 ret = 0, status; DD_HANDLE flash_handle; TLV_HEADER_ST head; @@ -65,7 +96,7 @@ int get_tbl(int readit){ if(INFO_TLV_HEADER != g_table->type){ ADDLOG_DEBUG(LOG_FEATURE_CFG, "get_tbl g_table corrupted"); - dump((unsigned char *)g_table, 32); + config_dump((unsigned char *)g_table, 32); cfg_len = 0; return cfg_len; } @@ -101,21 +132,42 @@ int get_tbl(int readit){ ddev_close(flash_handle); hal_flash_unlock(); - dump((unsigned char *)g_table, 32); + config_dump((unsigned char *)g_table, cfg_len); + config_dump_table(); return ret; } -int release_tbl(){ - void *table = g_table; +int config_release_tbl(){ + void *table; + BaseType_t taken; + if (!config_mutex) { + config_mutex = xSemaphoreCreateMutex( ); + } + taken = xSemaphoreTake( config_mutex, 100 ); + table = g_table; + + if(INFO_TLV_HEADER != g_table->type){ ADDLOG_DEBUG(LOG_FEATURE_CFG, "release_tbl g_table corrupted"); } - if (!table) + if (!table) { + if (taken == pdTRUE){ + xSemaphoreGive( config_mutex ); + } return 0; + } + + if (changes){ + config_save_table(); + } + g_table = NULL; os_free(table); + if (taken == pdTRUE){ + xSemaphoreGive( config_mutex ); + } ADDLOG_DEBUG(LOG_FEATURE_CFG, "release_tbl"); return 0; } @@ -128,7 +180,7 @@ static INFO_ITEM_ST *_search_item(INFO_ITEM_ST *item, UINT32 *p_usedlen) INFO_ITEM_ST *target = NULL; UINT32 usedlen = 0; UINT32 type = 0; - UINT32 tablelen = get_tbl(1); + UINT32 tablelen = config_get_tbl(1); if (!tablelen){ if (p_usedlen) *p_usedlen = usedlen; return NULL; @@ -175,23 +227,33 @@ static int tbl_used_data_len(){ return (int)len; } -INFO_ITEM_ST *search_item(INFO_ITEM_ST *item){ +INFO_ITEM_ST *config_search_item(INFO_ITEM_ST *item){ return _search_item(item, NULL); } -INFO_ITEM_ST *search_item_type(UINT32 type){ +INFO_ITEM_ST *config_search_item_type(UINT32 type){ INFO_ITEM_ST item; item.type = type; return _search_item(&item, NULL); } -int delete_item(UINT32 type) +int config_delete_item(UINT32 type) { UINT32 addr, end_addr; INFO_ITEM_ST *head; UINT32 deleted = 0; - UINT32 tablelen = get_tbl(1); + UINT32 tablelen; + + BaseType_t taken; + if (!config_mutex) { + config_mutex = xSemaphoreCreateMutex( ); + } + taken = xSemaphoreTake( config_mutex, 100 ); + + tablelen = config_get_tbl(1); + if (!tablelen){ + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); return 0; } @@ -211,23 +273,33 @@ int delete_item(UINT32 type) if (deleted){ ADDLOG_DEBUG(LOG_FEATURE_CFG, "Deleted %d items type 0x%08X", deleted, type); - compress_table(); - save_table(); + config_compress_table(); } + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); return deleted; } ////////////////////////////////////////// // remove redundant entries -static int compress_table() +static int config_compress_table() { UINT32 addr1, addr2, end_addr; INFO_ITEM_ST *head; UINT32 usedlen = 0; - UINT32 tablelen = get_tbl(1); + UINT32 tablelen; + + BaseType_t taken; + if (!config_mutex) { + config_mutex = xSemaphoreCreateMutex( ); + } + taken = xSemaphoreTake( config_mutex, 100 ); + + tablelen = config_get_tbl(1); + if (!tablelen){ + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); return 0; } @@ -258,9 +330,12 @@ static int compress_table() if (addr1 != addr2) { ADDLOG_DEBUG(LOG_FEATURE_CFG, "Compress table from %d to %d bytes", g_table->len, usedlen); + changes++; } g_table->len = addr2 - sizeof(TLV_HEADER_ST); + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); + if (addr1 != addr2) { return addr2; // maybe should save } @@ -269,22 +344,30 @@ static int compress_table() } -static int save_table(){ +static int config_save_table(){ UINT32 tablelen = 0; bk_logic_partition_t *pt = bk_flash_get_info(BK_PARTITION_NET_PARAM); flashaddr = pt->partition_start_addr; flashlen = pt->partition_length; + BaseType_t taken; + if (!config_mutex) { + config_mutex = xSemaphoreCreateMutex( ); + } + taken = xSemaphoreTake( config_mutex, 100 ); + if (!g_table) { ADDLOG_ERROR(LOG_FEATURE_CFG, "save_table - no table to save"); + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); return 0; } // should already have it... - tablelen = get_tbl(1); + tablelen = config_get_tbl(1); if (tablelen > flashlen){ ADDLOG_ERROR(LOG_FEATURE_CFG, "save_table - table too big - can't save"); + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); return 0; } @@ -295,28 +378,39 @@ static int save_table(){ bk_flash_write(BK_PARTITION_NET_PARAM,0,(uint8_t *)g_table,tablelen); bk_flash_enable_security(FLASH_PROTECT_ALL); hal_flash_unlock(); + changes = 0; + if (taken == pdTRUE) xSemaphoreGive( config_mutex ); ADDLOG_DEBUG(LOG_FEATURE_CFG, "would save_table %d bytes", tablelen); return 1; } -int save_item(INFO_ITEM_ST *item) +int config_save_item(INFO_ITEM_ST *item) { UINT32 item_len; INFO_ITEM_ST_PTR item_head_ptr; + BaseType_t taken; + if (!config_mutex) { + config_mutex = xSemaphoreCreateMutex( ); + } + taken = xSemaphoreTake( config_mutex, 100 ); + + item_len = sizeof(INFO_ITEM_ST) + item->len; - UINT32 tablelen = get_tbl(1); + UINT32 tablelen = config_get_tbl(1); if(g_table && (INFO_TLV_HEADER != g_table->type)){ ADDLOG_DEBUG(LOG_FEATURE_CFG, "save_item g_table corrupted"); + if (taken == pdTRUE){ + xSemaphoreGive( config_mutex ); + } return 0; } ADDLOG_DEBUG(LOG_FEATURE_CFG, "save_item type %08X len %d, tablelen %d", item->type, item->len, tablelen); - rtos_delay_milliseconds(1000); if (!tablelen){ // no table, creat it. @@ -329,9 +423,10 @@ int save_item(INFO_ITEM_ST *item) os_memcpy(item_head_ptr, item, item_len); tablelen = cfg_len; ADDLOG_DEBUG(LOG_FEATURE_CFG, "save_item, new table created"); + changes++; } else { // have table - do we have existing item? - item_head_ptr = search_item(item); + item_head_ptr = config_search_item(item); ADDLOG_DEBUG(LOG_FEATURE_API, "save search found %x len %d, our len %d", item_head_ptr, (item_head_ptr?item_head_ptr->len:0), item->len); if (item_head_ptr){ // if length mismatch, then zap this entry, and add on end @@ -347,12 +442,14 @@ int save_item(INFO_ITEM_ST *item) } else { ADDLOG_DEBUG(LOG_FEATURE_CFG, "save_item new item"); } - rtos_delay_milliseconds(1000); // if we STILL have an item, lengths match. // just copy in data and write whole table if (item_head_ptr){ - os_memcpy(item_head_ptr, item, item_len); + if (os_memcmp(item_head_ptr, item, item_len)){ + os_memcpy(item_head_ptr, item, item_len); + changes++; + } } else { UINT32 newlen = 0; // add to end @@ -363,11 +460,13 @@ int save_item(INFO_ITEM_ST *item) if(!newtable){ ADDLOG_DEBUG(LOG_FEATURE_CFG, "allocation failure for %d bytes - save aborted", tablelen + item_len); + if (taken == pdTRUE){ + xSemaphoreGive( config_mutex ); + } return 0; } ADDLOG_DEBUG(LOG_FEATURE_CFG, "copy from %x to %x len %d", g_table, newtable, tablelen); - rtos_delay_milliseconds(1000); os_memcpy(newtable, g_table, tablelen); item_head_ptr = (INFO_ITEM_ST *) (((char *)newtable) + tablelen); os_memcpy(item_head_ptr, item, item_len); @@ -375,24 +474,35 @@ int save_item(INFO_ITEM_ST *item) newtable->len = tablelen - sizeof(TLV_HEADER_ST); g_table = newtable; os_free(oldtable); - newlen = compress_table(); + newlen = config_compress_table(); if (newlen){ tablelen = newlen; } + changes++; + } + } + + if (taken == pdTRUE){ + xSemaphoreGive( config_mutex ); + } + + if (changes){ + // save config in 3 seconds.... + if (!g_savecfg){ + g_savecfg = 3; } } - save_table(); return 1; } -int dump_table() +int config_dump_table() { UINT32 addr, end_addr; INFO_ITEM_ST *head; UINT32 usedlen = 0; - UINT32 tablelen = get_tbl(1); + UINT32 tablelen = config_get_tbl(1); if (!tablelen){ ADDLOG_ERROR(LOG_FEATURE_CFG, "dump_table - no table"); return 0; @@ -408,6 +518,7 @@ int dump_table() while(addr < end_addr) { ADDLOG_DEBUG(LOG_FEATURE_CFG, "item type 0x%08X len %d at 0x%04X", head->type, head->len, addr); + config_dump(head, head->len); addr += sizeof(INFO_ITEM_ST); addr += head->len; @@ -424,3 +535,14 @@ int dump_table() return 1; } + +int config_commit(){ + if (changes){ + config_save_table(); + return 1; + } + // free config memory; + config_release_tbl(); + return 0; +} + diff --git a/src/flash_config/flash_config.h b/src/flash_config/flash_config.h index bc13e73a7..b21f52d0a 100644 --- a/src/flash_config/flash_config.h +++ b/src/flash_config/flash_config.h @@ -1,25 +1,52 @@ #include "net_param_pub.h" +///////////////////////////////////////////////////// +// mutex protected functions: -int get_tbl(int readit); -int release_tbl(); -INFO_ITEM_ST *search_item(INFO_ITEM_ST *item); -INFO_ITEM_ST *search_item_type(UINT32 type); -int compress_table(); -int save_item(INFO_ITEM_ST *item); -int delete_item(UINT32 type); -int dump_table(); +// copy a config item of type to 'container' +// 'container' should have been initialised with CONFIG_INIT_ITEM +// then it will have the right type and len.. +int config_get_item(void *container); + +// save an item to config. +// item should start with INFO_ITEM_ST, and be initialised with CONFIG_INIT_ITEM +// will trigger config save 3s later +int config_save_item(void *item); + +// delete ALL items of type +int config_delete_item(UINT32 type); + +// save pending changes NOW and release the config memory +int config_commit(); +///////////////////////////////////////////////////// + + +// other functions, not protected. +// internal +int config_get_tbl(int readit); +// release memory (saves if changes) +int config_release_tbl(); +// return a ptr to the item, unprotected +INFO_ITEM_ST *config_search_item(INFO_ITEM_ST *item); +// return a ptr to the item, unprotected +INFO_ITEM_ST *config_search_item_type(UINT32 type); +// list table contetn by type & len to debug +int config_dump_table(); -// config structures - only the tags really need to be here.... - +///////////////////////////////////////// +// config types not defined by beken // // strucutre is ITEM_URL_CONFIG -#define CONFIG_TAG_WEBAPP_ROOT ((UINT32) *((UINT32*)"TEST")) +#define CONFIG_TYPE_WEBAPP_ROOT ((UINT32) *((UINT32*)"TEST")) // +#define CONFIG_INIT_ITEM(t, ptr) { (ptr)->head.len = sizeof(*(ptr)) - sizeof((ptr)->head); (ptr)->head.type = t; } + +///////////////////////////////////////// +// config structures not defined by beken #define CONFIG_URL_SIZE_MAX 64 typedef struct item_url_config @@ -29,3 +56,26 @@ typedef struct item_url_config }ITEM_URL_CONFIG,*ITEM_URL_CONFIG_PTR; +// added for OpenBK7231T +typedef struct item_new_wifi_config2 +{ + INFO_ITEM_ST head; + char scrap[8]; + char ssid[32]; + char pass[64]; +}ITEM_NEW_WIFI_CONFIG2,*ITEM_NEW_WIFI_CONFIG2_PTR; + +typedef struct item_new_mqtt_config2 +{ + INFO_ITEM_ST head; + char scrap[8]; + char brokerName[64]; + char userName[64]; + int port; + char hostName[64]; + // Home Assistant default password is 64 chars.. + char pass[128]; +}ITEM_NEW_MQTT_CONFIG2,*ITEM_NEW_MQTT_CONFIG2_PTR; + + + diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index b3e220499..a451be17e 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -611,6 +611,35 @@ int HTTP_ProcessPacket(http_request_t *request) { poststr(request,"
"); poststr(request,"Return to MQTT settings"); + poststr(request,"
"); + poststr(request,htmlReturnToCfg); + HTTP_AddBuildFooter(request); + poststr(request,htmlEnd); + } else if(http_checkUrlBase(urlStr,"cfg_webapp")) { + http_setup(request, httpMimeTypeHTML); + poststr(request,htmlHeader); + poststr(request,g_header); + poststr(request,"

Use this to set the URL of the Webapp

"); + poststr(request,"
\ +
\ +
\ + \ +
"); + poststr(request,htmlReturnToCfg); + HTTP_AddBuildFooter(request); + poststr(request,htmlEnd); + } else if(http_checkUrlBase(urlStr,"cfg_webapp_set")) { + http_setup(request, httpMimeTypeHTML); + poststr(request,htmlHeader); + poststr(request,g_header); + + if(http_getArg(urlStr,"url",tmpA,sizeof(tmpA))) { + CFG_SetWebappRoot(tmpA); + } + poststr(request,"Webapp url set!"); + poststr(request,"
"); poststr(request,htmlReturnToCfg); HTTP_AddBuildFooter(request); @@ -903,6 +932,7 @@ int HTTP_ProcessPacket(http_request_t *request) { poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); + poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); poststr(request,"
"); diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c index f5992e477..a0232fc3a 100644 --- a/src/httpserver/rest_interface.c +++ b/src/httpserver/rest_interface.c @@ -13,6 +13,7 @@ #endif #include "lwip/sockets.h" #include "../flash_config/flash_config.h" +#include "../new_cfg.h" extern int g_reset; @@ -73,9 +74,8 @@ const char * apppage4 = "startup.js\">" static int http_rest_app(http_request_t *request){ - //char *webhost = "http://raspberrypi:1880";//CFG_GetWebRoot(); - char *webhost = CFG_GetWebappRoot(); - char *ourip = getMyIp(); //CFG_GetOurIP(); + const char *webhost = CFG_GetWebappRoot(); + const char *ourip = getMyIp(); //CFG_GetOurIP(); http_setup(request, httpMimeTypeHTML); if (webhost && ourip){ poststr(request, apppage1); @@ -94,7 +94,6 @@ static int http_rest_app(http_request_t *request){ return 0; } - #ifdef BK_LITTLEFS int EndsWith(const char *str, const char *suffix) @@ -709,7 +708,7 @@ static int http_rest_get_flash(http_request_t *request, int startaddr, int len){ static int http_rest_get_dumpconfig(http_request_t *request){ - dump_table(); + config_dump_table(); http_setup(request, httpMimeTypeText); poststr(request, NULL); @@ -736,29 +735,28 @@ static int http_rest_get_testconfig(http_request_t *request){ testconfig.head.len = sizeof(testconfig) - sizeof(testconfig.head); strcpy(testconfig.somename, "test it here"); - dump_table(); + config_dump_table(); - ret = search_item((INFO_ITEM_ST *)&testconfig); + ret = config_search_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "search found %x", ret); - dump_table(); + config_dump_table(); - intres = delete_item(testconfig.head.type); + intres = config_delete_item(testconfig.head.type); ADDLOG_DEBUG(LOG_FEATURE_API, "delete_item returned %d", intres); - intres = save_item((INFO_ITEM_ST *)&testconfig); + intres = config_save_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "save_item returned %d", intres); - ret = search_item((INFO_ITEM_ST *)&testconfig); + ret = config_search_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "search2 found %x len %d", ret, (ret?ret->len:0)); - intres = save_item((INFO_ITEM_ST *)&testconfig); + intres = config_save_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "save_item returned %d", intres); - ret = search_item((INFO_ITEM_ST *)&testconfig); + ret = config_search_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "search3 found %x len %d", ret, (ret?ret->len:0)); - rtos_delay_milliseconds(1000); if (ret){ @@ -769,28 +767,24 @@ static int http_rest_get_testconfig(http_request_t *request){ } } - rtos_delay_milliseconds(1000); testconfig.head.len = sizeof(testconfig) - sizeof(testconfig.head) - 1; - intres = save_item((INFO_ITEM_ST *)&testconfig); + intres = config_save_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "save_item returned %d", intres); - rtos_delay_milliseconds(1000); - ret = search_item((INFO_ITEM_ST *)&testconfig); + ret = config_search_item((INFO_ITEM_ST *)&testconfig); ADDLOG_DEBUG(LOG_FEATURE_API, "search4 found %x len %d", ret, (ret?ret->len:0)); - dump_table(); - rtos_delay_milliseconds(1000); + config_dump_table(); - intres = delete_item(testconfig.head.type); + intres = config_delete_item(testconfig.head.type); ADDLOG_DEBUG(LOG_FEATURE_API, "delete_item returned %d", intres); - dump_table(); - rtos_delay_milliseconds(1000); + config_dump_table(); - release_tbl(); + config_release_tbl(); - dump_table(); + config_dump_table(); http_setup(request, httpMimeTypeText); poststr(request, NULL); diff --git a/src/new_cfg.c b/src/new_cfg.c index 91efd44a5..b6ed92acf 100644 --- a/src/new_cfg.c +++ b/src/new_cfg.c @@ -26,27 +26,32 @@ static char g_mqtt_pass[128] = "qqqqqqqqqq"; static char g_wifi_ssid[64] = { 0 }; static char g_wifi_pass[64] = { 0 }; -static char g_webappRoot[CONFIG_URL_SIZE_MAX] = { 0 }; +static char g_webappRoot[CONFIG_URL_SIZE_MAX] = "https://openbekeniot.github.io/webapp/"; // Long unique device name, like OpenBK7231T_AABBCCDD char g_deviceName[64] = "testDev"; // Short unique device name, like obkAABBCCDD char g_shortDeviceName[64] = "td01"; +const char *CFG_LoadWebappRoot(){ + ITEM_URL_CONFIG item; + int res; + CONFIG_INIT_ITEM(CONFIG_TYPE_WEBAPP_ROOT, &item); + res = config_get_item(&item); + if (res) strcpy_safe(g_webappRoot, item.url,sizeof(g_webappRoot)); + return g_webappRoot; +} + const char *CFG_GetWebappRoot(){ - ITEM_URL_CONFIG *pItem = (ITEM_URL_CONFIG*)search_item_type(CONFIG_TAG_WEBAPP_ROOT); - if (pItem){ - strncpy(g_webappRoot, pItem->url, sizeof(g_webappRoot)); - } return g_webappRoot; } void CFG_SetWebappRoot(const char *s) { ITEM_URL_CONFIG item; - item.head.type = CONFIG_TAG_WEBAPP_ROOT; - item.head.len = sizeof(item) - sizeof(item.head); + CONFIG_INIT_ITEM(CONFIG_TYPE_WEBAPP_ROOT, &item); strcpy_safe(item.url,s,sizeof(item.url)); - save_item((INFO_ITEM_ST *)&item); + strcpy_safe(g_webappRoot, item.url,sizeof(g_webappRoot)); + config_save_item(&item); } const char *CFG_GetDeviceName(){ @@ -125,10 +130,12 @@ void CFG_SaveWiFi() { #elif PLATFORM_XR809 #else - ITEM_NEW_WIFI_CONFIG container; + ITEM_NEW_WIFI_CONFIG2 container; + os_memset(&container, 0, sizeof(container)); + CONFIG_INIT_ITEM(NEW_WIFI_CONFIG, &container); strcpy_safe(container.ssid, g_wifi_ssid, sizeof(container.ssid)); strcpy_safe(container.pass, g_wifi_pass, sizeof(container.pass)); - save_info_item(NEW_WIFI_CONFIG,(UINT8 *)&container, 0, 0); + config_save_item(&container); #endif } void CFG_LoadWiFi() { @@ -137,8 +144,9 @@ void CFG_LoadWiFi() { #elif PLATFORM_XR809 #else - ITEM_NEW_WIFI_CONFIG container; - if(get_info_item(NEW_WIFI_CONFIG,(UINT8 *)&container, 0, 0) != 0) { + ITEM_NEW_WIFI_CONFIG2 container; + CONFIG_INIT_ITEM(NEW_WIFI_CONFIG, &container); + if (config_get_item(&container) != 0){ strcpy_safe(g_wifi_ssid,container.ssid,sizeof(g_wifi_ssid)); strcpy_safe(g_wifi_pass,container.pass,sizeof(g_wifi_pass)); } @@ -151,13 +159,15 @@ void CFG_SaveMQTT() { #elif PLATFORM_XR809 #else - ITEM_NEW_MQTT_CONFIG container; + ITEM_NEW_MQTT_CONFIG2 container; + os_memset(&container, 0, sizeof(container)); + CONFIG_INIT_ITEM(NEW_MQTT_CONFIG, &container); strcpy_safe(container.userName, g_mqtt_userName, sizeof(container.userName)); strcpy_safe(container.pass, g_mqtt_pass, sizeof(container.pass)); strcpy_safe(container.hostName, g_mqtt_host, sizeof(container.hostName)); strcpy_safe(container.brokerName, g_mqtt_brokerName, sizeof(container.brokerName)); container.port = g_mqtt_port; - save_info_item(NEW_MQTT_CONFIG,(UINT8 *)&container, 0, 0); + config_save_item(&container); #endif } @@ -167,8 +177,9 @@ void CFG_LoadMQTT() { #elif PLATFORM_XR809 #else - ITEM_NEW_MQTT_CONFIG container; - if(get_info_item(NEW_MQTT_CONFIG,(UINT8 *)&container, 0, 0) != 0) { + ITEM_NEW_MQTT_CONFIG2 container; + CONFIG_INIT_ITEM(NEW_MQTT_CONFIG, &container); + if (config_get_item(&container) != 0){ strcpy_safe(g_mqtt_userName,container.userName,sizeof(g_mqtt_userName)); strcpy_safe(g_mqtt_pass,container.pass,sizeof(g_mqtt_pass)); strcpy_safe(g_mqtt_host,container.hostName,sizeof(g_mqtt_host)); diff --git a/src/new_cfg.h b/src/new_cfg.h index 79d450177..9c9956b0b 100644 --- a/src/new_cfg.h +++ b/src/new_cfg.h @@ -22,3 +22,6 @@ void CFG_SaveWiFi(); void CFG_LoadWiFi(); void CFG_SaveMQTT(); void CFG_LoadMQTT(); +const char *CFG_GetWebappRoot(); +const char *CFG_LoadWebappRoot(); + diff --git a/src/new_pins.c b/src/new_pins.c index 3a4210371..13cf25096 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -5,11 +5,12 @@ #include "new_pins.h" #include "httpserver/new_http.h" #include "logging/logging.h" +#include "flash_config/flash_config.h" #if WINDOWS #elif PLATFORM_XR809 - + #include "driver/chip/hal_gpio.h" #else @@ -25,6 +26,14 @@ #endif +typedef struct item_pins_config +{ + INFO_ITEM_ST head; + pinsState_t pins; +}ITEM_PINS_CONFIG,*ITEM_PINS_CONFIG_PTR; + + + /* // from BkDriverPwm.h" OSStatus bk_pwm_start(bk_pwm_t pwm); @@ -81,16 +90,21 @@ void (*g_doubleClickCallback)(int pinIndex) = 0; void PIN_SaveToFlash() { + ITEM_PINS_CONFIG pins; #if WINDOWS #elif PLATFORM_XR809 #else - save_info_item(NEW_PINS_CONFIG,(UINT8 *)&g_pins, 0, 0); + os_memcpy(&pins.pins, &g_pins, sizeof(pins.pins)); + CONFIG_INIT_ITEM(NEW_PINS_CONFIG, &pins); + config_save_item(&pins); #endif } void PIN_LoadFromFlash() { int i; + int res; + ITEM_PINS_CONFIG pins; PR_NOTICE("PIN_LoadFromFlash called - going to load pins.\r\n"); @@ -101,7 +115,11 @@ void PIN_LoadFromFlash() { #elif PLATFORM_XR809 #else - get_info_item(NEW_PINS_CONFIG,(UINT8 *)&g_pins, 0, 0); + CONFIG_INIT_ITEM(NEW_PINS_CONFIG, &pins); + res = config_get_item(&g_pins); + if (res){ + os_memcpy(&g_pins, &pins.pins, sizeof(g_pins)); + } #endif for(i = 0; i < GPIO_MAX; i++) { PIN_SetPinRoleForPinIndex(i,g_pins.roles[i]); @@ -120,13 +138,13 @@ int PIN_GetPinChannelForPinIndex(int index) { void RAW_SetPinValue(int index, int iVal){ #if WINDOWS -#elif PLATFORM_XR809 - GPIO_InitParam param; - int xr_port; // eg GPIO_PORT_A - int xr_pin; // eg. GPIO_PIN_20 - - PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin); - +#elif PLATFORM_XR809 + GPIO_InitParam param; + int xr_port; // eg GPIO_PORT_A + int xr_pin; // eg. GPIO_PIN_20 + + PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin); + HAL_GPIO_WritePin(xr_port, xr_pin, iVal); #else bk_gpio_output(index, iVal); @@ -304,17 +322,17 @@ void PIN_SetPinRoleForPinIndex(int index, int role) { { #if WINDOWS -#elif PLATFORM_XR809 - GPIO_InitParam param; - int xr_port; // eg GPIO_PORT_A - int xr_pin; // eg. GPIO_PIN_20 - - PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin); - - /*set pin driver capability*/ - param.driving = GPIO_DRIVING_LEVEL_1; - param.mode = GPIOx_Pn_F1_OUTPUT; - param.pull = GPIO_PULL_NONE; +#elif PLATFORM_XR809 + GPIO_InitParam param; + int xr_port; // eg GPIO_PORT_A + int xr_pin; // eg. GPIO_PIN_20 + + PIN_XR809_GetPortPinForIndex(index, &xr_port, &xr_pin); + + /*set pin driver capability*/ + param.driving = GPIO_DRIVING_LEVEL_1; + param.mode = GPIOx_Pn_F1_OUTPUT; + param.pull = GPIO_PULL_NONE; HAL_GPIO_Init(xr_port, xr_pin, ¶m); #else bk_gpio_config_output(index); diff --git a/src/new_pins.h b/src/new_pins.h index 45129ed25..e58b151ab 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -26,6 +26,7 @@ typedef struct pinsState_s { byte channels[32]; } pinsState_t; + extern pinsState_t g_pins; diff --git a/src/user_main.c b/src/user_main.c index 68886b863..c2218a96a 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -49,6 +49,7 @@ #include "lwip/netdb.h" #include "littlefs/our_lfs.h" +#include "flash_config/flash_config.h" #undef Malloc #undef Free @@ -62,6 +63,11 @@ static int g_openAP = 0; // reset in this number of seconds int g_reset = 0; + +// save config in this number of seconds +int g_savecfg = 0; + + // from wlan_ui.c void bk_reboot(void); @@ -187,6 +193,14 @@ static void app_led_timer_handler(void *data) bk_reboot(); } } + + if (g_savecfg){ + g_savecfg--; + if (!g_savecfg){ + config_commit(); + } + } + } void app_on_generic_dbl_click(int btnIndex) @@ -324,6 +338,7 @@ void user_main(void) CFG_CreateDeviceNameUnique(); + CFG_LoadWebappRoot(); CFG_LoadWiFi(); CFG_LoadMQTT(); PIN_LoadFromFlash();