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.

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