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