|
| 1 | +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <string.h> |
| 18 | +#include <esp_https_ota.h> |
| 19 | +#include <esp_ota_ops.h> |
| 20 | +#include <esp_log.h> |
| 21 | + |
| 22 | +#define OTA_BUF_SIZE 256 |
| 23 | +static const char *TAG = "esp_https_ota"; |
| 24 | + |
| 25 | +static void http_cleanup(esp_http_client_handle_t client) |
| 26 | +{ |
| 27 | + esp_http_client_close(client); |
| 28 | + esp_http_client_cleanup(client); |
| 29 | +} |
| 30 | + |
| 31 | +esp_err_t esp_https_ota(const esp_http_client_config_t *config) |
| 32 | +{ |
| 33 | + if (!config) { |
| 34 | + ESP_LOGE(TAG, "esp_http_client config not found"); |
| 35 | + return ESP_ERR_INVALID_ARG; |
| 36 | + } |
| 37 | + |
| 38 | + if (!config->cert_pem) { |
| 39 | + ESP_LOGE(TAG, "Server certificate not found in esp_http_client config"); |
| 40 | + return ESP_FAIL; |
| 41 | + } |
| 42 | + |
| 43 | + esp_http_client_handle_t client = esp_http_client_init(config); |
| 44 | + if (client == NULL) { |
| 45 | + ESP_LOGE(TAG, "Failed to initialise HTTP connection"); |
| 46 | + return ESP_FAIL; |
| 47 | + } |
| 48 | + |
| 49 | + if (esp_http_client_get_transport_type(client) != HTTP_TRANSPORT_OVER_SSL) { |
| 50 | + ESP_LOGE(TAG, "Transport is not over HTTPS"); |
| 51 | + return ESP_FAIL; |
| 52 | + } |
| 53 | + |
| 54 | + esp_err_t err = esp_http_client_open(client, 0); |
| 55 | + if (err != ESP_OK) { |
| 56 | + esp_http_client_cleanup(client); |
| 57 | + ESP_LOGE(TAG, "Failed to open HTTP connection: %d", err); |
| 58 | + return err; |
| 59 | + } |
| 60 | + esp_http_client_fetch_headers(client); |
| 61 | + |
| 62 | + esp_ota_handle_t update_handle = 0; |
| 63 | + const esp_partition_t *update_partition = NULL; |
| 64 | + ESP_LOGI(TAG, "Starting OTA..."); |
| 65 | + update_partition = esp_ota_get_next_update_partition(NULL); |
| 66 | + if (update_partition == NULL) { |
| 67 | + ESP_LOGE(TAG, "Passive OTA partition not found"); |
| 68 | + http_cleanup(client); |
| 69 | + return ESP_FAIL; |
| 70 | + } |
| 71 | + ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", |
| 72 | + update_partition->subtype, update_partition->address); |
| 73 | + |
| 74 | + err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); |
| 75 | + if (err != ESP_OK) { |
| 76 | + ESP_LOGE(TAG, "esp_ota_begin failed, error=%d", err); |
| 77 | + http_cleanup(client); |
| 78 | + return err; |
| 79 | + } |
| 80 | + ESP_LOGI(TAG, "esp_ota_begin succeeded"); |
| 81 | + ESP_LOGI(TAG, "Please Wait. This may take time"); |
| 82 | + |
| 83 | + esp_err_t ota_write_err = ESP_OK; |
| 84 | + char *upgrade_data_buf = (char *)malloc(OTA_BUF_SIZE); |
| 85 | + if (!upgrade_data_buf) { |
| 86 | + ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer"); |
| 87 | + return ESP_ERR_NO_MEM; |
| 88 | + } |
| 89 | + int binary_file_len = 0; |
| 90 | + while (1) { |
| 91 | + int data_read = esp_http_client_read(client, upgrade_data_buf, OTA_BUF_SIZE); |
| 92 | + if (data_read == 0) { |
| 93 | + ESP_LOGI(TAG, "Connection closed,all data received"); |
| 94 | + break; |
| 95 | + } |
| 96 | + if (data_read < 0) { |
| 97 | + ESP_LOGE(TAG, "Error: SSL data read error"); |
| 98 | + break; |
| 99 | + } |
| 100 | + if (data_read > 0) { |
| 101 | + ota_write_err = esp_ota_write( update_handle, (const void *)upgrade_data_buf, data_read); |
| 102 | + if (ota_write_err != ESP_OK) { |
| 103 | + break; |
| 104 | + } |
| 105 | + binary_file_len += data_read; |
| 106 | + ESP_LOGD(TAG, "Written image length %d", binary_file_len); |
| 107 | + } |
| 108 | + } |
| 109 | + free(upgrade_data_buf); |
| 110 | + http_cleanup(client); |
| 111 | + ESP_LOGD(TAG, "Total binary data length writen: %d", binary_file_len); |
| 112 | + |
| 113 | + esp_err_t ota_end_err = esp_ota_end(update_handle); |
| 114 | + if (ota_write_err != ESP_OK) { |
| 115 | + ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%d", err); |
| 116 | + return ota_write_err; |
| 117 | + } else if (ota_end_err != ESP_OK) { |
| 118 | + ESP_LOGE(TAG, "Error: esp_ota_end failed! err=0x%d. Image is invalid", ota_end_err); |
| 119 | + return ota_end_err; |
| 120 | + } |
| 121 | + |
| 122 | + err = esp_ota_set_boot_partition(update_partition); |
| 123 | + if (err != ESP_OK) { |
| 124 | + ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%d", err); |
| 125 | + return err; |
| 126 | + } |
| 127 | + ESP_LOGI(TAG, "esp_ota_set_boot_partition succeeded"); |
| 128 | + |
| 129 | + return ESP_OK; |
| 130 | +} |
0 commit comments