ftp_demo2.c 8.3 KB

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