-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiap_https.c
More file actions
576 lines (454 loc) · 22.3 KB
/
iap_https.c
File metadata and controls
576 lines (454 loc) · 22.3 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//
// iap_https.c
// esp32-ota-https
//
// Updating the firmware over the air.
//
// This module is responsible to trigger and coordinate firmware updates.
//
// Created by Andreas Schweizer on 11.01.2017.
// Copyright © 2017 Classy Code GmbH
//
// Copyright (c) 2018 Manuel Wick
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#include <string.h>
#include "esp_system.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "freertos/event_groups.h"
#include "ota_tls.h"
#include "ota_https_client.h"
#include "iap.h"
#include "iap_https.h"
#define TAG "fwup_wifi"
// The TLS context to communicate with the firmware update server.
static struct ota_tls_context_ *tls_context;
// Module configuration.
static iap_https_config_t *fwupdater_config;
// The metadata request.
static ota_http_request_t http_metadata_request;
// The firmware image request.
static ota_http_request_t http_firmware_data_request;
// The event group for our processing task.
#define FWUP_CHECK_FOR_UPDATE (1 << 0)
#define FWUP_FORCE_UPDATE (1 << 1)
#define FWUP_DOWNLOAD_IMAGE (1 << 2)
static EventGroupHandle_t iap_https_event_group;
// The event group for WiFi connection state
#define IAP_HTTPS_WIFI_STA_EVENT_GROUP_CONNECTED_FLAG (1 << 0)
static EventGroupHandle_t iap_https_wifi_event_group;
// The timer for the periodic checking.
static TimerHandle_t check_for_updates_timer;
static int has_iap_session;
static int has_new_firmware;
static int total_nof_bytes_received;
static bool force_update;
static void iap_https_periodic_check_timer_callback(TimerHandle_t xTimer);
static void iap_https_task(void *pvParameter);
static void iap_https_prepare_timer();
static void iap_https_trigger_processing(bool force);
static void iap_https_check_for_update();
static void iap_https_download_image();
ota_http_continue_receiving_t iap_https_metadata_headers_callback(struct ota_http_request_ *request, int statusCode, int contentLength);
ota_http_continue_receiving_t iap_https_metadata_body_callback(struct ota_http_request_ *request, size_t bytesReceived);
ota_http_continue_receiving_t iap_https_firmware_headers_callback(struct ota_http_request_ *request, int statusCode, int contentLength);
ota_http_continue_receiving_t iap_https_firmware_body_callback(struct ota_http_request_ *request, size_t bytesReceived);
void iap_https_error_callback(struct ota_http_request_ *request, ota_http_err_t error, int additionalInfo);
int iap_https_init(iap_https_config_t *config)
{
ESP_LOGD(TAG, "iap_https_init");
iap_init();
fwupdater_config = config;
// Initialise the HTTPS context to the OTA server.
ota_tls_init_struct_t tlsInitStruct = {
.server_host_name = config->server_host_name,
.server_port = config->server_port,
.server_root_ca_public_key_pem = config->server_root_ca_public_key_pem,
.server_root_ca_public_key_pem_len = config->server_root_ca_public_key_pem_len,
.peer_public_key_pem = config->peer_public_key_pem,
.peer_public_key_pem_len = config->peer_public_key_pem_len
};
tls_context = ota_tls_create_context(&tlsInitStruct);
// Initialise two requests, one to get the metadata and one to get the actual firmware image.
http_metadata_request.verb = OTA_HTTP_GET;
http_metadata_request.host = config->server_host_name;
http_metadata_request.path = config->server_metadata_path;
http_metadata_request.response_mode = OTA_HTTP_WAIT_FOR_COMPLETE_BODY;
http_metadata_request.response_buffer_len = 512;
http_metadata_request.response_buffer = malloc(http_metadata_request.response_buffer_len * sizeof(char));
http_metadata_request.error_callback = iap_https_error_callback;
http_metadata_request.headers_callback = iap_https_metadata_headers_callback;
http_metadata_request.body_callback = iap_https_metadata_body_callback;
http_firmware_data_request.verb = OTA_HTTP_GET;
http_firmware_data_request.host = config->server_host_name;
http_firmware_data_request.path = config->server_firmware_path;
http_firmware_data_request.response_mode = OTA_HTTP_STREAM_BODY;
http_firmware_data_request.response_buffer_len = 4096;
http_firmware_data_request.response_buffer = malloc(http_firmware_data_request.response_buffer_len * sizeof(char));
http_firmware_data_request.error_callback = iap_https_error_callback;
http_firmware_data_request.headers_callback = iap_https_firmware_headers_callback;
http_firmware_data_request.body_callback = iap_https_firmware_body_callback;
// Start our processing task.
iap_https_event_group = xEventGroupCreate();
iap_https_wifi_event_group = xEventGroupCreate();
iap_https_prepare_timer();
//xTaskCreate(&iap_https_task, "fwup_wifi_task", 4096, NULL, 1, NULL);
xTaskCreate(&iap_https_task, "fwup_wifi_task", 8192, NULL, 1, NULL);
return 0;
}
int iap_https_check_now(bool force)
{
ESP_LOGD(TAG, "iap_https_check_now");
iap_https_trigger_processing(force);
return 0;
}
int iap_https_update_in_progress()
{
return has_iap_session;
}
int iap_https_new_firmware_installed()
{
return has_new_firmware;
}
static void iap_https_periodic_check_timer_callback(TimerHandle_t xTimer)
{
//xEventGroupSetBits(iap_https_event_group, FWUP_CHECK_FOR_UPDATE);
iap_https_trigger_processing(false);
}
static void iap_https_trigger_processing(bool force)
{
static iap_https_event_t callbackEvent;
ESP_LOGD(TAG, "iap_https_trigger_processing: checking flag");
if (xEventGroupGetBits(iap_https_event_group) & (FWUP_CHECK_FOR_UPDATE | FWUP_FORCE_UPDATE )) {
ESP_LOGD(TAG, "iap_https_trigger_processing: flag is already set");
return;
}
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_CHECK_FOR_UPDATE;
fwupdater_config->event_callback(&callbackEvent);
}
ESP_LOGD(TAG, "iap_https_trigger_processing: flag is not set, setting it");
// Trigger processing in our task.
if(!force) {
xEventGroupSetBits(iap_https_event_group, FWUP_CHECK_FOR_UPDATE);
} else {
xEventGroupSetBits(iap_https_event_group, FWUP_FORCE_UPDATE);
}
}
static void iap_https_task(void *pvParameter)
{
ESP_LOGI(TAG, "Firmware updater task started.");
// When the time has come, trigger the firmware update process.
xEventGroupWaitBits(iap_https_wifi_event_group, IAP_HTTPS_WIFI_STA_EVENT_GROUP_CONNECTED_FLAG, pdFALSE, pdFALSE, portMAX_DELAY);
vTaskDelay(5000 / portTICK_PERIOD_MS);
while (1) {
// Wait until we get waked up (periodically or because somebody manually
// requests a firmware update check) and until we're connected to the WIFI
// network.
BaseType_t clearOnExit = pdFALSE;
BaseType_t waitForAllBits = pdFALSE;
EventBits_t bits = xEventGroupWaitBits(iap_https_event_group, FWUP_CHECK_FOR_UPDATE | FWUP_FORCE_UPDATE | FWUP_DOWNLOAD_IMAGE, clearOnExit, waitForAllBits, portMAX_DELAY);
xEventGroupWaitBits(iap_https_wifi_event_group, IAP_HTTPS_WIFI_STA_EVENT_GROUP_CONNECTED_FLAG, pdFALSE, pdFALSE, portMAX_DELAY);
// We give FWUP_DOWNLOAD_IMAGE priority because if it's set, the check for update has
// previously been executed and the result was that we should update the firmware.
if (bits & FWUP_DOWNLOAD_IMAGE) {
ESP_LOGI(TAG, "Firmware updater task will now download the new firmware image.");
iap_https_download_image();
// ESP_LOGI(TAG, "DUMMY!!! Firmware updater task will now download the new firmware image.");
xEventGroupClearBits(iap_https_event_group, FWUP_DOWNLOAD_IMAGE);
} else if (bits & (FWUP_CHECK_FOR_UPDATE | FWUP_FORCE_UPDATE)) {
ESP_LOGI(TAG, "Firmware updater task checking for firmware update.");
force_update = ((bits & FWUP_FORCE_UPDATE) != 0);
iap_https_check_for_update();
// If periodic OTA update checks are enabled, re-start the timer.
// Clear the bit *after* resetting the timer to avoid the race condition
// where the timer could have elapsed during the update check and we would
// immediately check again.
iap_https_prepare_timer();
xEventGroupClearBits(iap_https_event_group, (FWUP_CHECK_FOR_UPDATE | FWUP_FORCE_UPDATE));
}
}
}
static void iap_https_prepare_timer()
{
// Make sure we have a timer if we need one and don't have one if we don't need one.
if (fwupdater_config->polling_interval_s > 0) {
if (!check_for_updates_timer) {
// We need a timer but don't have one. Create it.
BaseType_t autoReload = pdFALSE;
check_for_updates_timer = xTimerCreate("fwup_periodic_check", 1000, autoReload, NULL, iap_https_periodic_check_timer_callback);
if (!check_for_updates_timer) {
ESP_LOGE(TAG, "iap_https_prepare_timer: failed to create the fwup_periodic_check timer!");
return;
}
}
// We need and have a timer, so make sure it uses the correct interval, then start it.
uint32_t timerMillisec = 1000 * fwupdater_config->polling_interval_s;
ESP_LOGD(TAG, "iap_https_prepare_timer: timer interval = %d ms", timerMillisec);
TickType_t timerPeriod = pdMS_TO_TICKS(timerMillisec);
xTimerChangePeriod(check_for_updates_timer, timerPeriod, pdMS_TO_TICKS(5000));
if (pdFAIL == xTimerReset(check_for_updates_timer, pdMS_TO_TICKS(5000))) {
ESP_LOGE(TAG, "iap_https_prepare_timer: failed to start the fwup_periodic_check timer!");
}
return;
}
// We don't need a timer.
if (check_for_updates_timer) {
// We have a timer but don't need it. Delete it.
xTimerDelete(check_for_updates_timer, pdMS_TO_TICKS(5000));
check_for_updates_timer = NULL;
}
}
static void iap_https_check_for_update()
{
static iap_https_event_t callbackEvent;
ESP_LOGD(TAG, "iap_https_check_for_update");
int tlsResult = ota_tls_connect(tls_context);
if (tlsResult) {
ESP_LOGE(TAG, "iap_https_check_for_update: failed to initiate SSL/TLS connection; ota_tls_connect returned %d", tlsResult);
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
return;
}
ESP_LOGI(TAG, "Requesting firmware metadata from server.");
ota_http_err_t httpResult = ota_https_send_request(tls_context, &http_metadata_request);
if (httpResult != OTA_HTTP_SUCCESS) {
ESP_LOGE(TAG, "iap_https_check_for_update: failed to send HTTPS metadata request; ota_https_send_request returned %d", httpResult);
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
}
}
static void iap_https_download_image()
{
static iap_https_event_t callbackEvent;
int tlsResult = ota_tls_connect(tls_context);
if (tlsResult) {
ESP_LOGE(TAG, "iap_https_download_image: failed to initiate SSL/TLS connection; ota_tls_connect returned %d", tlsResult);
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
}
// Make sure we open a new IAP session in the callback.
has_iap_session = 0;
ESP_LOGI(TAG, "Requesting firmware image '%s' from web server.", fwupdater_config->server_firmware_path);
ota_http_err_t httpResult = ota_https_send_request(tls_context, &http_firmware_data_request);
if (httpResult != OTA_HTTP_SUCCESS) {
ESP_LOGE(TAG, "iap_https_download_image: failed to send HTTPS firmware image request; ota_https_send_request returned %d", httpResult);
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
}
}
ota_http_continue_receiving_t iap_https_metadata_body_callback(struct ota_http_request_ *request, size_t bytesReceived)
{
iap_https_event_t callbackEvent;
ESP_LOGD(TAG, "iap_https_metadata_body_callback");
// --- Process the metadata information ---
// (Optional) interval to check for firmware updates.
int intervalSeconds = 0;
if (!ota_http_parse_key_value_int(request->response_buffer, "INTERVAL=", &intervalSeconds)) {
ESP_LOGD(TAG, "[INTERVAL=] '%d'", intervalSeconds);
if (intervalSeconds != fwupdater_config->polling_interval_s) {
ESP_LOGD(TAG, "iap_https_metadata_body_callback: polling interval changed from %d s to %d s",
fwupdater_config->polling_interval_s, intervalSeconds);
fwupdater_config->polling_interval_s = intervalSeconds;
}
}
static char version[64];
memset(version, 0, sizeof(version) / sizeof(char));
if (!ota_http_parse_key_value_string(request->response_buffer, "VERSION=", version, sizeof(version) / sizeof(char))) {
ESP_LOGD(TAG, "[VERSION=] '%s'", version);
} else {
ESP_LOGW(TAG, "iap_https_metadata_body_callback: firmware version not provided, skipping firmware update");
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
return OTA_HTTP_STOP_RECEIVING;
}
static char fileName[256];
memset(fileName, 0, sizeof(fileName) / sizeof(char));
if (!ota_http_parse_key_value_string(request->response_buffer, "FILE=", fileName, sizeof(fileName) / sizeof(char))) {
ESP_LOGD(TAG, "[FILE=] '%s'", fileName);
strncpy(fwupdater_config->server_firmware_path, fileName, sizeof(fwupdater_config->server_firmware_path) / sizeof(char));
} else {
ESP_LOGW(TAG, "iap_https_metadata_body_callback: firmware file name not provided, skipping firmware update");
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
return OTA_HTTP_STOP_RECEIVING;
}
static uint8_t md5sum[16];
memset(md5sum, 0, sizeof(md5sum) / sizeof(uint8_t));
if (0 == ota_http_parse_key_value_hex_byte_array(request->response_buffer, "MD5SUM=", md5sum, sizeof(md5sum) / sizeof(uint8_t))) {
ESP_LOGD(TAG, "[MD5SUM=] %02x %02x %02x %02x ...", md5sum[0], md5sum[1], md5sum[2], md5sum[3]);
memcpy(fwupdater_config->server_firmware_md5sum, md5sum, sizeof(fwupdater_config->server_firmware_md5sum) / sizeof(uint8_t));
} else {
ESP_LOGW(TAG, "iap_https_metadata_body_callback: firmware md5sum not provided, skipping firmware update");
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
return OTA_HTTP_STOP_RECEIVING;
}
// --- Check if the version on the server is the same as the currently installed version ---
// --- or if this is a forced update ---
if ((!force_update) && (0 == (strcmp(version, fwupdater_config->current_software_version)))) {
ESP_LOGD(TAG, "iap_https_metadata_body_callback: we're up-to-date!");
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UP_TO_DATE;
fwupdater_config->event_callback(&callbackEvent);
}
return OTA_HTTP_STOP_RECEIVING;
}
ESP_LOGD(TAG, "iap_https_metadata_body_callback: our version is %s, the version on the server is %s, force_update is %d",
fwupdater_config->current_software_version, version, force_update ? 1 : 0);
// --- Request the firmware image ---
xEventGroupSetBits(iap_https_event_group, FWUP_DOWNLOAD_IMAGE);
return OTA_HTTP_STOP_RECEIVING;
}
ota_http_continue_receiving_t iap_https_firmware_body_callback(struct ota_http_request_ *request, size_t bytesReceived)
{
static iap_https_event_t callbackEvent;
ESP_LOGD(TAG, "iap_https_firmware_body_callback");
// The first time we receive the callback, we neet to start the IAP session.
if (!has_iap_session) {
ESP_LOGD(TAG, "iap_https_firmware_body_callback: starting IAP session.");
iap_err_t result = iap_begin();
if (result == IAP_ERR_SESSION_ALREADY_OPEN) {
iap_abort();
result = iap_begin();
}
if (result != IAP_OK) {
ESP_LOGE(TAG, "iap_https_firmware_body_callback: iap_begin failed (%d)!", result);
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
return OTA_HTTP_STOP_RECEIVING;
}
total_nof_bytes_received = 0;
has_iap_session = 1;
}
if (bytesReceived > 0) {
// Write the received data to the flash.
iap_err_t result = iap_write((uint8_t*)request->response_buffer, bytesReceived);
total_nof_bytes_received += bytesReceived;
if (result != IAP_OK) {
ESP_LOGE(TAG, "iap_https_firmware_body_callback: write failed (%d), aborting firmware update!", result);
iap_abort();
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
return OTA_HTTP_STOP_RECEIVING;
}
return OTA_HTTP_CONTINUE_RECEIVING;
}
// After all data has been received, we get one last callback (with bytesReceived == 0).
// If this happens, we need to finish the IAP session and, if configured, reboot the device.
ESP_LOGD(TAG, "iap_https_firmware_body_callback: all data received (%d bytes), closing session", total_nof_bytes_received);
has_iap_session = 0;
if (total_nof_bytes_received > 0) {
iap_err_t result = iap_commit(fwupdater_config->server_firmware_md5sum);
if (result != IAP_OK) {
ESP_LOGE(TAG, "iap_https_firmware_body_callback: closing the session has failed (%d)!", result);
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
}
if(IAP_OK == result) {
has_new_firmware = 1;
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_FINISHED;
fwupdater_config->event_callback(&callbackEvent);
}
if (fwupdater_config->auto_reboot) {
ESP_LOGI(TAG, "Automatic re-boot in 2 seconds - goodbye!...");
vTaskDelay(2000 / portTICK_RATE_MS);
esp_restart();
}
}
} else {
ESP_LOGE(TAG, "iap_https_firmware_body_callback: something's not OK - the new firmware image is empty!");
iap_abort();
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
}
return OTA_HTTP_STOP_RECEIVING;
}
ota_http_continue_receiving_t iap_https_metadata_headers_callback(struct ota_http_request_ *request, int statusCode, int contentLength)
{
ESP_LOGD(TAG, "iap_https_metadata_headers_callback");
return OTA_HTTP_CONTINUE_RECEIVING;
}
ota_http_continue_receiving_t iap_https_firmware_headers_callback(struct ota_http_request_ *request, int statusCode, int contentLength)
{
ESP_LOGD(TAG, "iap_https_firmware_headers_callback");
return OTA_HTTP_CONTINUE_RECEIVING;
}
void iap_https_error_callback(struct ota_http_request_ *request, ota_http_err_t error, int additionalInfo)
{
static iap_https_event_t callbackEvent;
ESP_LOGE(TAG, "iap_https_error_callback: error=%d additionalInfo=%d", error, additionalInfo);
if (error == OTA_HTTP_ERR_NON_200_STATUS_CODE) {
switch (additionalInfo) {
case 401:
ESP_LOGE(TAG, "HTTP status code 401: Unauthorized.");
break;
case 403:
ESP_LOGE(TAG, "HTTP status code 403: The server is refusing to provide the resource.");
break;
case 404:
ESP_LOGE(TAG, "HTTP status code 404: Resource not found on the server.");
break;
default:
ESP_LOGE(TAG, "Non-200 status code received: %d", additionalInfo);
break;
}
if (fwupdater_config->event_callback) {
callbackEvent.event_id = IAP_HTTPS_EVENT_UPGRADE_ERROR;
fwupdater_config->event_callback(&callbackEvent);
}
}
}
void iap_https_wifi_sta_event_callback(system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(iap_https_wifi_event_group, IAP_HTTPS_WIFI_STA_EVENT_GROUP_CONNECTED_FLAG);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
xEventGroupClearBits(iap_https_wifi_event_group, IAP_HTTPS_WIFI_STA_EVENT_GROUP_CONNECTED_FLAG);
break;
default:
break;
}
}