1
1
//
2
- // fwupdater_wifi_tls .c
2
+ // iap_https .c
3
3
// esp32-ota-https
4
4
//
5
5
// Updating the firmware over the air.
41
41
#include "wifi_tls.h"
42
42
#include "https_client.h"
43
43
#include "iap.h"
44
- #include "fwupdater_wifi_tls .h"
44
+ #include "iap_https .h"
45
45
46
46
47
47
#define TAG "fwup_wifi"
51
51
static struct wifi_tls_context_ * tls_context ;
52
52
53
53
// Module configuration.
54
- static fwupdater_wifi_tls_config_t * fwupdater_config ;
54
+ static iap_https_config_t * fwupdater_config ;
55
55
56
56
// The metadata request.
57
57
static http_request_t http_metadata_request ;
@@ -71,23 +71,23 @@ static int has_iap_session;
71
71
static int has_new_firmware ;
72
72
static int total_nof_bytes_received ;
73
73
74
- static void fwupdater_wifi_tls_periodic_check_timer_callback (TimerHandle_t xTimer );
75
- static void fwupdater_wifi_tls_task (void * pvParameter );
76
- static void fwupdater_wifi_tls_prepare_timer ();
77
- static void fwupdater_wifi_tls_trigger_processing ();
78
- static void fwupdater_wifi_tls_check_for_update ();
79
- static void fwupdater_wifi_download_image ();
74
+ static void iap_https_periodic_check_timer_callback (TimerHandle_t xTimer );
75
+ static void iap_https_task (void * pvParameter );
76
+ static void iap_https_prepare_timer ();
77
+ static void iap_https_trigger_processing ();
78
+ static void iap_https_check_for_update ();
79
+ static void iap_https_download_image ();
80
80
81
- http_continue_receiving_t fwupdater_metadata_headers_callback (struct http_request_ * request , int statusCode , int contentLength );
82
- http_continue_receiving_t fwupdater_metadata_body_callback (struct http_request_ * request , size_t bytesReceived );
83
- http_continue_receiving_t fwupdater_firmware_headers_callback (struct http_request_ * request , int statusCode , int contentLength );
84
- http_continue_receiving_t fwupdater_firmware_body_callback (struct http_request_ * request , size_t bytesReceived );
85
- void fwupdater_error_callback (struct http_request_ * request , http_error_t error , int additionalInfo );
81
+ http_continue_receiving_t iap_https_metadata_headers_callback (struct http_request_ * request , int statusCode , int contentLength );
82
+ http_continue_receiving_t iap_https_metadata_body_callback (struct http_request_ * request , size_t bytesReceived );
83
+ http_continue_receiving_t iap_https_firmware_headers_callback (struct http_request_ * request , int statusCode , int contentLength );
84
+ http_continue_receiving_t iap_https_firmware_body_callback (struct http_request_ * request , size_t bytesReceived );
85
+ void iap_https_error_callback (struct http_request_ * request , http_error_t error , int additionalInfo );
86
86
87
87
88
- int fwupdater_wifi_tls_init ( fwupdater_wifi_tls_config_t * config )
88
+ int iap_https_init ( iap_https_config_t * config )
89
89
{
90
- ESP_LOGD (TAG , "fwupdater_wifi_tls_init " );
90
+ ESP_LOGD (TAG , "iap_https_init " );
91
91
92
92
iap_init ();
93
93
@@ -112,69 +112,69 @@ int fwupdater_wifi_tls_init(fwupdater_wifi_tls_config_t *config)
112
112
http_metadata_request .response_mode = HTTP_WAIT_FOR_COMPLETE_BODY ;
113
113
http_metadata_request .response_buffer_len = 512 ;
114
114
http_metadata_request .response_buffer = malloc (http_metadata_request .response_buffer_len * sizeof (char ));
115
- http_metadata_request .error_callback = fwupdater_error_callback ;
116
- http_metadata_request .headers_callback = fwupdater_metadata_headers_callback ;
117
- http_metadata_request .body_callback = fwupdater_metadata_body_callback ;
115
+ http_metadata_request .error_callback = iap_https_error_callback ;
116
+ http_metadata_request .headers_callback = iap_https_metadata_headers_callback ;
117
+ http_metadata_request .body_callback = iap_https_metadata_body_callback ;
118
118
119
119
http_firmware_data_request .verb = HTTP_GET ;
120
120
http_firmware_data_request .host = config -> server_host_name ;
121
121
http_firmware_data_request .path = config -> server_firmware_path ;
122
122
http_firmware_data_request .response_mode = HTTP_STREAM_BODY ;
123
123
http_firmware_data_request .response_buffer_len = 4096 ;
124
124
http_firmware_data_request .response_buffer = malloc (http_firmware_data_request .response_buffer_len * sizeof (char ));
125
- http_firmware_data_request .error_callback = fwupdater_error_callback ;
126
- http_firmware_data_request .headers_callback = fwupdater_firmware_headers_callback ;
127
- http_firmware_data_request .body_callback = fwupdater_firmware_body_callback ;
125
+ http_firmware_data_request .error_callback = iap_https_error_callback ;
126
+ http_firmware_data_request .headers_callback = iap_https_firmware_headers_callback ;
127
+ http_firmware_data_request .body_callback = iap_https_firmware_body_callback ;
128
128
129
129
// Start our processing task.
130
130
131
131
event_group = xEventGroupCreate ();
132
132
133
- fwupdater_wifi_tls_prepare_timer ();
133
+ iap_https_prepare_timer ();
134
134
135
- xTaskCreate (& fwupdater_wifi_tls_task , "fwup_wifi_task" , 4096 , NULL , 1 , NULL );
135
+ xTaskCreate (& iap_https_task , "fwup_wifi_task" , 4096 , NULL , 1 , NULL );
136
136
137
137
return 0 ;
138
138
}
139
139
140
- int fwupdater_wifi_tls_check_now ()
140
+ int iap_https_check_now ()
141
141
{
142
- ESP_LOGD (TAG , "fwupdater_wifi_tls_check_now " );
143
- fwupdater_wifi_tls_trigger_processing ();
142
+ ESP_LOGD (TAG , "iap_https_check_now " );
143
+ iap_https_trigger_processing ();
144
144
return 0 ;
145
145
}
146
146
147
- int fwupdater_wifi_tls_update_in_progress ()
147
+ int iap_https_update_in_progress ()
148
148
{
149
149
return has_iap_session ;
150
150
}
151
151
152
- int fwupdater_wifi_tls_new_firmware_installed ()
152
+ int iap_https_new_firmware_installed ()
153
153
{
154
154
return has_new_firmware ;
155
155
}
156
156
157
- static void fwupdater_wifi_tls_periodic_check_timer_callback (TimerHandle_t xTimer )
157
+ static void iap_https_periodic_check_timer_callback (TimerHandle_t xTimer )
158
158
{
159
159
xEventGroupSetBits (event_group , FWUP_CHECK_FOR_UPDATE );
160
160
}
161
161
162
- static void fwupdater_wifi_tls_trigger_processing ()
162
+ static void iap_https_trigger_processing ()
163
163
{
164
- ESP_LOGD (TAG , "fwupdater_wifi_tls_trigger_processing : checking flag" );
164
+ ESP_LOGD (TAG , "iap_https_trigger_processing : checking flag" );
165
165
166
166
if (xEventGroupGetBits (event_group ) & FWUP_CHECK_FOR_UPDATE ) {
167
- ESP_LOGD (TAG , "fwupdater_wifi_tls_trigger_processing : flag is already set" );
167
+ ESP_LOGD (TAG , "iap_https_trigger_processing : flag is already set" );
168
168
return ;
169
169
}
170
170
171
- ESP_LOGD (TAG , "fwupdater_wifi_tls_trigger_processing : flag is not set, setting it" );
171
+ ESP_LOGD (TAG , "iap_https_trigger_processing : flag is not set, setting it" );
172
172
173
173
// Trigger processing in our task.
174
174
xEventGroupSetBits (event_group , FWUP_CHECK_FOR_UPDATE );
175
175
}
176
176
177
- static void fwupdater_wifi_tls_task (void * pvParameter )
177
+ static void iap_https_task (void * pvParameter )
178
178
{
179
179
ESP_LOGI (TAG , "Firmware updater task started." );
180
180
@@ -200,49 +200,49 @@ static void fwupdater_wifi_tls_task(void *pvParameter)
200
200
201
201
if (bits & FWUP_DOWNLOAD_IMAGE ) {
202
202
ESP_LOGI (TAG , "Firmware updater task will now download the new firmware image." );
203
- fwupdater_wifi_download_image ();
203
+ iap_https_download_image ();
204
204
xEventGroupClearBits (event_group , FWUP_DOWNLOAD_IMAGE );
205
205
206
206
} else if (bits & FWUP_CHECK_FOR_UPDATE ) {
207
207
ESP_LOGI (TAG , "Firmware updater task checking for firmware update." );
208
- fwupdater_wifi_tls_check_for_update ();
208
+ iap_https_check_for_update ();
209
209
210
210
// If periodic OTA update checks are enabled, re-start the timer.
211
211
// Clear the bit *after* resetting the timer to avoid the race condition
212
212
// where the timer could have elapsed during the update check and we would
213
213
// immediately check again.
214
214
215
- fwupdater_wifi_tls_prepare_timer ();
215
+ iap_https_prepare_timer ();
216
216
xEventGroupClearBits (event_group , FWUP_CHECK_FOR_UPDATE );
217
217
}
218
218
}
219
219
}
220
220
221
- static void fwupdater_wifi_tls_prepare_timer ()
221
+ static void iap_https_prepare_timer ()
222
222
{
223
223
// Make sure we have a timer if we need one and don't have one if we don't need one.
224
224
225
225
if (fwupdater_config -> polling_interval_s > 0 ) {
226
226
if (!check_for_updates_timer ) {
227
227
// We need a timer but don't have one. Create it.
228
228
BaseType_t autoReload = pdFALSE ;
229
- check_for_updates_timer = xTimerCreate ("fwup_periodic_check" , 1000 , autoReload , NULL , fwupdater_wifi_tls_periodic_check_timer_callback );
229
+ check_for_updates_timer = xTimerCreate ("fwup_periodic_check" , 1000 , autoReload , NULL , iap_https_periodic_check_timer_callback );
230
230
if (!check_for_updates_timer ) {
231
- ESP_LOGE (TAG , "fwupdater_wifi_tls_prepare_timer : failed to create the fwup_periodic_check timer!" );
231
+ ESP_LOGE (TAG , "iap_https_prepare_timer : failed to create the fwup_periodic_check timer!" );
232
232
return ;
233
233
}
234
234
}
235
235
236
236
// We need and have a timer, so make sure it uses the correct interval, then start it.
237
237
238
238
uint32_t timerMillisec = 1000 * fwupdater_config -> polling_interval_s ;
239
- ESP_LOGD (TAG , "fwupdater_wifi_tls_prepare_timer : timer interval = %d ms" , timerMillisec );
239
+ ESP_LOGD (TAG , "iap_https_prepare_timer : timer interval = %d ms" , timerMillisec );
240
240
TickType_t timerPeriod = pdMS_TO_TICKS (timerMillisec );
241
241
242
242
xTimerChangePeriod (check_for_updates_timer , timerPeriod , pdMS_TO_TICKS (5000 ));
243
243
244
244
if (pdFAIL == xTimerReset (check_for_updates_timer , pdMS_TO_TICKS (5000 ))) {
245
- ESP_LOGE (TAG , "fwupdater_wifi_tls_prepare_timer : failed to start the fwup_periodic_check timer!" );
245
+ ESP_LOGE (TAG , "iap_https_prepare_timer : failed to start the fwup_periodic_check timer!" );
246
246
}
247
247
248
248
return ;
@@ -256,28 +256,28 @@ static void fwupdater_wifi_tls_prepare_timer()
256
256
}
257
257
}
258
258
259
- static void fwupdater_wifi_tls_check_for_update ()
259
+ static void iap_https_check_for_update ()
260
260
{
261
- ESP_LOGD (TAG , "fwupdater_wifi_tls_check_for_update " );
261
+ ESP_LOGD (TAG , "iap_https_check_for_update " );
262
262
263
263
int tlsResult = wifi_tls_connect (tls_context );
264
264
if (tlsResult ) {
265
- ESP_LOGE (TAG , "fwupdater_wifi_tls_check_for_update : failed to initiate SSL/TLS connection; wifi_tls_connect returned %d" , tlsResult );
265
+ ESP_LOGE (TAG , "iap_https_check_for_update : failed to initiate SSL/TLS connection; wifi_tls_connect returned %d" , tlsResult );
266
266
return ;
267
267
}
268
268
269
269
ESP_LOGI (TAG , "Requesting firmware metadata from server." );
270
270
http_error_t httpResult = https_send_request (tls_context , & http_metadata_request );
271
271
if (httpResult != HTTP_SUCCESS ) {
272
- ESP_LOGE (TAG , "fwupdater_wifi_tls_check_for_update : failed to send HTTPS metadata request; https_send_request returned %d" , httpResult );
272
+ ESP_LOGE (TAG , "iap_https_check_for_update : failed to send HTTPS metadata request; https_send_request returned %d" , httpResult );
273
273
}
274
274
}
275
275
276
- static void fwupdater_wifi_download_image ()
276
+ static void iap_https_download_image ()
277
277
{
278
278
int tlsResult = wifi_tls_connect (tls_context );
279
279
if (tlsResult ) {
280
- ESP_LOGE (TAG , "fwupdater_metadata_body_callback : failed to initiate SSL/TLS connection; wifi_tls_connect returned %d" , tlsResult );
280
+ ESP_LOGE (TAG , "iap_https_download_image : failed to initiate SSL/TLS connection; wifi_tls_connect returned %d" , tlsResult );
281
281
}
282
282
283
283
// Make sure we open a new IAP session in the callback.
@@ -286,13 +286,13 @@ static void fwupdater_wifi_download_image()
286
286
ESP_LOGI (TAG , "Requesting firmware image '%s' from web server." , fwupdater_config -> server_firmware_path );
287
287
http_error_t httpResult = https_send_request (tls_context , & http_firmware_data_request );
288
288
if (httpResult != HTTP_SUCCESS ) {
289
- ESP_LOGE (TAG , "fwupdater_wifi_tls_execute_check : failed to send HTTPS firmware image request; https_send_request returned %d" , httpResult );
289
+ ESP_LOGE (TAG , "iap_https_download_image : failed to send HTTPS firmware image request; https_send_request returned %d" , httpResult );
290
290
}
291
291
}
292
292
293
- http_continue_receiving_t fwupdater_metadata_body_callback (struct http_request_ * request , size_t bytesReceived )
293
+ http_continue_receiving_t iap_https_metadata_body_callback (struct http_request_ * request , size_t bytesReceived )
294
294
{
295
- ESP_LOGD (TAG , "fwupdater_metadata_body_callback " );
295
+ ESP_LOGD (TAG , "iap_https_metadata_body_callback " );
296
296
297
297
// --- Process the metadata information ---
298
298
@@ -301,7 +301,7 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
301
301
if (!http_parse_key_value_int (request -> response_buffer , "INTERVAL=" , & intervalSeconds )) {
302
302
ESP_LOGD (TAG , "[INTERVAL=] '%d'" , intervalSeconds );
303
303
if (intervalSeconds != fwupdater_config -> polling_interval_s ) {
304
- ESP_LOGD (TAG , "fwupdater_metadata_body_callback : polling interval changed from %d s to %d s" ,
304
+ ESP_LOGD (TAG , "iap_https_metadata_body_callback : polling interval changed from %d s to %d s" ,
305
305
fwupdater_config -> polling_interval_s , intervalSeconds );
306
306
fwupdater_config -> polling_interval_s = intervalSeconds ;
307
307
}
@@ -311,7 +311,7 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
311
311
if (!http_parse_key_value_int (request -> response_buffer , "VERSION=" , & version )) {
312
312
ESP_LOGD (TAG , "[VERSION=] '%d'" , version );
313
313
} else {
314
- ESP_LOGW (TAG , "fwupdater_metadata_body_callback : firmware version not provided, skipping firmware update" );
314
+ ESP_LOGW (TAG , "iap_https_metadata_body_callback : firmware version not provided, skipping firmware update" );
315
315
return HTTP_STOP_RECEIVING ;
316
316
}
317
317
@@ -320,19 +320,19 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
320
320
ESP_LOGD (TAG , "[FILE=] '%s'" , fileName );
321
321
strncpy (fwupdater_config -> server_firmware_path , fileName , sizeof (fwupdater_config -> server_firmware_path ) / sizeof (char ));
322
322
} else {
323
- ESP_LOGW (TAG , "fwupdater_metadata_body_callback : firmware file name not provided, skipping firmware update" );
323
+ ESP_LOGW (TAG , "iap_https_metadata_body_callback : firmware file name not provided, skipping firmware update" );
324
324
return HTTP_STOP_RECEIVING ;
325
325
}
326
326
327
327
328
328
// --- Check if the version on the server is the same as the currently installed version ---
329
329
330
330
if (version == SOFTWARE_VERSION ) {
331
- ESP_LOGD (TAG , "fwupdater_metadata_body_callback : we're up-to-date!" );
331
+ ESP_LOGD (TAG , "iap_https_metadata_body_callback : we're up-to-date!" );
332
332
return HTTP_STOP_RECEIVING ;
333
333
}
334
334
335
- ESP_LOGD (TAG , "fwupdater_metadata_body_callback : our version is %d, the version on the server is %d" , SOFTWARE_VERSION , version );
335
+ ESP_LOGD (TAG , "iap_https_metadata_body_callback : our version is %d, the version on the server is %d" , SOFTWARE_VERSION , version );
336
336
337
337
// --- Request the firmware image ---
338
338
@@ -341,20 +341,20 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
341
341
return HTTP_STOP_RECEIVING ;
342
342
}
343
343
344
- http_continue_receiving_t fwupdater_firmware_body_callback (struct http_request_ * request , size_t bytesReceived )
344
+ http_continue_receiving_t iap_https_firmware_body_callback (struct http_request_ * request , size_t bytesReceived )
345
345
{
346
- ESP_LOGD (TAG , "fwupdater_firmware_body_callback " );
346
+ ESP_LOGD (TAG , "iap_https_firmware_body_callback " );
347
347
348
348
// The first time we receive the callback, we neet to start the IAP session.
349
349
if (!has_iap_session ) {
350
- ESP_LOGD (TAG , "fwupdater_firmware_body_callback : starting IPA session." );
350
+ ESP_LOGD (TAG , "iap_https_firmware_body_callback : starting IPA session." );
351
351
iap_err_t result = iap_begin ();
352
352
if (result == IAP_ERR_SESSION_ALREADY_OPEN ) {
353
353
iap_abort ();
354
354
result = iap_begin ();
355
355
}
356
356
if (result != IAP_OK ) {
357
- ESP_LOGE (TAG , "fwupdater_firmware_body_callback : iap_begin failed (%d)!" , result );
357
+ ESP_LOGE (TAG , "iap_https_firmware_body_callback : iap_begin failed (%d)!" , result );
358
358
return HTTP_STOP_RECEIVING ;
359
359
}
360
360
total_nof_bytes_received = 0 ;
@@ -366,7 +366,7 @@ http_continue_receiving_t fwupdater_firmware_body_callback(struct http_request_
366
366
iap_err_t result = iap_write ((uint8_t * )request -> response_buffer , bytesReceived );
367
367
total_nof_bytes_received += bytesReceived ;
368
368
if (result != IAP_OK ) {
369
- ESP_LOGE (TAG , "fwupdater_firmware_body_callback : write failed (%d), aborting firmware update!" , result );
369
+ ESP_LOGE (TAG , "iap_https_firmware_body_callback : write failed (%d), aborting firmware update!" , result );
370
370
iap_abort ();
371
371
return HTTP_STOP_RECEIVING ;
372
372
}
@@ -376,13 +376,13 @@ http_continue_receiving_t fwupdater_firmware_body_callback(struct http_request_
376
376
// After all data has been received, we get one last callback (with bytesReceived == 0).
377
377
// If this happens, we need to finish the IAP session and, if configured, reboot the device.
378
378
379
- ESP_LOGD (TAG , "fwupdater_firmware_body_callback : all data received (%d bytes), closing session" , total_nof_bytes_received );
379
+ ESP_LOGD (TAG , "iap_https_firmware_body_callback : all data received (%d bytes), closing session" , total_nof_bytes_received );
380
380
has_iap_session = 0 ;
381
381
382
382
if (total_nof_bytes_received > 0 ) {
383
383
iap_err_t result = iap_commit ();
384
384
if (result != IAP_OK ) {
385
- ESP_LOGE (TAG , "fwupdater_firmware_body_callback : closing the session has failed (%d)!" , result );
385
+ ESP_LOGE (TAG , "iap_https_firmware_body_callback : closing the session has failed (%d)!" , result );
386
386
}
387
387
388
388
has_new_firmware = 1 ;
@@ -394,28 +394,28 @@ http_continue_receiving_t fwupdater_firmware_body_callback(struct http_request_
394
394
}
395
395
396
396
} else {
397
- ESP_LOGE (TAG , "fwupdater_firmware_body_callback : something's not OK - the new firmware image is empty!" );
397
+ ESP_LOGE (TAG , "iap_https_firmware_body_callback : something's not OK - the new firmware image is empty!" );
398
398
iap_abort ();
399
399
}
400
400
401
401
return HTTP_STOP_RECEIVING ;
402
402
}
403
403
404
- http_continue_receiving_t fwupdater_metadata_headers_callback (struct http_request_ * request , int statusCode , int contentLength )
404
+ http_continue_receiving_t iap_https_metadata_headers_callback (struct http_request_ * request , int statusCode , int contentLength )
405
405
{
406
- ESP_LOGD (TAG , "fwupdater_metadata_headers_callback " );
406
+ ESP_LOGD (TAG , "iap_https_metadata_headers_callback " );
407
407
return HTTP_CONTINUE_RECEIVING ;
408
408
}
409
409
410
- http_continue_receiving_t fwupdater_firmware_headers_callback (struct http_request_ * request , int statusCode , int contentLength )
410
+ http_continue_receiving_t iap_https_firmware_headers_callback (struct http_request_ * request , int statusCode , int contentLength )
411
411
{
412
- ESP_LOGD (TAG , "fwupdater_firmware_headers_callback " );
412
+ ESP_LOGD (TAG , "iap_https_firmware_headers_callback " );
413
413
return HTTP_CONTINUE_RECEIVING ;
414
414
}
415
415
416
- void fwupdater_error_callback (struct http_request_ * request , http_error_t error , int additionalInfo )
416
+ void iap_https_error_callback (struct http_request_ * request , http_error_t error , int additionalInfo )
417
417
{
418
- ESP_LOGE (TAG , "fwupdater_error_callback : error=%d additionalInfo=%d" , error , additionalInfo );
418
+ ESP_LOGE (TAG , "iap_https_error_callback : error=%d additionalInfo=%d" , error , additionalInfo );
419
419
420
420
if (error == HTTP_ERR_NON_200_STATUS_CODE ) {
421
421
switch (additionalInfo ) {
0 commit comments