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.

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