-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino_10BASE_T1S_UDP.cpp
238 lines (194 loc) · 5.55 KB
/
Arduino_10BASE_T1S_UDP.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
/*
* This file is part of the Arduino_10BASE_T1S library.
*
* Copyright (c) 2024 Arduino SA
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "Arduino_10BASE_T1S_UDP.h"
/**************************************************************************************
* MODULE INTERNAL FUNCTION DECLARATION
**************************************************************************************/
static void lwIp_udp_raw_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port);
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
Arduino_10BASE_T1S_UDP::Arduino_10BASE_T1S_UDP()
: _udp_pcb{nullptr}
, _send_to_ip{0,0,0,0}
, _send_to_port{0}
{
}
Arduino_10BASE_T1S_UDP::~Arduino_10BASE_T1S_UDP()
{
stop();
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
uint8_t Arduino_10BASE_T1S_UDP::begin(uint16_t port)
{
/* Create a UDP PCB (if none exists yet). */
if (!_udp_pcb)
_udp_pcb = udp_new();
/* Bind specified port to all local interfaces. */
err_t const err = udp_bind(_udp_pcb, IP_ADDR_ANY, port);
if (err != ERR_OK)
return 0;
/* Set a reception callback to be called upon the arrival of a UDP package. */
udp_recv(_udp_pcb , lwIp_udp_raw_recv, this);
return 1;
}
void Arduino_10BASE_T1S_UDP::stop()
{
if (_udp_pcb != nullptr)
{
udp_disconnect(_udp_pcb);
udp_remove(_udp_pcb);
_udp_pcb = nullptr;
}
}
int Arduino_10BASE_T1S_UDP::beginPacket(IPAddress ip, uint16_t port)
{
if (_udp_pcb == nullptr)
return 0;
_send_to_ip = ip;
_send_to_port = port;
/* Make sure that the transmit data buffer is empty. */
_tx_data.clear();
return 1;
}
int Arduino_10BASE_T1S_UDP::beginPacket(const char *host, uint16_t port)
{
/* TODO */
return 0;
}
int Arduino_10BASE_T1S_UDP::endPacket()
{
if (_udp_pcb == nullptr)
return 0;
/* Convert to IP address required for LWIP. */
ip_addr_t ipaddr;
IP_ADDR4(&ipaddr, _send_to_ip[0], _send_to_ip[1], _send_to_ip[2], _send_to_ip[3]);
/* Allocate pbuf structure. */
struct pbuf * p = pbuf_alloc(PBUF_TRANSPORT, _tx_data.size(), PBUF_RAM);
if (!p)
return 0;
/* Copy data from transmit buffer over. */
err_t err = pbuf_take(p, _tx_data.data(), _tx_data.size());
if (err != ERR_OK)
return -1;
/* Empty our transmit buffer. */
_tx_data.clear();
/* Send UDP packet. */
err = udp_sendto(_udp_pcb, p, &ipaddr, _send_to_port);
if (err != ERR_OK)
return -1;
/* Free pbuf */
pbuf_free(p);
return 1;
}
size_t Arduino_10BASE_T1S_UDP::write(uint8_t data)
{
_tx_data.push_back(data);
return 1;
}
size_t Arduino_10BASE_T1S_UDP::write(const uint8_t * buffer, size_t size)
{
std::copy(buffer, buffer + size, std::back_inserter(_tx_data));
return size;
}
int Arduino_10BASE_T1S_UDP::parsePacket()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->totalSize();
else
return 0;
}
int Arduino_10BASE_T1S_UDP::available()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->available();
else
return 0;
}
int Arduino_10BASE_T1S_UDP::read()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->read();
else
return -1;
}
int Arduino_10BASE_T1S_UDP::read(unsigned char* buffer, size_t len)
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->read(buffer, len);
else
return -1;
}
int Arduino_10BASE_T1S_UDP::read(char* buffer, size_t len)
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->read(buffer, len);
else
return -1;
}
int Arduino_10BASE_T1S_UDP::peek()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->peek();
else
return -1;
}
void Arduino_10BASE_T1S_UDP::flush()
{
/* Drop packet from receive buffer. */
if (_rx_pkt_list.size())
_rx_pkt_list.pop_front();
}
IPAddress Arduino_10BASE_T1S_UDP::remoteIP()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->remoteIP();
else
return IPAddress();
}
uint16_t Arduino_10BASE_T1S_UDP::remotePort()
{
if (_rx_pkt_list.size())
return _rx_pkt_list.front()->remotePort();
else
return 0;
}
void Arduino_10BASE_T1S_UDP::onUdpRawRecv(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port)
{
/* Obtain remote port and remote IP. */
auto const remote_ip = IPAddress(
ip4_addr1(addr),
ip4_addr2(addr),
ip4_addr3(addr),
ip4_addr4(addr));
auto const remote_port = port;
/* Create UDP object. */
auto const rx_pkt = std::make_shared<UdpRxPacket>(
remote_ip,
remote_port,
(uint8_t const *)p->payload,
p->len);
_rx_pkt_list.push_back(rx_pkt);
/* Free pbuf */
pbuf_free(p);
}
/**************************************************************************************
* LWIP CALLBACKS
**************************************************************************************/
void lwIp_udp_raw_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port)
{
Arduino_10BASE_T1S_UDP * this_ptr = (Arduino_10BASE_T1S_UDP * )arg;
this_ptr->onUdpRawRecv(pcb, p, addr, port);
}