Network Component  Version 6.6
MDK-Professional Middleware for IP Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TFTP Client Routines

TFTP Client Routines provide access to a TFTP Server. More...

Functions

void tftp_client_fclose (void *file)
 Close local file previously open in TFTP client.
 
void * tftp_client_fopen (const char *fname, const char *mode)
 Open local file for reading or writing in TFTP client.
 
uint32_t tftp_client_fread (void *file, uint8_t *buf, uint32_t len)
 Read block of data from local file in TFTP client.
 
uint32_t tftp_client_fwrite (void *file, const uint8_t *buf, uint32_t len)
 Write block of data to local file in TFTP client.
 
void tftp_client_notify (tftpClientEvent event)
 Notify the user application when TFTP client operation ends.
 
netStatus tftp_client_put (const uint8_t *ip_addr, uint16_t port, const char *src, const char *dst)
 Put a file to remote TFTP server.
 
netStatus tftp_client_get (const uint8_t *ip_addr, uint16_t port, const char *src, const char *dst)
 Retrieve a file from remote TFTP server.
 
enum  tftpClientEvent {
  tftpClientSuccess = 0,
  tftpClientTimeout,
  tftpClientAccessDenied,
  tftpClientFileNotFound,
  tftpClientDiskFull,
  tftpClientError
}
 TFTP Client Events. More...
 

Description

TFTP Client Routines provide access to a TFTP Server.

TFTP routines are not reentrant.

Enumeration Type Documentation

TFTP Client Events.

Parameter for:

Enumerator:
tftpClientSuccess 

File operation successful.

tftpClientTimeout 

Timeout on file operation.

tftpClientAccessDenied 

File access not allowed.

tftpClientFileNotFound 

File not found.

tftpClientDiskFull 

Disk full (local or remote)

tftpClientError 

Generic TFTP client error.

Function Documentation

void tftp_client_fclose ( void *  file)

Close local file previously open in TFTP client.

Parameters
[in]filepointer to the file to close.
Returns
none.

The function tftp_client_fclose closes the file identified by the file stream pointer in the function argument.

The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the TFTP_Client_FS.c module.

void * tftp_client_fopen ( const char *  fname,
const char *  mode 
)

Open local file for reading or writing in TFTP client.

Parameters
[in]fnamename of the file to open.
[in]modetype of access:
  • "rb" = opens a file for reading.
  • "wb" = opens a file for writing.
Returns
status information:
  • Pointer to an open file or
  • NULL in case of an error.

The function tftp_client_fopen opens a local file for reading or writing.

The argument mode defines the type of access permitted for the file fname. mode can have one of the following values:

ModeDescription
"rb"Opens the file for reading. If the file does not exist, the function fails.
"wb"Opens an empty file for writing. If the file exists already, then the content is cleared.

The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the TFTP_Client_FS.c module.

uint32_t tftp_client_fread ( void *  file,
uint8_t *  buf,
uint32_t  len 
)

Read block of data from local file in TFTP client.

Parameters
[in]filepointer to the file to read from.
[out]bufblock of memory to write data to.
[in]lenlength of data to read in bytes.
Returns
number of bytes successfully read.

The function tftp_client_fread reads len bytes from the file identified by the file stream pointer in the function argument.

The argument buf is a pointer to the buffer where the function stores the read data.

The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the TFTP_Client_FS.c module.

Note
The function should read len bytes. The TFTP Client stops reading and closes the TFTP session, if the return value is 0 bytes.
uint32_t tftp_client_fwrite ( void *  file,
const uint8_t *  buf,
uint32_t  len 
)

Write block of data to local file in TFTP client.

Parameters
[in]filepointer to the file to write to.
[in]bufblock of memory to be written.
[in]lenlength of data to write in bytes.
Returns
number of bytes successfully written.

The function tftp_client_fwrite writes a block of data to the file identified by the file stream pointer.

The argument buf points to the buffer containing the data to write to the file.

The argument len specifies the number of bytes to write to the file.

The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the TFTP_Client_FS.c module.

