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

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