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.

293 lines
7.6 KiB

  1. /*
  2. TERMINAL MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if TERMINAL_SUPPORT
  6. #include <vector>
  7. #include "libs/EmbedisWrap.h"
  8. #include <Stream.h>
  9. #include "libs/StreamInjector.h"
  10. #include "libs/HeapStats.h"
  11. StreamInjector _serial = StreamInjector(TERMINAL_BUFFER_SIZE);
  12. EmbedisWrap embedis(_serial, TERMINAL_BUFFER_SIZE);
  13. #if SERIAL_RX_ENABLED
  14. char _serial_rx_buffer[TERMINAL_BUFFER_SIZE];
  15. static unsigned char _serial_rx_pointer = 0;
  16. #endif // SERIAL_RX_ENABLED
  17. // -----------------------------------------------------------------------------
  18. // Commands
  19. // -----------------------------------------------------------------------------
  20. void _terminalHelpCommand() {
  21. // Get sorted list of commands
  22. std::vector<String> commands;
  23. unsigned char size = embedis.getCommandCount();
  24. for (unsigned int i=0; i<size; i++) {
  25. String command = embedis.getCommandName(i);
  26. bool inserted = false;
  27. for (unsigned char j=0; j<commands.size(); j++) {
  28. // Check if we have to insert it before the current element
  29. if (commands[j].compareTo(command) > 0) {
  30. commands.insert(commands.begin() + j, command);
  31. inserted = true;
  32. break;
  33. }
  34. }
  35. // If we could not insert it, just push it at the end
  36. if (!inserted) commands.push_back(command);
  37. }
  38. // Output the list
  39. DEBUG_MSG_P(PSTR("Available commands:\n"));
  40. for (unsigned char i=0; i<commands.size(); i++) {
  41. DEBUG_MSG_P(PSTR("> %s\n"), (commands[i]).c_str());
  42. }
  43. }
  44. void _terminalKeysCommand() {
  45. // Get sorted list of keys
  46. std::vector<String> keys = _settingsKeys();
  47. // Write key-values
  48. DEBUG_MSG_P(PSTR("Current settings:\n"));
  49. for (unsigned int i=0; i<keys.size(); i++) {
  50. String value = getSetting(keys[i]);
  51. DEBUG_MSG_P(PSTR("> %s => \"%s\"\n"), (keys[i]).c_str(), value.c_str());
  52. }
  53. unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
  54. UNUSED(freeEEPROM);
  55. DEBUG_MSG_P(PSTR("Number of keys: %d\n"), keys.size());
  56. DEBUG_MSG_P(PSTR("Current EEPROM sector: %u\n"), EEPROMr.current());
  57. DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
  58. }
  59. void _terminalInitCommand() {
  60. #if DEBUG_SUPPORT
  61. terminalRegisterCommand(F("CRASH"), [](Embedis* e) {
  62. crashDump();
  63. crashClear();
  64. terminalOK();
  65. });
  66. #endif
  67. terminalRegisterCommand(F("COMMANDS"), [](Embedis* e) {
  68. _terminalHelpCommand();
  69. terminalOK();
  70. });
  71. terminalRegisterCommand(F("ERASE.CONFIG"), [](Embedis* e) {
  72. terminalOK();
  73. customResetReason(CUSTOM_RESET_TERMINAL);
  74. eraseSDKConfig();
  75. *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494
  76. });
  77. terminalRegisterCommand(F("FACTORY.RESET"), [](Embedis* e) {
  78. resetSettings();
  79. terminalOK();
  80. });
  81. terminalRegisterCommand(F("GPIO"), [](Embedis* e) {
  82. int pin = -1;
  83. if (e->argc < 2) {
  84. DEBUG_MSG("Printing all GPIO pins:\n");
  85. } else {
  86. pin = String(e->argv[1]).toInt();
  87. if (!gpioValid(pin)) {
  88. terminalError(F("Invalid GPIO pin"));
  89. return;
  90. }
  91. if (e->argc > 2) {
  92. bool state = String(e->argv[2]).toInt() == 1;
  93. digitalWrite(pin, state);
  94. }
  95. }
  96. for (int i = 0; i <= 15; i++) {
  97. if (gpioValid(i) && (pin == -1 || pin == i)) {
  98. DEBUG_MSG_P(PSTR("GPIO %s pin %d is %s\n"), GPEP(i) ? "output" : "input", i, digitalRead(i) == HIGH ? "HIGH" : "LOW");
  99. }
  100. }
  101. terminalOK();
  102. });
  103. terminalRegisterCommand(F("HEAP"), [](Embedis* e) {
  104. infoHeapStats();
  105. terminalOK();
  106. });
  107. terminalRegisterCommand(F("STACK"), [](Embedis* e) {
  108. infoMemory("Stack", CONT_STACKSIZE, getFreeStack());
  109. terminalOK();
  110. });
  111. terminalRegisterCommand(F("HELP"), [](Embedis* e) {
  112. _terminalHelpCommand();
  113. terminalOK();
  114. });
  115. terminalRegisterCommand(F("INFO"), [](Embedis* e) {
  116. info();
  117. terminalOK();
  118. });
  119. terminalRegisterCommand(F("KEYS"), [](Embedis* e) {
  120. _terminalKeysCommand();
  121. terminalOK();
  122. });
  123. terminalRegisterCommand(F("GET"), [](Embedis* e) {
  124. if (e->argc < 2) {
  125. terminalError(F("Wrong arguments"));
  126. return;
  127. }
  128. for (unsigned char i = 1; i < e->argc; i++) {
  129. String key = String(e->argv[i]);
  130. String value;
  131. if (!Embedis::get(key, value)) {
  132. DEBUG_MSG_P(PSTR("> %s =>\n"), key.c_str());
  133. continue;
  134. }
  135. DEBUG_MSG_P(PSTR("> %s => \"%s\"\n"), key.c_str(), value.c_str());
  136. }
  137. terminalOK();
  138. });
  139. terminalRegisterCommand(F("RELOAD"), [](Embedis* e) {
  140. espurnaReload();
  141. terminalOK();
  142. });
  143. terminalRegisterCommand(F("RESET"), [](Embedis* e) {
  144. terminalOK();
  145. deferredReset(100, CUSTOM_RESET_TERMINAL);
  146. });
  147. terminalRegisterCommand(F("RESET.SAFE"), [](Embedis* e) {
  148. systemStabilityCounter(SYSTEM_CHECK_MAX);
  149. terminalOK();
  150. deferredReset(100, CUSTOM_RESET_TERMINAL);
  151. });
  152. terminalRegisterCommand(F("UPTIME"), [](Embedis* e) {
  153. DEBUG_MSG_P(PSTR("Uptime: %d seconds\n"), getUptime());
  154. terminalOK();
  155. });
  156. terminalRegisterCommand(F("CONFIG"), [](Embedis* e) {
  157. DynamicJsonBuffer jsonBuffer;
  158. JsonObject& root = jsonBuffer.createObject();
  159. settingsGetJson(root);
  160. String output;
  161. root.printTo(output);
  162. DEBUG_MSG(output.c_str());
  163. });
  164. #if not SETTINGS_AUTOSAVE
  165. terminalRegisterCommand(F("SAVE"), [](Embedis* e) {
  166. eepromCommit();
  167. DEBUG_MSG_P(PSTR("\n+OK\n"));
  168. });
  169. #endif
  170. }
  171. void _terminalLoop() {
  172. #if DEBUG_SERIAL_SUPPORT
  173. while (DEBUG_PORT.available()) {
  174. _serial.inject(DEBUG_PORT.read());
  175. }
  176. #endif
  177. embedis.process();
  178. #if SERIAL_RX_ENABLED
  179. while (SERIAL_RX_PORT.available() > 0) {
  180. char rc = SERIAL_RX_PORT.read();
  181. _serial_rx_buffer[_serial_rx_pointer++] = rc;
  182. if ((_serial_rx_pointer == TERMINAL_BUFFER_SIZE) || (rc == 10)) {
  183. terminalInject(_serial_rx_buffer, (size_t) _serial_rx_pointer);
  184. _serial_rx_pointer = 0;
  185. }
  186. }
  187. #endif // SERIAL_RX_ENABLED
  188. }
  189. // -----------------------------------------------------------------------------
  190. // Pubic API
  191. // -----------------------------------------------------------------------------
  192. void terminalInject(void *data, size_t len) {
  193. _serial.inject((char *) data, len);
  194. }
  195. Stream & terminalSerial() {
  196. return (Stream &) _serial;
  197. }
  198. void terminalRegisterCommand(const String& name, void (*call)(Embedis*)) {
  199. Embedis::command(name, call);
  200. };
  201. void terminalOK() {
  202. DEBUG_MSG_P(PSTR("+OK\n"));
  203. }
  204. void terminalError(const String& error) {
  205. DEBUG_MSG_P(PSTR("-ERROR: %s\n"), error.c_str());
  206. }
  207. void terminalSetup() {
  208. _serial.callback([](uint8_t ch) {
  209. #if TELNET_SUPPORT
  210. telnetWrite(ch);
  211. #endif
  212. #if DEBUG_SERIAL_SUPPORT
  213. DEBUG_PORT.write(ch);
  214. #endif
  215. });
  216. _terminalInitCommand();
  217. #if SERIAL_RX_ENABLED
  218. SERIAL_RX_PORT.begin(SERIAL_RX_BAUDRATE);
  219. #endif // SERIAL_RX_ENABLED
  220. // Register loop
  221. espurnaRegisterLoop(_terminalLoop);
  222. }
  223. #endif // TERMINAL_SUPPORT