Reputation: 21
I’m working on an ESP32 application that needs to transmit a CSV file through Bluetooth using the Serial Port Profile (SPP). The file transmission is triggered after receiving the "SEND_CSV" command. In my current implementation, I’m attempting to send the file in a loop using esp_spp_write()
, but I’m running into buffer overflow issues.
I understand that the ESP_SPP_WRITE_EVT
callback is triggered after data is sent, but I’m not sure how to properly use it to avoid buffer overflow and continue sending the remaining data from there. How can I manage packet transmission efficiently, respecting the buffer size, and use ESP_SPP_WRITE_EVT
to ensure the rest of the file is transmitted correctly?
Any examples or guidance on how to implement this flow would be greatly appreciated.
My code so far:
case ESP_SPP_DATA_IND_EVT:
ESP_LOGI(SPP_TAG, "Data received");
if (param->data_ind.len == 8 && strncmp((char*)param->data_ind.data, "SEND_CSV", 8) == 0) {
send_csv_via_bluetooth(param->data_ind.handle);
}
break;
#define MAX_CHUNK_SIZE 127
static void send_csv_via_bluetooth(uint32_t handle) {
FILE *file = fopen(FILE_PATH, "r");
if (file == NULL) {
ESP_LOGE(SPP_TAG, "Failed to open the CSV file");
return;
}
ESP_LOGI(SPP_TAG, "Checking size...");
// Get the file size
fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char size_string[20];
snprintf(size_string, sizeof(size_string), "%zu\n\r", file_size);
// Send the file size using Bluetooth
esp_spp_write(handle, strlen(size_string), (uint8_t *)size_string);
char buffer[MAX_CHUNK_SIZE];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
esp_err_t err = esp_spp_write(handle, bytes_read, (uint8_t *)buffer);
if (err != ESP_OK) {
ESP_LOGE(SPP_TAG, "Send data error %s", esp_err_to_name(err));
break;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
ESP_LOGI(SPP_TAG, "CSV file sent successfully.");
fclose(file);
}
Upvotes: 2
Views: 64