@@ -39,6 +39,19 @@ static long image_length = 0;
39
39
static const esp_partition_t * update_partition = NULL ;
40
40
static esp_ota_handle_t update_handle = 0 ;
41
41
42
+ static const char fw_cmd [][24 ] = {
43
+ "FW+RST\r\n" , // Reset Device
44
+ "FW+VER?\r\n" , // Get Firmware Version
45
+ "FW+UPD:%ld\r\n" // Update Device Firmware
46
+ };
47
+
48
+ static const char rsp_str [][24 ] = {
49
+ "OK\r\n" , // OK
50
+ "DONE\r\n" , // Done
51
+ "ERROR\r\n" , // Error
52
+ "RECV:%ld/%ld\r\n" // Receive Progress
53
+ };
54
+
42
55
static void bt_spp_print_speed (void )
43
56
{
44
57
float time_old_s = time_old .tv_sec + time_old .tv_usec / 1000000.0 ;
@@ -76,27 +89,26 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
76
89
break ;
77
90
case ESP_SPP_DATA_IND_EVT :
78
91
if (ota_running == 0 ) {
79
- if (strncmp ("FW+RST\r\n" , (const char * )param -> data_ind .data , param -> data_ind . len ) == 0 ) {
92
+ if (strncmp (fw_cmd [ 0 ] , (const char * )param -> data_ind .data , strlen ( fw_cmd [ 0 ]) ) == 0 ) {
80
93
ESP_LOGI (BT_SPP_TAG , "GET command: FW+RST" );
81
94
82
95
esp_restart ();
83
- } else if (strncmp ("FW+VER?\r\n" , (const char * )param -> data_ind .data , param -> data_ind . len ) == 0 ) {
96
+ } else if (strncmp (fw_cmd [ 1 ] , (const char * )param -> data_ind .data , strlen ( fw_cmd [ 1 ]) ) == 0 ) {
84
97
ESP_LOGI (BT_SPP_TAG , "GET command: FW+VER?" );
85
98
86
- uint8_t rsp_len = strlen (firmware_get_version ()) + 2 ;
87
- uint8_t * rsp_str = malloc (rsp_len * sizeof (uint8_t ));
88
- strncpy (( char * ) rsp_str , firmware_get_version (), rsp_len - 2 );
89
- rsp_str [ rsp_len - 2 ] = '\r' ;
90
- rsp_str [ rsp_len - 1 ] = '\n' ;
99
+ uint8_t str_len = strlen (firmware_get_version ()) + 2 ;
100
+ char * str_buf = malloc (str_len * sizeof (char ));
101
+ strncpy (str_buf , firmware_get_version (), str_len - 2 );
102
+ str_buf [ str_len - 2 ] = '\r' ;
103
+ str_buf [ str_len - 1 ] = '\n' ;
91
104
92
- esp_spp_write (param -> write .handle , rsp_len , rsp_str );
93
- } else if (strncmp ("FW+UPD:" , (const char * )param -> data_ind .data , 7 ) == 0 ) {
94
- sscanf ((const char * )param -> data_ind .data , "FW+UPD:%ld\r\n" , & image_length );
105
+ esp_spp_write (param -> write .handle , str_len , ( uint8_t * ) str_buf );
106
+ } else if (strncmp (fw_cmd [ 2 ] , (const char * )param -> data_ind .data , 7 ) == 0 ) {
107
+ sscanf ((const char * )param -> data_ind .data , fw_cmd [ 2 ] , & image_length );
95
108
ESP_LOGI (BT_SPP_TAG , "GET command: FW+UPD:%ld" , image_length );
96
109
97
110
if (image_length != 0 ) {
98
- uint8_t rsp_str [] = "OK\r\n" ;
99
- esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
111
+ esp_spp_write (param -> write .handle , strlen (rsp_str [0 ]), (uint8_t * )rsp_str [0 ]);
100
112
101
113
xEventGroupClearBits (user_event_group , KEY_SCAN_BIT );
102
114
@@ -116,8 +128,7 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
116
128
117
129
gettimeofday (& time_old , NULL );
118
130
} else {
119
- uint8_t rsp_str [] = "ERROR\r\n" ;
120
- esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
131
+ esp_spp_write (param -> write .handle , strlen (rsp_str [2 ]), (uint8_t * )rsp_str [2 ]);
121
132
}
122
133
}
123
134
} else {
@@ -129,6 +140,10 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
129
140
data_num += param -> data_ind .len ;
130
141
ESP_LOGD (BT_OTA_TAG , "have written image length %ld" , data_num );
131
142
143
+ char str_buf [24 ] = {0 };
144
+ snprintf (str_buf , sizeof (str_buf ), rsp_str [3 ], data_num , image_length );
145
+ esp_spp_write (param -> write .handle , strlen (str_buf ), (uint8_t * )str_buf );
146
+
132
147
if (data_num == image_length ) {
133
148
if (esp_ota_end (update_handle ) != ESP_OK ) {
134
149
ESP_LOGE (BT_OTA_TAG , "esp_ota_end failed" );
@@ -141,8 +156,7 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
141
156
}
142
157
gettimeofday (& time_new , NULL );
143
158
bt_spp_print_speed ();
144
- uint8_t rsp_str [] = "DONE\r\n" ;
145
- esp_spp_write (param -> write .handle , sizeof (rsp_str ), rsp_str );
159
+ esp_spp_write (param -> write .handle , strlen (rsp_str [1 ]), (uint8_t * )rsp_str [1 ]);
146
160
xEventGroupSetBits (user_event_group , KEY_SCAN_BIT );
147
161
exit :
148
162
ota_running = 0 ;
0 commit comments