ftp_demo2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #if 0
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "ql_api_osi.h"
  17. #include "ql_api_nw.h"
  18. #include "ql_log.h"
  19. #include "ql_api_datacall.h"
  20. #include "ql_ftp_client.h"
  21. #include "ql_fs.h"
  22. #include "ql_ssl.h"
  23. #define QL_FTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_FTP_LOG(msg, ...) QL_LOG(QL_FTP_LOG_LEVEL, "ql_FTP", msg, ##__VA_ARGS__)
  25. #define QL_FTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_FTP", msg, ##__VA_ARGS__)
  26. static ql_task_t ftp_task = NULL;
  27. #define PRINT_BUF_SIZE 65
  28. #define MAX_PATH_SIZE 256
  29. int ftp_app_net_datacall_fxn(uint8_t nSim,int profile_idx)
  30. {
  31. int ret = 0;
  32. int i = 0;
  33. ql_data_call_info_s info;
  34. char ip4_addr_str[16] = {0};
  35. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  36. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  37. {
  38. i++;
  39. QL_FTP_LOG("wait network register count: %d",i);
  40. ql_rtos_task_sleep_s(1);
  41. }
  42. if(ret != 0)
  43. {
  44. QL_FTP_LOG("network register failure!");
  45. return -1;
  46. }
  47. QL_FTP_LOG("network register sucess nSim = %d,i = %d",nSim,i);
  48. ret = ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  49. QL_FTP_LOG("network datacall asyn mode ret =%d",ret);
  50. ret = ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  51. QL_FTP_LOG("network datacall sucess ret =%d",ret);
  52. if(ret != 0){
  53. QL_FTP_LOG("data call failure!");
  54. }
  55. if(ql_get_data_call_info(nSim, profile_idx, &info) != 0){
  56. QL_FTP_LOG("get datacall info ret: %d", ret);
  57. ql_stop_data_call(nSim, profile_idx);
  58. return -1;
  59. }
  60. QL_FTP_LOG("info->profile_idx: %d", info.profile_idx);
  61. QL_FTP_LOG("info->ip_version: %d", info.ip_version);
  62. QL_FTP_LOG("info->v4.state: %d", info.v4.state);
  63. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  64. QL_FTP_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  65. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  66. QL_FTP_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  67. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  68. QL_FTP_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  69. return 0;
  70. }
  71. char hostname[64]="120.27.243.131";
  72. char username[]="qx";
  73. char password[]="qx900";
  74. char get_filename[] = "4G04/MCU/test_7.3.txt";
  75. char put_filename[] = "FTPtest_put.txt";
  76. char dload_filepath[] = "/quec_ftp_test_path";
  77. int ciphersuites=0xFFFF;
  78. int ssl_ctx=2;
  79. char *chain_path="UFS:cacert.pem";
  80. static void ftp_app_thread(void * arg)
  81. {
  82. int ret = 0;
  83. QFILE fd = 0;
  84. void * client = NULL;
  85. char buf[MAX_PATH_SIZE]={0};
  86. int profile_idx = 1;
  87. uint8_t nSim = 0;
  88. uint16_t sim_cid;
  89. int i = 0;
  90. QL_FTP_CLIENT_FILE_INFO_T ql_list_file[5]={0};
  91. ql_rtos_task_sleep_s(10);
  92. QL_FTP_LOG("=========================== ftp demo2 start ==============================");
  93. if(QL_DATACALL_SUCCESS != ql_bind_sim_and_profile(nSim, profile_idx, &sim_cid))
  94. {
  95. QL_FTP_LOG("nSim or profile_idx is invalid!!!!");
  96. ret = -1;
  97. goto exit;
  98. }
  99. if(ftp_app_net_datacall_fxn(nSim, profile_idx) != 0)
  100. {
  101. QL_FTP_LOG("net datacall failed!");
  102. goto exit;
  103. }
  104. client = ql_ftp_client_new();
  105. while (client)
  106. {
  107. QL_FTP_LOG("=====================ftp_client_test[]======================");
  108. QL_FTP_LOG("====================[open test]=======>>>>>>>>>");
  109. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SIM_CID, sim_cid);
  110. ql_ftp_client_setopt(client, QL_FTP_CLIENT_OPT_START_POS,0);
  111. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_CTX, ssl_ctx);
  112. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_CIPHERSUITE, &ciphersuites);
  113. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_VERIFY_LEVEL, QL_SSL_VERIFY_SERVER);
  114. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_VERSION, QL_SSL_VERSION_ALL);
  115. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_TRANSPORT, QL_SSL_TLS_PROTOCOL);
  116. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_ENABLE, QL_FTP_CLIENT_SSL_TYPE_EXP);
  117. if(ql_file_exist(chain_path)==QL_FILE_OK)
  118. {
  119. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_CACERT_PATH, chain_path);
  120. }
  121. else
  122. {
  123. QL_FTP_LOG("cacert not exist\r\n");
  124. ql_ftp_client_release(client);
  125. goto exit;
  126. }
  127. ret=ql_ftp_client_open(client,hostname,username,password);
  128. QL_FTP_LOG("ftp open[%s] ret = %d",hostname,ret);
  129. if(ret != 0)
  130. {
  131. break;
  132. }
  133. /*
  134. ret=ql_ftp_client_mkdir(client,dload_filepath);
  135. QL_FTP_LOG("mkdir[%s] ret=%d\n",dload_filepath,ret);
  136. ret=ql_ftp_client_cwd(client,dload_filepath);
  137. QL_FTP_LOG("cwd[%s] ret=%d\n",dload_filepath,ret);
  138. ret=ql_ftp_client_pwd(client, buf, sizeof(buf));
  139. QL_FTP_LOG("pwd remotedir:%s ret=%d\n",buf,ret);
  140. QL_FTP_LOG("=====================[get test]============>>>>>>>>>");
  141. ret= ql_ftp_client_get_ex(client,get_filename,NULL,NULL,NULL);
  142. QL_FTP_LOG("get[%s] ret %d",get_filename,ret);
  143. fd = ql_fopen(get_filename,"a+");
  144. if(fd < 0)
  145. {
  146. QL_FTP_LOG("open file failed: %d!",fd);
  147. return;
  148. }
  149. else
  150. {
  151. QL_FTP_LOG("open file [%s] size = %d fd = %d!",get_filename,ql_fsize(fd),fd);
  152. ql_fclose(fd);
  153. }
  154. QL_FTP_LOG("========================[put test]=============>>>>>>>>>");
  155. ql_rename(get_filename,put_filename);
  156. fd = ql_fopen(put_filename,"a+");
  157. if(fd < 0)
  158. {
  159. QL_FTP_LOG("open file failed: %d!",fd);
  160. return;
  161. }
  162. else
  163. {
  164. QL_FTP_LOG("file fd =%d size =%d\n",fd,ql_fsize(fd));
  165. ql_fclose(fd);
  166. }
  167. ret= ql_ftp_client_put_ex(client,put_filename,NULL,NULL,NULL);
  168. QL_FTP_LOG("ftp put[%s] ret=%d\n",put_filename,ret);
  169. QL_FTP_LOG("========================[list test]=============>>>>>>>>>");
  170. ql_rtos_task_sleep_s(10);
  171. ret=ql_ftp_client_list(client,dload_filepath,NULL,ql_list_file, 5);
  172. QL_FTP_LOG("list[%s] ret=%d\n",dload_filepath,ret);
  173. for(i=0;i<ret;i++)
  174. {
  175. 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,
  176. ql_list_file[i].filename,
  177. ql_list_file[i].type,
  178. ql_list_file[i].size,
  179. ql_list_file[i].group,
  180. ql_list_file[i].permissions,
  181. ql_list_file[i].timestamp.tm_year+1900,
  182. ql_list_file[i].timestamp.tm_mon+1,
  183. ql_list_file[i].timestamp.tm_mday,
  184. ql_list_file[i].timestamp.tm_hour,
  185. ql_list_file[i].timestamp.tm_min,
  186. ql_list_file[i].timestamp.tm_sec
  187. );
  188. }
  189. QL_FTP_LOG("=========================[delete test]=======>>>>>>>>>");
  190. ret=ql_ftp_client_delete(client,put_filename);
  191. QL_FTP_LOG("ftp delete[%s] ret=%d\n",put_filename,ret);
  192. QL_FTP_LOG("==========================[close test]=======>>>>>>>>>");
  193. ret=ql_ftp_client_close(client);
  194. QL_FTP_LOG("ftp close ret=%d\n",ret);
  195. QL_FTP_LOG("=================================ftp_client_test[] end============================\n");
  196. */
  197. ql_rtos_task_sleep_s(10);
  198. }
  199. ql_ftp_client_release(client);
  200. exit:
  201. ql_rtos_task_delete(ftp_task);
  202. return;
  203. }
  204. int ql_ftp_app_demo_init(void)
  205. {
  206. QlOSStatus err = QL_OSI_SUCCESS;
  207. err = ql_rtos_task_create(&ftp_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QftpApp2", ftp_app_thread, NULL, 5);
  208. if(err != QL_OSI_SUCCESS)
  209. {
  210. QL_FTP_LOG("ftp_app_demo2 init failed");
  211. }
  212. else
  213. {
  214. QL_FTP_LOG("ftp_app_demo2 init ok");
  215. }
  216. return err;
  217. }
  218. #endif