liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /************************************************************* |
| 2 | Description: |
| 3 | $file_description$ |
| 4 | Author: |
| 5 | LiuBin |
| 6 | Date: |
| 7 | 2020/10/28 11:49:08 |
| 8 | *************************************************************/ |
| 9 | #include "mbtk_ftp.h" |
| 10 | #include "mbtk_list.h" |
| 11 | #include "mbtk_sock.h" |
| 12 | #include "mbtk_str.h" |
| 13 | #include "mbtk_sock2.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 14 | #include<linux/msg.h> |
| 15 | |
| 16 | /************************************************************* |
| 17 | typedef struct:local at |
| 18 | *************************************************************/ |
| 19 | typedef struct |
| 20 | { |
| 21 | char host[64]; |
| 22 | uint16 port; |
| 23 | mbtk_ftp_auth_type_enum auth_type; |
| 24 | bool ftp_type; |
| 25 | bool use_cert; |
| 26 | char name[FTP_SERVER_USER_NAME_MAX+1]; |
| 27 | char pass[FTP_SERVER_USER_PASS_MAX+1]; |
| 28 | mbtk_ftp_handle at_ftp_handle; |
| 29 | char remote_path[MBTK_FTP_FILE_NAME_MAX+1]; |
| 30 | char local_path[MBTK_FTP_FILE_NAME_MAX+1]; |
| 31 | int rest_size; |
| 32 | int read_size; |
| 33 | int put_len; |
| 34 | } mbtk_at_init_parameter; |
| 35 | |
| 36 | mbtk_at_init_parameter mbtk_at_ftp_par_at={0}; |
| 37 | |
| 38 | /************************************************************* |
| 39 | Variables:local |
| 40 | *************************************************************/ |
| 41 | static list_node_t *ftp_client_list = NULL; |
| 42 | |
| 43 | /************************************************************* |
| 44 | Variables:public |
| 45 | *************************************************************/ |
| 46 | |
| 47 | /************************************************************* |
| 48 | Local Function Declaration |
| 49 | *************************************************************/ |
| 50 | |
| 51 | /************************************************************* |
| 52 | Local Function Definitions |
| 53 | *************************************************************/ |
| 54 | |
| 55 | //at |
| 56 | struct my_msg //消息队列结构体 |
| 57 | { |
| 58 | long int my_msg_type; |
| 59 | char *ptr; |
| 60 | }mbtk_at_msg; |
| 61 | |
| 62 | int msgid; |
| 63 | |
| 64 | int mbtk_at_msgid(int *at_msgid) |
| 65 | { |
| 66 | msgid = *at_msgid; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | void mbtk_at_printf(void *data) |
| 71 | { |
| 72 | char *ptr = malloc(strlen(data)+1); |
| 73 | memset(ptr,0,strlen(data)+1); |
| 74 | memcpy(ptr, data, strlen(data)+1); |
| 75 | mbtk_at_msg.ptr = ptr; |
| 76 | mbtk_at_msg.my_msg_type=3; |
| 77 | msgsnd(msgid,&mbtk_at_msg,sizeof(&mbtk_at_msg),0); //发送数据到缓冲区 |
| 78 | } |
| 79 | |
| 80 | int mbtk_at_ftp(int parameter,int port,char *addr, char *pass, char *name,int at_auth_type ,bool ftp_type, bool use_cert) |
| 81 | { |
| 82 | int at_ftp_ret = 0; |
| 83 | |
| 84 | LOGI("%s %d", __FUNCTION__, __LINE__); |
| 85 | switch (parameter) { |
| 86 | case 0: { |
| 87 | mbtk_at_ftp_par_at.port = port; |
| 88 | break; |
| 89 | } |
| 90 | case 1: { |
| 91 | memset(mbtk_at_ftp_par_at.host, 0, strlen(mbtk_at_ftp_par_at.host)+1); |
| 92 | memcpy(mbtk_at_ftp_par_at.host, addr, strlen(addr)+1); |
| 93 | break; |
| 94 | } |
| 95 | case 2: { |
| 96 | memset(mbtk_at_ftp_par_at.pass, 0, strlen(mbtk_at_ftp_par_at.pass)+1); |
| 97 | memcpy(mbtk_at_ftp_par_at.pass, pass, strlen(pass)+1); |
| 98 | break; |
| 99 | } |
| 100 | case 3: { |
| 101 | memset(mbtk_at_ftp_par_at.name, 0, strlen(mbtk_at_ftp_par_at.name)+1); |
| 102 | memcpy(mbtk_at_ftp_par_at.name, name, strlen(name)+1); |
| 103 | break; |
| 104 | } |
| 105 | case 4: { |
| 106 | mbtk_at_ftp_par_at.auth_type = at_auth_type; |
| 107 | mbtk_at_ftp_par_at.use_cert = use_cert; |
| 108 | break; |
| 109 | } |
| 110 | case 5: { |
| 111 | mbtk_at_ftp_par_at.ftp_type = ftp_type; |
| 112 | break; |
| 113 | } |
| 114 | default : |
| 115 | at_ftp_ret = -1; |
| 116 | return at_ftp_ret; |
| 117 | } |
| 118 | return at_ftp_ret; |
| 119 | } |
| 120 | |
| 121 | int mbtk_at_get_ftp_info(int parameter,int *port,char *addr, char *pass, char *name, int *at_auth_type ,int *ftp_type, int *use_cert) |
| 122 | { |
| 123 | int at_ftp_ret = 0; |
| 124 | |
| 125 | LOGI("%s %d", __FUNCTION__, __LINE__); |
| 126 | switch (parameter) { |
| 127 | case 0: { |
| 128 | *port = mbtk_at_ftp_par_at.port; |
| 129 | break; |
| 130 | } |
| 131 | case 1: { |
| 132 | memset(addr, 0, strlen(addr)+1); |
| 133 | memcpy(addr, mbtk_at_ftp_par_at.host, strlen(mbtk_at_ftp_par_at.host)+1); |
| 134 | break; |
| 135 | } |
| 136 | case 2: { |
| 137 | memset(pass, 0, strlen(pass)+1); |
| 138 | memcpy(pass, mbtk_at_ftp_par_at.pass, strlen(mbtk_at_ftp_par_at.pass)+1); |
| 139 | break; |
| 140 | } |
| 141 | case 3: { |
| 142 | memset(name, 0, strlen(name)+1); |
| 143 | memcpy(name, mbtk_at_ftp_par_at.name, strlen(mbtk_at_ftp_par_at.name)+1); |
| 144 | break; |
| 145 | } |
| 146 | case 4: { |
| 147 | *at_auth_type = mbtk_at_ftp_par_at.auth_type; |
| 148 | *use_cert = mbtk_at_ftp_par_at.use_cert; |
| 149 | break; |
| 150 | } |
| 151 | case 5: { |
| 152 | *ftp_type = mbtk_at_ftp_par_at.ftp_type; |
| 153 | break; |
| 154 | } |
| 155 | default : |
| 156 | at_ftp_ret = -1; |
| 157 | } |
| 158 | return at_ftp_ret; |
| 159 | } |
| 160 | |
| 161 | int mbtk_at_ftp_mkrmdel(int cmd,char *dir) |
| 162 | { |
| 163 | int at_ftp_ret = 0; |
| 164 | |
| 165 | LOGI("%s %d", __FUNCTION__, __LINE__); |
| 166 | |
| 167 | char send_buf[1024]; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 168 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 169 | mbtk_at_ftp_par_at.at_ftp_handle = mbtk_ftp_init(mbtk_at_ftp_par_at.host, mbtk_at_ftp_par_at.port, mbtk_at_ftp_par_at.auth_type, |
| 170 | 0, mbtk_at_ftp_par_at.use_cert); |
| 171 | mbtk_ftp_data_type_set(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.ftp_type);//FTP_DATA_TYPE_I = 2 FTP_DATA_TYPE_A = 0 |
| 172 | if(mbtk_at_ftp_par_at.at_ftp_handle < 0) |
| 173 | at_ftp_ret = -1; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 174 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 175 | at_ftp_ret = mbtk_ftp_login(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.name,mbtk_at_ftp_par_at.pass); |
| 176 | if(at_ftp_ret != 0) |
| 177 | { |
| 178 | printf("ftp_login err:%d\n",at_ftp_ret); |
| 179 | } |
| 180 | memset(mbtk_at_ftp_par_at.remote_path, 0, strlen(mbtk_at_ftp_par_at.remote_path)+1); |
| 181 | memcpy(mbtk_at_ftp_par_at.remote_path,dir,strlen(dir)+1); |
| 182 | switch (cmd) |
| 183 | { |
| 184 | case 0: |
| 185 | { |
| 186 | at_ftp_ret = mbtk_ftp_dir_mkdir(mbtk_at_ftp_par_at.at_ftp_handle,dir); |
| 187 | if(at_ftp_ret != 0) |
| 188 | { |
| 189 | printf("+CFTPMKD: FAIL,<%d>\n",at_ftp_ret); |
| 190 | sprintf(send_buf,"+CFTPMKD: FAIL,<%d>\n",at_ftp_ret); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | printf("+CFTPMKD: SUCCESS\n"); |
| 195 | sprintf(send_buf,"+CFTPMKD: SUCCESS\n"); |
| 196 | } |
| 197 | break; |
| 198 | } |
| 199 | case 1: |
| 200 | { |
| 201 | at_ftp_ret = mbtk_ftp_dir_rmdir(mbtk_at_ftp_par_at.at_ftp_handle,dir); |
| 202 | if(at_ftp_ret != 0) |
| 203 | { |
| 204 | printf("+CFTPRMD: FAIL,<%d>\n",at_ftp_ret); |
| 205 | sprintf(send_buf,"+CFTPRMD: FAIL,<%d>\n",at_ftp_ret); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | printf("+CFTPRMD: SUCCESS\n"); |
| 210 | sprintf(send_buf,"+CFTPRMD: SUCCESS\n"); |
| 211 | } |
| 212 | break; |
| 213 | } |
| 214 | case 2: |
| 215 | { |
| 216 | at_ftp_ret = mbtk_ftp_file_del(mbtk_at_ftp_par_at.at_ftp_handle,dir); |
| 217 | if(at_ftp_ret != 0) |
| 218 | { |
| 219 | printf("+CFTPDELE: FAIL,<%d>\n",at_ftp_ret); |
| 220 | sprintf(send_buf,"+CFTPDELE: FAIL,<%d>\n",at_ftp_ret); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | printf("+CFTPDELE: SUCCESS\n"); |
| 225 | sprintf(send_buf,"+CFTPDELE: SUCCESS\n"); |
| 226 | } |
| 227 | break; |
| 228 | } |
| 229 | default: |
| 230 | break; |
| 231 | } |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 232 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 233 | mbtk_at_printf(send_buf); |
| 234 | mbtk_at_printf("end"); |
| 235 | mbtk_ftp_quit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 236 | mbtk_ftp_deinit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 237 | return at_ftp_ret; |
| 238 | } |
| 239 | |
| 240 | int mbtk_at_ftp_upload(char *remote_path,char *local_path, int size_byte, int rest_size) |
| 241 | { |
| 242 | int at_ftp_ret=-1; |
| 243 | int len_size; |
| 244 | char send_buf[1024]; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 245 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 246 | mbtk_at_ftp_par_at.at_ftp_handle = mbtk_ftp_init(mbtk_at_ftp_par_at.host, mbtk_at_ftp_par_at.port, mbtk_at_ftp_par_at.auth_type, |
| 247 | 0, mbtk_at_ftp_par_at.use_cert); |
| 248 | mbtk_ftp_data_type_set(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.ftp_type);//FTP_DATA_TYPE_I = 2 FTP_DATA_TYPE_A = 0 |
| 249 | if(mbtk_at_ftp_par_at.at_ftp_handle < 0) |
| 250 | at_ftp_ret = -1; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 251 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 252 | at_ftp_ret = mbtk_ftp_login(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.name,mbtk_at_ftp_par_at.pass); |
| 253 | if(at_ftp_ret != 0) |
| 254 | { |
| 255 | printf("ftp_login err:%d\n",at_ftp_ret); |
| 256 | } |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 257 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 258 | memset(mbtk_at_ftp_par_at.remote_path, 0, strlen(mbtk_at_ftp_par_at.remote_path)+1); |
| 259 | memcpy(mbtk_at_ftp_par_at.remote_path, remote_path, strlen(remote_path)+1); |
| 260 | if(local_path!=NULL) |
| 261 | { |
| 262 | memset(mbtk_at_ftp_par_at.local_path, 0, strlen(mbtk_at_ftp_par_at.local_path)+1); |
| 263 | memcpy(mbtk_at_ftp_par_at.local_path, local_path, strlen(local_path)+1); |
| 264 | } |
| 265 | mbtk_at_ftp_par_at.put_len = size_byte; |
| 266 | mbtk_at_ftp_par_at.rest_size = rest_size; |
| 267 | |
| 268 | { |
| 269 | FILE *fp; |
| 270 | fp=fopen(local_path,"r"); |
| 271 | if(NULL==fp) |
| 272 | printf("Read file error\n"); |
| 273 | else |
| 274 | { |
| 275 | printf("Open file success\n"); |
| 276 | fseek(fp,0,SEEK_END); //将文件读写指针移动到文件尾 |
| 277 | len_size=ftell(fp); //ftell函数获取从文件尾移动到文件开头的偏移量 |
r.xiao | 2ffafe9 | 2023-10-27 00:25:01 -0700 | [diff] [blame] | 278 | fclose(fp); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | if(local_path == NULL) |
| 282 | { |
| 283 | at_ftp_ret = mbtk_ftp_upload_start(mbtk_at_ftp_par_at.at_ftp_handle, remote_path, |
| 284 | NULL, size_byte); |
| 285 | if(at_ftp_ret != 0) |
| 286 | { |
| 287 | printf("+CFTPPUT: FAIL,<%d>\n",at_ftp_ret); |
| 288 | sprintf(send_buf,"+CFTPPUT: FAIL,<%d>\n",at_ftp_ret); |
| 289 | } |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 290 | else |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 291 | { |
| 292 | at_ftp_ret = mbtk_ftp_upload_send(mbtk_at_ftp_par_at.at_ftp_handle,"" ,size_byte); |
| 293 | if(at_ftp_ret != 0) |
| 294 | { |
| 295 | printf("+CFTPPUT: FAIL,<%d>\n",at_ftp_ret); |
| 296 | sprintf(send_buf,"+CFTPPUT: FAIL,<%d>\n",at_ftp_ret); |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | printf("+CFTPPUT: SUCCESS,<%d>\n",len_size); |
| 301 | sprintf(send_buf,"+CFTPPUT: SUCCESS,<%d>\n",len_size); |
| 302 | } |
| 303 | } |
| 304 | mbtk_ftp_upload_end(mbtk_at_ftp_par_at.at_ftp_handle); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | at_ftp_ret = mbtk_ftp_upload_start(mbtk_at_ftp_par_at.at_ftp_handle, remote_path, |
| 309 | local_path, size_byte); |
| 310 | if(at_ftp_ret != 0) |
| 311 | { |
| 312 | printf("+CFTPPUT: FAIL,<%d>\n",at_ftp_ret); |
| 313 | sprintf(send_buf,"+CFTPPUT: FAIL,<%d>\n",at_ftp_ret); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | printf("+CFTPPUT: SUCCESS,<%d>\n",len_size); |
| 318 | sprintf(send_buf,"+CFTPPUT: SUCCESS,<%d>\n",len_size); |
| 319 | } |
| 320 | } |
| 321 | mbtk_at_printf(send_buf); |
| 322 | mbtk_at_printf("end"); |
| 323 | mbtk_ftp_quit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 324 | mbtk_ftp_deinit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 325 | return at_ftp_ret; |
| 326 | } |
| 327 | |
| 328 | void mbtk_at_ftp_data_cb(void *data, uint32 data_len) |
| 329 | { |
| 330 | char resp_buf[2150]; |
| 331 | printf("\n=====data_cb data[%s]=====\n=====data_cb data_len[%d]=====\n",data, data_len); |
| 332 | |
| 333 | memset(resp_buf,0,2150); |
| 334 | sprintf(resp_buf, "+CFTPGET:"); |
| 335 | mbtk_at_printf(resp_buf); |
| 336 | |
| 337 | if(data_len > 1021) |
| 338 | { |
| 339 | memset(resp_buf,0,2150); |
| 340 | memcpy(resp_buf,data,1021); |
| 341 | mbtk_at_printf(resp_buf); |
| 342 | memset(resp_buf,0,2150); |
| 343 | sprintf(resp_buf, "<%d>", 1021); |
| 344 | mbtk_at_printf(resp_buf); |
| 345 | |
| 346 | memset(resp_buf,0,2150); |
| 347 | sprintf(resp_buf, "+CFTPGET:"); |
| 348 | mbtk_at_printf(resp_buf); |
| 349 | memset(resp_buf,0,2150); |
| 350 | memcpy(resp_buf,data+1021,data_len-1021); |
| 351 | mbtk_at_printf(resp_buf); |
| 352 | memset(resp_buf,0,2150); |
| 353 | sprintf(resp_buf, "<%d>", data_len-1021); |
| 354 | mbtk_at_printf(resp_buf); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | memset(resp_buf,0,2150); |
| 359 | memcpy(resp_buf,data,data_len); |
| 360 | mbtk_at_printf(resp_buf); |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 361 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 362 | memset(resp_buf,0,2150); |
| 363 | sprintf(resp_buf, "<%d>", data_len); |
| 364 | mbtk_at_printf(resp_buf); |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | |
| 369 | int mbtk_at_ftp_download(void *remote_path, void *local_path, int data_cbc, int rest_size, int read_size) |
| 370 | { |
| 371 | |
| 372 | int at_ftp_ret=-1; |
| 373 | int len_size; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 374 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 375 | mbtk_at_ftp_par_at.at_ftp_handle = mbtk_ftp_init(mbtk_at_ftp_par_at.host, mbtk_at_ftp_par_at.port, mbtk_at_ftp_par_at.auth_type, |
| 376 | 0, mbtk_at_ftp_par_at.use_cert); |
| 377 | mbtk_ftp_data_type_set(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.ftp_type);//FTP_DATA_TYPE_I = 2 FTP_DATA_TYPE_A = 0 |
| 378 | if(mbtk_at_ftp_par_at.at_ftp_handle < 0) |
| 379 | at_ftp_ret = -1; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 380 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 381 | at_ftp_ret = mbtk_ftp_login(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.name,mbtk_at_ftp_par_at.pass); |
| 382 | if(at_ftp_ret != 0) |
| 383 | { |
| 384 | printf("ftp_login err:%d\n",at_ftp_ret); |
| 385 | } |
| 386 | |
| 387 | memset(mbtk_at_ftp_par_at.remote_path, 0, strlen(mbtk_at_ftp_par_at.remote_path)+1); |
| 388 | memcpy(mbtk_at_ftp_par_at.remote_path, remote_path, strlen(remote_path)+1); |
| 389 | if(local_path!=NULL) |
| 390 | { |
| 391 | memset(mbtk_at_ftp_par_at.local_path, 0, strlen(mbtk_at_ftp_par_at.local_path)+1); |
| 392 | memcpy(mbtk_at_ftp_par_at.local_path, local_path, strlen(local_path)+1); |
| 393 | } |
| 394 | mbtk_at_ftp_par_at.rest_size = rest_size; |
| 395 | mbtk_at_ftp_par_at.read_size = read_size; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 396 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 397 | // Download file: /data |
| 398 | uint32 len_count = 0; |
| 399 | uint32 len; |
| 400 | int download_time = 0; |
| 401 | char resp_buf[1024]; |
| 402 | int ret; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 403 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 404 | uint32 file_size = mbtk_ftp_file_size(mbtk_at_ftp_par_at.at_ftp_handle, remote_path); |
| 405 | if(file_size > 0) |
| 406 | { |
| 407 | if(rest_size > file_size) |
| 408 | { |
| 409 | printf("+CTPGETFILE: FAIL,<err>"); |
| 410 | memset(resp_buf,0,1024); |
| 411 | sprintf(resp_buf, "+CTPGETFILE: FAIL,<%d>\r\n",901); |
| 412 | mbtk_at_printf(resp_buf); |
| 413 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 414 | goto exit; |
| 415 | } |
| 416 | printf("Will download file:/data[%d]\n", file_size); |
| 417 | /* |
| 418 | memset(resp_buf,0,1024); |
| 419 | sprintf(resp_buf, "Will download file:/data[%d]",file_size); |
| 420 | mbtk_at_printf(resp_buf); |
| 421 | */ |
| 422 | // Start download |
| 423 | if(data_cbc == 0)//to file |
| 424 | { |
| 425 | len = mbtk_ftp_download_start(mbtk_at_ftp_par_at.at_ftp_handle, remote_path, local_path, NULL); |
| 426 | /* |
| 427 | memset(resp_buf,0,1024); |
| 428 | sprintf(resp_buf, "download size:/len[%d]",len); |
| 429 | mbtk_at_printf(resp_buf); |
| 430 | */ |
| 431 | } |
| 432 | else if(data_cbc == 1) |
| 433 | { |
| 434 | len = mbtk_ftp_download_start(mbtk_at_ftp_par_at.at_ftp_handle, remote_path, NULL, mbtk_at_ftp_data_cb); |
| 435 | /* |
| 436 | memset(resp_buf,0,1024); |
| 437 | sprintf(resp_buf, "download size:/len[%d]",len); |
| 438 | mbtk_at_printf(resp_buf); |
| 439 | */ |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | printf("+CTPGETFILE: FAIL,<err>"); |
| 444 | memset(resp_buf,0,1024); |
| 445 | sprintf(resp_buf, "+CTPGETFILE: FAIL,<%d>\r\n",902); |
| 446 | mbtk_at_printf(resp_buf); |
| 447 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 448 | goto exit; |
| 449 | } |
| 450 | if(len > 0) |
| 451 | { |
| 452 | len_count += len; |
| 453 | download_time++; |
| 454 | printf("Download[time-%d] size:[%d / %d]\n", download_time, len_count, file_size); |
| 455 | /* |
| 456 | memset(resp_buf,0,1024); |
| 457 | sprintf(resp_buf,"Download[time-%d] size:[%d / %d]\n", download_time, len_count, file_size); |
| 458 | mbtk_at_printf(resp_buf); |
| 459 | */ |
| 460 | while (len_count < file_size |
| 461 | && download_time <= 10 // Try 10 times. |
| 462 | && (len = mbtk_ftp_download_continue(mbtk_at_ftp_par_at.at_ftp_handle)) > 0) |
| 463 | { |
| 464 | len_count += len; |
| 465 | download_time++; |
| 466 | printf("Download[time-%d] size:[%d / %d]\n", download_time, len_count, file_size); |
| 467 | /* |
| 468 | memset(resp_buf,0,1024); |
| 469 | sprintf(resp_buf,"Download[time-%d] size:[%d / %d]\n", download_time, len_count, file_size); |
| 470 | mbtk_at_printf(resp_buf); |
| 471 | */ |
| 472 | } |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 473 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 474 | printf("Download complete - [%d / %d].\n",len_count, file_size); |
| 475 | memset(resp_buf,0,1024); |
| 476 | sprintf(resp_buf, "+CFTPGETFILE: SUCCESS,<%d>\r\n",len_count); |
| 477 | mbtk_at_printf(resp_buf); |
| 478 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | printf("FTP download fail[%d / %d].\n",len_count, file_size); |
| 483 | memset(resp_buf,0,1024); |
| 484 | sprintf(resp_buf, "+CTPGETFILE: FAIL,<%d / %d> len=%d",len_count, file_size,len); |
| 485 | mbtk_at_printf(resp_buf); |
| 486 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 487 | goto exit; |
| 488 | } |
| 489 | }else { |
| 490 | printf("File error.\n"); |
| 491 | memset(resp_buf,0,1024); |
| 492 | sprintf(resp_buf, "+CTPGETFILE: FAIL,<%d>\r\n",904); |
| 493 | mbtk_at_printf(resp_buf); |
| 494 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 495 | goto exit; |
| 496 | } |
| 497 | |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 498 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 499 | exit: |
| 500 | if(file_size != len_count) |
| 501 | mbtk_at_ftp_par_at.rest_size = len_count; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 502 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 503 | //mbtk_at_printf(resp_buf); |
| 504 | mbtk_at_printf("end"); |
| 505 | mbtk_ftp_quit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 506 | mbtk_ftp_deinit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 507 | return at_ftp_ret; |
| 508 | |
| 509 | } |
| 510 | |
| 511 | void ftp_ls_at_cb_func(void *file_list_cb) |
| 512 | { |
| 513 | int ret; |
| 514 | char resp_buf[1024 + 1]; |
| 515 | printf("+CFTPLIST:\r\n%s\n", ((mbtk_ftp_file_info_s *)file_list_cb)->name); |
| 516 | sprintf(resp_buf, "%s",((mbtk_ftp_file_info_s *)file_list_cb)->name); |
| 517 | mbtk_at_printf(resp_buf); |
| 518 | //ret = ATRESP(30163, 0, 0, resp_buf); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | int mbtk_at_ftp_list(char *ftp_path) |
| 523 | { |
| 524 | int at_ftp_ret=-1; |
| 525 | int len_size; |
| 526 | char resp_buf[1024]; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 527 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 528 | mbtk_at_ftp_par_at.at_ftp_handle = mbtk_ftp_init(mbtk_at_ftp_par_at.host, mbtk_at_ftp_par_at.port, mbtk_at_ftp_par_at.auth_type, |
| 529 | 0, mbtk_at_ftp_par_at.use_cert); |
| 530 | mbtk_ftp_data_type_set(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.ftp_type);//FTP_DATA_TYPE_I = 2 FTP_DATA_TYPE_A = 0 |
| 531 | if(mbtk_at_ftp_par_at.at_ftp_handle < 0) |
| 532 | at_ftp_ret = -1; |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 533 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 534 | at_ftp_ret = mbtk_ftp_login(mbtk_at_ftp_par_at.at_ftp_handle,mbtk_at_ftp_par_at.name,mbtk_at_ftp_par_at.pass); |
| 535 | if(at_ftp_ret != 0) |
| 536 | { |
| 537 | printf("ftp_login err:%d\n",at_ftp_ret); |
| 538 | } |
| 539 | |
| 540 | memset(mbtk_at_ftp_par_at.remote_path, 0, strlen(mbtk_at_ftp_par_at.remote_path)+1); |
| 541 | memcpy(mbtk_at_ftp_par_at.remote_path, ftp_path, strlen(ftp_path)+1); |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 542 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 543 | at_ftp_ret = mbtk_ftp_cd(mbtk_at_ftp_par_at.at_ftp_handle, ftp_path); |
| 544 | if(FTP_ERR_SUCCESS != at_ftp_ret) { |
| 545 | printf("mbtk_ftp_cd() fail:%d\n", at_ftp_ret); |
| 546 | sprintf(resp_buf,"+CFTPLIST: FAIL:%d\n", at_ftp_ret); |
| 547 | goto exit; |
| 548 | } |
| 549 | mbtk_at_printf("+CFTPLIST:"); |
| 550 | mbtk_ftp_file_info_s list_head; |
| 551 | list_head.ftp_ls_cb_typedef = ftp_ls_at_cb_func; |
| 552 | at_ftp_ret = mbtk_ftp_dir_ls(mbtk_at_ftp_par_at.at_ftp_handle, &list_head); |
| 553 | if(FTP_ERR_SUCCESS != at_ftp_ret) { |
| 554 | printf("mbtk_ftp_dir_ls() fail:%d\n", at_ftp_ret); |
| 555 | sprintf(resp_buf,"+CFTPLIST: FAIL:%d\n", at_ftp_ret); |
| 556 | goto exit; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | printf("+CFTPLIST: SUCCESS\r\n"); |
| 561 | sprintf(resp_buf,"+CFTPLIST: SUCCESS\r\n"); |
| 562 | } |
| 563 | exit: |
| 564 | mbtk_at_printf(resp_buf); |
| 565 | mbtk_at_printf("end"); |
| 566 | mbtk_ftp_quit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 567 | mbtk_ftp_deinit(mbtk_at_ftp_par_at.at_ftp_handle); |
| 568 | return at_ftp_ret; |
| 569 | } |
| 570 | |
| 571 | int mbtk_at_get_ftp_data_info(char *remote_path, char *local_path, int *rest_size, int *read_size, int *put_len) |
| 572 | { |
| 573 | if(remote_path != NULL) |
| 574 | { |
| 575 | memset(remote_path, 0, strlen(remote_path)+1); |
| 576 | memcpy(remote_path, mbtk_at_ftp_par_at.remote_path, strlen(mbtk_at_ftp_par_at.remote_path)+1); |
| 577 | } |
| 578 | if(local_path != NULL) |
| 579 | { |
| 580 | memset(local_path, 0, strlen(local_path)+1); |
| 581 | memcpy(local_path, mbtk_at_ftp_par_at.local_path, strlen(mbtk_at_ftp_par_at.local_path)+1); |
| 582 | } |
| 583 | if(rest_size != NULL) |
| 584 | *rest_size = mbtk_at_ftp_par_at.rest_size; |
| 585 | if(read_size != NULL) |
| 586 | *read_size = mbtk_at_ftp_par_at.read_size; |
| 587 | if(put_len != NULL) |
| 588 | *put_len = mbtk_at_ftp_par_at.put_len; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | |