bsp_log.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. #include <CH376/bsp/bsp_log.h>
  2. /*
  3. * Copyright (c) 2006-2021, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2025-11-20 RuoFeng Initial version
  10. * 2025-11-28 AI Assistant Optimized for memory usage and stability
  11. */
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <string.h>
  15. /********************* Constants ************************/
  16. #define INDEX_MAGIC 0x55AA55AA
  17. #define INDEX_FILE "/RECOV.IDX"
  18. #define DATA_FILE_FORMAT "/REC%04d.LOG"
  19. #define INIT_RETRY_COUNT 3
  20. #define DEVICE_READY_TIMEOUT 5000
  21. #define OPERATION_TIMEOUT 3000
  22. /********************* Global Variables ************************/
  23. static recovery_manager_t g_recovery_mgr = {0};
  24. static uint8_t g_initialized = 0;
  25. static uint8_t g_hw_initialized = 0;
  26. static rt_mutex_t g_log_mutex = RT_NULL;
  27. /********************* Private Functions ************************/
  28. /* 获取数据文件路径 */
  29. static void get_data_filepath(uint16_t pos, char *path, size_t size)
  30. {
  31. if (pos < 10)
  32. {
  33. rt_snprintf(path, size, "/REC000%d.LOG", pos);
  34. }
  35. else if (pos < 100)
  36. {
  37. rt_snprintf(path, size, "/REC00%d.LOG", pos);
  38. }
  39. else if (pos < 1000)
  40. {
  41. rt_snprintf(path, size, "/REC0%d.LOG", pos);
  42. }
  43. else
  44. {
  45. rt_snprintf(path, size, "/REC%d.LOG", pos);
  46. }
  47. }
  48. /* 安全的文件写入 - 使用小缓冲区循环写入 */
  49. static int safe_file_write(const char *path, const uint8_t *data, uint32_t len)
  50. {
  51. uint8_t io_buffer[IO_BUFFER_SIZE];
  52. uint32_t written = 0;
  53. uint32_t start_time = rt_tick_get();
  54. if (!path || !data)
  55. {
  56. rt_kprintf("[LOG] Write: invalid params\n");
  57. return -1;
  58. }
  59. /* 创建文件 */
  60. if (ch376.file_create(path) != CH376_RESULT_SUCCESS)
  61. {
  62. rt_kprintf("[LOG] Write: create failed: %s\n", path);
  63. return -1;
  64. }
  65. /* 分块写入数据 */
  66. while (written < len)
  67. {
  68. /* 检查超时 */
  69. if (rt_tick_get() - start_time > OPERATION_TIMEOUT)
  70. {
  71. rt_kprintf("[LOG] Write: timeout\n");
  72. ch376.file_close();
  73. return -2;
  74. }
  75. uint32_t chunk_size = len - written;
  76. if (chunk_size > IO_BUFFER_SIZE)
  77. {
  78. chunk_size = IO_BUFFER_SIZE;
  79. }
  80. /* 拷贝数据到IO缓冲区 */
  81. memcpy(io_buffer, data + written, chunk_size);
  82. /* 写入块数据 */
  83. if (ch376.file_write(io_buffer, chunk_size) != CH376_RESULT_SUCCESS)
  84. {
  85. rt_kprintf("[LOG] Write: write failed at %ld\n", written);
  86. ch376.file_close();
  87. return -3;
  88. }
  89. written += chunk_size;
  90. }
  91. /* 关闭文件 */
  92. ch376.file_close();
  93. return 0;
  94. }
  95. /* 安全的文件读取 - 使用小缓冲区循环读取 */
  96. static int safe_file_read(const char *path, uint8_t *buf, uint32_t buf_size, uint32_t *read_len)
  97. {
  98. // 使用小栈变量,避免大数组
  99. uint8_t chunk_buf[256]; // 从512减到256字节
  100. uint32_t total_read = 0;
  101. if (!path || !buf || !read_len)
  102. {
  103. return -1;
  104. }
  105. // 打开文件
  106. if (ch376.file_open(path) != CH376_RESULT_SUCCESS)
  107. {
  108. rt_kprintf("[LOG] Read: open failed: %s\n", path);
  109. return -1;
  110. }
  111. // 获取文件大小
  112. uint32_t file_size;
  113. if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
  114. {
  115. ch376.file_close();
  116. rt_kprintf("[LOG] Read: get size failed\n");
  117. return -2;
  118. }
  119. // 检查缓冲区大小
  120. if (file_size > buf_size)
  121. {
  122. ch376.file_close();
  123. rt_kprintf("[LOG] Read: buffer too small %ld > %ld\n", file_size, buf_size);
  124. return -3;
  125. }
  126. // 分块读取(使用256字节块)
  127. while (total_read < file_size)
  128. {
  129. uint32_t remain = file_size - total_read;
  130. uint32_t chunk_size = (remain > sizeof(chunk_buf)) ? sizeof(chunk_buf) : remain;
  131. uint32_t this_read = 0;
  132. if (ch376.file_read(chunk_buf, chunk_size, &this_read) != CH376_RESULT_SUCCESS)
  133. {
  134. ch376.file_close();
  135. rt_kprintf("[LOG] Read: read failed at %ld\n", total_read);
  136. return -4;
  137. }
  138. if (this_read == 0)
  139. {
  140. break; // 读取完成
  141. }
  142. // 复制到输出缓冲区
  143. memcpy(buf + total_read, chunk_buf, this_read);
  144. total_read += this_read;
  145. }
  146. ch376.file_close();
  147. *read_len = total_read;
  148. return 0;
  149. }
  150. /* 检查文件是否存在 */
  151. static int file_exists(const char *path)
  152. {
  153. return (ch376.file_exists(path) == CH376_RESULT_SUCCESS) ? 1 : 0;
  154. }
  155. /* 删除文件 */
  156. static int delete_file(const char *path)
  157. {
  158. return (ch376.file_delete(path) == CH376_RESULT_SUCCESS) ? 0 : -1;
  159. }
  160. /* 等待设备就绪 */
  161. static int wait_device_ready(void)
  162. {
  163. uint32_t start_time = rt_tick_get();
  164. while (rt_tick_get() - start_time < DEVICE_READY_TIMEOUT)
  165. {
  166. ch376_device_status_t status;
  167. if (ch376.get_status(&status) == CH376_RESULT_SUCCESS &&
  168. status.is_device_present)
  169. {
  170. return 0;
  171. }
  172. rt_thread_mdelay(200);
  173. }
  174. return -1;
  175. }
  176. /********************* Public API ************************/
  177. /* 初始化补录系统 */
  178. int recovery_log_init(void)
  179. {
  180. int retry;
  181. /* 创建互斥锁 */
  182. if (g_log_mutex == RT_NULL)
  183. {
  184. g_log_mutex = rt_mutex_create("log_mutex", RT_IPC_FLAG_FIFO);
  185. if (g_log_mutex == RT_NULL)
  186. {
  187. rt_kprintf("[LOG] Mutex create failed\n");
  188. return -1;
  189. }
  190. }
  191. /* 获取互斥锁 */
  192. if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
  193. {
  194. return -1;
  195. }
  196. /* 硬件初始化 */
  197. if (!g_hw_initialized)
  198. {
  199. for (retry = 0; retry < INIT_RETRY_COUNT; retry++)
  200. {
  201. if (ch376.init() == CH376_RESULT_SUCCESS)
  202. {
  203. g_hw_initialized = 1;
  204. break;
  205. }
  206. rt_thread_mdelay(100);
  207. }
  208. if (!g_hw_initialized)
  209. {
  210. rt_mutex_release(g_log_mutex);
  211. recovery_log_deinit();
  212. return -2;
  213. }
  214. }
  215. /* 切换到U盘模式 */ // 251210 换了新板子使用sd卡
  216. if (ch376.switch_mode(CH376_MODE_SD_CARD) != CH376_RESULT_SUCCESS)
  217. //if (ch376.switch_mode(CH376_MODE_USB_DISK) != CH376_RESULT_SUCCESS)
  218. {
  219. rt_mutex_release(g_log_mutex);
  220. recovery_log_deinit();
  221. return -3;
  222. }
  223. /* 等待设备就绪 */
  224. if (wait_device_ready() != 0)
  225. {
  226. rt_mutex_release(g_log_mutex);
  227. recovery_log_deinit();
  228. return -4;
  229. }
  230. /* 存储初始化 */
  231. if (!g_initialized)
  232. {
  233. /* 尝试加载索引文件 */
  234. uint32_t read_size;
  235. uint8_t buf[sizeof(recovery_manager_t)];
  236. if (safe_file_read(INDEX_FILE, buf, sizeof(buf), &read_size) == 0 &&
  237. read_size == sizeof(recovery_manager_t))
  238. {
  239. memcpy(&g_recovery_mgr, buf, sizeof(recovery_manager_t));
  240. /* 验证魔数 */
  241. if (g_recovery_mgr.initialized != INDEX_MAGIC)
  242. {
  243. memset(&g_recovery_mgr, 0, sizeof(recovery_manager_t));
  244. }
  245. }
  246. /* 初始化新系统 */
  247. if (g_recovery_mgr.initialized != INDEX_MAGIC)
  248. {
  249. memset(&g_recovery_mgr, 0, sizeof(recovery_manager_t));
  250. g_recovery_mgr.initialized = INDEX_MAGIC;
  251. g_recovery_mgr.max_items = MAX_RECOVERY_ITEMS;
  252. /* 保存索引文件 */
  253. if (safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr)) != 0)
  254. {
  255. rt_mutex_release(g_log_mutex);
  256. recovery_log_deinit();
  257. return -5;
  258. }
  259. }
  260. g_initialized = 1;
  261. }
  262. rt_mutex_release(g_log_mutex);
  263. return 0;
  264. }
  265. /* 写入一条最新数据 */
  266. int recovery_log_write(const uint8_t *data, uint16_t len, uint32_t timestamp)
  267. {
  268. char path[16];
  269. int ret = -1;
  270. /* === 参数校验 === */
  271. if (!g_initialized || data == RT_NULL || len == 0 || len > MAX_DATA_SIZE)
  272. {
  273. rt_kprintf("[LOG] Write: invalid params, len=%d\n", len);
  274. return -RT_EINVAL;
  275. }
  276. /* === 获取互斥锁(主同步机制)=== */
  277. if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
  278. {
  279. rt_kprintf("[LOG] Write: mutex timeout\n");
  280. return -RT_ETIMEOUT;
  281. }
  282. /* === 局部变量:动态分配,避免静态污染 === */
  283. typedef struct
  284. {
  285. uint32_t timestamp;
  286. uint16_t data_length;
  287. uint8_t data[MAX_DATA_SIZE];
  288. } data_item_t;
  289. data_item_t *item = RT_NULL;
  290. /* === 删除最老数据(如果已满)=== */
  291. if (g_recovery_mgr.item_count >= MAX_RECOVERY_ITEMS)
  292. {
  293. rt_kprintf("[LOG] Write: deleting oldest item\n");
  294. recovery_log_delete_oldest();
  295. }
  296. /* === 生成文件路径 === */
  297. get_data_filepath(g_recovery_mgr.write_index, path, sizeof(path));
  298. rt_kprintf("[LOG] Writing to: %s\n", path);
  299. /* === 分配内存 === */
  300. item = (data_item_t *)rt_malloc(sizeof(data_item_t));
  301. if (item == RT_NULL)
  302. {
  303. rt_kprintf("[LOG] rt_malloc failed for data item\n");
  304. ret = -RT_ENOMEM;
  305. goto cleanup;
  306. }
  307. /* === 填充数据 === */
  308. item->timestamp = timestamp;
  309. item->data_length = len;
  310. rt_memcpy(item->data, data, len);
  311. /* === 计算实际写入大小 === */
  312. uint32_t write_size = sizeof(item->timestamp) + sizeof(item->data_length) + len;
  313. /* === 写入数据文件 === */
  314. if (safe_file_write(path, (uint8_t *)item, write_size) != RT_EOK)
  315. {
  316. rt_kprintf("[LOG] Write: file write failed\n");
  317. ret = -RT_ERROR;
  318. goto cleanup;
  319. }
  320. /* === 更新管理器状态 === */
  321. g_recovery_mgr.write_index = (g_recovery_mgr.write_index + 1) % MAX_RECOVERY_ITEMS;
  322. if (g_recovery_mgr.item_count < MAX_RECOVERY_ITEMS)
  323. {
  324. g_recovery_mgr.item_count++;
  325. }
  326. g_recovery_mgr.total_written++;
  327. rt_kprintf("[LOG] Write success: idx=%d, count=%d\n",
  328. g_recovery_mgr.write_index, g_recovery_mgr.item_count);
  329. /* === 更新索引文件 === */
  330. if (safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr)) != RT_EOK)
  331. {
  332. rt_kprintf("[LOG] Write: index update failed\n");
  333. ret = -RT_ERROR;
  334. goto cleanup;
  335. }
  336. /* === 成功返回 === */
  337. ret = RT_EOK;
  338. cleanup:
  339. /* === 统一资源释放 === */
  340. if (item != RT_NULL)
  341. {
  342. // 注意:item 来自 rt_malloc,地址天然对齐,可直接 free
  343. rt_free(item);
  344. item = RT_NULL;
  345. }
  346. /* === 释放互斥锁(确保所有路径都释放)=== */
  347. rt_mutex_release(g_log_mutex);
  348. return ret;
  349. }
  350. /* 计算当前有多少条 */
  351. uint32_t recovery_log_get_count(void)
  352. {
  353. return g_initialized ? g_recovery_mgr.item_count : 0;
  354. }
  355. // 修改 recovery_log_read_last 函数
  356. int recovery_log_read_last(uint8_t *buffer, uint16_t *len, uint32_t *timestamp)
  357. {
  358. char path[16];
  359. if (!g_initialized || !buffer || !len || g_recovery_mgr.item_count == 0)
  360. {
  361. return -1;
  362. }
  363. if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
  364. {
  365. return -1;
  366. }
  367. // 修复:使用正确的读取位置逻辑
  368. uint16_t read_pos = g_recovery_mgr.read_index; // 应该是读索引,不是写索引
  369. get_data_filepath(read_pos, path, sizeof(path));
  370. rt_kprintf("[LOG] Read last: file %s, read_index=%d, count=%d\n",
  371. path, read_pos, g_recovery_mgr.item_count);
  372. // 直接读取文件并解析
  373. if (ch376.file_open(path) != CH376_RESULT_SUCCESS)
  374. {
  375. rt_kprintf("[LOG] Read: open failed\n");
  376. rt_mutex_release(g_log_mutex);
  377. return -2;
  378. }
  379. // 获取实际文件大小
  380. uint32_t file_size;
  381. if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
  382. {
  383. ch376.file_close();
  384. rt_mutex_release(g_log_mutex);
  385. rt_kprintf("[LOG] Read: get size failed\n");
  386. return -3;
  387. }
  388. rt_kprintf("[LOG] Read: actual file size = %ld bytes\n", file_size);
  389. // 读取整个文件到临时缓冲区
  390. uint8_t *file_data = rt_malloc(file_size);
  391. if (!file_data)
  392. {
  393. ch376.file_close();
  394. rt_mutex_release(g_log_mutex);
  395. return -10;
  396. }
  397. uint32_t total_read = 0;
  398. int read_result = 0;
  399. // 直接读取文件内容
  400. if (ch376.file_read(file_data, file_size, &total_read) != CH376_RESULT_SUCCESS)
  401. {
  402. rt_kprintf("[LOG] Read: read failed\n");
  403. read_result = -4;
  404. goto cleanup;
  405. }
  406. if (total_read < sizeof(uint32_t) + sizeof(uint16_t))
  407. {
  408. rt_kprintf("[LOG] Read: file too small\n");
  409. read_result = -5;
  410. goto cleanup;
  411. }
  412. // 修复:精确解析时间戳和数据长度
  413. // 注意:这里假设文件格式是 [时间戳(4字节)][数据长度(2字节)][数据]
  414. uint32_t file_timestamp = 0;
  415. uint16_t file_data_len = 0;
  416. // 字节级精确解析(避免结构体对齐问题)
  417. file_timestamp = (uint32_t)file_data[0] |
  418. ((uint32_t)file_data[1] << 8) |
  419. ((uint32_t)file_data[2] << 16) |
  420. ((uint32_t)file_data[3] << 24);
  421. file_data_len = (uint16_t)file_data[4] |
  422. ((uint16_t)file_data[5] << 8);
  423. rt_kprintf("[LOG] Read: parsed timestamp=%lu, len=%u\n", file_timestamp, file_data_len);
  424. // 修复:验证数据长度的合理性
  425. uint32_t header_size = 6; // 时间戳4 + 长度2
  426. uint32_t expected_total = header_size + file_data_len;
  427. // 计算实际可用数据长度
  428. uint16_t actual_data_len = file_data_len;
  429. if (file_data_len > MAX_DATA_SIZE)
  430. {
  431. rt_kprintf("[LOG] Read: warning: data length %u > max %d\n",
  432. file_data_len, MAX_DATA_SIZE);
  433. actual_data_len = MAX_DATA_SIZE;
  434. }
  435. if (expected_total > total_read)
  436. {
  437. rt_kprintf("[LOG] Read: warning: expected %lu > read %lu\n",
  438. expected_total, total_read);
  439. // 使用实际可用的数据
  440. if (total_read > header_size)
  441. {
  442. actual_data_len = total_read - header_size;
  443. if (actual_data_len > MAX_DATA_SIZE)
  444. {
  445. actual_data_len = MAX_DATA_SIZE;
  446. }
  447. }
  448. else
  449. {
  450. actual_data_len = 0;
  451. }
  452. }
  453. // 复制数据到输出缓冲区
  454. if (actual_data_len > 0)
  455. {
  456. memcpy(buffer, file_data + header_size, actual_data_len);
  457. }
  458. // 返回结果
  459. if (timestamp)
  460. *timestamp = file_timestamp;
  461. *len = actual_data_len;
  462. rt_kprintf("[LOG] Read: returning timestamp=%lu, len=%u\n", file_timestamp, actual_data_len);
  463. // 关闭文件
  464. ch376.file_close();
  465. cleanup:
  466. //rt_kprintf("rt_free(file_data) > %x\n" , file_data );
  467. rt_free(file_data);
  468. rt_mutex_release(g_log_mutex);
  469. return read_result;
  470. }
  471. /* 删除最老的一条 */
  472. int recovery_log_delete_oldest(void)
  473. {
  474. char path[16];
  475. if (!g_initialized || g_recovery_mgr.item_count == 0)
  476. {
  477. rt_kprintf("[LOG] Delete oldest: nothing to delete\n");
  478. return -1;
  479. }
  480. if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
  481. {
  482. rt_kprintf("[LOG] Delete oldest: mutex timeout\n");
  483. return -1;
  484. }
  485. /* 删除数据文件 */
  486. get_data_filepath(g_recovery_mgr.read_index, path, sizeof(path));
  487. rt_kprintf("[LOG] Delete oldest: deleting %s\n", path);
  488. delete_file(path);
  489. /* 更新索引 */
  490. g_recovery_mgr.read_index = (g_recovery_mgr.read_index + 1) % MAX_RECOVERY_ITEMS;
  491. g_recovery_mgr.item_count--;
  492. g_recovery_mgr.total_recovered++;
  493. rt_kprintf("[LOG] Delete oldest: index updated, read_index=%d, count=%d\n",
  494. g_recovery_mgr.read_index, g_recovery_mgr.item_count);
  495. /* 更新索引文件 */
  496. int result = safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr));
  497. rt_mutex_release(g_log_mutex);
  498. return result;
  499. }
  500. /* 获取补录系统状态 */
  501. void recovery_log_get_status(uint32_t *total_written, uint32_t *total_recovered, uint32_t *current_count)
  502. {
  503. if (total_written)
  504. *total_written = g_recovery_mgr.total_written;
  505. if (total_recovered)
  506. *total_recovered = g_recovery_mgr.total_recovered;
  507. if (current_count)
  508. *current_count = g_recovery_mgr.item_count;
  509. }
  510. /* 反初始化补录系统 */
  511. int recovery_log_deinit(void)
  512. {
  513. if (rt_mutex_take(g_log_mutex, OPERATION_TIMEOUT) != RT_EOK)
  514. {
  515. return -1;
  516. }
  517. if (g_initialized)
  518. {
  519. /* 保存索引文件 */
  520. // safe_file_write(INDEX_FILE, (uint8_t *)&g_recovery_mgr, sizeof(g_recovery_mgr));
  521. g_initialized = 0;
  522. }
  523. if (g_hw_initialized)
  524. {
  525. ch376.deinit();
  526. g_hw_initialized = 0;
  527. }
  528. rt_mutex_release(g_log_mutex);
  529. return 0;
  530. }
  531. /* 系统诊断信息 */
  532. void recovery_log_diagnostic(void)
  533. {
  534. rt_kprintf("\n=== Recovery Log Diagnostic ===\n");
  535. rt_kprintf("Module Initialized: %s\n", g_initialized ? "Yes" : "No");
  536. rt_kprintf("HW Initialized: %s\n", g_hw_initialized ? "Yes" : "No");
  537. rt_kprintf("Mutex Available: %s\n", g_log_mutex ? "Yes" : "No");
  538. if (g_initialized)
  539. {
  540. rt_kprintf("Current Items: %d\n", g_recovery_mgr.item_count);
  541. rt_kprintf("Write Index: %d\n", g_recovery_mgr.write_index);
  542. rt_kprintf("Read Index: %d\n", g_recovery_mgr.read_index);
  543. rt_kprintf("Total Written: %ld\n", g_recovery_mgr.total_written);
  544. rt_kprintf("Total Recovered: %ld\n", g_recovery_mgr.total_recovered);
  545. /* 显示前几个文件的状态 */
  546. rt_kprintf("\nFile Status (first 5):\n");
  547. for (int i = 0; i < 5 && i < MAX_RECOVERY_ITEMS; i++)
  548. {
  549. char path[16];
  550. get_data_filepath(i, path, sizeof(path));
  551. if (file_exists(path))
  552. {
  553. rt_kprintf(" %s: EXISTS\n", path);
  554. }
  555. }
  556. }
  557. }
  558. // MSH_CMD_EXPORT(recovery_log_diagnostic, Show recovery log status);
  559. /* 测试写入功能 */
  560. void test_log_write(void)
  561. {
  562. rt_kprintf("Testing log write...\n");
  563. if (recovery_log_init() != 0)
  564. {
  565. rt_kprintf("Init failed\n");
  566. return;
  567. }
  568. uint8_t test_data[] = "Test data for recovery log system";
  569. uint32_t timestamp = rt_tick_get();
  570. int result = recovery_log_write(test_data, sizeof(test_data), timestamp);
  571. rt_kprintf("Write test result: %d\n", result);
  572. recovery_log_deinit();
  573. }
  574. // MSH_CMD_EXPORT(test_log_write, Test log write functionality);
  575. /* 测试读取功能 */
  576. void test_log_read(void)
  577. {
  578. rt_kprintf("Testing log read...\n");
  579. if (recovery_log_init() != 0)
  580. {
  581. rt_kprintf("Init failed\n");
  582. return;
  583. }
  584. uint32_t count = recovery_log_get_count();
  585. rt_kprintf("Current count: %ld\n", count);
  586. if (count > 0)
  587. {
  588. uint8_t buffer[MAX_DATA_SIZE];
  589. uint16_t len = 0;
  590. uint32_t timestamp = 0;
  591. int result = recovery_log_read_last(buffer, &len, &timestamp);
  592. rt_kprintf("Read test result: %d\n", result);
  593. if (result == 0)
  594. {
  595. rt_kprintf("Data: timestamp=%ld, len=%d, content=%.*s\n",
  596. timestamp, len, len, buffer);
  597. }
  598. }
  599. else
  600. {
  601. rt_kprintf("No data to read\n");
  602. }
  603. recovery_log_deinit();
  604. }
  605. // MSH_CMD_EXPORT(test_log_read, Test log read functionality);
  606. // 添加文件格式检查函数
  607. void check_file_format(const char *path)
  608. {
  609. if (ch376.file_open(path) != CH376_RESULT_SUCCESS)
  610. {
  611. rt_kprintf("[CHECK] Cannot open %s\n", path);
  612. return;
  613. }
  614. uint32_t file_size;
  615. if (ch376.file_getsize(&file_size) != CH376_RESULT_SUCCESS)
  616. {
  617. ch376.file_close();
  618. return;
  619. }
  620. rt_kprintf("[CHECK] File %s size: %ld bytes\n", path, file_size);
  621. // 读取前32字节进行分析
  622. uint8_t buf[32];
  623. uint32_t read_size = 0;
  624. if (ch376.file_read(buf, 32, &read_size) == CH376_RESULT_SUCCESS && read_size >= 6)
  625. {
  626. rt_kprintf("[CHECK] First 32 bytes (hex):\n");
  627. for (int i = 0; i < read_size; i++)
  628. {
  629. rt_kprintf("%02X ", buf[i]);
  630. if ((i + 1) % 16 == 0)
  631. rt_kprintf("\n");
  632. }
  633. rt_kprintf("\n");
  634. // 尝试解析为不同格式
  635. uint32_t as_timestamp = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  636. uint16_t as_len = buf[4] | (buf[5] << 8);
  637. rt_kprintf("[CHECK] As timestamp+len: timestamp=%lu, len=%u\n", as_timestamp, as_len);
  638. rt_kprintf("[CHECK] Header(6) + len(%u) = %u bytes\n", as_len, 6 + as_len);
  639. rt_kprintf("[CHECK] File actual size: %ld bytes\n", file_size);
  640. if (6 + as_len == file_size)
  641. {
  642. rt_kprintf("[CHECK] ? Format matches timestamp+len structure\n");
  643. }
  644. else
  645. {
  646. rt_kprintf("[CHECK] ? Format mismatch\n");
  647. }
  648. }
  649. ch376.file_close();
  650. }
  651. // MSH_CMD_EXPORT(check_file_format, Check file format<filename>);