Skip to content

Commit 3a7b37e

Browse files
committed
Check in current code of RF24Ethernet
- Moving to new structure similar to the standard Arduino Ethernet library (Also using UIPEthernet as a reference) - Currently the library only directly supports incoming connections as a server, with little to no control beyond receiving data from the connection - Can now be used on multiple nodes, but the network address is hard-coded - RF24Network support for Arduino receiving fragmented multicast payloads allows actual ARP protocol handling, and integration with RF24Network (Using RF24 radio mac addresses at the routing/IP layer) - Need to add configuration for the MAC address as the radio address, etc, but wanted to check-in the code
1 parent 65066ec commit 3a7b37e

38 files changed

+9540
-0
lines changed

RF24Client.cpp

+227
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
2+
extern "C"
3+
{
4+
//#import "utility/uip-conf.h"
5+
#import "utility/uip.h"
6+
#import "utility/uip_arp.h"
7+
//#import "string.h"
8+
}
9+
10+
#include "RF24Ethernet.h"
11+
#include "RF24Client.h"
12+
13+
#define UIP_TCP_PHYH_LEN UIP_LLH_LEN+UIP_IPTCPH_LEN
14+
15+
uip_userdata_t RF24Client::all_data[UIP_CONNS];
16+
17+
18+
RF24Client::RF24Client(){}
19+
20+
RF24Client::RF24Client(uint16_t dataSize): _dataLen(dataSize){}
21+
22+
EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) {
23+
}
24+
25+
//RF24Client::RF24Client(uip_userdata_t* conn_data) :
26+
// data(conn_data)
27+
//{
28+
//}
29+
30+
uint8_t RF24Client::connected(){
31+
return 1;//(uip_connected());
32+
}
33+
34+
int RF24Client::available(){
35+
return RF24Ethernet.dataCnt;
36+
}
37+
38+
39+
40+
int RF24Client::connect(IPAddress ip, uint16_t port)
41+
{
42+
//stop();
43+
/*uip_ipaddr_t ipaddr;
44+
uip_ip_addr(ipaddr, ip);
45+
struct uip_conn* conn = uip_connect(&ipaddr, htons(port));
46+
if (conn)
47+
{
48+
#if UIP_CONNECT_TIMEOUT > 0
49+
int32_t timeout = millis() + 1000 * UIP_CONNECT_TIMEOUT;
50+
#endif
51+
while((conn->tcpstateflags & UIP_TS_MASK) != UIP_CLOSED)
52+
{
53+
RF24EthernetClass::tick();
54+
if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED)
55+
{
56+
data = (uip_userdata_t*) conn->appstate;
57+
#ifdef UIPETHERNET_DEBUG_CLIENT
58+
Serial.print(F("connected, state: "));
59+
Serial.print(data->state);
60+
Serial.print(F(", first packet in: "));
61+
Serial.println(data->packets_in[0]);
62+
#endif
63+
return 1;
64+
}
65+
#if UIP_CONNECT_TIMEOUT > 0
66+
if (((int32_t)(millis() - timeout)) > 0)
67+
{
68+
conn->tcpstateflags = UIP_CLOSED;
69+
break;
70+
}
71+
#endif
72+
}
73+
}*/
74+
return 1;
75+
}
76+
77+
int
78+
RF24Client::connect(const char *host, uint16_t port)
79+
{
80+
// Look up the host first
81+
int ret = 1;
82+
/*#if UIP_UDP
83+
DNSClient dns;
84+
IPAddress remote_addr;
85+
86+
dns.begin(UIPEthernetClass::_dnsServerAddress);
87+
ret = dns.getHostByName(host, remote_addr);
88+
if (ret == 1) {
89+
return connect(remote_addr, port);
90+
}
91+
#endif*/
92+
return ret;
93+
}
94+
95+
void
96+
RF24Client::stop()
97+
{
98+
/*if (data && data->state)
99+
{
100+
#ifdef UIPETHERNET_DEBUG_CLIENT
101+
Serial.println(F("before stop(), with data"));
102+
_dumpAllData();
103+
#endif
104+
//_flushBlocks(&data->packets_in[0]);
105+
if (data->state & UIP_CLIENT_REMOTECLOSED)
106+
{
107+
data->state = 0;
108+
}
109+
else
110+
{
111+
data->state |= UIP_CLIENT_CLOSE;
112+
}
113+
#ifdef UIPETHERNET_DEBUG_CLIENT
114+
Serial.println(F("after stop()"));
115+
_dumpAllData();
116+
#endif
117+
}
118+
#ifdef UIPETHERNET_DEBUG_CLIENT
119+
else
120+
{
121+
Serial.println(F("stop(), data: NULL"));
122+
}
123+
#endif
124+
data = NULL;*/
125+
//RF24EthernetClass::tick();
126+
}
127+
/*
128+
uint8_t RF24Client::connected()
129+
{
130+
//return (data && (data->packets_in[0] != NOBLOCK || (data->state & UIP_CLIENT_CONNECTED))) ? 1 : 0;
131+
return 0;
132+
}*/
133+
134+
// the next function allows us to use the client returned by
135+
// EthernetServer::available() as the condition in an if-statement.
136+
bool RF24Client::operator==(const RF24Client& rhs)
137+
{
138+
return RF24Ethernet.dataCnt;//uip_connected();//data && rhs.data && (data == rhs.data);
139+
//return 0;
140+
}
141+
142+
RF24Client::operator bool()
143+
{
144+
//RF24EthernetClass::tick();
145+
return RF24Ethernet.dataCnt;//uip_connected();//data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in[0] != NOBLOCK);
146+
}
147+
148+
size_t RF24Client::write(uint8_t c)
149+
{
150+
//return _write(data, &c, 1);
151+
return 0;
152+
}
153+
154+
size_t RF24Client::write(const uint8_t *buf, size_t size)
155+
{
156+
return 0;//_write(data, buf, size);
157+
}
158+
/*
159+
size_t RF24Client::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
160+
{
161+
return 0;
162+
}*/
163+
164+
/*
165+
int RF24Client::available()
166+
{
167+
if (*this)
168+
return _available(data);
169+
return 0;
170+
}*/
171+
/*
172+
int RF24Client::_available(uip_userdata_t *u)
173+
{
174+
int len = 0;
175+
// for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++)
176+
// {
177+
// len += Enc28J60Network::blockSize(u->packets_in[i]);
178+
// }
179+
// if(UIPEthernet.network.update() == EXTERNAL_DATA_TYPE){
180+
// RF24NetworkFrame *frame = UIPEthernet.network.frag_ptr;
181+
// len = frame->message_size;
182+
// }
183+
184+
// if(UIPEthernet.inPos > 0){
185+
// len=UIPEthernet.inPos;
186+
// }
187+
//len=UIPEthernet.dataSize;
188+
//Serial.print("av len");
189+
//Serial.println(len);
190+
return len;
191+
}
192+
*/
193+
int RF24Client::read(uint8_t *buf, size_t size)
194+
{
195+
}
196+
197+
int
198+
RF24Client::read()
199+
{/*
200+
uint8_t c;
201+
if (read(&c,1) < 0)
202+
return -1;
203+
return c;*/
204+
}
205+
206+
int
207+
RF24Client::peek()
208+
{
209+
if (*this)
210+
{
211+
/*if (data->packets_in[0] != NOBLOCK)
212+
{
213+
uint8_t c;
214+
Enc28J60Network::readPacket(data->packets_in[0],0,&c,1);
215+
return c;
216+
}*/
217+
}
218+
return -1;
219+
}
220+
221+
void RF24Client::flush()
222+
{
223+
if (*this)
224+
{
225+
//_flushBlocks(&data->packets_in[0]);
226+
}
227+
}

