ftp_up.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. ///*
  2. // * Copyright (c) 2006-2021, RT-Thread Development Team
  3. // *
  4. // * SPDX-License-Identifier: Apache-2.0
  5. // *
  6. // * Change Logs:
  7. // * Date Author Notes
  8. // * 2026-01-17 RuoFeng the first version
  9. // */
  10. //#include "ftp_up.h"
  11. //#include "zFTP.h"
  12. //#include "ch376_driver.h"
  13. //#include "bsp_csv.h"
  14. //#include <fal.h>
  15. //#include <string.h>
  16. //#include <rtdevice.h>
  17. //#include "rtthread.h"
  18. //
  19. //#define DBG_ENABLE
  20. //#define DBG_SECTION_NAME "FTP_APP"
  21. //#define DBG_LEVEL DBG_INFO
  22. //#include <rtdbg.h>
  23. //
  24. //#define FTP_APP_CONFIG_SERVER_ADDR "www.yfzhwlw.com"
  25. //#define FTP_APP_CONFIG_USERNAME "updateuser"
  26. //#define FTP_APP_CONFIG_PASSWORD "123456"
  27. //#define FTP_APP_CONFIG_CSV_PATH "/qxz_csv"
  28. //
  29. //static ftp_app_result_t upload_ecode_csv(void);
  30. //
  31. //static struct
  32. //{
  33. // uint8_t busy_flag;
  34. // uint8_t initialized;
  35. // char last_error[64];
  36. // uint32_t total_transferred;
  37. //
  38. // char current_local_file[32];
  39. // char current_remote_file[64];
  40. // uint32_t current_file_size;
  41. //
  42. // uint8_t first_block_buffer[FTP_APP_UPLOAD_BUFFER_SIZE];
  43. // uint32_t first_block_size;
  44. // uint8_t first_block_ready;
  45. //} ftp_state = {0};
  46. //
  47. //static struct rt_mutex ftp_mutex;
  48. //
  49. //static int csv_read_callback(void *handle, char *remote_filename, rt_uint8_t *buf,
  50. // rt_uint32_t len, rt_uint32_t pos,
  51. // rt_uint32_t *read_len, rt_uint32_t *total_len)
  52. //{
  53. // ch376_result_t result;
  54. // uint32_t actual_read = 0;
  55. // static uint32_t call_count = 0;
  56. // uint32_t this_call = ++call_count;
  57. //
  58. // LOG_D("[CSV-%lu] Callback: pos=%lu, len=%lu, cached=%d",
  59. // this_call, pos, len, ftp_state.first_block_ready);
  60. //
  61. // if (!buf || !read_len || !total_len)
  62. // {
  63. // LOG_E("[CSV-%lu] Invalid parameters", this_call);
  64. // return -RT_ERROR;
  65. // }
  66. //
  67. // if (ftp_state.current_local_file[0] == '\0' || ftp_state.current_file_size == 0)
  68. // {
  69. // LOG_E("[CSV-%lu] No file prepared!", this_call);
  70. // *read_len = 0;
  71. // *total_len = 0;
  72. // return -RT_ERROR;
  73. // }
  74. //
  75. // *total_len = ftp_state.current_file_size;
  76. // *read_len = 0;
  77. //
  78. // if (pos >= ftp_state.current_file_size)
  79. // {
  80. // LOG_D("[CSV-%lu] End of file reached", this_call);
  81. // return RT_EOK;
  82. // }
  83. //
  84. // if (pos == 0 && ftp_state.first_block_ready && ftp_state.first_block_size > 0)
  85. // {
  86. // LOG_D("[CSV-%lu] Using cached first block: %lu bytes",
  87. // this_call, ftp_state.first_block_size);
  88. //
  89. // uint32_t to_copy = ftp_state.first_block_size;
  90. // if (to_copy > len) to_copy = len;
  91. //
  92. // memcpy(buf, ftp_state.first_block_buffer, to_copy);
  93. // *read_len = to_copy;
  94. //
  95. // LOG_D("[CSV-%lu] Returned cached data immediately", this_call);
  96. // return RT_EOK;
  97. // }
  98. //
  99. // uint32_t to_read = ftp_state.current_file_size - pos;
  100. // if (to_read > len) to_read = len;
  101. // if (to_read > FTP_APP_UPLOAD_BUFFER_SIZE) to_read = FTP_APP_UPLOAD_BUFFER_SIZE;
  102. //
  103. // LOG_D("[CSV-%lu] Reading %lu bytes from pos %lu", this_call, to_read, pos);
  104. //
  105. // result = ch376.init();
  106. // if (result != CH376_RESULT_SUCCESS)
  107. // {
  108. // LOG_E("[CSV-%lu] CH376 init failed: %d", this_call, result);
  109. // return -RT_ERROR;
  110. // }
  111. //
  112. // result = ch376.switch_mode(CH376_MODE_SD_CARD);
  113. // if (result != CH376_RESULT_SUCCESS)
  114. // {
  115. // ch376.deinit();
  116. // LOG_E("[CSV-%lu] Switch to SD mode failed: %d", this_call, result);
  117. // return -RT_ERROR;
  118. // }
  119. //
  120. // result = ch376.file_open(ftp_state.current_local_file);
  121. // if (result != CH376_RESULT_SUCCESS)
  122. // {
  123. // ch376.deinit();
  124. // LOG_E("[CSV-%lu] Cannot open file: %s", this_call, ftp_state.current_local_file);
  125. // return -RT_ERROR;
  126. // }
  127. //
  128. // result = ch376.file_seek(pos);
  129. // if (result != CH376_RESULT_SUCCESS)
  130. // {
  131. // ch376.file_close();
  132. // ch376.deinit();
  133. // LOG_E("[CSV-%lu] Seek failed: pos=%lu", this_call, pos);
  134. // return -RT_ERROR;
  135. // }
  136. //
  137. // result = ch376.file_read(buf, to_read, &actual_read);
  138. // if (result != CH376_RESULT_SUCCESS)
  139. // {
  140. // ch376.file_close();
  141. // ch376.deinit();
  142. // LOG_E("[CSV-%lu] Read failed: pos=%lu", this_call, pos);
  143. // return -RT_ERROR;
  144. // }
  145. //
  146. // ch376.file_close();
  147. // ch376.deinit();
  148. //
  149. // *read_len = actual_read;
  150. //
  151. // LOG_D("[CSV-%lu] Read successful: %lu bytes", this_call, actual_read);
  152. //
  153. // return RT_EOK;
  154. //}
  155. //
  156. //static void set_last_error(const char *error)
  157. //{
  158. // if (error)
  159. // {
  160. // strncpy(ftp_state.last_error, error, sizeof(ftp_state.last_error) - 1);
  161. // ftp_state.last_error[sizeof(ftp_state.last_error) - 1] = '\0';
  162. // LOG_E("Error: %s", error);
  163. // }
  164. //}
  165. //
  166. //static uint8_t acquire_operation_lock(void)
  167. //{
  168. // if (rt_mutex_take(&ftp_mutex, 3000) != RT_EOK)
  169. // {
  170. // return 0;
  171. // }
  172. //
  173. // if (ftp_state.busy_flag)
  174. // {
  175. // rt_mutex_release(&ftp_mutex);
  176. // return 0;
  177. // }
  178. //
  179. // ftp_state.busy_flag = 1;
  180. // memset(ftp_state.last_error, 0, sizeof(ftp_state.last_error));
  181. // ftp_state.total_transferred = 0;
  182. //
  183. // memset(ftp_state.current_local_file, 0, sizeof(ftp_state.current_local_file));
  184. // memset(ftp_state.current_remote_file, 0, sizeof(ftp_state.current_remote_file));
  185. // ftp_state.current_file_size = 0;
  186. // ftp_state.first_block_size = 0;
  187. // ftp_state.first_block_ready = 0;
  188. //
  189. // return 1;
  190. //}
  191. //
  192. //static void release_operation_lock(void)
  193. //{
  194. // ftp_state.busy_flag = 0;
  195. // rt_mutex_release(&ftp_mutex);
  196. //}
  197. //
  198. //static ftp_app_result_t ftp_connect(zftp_client **ftp)
  199. //{
  200. // *ftp = zFTP_login(FTP_APP_CONFIG_USERNAME, FTP_APP_CONFIG_PASSWORD,
  201. // FTP_APP_CONFIG_SERVER_ADDR);
  202. // if (*ftp == RT_NULL)
  203. // {
  204. // LOG_E("FTP login failed");
  205. // return FTP_APP_RESULT_CONNECT_FAILED;
  206. // }
  207. //
  208. // LOG_I("FTP connected");
  209. // return FTP_APP_RESULT_SUCCESS;
  210. //}
  211. //
  212. //static void ftp_disconnect(zftp_client *ftp)
  213. //{
  214. // if (ftp)
  215. // {
  216. // zFTP_quit(ftp);
  217. // }
  218. //}
  219. //
  220. //static ftp_app_result_t ensure_remote_path(zftp_client *ftp, const char *path)
  221. //{
  222. // int retry;
  223. //
  224. // for (retry = 0; retry < 3; retry++)
  225. // {
  226. // if (zFTP_change_path(ftp, path) == RT_EOK)
  227. // {
  228. // return FTP_APP_RESULT_SUCCESS;
  229. // }
  230. // if (retry < 2)
  231. // {
  232. // rt_thread_mdelay(100);
  233. // }
  234. // }
  235. //
  236. // LOG_E("Cannot access path: %s", path);
  237. // return FTP_APP_RESULT_PATH_ERROR;
  238. //}
  239. //
  240. //static ftp_app_result_t prepare_file_for_upload(const char *local_filename)
  241. //{
  242. // ch376_result_t result;
  243. // uint32_t file_size = 0;
  244. //
  245. // LOG_I("Preparing file: %s", local_filename);
  246. //
  247. // memset(ftp_state.current_local_file, 0, sizeof(ftp_state.current_local_file));
  248. // memset(ftp_state.current_remote_file, 0, sizeof(ftp_state.current_remote_file));
  249. // ftp_state.current_file_size = 0;
  250. // ftp_state.first_block_size = 0;
  251. // ftp_state.first_block_ready = 0;
  252. //
  253. // result = ch376.init();
  254. // if (result != CH376_RESULT_SUCCESS)
  255. // {
  256. // LOG_E("CH376 init failed: %d", result);
  257. // return FTP_APP_RESULT_DISK_NOT_READY;
  258. // }
  259. //
  260. // result = ch376.switch_mode(CH376_MODE_SD_CARD);
  261. // if (result != CH376_RESULT_SUCCESS)
  262. // {
  263. // ch376.deinit();
  264. // LOG_E("Switch to SD mode failed: %d", result);
  265. // return FTP_APP_RESULT_DISK_NOT_READY;
  266. // }
  267. //
  268. // result = ch376.file_exists(local_filename);
  269. // if (result != CH376_RESULT_SUCCESS)
  270. // {
  271. // ch376.deinit();
  272. // LOG_E("File not found: %s", local_filename);
  273. // return FTP_APP_RESULT_FILE_NOT_FOUND;
  274. // }
  275. //
  276. // result = ch376.file_open(local_filename);
  277. // if (result != CH376_RESULT_SUCCESS)
  278. // {
  279. // ch376.deinit();
  280. // LOG_E("Cannot open file: %s", local_filename);
  281. // return FTP_APP_RESULT_ERROR;
  282. // }
  283. //
  284. // result = ch376.file_getsize(&file_size);
  285. // if (result != CH376_RESULT_SUCCESS)
  286. // {
  287. // ch376.file_close();
  288. // ch376.deinit();
  289. // LOG_E("Cannot get file size: %s", local_filename);
  290. // return FTP_APP_RESULT_ERROR;
  291. // }
  292. //
  293. // LOG_I("File size: %lu bytes", file_size);
  294. //
  295. // if (file_size > 0)
  296. // {
  297. // uint32_t to_read = FTP_APP_UPLOAD_BUFFER_SIZE;
  298. // if (to_read > file_size)
  299. // {
  300. // to_read = file_size;
  301. // }
  302. //
  303. // result = ch376.file_read(ftp_state.first_block_buffer, to_read, &ftp_state.first_block_size);
  304. // if (result != CH376_RESULT_SUCCESS)
  305. // {
  306. // LOG_E("Failed to read first block");
  307. // ftp_state.first_block_size = 0;
  308. // }
  309. // else
  310. // {
  311. // ftp_state.first_block_ready = 1;
  312. // LOG_I("First %lu bytes cached for immediate response", ftp_state.first_block_size);
  313. // }
  314. // }
  315. //
  316. // ch376.file_close();
  317. // ch376.deinit();
  318. //
  319. // strncpy(ftp_state.current_local_file, local_filename,
  320. // sizeof(ftp_state.current_local_file) - 1);
  321. // ftp_state.current_local_file[sizeof(ftp_state.current_local_file) - 1] = '\0';
  322. //
  323. // ftp_state.current_file_size = file_size;
  324. //
  325. // const char *basename = local_filename;
  326. // if (local_filename[0] == '/')
  327. // {
  328. // basename = local_filename + 1;
  329. // }
  330. // snprintf(ftp_state.current_remote_file, sizeof(ftp_state.current_remote_file),
  331. // "%s_%s", qxz.Param.device_sn, basename);
  332. //
  333. // LOG_I("File prepared: %s -> %s (%lu bytes)",
  334. // ftp_state.current_local_file, ftp_state.current_remote_file,
  335. // ftp_state.current_file_size);
  336. //
  337. // return FTP_APP_RESULT_SUCCESS;
  338. //}
  339. //
  340. //static ftp_app_result_t upload_single_csv(const char *local_filename, zftp_client *ftp)
  341. //{
  342. // LOG_I("Starting upload: %s", local_filename);
  343. //
  344. // ftp_app_result_t result = prepare_file_for_upload(local_filename);
  345. // if (result != FTP_APP_RESULT_SUCCESS)
  346. // {
  347. // return result;
  348. // }
  349. //
  350. // zFTP_set_callback(ftp, RT_NULL, csv_read_callback, RT_NULL);
  351. //
  352. // if (ensure_remote_path(ftp, FTP_APP_CONFIG_CSV_PATH) != FTP_APP_RESULT_SUCCESS)
  353. // {
  354. // LOG_E("Cannot change to CSV path");
  355. // return FTP_APP_RESULT_PATH_ERROR;
  356. // }
  357. //
  358. // LOG_I("Waiting 200ms for FTP server stabilization...");
  359. // rt_thread_mdelay(200);
  360. //
  361. // LOG_I("Uploading to FTP: %s", ftp_state.current_remote_file);
  362. //
  363. // int retry_count;
  364. // for (retry_count = 0; retry_count < 10; retry_count++)
  365. // {
  366. // if (retry_count > 0)
  367. // {
  368. // LOG_W("Retry attempt %d for %s", retry_count, ftp_state.current_remote_file);
  369. // rt_thread_mdelay(200 * retry_count);
  370. // }
  371. //
  372. // uint32_t start_time = rt_tick_get();
  373. //
  374. // if (zFTP_upload_file(ftp, ftp_state.current_remote_file) == RT_EOK)
  375. // {
  376. // uint32_t end_time = rt_tick_get();
  377. // uint32_t duration = (end_time - start_time) * 1000 / RT_TICK_PER_SECOND;
  378. //
  379. // ftp_state.total_transferred += ftp_state.current_file_size;
  380. // LOG_I("Upload completed: %s -> %s (%lu bytes, took %lu ms, retry=%d)",
  381. // ftp_state.current_local_file, ftp_state.current_remote_file,
  382. // ftp_state.current_file_size, duration, retry_count);
  383. //
  384. // memset(ftp_state.current_local_file, 0, sizeof(ftp_state.current_local_file));
  385. // memset(ftp_state.current_remote_file, 0, sizeof(ftp_state.current_remote_file));
  386. // ftp_state.current_file_size = 0;
  387. // ftp_state.first_block_size = 0;
  388. // ftp_state.first_block_ready = 0;
  389. //
  390. // return FTP_APP_RESULT_SUCCESS;
  391. // }
  392. // else
  393. // {
  394. // uint32_t elapsed = (rt_tick_get() - start_time) * 1000 / RT_TICK_PER_SECOND;
  395. // LOG_W("Upload attempt %d failed after %lu ms", retry_count + 1, elapsed);
  396. // }
  397. // }
  398. //
  399. // LOG_E("All upload attempts failed for: %s", ftp_state.current_remote_file);
  400. // return FTP_APP_RESULT_ERROR;
  401. //}
  402. //
  403. //static ftp_app_result_t upload_all_csv(void)
  404. //{
  405. // if (!acquire_operation_lock())
  406. // {
  407. // return FTP_APP_RESULT_ERROR;
  408. // }
  409. //
  410. // LOG_I("Starting CSV upload...");
  411. //
  412. // zftp_client *ftp = NULL;
  413. // ftp_app_result_t result;
  414. // int success_count = 0;
  415. //
  416. // result = ftp_connect(&ftp);
  417. // if (result != FTP_APP_RESULT_SUCCESS)
  418. // {
  419. // release_operation_lock();
  420. // return result;
  421. // }
  422. //
  423. // csv_bitmap_init();
  424. //
  425. // time_t now = time(NULL);
  426. // struct tm current_tm;
  427. // struct tm *p = localtime(&now);
  428. // if (!p)
  429. // {
  430. // ftp_disconnect(ftp);
  431. // release_operation_lock();
  432. // return FTP_APP_RESULT_ERROR;
  433. // }
  434. // current_tm = *p;
  435. //
  436. // int today_offset = calc_day_offset_from_tm(&current_tm);
  437. // for (int offset = 0; offset <= today_offset; offset++)
  438. // {
  439. // if (!csv_bitmap_is_exist_by_offset(offset))
  440. // {
  441. // continue;
  442. // }
  443. //
  444. // struct tm file_tm;
  445. // if (offset_to_date(offset, &file_tm) != 0)
  446. // {
  447. // continue;
  448. // }
  449. //
  450. // int yy = file_tm.tm_year % 100;
  451. // int mm = file_tm.tm_mon + 1;
  452. // int dd = file_tm.tm_mday;
  453. //
  454. // char local_filename[13];
  455. // snprintf(local_filename, sizeof(local_filename), "/%02d%02d%02d.CSV", yy, mm, dd);
  456. //
  457. // LOG_I("[%d] Uploading: %s", success_count + 1, local_filename);
  458. //
  459. // if (upload_single_csv(local_filename, ftp) == FTP_APP_RESULT_SUCCESS)
  460. // {
  461. // success_count++;
  462. // }
  463. //
  464. // rt_thread_mdelay(50);
  465. // }
  466. //
  467. // result = (success_count > 0) ? FTP_APP_RESULT_SUCCESS : FTP_APP_RESULT_ERROR;
  468. //
  469. // ftp_disconnect(ftp);
  470. // release_operation_lock();
  471. //
  472. // LOG_I("CSV upload complete: %d files", success_count);
  473. // return result;
  474. //}
  475. //
  476. //static ftp_app_result_t upload_csv_by_date(int year, int month, int day)
  477. //{
  478. // if (!acquire_operation_lock())
  479. // {
  480. // return FTP_APP_RESULT_ERROR;
  481. // }
  482. //
  483. // char local_filename[13];
  484. // snprintf(local_filename, sizeof(local_filename), "/%02d%02d%02d.CSV", year, month, day);
  485. //
  486. // LOG_I("Uploading by date: %s", local_filename);
  487. //
  488. // zftp_client *ftp = NULL;
  489. // ftp_app_result_t result;
  490. //
  491. // result = ftp_connect(&ftp);
  492. // if (result != FTP_APP_RESULT_SUCCESS)
  493. // {
  494. // release_operation_lock();
  495. // return result;
  496. // }
  497. //
  498. // result = upload_single_csv(local_filename, ftp);
  499. //
  500. // ftp_disconnect(ftp);
  501. // release_operation_lock();
  502. //
  503. // return result;
  504. //}
  505. //
  506. //static ftp_app_result_t upload_csv_file(const char *filename)
  507. //{
  508. // if (!filename)
  509. // {
  510. // return FTP_APP_RESULT_ERROR;
  511. // }
  512. //
  513. // if (!acquire_operation_lock())
  514. // {
  515. // return FTP_APP_RESULT_ERROR;
  516. // }
  517. //
  518. // LOG_I("Uploading file: %s", filename);
  519. //
  520. // zftp_client *ftp = NULL;
  521. // ftp_app_result_t result;
  522. //
  523. // result = ftp_connect(&ftp);
  524. // if (result != FTP_APP_RESULT_SUCCESS)
  525. // {
  526. // release_operation_lock();
  527. // return result;
  528. // }
  529. //
  530. // result = upload_single_csv(filename, ftp);
  531. //
  532. // ftp_disconnect(ftp);
  533. // release_operation_lock();
  534. //
  535. // return result;
  536. //}
  537. //
  538. //static ftp_app_result_t test_connection(void)
  539. //{
  540. // if (!acquire_operation_lock())
  541. // {
  542. // return FTP_APP_RESULT_ERROR;
  543. // }
  544. //
  545. // LOG_I("Testing FTP connection...");
  546. //
  547. // zftp_client *ftp = NULL;
  548. // ftp_app_result_t result = ftp_connect(&ftp);
  549. //
  550. // if (result == FTP_APP_RESULT_SUCCESS)
  551. // {
  552. // LOG_I("FTP connection OK");
  553. // ftp_disconnect(ftp);
  554. // }
  555. // else
  556. // {
  557. // LOG_E("FTP connection failed");
  558. // }
  559. //
  560. // release_operation_lock();
  561. // return result;
  562. //}
  563. //
  564. //static uint8_t is_busy(void)
  565. //{
  566. // return ftp_state.busy_flag;
  567. //}
  568. //
  569. //static const char *get_last_error(void)
  570. //{
  571. // return ftp_state.last_error;
  572. //}
  573. //
  574. //static uint32_t get_total_transferred(void)
  575. //{
  576. // return ftp_state.total_transferred;
  577. //}
  578. //
  579. //ftp_app_ops_t ftp_app = {
  580. // .upload_all_csv = upload_all_csv,
  581. // .upload_csv_by_date = upload_csv_by_date,
  582. // .upload_ecode_csv = upload_ecode_csv,
  583. // .upload_csv_file = upload_csv_file,
  584. // .test_connection = test_connection,
  585. // .is_busy = is_busy,
  586. // .get_last_error = get_last_error,
  587. // .get_total_transferred = get_total_transferred
  588. //};
  589. //
  590. //static int ftp_app_init(void)
  591. //{
  592. // rt_mutex_init(&ftp_mutex, "ftp_app", RT_IPC_FLAG_FIFO);
  593. // csv_bitmap_init();
  594. // ftp_state.initialized = 1;
  595. // LOG_I("FTP app initialized");
  596. // return RT_EOK;
  597. //}
  598. //INIT_APP_EXPORT(ftp_app_init);
  599. //
  600. //#ifdef FINSH_USING_MSH
  601. //#include <finsh.h>
  602. //
  603. //void upload_all_CSV(void)
  604. //{
  605. // ftp_app_result_t result = ftp_app.upload_all_csv();
  606. // if (result == FTP_APP_RESULT_SUCCESS)
  607. // {
  608. // rt_kprintf("CSV upload completed successfully\n");
  609. // }
  610. // else
  611. // {
  612. // rt_kprintf("CSV upload failed: %s\n", ftp_app.get_last_error());
  613. // }
  614. //}
  615. //MSH_CMD_EXPORT(upload_all_CSV, Upload all CSV files via FTP);
  616. //
  617. //void ftp_test(void)
  618. //{
  619. // ftp_app_result_t result = ftp_app.test_connection();
  620. // if (result == FTP_APP_RESULT_SUCCESS)
  621. // {
  622. // rt_kprintf("FTP connection test: SUCCESS\n");
  623. // }
  624. // else
  625. // {
  626. // rt_kprintf("FTP connection test: FAILED\n");
  627. // }
  628. //}
  629. //MSH_CMD_EXPORT(ftp_test, Test FTP connection);
  630. //
  631. //#endif
  632. //
  633. //static ftp_app_result_t upload_ecode_csv_internal(const char *local_filename, zftp_client *ftp)
  634. //{
  635. // LOG_I("Starting ECODE upload: %s", local_filename);
  636. //
  637. // ftp_app_result_t result = prepare_file_for_upload(local_filename);
  638. // if (result != FTP_APP_RESULT_SUCCESS)
  639. // {
  640. // return result;
  641. // }
  642. //
  643. // zFTP_set_callback(ftp, RT_NULL, csv_read_callback, RT_NULL);
  644. //
  645. // if (ensure_remote_path(ftp, FTP_APP_CONFIG_CSV_PATH) != FTP_APP_RESULT_SUCCESS)
  646. // {
  647. // LOG_E("Cannot change to CSV path");
  648. // return FTP_APP_RESULT_PATH_ERROR;
  649. // }
  650. //
  651. // char remote_ecode_filename[64];
  652. // snprintf(remote_ecode_filename, sizeof(remote_ecode_filename),
  653. // "%s_ECODE.CSV", qxz.Param.device_sn);
  654. //
  655. // LOG_I("Waiting 200ms for FTP server stabilization...");
  656. // rt_thread_mdelay(200);
  657. //
  658. // LOG_I("Uploading ECODE to FTP: %s -> %s",
  659. // ftp_state.current_local_file, remote_ecode_filename);
  660. //
  661. // int retry_count;
  662. // for (retry_count = 0; retry_count < 3; retry_count++)
  663. // {
  664. // if (retry_count > 0)
  665. // {
  666. // LOG_W("Retry attempt %d for ECODE", retry_count);
  667. // rt_thread_mdelay(200 * retry_count);
  668. // }
  669. //
  670. // uint32_t start_time = rt_tick_get();
  671. //
  672. // if (zFTP_upload_file(ftp, remote_ecode_filename) == RT_EOK)
  673. // {
  674. // uint32_t end_time = rt_tick_get();
  675. // uint32_t duration = (end_time - start_time) * 1000 / RT_TICK_PER_SECOND;
  676. //
  677. // ftp_state.total_transferred += ftp_state.current_file_size;
  678. // LOG_I("ECODE upload completed: %s -> %s (%lu bytes, took %lu ms, retry=%d)",
  679. // ftp_state.current_local_file, remote_ecode_filename,
  680. // ftp_state.current_file_size, duration, retry_count);
  681. //
  682. // memset(ftp_state.current_local_file, 0, sizeof(ftp_state.current_local_file));
  683. // memset(ftp_state.current_remote_file, 0, sizeof(ftp_state.current_remote_file));
  684. // ftp_state.current_file_size = 0;
  685. // ftp_state.first_block_size = 0;
  686. // ftp_state.first_block_ready = 0;
  687. //
  688. // return FTP_APP_RESULT_SUCCESS;
  689. // }
  690. // else
  691. // {
  692. // uint32_t elapsed = (rt_tick_get() - start_time) * 1000 / RT_TICK_PER_SECOND;
  693. // LOG_W("ECODE upload attempt %d failed after %lu ms", retry_count + 1, elapsed);
  694. // }
  695. // }
  696. //
  697. // LOG_E("All ECODE upload attempts failed");
  698. // return FTP_APP_RESULT_ERROR;
  699. //}
  700. //
  701. //static ftp_app_result_t upload_ecode_csv(void)
  702. //{
  703. // if (!acquire_operation_lock())
  704. // {
  705. // return FTP_APP_RESULT_ERROR;
  706. // }
  707. //
  708. // LOG_I("Starting ECODE.CSV upload...");
  709. //
  710. // zftp_client *ftp = NULL;
  711. // ftp_app_result_t result;
  712. //
  713. // result = ftp_connect(&ftp);
  714. // if (result != FTP_APP_RESULT_SUCCESS)
  715. // {
  716. // release_operation_lock();
  717. // return result;
  718. // }
  719. //
  720. // result = upload_ecode_csv_internal("/ECODE.CSV", ftp);
  721. //
  722. // ftp_disconnect(ftp);
  723. // release_operation_lock();
  724. //
  725. // if (result == FTP_APP_RESULT_SUCCESS)
  726. // {
  727. // LOG_I("ECODE.CSV upload completed successfully");
  728. // }
  729. // else
  730. // {
  731. // LOG_E("ECODE.CSV upload failed");
  732. // }
  733. //
  734. // return result;
  735. //}
  736. //
  737. //void upload_ecode(void)
  738. //{
  739. // ftp_app_result_t result = ftp_app.upload_ecode_csv();
  740. // if (result == FTP_APP_RESULT_SUCCESS)
  741. // {
  742. // rt_kprintf("ECODE.CSV upload completed successfully\n");
  743. // }
  744. // else
  745. // {
  746. // rt_kprintf("ECODE.CSV upload failed: %s\n", ftp_app.get_last_error());
  747. // }
  748. //}
  749. //MSH_CMD_EXPORT(upload_ecode, Upload ECODE.CSV file via FTP);