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.

249 lines
6.5 KiB

7 years ago
  1. /*
  2. OTA MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "ArduinoOTA.h"
  6. // -----------------------------------------------------------------------------
  7. // Arduino OTA
  8. // -----------------------------------------------------------------------------
  9. void _otaConfigure() {
  10. ArduinoOTA.setPort(OTA_PORT);
  11. ArduinoOTA.setHostname(getSetting("hostname").c_str());
  12. #if USE_PASSWORD
  13. ArduinoOTA.setPassword(getSetting("adminPass", ADMIN_PASS).c_str());
  14. #endif
  15. }
  16. void _otaLoop() {
  17. ArduinoOTA.handle();
  18. }
  19. // -----------------------------------------------------------------------------
  20. // Terminal OTA
  21. // -----------------------------------------------------------------------------
  22. #if TERMINAL_SUPPORT
  23. #include <ESPAsyncTCP.h>
  24. AsyncClient * _ota_client;
  25. char * _ota_host;
  26. char * _ota_url;
  27. unsigned int _ota_port = 80;
  28. unsigned long _ota_size = 0;
  29. const char OTA_REQUEST_TEMPLATE[] PROGMEM =
  30. "GET %s HTTP/1.1\r\n"
  31. "Host: %s\r\n"
  32. "User-Agent: ESPurna\r\n"
  33. "Connection: close\r\n"
  34. "Content-Type: application/x-www-form-urlencoded\r\n"
  35. "Content-Length: 0\r\n\r\n\r\n";
  36. void _otaFrom(const char * host, unsigned int port, const char * url) {
  37. if (_ota_host) free(_ota_host);
  38. if (_ota_url) free(_ota_url);
  39. _ota_host = strdup(host);
  40. _ota_url = strdup(url);
  41. _ota_port = port;
  42. _ota_size = 0;
  43. if (_ota_client == NULL) {
  44. _ota_client = new AsyncClient();
  45. }
  46. _ota_client->onDisconnect([](void *s, AsyncClient *c) {
  47. DEBUG_MSG_P(PSTR("\n"));
  48. if (Update.end(true)){
  49. DEBUG_MSG_P(PSTR("[OTA] Success: %u bytes\n"), _ota_size);
  50. deferredReset(100, CUSTOM_RESET_OTA);
  51. } else {
  52. #ifdef DEBUG_PORT
  53. Update.printError(DEBUG_PORT);
  54. #endif
  55. }
  56. DEBUG_MSG_P(PSTR("[OTA] Disconnected\n"));
  57. _ota_client->free();
  58. delete _ota_client;
  59. _ota_client = NULL;
  60. free(_ota_host);
  61. _ota_host = NULL;
  62. free(_ota_url);
  63. _ota_url = NULL;
  64. }, 0);
  65. _ota_client->onTimeout([](void *s, AsyncClient *c, uint32_t time) {
  66. _ota_client->close(true);
  67. }, 0);
  68. _ota_client->onData([](void * arg, AsyncClient * c, void * data, size_t len) {
  69. char * p = (char *) data;
  70. if (_ota_size == 0) {
  71. Update.runAsync(true);
  72. if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) {
  73. #ifdef DEBUG_PORT
  74. Update.printError(DEBUG_PORT);
  75. #endif
  76. }
  77. p = strstr((char *)data, "\r\n\r\n") + 4;
  78. len = len - (p - (char *) data);
  79. }
  80. if (!Update.hasError()) {
  81. if (Update.write((uint8_t *) p, len) != len) {
  82. #ifdef DEBUG_PORT
  83. Update.printError(DEBUG_PORT);
  84. #endif
  85. }
  86. }
  87. _ota_size += len;
  88. DEBUG_MSG_P(PSTR("[OTA] Progress: %u bytes\r"), _ota_size);
  89. }, NULL);
  90. _ota_client->onConnect([](void * arg, AsyncClient * client) {
  91. #if ASYNC_TCP_SSL_ENABLED
  92. if (443 == _ota_port) {
  93. uint8_t fp[20] = {0};
  94. sslFingerPrintArray(getSetting("otafp", OTA_GITHUB_FP).c_str(), fp);
  95. SSL * ssl = _ota_client->getSSL();
  96. if (ssl_match_fingerprint(ssl, fp) != SSL_OK) {
  97. DEBUG_MSG_P(PSTR("[OTA] Warning: certificate doesn't match\n"));
  98. }
  99. }
  100. #endif
  101. DEBUG_MSG_P(PSTR("[OTA] Downloading %s\n"), _ota_url);
  102. char buffer[strlen_P(OTA_REQUEST_TEMPLATE) + strlen(_ota_url) + strlen(_ota_host)];
  103. snprintf_P(buffer, sizeof(buffer), OTA_REQUEST_TEMPLATE, _ota_url, _ota_host);
  104. client->write(buffer);
  105. }, NULL);
  106. #if ASYNC_TCP_SSL_ENABLED
  107. bool connected = _ota_client->connect(host, port, 443 == port);
  108. #else
  109. bool connected = _ota_client->connect(host, port);
  110. #endif
  111. if (!connected) {
  112. DEBUG_MSG_P(PSTR("[OTA] Connection failed\n"));
  113. _ota_client->close(true);
  114. }
  115. }
  116. void _otaFrom(String url) {
  117. // Port from protocol
  118. unsigned int port = 80;
  119. if (url.startsWith("https://")) port = 443;
  120. url = url.substring(url.indexOf("/") + 2);
  121. // Get host
  122. String host = url.substring(0, url.indexOf("/"));
  123. // Explicit port
  124. int p = host.indexOf(":");
  125. if (p > 0) {
  126. port = host.substring(p + 1).toInt();
  127. host = host.substring(0, p);
  128. }
  129. // Get URL
  130. String uri = url.substring(url.indexOf("/"));
  131. _otaFrom(host.c_str(), port, uri.c_str());
  132. }
  133. void _otaInitCommands() {
  134. settingsRegisterCommand(F("OTA"), [](Embedis* e) {
  135. if (e->argc < 2) {
  136. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  137. } else {
  138. DEBUG_MSG_P(PSTR("+OK\n"));
  139. String url = String(e->argv[1]);
  140. _otaFrom(url);
  141. }
  142. });
  143. }
  144. #endif // TERMINAL_SUPPORT
  145. // -----------------------------------------------------------------------------
  146. void otaSetup() {
  147. _otaConfigure();
  148. #if WEB_SUPPORT
  149. wsOnAfterParseRegister(_otaConfigure);
  150. #endif
  151. #if TERMINAL_SUPPORT
  152. _otaInitCommands();
  153. #endif
  154. // Register loop
  155. espurnaRegisterLoop(_otaLoop);
  156. // -------------------------------------------------------------------------
  157. ArduinoOTA.onStart([]() {
  158. DEBUG_MSG_P(PSTR("[OTA] Start\n"));
  159. #if WEB_SUPPORT
  160. wsSend_P(PSTR("{\"message\": 2}"));
  161. #endif
  162. });
  163. ArduinoOTA.onEnd([]() {
  164. DEBUG_MSG_P(PSTR("\n"));
  165. DEBUG_MSG_P(PSTR("[OTA] Done, restarting...\n"));
  166. #if WEB_SUPPORT
  167. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  168. #endif
  169. deferredReset(100, CUSTOM_RESET_OTA);
  170. });
  171. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  172. DEBUG_MSG_P(PSTR("[OTA] Progress: %u%%\r"), (progress / (total / 100)));
  173. });
  174. ArduinoOTA.onError([](ota_error_t error) {
  175. #if DEBUG_SUPPORT
  176. DEBUG_MSG_P(PSTR("\n[OTA] Error #%u: "), error);
  177. if (error == OTA_AUTH_ERROR) DEBUG_MSG_P(PSTR("Auth Failed\n"));
  178. else if (error == OTA_BEGIN_ERROR) DEBUG_MSG_P(PSTR("Begin Failed\n"));
  179. else if (error == OTA_CONNECT_ERROR) DEBUG_MSG_P(PSTR("Connect Failed\n"));
  180. else if (error == OTA_RECEIVE_ERROR) DEBUG_MSG_P(PSTR("Receive Failed\n"));
  181. else if (error == OTA_END_ERROR) DEBUG_MSG_P(PSTR("End Failed\n"));
  182. #endif
  183. });
  184. ArduinoOTA.begin();
  185. }