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.

371 lines
10 KiB

8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. SETTINGS MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "Embedis.h"
  6. #include <EEPROM.h>
  7. #include "spi_flash.h"
  8. #include <StreamString.h>
  9. #if TELNET_SUPPORT
  10. #include "settings.h"
  11. #ifdef DEBUG_PORT
  12. StreamInjector _serial = StreamInjector(DEBUG_PORT);
  13. #else
  14. StreamInjector _serial = StreamInjector(Serial);
  15. #endif
  16. Embedis embedis(_serial);
  17. #else
  18. #ifdef DEBUG_PORT
  19. Embedis embedis(DEBUG_PORT);
  20. #else
  21. Embedis embedis(_serial);
  22. #endif
  23. #endif
  24. bool _settings_save = false;
  25. // -----------------------------------------------------------------------------
  26. // Settings
  27. // -----------------------------------------------------------------------------
  28. #if TELNET_SUPPORT
  29. void settingsInject(void *data, size_t len) {
  30. _serial.inject((char *) data, len);
  31. }
  32. #endif
  33. size_t settingsMaxSize() {
  34. size_t size = EEPROM_SIZE;
  35. if (size > SPI_FLASH_SEC_SIZE) size = SPI_FLASH_SEC_SIZE;
  36. size = (size + 3) & (~3);
  37. return size;
  38. }
  39. unsigned long settingsSize() {
  40. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  41. while (size_t len = EEPROM.read(pos)) {
  42. pos = pos - len - 2;
  43. }
  44. return SPI_FLASH_SEC_SIZE - pos;
  45. }
  46. unsigned int settingsKeyCount() {
  47. unsigned count = 0;
  48. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  49. while (size_t len = EEPROM.read(pos)) {
  50. pos = pos - len - 2;
  51. len = EEPROM.read(pos);
  52. pos = pos - len - 2;
  53. count ++;
  54. }
  55. return count;
  56. }
  57. String settingsKeyName(unsigned int index) {
  58. String s;
  59. unsigned count = 0;
  60. unsigned pos = SPI_FLASH_SEC_SIZE - 1;
  61. while (size_t len = EEPROM.read(pos)) {
  62. pos = pos - len - 2;
  63. if (count == index) {
  64. s.reserve(len);
  65. for (unsigned char i = 0 ; i < len; i++) {
  66. s += (char) EEPROM.read(pos + i + 1);
  67. }
  68. break;
  69. }
  70. count++;
  71. len = EEPROM.read(pos);
  72. pos = pos - len - 2;
  73. }
  74. return s;
  75. }
  76. void settingsFactoryReset() {
  77. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  78. EEPROM.write(i, 0xFF);
  79. }
  80. EEPROM.commit();
  81. }
  82. void settingsSetup() {
  83. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  84. #if TELNET_SUPPORT
  85. _serial.callback([](uint8_t ch) {
  86. telnetWrite(ch);
  87. });
  88. #endif
  89. Embedis::dictionary( F("EEPROM"),
  90. SPI_FLASH_SEC_SIZE,
  91. [](size_t pos) -> char { return EEPROM.read(pos); },
  92. [](size_t pos, char value) { EEPROM.write(pos, value); },
  93. #if SETTINGS_AUTOSAVE
  94. []() { _settings_save = true; }
  95. #else
  96. []() {}
  97. #endif
  98. );
  99. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  100. StreamString s;
  101. WiFi.printDiag(s);
  102. DEBUG_MSG(s.c_str());
  103. }, 0);
  104. // -------------------------------------------------------------------------
  105. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  106. wifiConfigure();
  107. wifiDisconnect();
  108. DEBUG_MSG_P(PSTR("+OK\n"));
  109. });
  110. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  111. mqttConfigure();
  112. mqttDisconnect();
  113. DEBUG_MSG_P(PSTR("+OK\n"));
  114. });
  115. Embedis::command( F("INFO"), [](Embedis* e) {
  116. welcome();
  117. DEBUG_MSG_P(PSTR("+OK\n"));
  118. });
  119. Embedis::command( F("UPTIME"), [](Embedis* e) {
  120. DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
  121. DEBUG_MSG_P(PSTR("+OK\n"));
  122. });
  123. Embedis::command( F("RESET"), [](Embedis* e) {
  124. DEBUG_MSG_P(PSTR("+OK\n"));
  125. deferredReset(100, CUSTOM_RESET_TERMINAL);
  126. });
  127. Embedis::command( F("ERASE.CONFIG"), [](Embedis* e) {
  128. DEBUG_MSG_P(PSTR("+OK\n"));
  129. resetReason(CUSTOM_RESET_TERMINAL);
  130. ESP.eraseConfig();
  131. *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494
  132. });
  133. #if NOFUSS_SUPPORT
  134. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  135. DEBUG_MSG_P(PSTR("+OK\n"));
  136. nofussRun();
  137. });
  138. #endif
  139. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  140. settingsFactoryReset();
  141. DEBUG_MSG_P(PSTR("+OK\n"));
  142. });
  143. Embedis::command( F("HEAP"), [](Embedis* e) {
  144. DEBUG_MSG_P(PSTR("Free HEAP: %d bytes\n"), ESP.getFreeHeap());
  145. DEBUG_MSG_P(PSTR("+OK\n"));
  146. });
  147. Embedis::command( F("RELAY"), [](Embedis* e) {
  148. if (e->argc < 2) {
  149. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  150. }
  151. int id = String(e->argv[1]).toInt();
  152. if (e->argc > 2) {
  153. int value = String(e->argv[2]).toInt();
  154. if (value == 2) {
  155. relayToggle(id);
  156. } else {
  157. relayStatus(id, value == 1);
  158. }
  159. }
  160. DEBUG_MSG_P(PSTR("Status: %s\n"), relayStatus(id) ? "true" : "false");
  161. DEBUG_MSG_P(PSTR("+OK\n"));
  162. });
  163. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  164. if (lightHasColor()) {
  165. Embedis::command( F("COLOR"), [](Embedis* e) {
  166. if (e->argc > 1) {
  167. String color = String(e->argv[1]);
  168. lightColor(color.c_str());
  169. lightUpdate(true, true);
  170. }
  171. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  172. DEBUG_MSG_P(PSTR("+OK\n"));
  173. });
  174. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  175. if (e->argc > 1) {
  176. lightBrightness(String(e->argv[1]).toInt());
  177. lightUpdate(true, true);
  178. }
  179. DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
  180. DEBUG_MSG_P(PSTR("+OK\n"));
  181. });
  182. Embedis::command( F("MIRED"), [](Embedis* e) {
  183. if (e->argc > 1) {
  184. String color = String("M") + String(e->argv[1]);
  185. lightColor(color.c_str());
  186. lightUpdate(true, true);
  187. }
  188. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  189. DEBUG_MSG_P(PSTR("+OK\n"));
  190. });
  191. Embedis::command( F("KELVIN"), [](Embedis* e) {
  192. if (e->argc > 1) {
  193. String color = String("K") + String(e->argv[1]);
  194. lightColor(color.c_str());
  195. lightUpdate(true, true);
  196. }
  197. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  198. DEBUG_MSG_P(PSTR("+OK\n"));
  199. });
  200. }
  201. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  202. if (e->argc < 2) {
  203. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  204. }
  205. int id = String(e->argv[1]).toInt();
  206. if (e->argc > 2) {
  207. int value = String(e->argv[2]).toInt();
  208. lightChannel(id, value);
  209. lightUpdate(true, true);
  210. }
  211. DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id));
  212. DEBUG_MSG_P(PSTR("+OK\n"));
  213. });
  214. #endif
  215. Embedis::command( F("EEPROM"), [](Embedis* e) {
  216. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  217. DEBUG_MSG_P(PSTR("Number of keys: %d\n"), settingsKeyCount());
  218. DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  219. DEBUG_MSG_P(PSTR("+OK\n"));
  220. });
  221. Embedis::command( F("DUMP"), [](Embedis* e) {
  222. unsigned int size = settingsKeyCount();
  223. for (unsigned int i=0; i<size; i++) {
  224. String key = settingsKeyName(i);
  225. String value = getSetting(key);
  226. DEBUG_MSG_P(PSTR("+%s => %s\n"), key.c_str(), value.c_str());
  227. }
  228. DEBUG_MSG_P(PSTR("+OK\n"));
  229. });
  230. #if DEBUG_SUPPORT
  231. Embedis::command( F("CRASH"), [](Embedis* e) {
  232. debugDumpCrashInfo();
  233. DEBUG_MSG_P(PSTR("+OK\n"));
  234. });
  235. #endif
  236. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  237. bool ascii = false;
  238. if (e->argc == 2) ascii = String(e->argv[1]).toInt() == 1;
  239. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  240. if (i % 16 == 0) DEBUG_MSG_P(PSTR("\n[%04X] "), i);
  241. byte c = EEPROM.read(i);
  242. if (ascii && 32 <= c && c <= 126) {
  243. DEBUG_MSG_P(PSTR(" %c "), c);
  244. } else {
  245. DEBUG_MSG_P(PSTR("%02X "), c);
  246. }
  247. }
  248. DEBUG_MSG_P(PSTR("\n+OK\n"));
  249. });
  250. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  251. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  252. }
  253. void settingsDump() {
  254. unsigned int size = settingsKeyCount();
  255. for (unsigned int i=0; i<size; i++) {
  256. String key = settingsKeyName(i);
  257. String value = getSetting(key);
  258. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  259. }
  260. }
  261. void settingsLoop() {
  262. if (_settings_save) {
  263. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  264. EEPROM.commit();
  265. _settings_save = false;
  266. }
  267. #if TERMINAL_SUPPORT
  268. embedis.process();
  269. #endif
  270. }
  271. void moveSetting(const char * from, const char * to) {
  272. String value = getSetting(from);
  273. if (value.length() > 0) setSetting(to, value);
  274. delSetting(from);
  275. }
  276. template<typename T> String getSetting(const String& key, T defaultValue) {
  277. String value;
  278. if (!Embedis::get(key, value)) value = String(defaultValue);
  279. return value;
  280. }
  281. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  282. return getSetting(key + String(index), defaultValue);
  283. }
  284. String getSetting(const String& key) {
  285. return getSetting(key, "");
  286. }
  287. template<typename T> bool setSetting(const String& key, T value) {
  288. return Embedis::set(key, String(value));
  289. }
  290. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  291. return setSetting(key + String(index), value);
  292. }
  293. bool delSetting(const String& key) {
  294. return Embedis::del(key);
  295. }
  296. bool delSetting(const String& key, unsigned int index) {
  297. return delSetting(key + String(index));
  298. }
  299. bool hasSetting(const String& key) {
  300. return getSetting(key).length() != 0;
  301. }
  302. bool hasSetting(const String& key, unsigned int index) {
  303. return getSetting(key, index, "").length() != 0;
  304. }
  305. void saveSettings() {
  306. #if not SETTINGS_AUTOSAVE
  307. _settings_save = true;
  308. #endif
  309. //settingsDump();
  310. }