RF24Client.h

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
3+
#ifndef RF24CLIENT_H
4+
#define RF24CLIENT_H
5+
6+
7+
#include "Print.h"
8+
#import "Client.h"
9+
10+
#include "ethernet_comp.h"
11+
#include "Print.h"
12+
#import "Client.h"
13+
14+
extern "C" {
15+
#import "utility/uip.h"
16+
}
17+
18+
19+
#define UIP_SOCKET_DATALEN UIP_TCP_MSS
20+
//#define UIP_SOCKET_NUMPACKETS UIP_RECEIVE_WINDOW/UIP_TCP_MSS+1
21+
#ifndef UIP_SOCKET_NUMPACKETS
22+
#define UIP_SOCKET_NUMPACKETS 5
23+
#endif
24+
25+
#define UIP_CLIENT_CONNECTED 0x10
26+
#define UIP_CLIENT_CLOSE 0x20
27+
#define UIP_CLIENT_REMOTECLOSED 0x40
28+
#define UIP_CLIENT_RESTART 0x80
29+
#define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART)
30+
#define UIP_CLIENT_SOCKETS ~UIP_CLIENT_STATEFLAGS
31+
32+
/*
33+
typedef struct {
34+
uint8_t state;
35+
memhandle packets_in[UIP_SOCKET_NUMPACKETS];
36+
memhandle packets_out[UIP_SOCKET_NUMPACKETS];
37+
memaddress out_pos;
38+
#if UIP_CLIENT_TIMER >= 0
39+
unsigned long timer;
40+
#endif
41+
} uip_userdata_t;
42+
*/
43+
typedef struct {
44+
uint8_t state;
45+
uint8_t packets_in[UIP_SOCKET_NUMPACKETS];
46+
uint8_t packets_out[UIP_SOCKET_NUMPACKETS];
47+
uint8_t out_pos;
48+
#if UIP_CLIENT_TIMER >= 0
49+
unsigned long timer;
50+
#endif
51+
} uip_userdata_t;
52+
53+
54+
55+
56+
class RF24Client : public Client {
57+
58+
public:
59+
RF24Client();
60+
//RF24Client(int len);
61+
RF24Client(uint8_t sock);
62+
int connect(IPAddress ip, uint16_t port);
63+
int connect(const char *host, uint16_t port);
64+
int read(uint8_t *buf, size_t size);
65+
void stop();
66+
67+
68+
uint8_t connected();
69+
//int available();
70+
71+
size_t write(uint8_t);
72+
size_t write(const uint8_t *buf, size_t size);
73+
int available();
74+
int read();
75+
int peek();
76+
void flush();
77+
78+
using Print::write;
79+
80+
operator bool();
81+
virtual bool operator==(const EthernetClient&);
82+
virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
83+
84+
85+
private:
86+
uint8_t _sock;
87+
uint16_t _dataLen;
88+
89+
//This is referenced by UIPServer.cpp in available()
90+
//RF24Client(serialip_state* conn_data);
91+
//RF24Client(uint8_t sock);
92+
RF24Client(size_t tmp);
93+
/*RF24Client(struct uip_conn *_conn);
94+
RF24Client(uip_userdata_t* conn_data);*/
95+
96+
uip_userdata_t* data;
97+
static uip_userdata_t all_data[UIP_CONNS];
98+
99+
friend class RF24EthernetClass;
100+
friend class RF24Server;
101+
102+
friend void uipclient_appcall(void);
103+
104+
};
105+
106+
107+
#endif

0 commit comments

Comments
 (0)