tftp_client.h
Go to the documentation of this file.
1 /**
2  * @file tftp_client.h
3  * @brief TFTP client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _TFTP_CLIENT_H
32 #define _TFTP_CLIENT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "tftp/tftp_common.h"
37 
38 //TFTP client support
39 #ifndef TFTP_CLIENT_SUPPORT
40  #define TFTP_CLIENT_SUPPORT ENABLED
41 #elif (TFTP_CLIENT_SUPPORT != ENABLED && TFTP_CLIENT_SUPPORT != DISABLED)
42  #error TFTP_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //TFTP client tick interval
46 #ifndef TFTP_CLIENT_TICK_INTERVAL
47  #define TFTP_CLIENT_TICK_INTERVAL 500
48 #elif (TFTP_CLIENT_TICK_INTERVAL < 100)
49  #error TFTP_CLIENT_TICK_INTERVAL parameter is not valid
50 #endif
51 
52 //Maximum number of retransmissions of packets
53 #ifndef TFTP_CLIENT_MAX_RETRIES
54  #define TFTP_CLIENT_MAX_RETRIES 5
55 #elif (TFTP_CLIENT_MAX_RETRIES < 1)
56  #error TFTP_CLIENT_MAX_RETRIES parameter is not valid
57 #endif
58 
59 //Retransmission timeout
60 #ifndef TFTP_CLIENT_TIMEOUT
61  #define TFTP_CLIENT_TIMEOUT 5000
62 #elif (TFTP_CLIENT_TIMEOUT < 1000)
63  #error TFTP_CLIENT_TIMEOUT parameter is not valid
64 #endif
65 
66 //Additional delay before closing the connection (when sending the final ACK)
67 #ifndef TFTP_CLIENT_FINAL_DELAY
68  #define TFTP_CLIENT_FINAL_DELAY 10000
69 #elif (TFTP_CLIENT_FINAL_DELAY < 1000)
70  #error TFTP_CLIENT_FINAL_DELAY parameter is not valid
71 #endif
72 
73 //Block size
74 #ifndef TFTP_CLIENT_BLOCK_SIZE
75  #define TFTP_CLIENT_BLOCK_SIZE 512
76 #elif (TFTP_CLIENT_BLOCK_SIZE < 512)
77  #error TFTP_CLIENT_BLOCK_SIZE parameter is not valid
78 #endif
79 
80 //Application specific context
81 #ifndef TFTP_CLIENT_PRIVATE_CONTEXT
82  #define TFTP_CLIENT_PRIVATE_CONTEXT
83 #endif
84 
85 //Maximum size of TFTP packets
86 #define TFTP_CLIENT_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_CLIENT_BLOCK_SIZE)
87 
88 //C++ guard
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92 
93 
94 /**
95  * @brief File access modes
96  **/
97 
98 typedef enum
99 {
105 
106 
107 /**
108  * @brief TFTP client state
109  **/
110 
111 typedef enum
112 {
122 
123 
124 /**
125  * @brief TFTP client context
126  **/
127 
128 typedef struct
129 {
130  NetInterface *interface; ///<Underlying network interface
132  uint16_t serverPort;
133  uint16_t serverTid;
134  Socket *socket; ///<Underlying UDP socket
135  TftpClientState state; ///<TFTP client state
136  uint16_t block; ///<Block number
137  systime_t timestamp; ///<Time stamp to manage retransmissions
138  uint_t retransmitCount; ///<Retransmission counter
139  uint8_t inPacket[TFTP_CLIENT_MAX_PACKET_SIZE]; ///<Incoming TFTP packet
140  size_t inPacketLen; ///<Length of the outgoing packet
141  size_t inDataLen;
142  size_t inDataPos;
143  uint8_t outPacket[TFTP_CLIENT_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
144  size_t outPacketLen; ///<Length of the outgoing packet
145  size_t outDataLen;
146  TFTP_CLIENT_PRIVATE_CONTEXT ///<Application specific context
148 
149 
150 //TFTP client related functions
152 
154  NetInterface *interface);
155 
157  const IpAddr *serverIpAddr, uint16_t serverPort);
158 
160  const char_t *filename, uint_t mode);
161 
163  const void *data, size_t length, size_t *written, uint_t flags);
164 
166 
168  void *data, size_t size, size_t *received, uint_t flags);
169 
171 
172 void tftpClientDeinit(TftpClientContext *context);
173 
174 //C++ guard
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #endif
@ TFTP_FILE_MODE_READ
Definition: tftp_client.h:100
@ TFTP_CLIENT_STATE_CLOSED
Definition: tftp_client.h:113
TftpClientState
TFTP client state.
Definition: tftp_client.h:112
IP network address.
Definition: ip.h:90
uint8_t data[]
Definition: ethernet.h:222
@ TFTP_CLIENT_STATE_COMPLETE
Definition: tftp_client.h:119
error_t tftpClientInit(TftpClientContext *context)
TFTP client initialization.
Definition: tftp_client.c:59
size_t inPacketLen
Length of the outgoing packet.
Definition: tftp_client.h:140
TFTP client context.
Definition: tftp_client.h:129
error_t tftpClientConnect(TftpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the address of the TFTP server.
Definition: tftp_client.c:106
error_t
Error codes.
Definition: error.h:43
@ TFTP_FILE_MODE_OCTET
Definition: tftp_client.h:102
TftpFileMode
File access modes.
Definition: tftp_client.h:99
error_t tftpClientWriteFile(TftpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write data to the file.
Definition: tftp_client.c:270
error_t tftpClientCloseFile(TftpClientContext *context)
Close the file.
Definition: tftp_client.c:524
void tftpClientDeinit(TftpClientContext *context)
Release TFTP client context.
Definition: tftp_client.c:546
uint16_t serverTid
Definition: tftp_client.h:133
#define NetInterface
Definition: net.h:36
@ TFTP_CLIENT_STATE_WRQ
Definition: tftp_client.h:115
char_t filename[]
Definition: tftp_common.h:93
systime_t timestamp
Time stamp to manage retransmissions.
Definition: tftp_client.h:137
@ TFTP_FILE_MODE_NETASCII
Definition: tftp_client.h:103
uint8_t length
Definition: tcp.h:368
error_t tftpClientReadFile(TftpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Read data from the file.
Definition: tftp_client.c:422
uint32_t systime_t
System time.
#define TFTP_CLIENT_PRIVATE_CONTEXT
Definition: tftp_client.h:82
uint8_t flags
Definition: tcp.h:351
char char_t
Definition: compiler_port.h:48
Socket * socket
Underlying UDP socket.
Definition: tftp_client.h:134
#define TFTP_CLIENT_MAX_PACKET_SIZE
Definition: tftp_client.h:86
@ TFTP_CLIENT_STATE_LAST_DATA
Definition: tftp_client.h:118
uint_t retransmitCount
Retransmission counter.
Definition: tftp_client.h:138
TftpClientState state
TFTP client state.
Definition: tftp_client.h:135
#define Socket
Definition: socket.h:36
error_t tftpClientOpenFile(TftpClientContext *context, const char_t *filename, uint_t mode)
Open a file for reading or writing.
Definition: tftp_client.c:135
@ TFTP_CLIENT_STATE_ACK
Definition: tftp_client.h:117
size_t outPacketLen
Length of the outgoing packet.
Definition: tftp_client.h:144
@ TFTP_CLIENT_STATE_DATA
Definition: tftp_client.h:116
NetInterface * interface
Underlying network interface.
Definition: tftp_client.h:130
Definitions common to TFTP client and server.
uint16_t block
Block number.
Definition: tftp_client.h:136
error_t tftpClientFlushFile(TftpClientContext *context)
Flush pending write operations.
Definition: tftp_client.c:357
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
@ TFTP_CLIENT_STATE_RRQ
Definition: tftp_client.h:114
@ TFTP_CLIENT_STATE_ERROR
Definition: tftp_client.h:120
@ TFTP_FILE_MODE_WRITE
Definition: tftp_client.h:101
uint16_t serverPort
Definition: tftp_client.h:132
error_t tftpClientBindToInterface(TftpClientContext *context, NetInterface *interface)
Bind the TFTP client to a particular network interface.
Definition: tftp_client.c:83