19 FTPBasicAPI() { FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI"); }
21 ~FTPBasicAPI() { FTPLogger.writeLog(LOG_DEBUG,
"~FTPBasicAPI"); }
23 bool begin(Client *cmdPar, Client *dataPar, IPAddress &address,
int port,
24 const char *username,
const char *password) {
25 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI ",
"open");
28 remote_address = address;
30 if (!connect(address, port, command_ptr,
true))
return false;
31 if (username !=
nullptr) {
32 const char *ok_result[] = {
"331",
"230",
nullptr};
33 if (!cmd(
"USER", username, ok_result))
return false;
35 if (strncmp(result_reply,
"331", 3) == 0) {
36 const char *ok_pass[] = {
"230",
"202",
nullptr};
37 if (!cmd(
"PASS", password ==
nullptr ?
"" : password, ok_pass))
43 const char *ok_result[] = {
"200",
nullptr};
44 cmd(
"OPTS",
"UTF8", ok_result);
50 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"quit");
51 const char *ok_result[] = {
"221",
"226",
nullptr};
52 bool result = cmd(
"QUIT",
nullptr, ok_result,
false);
54 result = cmd(
"BYE",
nullptr, ok_result,
false);
57 result = cmd(
"DISCONNECT",
nullptr, ok_result,
false);
62 bool connected() {
return is_open; }
64 operator bool() {
return connected(); }
67 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"passv");
68 bool ok = cmd(
"PASV",
nullptr,
"227");
71 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::passv", result_reply);
74 int p1 = atoi(result_reply + start1);
76 snprintf(buffer,
sizeof(buffer),
"*** port1 -> %d ", p1);
77 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::passv", buffer);
80 int p2 = atoi(result_reply + start2);
82 snprintf(buffer,
sizeof(buffer),
"*** port2 -> %d ", p2);
83 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::passv", buffer);
85 int dataPort = (p1 * 256) + p2;
86 snprintf(buffer,
sizeof(buffer),
"*** data port: %d", dataPort);
87 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::passv", buffer);
89 ok = connect(remote_address, dataPort, data_ptr);
94 bool del(
const char *file) {
95 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"del");
96 return cmd(
"DELE", file,
"250");
99 bool mkdir(
const char *dir) {
100 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"mkdir");
101 return cmd(
"MKD", dir,
"257");
104 bool rmd(
const char *dir) {
105 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"rd");
106 return cmd(
"RMD", dir,
"250");
109 size_t size(
const char *file) {
110 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"size");
111 if (cmd(
"SIZE", file,
"213")) {
112 return atol(result_reply + 4);
117 ObjectType objectType(
const char *file) {
118 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"objectType");
119 const char *ok_result[] = {
"213",
"550",
nullptr};
120 ObjectType result = TypeDirectory;
121 if (cmd(
"SIZE", file, ok_result)) {
122 if (strncmp(result_reply,
"213", 3) == 0) {
131 if (current_operation == RESERVED_OP) {
134 }
else if (current_operation == READ_OP || current_operation == WRITE_OP ||
135 current_operation == LS_OP) {
136 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"abort");
138 setCurrentOperation(NOP);
140 const char *ok[] = {
"426",
"226",
"225",
nullptr};
141 rc = cmd(
"ABOR",
nullptr, ok);
142 if (rc && strncmp(result_reply,
"426", 3) == 0) {
144 const char *ok_end[] = {
"226",
"225",
nullptr};
145 rc = checkResult(ok_end,
"abort-end");
156 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"binary");
157 const char *type_cmd = use_type ?
"TYPE I" :
"BIN";
158 return cmd(type_cmd,
nullptr,
"200");
162 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"ascii");
163 const char *type_cmd = use_type ?
"TYPE A" :
"ASC";
164 return cmd(type_cmd,
nullptr,
"200");
167 bool type(
const char *txt) {
168 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"type");
169 return cmd(
"TYPE", txt,
"200");
173 Client *
read(
const char *file_name) {
174 if (current_operation != READ_OP) {
175 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"read");
176 const char *ok[] = {
"150",
"125",
nullptr};
177 if (!cmd(
"RETR", file_name, ok)) {
181 setCurrentOperation(READ_OP);
188 if (current_operation != WRITE_OP) {
189 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"write");
190 const char *ok_write[] = {
"125",
"150",
nullptr};
191 if (!cmd(mode == WRITE_APPEND_MODE ?
"APPE" :
"STOR", file_name,
196 setCurrentOperation(WRITE_OP);
202 Client *
ls(
const char *file_name) {
203 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"ls");
204 const char *ok[] = {
"125",
"150",
nullptr};
205 if (!cmd(
"NLST", file_name, ok)) {
209 setCurrentOperation(LS_OP);
215 bool dataConnected() {
return data_ptr !=
nullptr && data_ptr->connected(); }
218 FTPLogger.writeLog(LOG_INFO,
"FTPBasicAPI",
"closeData");
227 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::endTransfer", tag);
228 if (current_operation == READ_OP || current_operation == LS_OP) {
231 const char *ok_result[] = {
"226",
"250",
"426",
nullptr};
232 ok = checkResult(ok_result, tag,
true);
233 }
else if (current_operation == WRITE_OP) {
235 const char *ok_result[] = {
"226",
"250",
nullptr};
236 ok = checkResult(ok_result, tag,
true);
237 }
else if (current_operation == RESERVED_OP) {
240 setCurrentOperation(NOP);
246 if (++transfer_id == 0) transfer_id = 1;
248 setCurrentOperation(RESERVED_OP);
257 return id != 0 &&
id == transfer_id && current_operation != NOP;
273 snprintf(msg,
sizeof(msg),
"setCurrentOperation: %d", (
int)op);
274 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI", msg);
275 current_operation = op;
281 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI",
"flush");
285 bool checkResult(
const char *expected[],
const char *command,
286 bool wait_for_data =
true) {
287 result_reply[0] =
'\0';
289 if (!wait_for_data && command_ptr->available() == 0)
return true;
291 char result_str[FTP_RESULT_BUFFER_SIZE];
292 bool ok =
readReply(result_str,
sizeof(result_str));
297 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::checkResult", result_str);
298 strncpy(result_reply, result_str,
sizeof(result_reply) - 1);
299 result_reply[
sizeof(result_reply) - 1] =
'\0';
305 FTPLogger.writeLog(LOG_ERROR,
"FTPBasicAPI::checkResult", command);
306 FTPLogger.writeLog(LOG_ERROR,
"FTPBasicAPI::checkResult", result_reply);
311 bool cmd(
const char *command,
const char *par,
const char *expected,
312 bool wait_for_data =
true) {
313 const char *expected_array[] = {expected,
nullptr};
314 return cmd(command, par, expected_array, wait_for_data);
317 bool cmd(
const char *command_str,
const char *par,
const char *expected[],
318 bool wait_for_data =
true) {
319 char command_buffer[FTP_COMMAND_BUFFER_SIZE];
320 if (par ==
nullptr) {
321 snprintf(command_buffer, FTP_COMMAND_BUFFER_SIZE,
"%s", command_str);
323 snprintf(command_buffer, FTP_COMMAND_BUFFER_SIZE,
"%s %s", command_str,
326 command_ptr->println(command_buffer);
327 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::cmd", command_buffer);
329 return checkResult(expected, command_buffer, wait_for_data);
332 void setUseTypeCommand(
bool useType) { use_type = useType; }
337 Client *command_ptr =
nullptr;
338 Client *data_ptr =
nullptr;
339 IPAddress remote_address;
340 bool is_open =
false;
341 bool use_type =
false;
342 char result_reply[100] = {0};
343 uint32_t transfer_id = 0;
348 current_operation = NOP;
350 if (++transfer_id == 0) transfer_id = 1;
355 char reply[FTP_RESULT_BUFFER_SIZE];
356 unsigned long start = millis();
357 while (millis() - start < ms) {
358 if (command_ptr->available() > 0) {
360 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::skipReplies", reply);
368 bool connect(IPAddress adr,
int port, Client *client_ptr,
369 bool doCheckResult =
false) {
371 snprintf(buffer,
sizeof(buffer),
"connect %d.%d.%d.%d:%d", adr[0], adr[1],
372 adr[2], adr[3], port);
373 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::connect", buffer);
375 if (client_ptr->connected()) client_ptr->stop();
376 for (
int j = 0; j < FTP_CONNECT_RETRIES; j++) {
377 if (j > 0) delay(FTP_CONNECT_RETRY_DELAY_MS);
378 if (client_ptr->connect(adr, port))
break;
380 bool ok = client_ptr->connected();
381 if (ok && doCheckResult) {
382 const char *ok_result[] = {
"220",
"200",
nullptr};
383 ok = checkResult(ok_result,
"connect");
387 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::connected", buffer);
389 FTPLogger.writeLog(LOG_ERROR,
"FTPBasicAPI::connected", buffer);
397 if (expected[0] ==
nullptr) {
398 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::checkResult",
399 "success because of not expected result codes");
402 for (
int j = 0; expected[j] !=
nullptr; j++) {
403 if (strncmp(reply, expected[j], 3) == 0) {
405 snprintf(msg,
sizeof(msg),
" -> success with %s", expected[j]);
406 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::checkResult", msg);
419 }
while (strlen(reply) < 3);
422 if (reply[3] ==
'-') {
423 char line[FTP_RESULT_BUFFER_SIZE];
426 FTPLogger.writeLog(LOG_DEBUG,
"FTPBasicAPI::readReply", line);
427 }
while (!(strncmp(line, reply, 3) == 0 && line[3] ==
' '));
436 unsigned long start = millis();
438 if (command_ptr->available() > 0) {
439 int c = command_ptr->read();
441 if (c ==
'\n')
break;
442 if (len < maxLen - 1) str[len++] = (char)c;
443 }
else if (!command_ptr->connected() ||
444 millis() - start >= FTP_REPLY_TIMEOUT_MS) {
446 FTPLogger.writeLog(LOG_ERROR,
"FTPBasicAPI::readReplyLine",
447 "timeout or connection closed");
454 if (len > 0 && str[len - 1] ==
'\r') len--;