Trivial File Transfer Protocol (TFTP) Client (Alpha)#
The TFTP Client facilitates file exchange between a client and any remote host over UDP. This component provides an easy-to-use solution for downloading and uploading files from a remote host. The TFTP client can be initialized with sl_tftp_clnt_init(), and the functions sl_tftp_clnt_get() and sl_tftp_clnt_put() can be used to download and upload files, respectively. Additionally, the block size and timeout options can be configured to negotiate between the server and the client, optimizing the transmission. The component implements the TFTP protocol as specified in RFC 1350.
To ensure proper functionality, a TFTP server, such as tftpd-hpa, must be prepared on the remote host.
The following code example shows how to download a file with TFTP Client:
#include "sl_tftp_clnt.h"
// TFTP data callback
// In this callback you can implement storing data chunks to flash, heap or any other memory.
static void tftp_data_hnd(sl_tftp_clnt_t * const clnt,
const uint8_t * const data_ptr,
const uint16_t data_size) {
(void)data_ptr;
(void)data_size;
printf("Data received chunk %u (%u bytes)\n",
clnt->packet.content.data.block_num,
clnt->packet.content.data.data_size);
}
// TFTP error callback
// You can implement error handling in this callback.
static void tftp_error_hnd(sl_tftp_clnt_t * const clnt,
const uint16_t error_code,
const char *error_msg) {
(void)clnt;
printf("Error (%u): %s\n", error_code, error_msg);
}
static void tftp_get_file(void) {
static sl_tftp_clnt_t tftp_clnt = { 0 };
// Initialize Client instance
if (sl_tftp_clnt_init(&tftp_clnt,
"2001:db8::1",
SL_TFTP_DEFAULT_HOST_PORT,
tftp_data_hnd,
tftp_error_hnd) != SL_STATUS_OK) {
// Error handling
return;
}
// Set blocksize option
if (sl_tftp_clnt_set_option(&tftp_clnt,
SL_TFTP_OPT_EXT_BLOCKSIZE,
1228) != SL_STATUS_OK) {
// Error handling
return;
}
// Start to download a file
if (sl_tftp_clnt_get(&tftp_clnt,
"test.txt") != SL_STATUS_OK) {
// Error handling
return;
}
while (1) {
if (sl_tftp_clnt_is_op_finished(&tftp_clnt)) {
printf("File download finished\n");
break;
}
osDelay(1);
}
}
Modules#
Typedefs#
Data handler callback definition.
Error handler callback defintion.
Functions#
Init TFTP Client default.
TFT Client Get.
Init TFTP Client.
TFTP Client is finished operation.
TFTP Client is Get operation.
TFTP Client is Put operation.
TFTP Client is RRQ or WRQ failed.
Print packet.
TFT Client Put.
TFTP Client request.
Init TFTP Client service.
Set TFTP Client option.
TFTP Client terminate session.
Macros#
TFTP Default data block size.
Default TFTP host port.
TFTP Default server retransmit timeout interval in seconds.
Access violation.
Disk full or allocation exceeded.
File already exists.
File not found.
Illegal TFTP operation.
Not defined, see error message (if any).
No such user.
Terminate transfer due to option negotiation.
Unknown transfer ID.
Netascii mode string.
Netascii mode string length.
Octet mode string.
Octet mode string length.
TFTP Acknowledgement operation code.
TFTP Data operation code.
TFTP Error operation code.
TFTP Option Acknowledgement operation code.
TFTP Read Request operation code.
TFTP Write Request operation code.
TFTP Blocksize Option.
TFTP Blocksize Option length.
TFTP Multicast Option.
TFTP Multicast Option length.
TFTP Timeout Interval Option.
TFTP Timeout Interval Option length.
TFTP Transfer Size Option.
TFTP Transfer Size Option length.
TFTP Windowsize Option.
TFTP Windowsize Option length.
TFTP Service loop definition.
TFTP string max length (filename, mode)
Typedef Documentation#
sl_tftp_clnt_data_hnd_t#
typedef void(* sl_tftp_clnt_data_hnd_t) (sl_tftp_clnt_t *const clnt, const uint8_t *const data_ptr, const uint16_t data_size) )(sl_tftp_clnt_t *const clnt, const uint8_t *const data_ptr, const uint16_t data_size)
Data handler callback definition.
sl_tftp_clnt_error_hnd_t#
typedef void(* sl_tftp_clnt_error_hnd_t) (sl_tftp_clnt_t *const clnt, const uint16_t error_code, const char *error_msg) )(sl_tftp_clnt_t *const clnt, const uint16_t error_code, const char *error_msg)
Error handler callback defintion.
Function Documentation#
sl_tftp_clnt_default_init#
__STATIC_INLINE sl_status_t sl_tftp_clnt_default_init (sl_tftp_clnt_t *const clnt)
Init TFTP Client default.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client to initialize |
Initialize Client with default values from config file parameters: host: SL_TFTP_CLNT_DEFAULT_HOST port: SL_TFTP_DEFAULT_HOST_PORT data_hnd: simple hex dump callback error_hnd: error code and message printer Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL
sl_tftp_clnt_get#
__STATIC_INLINE sl_status_t sl_tftp_clnt_get (sl_tftp_clnt_t *const clnt, const char * file)
TFT Client Get.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client |
const char * | [in] | file | File name |
Get file from server in octet mode Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL
sl_tftp_clnt_init#
sl_status_t sl_tftp_clnt_init (sl_tftp_clnt_t *const clnt, const char * host, const uint16_t port, sl_tftp_clnt_data_hnd_t data_hnd, sl_tftp_clnt_error_hnd_t error_hnd)
Init TFTP Client.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client to initialize |
const char * | [in] | host | Host address string |
const uint16_t | [in] | port | Port number |
sl_tftp_clnt_data_hnd_t | [in] | data_hnd | Data handler callback |
sl_tftp_clnt_error_hnd_t | [in] | error_hnd | Error handler callback |
Initialize TFTP Client context Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL
sl_tftp_clnt_is_op_finished#
bool sl_tftp_clnt_is_op_finished (const sl_tftp_clnt_t *const clnt)
TFTP Client is finished operation.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_tftp_clnt_t *const | [in] | clnt | Client |
Check read or write operations wheither is in progress Returns
true Is finished
false Is in progress
sl_tftp_clnt_is_op_get#
bool sl_tftp_clnt_is_op_get (const sl_tftp_clnt_t *const clnt)
TFTP Client is Get operation.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_tftp_clnt_t *const | [in] | clnt | Client |
Check Read operation is in progress Returns
true Is in progress otherwise false
sl_tftp_clnt_is_op_put#
bool sl_tftp_clnt_is_op_put (const sl_tftp_clnt_t *const clnt)
TFTP Client is Put operation.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_tftp_clnt_t *const | [in] | clnt | Client |
Check Write operation is in progress Returns
true Is in progress otherwise false
sl_tftp_clnt_is_op_rrq_wrq_failed#
bool sl_tftp_clnt_is_op_rrq_wrq_failed (const sl_tftp_clnt_t *const clnt)
TFTP Client is RRQ or WRQ failed.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_tftp_clnt_t *const | [in] | clnt | Client |
Check Read or Write Request operation is failed Returns
true Is failed otherwise false
sl_tftp_clnt_print_pkt#
void sl_tftp_clnt_print_pkt (const sl_tftp_pkt_t *const pkt)
Print packet.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_tftp_pkt_t *const | [in] | pkt | Packet to print |
Print packet info in json format
sl_tftp_clnt_put#
__STATIC_INLINE sl_status_t sl_tftp_clnt_put (sl_tftp_clnt_t *const clnt, const char * file)
TFT Client Put.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client |
const char * | [in] | file | File name to store data |
Send buffer content to file on remote server Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL
sl_tftp_clnt_request#
sl_status_t sl_tftp_clnt_request (sl_tftp_clnt_t *const clnt, const uint16_t opcode, const char * file, const char * mode)
TFTP Client request.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client |
const uint16_t | [in] | opcode | Operation code: SL_TFTP_OPCODE_RRQ or SL_TFTP_OPCODE_WRQ |
const char * | [in] | file | File name to put or get |
const char * | [in] | mode | Mode string |
Send request for Server (RRQ or WRQ) Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL
sl_tftp_clnt_service_init#
void sl_tftp_clnt_service_init (void )
Init TFTP Client service.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Initialize TFTP client OS objects
sl_tftp_clnt_set_option#
sl_status_t sl_tftp_clnt_set_option (sl_tftp_clnt_t *const clnt, const char * opt, const uint32_t value)
Set TFTP Client option.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client |
const char * | [in] | opt | Option string |
const uint32_t | [in] | value | Option value |
Set TFTP Client option in the TFTP client instance. The supported options are:
SL_TFTP_OPT_EXT_BLOCKSIZE: Blocksize option (8 - 1228 for Wi-SUN)
SL_TFTP_OPT_EXT_TIMEOUT_INTERVAL: Timeout interval option (1 - 255) Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL
sl_tftp_clnt_terminate_session#
sl_status_t sl_tftp_clnt_terminate_session (sl_tftp_clnt_t *const clnt)
TFTP Client terminate session.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_tftp_clnt_t *const | [inout] | clnt | Client |
Terminate TFTP session Returns
sl_status_t SL_STATUS_OK on success, otherwise SL_STATUS_FAIL