Note
The function must write len bytes. The TFTP Client stops writing, aborts data transfer, and closes the TFTP session if the return value is less than len bytes.
netStatus tftp_client_get ( const uint8_t *  ip_addr,
uint16_t  port,
const char *  src,
const char *  dst 
)

Retrieve a file from remote TFTP server.

Parameters
[in]ip_addrIP address of remote TFTP server.
[in]portport number of TFTP server.
[in]srcremote file name, a null-terminated string.
[in]dstlocal file name, a null-terminated string.
Returns
status code that indicates the execution status of the function as defined with netStatus.

The function tftp_client_get starts the TFTP client for transferring a file from a remote TFTP server to the local system. The TFTP client starts a TFTP session by connecting to a TFTP server on the UDP port specified in the function argument. If the port is not specified (port has a value of 0), then the system uses the standard TFTP server port 69.

The argument ip_addr points to an array of 4 bytes containing the dotted-decimal notation of the IP address of the TFTP server.

The arguments src and dst are pointers to 0-terminated names of source and destination files. Source file is a file on the TFTP server. Destination file is a local file to which the source file content will be copied. If the destination file is not specified (dst has a value of NULL), then the source file name will be used as the destination file name.

Code Example

void get_file (void) {
const uint8_t tftp_srv[4] = {192,168,2,253};
if (tftp_client_get (tftp_srv, 0, "my_file.c", NULL) != netOK) {
printf("File transfer not started, TFTP Client not ready.\n");
}
else {
printf("File transfer started.\n");
}
}
void tftp_client_notify ( tftpClientEvent  event)

Notify the user application when TFTP client operation ends.

Parameters
[in]eventTFTP client notification event.
Returns
none.
Note
This function is called by the system to inform about events.

The function tftp_client_notify is called automatically when an TFTP event occurred and notifies the user application when the TFTP client operation ends.

The argument event is a tftpClientEvent signal:

Event Description
tftpClientSuccess The file has been successfully transferred
tftpClientTimeout TFTP Server response has timed out, and hence the TFTP client has aborted the operation
tftpClientAccessDenied File access on TFTP server is not allowed for a specified file
tftpClientFileNotFound The TFTP server was not able to find the specified file
tftpClientDiskFull The TFTP server has run out of disk space, file transfer is not possible
tftpClientError The TFTP client has encountered an error during file transfer process

The function is in the TFTP_Client_UIF.c module.

Code Example

#include <stdio.h>
#include "rl_net.h"
// Notify the user application when TFTP client operation ends.
switch (event) {
// File operation successful
break;
// Timeout on file operation
break;
// File access not allowed
break;
// File not found
break;
// Disk full (local or remote)
break;
// Generic TFTP client error
break;
}
}
netStatus tftp_client_put ( const uint8_t *  ip_addr,
uint16_t  port,
const char *  src,
const char *  dst 
)

Put a file to remote TFTP server.

Parameters
[in]ip_addrIP address of remote TFTP server.
[in]portport number of TFTP server.
[in]srclocal file name, a null-terminated string.
[in]dstremote file name, a null-terminated string.
Returns
status code that indicates the execution status of the function as defined with netStatus.

The function tftp_client_put starts the TFTP client to transfer a file from the local system to remote TFTP server. This causes the TFTP client to start a TFTP session by connecting to a TFTP server on the UDP port specified in the function argument. If the port is not specified (port has a value of 0), then the system uses a standard TFTP server port 69.

The argument ip_addr points to an array of 4 bytes containing the dotted-decimal notation of the IP address of the TFTP server.

The arguments src and dst are pointers to 0-terminated names of source and destination files. Source file is the local file to be sent. Destination file resides on the TFTP server to which the source file content will be copied. If the destination file is not specified (dst has a value of NULL), then the source file name will be used as the destination file name.

Code Example

void send_file (void) {
uint8_t tftp_srv[4] = {192,168,2,253};
if (tftp_client_put (tftp_srv, 0, "my_file.c", NULL) != netOK) {
printf("File transfer not started, TFTP Client not ready.\n");
}
else {
printf("File transfer started.\n");
}
}