Skip to content

Commit ef30229

Browse files
committed
Merge branch 'feature/http_firmware_upgrade_v3.1' into 'release/v3.1'
esp_https_ota: Add esp_https_ota component (Backport v3.1) See merge request idf/esp-idf!2954
2 parents 45d7039 + 002189c commit ef30229

File tree

24 files changed

+544
-196
lines changed

24 files changed

+544
-196
lines changed

components/esp_http_client/esp_http_client.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const c
273273
return http_header_delete(client->request->headers, key);
274274
}
275275

276-
static esp_err_t _set_config(esp_http_client_handle_t client, esp_http_client_config_t *config)
276+
static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_client_config_t *config)
277277
{
278278
client->connection_info.method = config->method;
279279
client->connection_info.port = config->port;
@@ -411,7 +411,7 @@ static esp_err_t esp_http_client_prepare(esp_http_client_handle_t client)
411411
return ESP_OK;
412412
}
413413

414-
esp_http_client_handle_t esp_http_client_init(esp_http_client_config_t *config)
414+
esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config)
415415
{
416416

417417
esp_http_client_handle_t client;
@@ -992,3 +992,14 @@ bool esp_http_client_is_chunked_response(esp_http_client_handle_t client)
992992
{
993993
return client->response->is_chunked;
994994
}
995+
996+
esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client)
997+
{
998+
if (!strcmp(client->connection_info.scheme, "https") ) {
999+
return HTTP_TRANSPORT_OVER_SSL;
1000+
} else if (!strcmp(client->connection_info.scheme, "http")) {
1001+
return HTTP_TRANSPORT_OVER_TCP;
1002+
} else {
1003+
return HTTP_TRANSPORT_UNKNOWN;
1004+
}
1005+
}

components/esp_http_client/include/esp_http_client.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ typedef struct {
131131
* - `esp_http_client_handle_t`
132132
* - NULL if any errors
133133
*/
134-
esp_http_client_handle_t esp_http_client_init(esp_http_client_config_t *config);
134+
esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config);
135135

136136
/**
137137
* @brief Invoke this function after `esp_http_client_init` and all the options calls are made, and will perform the
@@ -334,11 +334,22 @@ esp_err_t esp_http_client_close(esp_http_client_handle_t client);
334334
*/
335335
esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client);
336336

337+
/**
338+
* @brief Get transport type
339+
*
340+
* @param[in] client The esp_http_client handle
341+
*
342+
* @return
343+
* - HTTP_TRANSPORT_UNKNOWN
344+
* - HTTP_TRANSPORT_OVER_TCP
345+
* - HTTP_TRANSPORT_OVER_SSL
346+
*/
347+
esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client);
337348

338349

339350
#ifdef __cplusplus
340351
}
341352
#endif
342353

343354

344-
#endif
355+
#endif

components/esp_https_ota/component.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
COMPONENT_SRCDIRS := src
2+
3+
COMPONENT_ADD_INCLUDEDIRS := include
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#pragma once
16+
17+
#include <esp_http_client.h>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
/**
24+
* @brief HTTPS OTA Firmware upgrade.
25+
*
26+
* This function performs HTTPS OTA Firmware upgrade
27+
*
28+
* @param[in] config pointer to esp_http_client_config_t structure.
29+
*
30+
* @note For secure HTTPS updates, the `cert_pem` member of `config`
31+
* structure must be set to the server certificate.
32+
*
33+
* @return
34+
* - ESP_OK: OTA data updated, next reboot will use specified partition.
35+
* - ESP_FAIL: For generic failure.
36+
* - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
37+
* - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
38+
* - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
39+
* - For other return codes, refer OTA documentation in esp-idf's app_update component.
40+
*/
41+
esp_err_t esp_https_ota(const esp_http_client_config_t *config);
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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: %s", esp_err_to_name(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+
}

docs/Doxyfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ INPUT = \
142142
../../components/esp32/include/esp_ipc.h \
143143
## Over The Air Updates (OTA)
144144
../../components/app_update/include/esp_ota_ops.h \
145+
## ESP HTTPS OTA
146+
../../components/esp_https_ota/include/esp_https_ota.h \
145147
## Sleep
146148
## NOTE: for line below header_file.inc is not used
147149
../../components/esp32/include/esp_sleep.h \
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ESP HTTPS OTA
2+
=============
3+
4+
Overview
5+
--------
6+
7+
``esp_https_ota`` provides simplified APIs to perform firmware upgrades over HTTPS.
8+
It's an abstraction layer over existing OTA APIs.
9+
10+
Application Example
11+
-------------------
12+
13+
.. highlight:: c
14+
15+
::
16+
17+
esp_err_t do_firmware_upgrade()
18+
{
19+
esp_http_client_config_t config = {
20+
.url = CONFIG_FIRMWARE_UPGRADE_URL,
21+
.cert_pem = (char *)server_cert_pem_start,
22+
};
23+
esp_err_t ret = esp_https_ota(&config);
24+
if (ret == ESP_OK) {
25+
esp_restart();
26+
} else {
27+
return ESP_FAIL;
28+
}
29+
return ESP_OK;
30+
}
31+
32+
API Reference
33+
-------------
34+
35+
.. include:: /_build/inc/esp_https_ota.inc

docs/en/api-reference/system/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ System API
1818
Sleep Modes <sleep_modes>
1919
Base MAC address <base_mac_address>
2020
Over The Air Updates (OTA) <ota>
21+
ESP HTTPS OTA <esp_https_ota>
2122
ESP pthread <esp_pthread>
2223
Error Codes and Helper Functions <esp_err>
2324

docs/en/api-reference/system/ota.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ See also
3737

3838
* :doc:`Partition Table documentation <../../api-guides/partition-tables>`
3939
* :doc:`Lower-Level SPI Flash/Partition API <../storage/spi_flash>`
40+
* :doc:`ESP HTTPS OTA <esp_https_ota>`
4041

4142
Application Example
4243
-------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.. include:: ../../../en/api-reference/system/esp_https_ota.rst

0 commit comments

Comments
 (0)