5#include "FTPBasicAPI.h"
7#include "FTPFileIterator.h"
24template <
class ClientType>
29 FTPClient(
int port = FTP_COMMAND_PORT,
bool useType =
false) {
30 FTPLogger.writeLog(LOG_DEBUG,
"FTPClient");
40 for (
auto &session : sessions) {
50 bool begin(IPAddress remote_addr,
const char *user =
"anonymous",
51 const char *password =
nullptr) {
52 FTPLogger.writeLog(LOG_INFO,
"FTPClient",
"begin");
54 this->remote_addr = remote_addr;
56 this->password = password;
63 FTPLogger.writeLog(LOG_INFO,
"FTPClient",
"end");
64 for (
auto &session : sessions) {
65 if (session !=
nullptr) {
66 if (session->connected()) session->quit();
77 snprintf(msg,
sizeof(msg),
"open: %s", filename);
78 FTPLogger.writeLog(LOG_INFO,
"FTPClient", msg);
81 if (api ==
nullptr)
return FTPFile();
82 return FTPFile(
this, api, filename, mode);
87 bool mkdir(
const char *filepath) {
88 FTPLogger.writeLog(LOG_INFO,
"FTPClient",
"mkdir");
90 return api !=
nullptr && api->mkdir(filepath);
95 FTPLogger.writeLog(LOG_INFO,
"FTPClient",
"remove");
97 return api !=
nullptr && api->del(filepath);
102 FTPLogger.writeLog(LOG_INFO,
"FTPClient",
"rmdir");
104 return api !=
nullptr && api->rmd(filepath);
109 FTPLogger.writeLog(LOG_INFO,
"FTPClient",
"ls");
118 return api !=
nullptr && api->binary();
124 return api !=
nullptr && api->ascii();
130 return api !=
nullptr && api->type(str);
133 void setPort(
int port) { this->port = port; }
138 FTPLogger.writeLog(LOG_DEBUG,
"FTPClient",
"abort");
141 for (
auto &session : sessions) {
142 if (session !=
nullptr && session->currentOperation() == op) {
144 ok = session->abort() && ok;
153 for (
auto &session : sessions) {
154 if (session !=
nullptr && session->connected()) result++;
162 for (
auto &session : sessions) {
163 if (session !=
nullptr && session->connected() &&
164 session->currentOperation() == op)
172 for (
auto &session : sessions) {
173 if (session !=
nullptr && session->currentOperation() == NOP &&
174 session->connected()) {
175 session->setUseTypeCommand(use_type_command);
180 for (
auto &session : sessions) {
181 if (session ==
nullptr) {
183 }
else if (
session->currentOperation() != NOP) {
187 if (!
session->
begin(remote_addr, port, userid, password)) {
188 FTPLogger.writeLog(LOG_ERROR,
"FTPClient",
"Connect failed");
192 session->setUseTypeCommand(use_type_command);
195 FTPLogger.writeLog(LOG_ERROR,
"FTPClient",
"No available sessions");
203 if (
result ==
nullptr)
return nullptr;
214 IPAddress remote_addr;
215 const char *userid =
nullptr;
216 const char *password =
nullptr;
218 bool use_type_command =
false;
FTPBasicAPI Implementation of Low Level FTP protocol. In order to simplify the logic we always use Pa...
Definition FTPBasicAPI.h:17
uint32_t reserve()
Reserves the session for a new transfer: returns its id.
Definition FTPBasicAPI.h:245
bool endTransfer(const char *tag)
Definition FTPBasicAPI.h:225
FTPClient Basic FTP access class which supports directory operations and the opening of files....
Definition FTPClient.h:25
void setUseTypeCommand(bool useType)
if set to true the BIN and ASCII command are executed as type I or A
Definition FTPClient.h:47
bool ascii()
Switch to ascii mode.
Definition FTPClient.h:122
bool binary()
Switch to binary mode.
Definition FTPClient.h:116
FTPClient(int port=FTP_COMMAND_PORT, bool useType=false)
Definition FTPClient.h:29
bool abort(CurrentOperation op)
Definition FTPClient.h:137
bool rmdir(const char *filepath)
Removes a directory.
Definition FTPClient.h:101
int count()
Count the connected sessions.
Definition FTPClient.h:151
bool begin(IPAddress remote_addr, const char *user="anonymous", const char *password=nullptr)
Defines the server details: the sessions are opened on demand.
Definition FTPClient.h:50
FTPSession< ClientType > * idleSession() override
Provides an idle, connected session: returns nullptr if none is available.
Definition FTPClient.h:171
void end()
Definition FTPClient.h:62
bool remove(const char *filepath)
Delete the file.
Definition FTPClient.h:94
FTPSession< ClientType > * openDataSession() override
Definition FTPClient.h:201
int count(CurrentOperation op)
Count the connected sessions with a specific current operation.
Definition FTPClient.h:160
FTPFile open(const char *filename, FileMode mode=READ_MODE, bool=true)
Definition FTPClient.h:74
bool mkdir(const char *filepath)
Definition FTPClient.h:87
FTPFileIterator ls(const char *path, FileMode mode=READ_MODE)
Lists all file names in the specified directory.
Definition FTPClient.h:108
bool type(const char *str)
Binary or ascii with type command.
Definition FTPClient.h:128
FTPFile A single file which supports read and write operations. This class is implemented as an Ardui...
Definition FTPFile.h:20
FTPFileIterator The file name iterator can be used to list all available files and directories....
Definition FTPFileIterator.h:21
FTPSession An FTPBasicAPI which owns its command and data connections.
Definition FTPSession.h:17
bool begin(IPAddress &address, int port, const char *username, const char *password)
Opens the command connection and logs in.
Definition FTPSession.h:27
void end()
Closes the command and data connections.
Definition FTPSession.h:34
FTPSessionProvider Provides sessions on demand (implemented by the FTPClient)
Definition FTPBasicAPI.h:464
FTP client and server functionality.
Definition FTPBasicAPI.h:7
FileMode
File Mode.
Definition FTPCommon.h:44
CurrentOperation
State of a session: RESERVED_OP means a data connection was opened (PASV) but no transfer command was...
Definition FTPCommon.h:47