1919#include "freertos/FreeRTOS.h"
2020#include "freertos/task.h"
2121
22+ #include "os/core.h"
2223#include "os/firmware.h"
2324
2425#define BT_SPP_TAG "bt_spp"
@@ -32,13 +33,11 @@ static long data_num = 0;
3233static const esp_spp_sec_t sec_mask = ESP_SPP_SEC_AUTHENTICATE ;
3334static const esp_spp_role_t role_slave = ESP_SPP_ROLE_SLAVE ;
3435
35- static uint8_t first_time = 1 ;
3636static uint8_t ota_running = 0 ;
3737static long image_length = 0 ;
3838
3939static const esp_partition_t * update_partition = NULL ;
4040static esp_ota_handle_t update_handle = 0 ;
41- static long binary_file_length = 0 ;
4241
4342static void bt_spp_print_speed (void )
4443{
@@ -64,6 +63,12 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
6463 break ;
6564 case ESP_SPP_CLOSE_EVT :
6665 ESP_LOGI (BT_SPP_TAG , "SPP connection state: disconnected" );
66+ if (ota_running == 1 ) {
67+ esp_ota_end (update_handle );
68+ xEventGroupSetBits (user_event_group , KEY_SCAN_BIT );
69+ ota_running = 0 ;
70+ image_length = 0 ;
71+ }
6772 break ;
6873 case ESP_SPP_START_EVT :
6974 break ;
@@ -90,20 +95,10 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
9095 ESP_LOGI (BT_SPP_TAG , "GET command: FW+UPD:%ld" , image_length );
9196
9297 if (image_length != 0 ) {
93- ota_running = 1 ;
94- gettimeofday (& time_old , NULL );
95-
9698 uint8_t rsp_str [] = "OK\r\n" ;
9799 esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
98- } else {
99- uint8_t rsp_str [] = "ERROR\r\n" ;
100- esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
101- }
102- }
103- } else {
104- if (binary_file_length != image_length ) {
105- if (first_time ) {
106- first_time = 0 ;
100+
101+ xEventGroupClearBits (user_event_group , KEY_SCAN_BIT );
107102
108103 update_partition = esp_ota_get_next_update_partition (NULL );
109104 ESP_LOGI (BT_OTA_TAG , "writing to partition subtype %d at offset 0x%x" ,
@@ -116,18 +111,25 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
116111 goto exit ;
117112 }
118113
119- binary_file_length = 0 ;
120- }
121- esp_err_t err = esp_ota_write (update_handle , (const void * )param -> data_ind .data , param -> data_ind .len );
122- if (err != ESP_OK ) {
123- ESP_LOGE (BT_OTA_TAG , "esp_ota_write failed (%s)" , esp_err_to_name (err ));
124- goto exit ;
114+ ota_running = 1 ;
115+ data_num = 0 ;
116+
117+ gettimeofday (& time_old , NULL );
118+ } else {
119+ uint8_t rsp_str [] = "ERROR\r\n" ;
120+ esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
125121 }
126- binary_file_length += param -> data_ind .len ;
127- ESP_LOGI (BT_OTA_TAG , "have written image length %ld" , binary_file_length );
128- } else {
129- data_num = binary_file_length ;
122+ }
123+ } else {
124+ esp_err_t err = esp_ota_write (update_handle , (const void * )param -> data_ind .data , param -> data_ind .len );
125+ if (err != ESP_OK ) {
126+ ESP_LOGE (BT_OTA_TAG , "esp_ota_write failed (%s)" , esp_err_to_name (err ));
127+ goto exit ;
128+ }
129+ data_num += param -> data_ind .len ;
130+ ESP_LOGD (BT_OTA_TAG , "have written image length %ld" , data_num );
130131
132+ if (data_num == image_length ) {
131133 if (esp_ota_end (update_handle ) != ESP_OK ) {
132134 ESP_LOGE (BT_OTA_TAG , "esp_ota_end failed" );
133135 goto exit ;
@@ -139,8 +141,10 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
139141 }
140142 gettimeofday (& time_new , NULL );
141143 bt_spp_print_speed ();
144+ uint8_t rsp_str [] = "DONE\r\n" ;
145+ esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
146+ xEventGroupSetBits (user_event_group , KEY_SCAN_BIT );
142147exit :
143- first_time = 1 ;
144148 ota_running = 0 ;
145149 image_length = 0 ;
146150 }
0 commit comments