RF24Ethernet - TCP/IP over RF24Network v1.6.15
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
RF24Client.h
Go to the documentation of this file.
1
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
54{
55 bool hold;
56 bool sent;
57 bool packets_in;
58 bool packets_out;
59 bool windowOpened;
60 uint8_t state;
61 uint16_t out_pos;
62 uint16_t dataPos;
63 uint16_t dataCnt;
64#if UIP_CLIENT_TIMER >= 0
65 uint32_t timer;
66#endif
67 uint32_t restartTime;
68 uint32_t restartInterval;
69#if UIP_CONNECTION_TIMEOUT > 0
70 uint32_t connectTimeout;
71 uint32_t connectTimer;
72#endif
73 uint8_t myData[OUTPUT_BUFFER_SIZE];
74} uip_userdata_t;
75
76class RF24Client : public Client
77{
78
79public:
80 /** Basic constructor */
81 RF24Client();
82
83 /** Establish a connection to a specified IP address and port */
84 int connect(IPAddress ip, uint16_t port);
85
86 /**
87 * Establish a connection to a given hostname and port
88 * @note UDP must be enabled in uip-conf.h for DNS lookups to work
89 *
90 * @note Tip: DNS lookups generally require a buffer size of 250-300 bytes or greater.
91 * Lookups will generally return responses with a single A record if using hostnames like
92 * "www.google.com" instead of "google.com" which works well with the default buffer size
93 */
94 int connect(const char* host, uint16_t port);
95
96 /**
97 * Read available data into a buffer
98 * @code
99 * uint8_t buf[size];
100 * client.read(buf,size);
101 * @endcode
102 */
103 int read(uint8_t* buf, size_t size);
104
105 /**
106 * Read data one byte at a time
107 * @code
108 * char c = client.read();
109 * @endcode
110 */
111 int read();
112
113 /** Disconnects from the current active connection */
114 void stop();
115
116 /** Indicates whether the client is connected or not */
117 uint8_t connected();
118
119 /**
120 * Write a single byte of data to the stream
121 * @note This will write an entire TCP payload with only 1 byte in it
122 */
123 size_t write(uint8_t);
124
125 /** Write a buffer of data, to be sent in a single TCP packet */
126 size_t write(const uint8_t* buf, size_t size);
127
128 /**
129 * Indicates whether data is available to be read by the client.
130 * @return Returns the number of bytes available to be read
131 * @note Calling client or server available() keeps the IP stack and RF24Network layer running, so needs to be called regularly,
132 * even when disconnected or delaying for extended periods.
133 */
134 int available();
135
136 /**
137 * Wait Available
138 *
139 * Helps to ensure all incoming data has been received, prior to writing data back to the client, etc.
140 *
141 * Indicates whether data is available to be read by the client, after waiting a maximum period of time.
142 * @return Returns the number of bytes available to be read or 0 if timed out
143 * @note Calling client or server available() keeps the IP stack and RF24Network layer running, so needs to be called regularly,
144 * even when disconnected or delaying for extended periods.
145 */
146 int waitAvailable(uint32_t timeout = 750);
147
148 /** Read a byte from the incoming buffer without advancing the point of reading */
149 int peek();
150
151 /** Flush all incoming client data from the current connection/buffer */
152 void flush();
153
154 using Print::write;
155
156 operator bool();
157 virtual bool operator==(const EthernetClient&);
158 virtual bool operator!=(const EthernetClient& rhs)
159 {
160 return !this->operator==(rhs);
161 };
162
163 static uip_userdata_t all_data[UIP_CONNS];
164
165private:
166 RF24Client(struct uip_conn* _conn);
167 RF24Client(uip_userdata_t* conn_data);
168
169 uip_userdata_t* data;
170
171 static int _available(uip_userdata_t*);
172
173 static uip_userdata_t* _allocateData();
174 static size_t _write(uip_userdata_t*, const uint8_t* buf, size_t size);
175
176 friend class RF24EthernetClass;
177 friend class RF24Server;
178
179 friend void serialip_appcall(void);
180 friend void uip_log(char* msg);
181};
182
183#endif // RF24CLIENT_H
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:163
int waitAvailable(uint32_t timeout=750)
virtual bool operator!=(const EthernetClient &rhs)
Definition RF24Client.h:158
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