增加文件列表读取,文件删除功能

This commit is contained in:
Yu-886 2025-03-26 17:57:06 +08:00
parent 79a5416e2a
commit 98626cbd47
6 changed files with 102 additions and 32 deletions

View File

@ -15,6 +15,7 @@ typedef struct
void Connection_test(uint8_t *buffer); void Connection_test(uint8_t *buffer);
void Transfer_file(uint8_t *buffer); void Transfer_file(uint8_t *buffer);
const DECODERCMD Cmd_Receiver[] = { const DECODERCMD Cmd_Receiver[] = {
{0x01,0x01,0x05,Connection_test}, {0x01,0x01,0x05,Connection_test},
{0x02,0x01,0x03,Transfer_file}, {0x02,0x01,0x03,Transfer_file},
@ -82,16 +83,18 @@ uint32_t flash_id;
void Connection_test(uint8_t *buffer) void Connection_test(uint8_t *buffer)
{ {
uint8_t send_buff[128] = {0x55, 0xAA, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00}; uint8_t send_buff[512] = {0x55, 0xAA, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
uint16_t data_len = 0;
ESP_LOGI(TAG,"Connection_test"); ESP_LOGI(TAG,"Connection_test");
send_buff[5] = sizeof(Device_info_t) + 2; data_len = sizeof(Device_info_t) + 2;
send_buff[(6 + sizeof(Device_info_t) + 2)] = '\r'; send_buff[4] = data_len & 0xff;
send_buff[(6 + sizeof(Device_info_t) + 2)+1] = '\n'; send_buff[5] = (data_len >> 8) & 0xff;
list_files_in_spiffs(off_line_tool.directory, 10);
memcpy(&send_buff[6],&off_line_tool,sizeof(Device_info_t)); memcpy(&send_buff[6],&off_line_tool,sizeof(Device_info_t));
if (buffer[0] == 0x01) { if (buffer[0] == 0x01) {
usb_serial_jtag_write_bytes(&send_buff[0],(6 + sizeof(Device_info_t) + 2 + 2),20 / portTICK_PERIOD_MS); usb_serial_jtag_write_bytes(&send_buff[0],(6 + sizeof(Device_info_t) + 2),20 / portTICK_PERIOD_MS);
} }
ESP_LOGI(TAG,"buff size:%d %d",sizeof(Device_info_t),data_len);
} }
/* /*
@ -110,7 +113,7 @@ void Transfer_file(uint8_t *buffer)
char file_name[64]; char file_name[64];
// ESP_LOGI(TAG,"Transfer_file"); // ESP_LOGI(TAG,"Transfer_file");
data_len = buffer[1] << 8 | buffer[2]; data_len = buffer[1] << 8 | buffer[2];
// ESP_LOGI(TAG,"data_len:%d",data_len); ESP_LOGI(TAG,"data_len:%d",data_len);
data_buff = malloc(data_len +10); data_buff = malloc(data_len +10);
if(buffer[0] == 0x01) {//文件名 if(buffer[0] == 0x01) {//文件名
if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len) if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len)
@ -140,27 +143,33 @@ void Transfer_file(uint8_t *buffer)
if (WaitPrnBufBytes(&uart_interface_data,data_len,0xffff,(uint8_t*)&data_buff[0]) == data_len) if (WaitPrnBufBytes(&uart_interface_data,data_len,0xffff,(uint8_t*)&data_buff[0]) == data_len)
{ {
crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2]; crc_received = data_buff[data_len-1]<<8 | data_buff[data_len-2];
// data_buff[data_len-2] = 0; data_buff[data_len-2] = 0;
// data_buff[data_len-1] = 0; data_buff[data_len-1] = 0;
// // //
// size_t written = fwrite(&data_buff[2], 1, (data_len - 4), file); size_t written = fwrite(&data_buff[2], 1, (data_len - 4), file);
// if (written != (data_len - 4)) { if (written != (data_len - 4)) {
// ESP_LOGE("SPIFFS", "fwrite failed, written=%d", written); ESP_LOGE("SPIFFS", "fwrite failed, written=%d", written);
// } else { } else {
// ESP_LOGI("SPIFFS", "Write success: %d bytes", written); ESP_LOGI("SPIFFS", "Write success: %d bytes", written);
// } }
if(data_buff[0] == 0xff && data_buff[1] == 0xff) if(data_buff[0] == 0xff && data_buff[1] == 0xff)
{ {
// fclose(file); fclose(file);
ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]); ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]);
} }
// ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]); ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]);
crc_calc = crc16_modbus((uint8_t*)&data_buff[2],data_len-4); crc_calc = crc16_modbus((uint8_t*)&data_buff[2],data_len-4);
}else{ }else{
ESP_LOGE(TAG,"fail:%d",data_len); ESP_LOGE(TAG,"fail:%d",data_len);
} }
} else if(buffer[0] == 0x03) {//删除所有文件
if (WaitPrnBufBytes(&uart_interface_data,2,0xffff,(uint8_t*)&data_buff[0]) == 2)
{
} }
// ESP_LOGI(TAG,"crc:%X----%X",crc_calc,crc_received); delete_all_file();
}
ESP_LOGI(TAG,"crc:%X----%X",crc_calc,crc_received);
free(data_buff); free(data_buff);
} }
@ -178,7 +187,7 @@ void Read_CMD_task(void *pvParameter)
{ {
frameStreamRead(&uart_interface_data,&ch,1); frameStreamRead(&uart_interface_data,&ch,1);
// ESP_LOGI(TAG, "Read:%X,%ld",ch,frameStreamRecLen(&uart_interface_data)); ESP_LOGI(TAG, "Read:%X,%ld,---%ld:%ld",ch,frameStreamRecLen(&uart_interface_data),uart_interface_data.read,uart_interface_data.write);
if(ch == 0xAA) if(ch == 0xAA)
{ {
if (WaitPrnBufBytes(&uart_interface_data,2,0xffff,&data[0]) == 2) if (WaitPrnBufBytes(&uart_interface_data,2,0xffff,&data[0]) == 2)

View File

@ -38,30 +38,76 @@ esp_err_t init_spiffs(void)
return ESP_OK; return ESP_OK;
} }
void list_files_in_spiffs() { void list_files_in_spiffs(spiffs_list_t *directory, size_t max_files) {
DIR *dir = opendir("/spiffs"); // 打开 SPIFFS 根目录 DIR *dir = opendir("/spiffs");
if (dir == NULL) { if (dir == NULL) {
ESP_LOGE(TAG, "Failed to open directory"); ESP_LOGE(TAG, "Failed to open directory");
return; return;
} }
struct dirent *entry; struct dirent *entry;
char filepath[300];
struct stat st; struct stat st;
char filepath[300];
uint8_t id = 0;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL && id < max_files) {
snprintf(filepath, sizeof(filepath), "/spiffs/%s", entry->d_name); snprintf(filepath, sizeof(filepath), "/spiffs/%s", entry->d_name);
if (stat(filepath, &st) == 0) { if (stat(filepath, &st) == 0) {
directory[id].id = id;
directory[id].addr = 0; // SPIFFS 本身没有物理地址,可以留空或自定义
directory[id].size = st.st_size;
memset(directory[id].name, 0, sizeof(directory[id].name));
strncpy(directory[id].name, entry->d_name, sizeof(directory[id].name) - 1);
ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", entry->d_name, st.st_size); ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", entry->d_name, st.st_size);
id++;
} else { } else {
ESP_LOGE(TAG, "Failed to get size of file: %s", entry->d_name); ESP_LOGE(TAG, "Failed to get size of file: %s", entry->d_name);
} }
} }
closedir(dir); // 关闭目录 closedir(dir);
} }
void delete_file(const char *file_path)
{
if (remove(file_path) == 0) {
ESP_LOGI(TAG, "File '%s' deleted successfully", file_path);
} else {
ESP_LOGE(TAG, "Failed to delete file '%s'", file_path);
}
}
void delete_all_file()
{
DIR *dir = opendir("/spiffs");
if (dir == NULL) {
ESP_LOGE(TAG, "Failed to open directory");
return;
}
struct dirent *entry;
struct stat st;
char filepath[300];
uint8_t id = 0;
while ((entry = readdir(dir)) != NULL && id < 10) {
snprintf(filepath, sizeof(filepath), "/spiffs/%s", entry->d_name);
if (stat(filepath, &st) == 0) {
ESP_LOGI(TAG, "remove file: %s, size: %ld bytes", entry->d_name, st.st_size);
remove(filepath);
id++;
} else {
ESP_LOGE(TAG, "Failed to get size of file: %s", entry->d_name);
}
}
closedir(dir);
}
// void Read_file(char* filename, Read_data data,uint16_t size) // void Read_file(char* filename, Read_data data,uint16_t size)
// { // {
// FILE* f ; // FILE* f ;

View File

@ -22,9 +22,19 @@ typedef struct
uint8_t buff[256]; uint8_t buff[256];
}spiffs_data_t; }spiffs_data_t;
typedef struct spiffs_list_
{
uint32_t id;
char name[32];
uint32_t addr;
uint32_t size;
}spiffs_list_t;
extern esp_err_t init_spiffs(void); extern esp_err_t init_spiffs(void);
extern esp_err_t sdmmc_init(void); extern esp_err_t sdmmc_init(void);
extern void list_files_in_spiffs(); extern void list_files_in_spiffs(spiffs_list_t *directory, size_t max_files);
extern void delete_file(const char *file_path);
extern void delete_all_file();
// void Read_file(char* filename, Read_data data,uint16_t size); // void Read_file(char* filename, Read_data data,uint16_t size);

View File

@ -79,9 +79,11 @@ void app_main(void)
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
init_spiffs(); init_spiffs();
list_files_in_spiffs(); list_files_in_spiffs(off_line_tool.directory, 10);
ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", off_line_tool.directory[0].name, off_line_tool.directory[0].size);
ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", off_line_tool.directory[1].name, off_line_tool.directory[1].size);
ESPFlash_Init(); ESPFlash_Init();
stream_init(&uart_interface_data,1024*50); stream_init(&uart_interface_data,1024*100);
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size()); ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
gpio_config_t config = { gpio_config_t config = {
@ -93,7 +95,7 @@ void app_main(void)
gpio_set_level(8,0); gpio_set_level(8,0);
// gpio_set_level(18,1); // gpio_set_level(18,1);
usb_serial2Uart(NULL); usb_serial2Uart(NULL);
xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*50, NULL, 14, NULL); xTaskCreate(&Read_CMD_task, "Read_CMD_task", 1024*20, NULL, 14, NULL);
#if 0 #if 0
home_html.html_data = load_html2Ram(home_html); home_html.html_data = load_html2Ram(home_html);
wifi_config_html.html_data = load_html2Ram(wifi_config_html); wifi_config_html.html_data = load_html2Ram(wifi_config_html);
@ -145,6 +147,8 @@ void app_main(void)
mac_wifi[0], mac_wifi[1], mac_wifi[2], mac_wifi[3], mac_wifi[4], mac_wifi[5]); mac_wifi[0], mac_wifi[1], mac_wifi[2], mac_wifi[3], mac_wifi[4], mac_wifi[5]);
memcpy(&off_line_tool.Device_MAC_WIFI[0],&mac_wifi[0],6); memcpy(&off_line_tool.Device_MAC_WIFI[0],&mac_wifi[0],6);
sprintf((char*)&off_line_tool.Device_SN[0],"1234567890");
// 获取蓝牙 MAC 地址 // 获取蓝牙 MAC 地址
// esp_bt_dev_get_address(mac_bt); // esp_bt_dev_get_address(mac_bt);
// ESP_LOGI("MAC Address", "Bluetooth MAC: %02x:%02x:%02x:%02x:%02x:%02x", // ESP_LOGI("MAC Address", "Bluetooth MAC: %02x:%02x:%02x:%02x:%02x:%02x",

View File

@ -43,6 +43,7 @@ typedef struct Device_info_
uint8_t Device_MAC_WIFI[6]; uint8_t Device_MAC_WIFI[6];
uint8_t Device_MAC_BLE[6]; uint8_t Device_MAC_BLE[6];
uint32_t flash_id; uint32_t flash_id;
spiffs_list_t directory[10];
}Device_info_t; }Device_info_t;
extern Device_info_t off_line_tool; extern Device_info_t off_line_tool;

View File

@ -32,18 +32,18 @@ static void echo_task(void *arg)
while (1) { while (1) {
int len = usb_serial_jtag_read_bytes(data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS); int len = usb_serial_jtag_read_bytes(data, (BUF_SIZE - 1), 2000 / portTICK_PERIOD_MS);
// Write data back to the USB SERIAL JTAG // Write data back to the USB SERIAL JTAG
if (len) { if (len) {
// usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS); // usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
if(frameStreamRecEmptyLen(&uart_interface_data) < 4096) if(frameStreamRecEmptyLen(&uart_interface_data) < 1024*30)
{ {
ESP_LOGE(TAG,"frameStream Be almost full:%d",len); ESP_LOGE(TAG,"frameStream Be almost full:%d",len);
while (1) while (1)
{ {
vTaskDelay(10/ portTICK_PERIOD_MS); vTaskDelay(10/ portTICK_PERIOD_MS);
if(frameStreamRecEmptyLen(&uart_interface_data) > 4096*3) if(frameStreamRecEmptyLen(&uart_interface_data) > 1024*32)
{ {
ESP_LOGE(TAG,"frameStream: %ld",frameStreamRecEmptyLen(&uart_interface_data)); ESP_LOGE(TAG,"frameStream: %ld",frameStreamRecEmptyLen(&uart_interface_data));
break; break;