2525
2626#define TAG "http_app_ota"
2727
28- static uint8_t ota_first_time = 1 ;
28+ static uint32_t data_length = 0 ;
29+
30+ static const esp_partition_t * update_partition = NULL ;
31+ static esp_ota_handle_t update_handle = 0 ;
2932
3033esp_err_t http_app_ota_event_handler (esp_http_client_event_t * evt )
3134{
32- static const esp_partition_t * update_partition = NULL ;
33- static esp_ota_handle_t update_handle = 0 ;
34- static long binary_file_length = 0 ;
35-
3635 switch (evt -> event_id ) {
3736 case HTTP_EVENT_ERROR :
3837 break ;
@@ -44,49 +43,56 @@ esp_err_t http_app_ota_event_handler(esp_http_client_event_t *evt)
4443 break ;
4544 case HTTP_EVENT_ON_DATA : {
4645 if (evt -> data_len ) {
47- if (ota_first_time ) {
48- ota_first_time = 0 ;
49-
50- led_set_mode (3 );
46+ if (!update_handle ) {
47+ led_set_mode (7 );
5148 gui_show_image (8 );
5249
5350 update_partition = esp_ota_get_next_update_partition (NULL );
54- ESP_LOGI (TAG , "writing to partition subtype %d at offset 0x%x" ,
55- update_partition -> subtype , update_partition -> address );
56- assert (update_partition != NULL );
51+ if (update_partition != NULL ) {
52+ ESP_LOGI (TAG , "writing to partition subtype %d at offset 0x%x" ,
53+ update_partition -> subtype , update_partition -> address );
54+ } else {
55+ ESP_LOGE (TAG , "no ota partition to write" );
56+ goto exit ;
57+ }
5758
5859 esp_err_t err = esp_ota_begin (update_partition , OTA_SIZE_UNKNOWN , & update_handle );
5960 if (err != ESP_OK ) {
6061 ESP_LOGE (TAG , "esp_ota_begin failed (%s)" , esp_err_to_name (err ));
6162 goto exit ;
6263 }
6364
64- binary_file_length = 0 ;
65+ data_length = 0 ;
6566 }
67+
6668 esp_err_t err = esp_ota_write (update_handle , (const void * )evt -> data , evt -> data_len );
6769 if (err != ESP_OK ) {
6870 ESP_LOGE (TAG , "esp_ota_write failed (%s)" , esp_err_to_name (err ));
6971 goto exit ;
7072 }
71- binary_file_length += evt -> data_len ;
72- ESP_LOGD ( TAG , "have written image length %ld" , binary_file_length ) ;
73+
74+ data_length += evt -> data_len ;
7375 }
7476 break ;
7577 }
7678 case HTTP_EVENT_ON_FINISH : {
77- if (binary_file_length != 0 ) {
79+ if (data_length != 0 ) {
7880 if (esp_ota_end (update_handle ) != ESP_OK ) {
7981 ESP_LOGE (TAG , "esp_ota_end failed" );
8082 goto exit ;
8183 }
84+
8285 esp_err_t err = esp_ota_set_boot_partition (update_partition );
8386 if (err != ESP_OK ) {
8487 ESP_LOGE (TAG , "esp_ota_set_boot_partition failed (%s)" , esp_err_to_name (err ));
8588 goto exit ;
8689 }
90+
8791 ESP_LOGW (TAG , "prepare to restart system" );
92+
8893 gui_show_image (4 );
8994 vTaskDelay (2000 / portTICK_RATE_MS );
95+
9096 esp_restart ();
9197 } else {
9298 ESP_LOGI (TAG , "no update found" );
@@ -127,6 +133,6 @@ void http_app_check_for_updates(void)
127133 if ((uxBits & HTTP_APP_OTA_READY_BIT ) == 0 ) {
128134 xEventGroupClearBits (user_event_group , HTTP_APP_OTA_RUN_BIT );
129135 }
130- ota_first_time = 1 ;
136+ update_handle = 0 ;
131137#endif
132138}
0 commit comments