From 50f8656b7a4a4464a7734546e4307d46f01f83f5 Mon Sep 17 00:00:00 2001 From: Bogdan Pilyugin Date: Thu, 12 Jan 2023 15:40:39 +0200 Subject: [PATCH] fixed webguiapp default configuration; changed network ready conditions checks; --- Kconfig | 49 ++++ include/WebGUIAppMain.h | 494 ++++++++++++++++++---------------------- src/WebGUIAppMain.c | 115 +++++----- src/WiFiTransport.c | 4 +- 4 files changed, 331 insertions(+), 331 deletions(-) diff --git a/Kconfig b/Kconfig index c20e180..df04b0a 100644 --- a/Kconfig +++ b/Kconfig @@ -40,6 +40,55 @@ menu "WebGUIApp" default n help This will reset to default settings on every startup + + menu "OTA settings" + config WEBGUIAPP_OTA_AUTOUPDATE_ENABLE + bool "Enabled OTA autoupdate firmware" + default y + + + config WEBGUIAPP_OTA_HOST + string "URL of firmware for OTA update" + default "https://iotronic.cloud/firmware/firmware.bin" + help + URL of firmware file for OTA update + + config WEBGUIAPP_OTA_AUTOUPDATE_PERIOD + int "OTA autoupdate period in seconds" + range 1 100000 + default 3600 + help + Autoupdate period for OTA + + endmenu + + menu "SNTP client settings" + config WEBGUIAPP_SNTP_AUTOUPDATE_ENABLE + bool "Enabled SNTP time update" + default y + + config WEBGUIAPP_SNTP_HOST + string "URL of SNTP server" + default "2.pool.ntp.org" + help + URL of firmware file for OTA update + + config WEBGUIAPP_SNTP_AUTOUPDATE_PERIOD + int "SNTP autoupdate period in seconds" + range 1 100000 + default 3600 + help + Autoupdate period for SNTP client + + config WEBGUIAPP_SNTP_TIMEZONE + int "Timezone" + range -12 12 + default 2 + help + Timezone in hours. Positive for East and negative for the West + endmenu + + menu "SPI settings" config WEBGUIAPP_SPI_ENABLE diff --git a/include/WebGUIAppMain.h b/include/WebGUIAppMain.h index 76354c0..5445041 100644 --- a/include/WebGUIAppMain.h +++ b/include/WebGUIAppMain.h @@ -1,271 +1,223 @@ - /*! Copyright 2022 Bogdan Pilyugin - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * \file SystemConfiguration.h - * \version 1.0 - * \date 2022-08-13 - * \author Bogdan Pilyugin - * \brief - * \details - * \copyright Apache License, Version 2.0 - */ - -#ifndef COMPONENTS_WEB_GUI_APPLICATION_INCLUDE_SYSTEMCONFIGURATION_H_ -#define COMPONENTS_WEB_GUI_APPLICATION_INCLUDE_SYSTEMCONFIGURATION_H_ - -#include -#include -#include "esp_netif.h" -#include "sdkconfig.h" - -#define DEFAULT_HOST_NAME "DEVICE_HOSTNAME" /// +#include +#include "esp_netif.h" +#include "sdkconfig.h" + +// Application-dependent structure used to contain address information + + /** + * @struct APP_CONFIG + * @brief The main configuration structure + * @details This structure saving to EEPROM and loading from EEPROM\n + * on device boot. On load the checksumm is calculate and compare to \n + * saved one. If find difference (due to eeprom memory distortions),\n + * the default values will be loaded. + */ + typedef struct + { + char NetBIOSName[32]; ///< NetBIOS name + char SysName[32]; ///< User Name + char SysPass[32]; ///< User Password + char OTAURL[64]; ///< OTA URL + + char SN[11]; ///< String of serial number (decimal ID) + char ID[9]; ///< String of ID ( last 4 bytes of MAC) + + struct + { + char bIsOTAEnabled :1; + char bIsLedsEnabled :1; ///< Indication LEDs enable + char bIsLoRaConfirm :1; ///< Enable send back confirmation in LoRa channel + char bIsTCPConfirm :1; ///< Enable send back confirmation in TCP channel + char bit4 :1; + char bit5 :1; + char bit6 :1; + char bit7 :1; + } Flags1; // Flag structure + + struct + { + int TimeZone; + char SntpServerAdr[33]; + struct + { + char b1 :1; + char b2 :1; + char b3 :1; + char b4 :1; + char b5 :1; + char b6 :1; + char b7 :1; + char bIsGlobalEnabled :1; + } Flags1; + } sntpClient; + +#if CONFIG_WEBGUIAPP_MQTT_ENABLE + struct + { + char ServerAddr[32]; + uint16_t ServerPort; + char SystemName[32]; + char GroupName[32]; + char ClientID[32]; + char UserName[32]; + char UserPass[32]; + struct + { + char b0 :1; + char b1 :1; + char b2 :1; + char b3 :1; + char b4 :1; + char b5 :1; + char b6 :1; + char bIsGlobalEnabled :1; + } Flags1; + } mqttStation[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM]; +#endif + +#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE + struct + { + esp_ip4_addr_t IPAddr; // IP address + esp_ip4_addr_t Mask; // network mask + esp_ip4_addr_t Gateway; // gateway + esp_ip4_addr_t DNSAddr1; // + esp_ip4_addr_t DNSAddr2; // + esp_ip4_addr_t DNSAddr3; // + uint8_t MACAddr[6]; // MAC address + + struct + { + char bIsDHCPEnabled :1; + char b1 :1; + char b2 :1; + char b3 :1; + char b4 :1; + char b5 :1; + char b6 :1; + char bIsETHEnabled :1; + } Flags1; // Flag structure + + } ethSettings; +#endif + +#if CONFIG_WEBGUIAPP_WIFI_ENABLE + struct + { + esp_ip4_addr_t InfIPAddr; // IP address in infrastructure(INF) mode + esp_ip4_addr_t InfMask; // network mask in INF mode + esp_ip4_addr_t InfGateway; // gateway IP in INF mode + esp_ip4_addr_t ApIPAddr; // IP address in Access point(AP) mode + esp_ip4_addr_t DNSAddr1; // DNS in station mode + esp_ip4_addr_t DNSAddr2; // DNS in station mode + esp_ip4_addr_t DNSAddr3; // DNS in station mode + + char InfSSID[32]; // Wireless SSID in INF mode + char InfSecurityKey[32]; // Network key in INF mode + char ApSSID[32]; // Wireless SSID in AP mode + char ApSecurityKey[32]; // Wireless key in AP mode + + char MACAddrInf[6]; // MAC address + char MACAddrAp[6]; // MAC address + + struct + { + char bIsDHCPEnabled :1; + char bIsAP :1; + char b2 :1; + char b3 :1; + char b4 :1; + char b5 :1; + char b6 :1; + char bIsWiFiEnabled :1; + } Flags1; // Flag structure + + } wifiSettings; + +#endif +#if CONFIG_WEBGUIAPP_GPRS_ENABLE + struct + { + esp_ip4_addr_t IPAddr; // IP address + esp_ip4_addr_t Mask; // network mask + esp_ip4_addr_t Gateway; // gateway + esp_ip4_addr_t DNSAddr1; // + esp_ip4_addr_t DNSAddr2; // + esp_ip4_addr_t DNSAddr3; // + uint8_t MACAddr[6]; // MAC address + struct + { + char b0 :1; + char b1 :1; + char b2 :1; + char b3 :1; + char b4 :1; + char b5 :1; + char b6 :1; + char bIsGSMEnabled :1; + } Flags1; // Flag structure + + } gsmSettings; +#endif + + struct + { + char DevEui[8]; + char AppEui[8]; + char AppKey[16]; + + struct + { + char b0 :1; + char b1 :1; + char b2 :1; + char b3 :1; + char b4 :1; + char b5 :1; + char b6 :1; + char bIsLoRaWANEnabled :1; + } Flags1; // Flag structure + + } lorawanSettings; + + + } SYS_CONFIG; + + esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf); + esp_err_t WriteNVSSysConfig(SYS_CONFIG *SysConf); + esp_err_t InitSysConfig(void); + esp_err_t ResetInitSysConfig(void); + SYS_CONFIG* GetSysConf(void); + + esp_err_t WebGuiAppInit(void); + void DelayedRestart(void); + + +#endif /* COMPONENTS_WEB_GUI_APPLICATION_INCLUDE_SYSTEMCONFIGURATION_H_ */ diff --git a/src/WebGUIAppMain.c b/src/WebGUIAppMain.c index 5667192..de18387 100644 --- a/src/WebGUIAppMain.c +++ b/src/WebGUIAppMain.c @@ -156,48 +156,37 @@ esp_err_t WebGuiAppInit(void) } #endif - bool WiFiApOnly = false; -#if !CONFIG_WEBGUIAPP_GPRS_ENABLE && !CONFIG_WEBGUIAPP_ETHERNET_ENABLE && CONFIG_WEBGUIAPP_WIFI_ENABLE - if (GetSysConf()->wifiSettings.Flags1.bIsAP) - WiFiApOnly = true; -#endif - /*Start services depends on client connection*/ #if CONFIG_WEBGUIAPP_GPRS_ENABLE || CONFIG_WEBGUIAPP_ETHERNET_ENABLE || CONFIG_WEBGUIAPP_WIFI_ENABLE - { - ESP_ERROR_CHECK(start_file_server()); - if (!WiFiApOnly) - { - //start all services - /*Wait for interfaces connected*/ - while (!( - #ifdef CONFIG_WEBGUIAPP_GPRS_ENABLE + //start all services no depends on network ready + ESP_ERROR_CHECK(start_file_server()); + //Wait for network ready + while (!( + +#ifdef CONFIG_WEBGUIAPP_GPRS_ENABLE isPPPConnected() || #endif #ifdef CONFIG_WEBGUIAPP_WIFI_ENABLE - isWIFIConnected() || - #endif + isWIFIConnected() || + #endif #ifdef CONFIG_WEBGUIAPP_ETHERNET_ENABLE isETHConnected() || #endif - ++NetworkStartTimeout >= NETWORK_START_TIMEOUT)) - vTaskDelay(pdMS_TO_TICKS(1000)); + ++NetworkStartTimeout >= NETWORK_START_TIMEOUT)) + vTaskDelay(pdMS_TO_TICKS(1000)); - //Start all services needed internet connection - StartTimeGet(); + //Network ready or network not available now, but maybe restore later + StartTimeGet(); #if CONFIG_WEBGUIAPP_MQTT_ENABLE - if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled - || GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled) - { - MQTTRun(); - } -#endif - } - -#endif - + if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled + || GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled) + { + MQTTRun(); } +#endif +#endif + return ESP_OK; } @@ -280,9 +269,9 @@ static void ResetSysConfig(SYS_CONFIG *Conf) sizeof(CONFIG_WEBGUIAPP_USERNAME)); memcpy(Conf->SysPass, CONFIG_WEBGUIAPP_USERPASS, sizeof(CONFIG_WEBGUIAPP_USERPASS)); -//memcpy(Conf->OTAURL, CONFIG_WEBGUIAPP_, sizeof(SYSTEM_DEFAULT_OTAURL)); - memcpy(Conf->OTAURL, SYSTEM_DEFAULT_OTAURL, sizeof(SYSTEM_DEFAULT_OTAURL)); + memcpy(Conf->OTAURL, CONFIG_WEBGUIAPP_OTA_HOST, sizeof(CONFIG_WEBGUIAPP_OTA_HOST)); + #if CONFIG_WEBGUIAPP_WIFI_ENABLE Conf->wifiSettings.Flags1.bIsWiFiEnabled = CONFIG_WEBGUIAPP_WIFI_ON; memcpy(Conf->wifiSettings.ApSSID, CONFIG_WEBGUIAPP_WIFI_SSID_AP, @@ -363,12 +352,10 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C memcpy(Conf->mqttStation[1].UserPass, CONFIG_WEBGUIAPP_MQTT_PASSWORD, sizeof(CONFIG_WEBGUIAPP_MQTT_PASSWORD)); #endif #endif - memcpy(Conf->sntpClient.SntpServerAdr, DEFAULT_SNTP_SERVERNAME, - sizeof(DEFAULT_SNTP_SERVERNAME)); - Conf->sntpClient.Flags1.bIsEthEnabled = DEFAULT_SNTP_ETH_IS_ENABLED; - Conf->sntpClient.Flags1.bIsWifiEnabled = DEFAULT_SNTP_WIFI_IS_ENABLED; - Conf->sntpClient.Flags1.bIsGlobalEnabled = DEFAULT_SNTP_GLOBAL_ENABLED; - Conf->sntpClient.TimeZone = DEFAULT_SNTP_TIMEZONE; + memcpy(Conf->sntpClient.SntpServerAdr, CONFIG_WEBGUIAPP_SNTP_HOST, + sizeof(CONFIG_WEBGUIAPP_SNTP_HOST)); + Conf->sntpClient.Flags1.bIsGlobalEnabled = CONFIG_WEBGUIAPP_SNTP_AUTOUPDATE_ENABLE; + Conf->sntpClient.TimeZone = CONFIG_WEBGUIAPP_SNTP_TIMEZONE; #ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE Conf->lorawanSettings.Flags1.bIsLoRaWANEnabled = true; @@ -397,7 +384,7 @@ esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf) ESP_LOGI(TAG, "Size of structure to read is %d", L); err = nvs_get_blob(my_handle, "sys_conf", SysConf, &L); if (err != ESP_OK) - return err; + goto nvs_operation_err; unsigned char sha256_saved[32]; unsigned char sha256_calculated[32]; @@ -406,17 +393,26 @@ esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf) L = 32; err = nvs_get_blob(my_handle, "sys_conf_sha256", sha256_saved, &L); if (err != ESP_OK) - return err; - SHA256Hash((unsigned char*) SysConf, sizeof(SYS_CONFIG), sha256_calculated); + goto nvs_operation_err; + SHA256Hash((unsigned char*) SysConf, sizeof(SYS_CONFIG), sha256_calculated); BytesToStr(sha256_saved, sha_print, 32); ESP_LOGI(TAG, "Saved hash of structure is %s", sha_print); - BytesToStr(sha256_calculated, sha_print, 32); ESP_LOGI(TAG, "Calculated hash of structure is %s", sha_print); + if (memcmp(sha256_calculated, sha256_saved, L)) + { + err = ESP_ERR_INVALID_CRC; + goto nvs_operation_err; + } + nvs_close(my_handle); return ESP_OK; + +nvs_operation_err: + nvs_close(my_handle); + return err; } esp_err_t WriteNVSSysConfig(SYS_CONFIG *SysConf) @@ -432,7 +428,7 @@ esp_err_t WriteNVSSysConfig(SYS_CONFIG *SysConf) ESP_LOGI(TAG, "Size of structure to write is %d", L); err = nvs_set_blob(my_handle, "sys_conf", SysConf, L); if (err != ESP_OK) - return err; + goto nvs_wr_oper_err; unsigned char sha256[32]; unsigned char sha_print[32 * 2 + 1]; @@ -444,16 +440,20 @@ esp_err_t WriteNVSSysConfig(SYS_CONFIG *SysConf) L = 32; err = nvs_set_blob(my_handle, "sys_conf_sha256", sha256, L); if (err != ESP_OK) - return err; + goto nvs_wr_oper_err; // Commit err = nvs_commit(my_handle); if (err != ESP_OK) - return err; -// Close - nvs_close(my_handle); + goto nvs_wr_oper_err; + nvs_close(my_handle); return ESP_OK; + +nvs_wr_oper_err: + nvs_close(my_handle); + return err; + } SYS_CONFIG* GetSysConf(void) @@ -465,20 +465,19 @@ esp_err_t InitSysConfig(void) { esp_err_t err; err = ReadNVSSysConfig(&SysConfig); - if (err != ESP_ERR_NVS_NOT_FOUND) + if (err == ESP_ERR_INVALID_CRC || err == ESP_ERR_NVS_NOT_FOUND) { - if (err == ESP_OK) - { - ESP_LOGI(TAG, "Read system configuration OK"); - } - return err; - } - else - { - ESP_LOGI(TAG, "Reset and write default system configuration"); + ESP_LOGW(TAG, "Reset and write default system configuration"); ResetSysConfig(&SysConfig); err = WriteNVSSysConfig(&SysConfig); + return err; } + else if (err == ESP_OK) + { + ESP_LOGI(TAG, "Read system configuration OK"); + } + else + ESP_LOGW(TAG, "Error reading NVS configuration:%s", esp_err_to_name(err)); return err; } @@ -497,7 +496,7 @@ void DelayedRestartTask(void *pvParameter) void DelayedRestart(void) { xTaskCreate(DelayedRestartTask, "RestartTask", 1024 * 4, (void*) 0, 3, - NULL); + NULL); } bool GetUserAppNeedReset(void) diff --git a/src/WiFiTransport.c b/src/WiFiTransport.c index dfb2a44..7b7fa1a 100644 --- a/src/WiFiTransport.c +++ b/src/WiFiTransport.c @@ -167,7 +167,7 @@ static void wifi_init_softap(void *pvParameter) .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; - if (strlen(DEFAULT_WIFI_SSID_AP_KEY) == 0) + if (strlen(CONFIG_WEBGUIAPP_WIFI_KEY_AP) == 0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } @@ -367,7 +367,7 @@ static void wifi_init_apsta(void *pvParameter) .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; - if (strlen(DEFAULT_WIFI_SSID_AP_KEY) == 0) + if (strlen(CONFIG_WEBGUIAPP_WIFI_KEY_AP) == 0) { ap_wifi_config.ap.authmode = WIFI_AUTH_OPEN; }