RF24Ethernet - TCP/IP over RF24Network v1.6.16
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
RF24Client.h
Go to the documentation of this file.
2/*
3 RF24Client.h - Arduino implementation of a uIP wrapper class.
4 Copyright (c) 2014 tmrh20@gmail.com, github.com/TMRh20
5 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
6 All rights reserved.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef RF24CLIENT_H
20#define RF24CLIENT_H
21
22#include "Print.h"
23#include "Client.h"
24
25//#define UIP_SOCKET_DATALEN UIP_TCP_MSS
26//#define UIP_SOCKET_NUMPACKETS UIP_RECEIVE_WINDOW/UIP_TCP_MSS+1
27//#ifndef UIP_SOCKET_NUMPACKETS
28//#define UIP_SOCKET_NUMPACKETS 5
29//#endif
30
31#define UIP_CLIENT_CONNECTED 0x10
32#define UIP_CLIENT_CLOSE 0x20
33#define UIP_CLIENT_REMOTECLOSED 0x40
34#define UIP_CLIENT_RESTART 0x80
35#define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART)
36#define UIP_CLIENT_SOCKETS ~UIP_CLIENT_STATEFLAGS
37
38/**
39 * @warning <b> This is used internally and should not be accessed directly by users </b>
40 */
41typedef struct
42{
43 uint8_t state;
44 uint8_t packets_in[UIP_SOCKET_NUMPACKETS];
45 /** The local TCP port, in network byte order. */
46 uint16_t lport;
47} uip_userdata_closed_t;
48
49/**
50 * Data structure for holding per connection data
51 * @warning <b> This is used internally and should not be accessed directly by users </b>
52 */
53typedef struct __attribute__((__packed__))
54{
55 bool hold;
56 bool packets_in;
57 bool packets_out;
58 bool windowOpened;
59 uint8_t state;
60 uint16_t in_pos;
61 uint16_t out_pos;
62 uint16_t dataCnt;
63#if UIP_CLIENT_TIMER >= 0
64 uint32_t timer;
65#endif
66 uint32_t restartTime;
67 uint32_t restartInterval;
68#if UIP_CONNECTION_TIMEOUT > 0
69 uint32_t connectTimeout;
70 uint32_t connectTimer;
71#endif
72 uint8_t myData[OUTPUT_BUFFER_SIZE];
73} uip_userdata_t;
74
75class RF24Client : public Client
76{
77
78public:
79 /** Basic constructor */
80 RF24Client();
81
82 /** Establish a connection to a specified IP address and port */
83 int connect(IPAddress ip, uint16_t port);
84
85 /**
86 * Establish a connection to a given hostname and port
87 * @note UDP must be enabled in uip-conf.h for DNS lookups to work
88 *
89 * @note Tip: DNS lookups generally require a buffer size of 250-300 bytes or greater.
90 * Lookups will generally return responses with a single A record if using hostnames like
91 * "www.google.com" instead of "google.com" which works well with the default buffer size
92 */
93 int connect(const char* host, uint16_t port);
94
95 /**
96 * Read available data into a buffer
97 * @code
98 * uint8_t buf[size];
99 * client.read(buf,size);
100 * @endcode
101 */
102 int read(uint8_t* buf, size_t size);
103
104 /**
105 * Read data one byte at a time
106 * @code
107 * char c = client.read();
108 * @endcode
109 */
110 int read();
111
112 /** Disconnects from the current active connection */
113 void stop();
114
115 /** Indicates whether the client is connected or not */
116 uint8_t connected();
117
118 /**
119 * Write a single byte of data to the stream
120 * @note This will write an entire TCP payload with only 1 byte in it
121 */
122 size_t write(uint8_t);
123
124 /** Write a buffer of data, to be sent in a single TCP packet */
125 size_t write(const uint8_t* buf, size_t size);
126
127 /**
128 * Indicates whether data is available to be read by the client.
129 * @return Returns the number of bytes available to be read
130 * @note Calling client or server available() keeps the IP stack and RF24Network layer running, so needs to be called regularly,
131 * even when disconnected or delaying for extended periods.
132 */
133 int available();
134
135 /**
136 * Wait Available
137 *
138 * Helps to ensure all incoming data has been received, prior to writing data back to the client, etc.
139 *
140 * Indicates whether data is available to be read by the client, after waiting a maximum period of time.
141 * @return Returns the number of bytes available to be read or 0 if timed out
142 * @note Calling client or server available() keeps the IP stack and RF24Network layer running, so needs to be called regularly,
143 * even when disconnected or delaying for extended periods.
144 */
145 int waitAvailable(uint32_t timeout = 750);
146
147 /** Read a byte from the incoming buffer without advancing the point of reading */
148 int peek();
149
150 /** Flush all incoming client data from the current connection/buffer */
151 void flush();
152
153 using Print::write;
154
155 operator bool();
156 virtual bool operator==(const EthernetClient&);
157 virtual bool operator!=(const EthernetClient& rhs)
158 {
159 return !this->operator==(rhs);
160 };
161
162 static uip_userdata_t all_data[UIP_CONNS];
163
164private:
165 RF24Client(struct uip_conn* _conn);
166 RF24Client(uip_userdata_t* conn_data);
167
168 uip_userdata_t* data;
169
170 static int _available(uip_userdata_t*);
171
172 static uip_userdata_t* _allocateData();
173 static size_t _write(uip_userdata_t*, const uint8_t* buf, size_t size);
174
175 friend class RF24EthernetClass;
176 friend class RF24Server;
177
178 friend void serialip_appcall(void);
179 friend void uip_log(char* msg);
180};
181
182#endif // RF24CLIENT_H
struct __attribute__((__packed__))
Definition RF24Client.h:53
virtual bool operator==(const EthernetClient &)
size_t write(uint8_t)
int connect(IPAddress ip, uint16_t port)
static uip_userdata_t all_data[UIP_CONNS]
Definition RF24Client.h:162
int waitAvailable(uint32_t timeout=750)
virtual bool operator!=(const EthernetClient &rhs)
Definition RF24Client.h:157
friend void serialip_appcall(void)
int available()
friend void uip_log(char *msg)
uint8_t connected()
#define EthernetClient
#define OUTPUT_BUFFER_SIZE
Definition uip-conf.h:153
#define UIP_SOCKET_NUMPACKETS
Definition uip-conf.h:175