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.

418 lines
12 KiB

6 years ago
8 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
  1. /*
  2. SETTINGS MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM.h>
  6. #include "spi_flash.h"
  7. #include "libs/EmbedisWrap.h"
  8. #include <StreamString.h>
  9. #if TELNET_SUPPORT
  10. #include "libs/StreamInjector.h"
  11. #ifdef DEBUG_PORT
  12. StreamInjector _serial = StreamInjector(DEBUG_PORT);
  13. #else
  14. StreamInjector _serial = StreamInjector(Serial);
  15. #endif
  16. EmbedisWrap embedis(_serial);
  17. #else
  18. #ifdef DEBUG_PORT
  19. EmbedisWrap embedis(DEBUG_PORT);
  20. #else
  21. EmbedisWrap 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. bool settingsRestore(JsonObject& data) {
  77. const char* app = data["app"];
  78. if (strcmp(app, APP_NAME) != 0) return false;
  79. for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) {
  80. EEPROM.write(i, 0xFF);
  81. }
  82. for (auto element : data) {
  83. if (strcmp(element.key, "app") == 0) continue;
  84. if (strcmp(element.key, "version") == 0) continue;
  85. setSetting(element.key, element.value.as<char*>());
  86. }
  87. saveSettings();
  88. DEBUG_MSG_P(PSTR("[SETTINGS] Settings restored successfully\n"));
  89. return true;
  90. }
  91. void settingsFactoryReset() {
  92. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  93. EEPROM.write(i, 0xFF);
  94. }
  95. EEPROM.commit();
  96. }
  97. void settingsHelp() {
  98. unsigned char len = embedis.getCommandsCount();
  99. DEBUG_MSG_P(PSTR("\nAvailable commands:\n\n"));
  100. for (unsigned char i=0; i<len; i++) {
  101. DEBUG_MSG_P(PSTR("* %s\n"), embedis.getCommandName(i).c_str());
  102. if (embedis.getCommandName(i).equals("WRITE")) {
  103. DEBUG_MSG_P(PSTR("\n"));
  104. }
  105. }
  106. DEBUG_MSG_P(PSTR("\n"));
  107. }
  108. void settingsSetup() {
  109. EEPROM.begin(SPI_FLASH_SEC_SIZE);
  110. #if TELNET_SUPPORT
  111. _serial.callback([](uint8_t ch) {
  112. telnetWrite(ch);
  113. });
  114. #endif
  115. Embedis::dictionary( F("EEPROM"),
  116. SPI_FLASH_SEC_SIZE,
  117. [](size_t pos) -> char { return EEPROM.read(pos); },
  118. [](size_t pos, char value) { EEPROM.write(pos, value); },
  119. #if SETTINGS_AUTOSAVE
  120. []() { _settings_save = true; }
  121. #else
  122. []() {}
  123. #endif
  124. );
  125. Embedis::hardware( F("WIFI"), [](Embedis* e) {
  126. StreamString s;
  127. WiFi.printDiag(s);
  128. DEBUG_MSG(s.c_str());
  129. }, 0);
  130. // -------------------------------------------------------------------------
  131. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  132. Embedis::command( F("BRIGHTNESS"), [](Embedis* e) {
  133. if (e->argc > 1) {
  134. lightBrightness(String(e->argv[1]).toInt());
  135. lightUpdate(true, true);
  136. }
  137. DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
  138. DEBUG_MSG_P(PSTR("+OK\n"));
  139. });
  140. #endif
  141. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  142. Embedis::command( F("CHANNEL"), [](Embedis* e) {
  143. if (e->argc < 2) {
  144. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  145. }
  146. int id = String(e->argv[1]).toInt();
  147. if (e->argc > 2) {
  148. int value = String(e->argv[2]).toInt();
  149. lightChannel(id, value);
  150. lightUpdate(true, true);
  151. }
  152. DEBUG_MSG_P(PSTR("Channel #%d: %d\n"), id, lightChannel(id));
  153. DEBUG_MSG_P(PSTR("+OK\n"));
  154. });
  155. #endif
  156. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  157. Embedis::command( F("COLOR"), [](Embedis* e) {
  158. if (e->argc > 1) {
  159. String color = String(e->argv[1]);
  160. lightColor(color.c_str());
  161. lightUpdate(true, true);
  162. }
  163. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  164. DEBUG_MSG_P(PSTR("+OK\n"));
  165. });
  166. #endif
  167. #if DEBUG_SUPPORT
  168. Embedis::command( F("CRASH"), [](Embedis* e) {
  169. debugDumpCrashInfo();
  170. DEBUG_MSG_P(PSTR("+OK\n"));
  171. });
  172. #endif
  173. Embedis::command( F("DUMP"), [](Embedis* e) {
  174. unsigned int size = settingsKeyCount();
  175. for (unsigned int i=0; i<size; i++) {
  176. String key = settingsKeyName(i);
  177. String value = getSetting(key);
  178. DEBUG_MSG_P(PSTR("+%s => %s\n"), key.c_str(), value.c_str());
  179. }
  180. DEBUG_MSG_P(PSTR("+OK\n"));
  181. });
  182. Embedis::command( F("DUMP.RAW"), [](Embedis* e) {
  183. bool ascii = false;
  184. if (e->argc == 2) ascii = String(e->argv[1]).toInt() == 1;
  185. for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) {
  186. if (i % 16 == 0) DEBUG_MSG_P(PSTR("\n[%04X] "), i);
  187. byte c = EEPROM.read(i);
  188. if (ascii && 32 <= c && c <= 126) {
  189. DEBUG_MSG_P(PSTR(" %c "), c);
  190. } else {
  191. DEBUG_MSG_P(PSTR("%02X "), c);
  192. }
  193. }
  194. DEBUG_MSG_P(PSTR("\n+OK\n"));
  195. });
  196. Embedis::command( F("EEPROM"), [](Embedis* e) {
  197. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  198. DEBUG_MSG_P(PSTR("Number of keys: %d\n"), settingsKeyCount());
  199. DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  200. DEBUG_MSG_P(PSTR("+OK\n"));
  201. });
  202. Embedis::command( F("ERASE.CONFIG"), [](Embedis* e) {
  203. DEBUG_MSG_P(PSTR("+OK\n"));
  204. resetReason(CUSTOM_RESET_TERMINAL);
  205. ESP.eraseConfig();
  206. *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494
  207. });
  208. Embedis::command( F("FACTORY.RESET"), [](Embedis* e) {
  209. settingsFactoryReset();
  210. DEBUG_MSG_P(PSTR("+OK\n"));
  211. });
  212. Embedis::command( F("HEAP"), [](Embedis* e) {
  213. DEBUG_MSG_P(PSTR("Free HEAP: %d bytes\n"), getFreeHeap());
  214. DEBUG_MSG_P(PSTR("+OK\n"));
  215. });
  216. Embedis::command( F("HELP"), [](Embedis* e) {
  217. settingsHelp();
  218. DEBUG_MSG_P(PSTR("+OK\n"));
  219. });
  220. Embedis::command( F("INFO"), [](Embedis* e) {
  221. welcome();
  222. wifiStatus();
  223. DEBUG_MSG_P(PSTR("+OK\n"));
  224. });
  225. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  226. Embedis::command( F("KELVIN"), [](Embedis* e) {
  227. if (e->argc > 1) {
  228. String color = String("K") + String(e->argv[1]);
  229. lightColor(color.c_str());
  230. lightUpdate(true, true);
  231. }
  232. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  233. DEBUG_MSG_P(PSTR("+OK\n"));
  234. });
  235. #endif
  236. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  237. Embedis::command( F("MIRED"), [](Embedis* e) {
  238. if (e->argc > 1) {
  239. String color = String("M") + String(e->argv[1]);
  240. lightColor(color.c_str());
  241. lightUpdate(true, true);
  242. }
  243. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  244. DEBUG_MSG_P(PSTR("+OK\n"));
  245. });
  246. #endif
  247. #if NOFUSS_SUPPORT
  248. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  249. DEBUG_MSG_P(PSTR("+OK\n"));
  250. nofussRun();
  251. });
  252. #endif
  253. Embedis::command( F("RELAY"), [](Embedis* e) {
  254. if (e->argc < 2) {
  255. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  256. }
  257. int id = String(e->argv[1]).toInt();
  258. if (e->argc > 2) {
  259. int value = String(e->argv[2]).toInt();
  260. if (value == 2) {
  261. relayToggle(id);
  262. } else {
  263. relayStatus(id, value == 1);
  264. }
  265. }
  266. DEBUG_MSG_P(PSTR("Status: %s\n"), relayStatus(id) ? "true" : "false");
  267. DEBUG_MSG_P(PSTR("+OK\n"));
  268. });
  269. Embedis::command( F("RESET"), [](Embedis* e) {
  270. DEBUG_MSG_P(PSTR("+OK\n"));
  271. deferredReset(100, CUSTOM_RESET_TERMINAL);
  272. });
  273. #if MQTT_SUPPORT
  274. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  275. mqttConfigure();
  276. mqttDisconnect();
  277. DEBUG_MSG_P(PSTR("+OK\n"));
  278. });
  279. #endif
  280. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  281. wifiConfigure();
  282. wifiDisconnect();
  283. DEBUG_MSG_P(PSTR("+OK\n"));
  284. });
  285. Embedis::command( F("UPTIME"), [](Embedis* e) {
  286. DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
  287. DEBUG_MSG_P(PSTR("+OK\n"));
  288. });
  289. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  290. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  291. }
  292. void settingsDump() {
  293. unsigned int size = settingsKeyCount();
  294. for (unsigned int i=0; i<size; i++) {
  295. String key = settingsKeyName(i);
  296. String value = getSetting(key);
  297. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  298. }
  299. }
  300. void settingsLoop() {
  301. if (_settings_save) {
  302. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  303. EEPROM.commit();
  304. _settings_save = false;
  305. }
  306. #if TERMINAL_SUPPORT
  307. embedis.process();
  308. #endif
  309. }
  310. void saveSettings() {
  311. #if not SETTINGS_AUTOSAVE
  312. _settings_save = true;
  313. #endif
  314. //settingsDump();
  315. }
  316. // -----------------------------------------------------------------------------
  317. void moveSetting(const char * from, const char * to) {
  318. String value = getSetting(from);
  319. if (value.length() > 0) setSetting(to, value);
  320. delSetting(from);
  321. }
  322. template<typename T> String getSetting(const String& key, T defaultValue) {
  323. String value;
  324. if (!Embedis::get(key, value)) value = String(defaultValue);
  325. return value;
  326. }
  327. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  328. return getSetting(key + String(index), defaultValue);
  329. }
  330. String getSetting(const String& key) {
  331. return getSetting(key, "");
  332. }
  333. template<typename T> bool setSetting(const String& key, T value) {
  334. return Embedis::set(key, String(value));
  335. }
  336. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  337. return setSetting(key + String(index), value);
  338. }
  339. bool delSetting(const String& key) {
  340. return Embedis::del(key);
  341. }
  342. bool delSetting(const String& key, unsigned int index) {
  343. return delSetting(key + String(index));
  344. }
  345. bool hasSetting(const String& key) {
  346. return getSetting(key).length() != 0;
  347. }
  348. bool hasSetting(const String& key, unsigned int index) {
  349. return getSetting(key, index, "").length() != 0;
  350. }