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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _TFTP_CLIENT_H
30 #define _TFTP_CLIENT_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "tftp/tftp_common.h"
35 
36 //TFTP client support
37 #ifndef TFTP_CLIENT_SUPPORT
38  #define TFTP_CLIENT_SUPPORT ENABLED
39 #elif (TFTP_CLIENT_SUPPORT != ENABLED && TFTP_CLIENT_SUPPORT != DISABLED)
40  #error TFTP_CLIENT_SUPPORT parameter is not valid
41 #endif
42 
43 //TFTP client tick interval
44 #ifndef TFTP_CLIENT_TICK_INTERVAL
45  #define TFTP_CLIENT_TICK_INTERVAL 500
46 #elif (TFTP_CLIENT_TICK_INTERVAL < 100)
47  #error TFTP_CLIENT_TICK_INTERVAL parameter is not valid
48 #endif
49 
50 //Maximum number of retransmissions of packets
51 #ifndef TFTP_CLIENT_MAX_RETRIES
52  #define TFTP_CLIENT_MAX_RETRIES 5
53 #elif (TFTP_CLIENT_MAX_RETRIES < 1)
54  #error TFTP_CLIENT_MAX_RETRIES parameter is not valid
55 #endif
56 
57 //Retransmission timeout
58 #ifndef TFTP_CLIENT_TIMEOUT
59  #define TFTP_CLIENT_TIMEOUT 5000
60 #elif (TFTP_CLIENT_TIMEOUT < 1000)
61  #error TFTP_CLIENT_TIMEOUT parameter is not valid
62 #endif
63 
64 //Additional delay before closing the connection (when sending the final ACK)
65 #ifndef TFTP_CLIENT_FINAL_DELAY
66  #define TFTP_CLIENT_FINAL_DELAY 10000
67 #elif (TFTP_CLIENT_FINAL_DELAY < 1000)
68  #error TFTP_CLIENT_FINAL_DELAY parameter is not valid
69 #endif
70 
71 //Block size
72 #ifndef TFTP_CLIENT_BLOCK_SIZE
73  #define TFTP_CLIENT_BLOCK_SIZE 512
74 #elif (TFTP_CLIENT_BLOCK_SIZE < 512)
75  #error TFTP_CLIENT_BLOCK_SIZE parameter is not valid
76 #endif
77 
78 //Maximum size of TFTP packets
79 #define TFTP_CLIENT_MAX_PACKET_SIZE (sizeof(TftpDataPacket) + TFTP_CLIENT_BLOCK_SIZE)
80 
81 //C++ guard
82 #ifdef __cplusplus
83  extern "C" {
84 #endif
85 
86 
87 /**
88  * @brief File access modes
89  **/
90 
91 typedef enum
92 {
97 } TftpFileMode;
98 
99 
100 /**
101  * @brief TFTP client state
102  **/
103 
104 typedef enum
105 {
115 
116 
117 /**
118  * @brief TFTP client context
119  **/
120 
121 typedef struct
122 {
123  NetInterface *interface; ///<Underlying network interface
125  uint16_t serverPort;
126  uint16_t serverTid;
127  Socket *socket; ///<Underlying UDP socket
128  TftpClientState state; ///<TFTP client state
129  uint16_t block; ///<Block number
130  systime_t timestamp; ///<Time stamp to manage retransmissions
131  uint_t retransmitCount; ///<Retransmission counter
132  uint8_t inPacket[TFTP_CLIENT_MAX_PACKET_SIZE]; ///<Incoming TFTP packet
133  size_t inPacketLen; ///<Length of the outgoing packet
134  size_t inDataLen;
135  size_t inDataPos;
136  uint8_t outPacket[TFTP_CLIENT_MAX_PACKET_SIZE]; ///<Outgoing TFTP packet
137  size_t outPacketLen; ///<Length of the outgoing packet
138  size_t outDataLen;
140 
141 
142 //TFTP client related functions
143 void tftpClientInit(TftpClientContext *context);
144 
146  NetInterface *interface);
147 
149  const IpAddr *serverIpAddr, uint16_t serverPort);
150 
152  const char_t *filename, uint_t mode);
153 
155  const void *data, size_t length, size_t *written, uint_t flags);
156 
158  void *data, size_t size, size_t *received, uint_t flags);
159 
162 
163 //C++ guard
164 #ifdef __cplusplus
165  }
166 #endif
167 
168 #endif
TFTP client context.
Definition: tftp_client.h:121
NetInterface * interface
Underlying network interface.
Definition: tftp_client.h:123
char_t filename[]
Definition: tftp_common.h:89
uint32_t systime_t
Definition: compiler_port.h:44
systime_t timestamp
Time stamp to manage retransmissions.
Definition: tftp_client.h:130
char char_t
Definition: compiler_port.h:41
uint8_t flags
Definition: tcp.h:312
size_t outPacketLen
Length of the outgoing packet.
Definition: tftp_client.h:137
uint_t retransmitCount
Retransmission counter.
Definition: tftp_client.h:131
TCP/IP stack core.
Socket * socket
Underlying UDP socket.
Definition: tftp_client.h:127
IP network address.
Definition: ip.h:57
error_t tftpClientFlushFile(TftpClientContext *context)
Flush pending write operations.
Definition: tftp_client.c:446
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:256
TftpClientState
TFTP client state.
Definition: tftp_client.h:104
uint16_t serverPort
Definition: tftp_client.h:125
uint16_t block
Block number.
Definition: tftp_client.h:129
size_t inPacketLen
Length of the outgoing packet.
Definition: tftp_client.h:133
Definitions common to TFTP client and server.
#define Socket
Definition: socket.h:34
void tftpClientCloseFile(TftpClientContext *context)
Close the file.
Definition: tftp_client.c:502
error_t tftpClientConnect(TftpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish connection with the TFTP server.
Definition: tftp_client.c:100
TftpClientState state
TFTP client state.
Definition: tftp_client.h:128
#define TFTP_CLIENT_MAX_PACKET_SIZE
Definition: tftp_client.h:79
error_t tftpClientOpenFile(TftpClientContext *context, const char_t *filename, uint_t mode)
Open a file for reading or writing.
Definition: tftp_client.c:129
TftpFileMode
File access modes.
Definition: tftp_client.h:91
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
void tftpClientInit(TftpClientContext *context)
TFTP client initialization.
Definition: tftp_client.c:56
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
uint8_t mode
Definition: sntp_client.h:143
error_t tftpClientBindToInterface(TftpClientContext *context, NetInterface *interface)
Bind the TFTP client to a particular network interface.
Definition: tftp_client.c:77
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:343
uint8_t length
Definition: dtls_misc.h:140
uint16_t serverTid
Definition: tftp_client.h:126