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-2019 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 1.9.6
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 //Maximum size of TFTP packets
81 #define TFTP_CLIENT_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_CLIENT_BLOCK_SIZE)
82 
83 //C++ guard
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 
89 /**
90  * @brief File access modes
91  **/
92 
93 typedef enum
94 {
99 } TftpFileMode;
100 
101 
102 /**
103  * @brief TFTP client state
104  **/
105 
106 typedef enum
107 {
117 
118 
119 /**
120  * @brief TFTP client context
121  **/
122 
123 typedef struct
124 {
125  NetInterface *interface; ///<Underlying network interface
127  uint16_t serverPort;
128  uint16_t serverTid;
129  Socket *socket; ///<Underlying UDP socket
130  TftpClientState state; ///<TFTP client state
131  uint16_t block; ///<Block number
132  systime_t timestamp; ///<Time stamp to manage retransmissions
133  uint_t retransmitCount; ///<Retransmission counter
134  uint8_t inPacket[TFTP_CLIENT_MAX_PACKET_SIZE]; ///<Incoming TFTP packet
135  size_t inPacketLen; ///<Length of the outgoing packet
136  size_t inDataLen;
137  size_t inDataPos;
138  uint8_t outPacket[TFTP_CLIENT_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
139  size_t outPacketLen; ///<Length of the outgoing packet
140  size_t outDataLen;
142 
143 
144 //TFTP client related functions
146 
148  NetInterface *interface);
149 
151  const IpAddr *serverIpAddr, uint16_t serverPort);
152 
154  const char_t *filename, uint_t mode);
155 
157  const void *data, size_t length, size_t *written, uint_t flags);
158 
160 
162  void *data, size_t size, size_t *received, uint_t flags);
163 
165 
166 void tftpClientDeinit(TftpClientContext *context);
167 
168 //C++ guard
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif
@ TFTP_FILE_MODE_READ
Definition: tftp_client.h:95
uint8_t length
Definition: dtls_misc.h:149
@ TFTP_CLIENT_STATE_CLOSED
Definition: tftp_client.h:108
TftpClientState
TFTP client state.
Definition: tftp_client.h:106
IP network address.
Definition: ip.h:71
@ TFTP_CLIENT_STATE_COMPLETE
Definition: tftp_client.h:114
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:135
TFTP client context.
Definition: tftp_client.h:123
error_t tftpClientConnect(TftpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the address of the TFTP server.
Definition: tftp_client.c:106
char_t filename[]
Definition: tftp_common.h:91
error_t
Error codes.
Definition: error.h:42
@ TFTP_FILE_MODE_OCTET
Definition: tftp_client.h:97
TftpFileMode
File access modes.
Definition: tftp_client.h:93
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:262
error_t tftpClientCloseFile(TftpClientContext *context)
Close the file.
Definition: tftp_client.c:516
void tftpClientDeinit(TftpClientContext *context)
Release TFTP client context.
Definition: tftp_client.c:538
uint16_t serverTid
Definition: tftp_client.h:128
#define NetInterface
Definition: net.h:36
@ TFTP_CLIENT_STATE_WRQ
Definition: tftp_client.h:110
systime_t timestamp
Time stamp to manage retransmissions.
Definition: tftp_client.h:132
@ TFTP_FILE_MODE_NETASCII
Definition: tftp_client.h:98
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:414
uint8_t flags
Definition: tcp.h:314
char char_t
Definition: compiler_port.h:43
Socket * socket
Underlying UDP socket.
Definition: tftp_client.h:129
#define TFTP_CLIENT_MAX_PACKET_SIZE
Definition: tftp_client.h:81
@ TFTP_CLIENT_STATE_LAST_DATA
Definition: tftp_client.h:113
uint_t retransmitCount
Retransmission counter.
Definition: tftp_client.h:133
TftpClientState state
TFTP client state.
Definition: tftp_client.h:130
#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:112
size_t outPacketLen
Length of the outgoing packet.
Definition: tftp_client.h:139
uint8_t mode
Definition: ntp_common.h:149
@ TFTP_CLIENT_STATE_DATA
Definition: tftp_client.h:111
NetInterface * interface
Underlying network interface.
Definition: tftp_client.h:125
Definitions common to TFTP client and server.
uint16_t block
Block number.
Definition: tftp_client.h:131
error_t tftpClientFlushFile(TftpClientContext *context)
Flush pending write operations.
Definition: tftp_client.c:349
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
@ TFTP_CLIENT_STATE_RRQ
Definition: tftp_client.h:109
@ TFTP_CLIENT_STATE_ERROR
Definition: tftp_client.h:115
@ TFTP_FILE_MODE_WRITE
Definition: tftp_client.h:96
uint32_t systime_t
Definition: compiler_port.h:46
uint16_t serverPort
Definition: tftp_client.h:127
error_t tftpClientBindToInterface(TftpClientContext *context, NetInterface *interface)
Bind the TFTP client to a particular network interface.
Definition: tftp_client.c:83