sftp_demo.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "vfs.h"
  16. #include "ql_ftp_client.h"
  17. #include "ql_api_osi.h"
  18. #include "ql_api_nw.h"
  19. #include "ql_log.h"
  20. #include "ql_api_datacall.h"
  21. #include "ql_ftp_client.h"
  22. #include "ql_fs.h"
  23. #include "ql_ssl.h"
  24. #define QL_SFTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  25. #define QL_SFTP_LOG(msg, ...) QL_LOG(QL_SFTP_LOG_LEVEL, "ql_SFTP", msg, ##__VA_ARGS__)
  26. #define QL_SFTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_SFTP", msg, ##__VA_ARGS__)
  27. static ql_task_t sftp_task = NULL;
  28. #define MAX_PATH_SIZE 256
  29. static int lizhu_put_len;
  30. static size_t quec_exec_sftp_get_cb(void* ptr, size_t size, size_t nmemb, void* stream)
  31. {
  32. size_t realsize = size * nmemb;
  33. char* printf_buff = NULL;
  34. printf_buff = (char*)malloc(realsize + 1);
  35. if (printf_buff == NULL)
  36. return 0;
  37. memset(printf_buff, 0, realsize + 1);
  38. memcpy(printf_buff, ptr, realsize + 1);
  39. printf_buff[realsize] = '\0';
  40. QL_SFTP_LOG("quec_exec_ftp_get_cb== %d %s\n", realsize, printf_buff);
  41. free(printf_buff);
  42. return realsize;
  43. }
  44. static size_t quec_exec_sftp_put_cb(void* ptr, size_t size, size_t nmemb, void* stream)
  45. {
  46. char buff[1000] = { 0 };
  47. memset(buff, 0x65, 1000);
  48. QL_SFTP_LOG("quec_exec_ftp_put_cb \n");
  49. if (lizhu_put_len < 1000)
  50. {
  51. lizhu_put_len = 1000;
  52. memcpy(ptr, buff, 1000);
  53. return 1000;
  54. }
  55. else
  56. {
  57. QL_SFTP_LOG("return 1000 success\n");
  58. }
  59. lizhu_put_len = 0;
  60. return 0;
  61. }
  62. static void sftp_app_thread(void *arg)
  63. {
  64. int ret = 0;
  65. int i = 0;
  66. int profile_idx = 1;
  67. ql_data_call_info_s info;
  68. char ip4_addr_str[16] = {0};
  69. void * client = NULL;
  70. char buf[MAX_PATH_SIZE]={0};
  71. int run_num=1;
  72. char hostname[64]="220.180.239.252:22";
  73. char username[]="admin";
  74. char password[]="admin";
  75. QL_FTP_CLIENT_FILE_INFO_T ql_list_file[5]={0};
  76. uint8_t nSim = 0;
  77. uint16_t sim_cid;
  78. ql_rtos_task_sleep_s(10);
  79. QL_SFTP_LOG("========== ftp demo start ==========");
  80. QL_SFTP_LOG("wait for network register done");
  81. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  82. i++;
  83. ql_rtos_task_sleep_s(1);
  84. }
  85. if(ret == 0){
  86. i = 0;
  87. QL_SFTP_LOG("====network registered!!!!====");
  88. }else{
  89. QL_SFTP_LOG("====network register failure!!!!!====");
  90. goto exit;
  91. }
  92. if(QL_DATACALL_SUCCESS != ql_bind_sim_and_profile(nSim, profile_idx, &sim_cid))
  93. {
  94. QL_SFTP_LOG("nSim or profile_idx is invalid!!!!");
  95. ret = -1;
  96. goto exit;
  97. }
  98. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  99. QL_SFTP_LOG("===start data call====");
  100. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  101. QL_SFTP_LOG("===data call result:%d", ret);
  102. if(ret != 0){
  103. QL_SFTP_LOG("====data call failure!!!!=====");
  104. }
  105. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  106. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  107. if(ret != 0){
  108. QL_SFTP_LOG("ql_get_data_call_info ret: %d", ret);
  109. ql_stop_data_call(nSim, profile_idx);
  110. goto exit;
  111. }
  112. QL_SFTP_LOG("info->profile_idx: %d", info.profile_idx);
  113. QL_SFTP_LOG("info->ip_version: %d", info.ip_version);
  114. QL_SFTP_LOG("info->v4.state: %d", info.v4.state);
  115. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  116. QL_SFTP_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  117. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  118. QL_SFTP_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  119. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  120. QL_SFTP_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  121. //ql_rtos_task_create(&ftp_task1, 16 * 1024, 23, "sftp_app1", sftp_app_thread, NULL, 5);
  122. run_num = 0;
  123. client = ql_ftp_client_new();
  124. if( client == NULL )
  125. {
  126. QL_SFTP_LOG("client init fail");
  127. goto exit;
  128. }
  129. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SIM_CID, sim_cid); /*set PDP cid,if not set,using default PDP*/
  130. ql_ftp_client_setopt(client, QL_FTP_CLIENT_SSL_ENABLE, QL_FTP_CLIENT_SSL_TYPE_SSH);
  131. ret=ql_ftp_client_open(client,hostname,username,password);
  132. QL_SFTP_LOG("\nql_ftp_client_open[%s] ret=%d;\n",hostname,ret);
  133. if(ret!=0)
  134. {
  135. QL_SFTP_LOG("client connect fail");
  136. goto exit;
  137. }
  138. do {
  139. ret=ql_ftp_client_pwd(client, buf, sizeof(buf));
  140. QL_SFTP_LOG("\nql_ftp_client_pwd ret=%d; remotedir:%s\n",ret,buf);
  141. ret=ql_ftp_client_cwd(client, "/temp");
  142. QL_SFTP_LOG("\nql_ftp_client_cwd[temp] ret=%d\n",ret);
  143. ret = ql_ftp_client_list(client, ".", NULL, ql_list_file, 5);
  144. if (ret < 0)
  145. {
  146. QL_SFTP_LOG("ql_ftp_client_list error ret = %d", ret);
  147. break;
  148. }
  149. QL_SFTP_LOG("read list number = %d", ret);
  150. for(i=0;i<ret;i++)
  151. {
  152. QL_SFTP_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,
  153. ql_list_file[i].filename,
  154. ql_list_file[i].type,
  155. ql_list_file[i].size,
  156. ql_list_file[i].group,
  157. ql_list_file[i].permissions,
  158. ql_list_file[i].timestamp.tm_year+1900,
  159. ql_list_file[i].timestamp.tm_mon+1,
  160. ql_list_file[i].timestamp.tm_mday,
  161. ql_list_file[i].timestamp.tm_hour,
  162. ql_list_file[i].timestamp.tm_min,
  163. ql_list_file[i].timestamp.tm_sec
  164. );
  165. }
  166. QL_SFTP_LOG("ql_ftp_client_list over remotdir\n");
  167. //ret = ql_ftp_client_put_ex(client, "UFS:20201210test.txt","/temp/1.txt", quec_exec_ftp_put_cb, NULL);
  168. ret = ql_ftp_client_put_ex(client, NULL,"/temp/1.txt", quec_exec_sftp_put_cb, NULL);
  169. if (ret != 0)
  170. {
  171. QL_SFTP_LOG("ql_ftp_client_put_ex error ret = %d", ret);
  172. break;
  173. }
  174. QL_SFTP_LOG("ql_ftp_client_put_ex over");
  175. ret = ql_ftp_client_get_ex(client, "/temp/1.txt", NULL, quec_exec_sftp_get_cb, NULL);
  176. if (ret != 0)
  177. {
  178. QL_SFTP_LOG("ql_ftp_client_get_ex error ret = %d", ret);
  179. break;
  180. }
  181. QL_SFTP_LOG("ql_ftp_client_get_ex over");
  182. ret = ql_ftp_client_delete(client, "/temp/1.txt");
  183. if (ret != 0)
  184. {
  185. QL_SFTP_LOG("ql_ftp_client_delete error ret = %d", ret);
  186. break;
  187. }
  188. QL_SFTP_LOG("ql_ftp_client_delete over");
  189. ret = ql_ftp_client_mkdir(client, "/temp/we");
  190. if (ret != 0)
  191. {
  192. QL_SFTP_LOG("ql_ftp_client_mkdir error ret = %d", ret);
  193. break;
  194. }
  195. QL_SFTP_LOG("ql_ftp_client_mkdir over");
  196. ret = ql_ftp_client_rmdir(client, "/temp/we");
  197. if (ret != 0)
  198. {
  199. QL_SFTP_LOG("ql_ftp_client_rmdir error ret = %d", ret);
  200. break;
  201. }
  202. QL_SFTP_LOG("ql_ftp_client_rmdir over");
  203. run_num++;
  204. QL_SFTP_LOG("==============sftp_client_test loop[%d]================\n",run_num);
  205. ql_rtos_task_sleep_s(5);
  206. }while(run_num < 10);
  207. do{
  208. QL_SFTP_LOG("==============sftp_client_test idle================\n");
  209. ql_rtos_task_sleep_s(5);
  210. }while(1);
  211. exit:
  212. if (client != NULL)
  213. {
  214. ql_ftp_client_release(client); /*release http resources*/
  215. }
  216. ql_rtos_task_delete(sftp_task);
  217. return;
  218. }
  219. int ql_sftp_app_init(void)
  220. {
  221. QlOSStatus err = QL_OSI_SUCCESS;
  222. err = ql_rtos_task_create(&sftp_task, 16 * 1024, APP_PRIORITY_ABOVE_NORMAL, "QsftpApp", sftp_app_thread, NULL, 5);
  223. if (err != QL_OSI_SUCCESS)
  224. {
  225. QL_SFTP_LOG("sftp_app init failed");
  226. }
  227. return 0;
  228. }