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.

436 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
7 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("GPIO"), [](Embedis* e) {
  213. if (e->argc < 2) {
  214. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  215. return;
  216. }
  217. int pin = String(e->argv[1]).toInt();
  218. //if (!gpioValid(pin)) {
  219. // DEBUG_MSG_P(PSTR("-ERROR: Invalid GPIO\n"));
  220. // return;
  221. //}
  222. if (e->argc > 2) {
  223. bool state = String(e->argv[2]).toInt() == 1;
  224. digitalWrite(pin, state);
  225. }
  226. DEBUG_MSG_P(PSTR("GPIO %d is %s\n"), pin, digitalRead(pin) == HIGH ? "HIGH" : "LOW");
  227. DEBUG_MSG_P(PSTR("+OK\n"));
  228. });
  229. Embedis::command( F("HEAP"), [](Embedis* e) {
  230. DEBUG_MSG_P(PSTR("Free HEAP: %d bytes\n"), getFreeHeap());
  231. DEBUG_MSG_P(PSTR("+OK\n"));
  232. });
  233. Embedis::command( F("HELP"), [](Embedis* e) {
  234. settingsHelp();
  235. DEBUG_MSG_P(PSTR("+OK\n"));
  236. });
  237. Embedis::command( F("INFO"), [](Embedis* e) {
  238. welcome();
  239. wifiStatus();
  240. DEBUG_MSG_P(PSTR("+OK\n"));
  241. });
  242. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  243. Embedis::command( F("KELVIN"), [](Embedis* e) {
  244. if (e->argc > 1) {
  245. String color = String("K") + String(e->argv[1]);
  246. lightColor(color.c_str());
  247. lightUpdate(true, true);
  248. }
  249. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  250. DEBUG_MSG_P(PSTR("+OK\n"));
  251. });
  252. #endif
  253. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  254. Embedis::command( F("MIRED"), [](Embedis* e) {
  255. if (e->argc > 1) {
  256. String color = String("M") + String(e->argv[1]);
  257. lightColor(color.c_str());
  258. lightUpdate(true, true);
  259. }
  260. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  261. DEBUG_MSG_P(PSTR("+OK\n"));
  262. });
  263. #endif
  264. #if NOFUSS_SUPPORT
  265. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  266. DEBUG_MSG_P(PSTR("+OK\n"));
  267. nofussRun();
  268. });
  269. #endif
  270. Embedis::command( F("RELAY"), [](Embedis* e) {
  271. if (e->argc < 2) {
  272. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  273. }
  274. int id = String(e->argv[1]).toInt();
  275. if (e->argc > 2) {
  276. int value = String(e->argv[2]).toInt();
  277. if (value == 2) {
  278. relayToggle(id);
  279. } else {
  280. relayStatus(id, value == 1);
  281. }
  282. }
  283. DEBUG_MSG_P(PSTR("Status: %s\n"), relayStatus(id) ? "true" : "false");
  284. DEBUG_MSG_P(PSTR("+OK\n"));
  285. });
  286. Embedis::command( F("RESET"), [](Embedis* e) {
  287. DEBUG_MSG_P(PSTR("+OK\n"));
  288. deferredReset(100, CUSTOM_RESET_TERMINAL);
  289. });
  290. #if MQTT_SUPPORT
  291. Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
  292. mqttConfigure();
  293. mqttDisconnect();
  294. DEBUG_MSG_P(PSTR("+OK\n"));
  295. });
  296. #endif
  297. Embedis::command( F("RESET.WIFI"), [](Embedis* e) {
  298. wifiConfigure();
  299. wifiDisconnect();
  300. DEBUG_MSG_P(PSTR("+OK\n"));
  301. });
  302. Embedis::command( F("UPTIME"), [](Embedis* e) {
  303. DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
  304. DEBUG_MSG_P(PSTR("+OK\n"));
  305. });
  306. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  307. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  308. }
  309. void settingsDump() {
  310. unsigned int size = settingsKeyCount();
  311. for (unsigned int i=0; i<size; i++) {
  312. String key = settingsKeyName(i);
  313. String value = getSetting(key);
  314. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  315. }
  316. }
  317. void settingsLoop() {
  318. if (_settings_save) {
  319. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  320. EEPROM.commit();
  321. _settings_save = false;
  322. }
  323. #if TERMINAL_SUPPORT
  324. embedis.process();
  325. #endif
  326. }
  327. void saveSettings() {
  328. #if not SETTINGS_AUTOSAVE
  329. _settings_save = true;
  330. #endif
  331. //settingsDump();
  332. }
  333. // -----------------------------------------------------------------------------
  334. void moveSetting(const char * from, const char * to) {
  335. String value = getSetting(from);
  336. if (value.length() > 0) setSetting(to, value);
  337. delSetting(from);
  338. }
  339. template<typename T> String getSetting(const String& key, T defaultValue) {
  340. String value;
  341. if (!Embedis::get(key, value)) value = String(defaultValue);
  342. return value;
  343. }
  344. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  345. return getSetting(key + String(index), defaultValue);
  346. }
  347. String getSetting(const String& key) {
  348. return getSetting(key, "");
  349. }
  350. template<typename T> bool setSetting(const String& key, T value) {
  351. return Embedis::set(key, String(value));
  352. }
  353. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  354. return setSetting(key + String(index), value);
  355. }
  356. bool delSetting(const String& key) {
  357. return Embedis::del(key);
  358. }
  359. bool delSetting(const String& key, unsigned int index) {
  360. return delSetting(key + String(index));
  361. }
  362. bool hasSetting(const String& key) {
  363. return getSetting(key).length() != 0;
  364. }
  365. bool hasSetting(const String& key, unsigned int index) {
  366. return getSetting(key, index, "").length() != 0;
  367. }