Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.9 KiB

6 years ago
6 years ago
6 years ago
  1. /*
  2. SETTINGS MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <vector>
  6. #include "libs/EmbedisWrap.h"
  7. // -----------------------------------------------------------------------------
  8. // Reverse engineering EEPROM storage format
  9. // -----------------------------------------------------------------------------
  10. unsigned long settingsSize() {
  11. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  12. while (size_t len = EEPROMr.read(pos)) {
  13. if (0xFF == len) break;
  14. pos = pos - len - 2;
  15. }
  16. return SPI_FLASH_SEC_SIZE - pos + EEPROM_DATA_END;
  17. }
  18. // -----------------------------------------------------------------------------
  19. unsigned int settingsKeyCount() {
  20. unsigned count = 0;
  21. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  22. while (size_t len = EEPROMr.read(pos)) {
  23. if (0xFF == len) break;
  24. pos = pos - len - 2;
  25. len = EEPROMr.read(pos);
  26. pos = pos - len - 2;
  27. count ++;
  28. }
  29. return count;
  30. }
  31. String settingsKeyName(unsigned int index) {
  32. String s;
  33. unsigned count = 0;
  34. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  35. while (size_t len = EEPROMr.read(pos)) {
  36. if (0xFF == len) break;
  37. pos = pos - len - 2;
  38. if (count == index) {
  39. s.reserve(len);
  40. for (unsigned char i = 0 ; i < len; i++) {
  41. s += (char) EEPROMr.read(pos + i + 1);
  42. }
  43. break;
  44. }
  45. count++;
  46. len = EEPROMr.read(pos);
  47. pos = pos - len - 2;
  48. }
  49. return s;
  50. }
  51. std::vector<String> _settingsKeys() {
  52. // Get sorted list of keys
  53. std::vector<String> keys;
  54. //unsigned int size = settingsKeyCount();
  55. unsigned int size = settingsKeyCount();
  56. for (unsigned int i=0; i<size; i++) {
  57. //String key = settingsKeyName(i);
  58. String key = settingsKeyName(i);
  59. bool inserted = false;
  60. for (unsigned char j=0; j<keys.size(); j++) {
  61. // Check if we have to insert it before the current element
  62. if (keys[j].compareTo(key) > 0) {
  63. keys.insert(keys.begin() + j, key);
  64. inserted = true;
  65. break;
  66. }
  67. }
  68. // If we could not insert it, just push it at the end
  69. if (!inserted) keys.push_back(key);
  70. }
  71. return keys;
  72. }
  73. // -----------------------------------------------------------------------------
  74. // Key-value API
  75. // -----------------------------------------------------------------------------
  76. void moveSetting(const char * from, const char * to) {
  77. String value = getSetting(from);
  78. if (value.length() > 0) setSetting(to, value);
  79. delSetting(from);
  80. }
  81. template<typename T> String getSetting(const String& key, T defaultValue) {
  82. String value;
  83. if (!Embedis::get(key, value)) value = String(defaultValue);
  84. return value;
  85. }
  86. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  87. return getSetting(key + String(index), defaultValue);
  88. }
  89. String getSetting(const String& key) {
  90. return getSetting(key, "");
  91. }
  92. template<typename T> bool setSetting(const String& key, T value) {
  93. return Embedis::set(key, String(value));
  94. }
  95. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  96. return setSetting(key + String(index), value);
  97. }
  98. bool delSetting(const String& key) {
  99. return Embedis::del(key);
  100. }
  101. bool delSetting(const String& key, unsigned int index) {
  102. return delSetting(key + String(index));
  103. }
  104. bool hasSetting(const String& key) {
  105. return getSetting(key).length() != 0;
  106. }
  107. bool hasSetting(const String& key, unsigned int index) {
  108. return getSetting(key, index, "").length() != 0;
  109. }
  110. void saveSettings() {
  111. #if not SETTINGS_AUTOSAVE
  112. eepromCommit();
  113. #endif
  114. }
  115. void resetSettings() {
  116. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  117. EEPROMr.write(i, 0xFF);
  118. }
  119. EEPROMr.commit();
  120. }
  121. // -----------------------------------------------------------------------------
  122. // API
  123. // -----------------------------------------------------------------------------
  124. size_t settingsMaxSize() {
  125. size_t size = EEPROM_SIZE;
  126. if (size > SPI_FLASH_SEC_SIZE) size = SPI_FLASH_SEC_SIZE;
  127. size = (size + 3) & (~3);
  128. return size;
  129. }
  130. bool settingsRestoreJson(JsonObject& data) {
  131. // Check this is an ESPurna configuration file (must have "app":"ESPURNA")
  132. const char* app = data["app"];
  133. if (!app || strcmp(app, APP_NAME) != 0) {
  134. DEBUG_MSG_P(PSTR("[SETTING] Wrong or missing 'app' key\n"));
  135. return false;
  136. }
  137. // Clear settings
  138. bool is_backup = data["backup"];
  139. if (is_backup) {
  140. for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) {
  141. EEPROMr.write(i, 0xFF);
  142. }
  143. }
  144. // Dump settings to memory buffer
  145. for (auto element : data) {
  146. if (strcmp(element.key, "app") == 0) continue;
  147. if (strcmp(element.key, "version") == 0) continue;
  148. if (strcmp(element.key, "backup") == 0) continue;
  149. setSetting(element.key, element.value.as<char*>());
  150. }
  151. // Persist to EEPROM
  152. saveSettings();
  153. DEBUG_MSG_P(PSTR("[SETTINGS] Settings restored successfully\n"));
  154. return true;
  155. }
  156. void settingsGetJson(JsonObject& root) {
  157. // Get sorted list of keys
  158. std::vector<String> keys = _settingsKeys();
  159. // Add the key-values to the json object
  160. for (unsigned int i=0; i<keys.size(); i++) {
  161. String value = getSetting(keys[i]);
  162. root[keys[i]] = value;
  163. }
  164. }
  165. // -----------------------------------------------------------------------------
  166. // Initialization
  167. // -----------------------------------------------------------------------------
  168. void settingsSetup() {
  169. Embedis::dictionary( F("EEPROM"),
  170. SPI_FLASH_SEC_SIZE,
  171. [](size_t pos) -> char { return EEPROMr.read(pos); },
  172. [](size_t pos, char value) { EEPROMr.write(pos, value); },
  173. #if SETTINGS_AUTOSAVE
  174. []() { eepromCommit(); }
  175. #else
  176. []() {}
  177. #endif
  178. );
  179. }