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.

76 lines
1.9 KiB

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