/****************************************************************************** * 2016 ideasX (Tyler Berezowsky) * * FileName: config.c * * Description: This file is going to need alot of help by a C wizard, but it currently works...I think. * * 2016/8/8, v1.0 created this file ******************************************************************************* * * Copyright (c) 2014-2015, Tuan PM * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "util/config.h" #include "log/esp_log.h" static const char* TAG = "config.c"; /* CFG_LOCATION: 0x2 * (CFG_LOCATION + 0)*0x1000 = 0x2000 * (CFG_LOCATION + 1)*0x1000 = 0x3000 * (CFG_LOCATION + 2)*0x1000 = 0x4000 * (CFG_LOCATOIN + 3)*0x1000 = 0x5000 * */ System_Config_t system_config; Save_Flag_t save_flag; void ICACHE_FLASH_ATTR cfg_save() { ESP_LOGI(TAG, "Writing New Default Module Configuration"); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // read config sector 3 for (uint32 *)&save_flag, sizeof(Save_Flag_t)); // save_flag if (save_flag.flag == 0) { spi_flash_erase_sector(CFG_LOCATION + 1); // erase config sector 1 spi_flash_write((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, // write system_config in config sector 1 (uint32 *)&system_config, sizeof(System_Config_t); save_flag.flag = 1; // set save_flag.flag spi_flash_erase_sector(CFG_LOCATION + 3); // erase config sector 3 spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // write save_flag to config sector 3 (uint32 *)&save_flag, sizeof(Save_Flag_t)); } else { spi_flash_erase_sector(CFG_LOCATION + 0); // erase config sector 0 for system_config spi_flash_write((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, // write system_config to config sector 0 (uint32 *)&system_config, sizeof(System_Config_t)); save_flag.flag = 0; // clear save_flag.flag spi_flash_erase_sector(CFG_LOCATION + 3); // erase config sector 3 spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // write save_flag to config sector 3 (uint32 *)&save_flag, sizeof(Save_Flag_t)); } } void ICACHE_FLASH_ATTR cfg_load() { ESP_LOGI(TAG, "Loading Default Module Configuration..."); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // read save Flag to config sector 3 (uint32 *)&save_flag, sizeof(Save_Flag_t)); if (save_flag.flag == 0) { // if save_flag.flag == 0 spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, // write system_config to config sector 0 (uint32 *)&system_config, sizeof(System_Config_t)); } else { // if save_flag.flag == 1 spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, // write system_config to config sector 1 (uint32 *)&system_config, sizeof(System_Config_t)); } if(system_config.cfg_holder != CFG_HOLDER){ // if system_config doesn't contain the proper check value os_memset(&system_config, 0x00, sizeof(System_Config_t)); // clear system_config system_config.cfg_holder = CFG_HOLDER; os_sprintf(system_config.sta_ssid[0], "%s", DEFAULT_STA_SSID); // load STA_SSID into system_config os_sprintf(system_config.sta_pwd[0], "%s", DEDFAULT_STA_PASS); // load STA_PASS into system_config system_config.sta_type[0] = DEFAULT_STA_TYPE; // load STA_TYPE into system_config system_config.registered_stations = 1; system_config.current_station = 0; wifi_get_macaddr(STATION_IF, system_config.device_id); os_sprintf(system_config.mqtt_host, "%s", DEFAULT_MQTT_HOST); // load MQTT_HOST system_config.mqtt_port = DEFAULT_MQTT_PORT; // load MQTT_PORT os_sprintf(system_config.mqtt_user, "%s", DEFAULT_MQTT_USER); // load MQTT_USER os_sprintf(system_config.mqtt_pass, "%s", DEFAULT_MQTT_PASS); // load MQTT_PASS system_config.security = DEFAULT_SECURITY; /* default non ssl */ system_config.mqtt_keepalive = DEFAULT_MQTT_KEEPALIVE; // load MQTT_KEEPALIVE os_sprintf(system_config.ota_host, "%s", DEFAULT_OTA_HOST); system_config.ota_port = DEFAULT_OTA_PORT; cfg_save(); } }