-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPClient.cpp
372 lines (332 loc) · 10.1 KB
/
UDPClient.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* @file UDPClient.cpp
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* 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 <https: //www.gnu.org/licenses/>.
*
*/
#include "UDPClient.h"
#include "IPAddress.h"
#include "esp_netif.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "esp_log.h"
static const char *TAG = "UDPClient";
UDPClient::UDPClient() {}
UDPClient::~UDPClient() { stop(); }
// bool UDPClient::begin(const char *url) {
// if (!strstr(url, "://")) {
// return false;
// }
// int state = 0;
// bool hostmode = true;
// memset((void *)_hostname, 0, sizeof(_hostname));
// memset((void *)_cPort, 0, sizeof(_cPort));
// memset((void *)_protocol, 0, sizeof(_protocol));
// for (int i = 0, j = 0, k = 0, l = 0; i < strlen(url); i++) {
// switch (url[i]) {
// case ':':
// if (state == 0) {
// // first occurrence of :
// state++;
// protocolToPort();
// } else {
// // second occurrence of :
// hostmode = false;
// }
// break;
// case '/':
// if (state > 0) {
// // after initial protocol
// state++;
// }
// break;
// default:
// if (state == 0 && l < sizeof(_protocol) - 1) {
// // protocol part
// _protocol[l] = url[i];
// } else if (state == 3) {
// // host and port part
// if (hostmode && j < sizeof(_hostname) - 1) {
// _hostname[j++] = url[i];
// } else if (!hostmode && k < sizeof(_cPort) - 1) {
// _cPort[k++] = url[i];
// }
// } else {
// // uri part
// }
// break;
// }
// }
// if (resolve(_hostname, _cPort, _ip)) {
// return open(_ip, atoi(_cPort));
// }
// return false;
// }
bool UDPClient::begin(IPAddress addr, uint16_t port) {
// struct sockaddr_in6 dest_addr = {0};
_sock = lwip_socket(_addr_family, SOCK_DGRAM, _ip_protocol);
if (_sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", _sock);
return false;
}
ESP_LOGD(TAG, "Socket created");
int yes = 1;
if (lwip_setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
ESP_LOGD(TAG, "could not set socket option: %d", errno);
stop();
return false;
}
_remoteIp = addr.toAddr();
_serverPort = port;
struct sockaddr_in *_src_addr =
(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
if (addr.type() == ESP_IPADDR_TYPE_V4) {
_src_addr->sin_addr.s_addr = htonl(addr.toUInt());
_src_addr->sin_family = AF_INET;
_src_addr->sin_port = htons(_serverPort);
_addr_family = AF_INET;
_ip_protocol = IPPROTO_IP;
} /* else {
inet6_aton(addr.ipAddress.u_addr.ip6.addr, &dest_addr.sin6_addr);
dest_addr.sin6_family = AF_INET6;
dest_addr.sin6_port = htons(port);
dest_addr.sin6_scope_id =
esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); addr_family = AF_INET6;
ip_protocol = IPPROTO_IPV6;
} */
if (lwip_bind(_sock, (struct sockaddr *)_src_addr, sizeof(struct sockaddr)) <
0) {
ESP_LOGD(TAG, "could not bind socket: %d", errno);
stop();
return false;
}
fcntl(_sock, F_SETFL, O_NONBLOCK);
return true;
}
bool UDPClient::begin(uint16_t port) { return begin(ipNull, port); }
bool UDPClient::beginMulticast(IPAddress addr, uint16_t port) {
ESP_LOGD(TAG, "BeginMulticast: %s:%d", addr.toChar(), port);
if (begin(ipNull, port)) {
ESP_LOGD(TAG, "IP=%s", addr.toChar());
if (addr != ipNull) {
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = (in_addr_t)addr.toAddr();
mreq.imr_interface.s_addr = ipNull.toAddr();
if (setsockopt(_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) < 0) {
ESP_LOGD(TAG, "could not join igmp: %d", errno);
stop();
return false;
}
_multicastIp = addr;
}
return true;
}
return false;
}
void UDPClient::stop(void) {
if (_sock != -1) {
if (_multicastIp != ipNull) {
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = _multicastIp.toAddr();
mreq.imr_interface.s_addr = ipNull.toAddr();
setsockopt(_sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
_multicastIp = ipNull;
}
lwip_shutdown(_sock, 0);
lwip_close(_sock);
ESP_LOGD(TAG, "Socket shutdown");
}
}
bool UDPClient::beginMulticastPacket() {
if (!_serverPort || _multicastIp == ipNull) {
return 0;
}
_remoteIp = _multicastIp;
_remotePort = _serverPort;
return beginPacket();
}
bool UDPClient::beginPacket() {
if (!_remotePort) {
return false;
}
_txLen = 0;
if (_sock < 0) {
return true;
}
if ((_sock = lwip_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
ESP_LOGD(TAG, "could not create socket: %d", _sock);
return false;
}
fcntl(_sock, F_SETFL, O_NONBLOCK);
return true;
}
bool UDPClient::beginPacket(IPAddress ip, uint16_t port) {
_remoteIp = ip;
_remotePort = port;
return beginPacket();
}
bool UDPClient::beginPacket(const char *host, uint16_t port) {
struct hostent *server;
server = gethostbyname(host);
if (server == NULL) {
ESP_LOGD(TAG, "could not get host from dns");
return false;
}
return beginPacket(IPAddress((const uint8_t *)(server->h_addr_list[0])),
port);
}
bool UDPClient::endPacket() {
struct sockaddr_in recipient;
recipient.sin_addr.s_addr = _remoteIp.toAddr();
recipient.sin_family = AF_INET;
recipient.sin_port = htons(_remotePort);
int sent = lwip_sendto(_sock, _txBuffer, _txLen, 0,
(struct sockaddr *)&recipient, sizeof(recipient));
if (sent < 0) {
ESP_LOGD(TAG, "could not send data: %d", sent);
return false;
}
return true;
}
size_t UDPClient::write(uint8_t data) {
if (_txLen == 1460) {
endPacket();
_txLen = 0;
}
_txBuffer[_txLen++] = data;
return 1;
}
size_t UDPClient::write(const uint8_t *buffer, size_t size) {
size_t i;
for (i = 0; i < size; i++) write(buffer[i]);
return i;
}
size_t UDPClient::write(const char *buffer, size_t size) {
return write((uint8_t *)buffer, size);
}
int UDPClient::parsePacket() {
struct sockaddr_in si_other;
int slen = sizeof(si_other);
if ((_rxLen = lwip_recvfrom(_sock, _rxBuffer, 1460, MSG_DONTWAIT,
(struct sockaddr *)&si_other,
(socklen_t *)&slen)) < 0) {
if (_rxLen == EWOULDBLOCK) {
return 0;
}
ESP_LOGD(TAG, "could not receive data: %d", _rxLen);
return 0;
}
_remoteIp = IPAddress(si_other.sin_addr.s_addr);
_remotePort = ntohs(si_other.sin_port);
_rxPtr = 0;
return _rxLen;
}
bool UDPClient::available() { return (_rxLen > 0); }
int UDPClient::read() {
if (_rxLen <= 0) {
return -1;
}
int out = _rxBuffer[_rxPtr++];
_rxLen--;
return out;
}
int UDPClient::read(uint8_t *rxBuffer, size_t rxLen) {
size_t len = min(rxLen, _rxLen);
if (len <= 0) return 0;
memcpy((void *)rxBuffer, (void *)_rxBuffer, len);
rxLen = len;
if (len < _rxLen) {
memcpy((void *)_rxBuffer, (void *)(_rxBuffer + (_rxLen - len)),
_rxLen - len);
_rxLen -= len;
}
return rxLen;
}
int UDPClient::read(char *rxBuffer, size_t rxLen) {
return read((uint8_t *)rxBuffer, rxLen);
}
int UDPClient::peek() {
if (_rxLen <= 0) return -1;
return _rxBuffer[_rxPtr];
}
void UDPClient::setTimeout(uint32_t timeout) {
// Set timeout
struct timeval _timeout;
_timeout.tv_sec = timeout / 1000U;
_timeout.tv_usec = (timeout % 1000U) * 1000U;
setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, &_timeout, sizeof _timeout);
}
// int UDPClient::send(uint8_t *txBuffer, size_t txLen) {
// int err =
// lwip_sendto(_sock, txBuffer, txLen, 0, (struct sockaddr *)_dest_addr,
// sizeof(struct sockaddr_in));
// if (err < 0) {
// ESP_LOGE(TAG, "Error occurred during sending: errno %d", err);
// }
// return err;
// }
// int UDPClient::sendAndReceive(uint8_t *txBuffer, size_t txLen,
// uint8_t *rxBuffer, size_t &rxLen,
// uint32_t timeout) {
// int ret = send(txBuffer, txLen, timeout);
// return ret < 0 ? ret : receive(rxBuffer, rxLen, timeout);
// }
// int UDPClient::receive(uint8_t *rxBuffer, size_t &rxLen, uint32_t timeout)
// {
// setTimeout(timeout);
// struct sockaddr_storage source_addr; // Large enough for both IPv4 or
// IPv6 socklen_t socklen = sizeof(source_addr); _rxLen =
// lwip_recvfrom(_sock, _rxBuffer, rxLen, 0,
// (struct sockaddr *)&source_addr, &socklen);
// // Error occurred during receiving
// if (_rxLen < 0) {
// ESP_LOGE(TAG, "recv failed: errno %d", _rxLen);
// return _rxLen;
// } else if (!_rxLen) {
// return 0;
// }
// // Data rxLen
// ESP_LOGI(TAG, "rxLen %d bytes from %s:", _rxLen, _remoteIp.toChar());
// ESP_LOG_BUFFER_HEXDUMP(TAG, _rxBuffer, _rxLen, ESP_LOG_DEBUG);
// return read(rxBuffer, rxLen);
// }
void UDPClient::printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char buffer[1024];
va_end(args);
sprintf(buffer, fmt, args);
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, strlen(buffer), ESP_LOG_DEBUG);
write(buffer, strlen(buffer));
}
void UDPClient::flush() {
_rxPtr = 0;
if (_rxLen <= 0) return;
}
IPAddress UDPClient::remoteIP() { return _remoteIp; }
uint16_t UDPClient::remotePort() { return _remotePort; }