lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file ext_ftp_func.c |
| 3 | * @brief zte_ftpÏà¹ØÀ©Õ¹at×¢²á¼°´¦ÀíʵÏÖ |
| 4 | * |
| 5 | * Copyright (C) 2017 Sanechips Technology Co., Ltd. |
| 6 | * @author |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #ifndef CONFIG_MIN_8M_VERSION |
| 15 | /******************************************************************************* |
| 16 | * Include header files * |
| 17 | ******************************************************************************/ |
| 18 | #include "ext_regist.h" |
| 19 | #include "at_utils.h" |
| 20 | #include "softap_api.h" |
| 21 | #include "ext_ftp_func.h" |
| 22 | |
| 23 | /******************************************************************************* |
| 24 | * Macro definitions * |
| 25 | ******************************************************************************/ |
| 26 | |
| 27 | /******************************************************************************* |
| 28 | * Type definitions * |
| 29 | ******************************************************************************/ |
| 30 | /******************************************************************************* |
| 31 | * Local function declarations * |
| 32 | ******************************************************************************/ |
| 33 | /******************************************************************************* |
| 34 | * Local variable definitions * |
| 35 | ******************************************************************************/ |
| 36 | /* |
| 37 | $MYFTPOPEN=<channel>,<destination_ip/url>,<username>,<password>,<mode>,<Tout>,<FTPtype> |
| 38 | */ |
| 39 | static int ext_ftpopen_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 40 | { |
| 41 | struct zteftp_open_reqmsg *reqmsg = malloc(sizeof(struct zteftp_open_reqmsg)); |
| 42 | assert(reqmsg); |
| 43 | char *p[7] = { |
| 44 | &reqmsg->channel, |
| 45 | reqmsg->destination, |
| 46 | reqmsg->username, |
| 47 | reqmsg->password, |
| 48 | &reqmsg->mode, |
| 49 | &reqmsg->tout, |
| 50 | &reqmsg->ftptype |
| 51 | }; |
| 52 | memset(reqmsg, 0x00, sizeof(struct zteftp_open_reqmsg)); |
| 53 | parse_param2("%d,%s,%s,%s,%d,%d,%d", at_paras, p); |
| 54 | at_print(AT_NORMAL, "ext_ftpopen_req %d,%s,%s,%s,%d,%d,%d\n", reqmsg->channel, |
| 55 | reqmsg->destination, |
| 56 | reqmsg->username, |
| 57 | reqmsg->password, |
| 58 | reqmsg->mode, |
| 59 | reqmsg->tout, |
| 60 | reqmsg->ftptype); |
| 61 | *res_msg = reqmsg; |
| 62 | *res_msglen = sizeof(struct zteftp_open_reqmsg); |
| 63 | return AT_CONTINUE; |
| 64 | } |
| 65 | /* |
| 66 | <CR><LF>OK<CR><LF> |
| 67 | <CR><LF>ERROR:<err><CR><LF> |
| 68 | */ |
| 69 | static int ext_ftpopen_rsp(void *rsp_msg, void**ret, int *retlen) |
| 70 | { |
| 71 | struct zteftp_open_rspmsg *rspmsg = (struct zteftp_open_rspmsg*)rsp_msg; |
| 72 | char *at_str = malloc(32); |
| 73 | assert(at_str); |
| 74 | memset(at_str, 0x00, 32); |
| 75 | |
| 76 | at_print(AT_NORMAL, "result %d\n", rspmsg->result); |
| 77 | if (rspmsg->result < 0) { |
| 78 | sprintf(at_str, "\r\nERROR: %d\r\n", rspmsg->err); |
| 79 | } else { |
| 80 | sprintf(at_str, "\r\nOK\r\n"); |
| 81 | } |
| 82 | *ret = at_str; |
| 83 | *retlen = strlen(at_str); |
| 84 | return AT_END; |
| 85 | } |
| 86 | /* |
| 87 | at$MYFTPOPEN? |
| 88 | */ |
| 89 | static int ext_ftpstate_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 90 | { |
| 91 | return AT_CONTINUE; |
| 92 | } |
| 93 | /* |
| 94 | <CR><LF>$MYFTPOPEN:<connect_status> |
| 95 | <CR><LF>OK<CR><LF> |
| 96 | */ |
| 97 | static int ext_ftpstate_rsp(void *rsp_msg, void**ret, int *retlen) |
| 98 | { |
| 99 | struct zteftp_query_rspmsg *rspmsg = (struct zteftp_query_rspmsg*)rsp_msg; |
| 100 | char *at_str = malloc(32); |
| 101 | assert(at_str); |
| 102 | memset(at_str, 0x00, 32); |
| 103 | |
| 104 | sprintf(at_str, "\r\n$MYFTPOPEN: %d\r\nOK\r\n", rspmsg->connect_status); |
| 105 | |
| 106 | *ret = at_str; |
| 107 | *retlen = strlen(at_str); |
| 108 | return AT_END; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | at$MYFTPOPEN=? |
| 113 | |
| 114 | <CR><LF>OK<CR><LF> |
| 115 | */ |
| 116 | static int ext_ftpstate_confirm(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 117 | { |
| 118 | char *at_str = malloc(32); |
| 119 | assert(at_str); |
| 120 | memset(at_str, 0x00, 32); |
| 121 | |
| 122 | sprintf(at_str, "\r\nOK\r\n"); |
| 123 | |
| 124 | *res_msg = at_str; |
| 125 | *res_msglen = strlen(at_str); |
| 126 | return AT_END; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | at$MYFTPCLOSE |
| 131 | */ |
| 132 | static int ext_ftpclose_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 133 | { |
| 134 | return AT_CONTINUE; |
| 135 | } |
| 136 | /* |
| 137 | <CR><LF>OK<CR><LF> |
| 138 | <CR><LF>ERROR:<err><CR><LF> |
| 139 | */ |
| 140 | static int ext_ftpclose_rsp(void *rsp_msg, void**ret, int *retlen) |
| 141 | { |
| 142 | struct zteftp_close_rspmsg *rspmsg = (struct zteftp_close_rspmsg*)rsp_msg; |
| 143 | char *at_str = malloc(32); |
| 144 | assert(at_str); |
| 145 | memset(at_str, 0x00, 32); |
| 146 | |
| 147 | if (rspmsg->result < 0) { |
| 148 | sprintf(at_str, "\r\nERROR\r\n"); |
| 149 | } else { |
| 150 | sprintf(at_str, "\r\nOK\r\n"); |
| 151 | } |
| 152 | |
| 153 | *ret = at_str; |
| 154 | *retlen = strlen(at_str); |
| 155 | return AT_END; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | at$MYFTPSIZE=<File_Name> |
| 160 | */ |
| 161 | static int ext_ftpsize_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 162 | { |
| 163 | struct zteftp_size_reqmsg *reqmsg = malloc(sizeof(struct zteftp_size_reqmsg)); |
| 164 | assert(reqmsg); |
| 165 | char *p[5] = {reqmsg->file_name}; |
| 166 | memset(reqmsg, 0x00, sizeof(struct zteftp_size_reqmsg)); |
| 167 | parse_param2("%s", at_paras, p); |
| 168 | at_print(AT_NORMAL, "ext_ftpsize_req %s", reqmsg->file_name); |
| 169 | *res_msg = reqmsg; |
| 170 | *res_msglen = sizeof(struct zteftp_size_reqmsg); |
| 171 | return AT_CONTINUE; |
| 172 | } |
| 173 | /* |
| 174 | <CR><LF>$MYFTPSIZE:<file_lenth> |
| 175 | <CR><LF>OK<CR><LF> |
| 176 | |
| 177 | <CR><LF>ERROR:<err><CR><LF> |
| 178 | |
| 179 | <CR><LF>$MYURLFTP:<status><CR><LF> |
| 180 | */ |
| 181 | static int ext_ftpsize_rsp(void *rsp_msg, void**ret, int *retlen) |
| 182 | { |
| 183 | struct zteftp_size_rspmsg *rspmsg = (struct zteftp_size_rspmsg*)rsp_msg; |
| 184 | int offset = 0; |
| 185 | char *at_str = malloc(64); |
| 186 | assert(at_str); |
| 187 | memset(at_str, 0x00, 64); |
| 188 | |
| 189 | if (rspmsg->file_lenth < 0) { |
| 190 | offset += sprintf(at_str, "\r\nERROR\r\n"); |
| 191 | } else { |
| 192 | offset += sprintf(at_str, "\r\n$MYFTPSIZE: %d\r\nOK\r\n", rspmsg->file_lenth); |
| 193 | #if 0 |
| 194 | offset += sprintf(at_str + offset, "\r\n$MYURCFTP:1\r\n");/*Êý¾ÝÁ¬½Ó¶Ï¿ª*/ |
| 195 | #endif |
| 196 | } |
| 197 | |
| 198 | *ret = at_str; |
| 199 | *retlen = strlen(at_str); |
| 200 | return AT_END; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | at$MYFTPSIZE=<File_Name>[,<data_offset>,<data_lenth>] |
| 205 | */ |
| 206 | static int ext_ftpget_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 207 | { |
| 208 | struct zteftp_get_reqmsg *reqmsg = malloc(sizeof(struct zteftp_get_reqmsg)); |
| 209 | assert(reqmsg); |
| 210 | char *p[5] = {reqmsg->file_name, &reqmsg->data_offset, &reqmsg->data_lenth}; |
| 211 | memset(reqmsg, 0x00, sizeof(struct zteftp_get_reqmsg)); |
| 212 | parse_param2("%s,%d,%d", at_paras, p); |
| 213 | at_print(AT_NORMAL, "ext_ftpget_req %s,%d,%d", reqmsg->file_name, reqmsg->data_offset, reqmsg->data_lenth); |
| 214 | *res_msg = reqmsg; |
| 215 | *res_msglen = sizeof(struct zteftp_get_reqmsg); |
| 216 | return AT_CONTINUE; |
| 217 | } |
| 218 | /* |
| 219 | <CR><LF>CONNECT<CR><LF> |
| 220 | <CR><LF><file_content> |
| 221 | <CR><LF>OK<CR><LF> |
| 222 | |
| 223 | <CR><LF>ERROR<CR><LF> |
| 224 | |
| 225 | <CR><LF>CONNECT<CR><LF> |
| 226 | <CR><LF><file_content> |
| 227 | <CR><LF>ERROR<CR><LF> |
| 228 | */ |
| 229 | static int ext_ftpget_rsp(void *rsp_msg, void**ret, int *retlen) |
| 230 | { |
| 231 | struct zteftp_get_rspmsg *rspmsg = (struct zteftp_get_rspmsg*)rsp_msg; |
| 232 | char *at_str = NULL; |
| 233 | int offset = 0; |
| 234 | |
| 235 | if (rspmsg->result < 0) { |
| 236 | at_str = malloc(64); |
| 237 | assert(at_str); |
| 238 | memset(at_str, 0x00, 64); |
| 239 | offset += sprintf(at_str, "\r\nERROR\r\n"); |
| 240 | } else { |
| 241 | at_str = malloc(64 + rspmsg->data_lenth); |
| 242 | assert(at_str); |
| 243 | memset(at_str, 0x00, 64 + rspmsg->data_lenth); |
| 244 | #if 1 |
| 245 | offset += sprintf(at_str, "\r\nCONNECT"); |
| 246 | #else |
| 247 | offset += sprintf(at_str, "\r\nCONNECT\r\n"); |
| 248 | /*Êý¾Ýͨ¹ýpassthrough·¢ËÍ*/ |
| 249 | //offset += sprintf(at_str+offset, "%s",rspmsg->file_content); |
| 250 | memcpy(at_str + offset, rspmsg->file_content, rspmsg->data_lenth); |
| 251 | offset += rspmsg->data_lenth; |
| 252 | offset += sprintf(at_str + offset, "\r\nOK\r\n"); |
| 253 | #endif |
| 254 | } |
| 255 | |
| 256 | *ret = at_str; |
| 257 | *retlen = offset;//strlen(at_str); |
| 258 | |
| 259 | return AT_END; |
| 260 | } |
| 261 | /* |
| 262 | at$MYFTPSIZE=<File_Name>,<data_lenth>,<EOF> |
| 263 | */ |
| 264 | static int ext_ftpput_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 265 | { |
| 266 | struct zteftp_put_reqmsg *reqmsg = malloc(sizeof(struct zteftp_put_reqmsg)); |
| 267 | assert(reqmsg); |
| 268 | char *p[5] = {reqmsg->file_name, &reqmsg->data_lenth, &reqmsg->eof}; |
| 269 | memset(reqmsg, 0x00, sizeof(struct zteftp_put_reqmsg)); |
| 270 | parse_param2("%s,%d,%d", at_paras, p); |
| 271 | at_print(AT_NORMAL, "ext_ftpput_req %s,%d,%d\n", reqmsg->file_name, reqmsg->data_lenth, reqmsg->eof); |
| 272 | *res_msg = reqmsg; |
| 273 | *res_msglen = sizeof(struct zteftp_put_reqmsg); |
| 274 | return AT_CONTINUE; |
| 275 | } |
| 276 | /* |
| 277 | <CR><LF>CONNECT<CR><LF> |
| 278 | <CR><LF><file_content> |
| 279 | <CR><LF>OK<CR><LF> |
| 280 | |
| 281 | <CR><LF>ERROR<CR><LF> |
| 282 | */ |
| 283 | static int ext_ftpput_rsp(void *rsp_msg, void**ret, int *retlen) |
| 284 | { |
| 285 | struct zteftp_put_rspmsg *rspmsg = (struct zteftp_put_rspmsg*)rsp_msg; |
| 286 | char *at_str = malloc(32); |
| 287 | assert(at_str); |
| 288 | |
| 289 | memset(at_str, 0x00, 32); |
| 290 | if (rspmsg->ret < 0) { |
| 291 | sprintf(at_str, "\r\nERROR\r\n"); |
| 292 | } else if (rspmsg->ret == 0) { |
| 293 | sprintf(at_str, "\r\nCONNECT\r\n"); |
| 294 | } else if (rspmsg->ret == 1) { |
| 295 | sprintf(at_str, "\r\nOK\r\n"); |
| 296 | } |
| 297 | |
| 298 | *ret = at_str; |
| 299 | *retlen = strlen(at_str); |
| 300 | |
| 301 | return AT_END; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | at+FTPFOTA |
| 306 | */ |
| 307 | static int ext_ftpfota_req(int at_fd, char *at_paras, void ** res_msg, int *res_msglen) |
| 308 | { |
| 309 | struct zteftp_fota_reqmsg *reqmsg = malloc(sizeof(struct zteftp_fota_reqmsg)); |
| 310 | assert(reqmsg); |
| 311 | char *p[7] = { |
| 312 | &reqmsg->channel, |
| 313 | reqmsg->destination, |
| 314 | reqmsg->file_name, |
| 315 | reqmsg->username, |
| 316 | reqmsg->password |
| 317 | }; |
| 318 | memset(reqmsg, 0x00, sizeof(struct zteftp_fota_reqmsg)); |
| 319 | parse_param2("%d,%s,%s,%s,%s", at_paras, p); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 320 | at_print(AT_NORMAL, "ftp_fota_reqmsg %d,%s,%s,%s,%s\n", reqmsg->channel, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 321 | reqmsg->destination, |
| 322 | reqmsg->file_name, |
| 323 | reqmsg->username, |
| 324 | reqmsg->password |
| 325 | ); |
| 326 | *res_msg = reqmsg; |
| 327 | *res_msglen = sizeof(struct zteftp_fota_reqmsg); |
| 328 | return AT_CONTINUE; |
| 329 | } |
| 330 | /* |
| 331 | <CR><LF>OK<CR><LF> |
| 332 | <CR><LF>ERROR<CR><LF> |
| 333 | */ |
| 334 | static int ext_ftpfota_rsp(void *rsp_msg, void**ret, int *retlen) |
| 335 | { |
| 336 | struct zteftp_fota_rspmsg *rspmsg = (struct zteftp_fota_rspmsg*)rsp_msg; |
| 337 | char *at_str = malloc(32); |
| 338 | assert(at_str); |
| 339 | memset(at_str, 0x00, 32); |
| 340 | |
| 341 | if (rspmsg->result < 0) { |
| 342 | sprintf(at_str, "\r\nERROR\r\n"); |
| 343 | } else { |
| 344 | sprintf(at_str, "\r\nOK\r\n"); |
| 345 | } |
| 346 | |
| 347 | *ret = at_str; |
| 348 | *retlen = strlen(at_str); |
| 349 | return AT_END; |
| 350 | } |
| 351 | /******************************************************************************* |
| 352 | * Global variable definitions * |
| 353 | ******************************************************************************/ |
| 354 | /** |
| 355 | * @brief ×¢²áftpÏà¹ØÀ©Õ¹at |
| 356 | * @param |
| 357 | * @return |
| 358 | * @retval |
| 359 | */ |
| 360 | int ext_ftp_regist(void) |
| 361 | { |
| 362 | //#ifdef GUODIAN |
| 363 | if (g_customer_type == CUSTOMER_GUODIAN) { |
| 364 | register_serv_func2("MYFTPOPEN=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPOPEN_REQ, MSG_CMD_FTPOPEN_RSP, ext_ftpopen_req, ext_ftpopen_rsp); |
| 365 | register_serv_func2("MYFTPOPEN?", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPSTATE_REQ, MSG_CMD_FTPSTATE_RSP, ext_ftpstate_req, ext_ftpstate_rsp); |
| 366 | register_serv_func2("MYFTPOPEN=?", MODULE_ID_SOCKET_PROXY, 0, 0, ext_ftpstate_confirm, NULL); |
| 367 | register_serv_func2("MYFTPCLOSE", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPCLOSE_REQ, MSG_CMD_FTPCLOSE_RSP, ext_ftpclose_req, ext_ftpclose_rsp); |
| 368 | register_serv_func2("MYFTPSIZE=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPSIZE_REQ, MSG_CMD_FTPSIZE_RSP, ext_ftpsize_req, ext_ftpsize_rsp); |
| 369 | register_serv_func2("MYFTPGET=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPGET_REQ, MSG_CMD_FTPGET_RSP, ext_ftpget_req, ext_ftpget_rsp); |
| 370 | register_serv_func2("MYFTPPUT=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPPUT_REQ, MSG_CMD_FTPPUT_RSP, ext_ftpput_req, ext_ftpput_rsp); |
| 371 | |
| 372 | register_serv_func2("FOTAFTP=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPFOTA_REQ, MSG_CMD_FTPFOTA_RSP, ext_ftpfota_req, ext_ftpfota_rsp); |
| 373 | } else if (g_customer_type == CUSTOMER_NANDIAN) { |
| 374 | |
| 375 | register_serv_func2("MYFTPOPEN=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPOPEN_REQ, MSG_CMD_FTPOPEN_RSP, ext_ftpopen_req, ext_ftpopen_rsp); |
| 376 | register_serv_func2("MYFTPOPEN?", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPSTATE_REQ, MSG_CMD_FTPSTATE_RSP, ext_ftpstate_req, ext_ftpstate_rsp); |
| 377 | register_serv_func2("MYFTPOPEN=?", MODULE_ID_SOCKET_PROXY, 0, 0, ext_ftpstate_confirm, NULL); |
| 378 | register_serv_func2("MYFTPCLOSE", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPCLOSE_REQ, MSG_CMD_FTPCLOSE_RSP, ext_ftpclose_req, ext_ftpclose_rsp); |
| 379 | register_serv_func2("MYFTPSIZE=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPSIZE_REQ, MSG_CMD_FTPSIZE_RSP, ext_ftpsize_req, ext_ftpsize_rsp); |
| 380 | register_serv_func2("MYFTPGET=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPGET_REQ, MSG_CMD_FTPGET_RSP, ext_ftpget_req, ext_ftpget_rsp); |
| 381 | register_serv_func2("MYFTPPUT=", MODULE_ID_SOCKET_PROXY, MSG_CMD_FTPPUT_REQ, MSG_CMD_FTPPUT_RSP, ext_ftpput_req, ext_ftpput_rsp); |
| 382 | } |
| 383 | //#endif |
| 384 | return 0; |
| 385 | |
| 386 | } |
| 387 | #endif |