Mirror of espurna firmware for wireless switches and more
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.

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