|
|||||||||||
|
Technical Support Support Resources
Product Information |
MDK MIDDLEWARE: FTP Client Example Using MiddlewareInformation in this knowledgebase article applies to:
QUESTIONIn Pack Installer of MDK µVision I can find FTP server example project for multiple devices. But apart from the documentation of Middleware FTP client APIs, is there a FTP client example project using the FTP client APIs to demonstrate how to use these FTP client APIs and callback functions? ANSWERThe following steps show an example how to send a local file stored on the local filesystem (e.g. on a SD card) of a target device via FTP client APIs and callback functions to a FTP server. After initializing the Middleware Network stack properly by calling netInitialize() and the link status turns up and running, a FTP client session can be started with a connect request by calling netFTPc_Connect(), for instance:
const NET_ADDR4 addr = { NET_ADDR_IP4, 0, 192, 168, 0, 1 };
if (netFTPc_Connect ((NET_ADDR *)&addr, netFTP_CommandPUT) == netOK) {
printf("FTP client started.\n");
}
else {
printf("FTP client is busy.\n");
}
In this way the FTP client tries connectíng to a FTP server with the IP address 192.168.0.1 using the default port 21, and specifies the FTP command it will perform with the FTP server is to transfer/put a file from the local filesystem to the FTP server. Afterwards, add a new item to the project by selecting the FTP Client User Interface template FTP_Client_UIF.c, as shown in the following screenshot:
This FTP Client User Interface template file FTP_Client_UIF.c defines a callback function template netFTPc_Process(), which will be called for multiple times automatically by the FTP stack to process all required FTP session parameters. An example of netFTPc_Process() is given as follows:
uint32_t netFTPc_Process (netFTPc_Request request, char *buf, uint32_t buf_len) {
uint32_t len = 0;
int32_t i;
switch (request) {
case netFTPc_RequestUsername:
// Username to login to FTP server
len = sprintf (buf, "testUser");
break;
case netFTPc_RequestPassword:
// Password to login to FTP server
len = sprintf (buf, "test123");
break;
case netFTPc_RequestDirectory:
// Working directory path on server
len = sprintf (buf, "/");
break;
case netFTPc_RequestName:
// The file "test.log" is to be transferred to FTP server
len = sprintf (buf, "test.log");
break;
case netFTPc_RequestNewName:
// New name for a RENAME command
break;
case netFTPc_RequestListMask:
// File filter/mask for LIST command (wildcards allowed)
break;
case netFTPc_RequestList:
// Received data if LIST command is given
break;
case netFTPc_RequestLocalFilename:
// Local filename to be opened and read from local filesystem
len = sprintf (buf, "test.log");
break;
}
return (len);
}
In this way, at runtime first of all netFTPc_Process() will be called twice by the FTP stack with the requests netFTPc_RequestUsername followed by netFTPc_RequestPassword, in order to provide FTP username and password to the FTP server. Then, the FTP client will sends FTP PUT command to transfer a file to the FTP server. After being confirmed by the FTP server, the netFTPc_Process() is called with the request netFTPc_RequestLocalFilename by the FTP stack. In the code snippet given above, the file test.log will be opened and read from the default local filesystem used in the project. Here there is no need to manually call fopen() etc. to open and read a file. A filename here is sufficient. After reading the local file test.log, the netFTPc_Process is called again with the request netFTPc_RequestName. Here we specify test.log as the file to be transferred to the FTP server. After this test.log has been successfully transferred to the FTP server, this FTP session is closed. MORE INFORMATIONLast Reviewed: Monday, February 15, 2021 | ||||||||||
|
|||||||||||
Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.