blob: 481108dc22b93fe8bce6b429676428e66dc63a35 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <string.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <errno.h>
7
8#include "mbtk_type.h"
9#include "mbtk_ftp.h"
10#include "mbtk_log.h"
11#include "mbtk_str.h"
12
13static int ftp_cmd_process(mbtk_ftp_handle handle, const char *cmd)
14{
15 if(!strncasecmp(cmd, "download", 8)) {
16 // Download file: /data
17 uint32 len_count = 0;
18 uint32 len;
19 int download_time = 0;
20
21 uint32 file_size = mbtk_ftp_file_size(handle, "/Luo/Luo_up.txt");
22 if(file_size > 0)
23 {
24 printf("Will download file:/data[%d]\n", file_size);
25 // Start download
26 len = mbtk_ftp_download_start(handle, "/Luo/Luo_up.txt", "/tmp/ftp_data", NULL);
27 if(len > 0)
28 {
29 len_count += len;
30 download_time++;
31 printf("Download[time-%d] size:[%d / %d]\n", download_time, len_count, file_size);
32 while (len_count < file_size
33 && download_time <= 10 // Try 10 times.
34 && (len = mbtk_ftp_download_continue(handle)) > 0)
35 {
36 len_count += len;
37 download_time++;
38 printf("Download[time-%d] size:[%d / %d]\n", download_time, len_count, file_size);
39 }
40
41 printf("Download complete - [%d / %d].\n",len_count, file_size);
42 }
43 else
44 {
45 printf("FTP download fail[%d / %d].\n",len_count, file_size);
46 return -1;
47 }
48 }else {
49 printf("File error.\n");
50 return -1;
51 }
52 }
53 else if(!strncasecmp(cmd, "upload", 6)) {
54 int len = 0;
55 len = mbtk_ftp_upload_start(handle, "/Luo/up_ftp3.txt", "/tmp/ftp_data", 0);
56 if( len != 0)
57 {
58 printf("FTP update fail\n");
59 return -1;
60 }
61 else
62 {
63 printf("FTP update success\n");
64 }
65 }
66 else if(!strncasecmp(cmd, "pwd", 3)) {
67 char path[50] = {0};
68 mbtk_ftp_error_enum err = mbtk_ftp_pwd(handle, path);
69 if(FTP_ERR_SUCCESS != err) {
70 printf("mbtk_ftp_pwd() fail:%d\n", err);
71 return -1;
72 }
73
74 printf("PWD : %s\n", path);
75 } else if(!strncasecmp(cmd, "cd ", 3)) {
76 char path[50] = {0};
77 memcpy(path, cmd + 3, strlen(cmd) - 3);
78 mbtk_ftp_error_enum err = mbtk_ftp_cd(handle, path);
79 if(FTP_ERR_SUCCESS != err) {
80 printf("mbtk_ftp_cd() fail:%d\n", err);
81 return -1;
82 }
83
84 printf("cd %s\n", path);
85 } else if(!strncasecmp(cmd, "ls", 2)) {
86 mbtk_ftp_file_info_s list_head;
87 mbtk_ftp_error_enum err = mbtk_ftp_dir_ls(handle, &list_head);
88 if(FTP_ERR_SUCCESS != err) {
89 printf("mbtk_ftp_dir_ls() fail:%d\n", err);
90 return -1;
91 }
92
93 mbtk_ftp_file_info_s *f_ptr = list_head.next;
94 while(f_ptr && !str_empty(f_ptr->name)) {
95 printf("%s, %s, %d\n", f_ptr->name, f_ptr->is_file ? "F" : "D", f_ptr->size);
96 f_ptr = f_ptr->next;
97 }
98 } else {
99 printf("Unknow CMD.\n");
100 return -1;
101 }
102
103 return 0;
104}
105
106int main(int argc, char *argv[])
107{
108 mbtk_ftp_handle handle = mbtk_ftp_init("58.246.1.50", 6521, FTP_AUTH_TYPE_NON, false, false);
109 if(handle < 0) {
110 printf("mbtk_ftp_init() fail.\n");
111 return -1;
112 }
113
114 mbtk_ftp_error_enum err = mbtk_ftp_login(handle, " FTP_TST", "FTPTST_0320");
115 if(err != FTP_ERR_SUCCESS) {
116 printf("mbtk_ftp_login() fail:%d\n", err);
117 goto ftp_exit;
118 }
119
120 printf("Login success.\nCMD:");
121
122 char cmd[50];
123 while(1) {
124 memset(cmd, 0, 50);
125 gets(cmd);
126 if(!strcasecmp(cmd, "q")) {
127 break;
128 } else {
129 ftp_cmd_process(handle, cmd);
130 }
131
132 printf("CMD:");
133 }
134
135 printf("FTP test success.\n");
136ftp_quit:
137 mbtk_ftp_quit(handle);
138ftp_exit:
139 mbtk_ftp_deinit(handle);
140 printf("FTP test complete.\n");
141 return 0;
142
143}
144