| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090 |
- /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2025-11-20 RuoFeng the first version
- */
- /**
- * BSP CSV文件操作模块实现
- * 优化版:简化操作,提升稳定性,减少Flash占用
- */
- /*********************↓ define ↓************************/
- #define CSV_DISK CH376_MODE_SD_CARD
- // #define CSV_DISK CH376_MODE_USB_DISK
- /*********************↑ define ↑************************/
- /**
- *
- *
- *
- *
- * */
- /*********************↓ include ↓************************/
- #include <CH376/bsp/bsp_csv.h>
- #include <stdarg.h>
- /*********************↑ include ↑************************/
- /**
- *
- *
- *
- *
- * */
- /*********************↓ extern ↓************************/
- extern uint32_t month;
- extern uint32_t day;
- extern uint32_t year;
- extern uint32_t hour;
- extern uint32_t min;
- extern uint32_t sec;
- extern uint8_t week;
- /*********************↑ extern ↑************************/
- /**
- *
- *
- *
- *
- * */
- /********************↓ declaration ↓***********************/
- static csv_result_t csv_init(ch376_mode_t mode);
- static csv_result_t csv_deinit(void);
- static csv_result_t csv_create(const char *filename, const char *header);
- static csv_result_t csv_open(const char *filename, csv_mode_t mode, const char *header);
- static csv_result_t csv_append(const char *format, ...);
- static csv_result_t csv_close(void);
- static uint32_t csv_get_line_count(void);
- static uint32_t csv_count_file_lines(const char *filename);
- static uint8_t csv_is_opened(void);
- static const char *csv_get_filename(void);
- /********************↑ declaration ↑***********************/
- /**
- *
- *
- *
- *
- * */
- /*********************↓ global variables ↓************************/
- /* 模块内部状态 */
- static struct
- {
- uint8_t file_opened;
- uint8_t device_ready;
- char current_file[13];
- uint32_t line_count;
- uint32_t file_size;
- } csv_state = {0};
- /* 全局操作接口 */
- csv_ops_t csv =
- {
- .init = csv_init,
- .deinit = csv_deinit,
- .create = csv_create,
- .open = csv_open,
- .append = csv_append,
- .close = csv_close,
- .get_line_count = csv_get_line_count,
- .count_file_lines = csv_count_file_lines,
- .is_opened = csv_is_opened,
- .get_filename = csv_get_filename};
- static volatile uint8_t busy_flg = 0; // 忙标记
- /*********************↑ global variables ↑************************/
- /**
- *
- *
- *
- *
- * */
- /* 简化文件名转换 */
- static void make_safe_filename(const char *input, char *output)
- {
- if (!input || strlen(input) == 0)
- {
- strcpy(output, "/DATA.CSV");
- return;
- }
- if (input[0] == '/')
- {
- strncpy(output, input, 12);
- }
- else
- {
- snprintf(output, 13, "/%s", input);
- }
- output[12] = '\0';
- }
- /* 设备初始化 - 简化版 */
- static csv_result_t csv_init(ch376_mode_t mode)
- {
- char busy_check = 0;
- static char busy_cnt = 0;
- if (busy_cnt >= 3)
- {
- busy_cnt = 0;
- busy_flg = 0;
- }
- if (busy_flg == 0x9D)
- {
- busy_check = 1;
- for (char a = 0; a < 3; a++)
- {
- rt_kprintf("CSV BUSY waiting now %d.\n", a);
- rt_thread_mdelay(1000);
- if (busy_flg != 0x9D)
- {
- rt_kprintf("CSV Free now.\n");
- busy_check = 0;
- break;
- }
- }
- if (busy_check)
- {
- return CSV_DEVICE_NOT_READY;
- }
- }
- if (csv_state.device_ready)
- {
- busy_flg = 0x9D;
- return CSV_SUCCESS;
- }
- ch376_result_t result = ch376.init();
- if (result != CH376_RESULT_SUCCESS)
- {
- // 简单重试机制
- rt_thread_mdelay(100);
- result = ch376.init();
- if (result != CH376_RESULT_SUCCESS)
- {
- return CSV_DEVICE_NOT_READY;
- }
- }
- result = ch376.switch_mode(mode);
- if (result != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return CSV_DEVICE_NOT_READY;
- }
- csv_state.device_ready = 1;
- busy_flg = 0x9D;
- return CSV_SUCCESS;
- }
- /* 设备反初始化 */
- static csv_result_t csv_deinit(void)
- {
- if (csv_state.file_opened)
- {
- csv.close();
- }
- ch376.deinit();
- csv_state.device_ready = 0;
- memset(&csv_state, 0, sizeof(csv_state));
- busy_flg = 0;
- return CSV_SUCCESS;
- }
- /* 创建CSV文件 - 简化版 */
- static csv_result_t csv_create(const char *filename, const char *header)
- {
- if (!filename)
- {
- return CSV_ERROR;
- }
- // 确保设备就绪
- if (!csv_state.device_ready)
- {
- if (csv_init(CSV_DISK) != CSV_SUCCESS)
- {
- return CSV_DEVICE_NOT_READY;
- }
- }
- // 关闭已打开的文件
- if (csv_state.file_opened)
- {
- ch376.file_close();
- csv_state.file_opened = 0;
- }
- // 生成安全文件名
- char safe_name[13];
- make_safe_filename(filename, safe_name);
- // rt_kprintf("[CSV] Creating file: %s\n", safe_name);
- // 创建文件
- ch376_result_t result = ch376.file_create(safe_name);
- if (result != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CSV] File creation failed: %d\n", result);
- return CSV_ERROR;
- }
- else
- {
- csv_bitmap_mark_exist(year - 2000, month, day);
- }
- // 写入表头(如果有)
- if (header && strlen(header) > 0)
- {
- char header_line[64]; // 减小缓冲区大小
- int len = snprintf(header_line, sizeof(header_line), "%s\n", header);
- result = ch376.file_write(header_line, len);
- if (result != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CSV] Header write failed: %d\n", result);
- ch376.file_close();
- return CSV_ERROR;
- }
- csv_state.line_count = 1;
- csv_state.file_size = len;
- }
- else
- {
- csv_state.line_count = 0;
- csv_state.file_size = 0;
- }
- // 更新状态
- csv_state.file_opened = 1;
- strncpy(csv_state.current_file, safe_name, sizeof(csv_state.current_file) - 1);
- rt_kprintf("[CSV] File created successfully: %s\n", safe_name);
- return CSV_SUCCESS;
- }
- /* 统计文件行数 - 修复版:区分表头行和数据行 */
- static uint32_t csv_count_file_lines(const char *filename)
- {
- if (!filename)
- {
- return 0;
- }
- // 确保设备就绪
- if (!csv_state.device_ready)
- {
- if (csv_init(CSV_DISK) != CSV_SUCCESS)
- {
- return 0;
- }
- }
- char safe_name[13];
- make_safe_filename(filename, safe_name);
- // 检查文件是否存在
- if (ch376.file_exists(safe_name) != CH376_RESULT_SUCCESS)
- {
- return 0;
- }
- ch376_result_t result = ch376.file_open(safe_name);
- if (result != CH376_RESULT_SUCCESS)
- {
- return 0;
- }
- uint32_t line_count = 0;
- uint8_t buffer[256];
- uint32_t read_size;
- uint32_t bytes_read = 0;
- uint8_t has_header = 0;
- while (bytes_read < 1024 * 1024) // 限制读取大小 为1M
- {
- result = ch376.file_read(buffer, sizeof(buffer), &read_size);
- if (result != CH376_RESULT_SUCCESS || read_size == 0)
- {
- break;
- }
- bytes_read += read_size;
- for (uint32_t i = 0; i < read_size; i++)
- {
- if (buffer[i] == '\n')
- {
- line_count++;
- }
- }
- }
- ch376.file_close();
- // 修复:如果文件有内容但行数为0,返回1(至少有一行数据)
- if (line_count == 0 && bytes_read > 0)
- {
- line_count = 1;
- }
- // rt_kprintf("[CSV] File statistics: %s -> %lu lines (%lu bytes)\n",
- // safe_name, line_count, bytes_read);
- return line_count;
- }
- /* 打开CSV文件 - 完全修复追加模式 */
- static csv_result_t csv_open(const char *filename, csv_mode_t mode, const char *header)
- {
- if (!filename)
- {
- return CSV_ERROR;
- }
- // 检查是否已有文件打开
- if (csv_state.file_opened)
- {
- rt_kprintf("[CSV] Error: File already open\n");
- return CSV_FILE_ALREADY_OPEN;
- }
- // 确保设备就绪
- if (!csv_state.device_ready)
- {
- if (csv_init(CSV_DISK) != CSV_SUCCESS)
- {
- return CSV_DEVICE_NOT_READY;
- }
- }
- char safe_name[13];
- make_safe_filename(filename, safe_name);
- ch376_result_t result;
- uint32_t existing_lines = 0;
- switch (mode)
- {
- case CSV_MODE_READ:
- // 只读模式
- result = ch376.file_open(safe_name);
- if (result != CH376_RESULT_SUCCESS)
- {
- return CSV_FILE_NOT_FOUND;
- }
- existing_lines = csv_count_file_lines(filename);
- break;
- case CSV_MODE_APPEND:
- // 关键修复:避免重复打开文件
- // 先检查文件是否存在,但不重复打开
- result = ch376.file_exists(safe_name);
- if (result == CH376_RESULT_SUCCESS)
- {
- // 文件存在,获取行数但不重复打开
- existing_lines = csv_count_file_lines(filename);
- // 现在打开文件用于追加
- result = ch376.file_open(safe_name);
- if (result == CH376_RESULT_SUCCESS)
- {
- csv_bitmap_mark_exist(year - 2000, month, day);
- uint32_t file_size = CH376GetFileSize();
- // rt_kprintf("[CSV] Append mode:(%s) File size %ld bytes, existing %lu lines\n", filename,file_size, existing_lines);
- // 定位到文件末尾
- if (ch376.file_seek(file_size) == CH376_RESULT_SUCCESS)
- {
- // rt_kprintf("[CSV] Successfully positioned to end of file\n");
- }
- else
- {
- rt_kprintf("[CSV] Failed to position to end of file\n");
- ch376.file_close();
- return CSV_ERROR;
- }
- }
- else
- {
- rt_kprintf("[CSV] File open failed: %d\n", result);
- return CSV_ERROR;
- }
- }
- else if (result == CH376_RESULT_FILE_NOT_FOUND)
- {
- // 文件不存在,创建新文件
- result = ch376.file_create(safe_name);
- if (result != CH376_RESULT_SUCCESS)
- {
- return CSV_ERROR;
- }
- // 写入表头(如果有)
- if (header && strlen(header) > 0)
- {
- if (strstr(header, "\n") == NULL)
- {
- char header_line[128];
- int len = snprintf(header_line, sizeof(header_line), "%s\n", header);
- result = ch376.file_write(header_line, len);
- }
- else
- {
- result = ch376.file_write(header, strlen(header));
- }
- if (result != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- return CSV_ERROR;
- }
- existing_lines = 1;
- }
- }
- else
- {
- return CSV_ERROR;
- }
- break;
- case CSV_MODE_OVERWRITE:
- // 覆盖模式
- return csv_create(filename, header);
- default:
- return CSV_ERROR;
- }
- // 更新状态
- csv_state.file_opened = 1;
- csv_state.line_count = existing_lines;
- strncpy(csv_state.current_file, safe_name, sizeof(csv_state.current_file) - 1);
- // rt_kprintf("[CSV] File opened successfully: %s, mode: %d\n", safe_name, mode);
- return CSV_SUCCESS;
- }
- /* 追加数据 - 优化版 */
- static csv_result_t csv_append(const char *format, ...)
- {
- if (!csv_state.file_opened)
- {
- return CSV_ERROR;
- }
- if (!format)
- {
- return CSV_ERROR;
- }
- // 简化格式化,使用固定大小缓冲区
- char buffer[128]; // 减小缓冲区
- va_list args;
- va_start(args, format);
- int len = rt_vsnprintf(buffer, sizeof(buffer) - 2, format, args);
- va_end(args);
- if (len <= 0)
- {
- return CSV_ERROR;
- }
- // 添加换行符
- buffer[len] = '\n';
- buffer[len + 1] = '\0';
- len++;
- // 简化写入,单次尝试
- ch376_result_t result = ch376.file_write(buffer, len);
- if (result == CH376_RESULT_SUCCESS)
- {
- csv_state.line_count++;
- csv_state.file_size += len;
- return CSV_SUCCESS;
- }
- return CSV_ERROR;
- }
- /* 关闭文件 - 简化版 */
- static csv_result_t csv_close(void)
- {
- if (!csv_state.file_opened)
- {
- return CSV_SUCCESS;
- }
- ch376_result_t result = ch376.file_close();
- csv_state.file_opened = 0;
- csv_state.line_count = 0;
- csv_state.file_size = 0;
- csv_state.current_file[0] = '\0';
- return (result == CH376_RESULT_SUCCESS) ? CSV_SUCCESS : CSV_ERROR;
- }
- /* 获取当前行数 */
- static uint32_t csv_get_line_count(void)
- {
- return csv_state.line_count;
- }
- /* 检查文件是否打开 */
- static uint8_t csv_is_opened(void)
- {
- return csv_state.file_opened;
- }
- /* 获取当前文件名 */
- static const char *csv_get_filename(void)
- {
- return csv_state.current_file;
- }
- /**
- *
- * BIT_MAP
- *
- *
- * */
- #include "easyflash.h"
- // 内部位图结构
- typedef struct
- {
- uint32_t version;
- uint8_t bitmap[CSV_BITMAP_BYTES];
- } csv_bitmap_t;
- static csv_bitmap_t g_bitmap = {0};
- static bool g_inited = false;
- static struct rt_mutex g_mutex;
- // ────────────────────────────────
- // 辅助函数:计算儒略日(Julian Day Number)
- // 来源:RT-Thread time.c 简化版,支持 2000~2099
- // ────────────────────────────────
- static long date_to_jdn(int year, int month, int day)
- {
- if (month <= 2)
- {
- year--;
- month += 12;
- }
- long a = year / 100;
- long b = 2 - a + (a / 4);
- return (long)(365.25 * (year + 4716)) + (long)(30.6001 * (month + 1)) + day + b - 1524;
- }
- // 计算相对于基准日的天数偏移
- static int calc_day_offset(int yy, int mm, int dd)
- {
- int full_year = (yy >= 0 && yy < 100) ? (2000 + yy) : yy;
- long jdn_now = date_to_jdn(full_year, mm, dd);
- // 基准日:2025-01-01
- long jdn_epoch = date_to_jdn(2000 + CSV_BITMAP_EPOCH_YY, CSV_BITMAP_EPOCH_MM, CSV_BITMAP_EPOCH_DD);
- long offset = jdn_now - jdn_epoch;
- if (offset < 0 || offset >= CSV_BITMAP_MAX_DAYS)
- {
- return -1; // 超出范围
- }
- return (int)offset;
- }
- // ────────────────────────────────
- // 公共接口实现
- // ────────────────────────────────
- void csv_bitmap_init(void)
- {
- //rt_kprintf("csv_bitmap_init \n\n");
- static bool mutex_created = false;
- if (!mutex_created)
- {
- rt_mutex_init(&g_mutex, "csv_bm", RT_IPC_FLAG_FIFO);
- mutex_created = true;
- }
- rt_mutex_take(&g_mutex, RT_WAITING_FOREVER);
- if (!g_inited)
- {
- size_t read_len = 0;
- ef_get_env_blob("csv_exist", &g_bitmap, sizeof(g_bitmap), &read_len);
- // 校验:长度 + 版本
- if (read_len != sizeof(g_bitmap) || g_bitmap.version != CSV_BITMAP_VERSION)
- {
- // 初始化新位图
- memset(&g_bitmap, 0, sizeof(g_bitmap));
- g_bitmap.version = CSV_BITMAP_VERSION;
- ef_set_env_blob("csv_exist", &g_bitmap, sizeof(g_bitmap));
- }
- g_inited = true;
- }
- rt_mutex_release(&g_mutex);
- }
- int csv_bitmap_mark_exist(int yy, int mm, int dd)
- {
- if (!g_inited)
- csv_bitmap_init();
- int offset = calc_day_offset(yy, mm, dd);
- if (offset < 0)
- return -1;
- if (csv_bitmap_is_exist(yy, mm, dd))
- {
- //rt_kprintf("%02d%02d%02d already marked\n\n", yy, mm, dd);
- return 0;
- }
- rt_mutex_take(&g_mutex, RT_WAITING_FOREVER);
- int byte_idx = offset / 8;
- int bit_idx = offset % 8;
- g_bitmap.bitmap[byte_idx] |= (1U << bit_idx);
- int ret = ef_set_env_blob("csv_exist", &g_bitmap, sizeof(g_bitmap));
- rt_mutex_release(&g_mutex);
- return (ret == EF_NO_ERR) ? 0 : -1;
- }
- int csv_bitmap_is_exist(int yy, int mm, int dd)
- {
- if (!g_inited)
- csv_bitmap_init();
- int offset = calc_day_offset(yy, mm, dd);
- if (offset < 0)
- return 0;
- rt_mutex_take(&g_mutex, RT_WAITING_FOREVER);
- int byte_idx = offset / 8;
- int bit_idx = offset % 8;
- int result = (g_bitmap.bitmap[byte_idx] >> bit_idx) & 1;
- rt_mutex_release(&g_mutex);
- return result;
- }
- // === 新增:从 struct tm 计算 offset(供初始化使用)===
- int calc_day_offset_from_tm(const struct tm *tm)
- {
- int yy = tm->tm_year % 100;
- int mm = tm->tm_mon + 1;
- int dd = tm->tm_mday;
- return calc_day_offset(yy, mm, dd); // 复用已有函数
- }
- // === 新增:从 offset 反推日期(用于遍历)===
- int offset_to_date(int offset, struct tm *out_tm)
- {
- if (!out_tm || offset < 0 || offset >= CSV_BITMAP_MAX_DAYS)
- {
- return -1;
- }
- // 基准日:2025-01-01
- struct tm epoch = {0};
- epoch.tm_year = 2000 + CSV_BITMAP_EPOCH_YY - 1900; // tm_year is years since 1900
- epoch.tm_mon = CSV_BITMAP_EPOCH_MM - 1;
- epoch.tm_mday = CSV_BITMAP_EPOCH_DD;
- epoch.tm_hour = 0;
- epoch.tm_min = 0;
- epoch.tm_sec = 0;
- epoch.tm_isdst = -1;
- time_t epoch_time = mktime(&epoch);
- if (epoch_time == (time_t)-1)
- {
- return -1;
- }
- // 加上 offset 天(86400 秒/天)
- time_t target_time = epoch_time + (time_t)offset * 86400;
- struct tm *p = localtime(&target_time);
- if (!p)
- return -1;
- *out_tm = *p;
- return 0;
- }
- // === 新增:通过 offset 直接查询位图(内部使用)===
- int csv_bitmap_is_exist_by_offset(int offset)
- {
- if (offset < 0 || offset >= CSV_BITMAP_MAX_DAYS)
- return 0;
- rt_mutex_take(&g_mutex, RT_WAITING_FOREVER);
- int byte_idx = offset / 8;
- int bit_idx = offset % 8;
- int result = (g_bitmap.bitmap[byte_idx] >> bit_idx) & 1;
- rt_mutex_release(&g_mutex);
- return result;
- }
- /**
- *
- * BIT_MAP
- *
- *
- * */
- #define BLOCK_SIZE 512
- /**
- * @brief 搬运指定日期的 CSV 文件(如 260105.csv)
- * @param yy, mm, dd: 日期(如 26, 1, 5)
- * @return 0:成功, 1:文件不存在, -1:错误
- */
- static int transfer_csv_by_date(int yy, int mm, int dd)
- {
- char filename[13];
- rt_snprintf(filename, sizeof(filename), "/%02d%02d%02d.CSV", yy, mm, dd);
- rt_kprintf("\n\n transfer_csv_by_date %s \n\n", filename);
- uint32_t file_size = 0;
- // === 步骤1: 预检查 U 盘是否在位 ===
- if (ch376.init() != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CSV] CH376 init failed (USB check)\n");
- return -1;
- }
- if (ch376.switch_mode(CH376_MODE_USB_DISK) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- rt_kprintf("[CSV] Failed to switch to USB mode\n");
- return -1;
- }
- ch376_device_status_t status;
- if (ch376.get_status(&status) != CH376_RESULT_SUCCESS || !status.is_device_present)
- {
- ch376.deinit();
- rt_kprintf("[CSV] USB device not present\n");
- return -1;
- }
- ch376.deinit(); // 仅检查,不操作文件
- // === 步骤2: 检查 SD 卡上文件是否存在,并获取大小 ===
- if (ch376.init() != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CSV] CH376 init failed (SD check)\n");
- return -1;
- }
- if (ch376.switch_mode(CH376_MODE_SD_CARD) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- if (ch376.get_status(&status) != CH376_RESULT_SUCCESS || !status.is_device_present)
- {
- ch376.deinit();
- return -1;
- }
- // 检查文件是否存在
- if (ch376.file_exists(filename) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- rt_kprintf("file dont exists %s\n", filename);
- return 1; // 文件不存在
- }
- if (ch376.file_open(filename) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- ch376.deinit();
- return -1;
- }
- ch376.file_close();
- ch376.deinit();
- rt_kprintf("[CSV] Start transferring %s (%lu bytes)\n", filename, file_size);
- // === 步骤3: 分块搬运(512B/块)===
- uint8_t buffer[BLOCK_SIZE];
- uint32_t offset = 0;
- while (offset < file_size)
- {
- uint32_t to_read = (file_size - offset > BLOCK_SIZE) ? BLOCK_SIZE : (file_size - offset);
- // ---- 从 SD 卡读取一块 ----
- if (ch376.init() != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CSV] SD init failed during read\n");
- return -1;
- }
- if (ch376.switch_mode(CH376_MODE_SD_CARD) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- if (ch376.file_open(filename) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- if (ch376.file_seek(offset) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- ch376.deinit();
- return -1;
- }
- uint32_t rlen = 0;
- if (ch376.file_read(buffer, to_read, &rlen) != CH376_RESULT_SUCCESS || rlen != to_read)
- {
- ch376.file_close();
- ch376.deinit();
- return -1;
- }
- ch376.file_close();
- ch376.deinit();
- // ---- 写入 U 盘(覆盖指定位置)----
- if (ch376.init() != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CSV] USB init failed during write\n");
- return -1;
- }
- if (ch376.switch_mode(CH376_MODE_USB_DISK) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- // 注意:CH376 的 file_create 会清空文件,file_open 不会创建新文件
- // 所以首次写入需 create,后续可 open。但为了统一,我们每次都 create(会丢失之前数据!)
- // 更好的做法:只在 offset==0 时 create,否则 open。
- if (offset == 0)
- {
- // 首次写入:创建新文件
- if (ch376.file_create(filename) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- }
- else
- {
- // 后续写入:打开已有文件
- if (ch376.file_open(filename) != CH376_RESULT_SUCCESS)
- {
- ch376.deinit();
- return -1;
- }
- }
- if (ch376.file_seek(offset) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- ch376.deinit();
- return -1;
- }
- if (ch376.file_write(buffer, rlen) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- ch376.deinit();
- return -1;
- }
- ch376.file_close();
- ch376.deinit();
- offset += rlen;
- rt_kprintf("[CSV] Transferred %lu / %lu bytes\n", offset, file_size);
- }
- rt_kprintf("[CSV] Transfer completed: %s\n", filename);
- return 0;
- }
- int transfer_csv_recent_days(int days)
- {
- time_t now = time(RT_NULL);
- if (now == (time_t)-1)
- {
- rt_kprintf("[CSV] Failed to get current time\n");
- return -1;
- }
- // ? 立即复制当前时间到本地结构体,不保留指针
- struct tm current_tm;
- struct tm *p = localtime(&now);
- if (!p)
- {
- rt_kprintf("[CSV] localtime failed\n");
- return -1;
- }
- current_tm = *p; // 复制内容!
- int success_count = 0;
- for (int i = 0; i < days; i++)
- {
- rt_kprintf("transfer_csv_recent_days >> for times %d / %d ", i, days);
- struct tm target = {0};
- target.tm_year = current_tm.tm_year;
- target.tm_mon = current_tm.tm_mon;
- target.tm_mday = current_tm.tm_mday - i;
- target.tm_hour = current_tm.tm_hour;
- target.tm_min = current_tm.tm_min;
- target.tm_sec = current_tm.tm_sec;
- target.tm_isdst = -1;
- time_t t_target = mktime(&target);
- if (t_target == (time_t)-1)
- {
- rt_kprintf("[CSV] mktime failed for offset %d\n", i);
- continue;
- }
- // 获取归一化后的日期(这次可以安全使用,因为我们不保存指针)
- p = localtime(&t_target);
- if (!p)
- continue;
- int yy = p->tm_year % 100;
- int mm = p->tm_mon + 1;
- int dd = p->tm_mday;
- char filename[13];
- rt_snprintf(filename, sizeof(filename), "/%02d%02d%02d.CSV", yy, mm, dd);
- rt_kprintf("[CSV] Trying: %s\n", filename);
- int ret = transfer_csv_by_date(yy, mm, dd);
- if (ret == 0)
- {
- success_count++;
- }
- else if (ret == -1)
- {
- rt_kprintf("[CSV] Fatal error on %s\n", filename);
- break;
- }
- // ret == 1: file not exists, continue
- }
- rt_kprintf("[CSV] Transfer finished. %d files moved.\n", success_count);
- return success_count;
- }
- /**
- * @brief 导出所有已知存在的 CSV 文件(从基准日到今天)
- * @return 成功搬运的文件数量
- */
- int transfer_csv_all_known_exists(void)
- {
- time_t now = time(RT_NULL);
- if (now == (time_t)-1)
- {
- rt_kprintf("[CSV] Failed to get current time\n");
- return -1;
- }
- csv_bitmap_init();
- struct tm current_tm;
- struct tm *p = localtime(&now);
- if (!p)
- {
- rt_kprintf("[CSV] localtime failed\n");
- return -1;
- }
- current_tm = *p; // 安全复制
- // 计算今天的儒略日偏移(相对于基准日)
- int today_offset = calc_day_offset_from_tm(¤t_tm);
- if (today_offset < 0 || today_offset >= MAX_EXPORT_SPAN_DAYS)
- {
- rt_kprintf("[CSV] Current date out of bitmap range\n");
- return -1;
- }
- int success_count = 0;
- // 从基准日(offset=0)遍历到今天(inclusive)
- for (int offset = 0; offset <= today_offset; offset++)
- {
- // 快速查询位图:该天是否存在 CSV?
- if (!csv_bitmap_is_exist_by_offset(offset))
- {
- continue; // 不存在,跳过
- }
- // 将 offset 转回 yy/mm/dd
- struct tm target_tm = {0};
- if (offset_to_date(offset, &target_tm) != 0)
- {
- continue;
- }
- int yy = target_tm.tm_year % 100;
- int mm = target_tm.tm_mon + 1;
- int dd = target_tm.tm_mday;
- char filename[13];
- rt_snprintf(filename, sizeof(filename), "/%02d%02d%02d.CSV", yy, mm, dd);
- rt_kprintf("[CSV] Found known file: %s\n", filename);
- // 执行搬运
- int ret = transfer_csv_by_date(yy, mm, dd);
- if (ret == 0)
- {
- success_count++;
- }
- else if (ret == -1)
- {
- rt_kprintf("[CSV] Fatal error on %s, aborting.\n", filename);
- break;
- }
- // ret == 1: 物理文件不存在(但位图标记了)→ 可选:清除位图标记
- }
- if (success_count <= 0)
- {
- rt_kprintf("[CSV] Export finished. %d files transferred. Lets Try again!\n", success_count);
- success_count = transfer_csv_recent_days(30);
- rt_kprintf("[CSV] Export finished. %d files transferred.\n", success_count);
- }
- else
- {
- rt_kprintf("[CSV] Export finished. %d files transferred. QUICK\n", success_count);
- }
- return success_count;
- }
- MSH_CMD_EXPORT(transfer_csv_all_known_exists, transfer_csv_all_known_exists)
|