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
+ }
0 commit comments