esp_https_ota.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <esp_https_ota.h>
  10. #include <esp_log.h>
  11. #include <esp_ota_ops.h>
  12. #include <errno.h>
  13. #include <sys/param.h>
  14. #define IMAGE_HEADER_SIZE (1024)
  15. /* This is kept sufficiently large enough to cover image format headers
  16. * and also this defines default minimum OTA buffer chunk size */
  17. #define DEFAULT_OTA_BUF_SIZE (IMAGE_HEADER_SIZE)
  18. _Static_assert(DEFAULT_OTA_BUF_SIZE > (sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1), "OTA data buffer too small");
  19. #define DEFAULT_REQUEST_SIZE (64 * 1024)
  20. static const char *TAG = "esp_https_ota";
  21. typedef enum {
  22. ESP_HTTPS_OTA_INIT,
  23. ESP_HTTPS_OTA_BEGIN,
  24. ESP_HTTPS_OTA_IN_PROGRESS,
  25. ESP_HTTPS_OTA_SUCCESS,
  26. } esp_https_ota_state;
  27. struct esp_https_ota_handle {
  28. esp_ota_handle_t update_handle;
  29. const esp_partition_t *update_partition;
  30. esp_http_client_handle_t http_client;
  31. char *ota_upgrade_buf;
  32. size_t ota_upgrade_buf_size;
  33. int binary_file_len;
  34. int image_length;
  35. int max_http_request_size;
  36. esp_https_ota_state state;
  37. bool bulk_flash_erase;
  38. bool partial_http_download;
  39. };
  40. typedef struct esp_https_ota_handle esp_https_ota_t;
  41. static bool process_again(int status_code)
  42. {
  43. switch (status_code) {
  44. case HttpStatus_MovedPermanently:
  45. case HttpStatus_Found:
  46. case HttpStatus_TemporaryRedirect:
  47. case HttpStatus_Unauthorized:
  48. return true;
  49. default:
  50. return false;
  51. }
  52. return false;
  53. }
  54. static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code)
  55. {
  56. esp_err_t err;
  57. if (status_code == HttpStatus_MovedPermanently || status_code == HttpStatus_Found || status_code == HttpStatus_TemporaryRedirect) {
  58. err = esp_http_client_set_redirection(http_client);
  59. if (err != ESP_OK) {
  60. ESP_LOGE(TAG, "URL redirection Failed");
  61. return err;
  62. }
  63. } else if (status_code == HttpStatus_Unauthorized) {
  64. esp_http_client_add_auth(http_client);
  65. } else if(status_code == HttpStatus_NotFound || status_code == HttpStatus_Forbidden) {
  66. ESP_LOGE(TAG, "File not found(%d)", status_code);
  67. return ESP_FAIL;
  68. } else if (status_code >= HttpStatus_BadRequest && status_code < HttpStatus_InternalError) {
  69. ESP_LOGE(TAG, "Client error (%d)", status_code);
  70. return ESP_FAIL;
  71. } else if (status_code >= HttpStatus_InternalError) {
  72. ESP_LOGE(TAG, "Server error (%d)", status_code);
  73. return ESP_FAIL;
  74. }
  75. char upgrade_data_buf[256];
  76. // process_again() returns true only in case of redirection.
  77. if (process_again(status_code)) {
  78. while (1) {
  79. /*
  80. * In case of redirection, esp_http_client_read() is called
  81. * to clear the response buffer of http_client.
  82. */
  83. int data_read = esp_http_client_read(http_client, upgrade_data_buf, sizeof(upgrade_data_buf));
  84. if (data_read <= 0) {
  85. return ESP_OK;
  86. }
  87. }
  88. }
  89. return ESP_OK;
  90. }
  91. static esp_err_t _http_connect(esp_http_client_handle_t http_client)
  92. {
  93. esp_err_t err = ESP_FAIL;
  94. int status_code, header_ret;
  95. do {
  96. char *post_data = NULL;
  97. /* Send POST request if body is set.
  98. * Note: Sending POST request is not supported if partial_http_download
  99. * is enabled
  100. */
  101. int post_len = esp_http_client_get_post_field(http_client, &post_data);
  102. err = esp_http_client_open(http_client, post_len);
  103. if (err != ESP_OK) {
  104. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  105. return err;
  106. }
  107. if (post_len) {
  108. int write_len = 0;
  109. while (post_len > 0) {
  110. write_len = esp_http_client_write(http_client, post_data, post_len);
  111. if (write_len < 0) {
  112. ESP_LOGE(TAG, "Write failed");
  113. return ESP_FAIL;
  114. }
  115. post_len -= write_len;
  116. post_data += write_len;
  117. }
  118. }
  119. header_ret = esp_http_client_fetch_headers(http_client);
  120. if (header_ret < 0) {
  121. return header_ret;
  122. }
  123. status_code = esp_http_client_get_status_code(http_client);
  124. err = _http_handle_response_code(http_client, status_code);
  125. if (err != ESP_OK) {
  126. return err;
  127. }
  128. } while (process_again(status_code));
  129. return err;
  130. }
  131. static void _http_cleanup(esp_http_client_handle_t client)
  132. {
  133. esp_http_client_close(client);
  134. esp_http_client_cleanup(client);
  135. }
  136. static esp_err_t _ota_write(esp_https_ota_t *https_ota_handle, const void *buffer, size_t buf_len)
  137. {
  138. if (buffer == NULL || https_ota_handle == NULL) {
  139. return ESP_FAIL;
  140. }
  141. esp_err_t err = esp_ota_write(https_ota_handle->update_handle, buffer, buf_len);
  142. if (err != ESP_OK) {
  143. ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%x", err);
  144. } else {
  145. https_ota_handle->binary_file_len += buf_len;
  146. ESP_LOGD(TAG, "Written image length %d", https_ota_handle->binary_file_len);
  147. err = ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  148. }
  149. return err;
  150. }
  151. static bool is_server_verification_enabled(esp_https_ota_config_t *ota_config) {
  152. return (ota_config->http_config->cert_pem
  153. || ota_config->http_config->use_global_ca_store
  154. || ota_config->http_config->crt_bundle_attach != NULL);
  155. }
  156. esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle)
  157. {
  158. esp_err_t err;
  159. if (handle == NULL || ota_config == NULL || ota_config->http_config == NULL) {
  160. ESP_LOGE(TAG, "esp_https_ota_begin: Invalid argument");
  161. if (handle) {
  162. *handle = NULL;
  163. }
  164. return ESP_ERR_INVALID_ARG;
  165. }
  166. if (!is_server_verification_enabled(ota_config)) {
  167. #if CONFIG_OTA_ALLOW_HTTP
  168. ESP_LOGW(TAG, "Continuing with insecure option because CONFIG_OTA_ALLOW_HTTP is set.");
  169. #else
  170. ESP_LOGE(TAG, "No option for server verification is enabled in esp_http_client config.");
  171. *handle = NULL;
  172. return ESP_ERR_INVALID_ARG;
  173. #endif
  174. }
  175. esp_https_ota_t *https_ota_handle = calloc(1, sizeof(esp_https_ota_t));
  176. if (!https_ota_handle) {
  177. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  178. *handle = NULL;
  179. return ESP_ERR_NO_MEM;
  180. }
  181. https_ota_handle->partial_http_download = ota_config->partial_http_download;
  182. https_ota_handle->max_http_request_size = (ota_config->max_http_request_size == 0) ? DEFAULT_REQUEST_SIZE : ota_config->max_http_request_size;
  183. /* Initiate HTTP Connection */
  184. https_ota_handle->http_client = esp_http_client_init(ota_config->http_config);
  185. if (https_ota_handle->http_client == NULL) {
  186. ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  187. err = ESP_FAIL;
  188. goto failure;
  189. }
  190. if (https_ota_handle->partial_http_download) {
  191. esp_http_client_set_method(https_ota_handle->http_client, HTTP_METHOD_HEAD);
  192. err = esp_http_client_perform(https_ota_handle->http_client);
  193. if (err == ESP_OK) {
  194. int status = esp_http_client_get_status_code(https_ota_handle->http_client);
  195. if (status != HttpStatus_Ok) {
  196. ESP_LOGE(TAG, "Received incorrect http status %d", status);
  197. err = ESP_FAIL;
  198. goto http_cleanup;
  199. }
  200. } else {
  201. ESP_LOGE(TAG, "ESP HTTP client perform failed: %d", err);
  202. goto http_cleanup;
  203. }
  204. https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client);
  205. esp_http_client_close(https_ota_handle->http_client);
  206. if (https_ota_handle->image_length > https_ota_handle->max_http_request_size) {
  207. char *header_val = NULL;
  208. asprintf(&header_val, "bytes=0-%d", https_ota_handle->max_http_request_size - 1);
  209. if (header_val == NULL) {
  210. ESP_LOGE(TAG, "Failed to allocate memory for HTTP header");
  211. err = ESP_ERR_NO_MEM;
  212. goto http_cleanup;
  213. }
  214. esp_http_client_set_header(https_ota_handle->http_client, "Range", header_val);
  215. free(header_val);
  216. }
  217. esp_http_client_set_method(https_ota_handle->http_client, HTTP_METHOD_GET);
  218. }
  219. if (ota_config->http_client_init_cb) {
  220. err = ota_config->http_client_init_cb(https_ota_handle->http_client);
  221. if (err != ESP_OK) {
  222. ESP_LOGE(TAG, "http_client_init_cb returned 0x%x", err);
  223. goto http_cleanup;
  224. }
  225. }
  226. err = _http_connect(https_ota_handle->http_client);
  227. if (err != ESP_OK) {
  228. ESP_LOGE(TAG, "Failed to establish HTTP connection");
  229. goto http_cleanup;
  230. }
  231. if (!https_ota_handle->partial_http_download) {
  232. https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client);
  233. }
  234. https_ota_handle->update_partition = NULL;
  235. ESP_LOGI(TAG, "Starting OTA...");
  236. https_ota_handle->update_partition = esp_ota_get_next_update_partition(NULL);
  237. if (https_ota_handle->update_partition == NULL) {
  238. ESP_LOGE(TAG, "Passive OTA partition not found");
  239. err = ESP_FAIL;
  240. goto http_cleanup;
  241. }
  242. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
  243. https_ota_handle->update_partition->subtype, https_ota_handle->update_partition->address);
  244. const int alloc_size = MAX(ota_config->http_config->buffer_size, DEFAULT_OTA_BUF_SIZE);
  245. https_ota_handle->ota_upgrade_buf = (char *)malloc(alloc_size);
  246. if (!https_ota_handle->ota_upgrade_buf) {
  247. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  248. err = ESP_ERR_NO_MEM;
  249. goto http_cleanup;
  250. }
  251. https_ota_handle->ota_upgrade_buf_size = alloc_size;
  252. https_ota_handle->bulk_flash_erase = ota_config->bulk_flash_erase;
  253. https_ota_handle->binary_file_len = 0;
  254. *handle = (esp_https_ota_handle_t)https_ota_handle;
  255. https_ota_handle->state = ESP_HTTPS_OTA_BEGIN;
  256. return ESP_OK;
  257. http_cleanup:
  258. _http_cleanup(https_ota_handle->http_client);
  259. failure:
  260. free(https_ota_handle);
  261. *handle = NULL;
  262. return err;
  263. }
  264. static esp_err_t read_header(esp_https_ota_t *handle)
  265. {
  266. /*
  267. * `data_read_size` holds number of bytes needed to read complete header.
  268. * `bytes_read` holds number of bytes read.
  269. */
  270. int data_read_size = IMAGE_HEADER_SIZE;
  271. int data_read = 0, bytes_read = 0;
  272. /*
  273. * while loop is added to download complete image headers, even if the headers
  274. * are not sent in a single packet.
  275. */
  276. while (data_read_size > 0 && !esp_http_client_is_complete_data_received(handle->http_client)) {
  277. data_read = esp_http_client_read(handle->http_client,
  278. (handle->ota_upgrade_buf + bytes_read),
  279. data_read_size);
  280. /*
  281. * As esp_http_client_read doesn't return negative error code if select fails, we rely on
  282. * `errno` to check for underlying transport connectivity closure if any
  283. */
  284. if (errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED || data_read < 0) {
  285. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  286. break;
  287. }
  288. data_read_size -= data_read;
  289. bytes_read += data_read;
  290. }
  291. if (data_read_size > 0) {
  292. ESP_LOGE(TAG, "Complete headers were not received");
  293. return ESP_FAIL;
  294. }
  295. handle->binary_file_len = bytes_read;
  296. return ESP_OK;
  297. }
  298. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
  299. {
  300. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  301. if (handle == NULL || new_app_info == NULL) {
  302. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
  303. return ESP_ERR_INVALID_ARG;
  304. }
  305. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  306. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
  307. return ESP_FAIL;
  308. }
  309. if (read_header(handle) != ESP_OK) {
  310. return ESP_FAIL;
  311. }
  312. memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
  313. return ESP_OK;
  314. }
  315. static esp_err_t esp_ota_verify_chip_id(void *arg)
  316. {
  317. esp_image_header_t *data = (esp_image_header_t*)(arg);
  318. if (data->chip_id != CONFIG_IDF_FIRMWARE_CHIP_ID) {
  319. ESP_LOGE(TAG, "Mismatch chip id, expected %d, found %d", CONFIG_IDF_FIRMWARE_CHIP_ID, data->chip_id);
  320. return ESP_ERR_INVALID_VERSION;
  321. }
  322. return ESP_OK;
  323. }
  324. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
  325. {
  326. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  327. if (handle == NULL) {
  328. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid argument");
  329. return ESP_ERR_INVALID_ARG;
  330. }
  331. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  332. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid state");
  333. return ESP_FAIL;
  334. }
  335. esp_err_t err;
  336. int data_read;
  337. const int erase_size = handle->bulk_flash_erase ? OTA_SIZE_UNKNOWN : OTA_WITH_SEQUENTIAL_WRITES;
  338. switch (handle->state) {
  339. case ESP_HTTPS_OTA_BEGIN:
  340. err = esp_ota_begin(handle->update_partition, erase_size, &handle->update_handle);
  341. if (err != ESP_OK) {
  342. ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
  343. return err;
  344. }
  345. handle->state = ESP_HTTPS_OTA_IN_PROGRESS;
  346. /* In case `esp_https_ota_read_img_desc` was invoked first,
  347. then the image data read there should be written to OTA partition
  348. */
  349. int binary_file_len = 0;
  350. if (handle->binary_file_len) {
  351. /*
  352. * Header length gets added to handle->binary_file_len in _ota_write
  353. * Clear handle->binary_file_len to avoid additional 289 bytes in binary_file_len
  354. */
  355. binary_file_len = handle->binary_file_len;
  356. handle->binary_file_len = 0;
  357. } else {
  358. if (read_header(handle) != ESP_OK) {
  359. return ESP_FAIL;
  360. }
  361. binary_file_len = IMAGE_HEADER_SIZE;
  362. }
  363. err = esp_ota_verify_chip_id(handle->ota_upgrade_buf);
  364. if (err != ESP_OK) {
  365. return err;
  366. }
  367. return _ota_write(handle, (const void *)handle->ota_upgrade_buf, binary_file_len);
  368. case ESP_HTTPS_OTA_IN_PROGRESS:
  369. data_read = esp_http_client_read(handle->http_client,
  370. handle->ota_upgrade_buf,
  371. handle->ota_upgrade_buf_size);
  372. if (data_read == 0) {
  373. /*
  374. * esp_http_client_is_complete_data_received is added to check whether
  375. * complete image is received.
  376. */
  377. bool is_recv_complete = esp_http_client_is_complete_data_received(handle->http_client);
  378. /*
  379. * As esp_http_client_read doesn't return negative error code if select fails, we rely on
  380. * `errno` to check for underlying transport connectivity closure if any.
  381. * Incase the complete data has not been received but the server has sent
  382. * an ENOTCONN or ECONNRESET, failure is returned. We close with success
  383. * if complete data has been received.
  384. */
  385. if ((errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) && !is_recv_complete) {
  386. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  387. return ESP_FAIL;
  388. } else if (!is_recv_complete) {
  389. return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  390. }
  391. ESP_LOGD(TAG, "Connection closed");
  392. } else if (data_read > 0) {
  393. return _ota_write(handle, (const void *)handle->ota_upgrade_buf, data_read);
  394. } else {
  395. ESP_LOGE(TAG, "data read %d, errno %d", data_read, errno);
  396. return ESP_FAIL;
  397. }
  398. if (!handle->partial_http_download || (handle->partial_http_download && handle->image_length == handle->binary_file_len)) {
  399. handle->state = ESP_HTTPS_OTA_SUCCESS;
  400. }
  401. break;
  402. default:
  403. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  404. return ESP_FAIL;
  405. break;
  406. }
  407. if (handle->partial_http_download) {
  408. if (handle->state == ESP_HTTPS_OTA_IN_PROGRESS && handle->image_length > handle->binary_file_len) {
  409. esp_http_client_close(handle->http_client);
  410. char *header_val = NULL;
  411. if ((handle->image_length - handle->binary_file_len) > handle->max_http_request_size) {
  412. asprintf(&header_val, "bytes=%d-%d", handle->binary_file_len, (handle->binary_file_len + handle->max_http_request_size - 1));
  413. } else {
  414. asprintf(&header_val, "bytes=%d-", handle->binary_file_len);
  415. }
  416. if (header_val == NULL) {
  417. ESP_LOGE(TAG, "Failed to allocate memory for HTTP header");
  418. return ESP_ERR_NO_MEM;
  419. }
  420. esp_http_client_set_header(handle->http_client, "Range", header_val);
  421. free(header_val);
  422. err = _http_connect(handle->http_client);
  423. if (err != ESP_OK) {
  424. ESP_LOGE(TAG, "Failed to establish HTTP connection");
  425. return ESP_FAIL;
  426. }
  427. ESP_LOGD(TAG, "Connection start");
  428. return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  429. }
  430. }
  431. return ESP_OK;
  432. }
  433. bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle)
  434. {
  435. bool ret = false;
  436. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  437. if (handle->partial_http_download) {
  438. ret = (handle->image_length == handle->binary_file_len);
  439. } else {
  440. ret = esp_http_client_is_complete_data_received(handle->http_client);
  441. }
  442. return ret;
  443. }
  444. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
  445. {
  446. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  447. if (handle == NULL) {
  448. return ESP_ERR_INVALID_ARG;
  449. }
  450. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  451. return ESP_FAIL;
  452. }
  453. esp_err_t err = ESP_OK;
  454. switch (handle->state) {
  455. case ESP_HTTPS_OTA_SUCCESS:
  456. case ESP_HTTPS_OTA_IN_PROGRESS:
  457. err = esp_ota_end(handle->update_handle);
  458. /* falls through */
  459. case ESP_HTTPS_OTA_BEGIN:
  460. if (handle->ota_upgrade_buf) {
  461. free(handle->ota_upgrade_buf);
  462. }
  463. if (handle->http_client) {
  464. _http_cleanup(handle->http_client);
  465. }
  466. break;
  467. default:
  468. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  469. break;
  470. }
  471. if ((err == ESP_OK) && (handle->state == ESP_HTTPS_OTA_SUCCESS)) {
  472. esp_err_t err = esp_ota_set_boot_partition(handle->update_partition);
  473. if (err != ESP_OK) {
  474. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%x", err);
  475. }
  476. }
  477. free(handle);
  478. return err;
  479. }
  480. esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle)
  481. {
  482. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  483. if (handle == NULL) {
  484. return ESP_ERR_INVALID_ARG;
  485. }
  486. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  487. return ESP_FAIL;
  488. }
  489. esp_err_t err = ESP_OK;
  490. switch (handle->state) {
  491. case ESP_HTTPS_OTA_SUCCESS:
  492. case ESP_HTTPS_OTA_IN_PROGRESS:
  493. err = esp_ota_abort(handle->update_handle);
  494. /* falls through */
  495. case ESP_HTTPS_OTA_BEGIN:
  496. if (handle->ota_upgrade_buf) {
  497. free(handle->ota_upgrade_buf);
  498. }
  499. if (handle->http_client) {
  500. _http_cleanup(handle->http_client);
  501. }
  502. break;
  503. default:
  504. err = ESP_ERR_INVALID_STATE;
  505. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  506. break;
  507. }
  508. free(handle);
  509. return err;
  510. }
  511. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)
  512. {
  513. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  514. if (handle == NULL) {
  515. return -1;
  516. }
  517. if (handle->state < ESP_HTTPS_OTA_IN_PROGRESS) {
  518. return -1;
  519. }
  520. return handle->binary_file_len;
  521. }
  522. int esp_https_ota_get_image_size(esp_https_ota_handle_t https_ota_handle)
  523. {
  524. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  525. if (handle == NULL) {
  526. return -1;
  527. }
  528. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  529. return -1;
  530. }
  531. return handle->image_length;
  532. }
  533. esp_err_t esp_https_ota(const esp_http_client_config_t *config)
  534. {
  535. if (!config) {
  536. ESP_LOGE(TAG, "esp_http_client config not found");
  537. return ESP_ERR_INVALID_ARG;
  538. }
  539. esp_https_ota_config_t ota_config = {
  540. .http_config = config,
  541. };
  542. esp_https_ota_handle_t https_ota_handle = NULL;
  543. esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle);
  544. if (https_ota_handle == NULL) {
  545. return ESP_FAIL;
  546. }
  547. while (1) {
  548. err = esp_https_ota_perform(https_ota_handle);
  549. if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
  550. break;
  551. }
  552. }
  553. if (err != ESP_OK) {
  554. esp_https_ota_abort(https_ota_handle);
  555. return err;
  556. }
  557. esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
  558. if (ota_finish_err != ESP_OK) {
  559. return ota_finish_err;
  560. }
  561. return ESP_OK;
  562. }