Arduino TinyFTP
Loading...
Searching...
No Matches
FTPClient.h
1#pragma once
2
3#include "Arduino.h"
4#include "Client.h"
5#include "FTPBasicAPI.h"
6#include "FTPFile.h"
7#include "FTPFileIterator.h"
8#include "FTPSession.h"
9#include "IPAddress.h"
10#include "Stream.h"
11
12namespace tinyftp {
13
24template <class ClientType>
26 public:
29 FTPClient(int port = FTP_COMMAND_PORT, bool useType = false) {
30 FTPLogger.writeLog(LOG_DEBUG, "FTPClient");
31 setUseTypeCommand(useType);
32 setPort(port);
33 }
34
35 FTPClient(const FTPClient &) = delete;
36 FTPClient &operator=(const FTPClient &) = delete;
37
38 ~FTPClient() {
39 end();
40 for (auto &session : sessions) {
41 delete session;
42 session = nullptr;
43 }
44 }
45
47 void setUseTypeCommand(bool useType) { use_type_command = useType; }
48
50 bool begin(IPAddress remote_addr, const char *user = "anonymous",
51 const char *password = nullptr) {
52 FTPLogger.writeLog(LOG_INFO, "FTPClient", "begin");
53 end();
54 this->remote_addr = remote_addr;
55 this->userid = user;
56 this->password = password;
57 return true;
58 }
59
62 void end() {
63 FTPLogger.writeLog(LOG_INFO, "FTPClient", "end");
64 for (auto &session : sessions) {
65 if (session != nullptr) {
66 if (session->connected()) session->quit();
67 session->end(); // closes the connections
68 }
69 }
70 }
71
74 FTPFile open(const char *filename, FileMode mode = READ_MODE,
75 bool /*autoClose*/ = true) {
76 char msg[200];
77 snprintf(msg, sizeof(msg), "open: %s", filename);
78 FTPLogger.writeLog(LOG_INFO, "FTPClient", msg);
79
81 if (api == nullptr) return FTPFile();
82 return FTPFile(this, api, filename, mode);
83 }
84
87 bool mkdir(const char *filepath) {
88 FTPLogger.writeLog(LOG_INFO, "FTPClient", "mkdir");
89 FTPBasicAPI *api = idleSession();
90 return api != nullptr && api->mkdir(filepath);
91 }
92
94 bool remove(const char *filepath) {
95 FTPLogger.writeLog(LOG_INFO, "FTPClient", "remove");
96 FTPBasicAPI *api = idleSession();
97 return api != nullptr && api->del(filepath);
98 }
99
101 bool rmdir(const char *filepath) {
102 FTPLogger.writeLog(LOG_INFO, "FTPClient", "rmdir");
103 FTPBasicAPI *api = idleSession();
104 return api != nullptr && api->rmd(filepath);
105 }
106
108 FTPFileIterator ls(const char *path, FileMode mode = READ_MODE) {
109 FTPLogger.writeLog(LOG_INFO, "FTPClient", "ls");
111 if (api == nullptr) return FTPFileIterator();
112 return FTPFileIterator(this, api, path, mode);
113 }
114
116 bool binary() {
117 FTPBasicAPI *api = idleSession();
118 return api != nullptr && api->binary();
119 }
120
122 bool ascii() {
123 FTPBasicAPI *api = idleSession();
124 return api != nullptr && api->ascii();
125 }
126
128 bool type(const char *str) {
129 FTPBasicAPI *api = idleSession();
130 return api != nullptr && api->type(str);
131 }
132
133 void setPort(int port) { this->port = port; }
134
138 FTPLogger.writeLog(LOG_DEBUG, "FTPClient", "abort");
139 bool found = false;
140 bool ok = true;
141 for (auto &session : sessions) {
142 if (session != nullptr && session->currentOperation() == op) {
143 found = true;
144 ok = session->abort() && ok;
145 }
146 }
147 return found && ok;
148 }
149
151 int count() {
152 int result = 0;
153 for (auto &session : sessions) {
154 if (session != nullptr && session->connected()) result++;
155 }
156 return result;
157 }
158
161 int result = 0;
162 for (auto &session : sessions) {
163 if (session != nullptr && session->connected() &&
164 session->currentOperation() == op)
165 result++;
166 }
167 return result;
168 }
169
172 for (auto &session : sessions) {
173 if (session != nullptr && session->currentOperation() == NOP &&
174 session->connected()) {
175 session->setUseTypeCommand(use_type_command);
176 return session;
177 }
178 }
179 // (re)connect an unused session: idle sessions are not connected here
180 for (auto &session : sessions) {
181 if (session == nullptr) {
182 session = new FTPSession<ClientType>();
183 } else if (session->currentOperation() != NOP) {
184 continue;
185 }
186 session->end();
187 if (!session->begin(remote_addr, port, userid, password)) {
188 FTPLogger.writeLog(LOG_ERROR, "FTPClient", "Connect failed");
189 session->end();
190 return nullptr;
191 }
192 session->setUseTypeCommand(use_type_command);
193 return session;
194 }
195 FTPLogger.writeLog(LOG_ERROR, "FTPClient", "No available sessions");
196 return nullptr;
197 }
198
203 if (result == nullptr) return nullptr;
204 if (!result->passv()) {
205 result->endTransfer("passv failed");
206 return nullptr;
207 }
208 result->reserve();
209 return result;
210 }
211
212 protected:
213 FTPSession<ClientType> *sessions[FTP_MAX_SESSIONS] = {nullptr};
214 IPAddress remote_addr;
215 const char *userid = nullptr;
216 const char *password = nullptr;
217 int port;
218 bool use_type_command = false;
219};
220
221} // namespace tinyftp
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