Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "esp_http_client.h"
#include "esp_https_ota.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"
#include "esp_efuse.h"
#include "esp_wifi.h"
#include "ble_api.h"
TAG
#define OTA_URL_SIZE
event_handler(void *, esp_event_base_t, int32_t, void *)
validate_image_header(esp_app_desc_t *)
_http_client_init_cb(esp_http_client_handle_t)
advanced_ota_example_task(void *)
app_main()
Files
loading (3/4)...
SourceVuESP-IDF Framework and Examplesadvanced_https_ota samplemain/advanced_https_ota_example.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* Advanced HTTPS OTA example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *//* ... */ #include <string.h> #include <inttypes.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_http_client.h" #include "esp_https_ota.h" #include "nvs.h" #include "nvs_flash.h" #include "protocol_examples_common.h"14 includes #if CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK #include "esp_efuse.h" #endif #if CONFIG_EXAMPLE_CONNECT_WIFI #include "esp_wifi.h" #endif #if CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED #include "ble_api.h" #endif static const char *TAG = "advanced_https_ota_example"; extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end"); #define OTA_URL_SIZE 256 /* Event handler for catching system events */ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == ESP_HTTPS_OTA_EVENT) { switch (event_id) { case ESP_HTTPS_OTA_START: ESP_LOGI(TAG, "OTA started"); break;... case ESP_HTTPS_OTA_CONNECTED: ESP_LOGI(TAG, "Connected to server"); break;... case ESP_HTTPS_OTA_GET_IMG_DESC: ESP_LOGI(TAG, "Reading Image Description"); break;... case ESP_HTTPS_OTA_VERIFY_CHIP_ID: ESP_LOGI(TAG, "Verifying chip id of new image: %d", *(esp_chip_id_t *)event_data); break;... case ESP_HTTPS_OTA_DECRYPT_CB: ESP_LOGI(TAG, "Callback to decrypt function"); break;... case ESP_HTTPS_OTA_WRITE_FLASH: ESP_LOGD(TAG, "Writing to flash: %d written", *(int *)event_data); break;... case ESP_HTTPS_OTA_UPDATE_BOOT_PARTITION: ESP_LOGI(TAG, "Boot partition updated. Next Partition: %d", *(esp_partition_subtype_t *)event_data); break;... case ESP_HTTPS_OTA_FINISH: ESP_LOGI(TAG, "OTA finish"); break;... case ESP_HTTPS_OTA_ABORT: ESP_LOGI(TAG, "OTA abort"); break;... }{...} }{...} }{ ... } static esp_err_t validate_image_header(esp_app_desc_t *new_app_info) { if (new_app_info == NULL) { return ESP_ERR_INVALID_ARG; }{...} const esp_partition_t *running = esp_ota_get_running_partition(); esp_app_desc_t running_app_info; if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) { ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version); }{...} #ifndef CONFIG_EXAMPLE_SKIP_VERSION_CHECK if (memcmp(new_app_info->version, running_app_info.version, sizeof(new_app_info->version)) == 0) { ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update."); return ESP_FAIL; }{...} #endif/* ... */ #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK /** * Secure version check from firmware image header prevents subsequent download and flash write of * entire firmware image. However this is optional because it is also taken care in API * esp_https_ota_finish at the end of OTA update procedure. *//* ... */ const uint32_t hw_sec_version = esp_efuse_read_secure_version(); if (new_app_info->secure_version < hw_sec_version) { ESP_LOGW(TAG, "New firmware security version is less than eFuse programmed, %"PRIu32" < %"PRIu32, new_app_info->secure_version, hw_sec_version); return ESP_FAIL; }{...} #endif/* ... */ return ESP_OK; }{ ... } static esp_err_t _http_client_init_cb(esp_http_client_handle_t http_client) { esp_err_t err = ESP_OK; /* Uncomment to add custom headers to HTTP request */ // err = esp_http_client_set_header(http_client, "Custom-Header", "Value"); return err; }{ ... } void advanced_ota_example_task(void *pvParameter) { ESP_LOGI(TAG, "Starting Advanced OTA example"); esp_err_t ota_finish_err = ESP_OK; esp_http_client_config_t config = { .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL, .cert_pem = (char *)server_cert_pem_start, .timeout_ms = CONFIG_EXAMPLE_OTA_RECV_TIMEOUT, .keep_alive_enable = true, }{...}; #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN char url_buf[OTA_URL_SIZE]; if (strcmp(config.url, "FROM_STDIN") == 0) { example_configure_stdin_stdout(); fgets(url_buf, OTA_URL_SIZE, stdin); int len = strlen(url_buf); url_buf[len - 1] = '\0'; config.url = url_buf; }{...} else { ESP_LOGE(TAG, "Configuration mismatch: wrong firmware upgrade image url"); abort(); }{...} #endif/* ... */ #ifdef CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK config.skip_cert_common_name_check = true; #endif esp_https_ota_config_t ota_config = { .http_config = &config, .http_client_init_cb = _http_client_init_cb, // Register a callback to be invoked after esp_http_client is initialized #ifdef CONFIG_EXAMPLE_ENABLE_PARTIAL_HTTP_DOWNLOAD .partial_http_download = true, .max_http_request_size = CONFIG_EXAMPLE_HTTP_REQUEST_SIZE,/* ... */ #endif }{...}; esp_https_ota_handle_t https_ota_handle = NULL; esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "ESP HTTPS OTA Begin failed"); vTaskDelete(NULL); }{...} esp_app_desc_t app_desc; err = esp_https_ota_get_img_desc(https_ota_handle, &app_desc); if (err != ESP_OK) { ESP_LOGE(TAG, "esp_https_ota_get_img_desc failed"); goto ota_end; }{...} err = validate_image_header(&app_desc); if (err != ESP_OK) { ESP_LOGE(TAG, "image header verification failed"); goto ota_end; }{...} while (1) { err = esp_https_ota_perform(https_ota_handle); if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) { break; }{...} // esp_https_ota_perform returns after every read operation which gives user the ability to // monitor the status of OTA upgrade by calling esp_https_ota_get_image_len_read, which gives length of image // data read so far. ESP_LOGD(TAG, "Image bytes read: %d", esp_https_ota_get_image_len_read(https_ota_handle)); }{...} if (esp_https_ota_is_complete_data_received(https_ota_handle) != true) { // the OTA image was not completely received and user can customise the response to this situation. ESP_LOGE(TAG, "Complete data was not received."); }{...} else { ota_finish_err = esp_https_ota_finish(https_ota_handle); if ((err == ESP_OK) && (ota_finish_err == ESP_OK)) { ESP_LOGI(TAG, "ESP_HTTPS_OTA upgrade successful. Rebooting ..."); vTaskDelay(1000 / portTICK_PERIOD_MS); esp_restart(); }{...} else { if (ota_finish_err == ESP_ERR_OTA_VALIDATE_FAILED) { ESP_LOGE(TAG, "Image validation failed, image is corrupted"); }{...} ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed 0x%x", ota_finish_err); vTaskDelete(NULL); }{...} }{...} ota_end: esp_https_ota_abort(https_ota_handle); ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed"); vTaskDelete(NULL); }{ ... } void app_main(void) { ESP_LOGI(TAG, "OTA example app_main start"); // Initialize NVS. esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { // 1.OTA app partition table has a smaller NVS partition size than the non-OTA // partition table. This size mismatch may cause NVS initialization to fail. // 2.NVS partition contains data in new format and cannot be recognized by this version of code. // If this happens, we erase NVS partition and initialize NVS again. ESP_ERROR_CHECK(nvs_flash_erase()); err = nvs_flash_init(); }{...} ESP_ERROR_CHECK( err ); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_OTA_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. * Read "Establishing Wi-Fi or Ethernet Connection" section in * examples/protocols/README.md for more information about this function. *//* ... */ ESP_ERROR_CHECK(example_connect()); #if defined(CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE) /** * We are treating successful WiFi connection as a checkpoint to cancel rollback * process and mark newly updated firmware image as active. For production cases, * please tune the checkpoint behavior per end application requirement. *//* ... */ const esp_partition_t *running = esp_ota_get_running_partition(); esp_ota_img_states_t ota_state; if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) { if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) { if (esp_ota_mark_app_valid_cancel_rollback() == ESP_OK) { ESP_LOGI(TAG, "App is valid, rollback cancelled successfully"); }{...} else { ESP_LOGE(TAG, "Failed to cancel rollback"); }{...} }{...} }{...} #endif/* ... */ #if CONFIG_EXAMPLE_CONNECT_WIFI #if !CONFIG_BT_ENABLED /* Ensure to disable any WiFi power save mode, this allows best throughput * and hence timings for overall OTA operation. *//* ... */ esp_wifi_set_ps(WIFI_PS_NONE);/* ... */ #else /* WIFI_PS_MIN_MODEM is the default mode for WiFi Power saving. When both * WiFi and Bluetooth are running, WiFI modem has to go down, hence we * need WIFI_PS_MIN_MODEM. And as WiFi modem goes down, OTA download time * increases. *//* ... */ esp_wifi_set_ps(WIFI_PS_MIN_MODEM);/* ... */ #endif // CONFIG_BT_ENABLED/* ... */ #endif // CONFIG_EXAMPLE_CONNECT_WIFI #if CONFIG_BT_CONTROLLER_ENABLED && (CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED) ESP_ERROR_CHECK(esp_ble_helper_init()); #endif xTaskCreate(&advanced_ota_example_task, "advanced_ota_example_task", 1024 * 8, NULL, 5, NULL); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.