| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 |
- #include <CH376/bsp/bsp_log.h>
- /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2025-11-20 RuoFeng Initial version
- * 2025-11-28 AI Assistant Optimized for memory usage and stability
- */
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <string.h>
- /********************* Constants ************************/
- #define INDEX_MAGIC 0x55AA55AA
- #define INDEX_FILE "/RECOV.IDX"
- #define DATA_FILE_FORMAT "/REC%04d.LOG"
- #define INIT_RETRY_COUNT 3
- #define DEVICE_READY_TIMEOUT 5000
- #define OPERATION_TIMEOUT 3000
- /********************* Global Variables ************************/
- static recovery_manager_t g_recovery_mgr = {0};
- static uint8_t g_initialized = 0;
- static uint8_t g_hw_initialized = 0;
- static rt_mutex_t g_log_mutex = RT_NULL;
- /********************* Private Functions ************************/
- /* 获取数据文件路径 */
- static void get_data_filepath(uint16_t pos, char *path, size_t size)
- {
- if (pos < 10)
- {
- rt_snprintf(path, size, "/REC000%d.LOG", pos);
- }
- else if (pos < 100)
- {
- rt_snprintf(path, size, "/REC00%d.LOG", pos);
- }
- else if (pos < 1000)
- {
- rt_snprintf(path, size, "/REC0%d.LOG", pos);
- }
- else
- {
- rt_snprintf(path, size, "/REC%d.LOG", pos);
- }
- }
- /* 安全的文件写入 - 使用小缓冲区循环写入 */
- static int safe_file_write(const char *path, const uint8_t *data, uint32_t len)
- {
- uint8_t io_buffer[IO_BUFFER_SIZE];
- uint32_t written = 0;
- uint32_t start_time = rt_tick_get();
- if (!path || !data)
- {
- rt_kprintf("[LOG] Write: invalid params\n");
- return -1;
- }
- /* 创建文件 */
- if (ch376.file_create(path) != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[LOG] Write: create failed: %s\n", path);
- return -1;
- }
- /* 分块写入数据 */
- while (written < len)
- {
- /* 检查超时 */
- if (rt_tick_get() - start_time > OPERATION_TIMEOUT)
- {
- rt_kprintf("[LOG] Write: timeout\n");
- ch376.file_close();
- return -2;
- }
- uint32_t chunk_size = len - written;
- if (chunk_size > IO_BUFFER_SIZE)
- {
- chunk_size = IO_BUFFER_SIZE;
- }
- /* 拷贝数据到IO缓冲区 */
- memcpy(io_buffer, data + written, chunk_size);
- /* 写入块数据 */
- if (ch376.file_write(io_buffer, chunk_size) != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[LOG] Write: write failed at %ld\n", written);
- ch376.file_close();
- return -3;
- }
- written += chunk_size;
- }
- /* 关闭文件 */
- ch376.file_close();
- return 0;
- }
- /* 安全的文件读取 - 使用小缓冲区循环读取 */
- static int safe_file_read(const char *path, uint8_t *buf, uint32_t buf_size, uint32_t *read_len)
- {
- // 使用小栈变量,避免大数组
- uint8_t chunk_buf[256]; // 从512减到256字节
- uint32_t total_read = 0;
- if (!path || !buf || !read_len)
- {
- return -1;
- }
- // 打开文件
- if (ch376.file_open(path) != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[LOG] Read: open failed: %s\n", path);
- return -1;
- }
- // 获取文件大小
- uint32_t file_size;
- if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- rt_kprintf("[LOG] Read: get size failed\n");
- return -2;
- }
- // 检查缓冲区大小
- if (file_size > buf_size)
- {
- ch376.file_close();
- rt_kprintf("[LOG] Read: buffer too small %ld > %ld\n", file_size, buf_size);
- return -3;
- }
- // 分块读取(使用256字节块)
- while (total_read < file_size)
- {
- uint32_t remain = file_size - total_read;
- uint32_t chunk_size = (remain > sizeof(chunk_buf)) ? sizeof(chunk_buf) : remain;
- uint32_t this_read = 0;
- if (ch376.file_read(chunk_buf, chunk_size, &this_read) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- rt_kprintf("[LOG] Read: read failed at %ld\n", total_read);
- return -4;
- }
- if (this_read == 0)
- {
- break; // 读取完成
- }
- // 复制到输出缓冲区
- memcpy(buf + total_read, chunk_buf, this_read);
- total_read += this_read;
- }
- ch376.file_close();
- *read_len = total_read;
- return 0;
- }
- /* 检查文件是否存在 */
- static int file_exists(const char *path)
- {
- return (ch376.file_exists(path) == CH376_RESULT_SUCCESS) ? 1 : 0;
- }
- /* 删除文件 */
- static int delete_file(const char *path)
- {
- return (ch376.file_delete(path) == CH376_RESULT_SUCCESS) ? 0 : -1;
- }
- /* 等待设备就绪 */
- static int wait_device_ready(void)
- {
- uint32_t start_time = rt_tick_get();
- while (rt_tick_get() - start_time < DEVICE_READY_TIMEOUT)
- {
- ch376_device_status_t status;
- if (ch376.get_status(&status) == CH376_RESULT_SUCCESS &&
- status.is_device_present)
- {
- return 0;
- }
- rt_thread_mdelay(200);
- }
- return -1;
- }
- /********************* Public API ************************/
- /* 初始化补录系统 */
- int recovery_log_init(void)
- {
- int retry;
- /* 创建互斥锁 */
- if (g_log_mutex == RT_NULL)
- {
- g_log_mutex = rt_mutex_create("log_mutex", RT_IPC_FLAG_FIFO);
- if (g_log_mutex == RT_NULL)
- {
- rt_kprintf("[LOG] Mutex create failed\n");
- return -1;
- }
- }
- /* 获取互斥锁 */
- if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
- {
- return -1;
- }
- /* 硬件初始化 */
- if (!g_hw_initialized)
- {
- for (retry = 0; retry < INIT_RETRY_COUNT; retry++)
- {
- if (ch376.init() == CH376_RESULT_SUCCESS)
- {
- g_hw_initialized = 1;
- break;
- }
- rt_thread_mdelay(100);
- }
- if (!g_hw_initialized)
- {
- rt_mutex_release(g_log_mutex);
- recovery_log_deinit();
- return -2;
- }
- }
- /* 切换到U盘模式 */ // 251210 换了新板子使用sd卡
- if (ch376.switch_mode(CH376_MODE_SD_CARD) != CH376_RESULT_SUCCESS)
- //if (ch376.switch_mode(CH376_MODE_USB_DISK) != CH376_RESULT_SUCCESS)
- {
- rt_mutex_release(g_log_mutex);
- recovery_log_deinit();
- return -3;
- }
- /* 等待设备就绪 */
- if (wait_device_ready() != 0)
- {
- rt_mutex_release(g_log_mutex);
- recovery_log_deinit();
- return -4;
- }
- /* 存储初始化 */
- if (!g_initialized)
- {
- /* 尝试加载索引文件 */
- uint32_t read_size;
- uint8_t buf[sizeof(recovery_manager_t)];
- if (safe_file_read(INDEX_FILE, buf, sizeof(buf), &read_size) == 0 &&
- read_size == sizeof(recovery_manager_t))
- {
- memcpy(&g_recovery_mgr, buf, sizeof(recovery_manager_t));
- /* 验证魔数 */
- if (g_recovery_mgr.initialized != INDEX_MAGIC)
- {
- memset(&g_recovery_mgr, 0, sizeof(recovery_manager_t));
- }
- }
- /* 初始化新系统 */
- if (g_recovery_mgr.initialized != INDEX_MAGIC)
- {
- memset(&g_recovery_mgr, 0, sizeof(recovery_manager_t));
- g_recovery_mgr.initialized = INDEX_MAGIC;
- g_recovery_mgr.max_items = MAX_RECOVERY_ITEMS;
- /* 保存索引文件 */
- if (safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr)) != 0)
- {
- rt_mutex_release(g_log_mutex);
- recovery_log_deinit();
- return -5;
- }
- }
- g_initialized = 1;
- }
- rt_mutex_release(g_log_mutex);
- return 0;
- }
- /* 写入一条最新数据 */
- int recovery_log_write(const uint8_t *data, uint16_t len, uint32_t timestamp)
- {
- char path[16];
- int ret = -1;
- /* === 参数校验 === */
- if (!g_initialized || data == RT_NULL || len == 0 || len > MAX_DATA_SIZE)
- {
- rt_kprintf("[LOG] Write: invalid params, len=%d\n", len);
- return -RT_EINVAL;
- }
- /* === 获取互斥锁(主同步机制)=== */
- if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
- {
- rt_kprintf("[LOG] Write: mutex timeout\n");
- return -RT_ETIMEOUT;
- }
- /* === 局部变量:动态分配,避免静态污染 === */
- typedef struct
- {
- uint32_t timestamp;
- uint16_t data_length;
- uint8_t data[MAX_DATA_SIZE];
- } data_item_t;
- data_item_t *item = RT_NULL;
- /* === 删除最老数据(如果已满)=== */
- if (g_recovery_mgr.item_count >= MAX_RECOVERY_ITEMS)
- {
- rt_kprintf("[LOG] Write: deleting oldest item\n");
- recovery_log_delete_oldest();
- }
- /* === 生成文件路径 === */
- get_data_filepath(g_recovery_mgr.write_index, path, sizeof(path));
- rt_kprintf("[LOG] Writing to: %s\n", path);
- /* === 分配内存 === */
- item = (data_item_t *)rt_malloc(sizeof(data_item_t));
- if (item == RT_NULL)
- {
- rt_kprintf("[LOG] rt_malloc failed for data item\n");
- ret = -RT_ENOMEM;
- goto cleanup;
- }
- /* === 填充数据 === */
- item->timestamp = timestamp;
- item->data_length = len;
- rt_memcpy(item->data, data, len);
- /* === 计算实际写入大小 === */
- uint32_t write_size = sizeof(item->timestamp) + sizeof(item->data_length) + len;
- /* === 写入数据文件 === */
- if (safe_file_write(path, (uint8_t *)item, write_size) != RT_EOK)
- {
- rt_kprintf("[LOG] Write: file write failed\n");
- ret = -RT_ERROR;
- goto cleanup;
- }
- /* === 更新管理器状态 === */
- g_recovery_mgr.write_index = (g_recovery_mgr.write_index + 1) % MAX_RECOVERY_ITEMS;
- if (g_recovery_mgr.item_count < MAX_RECOVERY_ITEMS)
- {
- g_recovery_mgr.item_count++;
- }
- g_recovery_mgr.total_written++;
- rt_kprintf("[LOG] Write success: idx=%d, count=%d\n",
- g_recovery_mgr.write_index, g_recovery_mgr.item_count);
- /* === 更新索引文件 === */
- if (safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr)) != RT_EOK)
- {
- rt_kprintf("[LOG] Write: index update failed\n");
- ret = -RT_ERROR;
- goto cleanup;
- }
- /* === 成功返回 === */
- ret = RT_EOK;
- cleanup:
- /* === 统一资源释放 === */
- if (item != RT_NULL)
- {
- // 注意:item 来自 rt_malloc,地址天然对齐,可直接 free
- rt_free(item);
- item = RT_NULL;
- }
- /* === 释放互斥锁(确保所有路径都释放)=== */
- rt_mutex_release(g_log_mutex);
- return ret;
- }
- /* 计算当前有多少条 */
- uint32_t recovery_log_get_count(void)
- {
- return g_initialized ? g_recovery_mgr.item_count : 0;
- }
- // 修改 recovery_log_read_last 函数
- int recovery_log_read_last(uint8_t *buffer, uint16_t *len, uint32_t *timestamp)
- {
- char path[16];
- if (!g_initialized || !buffer || !len || g_recovery_mgr.item_count == 0)
- {
- return -1;
- }
- if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
- {
- return -1;
- }
- // 修复:使用正确的读取位置逻辑
- uint16_t read_pos = g_recovery_mgr.read_index; // 应该是读索引,不是写索引
- get_data_filepath(read_pos, path, sizeof(path));
- rt_kprintf("[LOG] Read last: file %s, read_index=%d, count=%d\n",
- path, read_pos, g_recovery_mgr.item_count);
- // 直接读取文件并解析
- if (ch376.file_open(path) != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[LOG] Read: open failed\n");
- rt_mutex_release(g_log_mutex);
- return -2;
- }
- // 获取实际文件大小
- uint32_t file_size;
- if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- rt_mutex_release(g_log_mutex);
- rt_kprintf("[LOG] Read: get size failed\n");
- return -3;
- }
- rt_kprintf("[LOG] Read: actual file size = %ld bytes\n", file_size);
- // 读取整个文件到临时缓冲区
- uint8_t *file_data = rt_malloc(file_size);
- if (!file_data)
- {
- ch376.file_close();
- rt_mutex_release(g_log_mutex);
- return -10;
- }
- uint32_t total_read = 0;
- int read_result = 0;
- // 直接读取文件内容
- if (ch376.file_read(file_data, file_size, &total_read) != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[LOG] Read: read failed\n");
- read_result = -4;
- goto cleanup;
- }
- if (total_read < sizeof(uint32_t) + sizeof(uint16_t))
- {
- rt_kprintf("[LOG] Read: file too small\n");
- read_result = -5;
- goto cleanup;
- }
- // 修复:精确解析时间戳和数据长度
- // 注意:这里假设文件格式是 [时间戳(4字节)][数据长度(2字节)][数据]
- uint32_t file_timestamp = 0;
- uint16_t file_data_len = 0;
- // 字节级精确解析(避免结构体对齐问题)
- file_timestamp = (uint32_t)file_data[0] |
- ((uint32_t)file_data[1] << 8) |
- ((uint32_t)file_data[2] << 16) |
- ((uint32_t)file_data[3] << 24);
- file_data_len = (uint16_t)file_data[4] |
- ((uint16_t)file_data[5] << 8);
- rt_kprintf("[LOG] Read: parsed timestamp=%lu, len=%u\n", file_timestamp, file_data_len);
- // 修复:验证数据长度的合理性
- uint32_t header_size = 6; // 时间戳4 + 长度2
- uint32_t expected_total = header_size + file_data_len;
- // 计算实际可用数据长度
- uint16_t actual_data_len = file_data_len;
- if (file_data_len > MAX_DATA_SIZE)
- {
- rt_kprintf("[LOG] Read: warning: data length %u > max %d\n",
- file_data_len, MAX_DATA_SIZE);
- actual_data_len = MAX_DATA_SIZE;
- }
- if (expected_total > total_read)
- {
- rt_kprintf("[LOG] Read: warning: expected %lu > read %lu\n",
- expected_total, total_read);
- // 使用实际可用的数据
- if (total_read > header_size)
- {
- actual_data_len = total_read - header_size;
- if (actual_data_len > MAX_DATA_SIZE)
- {
- actual_data_len = MAX_DATA_SIZE;
- }
- }
- else
- {
- actual_data_len = 0;
- }
- }
- // 复制数据到输出缓冲区
- if (actual_data_len > 0)
- {
- memcpy(buffer, file_data + header_size, actual_data_len);
- }
- // 返回结果
- if (timestamp)
- *timestamp = file_timestamp;
- *len = actual_data_len;
- rt_kprintf("[LOG] Read: returning timestamp=%lu, len=%u\n", file_timestamp, actual_data_len);
- // 关闭文件
- ch376.file_close();
- cleanup:
- //rt_kprintf("rt_free(file_data) > %x\n" , file_data );
- rt_free(file_data);
- rt_mutex_release(g_log_mutex);
- return read_result;
- }
- /* 删除最老的一条 */
- int recovery_log_delete_oldest(void)
- {
- char path[16];
- if (!g_initialized || g_recovery_mgr.item_count == 0)
- {
- rt_kprintf("[LOG] Delete oldest: nothing to delete\n");
- return -1;
- }
- if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
- {
- rt_kprintf("[LOG] Delete oldest: mutex timeout\n");
- return -1;
- }
- /* 删除数据文件 */
- get_data_filepath(g_recovery_mgr.read_index, path, sizeof(path));
- rt_kprintf("[LOG] Delete oldest: deleting %s\n", path);
- delete_file(path);
- /* 更新索引 */
- g_recovery_mgr.read_index = (g_recovery_mgr.read_index + 1) % MAX_RECOVERY_ITEMS;
- g_recovery_mgr.item_count--;
- g_recovery_mgr.total_recovered++;
- rt_kprintf("[LOG] Delete oldest: index updated, read_index=%d, count=%d\n",
- g_recovery_mgr.read_index, g_recovery_mgr.item_count);
- /* 更新索引文件 */
- int result = safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr));
- rt_mutex_release(g_log_mutex);
- return result;
- }
- /* 获取补录系统状态 */
- void recovery_log_get_status(uint32_t *total_written, uint32_t *total_recovered, uint32_t *current_count)
- {
- if (total_written)
- *total_written = g_recovery_mgr.total_written;
- if (total_recovered)
- *total_recovered = g_recovery_mgr.total_recovered;
- if (current_count)
- *current_count = g_recovery_mgr.item_count;
- }
- /* 反初始化补录系统 */
- int recovery_log_deinit(void)
- {
- if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
- {
- return -1;
- }
- if (g_initialized)
- {
- /* 保存索引文件 */
- // safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr));
- g_initialized = 0;
- }
- if (g_hw_initialized)
- {
- ch376.deinit();
- g_hw_initialized = 0;
- }
- rt_mutex_release(g_log_mutex);
- return 0;
- }
- /* 系统诊断信息 */
- void recovery_log_diagnostic(void)
- {
- rt_kprintf("\n=== Recovery Log Diagnostic ===\n");
- rt_kprintf("Module Initialized: %s\n", g_initialized ? "Yes" : "No");
- rt_kprintf("HW Initialized: %s\n", g_hw_initialized ? "Yes" : "No");
- rt_kprintf("Mutex Available: %s\n", g_log_mutex ? "Yes" : "No");
- if (g_initialized)
- {
- rt_kprintf("Current Items: %d\n", g_recovery_mgr.item_count);
- rt_kprintf("Write Index: %d\n", g_recovery_mgr.write_index);
- rt_kprintf("Read Index: %d\n", g_recovery_mgr.read_index);
- rt_kprintf("Total Written: %ld\n", g_recovery_mgr.total_written);
- rt_kprintf("Total Recovered: %ld\n", g_recovery_mgr.total_recovered);
- /* 显示前几个文件的状态 */
- rt_kprintf("\nFile Status (first 5):\n");
- for (int i = 0; i < 5 && i < MAX_RECOVERY_ITEMS; i++)
- {
- char path[16];
- get_data_filepath(i, path, sizeof(path));
- if (file_exists(path))
- {
- rt_kprintf(" %s: EXISTS\n", path);
- }
- }
- }
- }
- // MSH_CMD_EXPORT(recovery_log_diagnostic, Show recovery log status);
- /* 测试写入功能 */
- void test_log_write(void)
- {
- rt_kprintf("Testing log write...\n");
- if (recovery_log_init() != 0)
- {
- rt_kprintf("Init failed\n");
- return;
- }
- uint8_t test_data[] = "Test data for recovery log system";
- uint32_t timestamp = rt_tick_get();
- int result = recovery_log_write(test_data, sizeof(test_data), timestamp);
- rt_kprintf("Write test result: %d\n", result);
- recovery_log_deinit();
- }
- // MSH_CMD_EXPORT(test_log_write, Test log write functionality);
- /* 测试读取功能 */
- void test_log_read(void)
- {
- rt_kprintf("Testing log read...\n");
- if (recovery_log_init() != 0)
- {
- rt_kprintf("Init failed\n");
- return;
- }
- uint32_t count = recovery_log_get_count();
- rt_kprintf("Current count: %ld\n", count);
- if (count > 0)
- {
- uint8_t buffer[MAX_DATA_SIZE];
- uint16_t len = 0;
- uint32_t timestamp = 0;
- int result = recovery_log_read_last(buffer, &len, ×tamp);
- rt_kprintf("Read test result: %d\n", result);
- if (result == 0)
- {
- rt_kprintf("Data: timestamp=%ld, len=%d, content=%.*s\n",
- timestamp, len, len, buffer);
- }
- }
- else
- {
- rt_kprintf("No data to read\n");
- }
- recovery_log_deinit();
- }
- // MSH_CMD_EXPORT(test_log_read, Test log read functionality);
- // 添加文件格式检查函数
- void check_file_format(const char *path)
- {
- if (ch376.file_open(path) != CH376_RESULT_SUCCESS)
- {
- rt_kprintf("[CHECK] Cannot open %s\n", path);
- return;
- }
- uint32_t file_size;
- if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
- {
- ch376.file_close();
- return;
- }
- rt_kprintf("[CHECK] File %s size: %ld bytes\n", path, file_size);
- // 读取前32字节进行分析
- uint8_t buf[32];
- uint32_t read_size = 0;
- if (ch376.file_read(buf, 32, &read_size) == CH376_RESULT_SUCCESS && read_size >= 6)
- {
- rt_kprintf("[CHECK] First 32 bytes (hex):\n");
- for (int i = 0; i < read_size; i++)
- {
- rt_kprintf("%02X ", buf[i]);
- if ((i + 1) % 16 == 0)
- rt_kprintf("\n");
- }
- rt_kprintf("\n");
- // 尝试解析为不同格式
- uint32_t as_timestamp = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
- uint16_t as_len = buf[4] | (buf[5] << 8);
- rt_kprintf("[CHECK] As timestamp+len: timestamp=%lu, len=%u\n", as_timestamp, as_len);
- rt_kprintf("[CHECK] Header(6) + len(%u) = %u bytes\n", as_len, 6 + as_len);
- rt_kprintf("[CHECK] File actual size: %ld bytes\n", file_size);
- if (6 + as_len == file_size)
- {
- rt_kprintf("[CHECK] ? Format matches timestamp+len structure\n");
- }
- else
- {
- rt_kprintf("[CHECK] ? Format mismatch\n");
- }
- }
- ch376.file_close();
- }
- // MSH_CMD_EXPORT(check_file_format, Check file format<filename>);
|