Skip to content

Commit 0184285

Browse files
committed
bt_app_spp: fix unexpected FW+RST & add echo during the FW+UPD
1 parent 6aa2b55 commit 0184285

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

main/src/user/bt_app_spp.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ static long image_length = 0;
3939
static const esp_partition_t *update_partition = NULL;
4040
static esp_ota_handle_t update_handle = 0;
4141

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+
4255
static void bt_spp_print_speed(void)
4356
{
4457
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)
7689
break;
7790
case ESP_SPP_DATA_IND_EVT:
7891
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) {
8093
ESP_LOGI(BT_SPP_TAG, "GET command: FW+RST");
8194

8295
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) {
8497
ESP_LOGI(BT_SPP_TAG, "GET command: FW+VER?");
8598

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';
91104

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);
95108
ESP_LOGI(BT_SPP_TAG, "GET command: FW+UPD:%ld", image_length);
96109

97110
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]);
100112

101113
xEventGroupClearBits(user_event_group, KEY_SCAN_BIT);
102114

@@ -116,8 +128,7 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
116128

117129
gettimeofday(&time_old, NULL);
118130
} 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]);
121132
}
122133
}
123134
} else {
@@ -129,6 +140,10 @@ void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
129140
data_num += param->data_ind.len;
130141
ESP_LOGD(BT_OTA_TAG, "have written image length %ld", data_num);
131142

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+
132147
if (data_num == image_length) {
133148
if (esp_ota_end(update_handle) != ESP_OK) {
134149
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)
141156
}
142157
gettimeofday(&time_new, NULL);
143158
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]);
146160
xEventGroupSetBits(user_event_group, KEY_SCAN_BIT);
147161
exit:
148162
ota_running = 0;

0 commit comments

Comments
 (0)