config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "ets_sys.h"
  39. #include "os_type.h"
  40. #include "mem.h"
  41. #include "osapi.h"
  42. #include "user_interface.h"
  43. #include "mqtt/mqtt.h"
  44. #include "modules/config.h"
  45. #include "user_config.h"
  46. #include "driver/uart.h"
  47. /* CFG_LOCATION: 0x2
  48. * (CFG_LOCATION + 0)*0x1000 = 0x2000
  49. * (CFG_LOCATION + 1)*0x1000 = 0x3000
  50. * (CFG_LOCATION + 2)*0x1000 = 0x4000
  51. * (CFG_LOCATOIN + 3)*0x1000 = 0x5000
  52. *
  53. */
  54. SYSCFG sysCfg;
  55. SAVE_FLAG saveFlag;
  56. MQTT_TOPICS mqttTopics;
  57. void ICACHE_FLASH_ATTR cfg_save()
  58. {
  59. #if CONFIG_DEBUG
  60. uart0_sendStr("Writing New Default Module Configuration\r\n");
  61. #endif
  62. spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // read config sector 3 for
  63. (uint32 *)&saveFlag, sizeof(SAVE_FLAG)); // saveFlag
  64. if (saveFlag.flag == 0) {
  65. spi_flash_erase_sector(CFG_LOCATION + 1); // erase config sector 1
  66. spi_flash_write((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, // write sysCfg in config sector 1
  67. (uint32 *)&sysCfg, sizeof(SYSCFG));
  68. saveFlag.flag = 1; // set saveFlag.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 saveFlag to config sector 3
  71. (uint32 *)&saveFlag, sizeof(SAVE_FLAG));
  72. }
  73. else {
  74. spi_flash_erase_sector(CFG_LOCATION + 0); // erase config sector 0 for sysCfg
  75. spi_flash_write((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, // write sysCfg to config sector 0
  76. (uint32 *)&sysCfg, sizeof(SYSCFG));
  77. saveFlag.flag = 0; // clear saveFlag.flag
  78. spi_flash_erase_sector(CFG_LOCATION + 3); // erase config sector 3
  79. spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // write saveFlag to config sector 3
  80. (uint32 *)&saveFlag, sizeof(SAVE_FLAG));
  81. }
  82. }
  83. void ICACHE_FLASH_ATTR cfg_load()
  84. {
  85. #if CONFIG_DEBUG
  86. uart0_sendStr("Loading Default Module Configuration...\r\n");
  87. #endif
  88. spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, // read save Flag to config sector 3
  89. (uint32 *)&saveFlag, sizeof(SAVE_FLAG));
  90. if (saveFlag.flag == 0) { // if saveFlag.flag == 0
  91. spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, // write sysCfg to config sector 0
  92. (uint32 *)&sysCfg, sizeof(SYSCFG));
  93. } else { // if saveFlag.flag == 1
  94. spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, // write sysCfg to config sector 1
  95. (uint32 *)&sysCfg, sizeof(SYSCFG));
  96. }
  97. if(sysCfg.cfg_holder != CFG_HOLDER){ // if sysCfg doesn't contain the proper check value
  98. os_memset(&sysCfg, 0x00, sizeof sysCfg); // clear sysCfg
  99. sysCfg.cfg_holder = CFG_HOLDER;
  100. os_sprintf(sysCfg.sta_ssid[0], "%s", STA_SSID); // load STA_SSID into sysCfg
  101. os_sprintf(sysCfg.sta_pwd[0], "%s", STA_PASS); // load STA_PASS into sysCfg
  102. sysCfg.sta_type[0] = STA_TYPE; // load STA_TYPE into sysCfg
  103. sysCfg.registered_stations = 1;
  104. sysCfg.current_station = 0;
  105. wifi_get_macaddr(STATION_IF, sysCfg.device_id);
  106. os_sprintf(sysCfg.mqtt_host, "%s", MQTT_HOST); // load MQTT_HOST
  107. sysCfg.mqtt_port = MQTT_PORT; // load MQTT_PORT
  108. os_sprintf(sysCfg.mqtt_user, "%s", MQTT_USER); // load MQTT_USER
  109. os_sprintf(sysCfg.mqtt_pass, "%s", MQTT_PASS); // load MQTT_PASS
  110. sysCfg.security = DEFAULT_SECURITY; /* default non ssl */
  111. sysCfg.mqtt_keepalive = MQTT_KEEPALIVE; // load MQTT_KEEPALIVE
  112. os_sprintf(sysCfg.ota_host, "%s", OTA_HOST);
  113. sysCfg.ota_port = OTA_PORT;
  114. cfg_save();
  115. }
  116. sysCfg.module_state.all_flags = 0U; // clear all flags
  117. sysCfg.uart_state = 0;
  118. sysCfg.module_state.alive = true;
  119. os_sprintf(mqttTopics.data, "/encoder/" MACSTR "/data", MAC2STR(sysCfg.device_id));
  120. os_sprintf(mqttTopics.command, "/encoder/" MACSTR "/command", MAC2STR(sysCfg.device_id));
  121. os_sprintf(mqttTopics.health, "/encoder/" MACSTR "/health", MAC2STR(sysCfg.device_id));
  122. }