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);
}
}