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.

230 lines
5.9 KiB

  1. /*
  2. ASYNC CLIENT OTA MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if OTA_CLIENT == OTA_CLIENT_ASYNCTCP
  6. // -----------------------------------------------------------------------------
  7. // Terminal OTA command
  8. // -----------------------------------------------------------------------------
  9. #if TERMINAL_SUPPORT || OTA_MQTT_SUPPORT
  10. #include <ESPAsyncTCP.h>
  11. #include "libs/URL.h"
  12. std::unique_ptr<AsyncClient> _ota_client = nullptr;
  13. unsigned long _ota_size = 0;
  14. bool _ota_connected = false;
  15. std::unique_ptr<URL> _ota_url = nullptr;
  16. const char OTA_REQUEST_TEMPLATE[] PROGMEM =
  17. "GET %s HTTP/1.1\r\n"
  18. "Host: %s\r\n"
  19. "User-Agent: ESPurna\r\n"
  20. "Connection: close\r\n"
  21. "Content-Type: application/x-www-form-urlencoded\r\n"
  22. "Content-Length: 0\r\n\r\n\r\n";
  23. void _otaClientOnDisconnect(void *s, AsyncClient *c) {
  24. DEBUG_MSG_P(PSTR("\n"));
  25. if (Update.end(true)){
  26. DEBUG_MSG_P(PSTR("[OTA] Success: %u bytes\n"), _ota_size);
  27. deferredReset(100, CUSTOM_RESET_OTA);
  28. } else {
  29. #ifdef DEBUG_PORT
  30. Update.printError(DEBUG_PORT);
  31. #endif
  32. eepromRotate(true);
  33. }
  34. DEBUG_MSG_P(PSTR("[OTA] Disconnected\n"));
  35. _ota_connected = false;
  36. _ota_url = nullptr;
  37. _ota_client = nullptr;
  38. }
  39. void _otaClientOnTimeout(void *s, AsyncClient *c, uint32_t time) {
  40. _ota_connected = false;
  41. _ota_url = nullptr;
  42. _ota_client->close(true);
  43. }
  44. void _otaClientOnData(void * arg, AsyncClient * c, void * data, size_t len) {
  45. char * p = (char *) data;
  46. if (_ota_size == 0) {
  47. Update.runAsync(true);
  48. if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) {
  49. #ifdef DEBUG_PORT
  50. Update.printError(DEBUG_PORT);
  51. #endif
  52. c->close(true);
  53. return;
  54. }
  55. p = strstr((char *)data, "\r\n\r\n") + 4;
  56. len = len - (p - (char *) data);
  57. }
  58. if (!Update.hasError()) {
  59. if (Update.write((uint8_t *) p, len) != len) {
  60. #ifdef DEBUG_PORT
  61. Update.printError(DEBUG_PORT);
  62. #endif
  63. c->close(true);
  64. return;
  65. }
  66. }
  67. _ota_size += len;
  68. DEBUG_MSG_P(PSTR("[OTA] Progress: %u bytes\r"), _ota_size);
  69. delay(0);
  70. }
  71. void _otaClientOnConnect(void *arg, AsyncClient *client) {
  72. #if ASYNC_TCP_SSL_ENABLED
  73. int check = getSetting("otaScCheck", OTA_SECURE_CLIENT_CHECK).toInt();
  74. if ((check == SECURE_CLIENT_CHECK_FINGERPRINT) && (443 == _ota_url->port)) {
  75. uint8_t fp[20] = {0};
  76. sslFingerPrintArray(getSetting("otaFP", OTA_FINGERPRINT).c_str(), fp);
  77. SSL * ssl = _ota_client->getSSL();
  78. if (ssl_match_fingerprint(ssl, fp) != SSL_OK) {
  79. DEBUG_MSG_P(PSTR("[OTA] Warning: certificate fingerpint doesn't match\n"));
  80. client->close(true);
  81. return;
  82. }
  83. }
  84. #endif
  85. // Disabling EEPROM rotation to prevent writing to EEPROM after the upgrade
  86. eepromRotate(false);
  87. DEBUG_MSG_P(PSTR("[OTA] Downloading %s\n"), _ota_url->path.c_str());
  88. char buffer[strlen_P(OTA_REQUEST_TEMPLATE) + _ota_url->path.length() + _ota_url->host.length()];
  89. snprintf_P(buffer, sizeof(buffer), OTA_REQUEST_TEMPLATE, _ota_url->path.c_str(), _ota_url->host.c_str());
  90. client->write(buffer);
  91. }
  92. void _otaClientFrom(const String& url) {
  93. if (_ota_connected) {
  94. DEBUG_MSG_P(PSTR("[OTA] Already connected\n"));
  95. return;
  96. }
  97. _ota_size = 0;
  98. if (_ota_url) _ota_url = nullptr;
  99. _ota_url = std::make_unique<URL>(url);
  100. /*
  101. DEBUG_MSG_P(PSTR("[OTA] proto:%s host:%s port:%u path:%s\n"),
  102. _ota_url->protocol.c_str(),
  103. _ota_url->host.c_str(),
  104. _ota_url->port,
  105. _ota_url->path.c_str()
  106. );
  107. */
  108. // we only support HTTP
  109. if ((!_ota_url->protocol.equals("http")) && (!_ota_url->protocol.equals("https"))) {
  110. DEBUG_MSG_P(PSTR("[OTA] Incorrect URL specified\n"));
  111. _ota_url = nullptr;
  112. return;
  113. }
  114. if (!_ota_client) {
  115. _ota_client = std::make_unique<AsyncClient>();
  116. }
  117. _ota_client->onDisconnect(_otaClientOnDisconnect, nullptr);
  118. _ota_client->onTimeout(_otaClientOnTimeout, nullptr);
  119. _ota_client->onData(_otaClientOnData, nullptr);
  120. _ota_client->onConnect(_otaClientOnConnect, nullptr);
  121. #if ASYNC_TCP_SSL_ENABLED
  122. _ota_connected = _ota_client->connect(_ota_url->host.c_str(), _ota_url->port, 443 == _ota_url->port);
  123. #else
  124. _ota_connected = _ota_client->connect(_ota_url->host.c_str(), _ota_url->port);
  125. #endif
  126. if (!_ota_connected) {
  127. DEBUG_MSG_P(PSTR("[OTA] Connection failed\n"));
  128. _ota_url = nullptr;
  129. _ota_client->close(true);
  130. }
  131. }
  132. #endif // TERMINAL_SUPPORT || OTA_MQTT_SUPPORT
  133. #if TERMINAL_SUPPORT
  134. void _otaClientInitCommands() {
  135. terminalRegisterCommand(F("OTA"), [](Embedis* e) {
  136. if (e->argc < 2) {
  137. terminalError(F("OTA <url>"));
  138. } else {
  139. _otaClientFrom(String(e->argv[1]));
  140. terminalOK();
  141. }
  142. });
  143. }
  144. #endif // TERMINAL_SUPPORT
  145. #if OTA_MQTT_SUPPORT
  146. void _otaClientMqttCallback(unsigned int type, const char * topic, const char * payload) {
  147. if (type == MQTT_CONNECT_EVENT) {
  148. mqttSubscribe(MQTT_TOPIC_OTA);
  149. }
  150. if (type == MQTT_MESSAGE_EVENT) {
  151. String t = mqttMagnitude((char *) topic);
  152. if (t.equals(MQTT_TOPIC_OTA)) {
  153. DEBUG_MSG_P(PSTR("[OTA] Initiating from URL: %s\n"), payload);
  154. _otaClientFrom(payload);
  155. }
  156. }
  157. }
  158. #endif // OTA_MQTT_SUPPORT
  159. // -----------------------------------------------------------------------------
  160. void otaClientSetup() {
  161. // Backwards compatibility
  162. moveSetting("otafp", "otaFP");
  163. #if TERMINAL_SUPPORT
  164. _otaClientInitCommands();
  165. #endif
  166. #if (MQTT_SUPPORT && OTA_MQTT_SUPPORT)
  167. mqttRegister(_otaClientMqttCallback);
  168. #endif
  169. }
  170. #endif // OTA_CLIENT == OTA_CLIENT_ASYNCTCP