ftp_demo.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_nw.h"
  17. #include "ql_log.h"
  18. #include "ql_api_datacall.h"
  19. #include "ql_ftp_client.h"
  20. #include "ql_fs.h"
  21. #include "ql_ssl.h"
  22. #define QL_FTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  23. #define QL_FTP_LOG(msg, ...) QL_LOG(QL_FTP_LOG_LEVEL, "ql_FTP", msg, ##__VA_ARGS__)
  24. #define QL_FTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_FTP", msg, ##__VA_ARGS__)
  25. static ql_task_t ftp_task = NULL;
  26. #define PRINT_BUF_SIZE 65
  27. #define MAX_PATH_SIZE 256
  28. #define DISK "UFS:"
  29. #define UPDATE_DIR "update_dir"
  30. #define FILE_NAME "update.bin"
  31. #define DEST_FILE_NAME "dest_file.txt"
  32. #define DIR_PATH ""DISK"/"UPDATE_DIR""
  33. #define FILE_PATH ""DIR_PATH"/"FILE_NAME""
  34. int ftp_app_net_datacall_fxn(uint8_t nSim,int profile_idx)
  35. {
  36. int ret = 0;
  37. int i = 0;
  38. ql_data_call_info_s info;
  39. char ip4_addr_str[16] = {0};
  40. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  41. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  42. {
  43. i++;
  44. QL_FTP_LOG("wait network register count: %d",i);
  45. ql_rtos_task_sleep_s(1);
  46. }
  47. if(ret != 0)
  48. {
  49. QL_FTP_LOG("network register failure!");
  50. return -1;
  51. }
  52. QL_FTP_LOG("network register sucess nSim = %d,i = %d",nSim,i);
  53. ret = ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  54. QL_FTP_LOG("network datacall asyn mode ret =%d",ret);
  55. ret = ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  56. QL_FTP_LOG("network datacall sucess ret =%d",ret);
  57. if(ret != 0){
  58. QL_FTP_LOG("data call failure!");
  59. }
  60. if(ql_get_data_call_info(nSim, profile_idx, &info) != 0){
  61. QL_FTP_LOG("get datacall info ret: %d", ret);
  62. ql_stop_data_call(nSim, profile_idx);
  63. return -1;
  64. }
  65. QL_FTP_LOG("info->profile_idx: %d", info.profile_idx);
  66. QL_FTP_LOG("info->ip_version: %d", info.ip_version);
  67. QL_FTP_LOG("info->v4.state: %d", info.v4.state);
  68. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  69. QL_FTP_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  70. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  71. QL_FTP_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  72. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  73. QL_FTP_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  74. return 0;
  75. }
  76. char hostname[64]="120.27.243.131";
  77. char username[]="qx";
  78. char password[]="qx900";
  79. char get_filename[] = "4G04/MCU/test_7.3-1.txt";
  80. #if 0
  81. size_t mcu_data_write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  82. {
  83. QL_FTP_LOG("********** fota_ftp_write_cb **********");
  84. int ret = 0;
  85. QFILE fd = -1;
  86. fota_ftp_client_t *user_data;
  87. user_data = (fota_ftp_client_t *)stream;
  88. //"ab" 打开或新建一个二进制文件,只允许在文件末尾追写
  89. fd = ql_fopen("U:4G04/MCU/test_7.3.txt", "ab");
  90. //回写数据前将状态置为中断,ql_fwrite出现异常无法继续执行下去时,将会一直保持此状态,再次调用FTP FOTA线程时,将使用本次的断点信息继续下载
  91. user_data->down_state = DOWN_INTR;
  92. ret = ql_fwrite(ptr, size, nmemb, fd);
  93. QL_FTP_LOG("ql_fwrite ret = %d",ret);
  94. ql_fclose(fd);
  95. }
  96. #endif
  97. extern uint8_t OTA_FLAG;
  98. static void ftp_app_thread(void * arg)
  99. {
  100. int ret = 0;
  101. void * client = NULL;
  102. int profile_idx = 1;
  103. uint8_t nSim = 0;
  104. uint16_t sim_cid;
  105. ql_rtos_task_sleep_s(10);
  106. QL_FTP_LOG("=========================== ftp demo2 start ==============================");
  107. if(QL_DATACALL_SUCCESS != ql_bind_sim_and_profile(nSim, profile_idx, &sim_cid))
  108. {
  109. QL_FTP_LOG("nSim or profile_idx is invalid!!!!");
  110. ret = -1;
  111. goto exit;
  112. }
  113. if(ftp_app_net_datacall_fxn(nSim, profile_idx) != 0)
  114. {
  115. QL_FTP_LOG("net datacall failed!");
  116. goto exit;
  117. }
  118. client = ql_ftp_client_new();
  119. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SIM_CID, sim_cid);
  120. ql_ftp_client_setopt(client, QL_FTP_CLIENT_OPT_PDP_CID,1);
  121. ql_ftp_client_setopt(client, QL_FTP_CLIENT_OPT_START_POS,0);
  122. ret=ql_ftp_client_open(client,hostname,username,password);
  123. QL_FTP_LOG("ftp open[%s] ret = %d",hostname,ret);
  124. QL_FTP_LOG("=====================ftp_client_test[]======================");
  125. uint8 flag = 1;
  126. while (client)
  127. {
  128. QL_FTP_LOG("=====================[get test start]============>>>>>>>>>");
  129. if(1)//if(0 == flag)
  130. {
  131. ql_rtos_task_sleep_s(5);
  132. continue;
  133. }
  134. int i = 0;
  135. QL_FTP_CLIENT_FILE_INFO_T ql_list_file[5]={0};
  136. ret=ql_ftp_client_list(client,"/4G04/MCU","HC32_4G04",ql_list_file, 5);
  137. QL_FTP_LOG("\nql_ftp_client_list[/] ret=%d\n",ret);
  138. for(i=0;i<ret;i++)
  139. {
  140. QL_FTP_LOG("file%d:%s\n type:%d\n size:%d\n group:%s\n permissions:%d\n time:%04d-%02d-%02d %02d:%02d:%02d\n",i,
  141. ql_list_file[i].filename,
  142. ql_list_file[i].type,
  143. ql_list_file[i].size,
  144. ql_list_file[i].group,
  145. ql_list_file[i].permissions,
  146. ql_list_file[i].timestamp.tm_year+1900,
  147. ql_list_file[i].timestamp.tm_mon+1,
  148. ql_list_file[i].timestamp.tm_mday,
  149. ql_list_file[i].timestamp.tm_hour,
  150. ql_list_file[i].timestamp.tm_min,
  151. ql_list_file[i].timestamp.tm_sec
  152. );
  153. }
  154. ret=ql_ftp_client_cwd(client,"/4G04/MCU");
  155. QL_FTP_LOG("ret=%d\n",ret);
  156. ret= ql_ftp_client_get_ex(client,"test_7.3-5.bin","updata.bin",NULL,NULL);
  157. QL_FTP_LOG("download from server ret=%d",ret);
  158. OTA_FLAG = flag = ret;
  159. //int ql_fread(void * buffer, size_t size, size_t num, QFILE fd);
  160. QL_FTP_LOG("=====================[get test end]============>>>>>>>>>");
  161. ql_rtos_task_sleep_s(5);
  162. }
  163. ql_ftp_client_release(client);
  164. exit:
  165. ql_rtos_task_delete(ftp_task);
  166. return;
  167. }
  168. int ql_ftp_app_demo_init(void)
  169. {
  170. QlOSStatus err = QL_OSI_SUCCESS;
  171. err = ql_rtos_task_create(&ftp_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QftpApp2", ftp_app_thread, NULL, 5);
  172. if(err != QL_OSI_SUCCESS)
  173. {
  174. QL_FTP_LOG("ftp_app_demo init failed");
  175. }
  176. else
  177. {
  178. QL_FTP_LOG("ftp_app_demo init ok");
  179. }
  180. return err;
  181. }