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.

441 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
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 MQTT_SUPPORT
  265. Embedis::command( F("MQTT.RESET"), [](Embedis* e) {
  266. mqttConfigure();
  267. mqttDisconnect();
  268. DEBUG_MSG_P(PSTR("+OK\n"));
  269. });
  270. #endif
  271. #if NOFUSS_SUPPORT
  272. Embedis::command( F("NOFUSS"), [](Embedis* e) {
  273. DEBUG_MSG_P(PSTR("+OK\n"));
  274. nofussRun();
  275. });
  276. #endif
  277. Embedis::command( F("RELAY"), [](Embedis* e) {
  278. if (e->argc < 2) {
  279. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  280. }
  281. int id = String(e->argv[1]).toInt();
  282. if (e->argc > 2) {
  283. int value = String(e->argv[2]).toInt();
  284. if (value == 2) {
  285. relayToggle(id);
  286. } else {
  287. relayStatus(id, value == 1);
  288. }
  289. }
  290. DEBUG_MSG_P(PSTR("Status: %s\n"), relayStatus(id) ? "true" : "false");
  291. DEBUG_MSG_P(PSTR("+OK\n"));
  292. });
  293. Embedis::command( F("RESET"), [](Embedis* e) {
  294. DEBUG_MSG_P(PSTR("+OK\n"));
  295. deferredReset(100, CUSTOM_RESET_TERMINAL);
  296. });
  297. Embedis::command( F("UPTIME"), [](Embedis* e) {
  298. DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
  299. DEBUG_MSG_P(PSTR("+OK\n"));
  300. });
  301. Embedis::command( F("WIFI.RESET"), [](Embedis* e) {
  302. wifiConfigure();
  303. wifiDisconnect();
  304. DEBUG_MSG_P(PSTR("+OK\n"));
  305. });
  306. Embedis::command( F("WIFI.SCAN"), [](Embedis* e) {
  307. wifiScan();
  308. DEBUG_MSG_P(PSTR("+OK\n"));
  309. });
  310. DEBUG_MSG_P(PSTR("[SETTINGS] EEPROM size: %d bytes\n"), SPI_FLASH_SEC_SIZE);
  311. DEBUG_MSG_P(PSTR("[SETTINGS] Settings size: %d bytes\n"), settingsSize());
  312. }
  313. void settingsDump() {
  314. unsigned int size = settingsKeyCount();
  315. for (unsigned int i=0; i<size; i++) {
  316. String key = settingsKeyName(i);
  317. String value = getSetting(key);
  318. DEBUG_MSG_P(PSTR("%s => %s\n"), key.c_str(), value.c_str());
  319. }
  320. }
  321. void settingsLoop() {
  322. if (_settings_save) {
  323. //DEBUG_MSG_P(PSTR("[SETTINGS] Saving\n"));
  324. EEPROM.commit();
  325. _settings_save = false;
  326. }
  327. #if TERMINAL_SUPPORT
  328. embedis.process();
  329. #endif
  330. }
  331. void saveSettings() {
  332. #if not SETTINGS_AUTOSAVE
  333. _settings_save = true;
  334. #endif
  335. //settingsDump();
  336. }
  337. // -----------------------------------------------------------------------------
  338. void moveSetting(const char * from, const char * to) {
  339. String value = getSetting(from);
  340. if (value.length() > 0) setSetting(to, value);
  341. delSetting(from);
  342. }
  343. template<typename T> String getSetting(const String& key, T defaultValue) {
  344. String value;
  345. if (!Embedis::get(key, value)) value = String(defaultValue);
  346. return value;
  347. }
  348. template<typename T> String getSetting(const String& key, unsigned int index, T defaultValue) {
  349. return getSetting(key + String(index), defaultValue);
  350. }
  351. String getSetting(const String& key) {
  352. return getSetting(key, "");
  353. }
  354. template<typename T> bool setSetting(const String& key, T value) {
  355. return Embedis::set(key, String(value));
  356. }
  357. template<typename T> bool setSetting(const String& key, unsigned int index, T value) {
  358. return setSetting(key + String(index), value);
  359. }
  360. bool delSetting(const String& key) {
  361. return Embedis::del(key);
  362. }
  363. bool delSetting(const String& key, unsigned int index) {
  364. return delSetting(key + String(index));
  365. }
  366. bool hasSetting(const String& key) {
  367. return getSetting(key).length() != 0;
  368. }
  369. bool hasSetting(const String& key, unsigned int index) {
  370. return getSetting(key, index, "").length() != 0;
  371. }