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.

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