-
Notifications
You must be signed in to change notification settings - Fork 50
/
RF24Server.cpp
97 lines (80 loc) · 2.78 KB
/
RF24Server.cpp
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
/*
RF24Server.cpp - Arduino implementation of a uIP wrapper class.
Copyright (c) 2014 [email protected], github.com/TMRh20
Copyright (c) 2013 Norbert Truchsess <[email protected]>
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"
}
/*************************************************************/
RF24Server::RF24Server(uint16_t port) : _port(htons(port))
{
}
/*************************************************************/
RF24Client RF24Server::available()
{
Ethernet.tick();
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);
}
}
return RF24Client();
}
/*************************************************************/
void RF24Server::begin()
{
uip_listen(_port);
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;
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);
}
return ret;
}
/*************************************************************/
void RF24Server::setTimeout(uint32_t timeout)
{
#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
}