Arduino TinyFTP
All Classes Functions Pages
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 "FTPSessionMgr.h"
9 #include "IPAddress.h"
10 #include "Stream.h"
11 
12 namespace ftp_client {
13 
20 template <class ClientType>
21 class FTPClient {
22  public:
24  FTPClient(int port = FTP_COMMAND_PORT) {
25  FTPLogger::writeLog(LOG_DEBUG, "FTPClient");
26  setPort(port);
27  }
28 
30  bool begin(IPAddress remote_addr, const char *user = "anonymous",
31  const char *password = nullptr) {
32  FTPLogger::writeLog(LOG_INFO, "FTPClient", "begin");
33  this->userid = user;
34  this->password = password;
35  this->remote_addr = remote_addr;
36  return mgr.begin(remote_addr, this->port, user, password);
37  }
38 
40  void end() {
41  FTPLogger::writeLog(LOG_INFO, "FTPClient", "end");
42  mgr.end();
43  }
44 
46  FTPFile open(const char *filename, FileMode mode = READ_MODE,
47  bool autoClose = false) {
48  char msg[200];
49  snprintf(msg, sizeof(msg), "open: %s", filename);
50  FTPLogger::writeLog(LOG_INFO, "FTPClient", msg);
51 
52  FTPBasicAPI &api = mgr.session().api();
53 
54  // Open new data connection
55  api.passv();
56 
57  return FTPFile(&api, filename, mode, autoClose);
58  }
59 
62  bool mkdir(const char *filepath) {
63  FTPLogger::writeLog(LOG_INFO, "FTPClient", "mkdir");
64  FTPBasicAPI &api = mgr.session().api();
65  if (!api) return false;
66  return api.mkdir(filepath);
67  }
68 
70  bool remove(const char *filepath) {
71  FTPLogger::writeLog(LOG_INFO, "FTPClient", "remove");
72  FTPBasicAPI &api = mgr.session().api();
73  if (!api) return false;
74  return api.del(filepath);
75  }
76 
78  bool rmdir(const char *filepath) {
79  FTPLogger::writeLog(LOG_INFO, "FTPClient", "rmdir");
80  FTPBasicAPI &api = mgr.session().api();
81  if (!api) return false;
82  return api.rmd(filepath);
83  }
84 
86  FTPFileIterator ls(const char *path, FileMode mode = WRITE_MODE) {
87  FTPLogger::writeLog(LOG_INFO, "FTPClient", "ls");
88  FTPBasicAPI &api = mgr.session().api();
89 
90  // Open new data connection
91  api.passv();
92 
93  FTPFileIterator it(&api, path, mode);
94  return it;
95  }
96 
98  bool binary() {
99  FTPBasicAPI &api = mgr.session().api();
100  if (!api) return false;
101  return api.binary();
102  }
103 
105  bool ascii() {
106  FTPBasicAPI &api = mgr.session().api();
107  if (!api) return false;
108  return api.ascii();
109  }
110 
112  bool type(const char *str) {
113  FTPBasicAPI &api = mgr.session().api();
114  if (!api) return false;
115  return api.type(str);
116  }
117 
118  void setPort(int port) {
119  this->port = port;
120  }
121 
123  bool abort(CurrentOperation op) {
124  return mgr.abort(op);
125  }
126 
129  return mgr;
130  }
131  protected:
133  IPAddress remote_addr;
134  const char *userid = nullptr;
135  const char *password = nullptr;
136  int port;
137  bool cleanup_clients;
138  bool auto_close = true;
139 
140 };
141 
142 } // namespace ftp_client
FTPBasicAPI Implementation of Low Level FTP protocol. In order to simplify the logic we always use Pa...
Definition: FTPBasicAPI.h:17
FTPClient Basic FTP access class which supports directory operations and the opening of files.
Definition: FTPClient.h:21
bool remove(const char *filepath)
Delete the file.
Definition: FTPClient.h:70
bool abort(CurrentOperation op)
Abort the indicated operation (e.g., READ_OP, WRITE_OP, LS_OP.)
Definition: FTPClient.h:123
FTPFileIterator ls(const char *path, FileMode mode=WRITE_MODE)
Lists all file names in the specified directory.
Definition: FTPClient.h:86
bool ascii()
Switch to ascii mode.
Definition: FTPClient.h:105
FTPFile open(const char *filename, FileMode mode=READ_MODE, bool autoClose=false)
Open a file.
Definition: FTPClient.h:46
void end()
Close the sessions by calling QUIT or BYE.
Definition: FTPClient.h:40
FTPClient(int port=FTP_COMMAND_PORT)
Default constructor: Provide the client class as template argument e.g. FTPClient<WiFiClient> client;...
Definition: FTPClient.h:24
bool binary()
Switch to binary mode.
Definition: FTPClient.h:98
bool type(const char *str)
Binary or ascii with type command.
Definition: FTPClient.h:112
bool mkdir(const char *filepath)
Definition: FTPClient.h:62
bool begin(IPAddress remote_addr, const char *user="anonymous", const char *password=nullptr)
Opens the FTP connection.
Definition: FTPClient.h:30
bool rmdir(const char *filepath)
Removes a directory.
Definition: FTPClient.h:78
FTPSessionMgr< ClientType > & sessionMgr()
Provides access to the session manager.
Definition: FTPClient.h:128
FTPFile A single file which supports read and write operations. This class is implemented as an Ardui...
Definition: FTPFile.h:16
FTPFileIterator The file name iterator can be used to list all available files and directories....
Definition: FTPFileIterator.h:18
FTPSessionMgr This class manages multiple FTP sessions, allowing for concurrent operations and sessio...
Definition: FTPSessionMgr.h:17