Repository navigation
Expand file tree
/
Copy pathRF24Server.cpp
More file actions
293 lines (241 loc) · 8.01 KB
/
Copy pathRF24Server.cpp
File metadata and controls
293 lines (241 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
RF24Server.cpp - Arduino implementation of a uIP wrapper class.
Copyright (c) 2014 tmrh20@gmail.com, github.com/TMRh20
Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RF24Ethernet.h"
#include "RF24Server.h"
extern "C" {
//#include "uip-conf.h"
}
/*************************************************************/
#if USE_LWIP < 1
RF24Server::RF24Server(uint16_t port) : _port(htons(port))
{
}
#elif USE_LWIP == 1
uint16_t RF24Server::_port;
struct tcp_pcb* RF24Server::sPcb;
EthernetClient::ConnectState* RF24Server::serverState;
RF24Server::RF24Server(uint16_t port)
{
_port = port;
// Allocate data for a second server/client
RF24Client::incomingData[1] = (char*)malloc(INCOMING_DATA_SIZE);
}
#elif USE_LWIP == 2
#include <zephyr/kernel.h>
#include <zephyr/net/socket.h>
#include <zephyr/logging/log.h>
#include <zephyr/posix/fcntl.h>
#include <errno.h>
LOG_MODULE_REGISTER(tcp_nonblock_listener, LOG_LEVEL_INF);
#define BACKLOG 1
uint16_t RF24Server::_port;
bool RF24Server::serverListening;
bool RF24Server::connectionActive;
int RF24Server::serverSocket = -1;
RF24Client RF24Server::serverClient;
uint32_t RF24Server::timeoutTimer;
RF24Server::RF24Server(uint16_t port)
{
_port = port;
}
#endif
/*************************************************************/
RF24Client RF24Server::available()
{
Ethernet.tick();
#if USE_LWIP < 1
for (uip_userdata_t* data = &RF24Client::all_data[0]; data < &RF24Client::all_data[UIP_CONNS]; data++)
{
if (data->packets_in != 0 && (((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport == _port) || ((data->state & UIP_CLIENT_REMOTECLOSED) && ((uip_userdata_closed_t*)data)->lport == _port)))
{
return RF24Client(data);
}
}
#elif USE_LWIP == 1
uint32_t data = 1;
return RF24Client(data);
#elif USE_LWIP == 2
uint32_t data = 1;
if (millis() - timeoutTimer > RF24Client::serverConnectionTimeout) {
connectionActive = false;
if (RF24Client::_socket >= 0) {
zsock_close(RF24Client::_socket);
RF24Client::_socket = -1;
}
timeoutTimer = millis();
return RF24Client(data);
}
if (!Ethernet.isInitialized || !serverListening || connectionActive || RF24Client::g_rf24client_instance->_socket > 0) {
if (RF24Client(data)) {
timeoutTimer = millis();
}
return RF24Client(data);
}
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// Check once without blocking
int client_sock = zsock_accept(serverSocket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_sock < 0) {
// Check if the "error" is just the expected non-blocking silence
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Absolutely normal. No one is connecting right now.
if (RF24Client(data)) {
timeoutTimer = millis();
}
return RF24Client(data);
}
// This is a REAL error (e.g., socket closed, invalid descriptor, stack crash)
printk("CRITICAL Accept error: %d\n", errno);
if (RF24Client(data)) {
timeoutTimer = millis();
}
return RF24Client(data);
}
// Success! A client has successfully connected.
printk("Connection accepted (FD: %d)\n", client_sock);
connectionActive = true;
serverClient._socket = client_sock;
RF24Client::g_rf24client_instance = &serverClient;
timeoutTimer = millis();
return serverClient;
#endif
return RF24Client();
}
/*************************************************************/
void RF24Server::begin()
{
#if USE_LWIP < 1
uip_listen(_port);
#elif USE_LWIP == 1
#if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
if (Ethernet.useCoreLocking) {
ETHERNET_APPLY_LOCK();
}
#endif
bool closed = false;
if (sPcb == nullptr) {
sPcb = tcp_new();
}
if (sPcb != nullptr) {
tcp_err(sPcb, RF24Client::error_callback);
}
err_t err = tcp_bind(sPcb, IP_ADDR_ANY, RF24Server::_port);
if (err != ERR_OK) {
//Debug print
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: Unable to bind to port"););
}
if (serverState == nullptr) {
serverState = new RF24Client::ConnectState;
}
RF24Client::gState[1]->stateActiveID = 1;
if (serverState != nullptr) {
serverState->connected = false;
serverState->result = 0;
}
sPcb = tcp_listen_with_backlog(sPcb, 1);
if (sPcb != nullptr) {
tcp_arg(sPcb, serverState);
tcp_accept(sPcb, RF24Client::accept);
}
else {
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: Failed to initialize"););
}
#if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
if (Ethernet.useCoreLocking) {
ETHERNET_REMOVE_LOCK();
}
#endif
#elif USE_LWIP == 2
struct sockaddr_in bind_addr;
serverSocket = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket < 0) {
printk("Socket creation failed: %d", errno);
return;
}
// Bind to port
memset(&bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(_port);
bind_addr.sin_addr.s_addr = INADDR_ANY;
if (zsock_bind(serverSocket, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) < 0) {
printk("Bind failed: %d", errno);
zsock_close(serverSocket);
return;
}
// Start listening FIRST while the socket is still in default blocking state
if (zsock_listen(serverSocket, BACKLOG) < 0) {
printk("Listen failed: %d", errno);
zsock_close(serverSocket);
return;
}
// CRITICAL MOVE: Set to non-blocking mode ONLY AFTER listening is active
int flags = zsock_fcntl(serverSocket, F_GETFL, 0);
if (flags < 0 || zsock_fcntl(serverSocket, F_SETFL, flags | O_NONBLOCK) < 0) {
printk("Failed to set non-blocking flag: %d", errno);
zsock_close(serverSocket);
return;
}
serverListening = true;
printk("Server initialized successfully!");
#endif
RF24Ethernet.tick();
}
/*************************************************************/
#if defined(ESP32)
void RF24Server::begin(uint16_t port)
{
_port = port;
begin();
}
#endif
/*************************************************************/
size_t RF24Server::write(uint8_t c)
{
return write(&c, 1);
}
/*************************************************************/
size_t RF24Server::write(const uint8_t* buf, size_t size)
{
size_t ret = 0;
#if USE_LWIP < 1
for (uip_userdata_t* data = &RF24Client::all_data[0]; data < &RF24Client::all_data[UIP_CONNS]; data++)
{
if ((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport == _port)
ret += RF24Client::_write(data, buf, size);
}
#else
uint8_t data;
RF24Client::_write(&data, buf, size);
#endif
return ret;
}
/*************************************************************/
void RF24Server::setTimeout(uint32_t timeout)
{
#if USE_LWIP < 1
#if UIP_CONNECTION_TIMEOUT > 0
for (uint8_t i = 0; i < UIP_CONNS; i++) {
uip_userdata_t* data = &RF24Client::all_data[i];
if (data) {
data->connectTimeout = timeout;
}
}
#endif
#else
RF24Client::serverConnectionTimeout = timeout;
#endif
}