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 "FTPSessionMgr.h"
9#include "IPAddress.h"
10#include "Stream.h"
11
12namespace ftp_client {
13
20template <class ClientType>
21class FTPClient {
22 public:
24 FTPClient(int port = FTP_COMMAND_PORT, bool useType = false) {
25 FTPLogger::writeLog(LOG_DEBUG, "FTPClient");
26 setUseTypeCommand(useType);
27 setPort(port);
28 }
29
31 void setUseTypeCommand(bool useType) {
32 use_type_command = useType;
33 }
34
36 bool begin(IPAddress remote_addr, const char *user = "anonymous",
37 const char *password = nullptr) {
38 FTPLogger::writeLog(LOG_INFO, "FTPClient", "begin");
39 this->userid = user;
40 this->password = password;
41 this->remote_addr = remote_addr;
42 return mgr.begin(remote_addr, this->port, user, password);
43 }
44
46 void end() {
47 FTPLogger::writeLog(LOG_INFO, "FTPClient", "end");
48 mgr.end();
49 }
50
52 FTPFile open(const char *filename, FileMode mode = READ_MODE,
53 bool autoClose = false) {
54 char msg[200];
55 snprintf(msg, sizeof(msg), "open: %s", filename);
56 FTPLogger::writeLog(LOG_INFO, "FTPClient", msg);
57
58 FTPBasicAPI &api = mgr.session().api();
59
60 // Open new data connection
61 api.passv();
62
63 return FTPFile(&api, filename, mode, autoClose);
64 }
65
68 bool mkdir(const char *filepath) {
69 FTPLogger::writeLog(LOG_INFO, "FTPClient", "mkdir");
70 FTPBasicAPI &api = mgr.session().api();
71 if (!api) return false;
72 return api.mkdir(filepath);
73 }
74
76 bool remove(const char *filepath) {
77 FTPLogger::writeLog(LOG_INFO, "FTPClient", "remove");
78 FTPBasicAPI &api = mgr.session().api();
79 if (!api) return false;
80 return api.del(filepath);
81 }
82
84 bool rmdir(const char *filepath) {
85 FTPLogger::writeLog(LOG_INFO, "FTPClient", "rmdir");
86 FTPBasicAPI &api = mgr.session().api();
87 if (!api) return false;
88 return api.rmd(filepath);
89 }
90
92 FTPFileIterator ls(const char *path, FileMode mode = WRITE_MODE) {
93 FTPLogger::writeLog(LOG_INFO, "FTPClient", "ls");
94 FTPBasicAPI &api = mgr.session().api();
95
96 // Open new data connection
97 api.passv();
98
99 FTPFileIterator it(&api, path, mode);
100 return it;
101 }
102
104 bool binary() {
105 FTPBasicAPI &api = mgr.session().api();
106 if (!api) return false;
107 api.setUseTypeCommand(use_type_command);
108 return api.binary();
109 }
110
112 bool ascii() {
113 FTPBasicAPI &api = mgr.session().api();
114 if (!api) return false;
115 api.setUseTypeCommand(use_type_command);
116 return api.ascii();
117 }
118
120 bool type(const char *str) {
121 FTPBasicAPI &api = mgr.session().api();
122 if (!api) return false;
123 return api.type(str);
124 }
125
126 void setPort(int port) {
127 this->port = port;
128 }
129
131 bool abort(CurrentOperation op) {
132 return mgr.abort(op);
133 }
134
137 return mgr;
138 }
139 protected:
141 IPAddress remote_addr;
142 const char *userid = nullptr;
143 const char *password = nullptr;
144 int port;
145 bool cleanup_clients;
146 bool auto_close = true;
147 bool use_type_command = false;
148
149};
150
151} // 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:76
bool abort(CurrentOperation op)
Abort the indicated operation (e.g., READ_OP, WRITE_OP, LS_OP.)
Definition FTPClient.h:131
void setUseTypeCommand(bool useType)
if set to true the BIN and ASCII command are executed as type I or A
Definition FTPClient.h:31
FTPClient(int port=FTP_COMMAND_PORT, bool useType=false)
Default constructor: Provide the client class as template argument e.g. FTPClient<WiFiClient> client;...
Definition FTPClient.h:24
FTPFileIterator ls(const char *path, FileMode mode=WRITE_MODE)
Lists all file names in the specified directory.
Definition FTPClient.h:92
bool ascii()
Switch to ascii mode.
Definition FTPClient.h:112
FTPFile open(const char *filename, FileMode mode=READ_MODE, bool autoClose=false)
Open a file.
Definition FTPClient.h:52
void end()
Close the sessions by calling QUIT or BYE.
Definition FTPClient.h:46
bool binary()
Switch to binary mode.
Definition FTPClient.h:104
bool type(const char *str)
Binary or ascii with type command.
Definition FTPClient.h:120
FTPSessionMgr< ClientType > & sessionMgr()
Provides access to the session manager.
Definition FTPClient.h:136
bool mkdir(const char *filepath)
Definition FTPClient.h:68
bool begin(IPAddress remote_addr, const char *user="anonymous", const char *password=nullptr)
Opens the FTP connection.
Definition FTPClient.h:36
bool rmdir(const char *filepath)
Removes a directory.
Definition FTPClient.h:84
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