1515
1616static const char * TAG = "ota_update" ;
1717
18- esp_err_t ota_update_perform (const char * url , bool reboot_on_success )
18+ static void emit_event (ota_update_event_cb_t callback ,
19+ void * user_data ,
20+ ota_update_event_type_t type ,
21+ size_t bytes_downloaded ,
22+ size_t image_size ,
23+ esp_err_t error )
24+ {
25+ if (callback == NULL )
26+ {
27+ return ;
28+ }
29+ ota_update_event_t event = {
30+ .type = type ,
31+ .bytes_downloaded = bytes_downloaded ,
32+ .image_size = image_size ,
33+ .error = error ,
34+ };
35+ callback (& event , user_data );
36+ }
37+
38+ esp_err_t ota_update_perform_with_callback (const char * url ,
39+ bool reboot_on_success ,
40+ ota_update_event_cb_t callback ,
41+ void * user_data )
1942{
2043 if (!url )
2144 {
45+ emit_event (callback , user_data , OTA_UPDATE_EVENT_ERROR , 0U , 0U , ESP_ERR_INVALID_ARG );
2246 return ESP_ERR_INVALID_ARG ;
2347 }
2448
49+ emit_event (callback , user_data , OTA_UPDATE_EVENT_START , 0U , 0U , ESP_OK );
50+
2551 esp_http_client_config_t http_config = {
2652 .url = url ,
2753 .timeout_ms = 10000 ,
@@ -37,11 +63,16 @@ esp_err_t ota_update_perform(const char* url, bool reboot_on_success)
3763 if (err != ESP_OK )
3864 {
3965 ESP_LOGE (TAG , "OTA begin failed: 0x%x" , (unsigned int )err );
66+ emit_event (callback , user_data , OTA_UPDATE_EVENT_ERROR , 0U , 0U , err );
4067 return err ;
4168 }
4269
4370 while ((err = esp_https_ota_perform (ota_handle )) == ESP_ERR_HTTPS_OTA_IN_PROGRESS )
4471 {
72+ size_t image_size = esp_https_ota_get_image_size (ota_handle );
73+ size_t bytes_downloaded = esp_https_ota_get_image_len_read (ota_handle );
74+ emit_event (
75+ callback , user_data , OTA_UPDATE_EVENT_PROGRESS , bytes_downloaded , image_size , ESP_OK );
4576 vTaskDelay (pdMS_TO_TICKS (100 ));
4677 }
4778
@@ -52,19 +83,52 @@ esp_err_t ota_update_perform(const char* url, bool reboot_on_success)
5283 else
5384 {
5485 ESP_LOGE (TAG , "OTA perform failed: 0x%x" , (unsigned int )err );
86+ emit_event (callback ,
87+ user_data ,
88+ OTA_UPDATE_EVENT_ERROR ,
89+ esp_https_ota_get_image_len_read (ota_handle ),
90+ esp_https_ota_get_image_size (ota_handle ),
91+ err );
5592 }
5693
5794 esp_err_t finish_err = esp_https_ota_finish (ota_handle );
5895 if (finish_err != ESP_OK )
5996 {
6097 ESP_LOGE (TAG , "OTA finish failed: 0x%x" , (unsigned int )finish_err );
98+ emit_event (callback ,
99+ user_data ,
100+ OTA_UPDATE_EVENT_ERROR ,
101+ esp_https_ota_get_image_len_read (ota_handle ),
102+ esp_https_ota_get_image_size (ota_handle ),
103+ finish_err );
61104 return finish_err ;
62105 }
63106
64107 if (err == ESP_OK && reboot_on_success )
65108 {
66109 ESP_LOGI (TAG , "Rebooting after OTA update" );
110+ emit_event (callback ,
111+ user_data ,
112+ OTA_UPDATE_EVENT_COMPLETED ,
113+ esp_https_ota_get_image_len_read (ota_handle ),
114+ esp_https_ota_get_image_size (ota_handle ),
115+ ESP_OK );
67116 esp_restart ();
68117 }
118+ else if (err == ESP_OK )
119+ {
120+ emit_event (callback ,
121+ user_data ,
122+ OTA_UPDATE_EVENT_COMPLETED ,
123+ esp_https_ota_get_image_len_read (ota_handle ),
124+ esp_https_ota_get_image_size (ota_handle ),
125+ ESP_OK );
126+ }
127+
69128 return err ;
70129}
130+
131+ esp_err_t ota_update_perform (const char * url , bool reboot_on_success )
132+ {
133+ return ota_update_perform_with_callback (url , reboot_on_success , NULL , NULL );
134+ }
0 commit comments