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.

79 lines
2.0 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
  1. /*
  2. OTA MODULE COMMON FUNCTIONS
  3. */
  4. #include "ota.h"
  5. #include "system.h"
  6. #include "terminal.h"
  7. #include "ws.h"
  8. void otaPrintError() {
  9. if (Update.hasError()) {
  10. #if TERMINAL_SUPPORT
  11. Update.printError(terminalDefaultStream());
  12. #elif DEBUG_SERIAL_SUPPORT && defined(DEBUG_PORT)
  13. Update.printError(DEBUG_PORT);
  14. #endif
  15. }
  16. }
  17. bool otaFinalize(size_t size, int reason, bool evenIfRemaining) {
  18. if (Update.isRunning() && Update.end(evenIfRemaining)) {
  19. DEBUG_MSG_P(PSTR("[OTA] Success: %7u bytes\n"), size);
  20. deferredReset(500, reason);
  21. return true;
  22. }
  23. otaPrintError();
  24. eepromRotate(true);
  25. return false;
  26. }
  27. // Helper methods from UpdaterClass that need to be called manually for async mode,
  28. // because we are not using Stream interface to feed it data.
  29. bool otaVerifyHeader(uint8_t* data, size_t len) {
  30. if (len < 4) {
  31. return false;
  32. }
  33. // ref: https://github.com/esp8266/Arduino/pull/6820
  34. // accept gzip, let unpacker figure things out later
  35. if (data[0] == 0x1F && data[1] == 0x8B) {
  36. return true;
  37. }
  38. // Check for magic byte with a normal .bin
  39. if (data[0] != 0xE9) {
  40. return false;
  41. }
  42. // Make sure that flash config can be recognized and fit the flash
  43. const auto flash_config = ESP.magicFlashChipSize((data[3] & 0xf0) >> 4);
  44. if (flash_config && (flash_config > ESP.getFlashChipRealSize())) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. void otaProgress(size_t bytes, size_t each) {
  50. // Removed to avoid websocket ping back during upgrade (see #1574)
  51. // TODO: implement as separate from debugging message
  52. #if WEB_SUPPORT
  53. if (wsConnected()) return;
  54. #endif
  55. // Telnet and serial will still output things, but slightly throttled
  56. static size_t last = 0;
  57. if (bytes < last) {
  58. last = 0;
  59. }
  60. if ((bytes > each) && (bytes - each > last)) {
  61. DEBUG_MSG_P(PSTR("[OTA] Progress: %7u bytes\r"), bytes);
  62. last = bytes;
  63. }
  64. }