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.

107 lines
2.4 KiB

Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
  1. /*
  2. RTMEM MODULE
  3. Copyright (C) 2019 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. */
  5. #include "rtcmem.h"
  6. volatile RtcmemData* Rtcmem = reinterpret_cast<volatile RtcmemData*>(RTCMEM_ADDR);
  7. bool _rtcmem_status = false;
  8. void _rtcmemErase() {
  9. auto ptr = reinterpret_cast<volatile uint32_t*>(RTCMEM_ADDR);
  10. const auto end = ptr + RTCMEM_BLOCKS;
  11. DEBUG_MSG_P(PSTR("[RTCMEM] Erasing start=%p end=%p\n"), ptr, end);
  12. do {
  13. *ptr = 0;
  14. } while (++ptr != end);
  15. }
  16. void _rtcmemInit() {
  17. _rtcmemErase();
  18. Rtcmem->magic = RTCMEM_MAGIC;
  19. }
  20. // Treat memory as dirty on cold boot, hardware wdt reset and rst pin
  21. bool _rtcmemStatus() {
  22. bool readable;
  23. switch (systemResetReason()) {
  24. case REASON_EXT_SYS_RST:
  25. case REASON_WDT_RST:
  26. case REASON_DEFAULT_RST:
  27. readable = false;
  28. break;
  29. default:
  30. readable = true;
  31. }
  32. readable = readable and (RTCMEM_MAGIC == Rtcmem->magic);
  33. return readable;
  34. }
  35. #if TERMINAL_SUPPORT
  36. void _rtcmemInitCommands() {
  37. terminalRegisterCommand(F("RTCMEM.REINIT"), [](const terminal::CommandContext&) {
  38. _rtcmemInit();
  39. });
  40. #if DEBUG_SUPPORT
  41. terminalRegisterCommand(F("RTCMEM.DUMP"), [](const terminal::CommandContext&) {
  42. DEBUG_MSG_P(PSTR("[RTCMEM] boot_status=%u status=%u blocks_used=%u\n"),
  43. _rtcmem_status, _rtcmemStatus(), RtcmemSize);
  44. String line;
  45. line.reserve(96);
  46. char buffer[16] = {0};
  47. auto addr = reinterpret_cast<volatile uint32_t*>(RTCMEM_ADDR);
  48. uint8_t block = 1;
  49. uint8_t offset = 0;
  50. uint8_t start = 0;
  51. do {
  52. offset = block - 1;
  53. snprintf(buffer, sizeof(buffer), "%08x ", *(addr + offset));
  54. line += buffer;
  55. if ((block % 8) == 0) {
  56. DEBUG_MSG_P(PSTR("%02u %p: %s\n"), start, addr+start, line.c_str());
  57. start = block;
  58. line = "";
  59. }
  60. ++block;
  61. } while (block<(RTCMEM_BLOCKS+1));
  62. });
  63. #endif
  64. }
  65. #endif
  66. bool rtcmemStatus() {
  67. return _rtcmem_status;
  68. }
  69. void rtcmemSetup() {
  70. _rtcmem_status = _rtcmemStatus();
  71. if (!_rtcmem_status) {
  72. _rtcmemInit();
  73. }
  74. #if TERMINAL_SUPPORT
  75. _rtcmemInitCommands();
  76. #endif
  77. }