11//
2- // fwupdater_wifi_tls .c
2+ // iap_https .c
33// esp32-ota-https
44//
55// Updating the firmware over the air.
4141#include "wifi_tls.h"
4242#include "https_client.h"
4343#include "iap.h"
44- #include "fwupdater_wifi_tls .h"
44+ #include "iap_https .h"
4545
4646
4747#define TAG "fwup_wifi"
5151static struct wifi_tls_context_ * tls_context ;
5252
5353// Module configuration.
54- static fwupdater_wifi_tls_config_t * fwupdater_config ;
54+ static iap_https_config_t * fwupdater_config ;
5555
5656// The metadata request.
5757static http_request_t http_metadata_request ;
@@ -71,23 +71,23 @@ static int has_iap_session;
7171static int has_new_firmware ;
7272static int total_nof_bytes_received ;
7373
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 ();
8080
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 );
8686
8787
88- int fwupdater_wifi_tls_init ( fwupdater_wifi_tls_config_t * config )
88+ int iap_https_init ( iap_https_config_t * config )
8989{
90- ESP_LOGD (TAG , "fwupdater_wifi_tls_init " );
90+ ESP_LOGD (TAG , "iap_https_init " );
9191
9292 iap_init ();
9393
@@ -112,69 +112,69 @@ int fwupdater_wifi_tls_init(fwupdater_wifi_tls_config_t *config)
112112 http_metadata_request .response_mode = HTTP_WAIT_FOR_COMPLETE_BODY ;
113113 http_metadata_request .response_buffer_len = 512 ;
114114 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 ;
118118
119119 http_firmware_data_request .verb = HTTP_GET ;
120120 http_firmware_data_request .host = config -> server_host_name ;
121121 http_firmware_data_request .path = config -> server_firmware_path ;
122122 http_firmware_data_request .response_mode = HTTP_STREAM_BODY ;
123123 http_firmware_data_request .response_buffer_len = 4096 ;
124124 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 ;
128128
129129 // Start our processing task.
130130
131131 event_group = xEventGroupCreate ();
132132
133- fwupdater_wifi_tls_prepare_timer ();
133+ iap_https_prepare_timer ();
134134
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 );
136136
137137 return 0 ;
138138}
139139
140- int fwupdater_wifi_tls_check_now ()
140+ int iap_https_check_now ()
141141{
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 ();
144144 return 0 ;
145145}
146146
147- int fwupdater_wifi_tls_update_in_progress ()
147+ int iap_https_update_in_progress ()
148148{
149149 return has_iap_session ;
150150}
151151
152- int fwupdater_wifi_tls_new_firmware_installed ()
152+ int iap_https_new_firmware_installed ()
153153{
154154 return has_new_firmware ;
155155}
156156
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 )
158158{
159159 xEventGroupSetBits (event_group , FWUP_CHECK_FOR_UPDATE );
160160}
161161
162- static void fwupdater_wifi_tls_trigger_processing ()
162+ static void iap_https_trigger_processing ()
163163{
164- ESP_LOGD (TAG , "fwupdater_wifi_tls_trigger_processing : checking flag" );
164+ ESP_LOGD (TAG , "iap_https_trigger_processing : checking flag" );
165165
166166 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" );
168168 return ;
169169 }
170170
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" );
172172
173173 // Trigger processing in our task.
174174 xEventGroupSetBits (event_group , FWUP_CHECK_FOR_UPDATE );
175175}
176176
177- static void fwupdater_wifi_tls_task (void * pvParameter )
177+ static void iap_https_task (void * pvParameter )
178178{
179179 ESP_LOGI (TAG , "Firmware updater task started." );
180180
@@ -200,49 +200,49 @@ static void fwupdater_wifi_tls_task(void *pvParameter)
200200
201201 if (bits & FWUP_DOWNLOAD_IMAGE ) {
202202 ESP_LOGI (TAG , "Firmware updater task will now download the new firmware image." );
203- fwupdater_wifi_download_image ();
203+ iap_https_download_image ();
204204 xEventGroupClearBits (event_group , FWUP_DOWNLOAD_IMAGE );
205205
206206 } else if (bits & FWUP_CHECK_FOR_UPDATE ) {
207207 ESP_LOGI (TAG , "Firmware updater task checking for firmware update." );
208- fwupdater_wifi_tls_check_for_update ();
208+ iap_https_check_for_update ();
209209
210210 // If periodic OTA update checks are enabled, re-start the timer.
211211 // Clear the bit *after* resetting the timer to avoid the race condition
212212 // where the timer could have elapsed during the update check and we would
213213 // immediately check again.
214214
215- fwupdater_wifi_tls_prepare_timer ();
215+ iap_https_prepare_timer ();
216216 xEventGroupClearBits (event_group , FWUP_CHECK_FOR_UPDATE );
217217 }
218218 }
219219}
220220
221- static void fwupdater_wifi_tls_prepare_timer ()
221+ static void iap_https_prepare_timer ()
222222{
223223 // Make sure we have a timer if we need one and don't have one if we don't need one.
224224
225225 if (fwupdater_config -> polling_interval_s > 0 ) {
226226 if (!check_for_updates_timer ) {
227227 // We need a timer but don't have one. Create it.
228228 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 );
230230 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!" );
232232 return ;
233233 }
234234 }
235235
236236 // We need and have a timer, so make sure it uses the correct interval, then start it.
237237
238238 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 );
240240 TickType_t timerPeriod = pdMS_TO_TICKS (timerMillisec );
241241
242242 xTimerChangePeriod (check_for_updates_timer , timerPeriod , pdMS_TO_TICKS (5000 ));
243243
244244 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!" );
246246 }
247247
248248 return ;
@@ -256,28 +256,28 @@ static void fwupdater_wifi_tls_prepare_timer()
256256 }
257257}
258258
259- static void fwupdater_wifi_tls_check_for_update ()
259+ static void iap_https_check_for_update ()
260260{
261- ESP_LOGD (TAG , "fwupdater_wifi_tls_check_for_update " );
261+ ESP_LOGD (TAG , "iap_https_check_for_update " );
262262
263263 int tlsResult = wifi_tls_connect (tls_context );
264264 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 );
266266 return ;
267267 }
268268
269269 ESP_LOGI (TAG , "Requesting firmware metadata from server." );
270270 http_error_t httpResult = https_send_request (tls_context , & http_metadata_request );
271271 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 );
273273 }
274274}
275275
276- static void fwupdater_wifi_download_image ()
276+ static void iap_https_download_image ()
277277{
278278 int tlsResult = wifi_tls_connect (tls_context );
279279 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 );
281281 }
282282
283283 // Make sure we open a new IAP session in the callback.
@@ -286,13 +286,13 @@ static void fwupdater_wifi_download_image()
286286 ESP_LOGI (TAG , "Requesting firmware image '%s' from web server." , fwupdater_config -> server_firmware_path );
287287 http_error_t httpResult = https_send_request (tls_context , & http_firmware_data_request );
288288 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 );
290290 }
291291}
292292
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 )
294294{
295- ESP_LOGD (TAG , "fwupdater_metadata_body_callback " );
295+ ESP_LOGD (TAG , "iap_https_metadata_body_callback " );
296296
297297 // --- Process the metadata information ---
298298
@@ -301,7 +301,7 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
301301 if (!http_parse_key_value_int (request -> response_buffer , "INTERVAL=" , & intervalSeconds )) {
302302 ESP_LOGD (TAG , "[INTERVAL=] '%d'" , intervalSeconds );
303303 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" ,
305305 fwupdater_config -> polling_interval_s , intervalSeconds );
306306 fwupdater_config -> polling_interval_s = intervalSeconds ;
307307 }
@@ -311,7 +311,7 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
311311 if (!http_parse_key_value_int (request -> response_buffer , "VERSION=" , & version )) {
312312 ESP_LOGD (TAG , "[VERSION=] '%d'" , version );
313313 } 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" );
315315 return HTTP_STOP_RECEIVING ;
316316 }
317317
@@ -320,19 +320,19 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
320320 ESP_LOGD (TAG , "[FILE=] '%s'" , fileName );
321321 strncpy (fwupdater_config -> server_firmware_path , fileName , sizeof (fwupdater_config -> server_firmware_path ) / sizeof (char ));
322322 } 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" );
324324 return HTTP_STOP_RECEIVING ;
325325 }
326326
327327
328328 // --- Check if the version on the server is the same as the currently installed version ---
329329
330330 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!" );
332332 return HTTP_STOP_RECEIVING ;
333333 }
334334
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 );
336336
337337 // --- Request the firmware image ---
338338
@@ -341,20 +341,20 @@ http_continue_receiving_t fwupdater_metadata_body_callback(struct http_request_
341341 return HTTP_STOP_RECEIVING ;
342342}
343343
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 )
345345{
346- ESP_LOGD (TAG , "fwupdater_firmware_body_callback " );
346+ ESP_LOGD (TAG , "iap_https_firmware_body_callback " );
347347
348348 // The first time we receive the callback, we neet to start the IAP session.
349349 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." );
351351 iap_err_t result = iap_begin ();
352352 if (result == IAP_ERR_SESSION_ALREADY_OPEN ) {
353353 iap_abort ();
354354 result = iap_begin ();
355355 }
356356 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 );
358358 return HTTP_STOP_RECEIVING ;
359359 }
360360 total_nof_bytes_received = 0 ;
@@ -366,7 +366,7 @@ http_continue_receiving_t fwupdater_firmware_body_callback(struct http_request_
366366 iap_err_t result = iap_write ((uint8_t * )request -> response_buffer , bytesReceived );
367367 total_nof_bytes_received += bytesReceived ;
368368 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 );
370370 iap_abort ();
371371 return HTTP_STOP_RECEIVING ;
372372 }
@@ -376,13 +376,13 @@ http_continue_receiving_t fwupdater_firmware_body_callback(struct http_request_
376376 // After all data has been received, we get one last callback (with bytesReceived == 0).
377377 // If this happens, we need to finish the IAP session and, if configured, reboot the device.
378378
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 );
380380 has_iap_session = 0 ;
381381
382382 if (total_nof_bytes_received > 0 ) {
383383 iap_err_t result = iap_commit ();
384384 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 );
386386 }
387387
388388 has_new_firmware = 1 ;
@@ -394,28 +394,28 @@ http_continue_receiving_t fwupdater_firmware_body_callback(struct http_request_
394394 }
395395
396396 } 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!" );
398398 iap_abort ();
399399 }
400400
401401 return HTTP_STOP_RECEIVING ;
402402}
403403
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 )
405405{
406- ESP_LOGD (TAG , "fwupdater_metadata_headers_callback " );
406+ ESP_LOGD (TAG , "iap_https_metadata_headers_callback " );
407407 return HTTP_CONTINUE_RECEIVING ;
408408}
409409
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 )
411411{
412- ESP_LOGD (TAG , "fwupdater_firmware_headers_callback " );
412+ ESP_LOGD (TAG , "iap_https_firmware_headers_callback " );
413413 return HTTP_CONTINUE_RECEIVING ;
414414}
415415
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 )
417417{
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 );
419419
420420 if (error == HTTP_ERR_NON_200_STATUS_CODE ) {
421421 switch (additionalInfo ) {
0 commit comments