ftp_demo.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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]="120.27.243.131";
  71. char username[]="qx";
  72. char password[]="qx900";
  73. char get_filename[] = "4G04/MCU/test_7.3-1.txt";
  74. #if 0
  75. size_t mcu_data_write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  76. {
  77. QL_FTP_LOG("********** fota_ftp_write_cb **********");
  78. int ret = 0;
  79. QFILE fd = -1;
  80. fota_ftp_client_t *user_data;
  81. user_data = (fota_ftp_client_t *)stream;
  82. //"ab" 打开或新建一个二进制文件,只允许在文件末尾追写
  83. fd = ql_fopen("U:4G04/MCU/test_7.3.txt", "ab");
  84. //回写数据前将状态置为中断,ql_fwrite出现异常无法继续执行下去时,将会一直保持此状态,再次调用FTP FOTA线程时,将使用本次的断点信息继续下载
  85. user_data->down_state = DOWN_INTR;
  86. ret = ql_fwrite(ptr, size, nmemb, fd);
  87. QL_FTP_LOG("ql_fwrite ret = %d",ret);
  88. ql_fclose(fd);
  89. }
  90. #endif
  91. static void ftp_app_thread(void * arg)
  92. {
  93. int ret = 0;
  94. void * client = NULL;
  95. int profile_idx = 1;
  96. uint8_t nSim = 0;
  97. uint16_t sim_cid;
  98. ql_rtos_task_sleep_s(10);
  99. QL_FTP_LOG("=========================== ftp demo2 start ==============================");
  100. if(QL_DATACALL_SUCCESS != ql_bind_sim_and_profile(nSim, profile_idx, &sim_cid))
  101. {
  102. QL_FTP_LOG("nSim or profile_idx is invalid!!!!");
  103. ret = -1;
  104. goto exit;
  105. }
  106. if(ftp_app_net_datacall_fxn(nSim, profile_idx) != 0)
  107. {
  108. QL_FTP_LOG("net datacall failed!");
  109. goto exit;
  110. }
  111. client = ql_ftp_client_new();
  112. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SIM_CID, sim_cid);
  113. ql_ftp_client_setopt(client, QL_FTP_CLIENT_OPT_PDP_CID,1);
  114. ql_ftp_client_setopt(client, QL_FTP_CLIENT_OPT_START_POS,0);
  115. ret=ql_ftp_client_open(client,hostname,username,password);
  116. QL_FTP_LOG("ftp open[%s] ret = %d",hostname,ret);
  117. QL_FTP_LOG("=====================ftp_client_test[]======================");
  118. uint8 i = 1;
  119. while(client)
  120. {
  121. QL_FTP_LOG("=====================[get test start]============>>>>>>>>>");
  122. if(0 == i)
  123. {
  124. ql_rtos_task_sleep_s(5);
  125. continue;
  126. }
  127. int i = 0;
  128. QL_FTP_CLIENT_FILE_INFO_T ql_list_file[5]={0};
  129. ret=ql_ftp_client_list(client,"/4G04/MCU","HC32_4G04_08_10_17_43.bin",ql_list_file, 5);
  130. QL_FTP_LOG("\nql_ftp_client_list[/] ret=%d\n",ret);
  131. for(i=0;i<ret;i++)
  132. {
  133. 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,
  134. ql_list_file[i].filename,
  135. ql_list_file[i].type,
  136. ql_list_file[i].size,
  137. ql_list_file[i].group,
  138. ql_list_file[i].permissions,
  139. ql_list_file[i].timestamp.tm_year+1900,
  140. ql_list_file[i].timestamp.tm_mon+1,
  141. ql_list_file[i].timestamp.tm_mday,
  142. ql_list_file[i].timestamp.tm_hour,
  143. ql_list_file[i].timestamp.tm_min,
  144. ql_list_file[i].timestamp.tm_sec
  145. );
  146. }
  147. ret=ql_ftp_client_cwd(client,"/4G04/MCU");
  148. QL_FTP_LOG("ret=%d\n",ret);
  149. ret= ql_ftp_client_get_ex(client,"HC32_4G04_08_10_17_43.bin",NULL,NULL,NULL);
  150. QL_FTP_LOG("download from server ret=%d",ret);
  151. i = ret;
  152. //int ql_fread(void * buffer, size_t size, size_t num, QFILE fd);
  153. QL_FTP_LOG("=====================[get test end]============>>>>>>>>>");
  154. ql_rtos_task_sleep_s(5);
  155. }
  156. ql_ftp_client_release(client);
  157. exit:
  158. ql_rtos_task_delete(ftp_task);
  159. return;
  160. }
  161. int ql_ftp_app_demo_init(void)
  162. {
  163. QlOSStatus err = QL_OSI_SUCCESS;
  164. QL_FTP_LOG("ftp_app_demo init");
  165. err = ql_rtos_task_create(&ftp_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QftpApp2", ftp_app_thread, NULL, 5);
  166. if(err != QL_OSI_SUCCESS)
  167. {
  168. QL_FTP_LOG("ftp_app_demo init failed");
  169. }
  170. else
  171. {
  172. QL_FTP_LOG("ftp_app_demo init ok");
  173. }
  174. return err;
  175. }