config.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /******************************************************************************
  2. * 2016 ideasX (Tyler Berezowsky)
  3. *
  4. * FileName: config.c
  5. *
  6. * Description: This file is going to need alot of help by a C wizard, but it currently works...I think.
  7. *
  8. * 2016/8/8, v1.0 created this file
  9. *******************************************************************************
  10. *
  11. * Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com>
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * Neither the name of Redis nor the names of its contributors may be used
  23. * to endorse or promote products derived from this software without
  24. * specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include "util/config.h"
  39. #include "log/esp_log.h"
  40. static const char* TAG = "config.c";
  41. /* CFG_LOCATION: 0x2
  42. * (CFG_LOCATION + 0)*0x1000 = 0x2000
  43. * (CFG_LOCATION + 1)*0x1000 = 0x3000
  44. * (CFG_LOCATION + 2)*0x1000 = 0x4000
  45. * (CFG_LOCATOIN + 3)*0x1000 = 0x5000
  46. *
  47. */
  48. System_Config_t system_config;
  49. Save_Flag_t save_flag;
  50. void ICACHE_FLASH_ATTR cfg_save()
  51. {
  52. ESP_LOGI(TAG, "Writing New Default Module Configuration");
  53. spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // read config sector 3 for
  54. (uint32 *)&save_flag, sizeof(Save_Flag_t)); // save_flag
  55. if (save_flag.flag == 0) {
  56. spi_flash_erase_sector(CFG_LOCATION + 1); // erase config sector 1
  57. spi_flash_write((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, // write system_config in config sector 1
  58. (uint32 *)&system_config, sizeof(System_Config_t);
  59. save_flag.flag = 1; // set save_flag.flag
  60. spi_flash_erase_sector(CFG_LOCATION + 3); // erase config sector 3
  61. spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // write save_flag to config sector 3
  62. (uint32 *)&save_flag, sizeof(Save_Flag_t));
  63. }
  64. else {
  65. spi_flash_erase_sector(CFG_LOCATION + 0); // erase config sector 0 for system_config
  66. spi_flash_write((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, // write system_config to config sector 0
  67. (uint32 *)&system_config, sizeof(System_Config_t));
  68. save_flag.flag = 0; // clear save_flag.flag
  69. spi_flash_erase_sector(CFG_LOCATION + 3); // erase config sector 3
  70. spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // write save_flag to config sector 3
  71. (uint32 *)&save_flag, sizeof(Save_Flag_t));
  72. }
  73. }
  74. void ICACHE_FLASH_ATTR cfg_load()
  75. {
  76. ESP_LOGI(TAG, "Loading Default Module Configuration...");
  77. spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // read save Flag to config sector 3
  78. (uint32 *)&save_flag, sizeof(Save_Flag_t));
  79. if (save_flag.flag == 0) { // if save_flag.flag == 0
  80. spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, // write system_config to config sector 0
  81. (uint32 *)&system_config, sizeof(System_Config_t));
  82. } else { // if save_flag.flag == 1
  83. spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, // write system_config to config sector 1
  84. (uint32 *)&system_config, sizeof(System_Config_t));
  85. }
  86. if(system_config.cfg_holder != CFG_HOLDER){ // if system_config doesn't contain the proper check value
  87. os_memset(&system_config, 0x00, sizeof(System_Config_t)); // clear system_config
  88. system_config.cfg_holder = CFG_HOLDER;
  89. os_sprintf(system_config.sta_ssid[0], "%s", DEFAULT_STA_SSID); // load STA_SSID into system_config
  90. os_sprintf(system_config.sta_pwd[0], "%s", DEDFAULT_STA_PASS); // load STA_PASS into system_config
  91. system_config.sta_type[0] = DEFAULT_STA_TYPE; // load STA_TYPE into system_config
  92. system_config.registered_stations = 1;
  93. system_config.current_station = 0;
  94. wifi_get_macaddr(STATION_IF, system_config.device_id);
  95. os_sprintf(system_config.mqtt_host, "%s", DEFAULT_MQTT_HOST); // load MQTT_HOST
  96. system_config.mqtt_port = DEFAULT_MQTT_PORT; // load MQTT_PORT
  97. os_sprintf(system_config.mqtt_user, "%s", DEFAULT_MQTT_USER); // load MQTT_USER
  98. os_sprintf(system_config.mqtt_pass, "%s", DEFAULT_MQTT_PASS); // load MQTT_PASS
  99. system_config.security = DEFAULT_SECURITY; /* default non ssl */
  100. system_config.mqtt_keepalive = DEFAULT_MQTT_KEEPALIVE; // load MQTT_KEEPALIVE
  101. os_sprintf(system_config.ota_host, "%s", DEFAULT_OTA_HOST);
  102. system_config.ota_port = DEFAULT_OTA_PORT;
  103. cfg_save();
  104. }
  105. }