Repository navigation
Expand file tree
/
Copy pathxdrv_41_tcp_bridge.ino
More file actions
324 lines (284 loc) · 10 KB
/
Copy pathxdrv_41_tcp_bridge.ino
File metadata and controls
324 lines (284 loc) · 10 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
xdrv_41_tcp_bridge.ino - TCP to serial bridge
Copyright (C) 2021 Theo Arends and Stephan Hadinger
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/>.
*/
#ifdef USE_TCP_BRIDGE
#define XDRV_41 41
#ifndef TCP_BRIDGE_CONNECTIONS
#define TCP_BRIDGE_CONNECTIONS 2 // number of maximum parallel connections
#endif
#ifndef TCP_BRIDGE_BUF_SIZE
#define TCP_BRIDGE_BUF_SIZE 255 // size of the buffer, above 132 required for efficient XMODEM
#endif
//const uint16_t tcp_port = 8880;
WiFiServer *server_tcp = nullptr;
//WiFiClient client_tcp1, client_tcp2;
WiFiClient client_tcp[TCP_BRIDGE_CONNECTIONS];
uint8_t client_next = 0;
uint8_t *tcp_buf = nullptr; // data transfer buffer
bool ip_filter_enabled = false;
IPAddress ip_filter;
bool tcp_serial = false;
#include <TasmotaSerial.h>
TasmotaSerial *TCPSerial = nullptr;
const char kTCPCommands[] PROGMEM = "TCP" "|" // prefix
"Start" "|" "Baudrate" "|" "Config" "|" "Connect"
;
void (* const TCPCommand[])(void) PROGMEM = {
&CmndTCPStart, &CmndTCPBaudrate, &CmndTCPConfig, &CmndTCPConnect
};
//
// Called at event loop, checks for incoming data from the CC2530
//
void TCPLoop(void)
{
uint8_t c;
bool busy; // did we transfer some data?
int32_t buf_len;
// check for a new client connection
if ((server_tcp) && (server_tcp->hasClient())) {
WiFiClient new_client = server_tcp->available();
// count already connected ports
uint32_t connected = 0;
for (uint32_t i=0; i<nitems(client_tcp); i++) {
WiFiClient &client = client_tcp[i];
if (client.connected()) {
connected++;
}
}
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Got connection from %s (%i already connected)"), new_client.remoteIP().toString().c_str(), connected);
// Check for IP filtering if it's enabled.
if (ip_filter_enabled) {
if (ip_filter != new_client.remoteIP()) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Rejected due to filtering"));
new_client.stop();
} else {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Allowed through filter"));
}
}
// find an empty slot
uint32_t i;
for (i=0; i<nitems(client_tcp); i++) {
WiFiClient &client = client_tcp[i];
if (!client) {
client = new_client;
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "Adding connection to slot %i"), i);
break;
}
}
if (i >= nitems(client_tcp)) {
i = client_next++ % nitems(client_tcp);
WiFiClient &client = client_tcp[i];
if (client.connected()) {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "Replacing connection in slot %i, closing connection from %s"), i, client.remoteIP().toString().c_str());
client.stop();
}
client = new_client;
}
}
do {
busy = false; // exit loop if no data was transferred
// start reading the UART, this buffer can quickly overflow
buf_len = 0;
while ((buf_len < TCP_BRIDGE_BUF_SIZE) && (TCPSerial->available())) {
c = TCPSerial->read();
if (c >= 0) {
tcp_buf[buf_len++] = c;
busy = true;
}
}
if (buf_len > 0) {
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_TCP "from MCU: %*_H"), buf_len, tcp_buf);
for (uint32_t i=0; i<nitems(client_tcp); i++) {
WiFiClient &client = client_tcp[i];
if (client) { client.write(tcp_buf, buf_len); }
}
}
// handle data received from TCP
for (uint32_t i=0; i<nitems(client_tcp); i++) {
WiFiClient &client = client_tcp[i];
buf_len = 0;
while (client && (buf_len < TCP_BRIDGE_BUF_SIZE) && (client.available())) {
c = client.read();
if (c >= 0) {
tcp_buf[buf_len++] = c;
busy = true;
}
}
if (buf_len > 0) {
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_TCP "to MCU/%d: %*_H"), i+1, buf_len, tcp_buf);
TCPSerial->write(tcp_buf, buf_len);
}
}
yield(); // avoid WDT if heavy traffic
} while (busy);
}
/********************************************************************************************/
void TCPInit(void) {
if (PinUsed(GPIO_TCP_RX) && PinUsed(GPIO_TCP_TX)) {
if (0 == (0x80 & Settings->tcp_config)) { // !0x80 means unitialized
Settings->tcp_config = 0x80 | ParseSerialConfig("8N1"); // default as 8N1 for backward compatibility
}
tcp_buf = (uint8_t*) malloc(TCP_BRIDGE_BUF_SIZE);
if (!tcp_buf) {
AddLog(LOG_LEVEL_ERROR, PSTR(D_LOG_TCP "could not allocate buffer"));
return;
}
if (!Settings->tcp_baudrate) {
Settings->tcp_baudrate = 115200 / 300;
}
// patch for ESP32S3
#if CONFIG_IDF_TARGET_ESP32S3
pinMode(Pin(GPIO_TCP_TX), OUTPUT);
digitalWrite(Pin(GPIO_TCP_TX), HIGH);
sleep(1);
#endif // CONFIG_IDF_TARGET_ESP32S3
TCPSerial = new TasmotaSerial(Pin(GPIO_TCP_RX), Pin(GPIO_TCP_TX), TasmotaGlobal.seriallog_level ? 1 : 2, 0, TCP_BRIDGE_BUF_SIZE); // set a receive buffer of 256 bytes
tcp_serial = TCPSerial->begin(Settings->tcp_baudrate * 300, ConvertSerialConfig(0x7F & Settings->tcp_config));
if (PinUsed(GPIO_TCP_TX_EN)) {
TCPSerial->setTransmitEnablePin(Pin(GPIO_TCP_TX_EN));
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "TCP Bridge EN is used on Pin %d"), Pin(GPIO_TCP_TX_EN));
}
if (tcp_serial) {
if (TCPSerial->hardwareSerial()) {
ClaimSerial();
}
#ifdef ESP32
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "Serial UART%d"), TCPSerial->getUart());
#endif
} else {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "failed init serial"));
}
}
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
//
// Command `TCPStart`
// Params: port,<IPv4 allow>
//
void CmndTCPStart(void) {
int32_t tcp_port = XdrvMailbox.payload;
if (ArgC() == 2) {
char sub_string[XdrvMailbox.data_len];
ip_filter.fromString(ArgV(sub_string, 2));
ip_filter_enabled = true;
} else {
// Disable whitelist if previously set
ip_filter_enabled = false;
}
if (server_tcp) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Stopping TCP server"));
server_tcp->stop();
delete server_tcp;
server_tcp = nullptr;
for (uint32_t i=0; i<nitems(client_tcp); i++) {
WiFiClient &client = client_tcp[i];
client.stop();
}
}
if (tcp_port > 0) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Starting TCP server on port %d"), tcp_port);
if (ip_filter_enabled) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Filtering %s"), ip_filter.toString().c_str());
}
server_tcp = new WiFiServer(tcp_port);
server_tcp->begin(); // start TCP server
server_tcp->setNoDelay(true);
}
ResponseCmndDone();
}
void CmndTCPBaudrate(void) {
if (XdrvMailbox.payload >= 300) {
XdrvMailbox.payload /= 300; // Make it a valid baudrate
if (Settings->tcp_baudrate != XdrvMailbox.payload) {
Settings->tcp_baudrate = XdrvMailbox.payload;
if (!TCPSerial->begin(Settings->tcp_baudrate * 300, ConvertSerialConfig(0x7F & Settings->tcp_config))) {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "failed reinit serial"));
} // Reinitialize serial port with new baud rate
}
}
ResponseCmndNumber(Settings->tcp_baudrate * 300);
}
void CmndTCPConfig(void) {
if (XdrvMailbox.data_len > 0) {
uint8_t serial_config = ParseSerialConfig(XdrvMailbox.data);
if ((serial_config >= 0) && (Settings->tcp_config != (0x80 | serial_config))) {
Settings->tcp_config = 0x80 | serial_config; // default 0x00 should be 8N1
if (!TCPSerial->begin(Settings->tcp_baudrate * 300, ConvertSerialConfig(0x7F & Settings->tcp_config))) {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_TCP "failed reinit serial"));
} // Reinitialize serial port with new config
}
}
ResponseCmndChar_P(GetSerialConfig(0x7F & Settings->tcp_config).c_str());
}
//
// Command `Connect`
// Params: port,<IPv4>
//
void CmndTCPConnect(void) {
int32_t tcp_port = XdrvMailbox.payload;
if (ArgC() == 2) {
char sub_string[XdrvMailbox.data_len];
WiFiClient new_client;
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Connecting to %s on port %d"), ArgV(sub_string, 2),tcp_port);
if (new_client.connect(ArgV(sub_string, 2),tcp_port)) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "connected!"));
} else {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "error connecting!"));
}
// find an empty slot
uint32_t i;
for (i=0; i<nitems(client_tcp); i++) {
WiFiClient &client = client_tcp[i];
if (!client) {
client = new_client;
break;
}
}
if (i >= nitems(client_tcp)) {
i = client_next++ % nitems(client_tcp);
WiFiClient &client = client_tcp[i];
client.stop();
client = new_client;
}
} else {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_TCP "Usage: port,ip_address"));
}
ResponseCmndDone();
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xdrv41(uint32_t function)
{
bool result = false;
if (FUNC_PRE_INIT == function) {
TCPInit();
} else if (tcp_serial) {
switch (function) {
case FUNC_LOOP:
TCPLoop();
break;
case FUNC_COMMAND:
result = DecodeCommand(kTCPCommands, TCPCommand);
break;
case FUNC_ACTIVE:
result = true;
break;
}
}
return result;
}
#endif // USE_TCP_BRIDGE