RF24Ethernet - TCP/IP over RF24Network v2.0.3
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
RF24Server.cpp
Go to the documentation of this file.
1/*
2 RF24Server.cpp - Arduino implementation of a uIP wrapper class.
3 Copyright (c) 2014 tmrh20@gmail.com, github.com/TMRh20
4 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
5 All rights reserved.
6
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
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#include "RF24Ethernet.h"
21#include "RF24Server.h"
22
23extern "C" {
24//#include "uip-conf.h"
25}
26
27/*************************************************************/
28#if USE_LWIP < 1
29RF24Server::RF24Server(uint16_t port) : _port(htons(port))
30{
31}
32#else
33uint16_t RF24Server::_port;
34struct tcp_pcb* RF24Server::sPcb;
35EthernetClient::ConnectState* RF24Server::serverState;
36
38{
39 _port = port;
40 // Allocate data for a second server/client
41 RF24Client::incomingData[1] = (char*)malloc(INCOMING_DATA_SIZE);
42}
43
44#endif
45/*************************************************************/
46
48{
49
50 Ethernet.tick();
51#if USE_LWIP < 1
52 for (uip_userdata_t* data = &RF24Client::all_data[0]; data < &RF24Client::all_data[UIP_CONNS]; data++)
53 {
54 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)))
55 {
56 return RF24Client(data);
57 }
58 }
59#else
60 uint32_t data = 1;
61 return RF24Client(data);
62#endif
63 return RF24Client();
64}
65
66/*************************************************************/
67
69{
70#if USE_LWIP < 1
71 uip_listen(_port);
72#else
73
74 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
75 if (Ethernet.useCoreLocking) {
76 ETHERNET_APPLY_LOCK();
77 }
78 #endif
79
80 bool closed = false;
81
82 if (sPcb == nullptr) {
83 sPcb = tcp_new();
84 }
85
86 if (sPcb != nullptr) {
87 tcp_err(sPcb, RF24Client::error_callback);
88 }
89
90 err_t err = tcp_bind(sPcb, IP_ADDR_ANY, RF24Server::_port);
91
92 if (err != ERR_OK) {
93 //Debug print
94 IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: Unable to bind to port"););
95 }
96
97 if (serverState == nullptr) {
98 serverState = new RF24Client::ConnectState;
99 }
100
101 RF24Client::gState[1]->stateActiveID = 1;
102
103 if (serverState != nullptr) {
104 serverState->finished = false;
105 serverState->connected = false;
106 serverState->result = 0;
107 serverState->waiting_for_ack = false;
108 }
109 sPcb = tcp_listen_with_backlog(sPcb, 1);
110
111 if (sPcb != nullptr) {
112 tcp_arg(sPcb, serverState);
113 tcp_accept(sPcb, RF24Client::accept);
114 }
115 else {
116 IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println("Server: Failed to initialize"););
117 }
118
119 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
120 if (Ethernet.useCoreLocking) {
121 ETHERNET_REMOVE_LOCK();
122 }
123 #endif
124
125#endif
126 RF24Ethernet.tick();
127}
128
129/*************************************************************/
130#if defined(ESP32)
131void RF24Server::begin(uint16_t port)
132{
133 _port = port;
134 begin();
135}
136#endif
137
138/*************************************************************/
139
140size_t RF24Server::write(uint8_t c)
141{
142 return write(&c, 1);
143}
144
145/*************************************************************/
146
147size_t RF24Server::write(const uint8_t* buf, size_t size)
148{
149 size_t ret = 0;
150#if USE_LWIP < 1
151 for (uip_userdata_t* data = &RF24Client::all_data[0]; data < &RF24Client::all_data[UIP_CONNS]; data++)
152 {
153 if ((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport == _port)
154 ret += RF24Client::_write(data, buf, size);
155 }
156#else
157 uint8_t data;
158 RF24Client::_write(&data, buf, size);
159#endif
160 return ret;
161}
162
163/*************************************************************/
164
165void RF24Server::setTimeout(uint32_t timeout)
166{
167#if USE_LWIP < 1
168 #if UIP_CONNECTION_TIMEOUT > 0
169 for (uint8_t i = 0; i < UIP_CONNS; i++) {
170 uip_userdata_t* data = &RF24Client::all_data[i];
171 if (data) {
172 data->connectTimeout = timeout;
173 }
174 }
175 #endif
176#else
177 RF24Client::serverConnectionTimeout = timeout;
178#endif
179}
#define INCOMING_DATA_SIZE
Definition RF24Client.h:78
RF24EthernetClass RF24Ethernet
static void error_callback(void *arg, err_t err)
static ConnectState * gState[2]
Definition RF24Client.h:218
size_t write(uint8_t)
void begin()
RF24Client available()
void setTimeout(uint32_t timeout)
#define Ethernet
#define IF_RF24ETHERNET_DEBUG_CLIENT(x)