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.

110 lines
2.7 KiB

  1. /*
  2. ARDUINO OTA MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if OTA_ARDUINOOTA_SUPPORT
  6. #include "ota.h"
  7. #include "system.h"
  8. #include "ws.h"
  9. // TODO: allocate ArduinoOTAClass on-demand, stop using global instance
  10. void _arduinoOtaConfigure() {
  11. ArduinoOTA.setPort(OTA_PORT);
  12. ArduinoOTA.setHostname(getSetting("hostname").c_str());
  13. #if USE_PASSWORD
  14. ArduinoOTA.setPassword(getAdminPass().c_str());
  15. #endif
  16. ArduinoOTA.begin();
  17. }
  18. void _arduinoOtaLoop() {
  19. ArduinoOTA.handle();
  20. }
  21. void _arduinoOtaOnStart() {
  22. // Disabling EEPROM rotation to prevent writing to EEPROM after the upgrade
  23. eepromRotate(false);
  24. // Because ArduinoOTA is synchronous, force backup right now instead of waiting for the next loop()
  25. eepromBackup(0);
  26. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  27. #if WEB_SUPPORT
  28. wsSend_P(PSTR("{\"message\": 2}"));
  29. #endif
  30. }
  31. void _arduinoOtaOnEnd() {
  32. DEBUG_MSG_P(PSTR("\n"));
  33. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  34. #if WEB_SUPPORT
  35. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  36. #endif
  37. // Note: ArduinoOTA will reset the board after this callback returns.
  38. customResetReason(CUSTOM_RESET_OTA);
  39. nice_delay(100);
  40. }
  41. void _arduinoOtaOnProgress(unsigned int progress, unsigned int total) {
  42. // Removed to avoid websocket ping back during upgrade (see #1574)
  43. // TODO: implement as separate from debugging message
  44. #if WEB_SUPPORT
  45. if (wsConnected()) return;
  46. #endif
  47. #if DEBUG_SUPPORT
  48. static unsigned int _progOld;
  49. unsigned int _prog = (progress / (total / 100));
  50. if (_prog != _progOld) {
  51. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), _prog);
  52. _progOld = _prog;
  53. }
  54. #endif
  55. }
  56. void _arduinoOtaOnError(ota_error_t error) {
  57. #if DEBUG_SUPPORT
  58. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  59. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  60. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  61. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  62. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  63. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  64. #endif
  65. eepromRotate(true);
  66. }
  67. void arduinoOtaSetup() {
  68. espurnaRegisterLoop(_arduinoOtaLoop);
  69. espurnaRegisterReload(_arduinoOtaConfigure);
  70. ArduinoOTA.onStart(_arduinoOtaOnStart);
  71. ArduinoOTA.onEnd(_arduinoOtaOnEnd);
  72. ArduinoOTA.onError(_arduinoOtaOnError);
  73. ArduinoOTA.onProgress(_arduinoOtaOnProgress);
  74. _arduinoOtaConfigure();
  75. }
  76. #endif // OTA_ARDUINOOTA_SUPPORT