STM32Ethernet
Arduino Ethernet library for STM32 boards
Loading...
Searching...
No Matches
EthernetUdp.h
Go to the documentation of this file.
1
8/*
9 * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
10 * This version only offers minimal wrapping of socket.c/socket.h
11 * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/
12 *
13 * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
14 * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
15 * might not happen often in practice, but in larger network topologies, a UDP
16 * packet can be received out of sequence.
17 * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
18 * aware of it. Again, this may not be a concern in practice on small local networks.
19 * For more information, see http://www.cafeaulait.org/course/week12/35.html
20 *
21 * MIT License:
22 * Copyright (c) 2008 Bjoern Hartmann
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 *
41 * bjoern@cs.stanford.edu 12/30/2008
42 */
43
44#ifndef ethernetudp_h
45#define ethernetudp_h
46
47#include <Udp.h>
48#include <functional>
49
50#include "utility/stm32_eth.h"
51
52#define UDP_TX_PACKET_MAX_SIZE 24
53
54class EthernetUDP : public UDP {
55 private:
56 uint16_t _port; // local port to listen on
57 IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
58 uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
59 IPAddress _sendtoIP; // the remote IP address set by beginPacket
60 uint16_t _sendtoPort; // the remote port set by beginPacket
61
62 struct pbuf *_data; //pbuf for data to send
63 struct udp_struct _udp; //udp settings
64
65 protected:
66 uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
67
68 public:
83 virtual uint8_t begin(uint16_t);
100 virtual uint8_t begin(IPAddress, uint16_t, bool multicast = false);
110 virtual uint8_t beginMulticast(IPAddress, uint16_t);
116 virtual void stop();
117
118 // Sending UDP packets
119
130 virtual int beginPacket(IPAddress ip, uint16_t port);
143 virtual int beginPacket(const char *host, uint16_t port);
152 virtual int endPacket();
159 virtual size_t write(uint8_t);
170 virtual size_t write(const uint8_t *buffer, size_t size);
171
172 using Print::write;
173
183 virtual int parsePacket();
191 virtual int available();
197 virtual int read();
207 virtual int read(unsigned char *buffer, size_t len);
217 virtual int read(char *buffer, size_t len)
218 {
219 return read((unsigned char *)buffer, len);
220 };
226 virtual int peek();
232 virtual void flush();
233
241 virtual IPAddress remoteIP()
242 {
243 return _remoteIP;
244 };
252 virtual uint16_t remotePort()
253 {
254 return _remotePort;
255 };
263 virtual void onDataArrival(std::function<void()> onDataArrival_fn);
264};
265
266#endif
Definition EthernetUdp.h:54
EthernetUDP()
Constructs an EthernetUDP object without opening a socket.
Definition EthernetUdp.cpp:43
virtual int read()
Reads one byte from the current packet.
Definition EthernetUdp.cpp:200
virtual int available()
Returns the number of unread bytes in the current packet.
Definition EthernetUdp.cpp:96
virtual void onDataArrival(std::function< void()> onDataArrival_fn)
Registers a callback invoked when a UDP datagram arrives.
virtual int beginPacket(IPAddress ip, uint16_t port)
Sets the destination for the next outgoing packet.
Definition EthernetUdp.cpp:128
virtual int parsePacket()
Advances to the next received packet.
Definition EthernetUdp.cpp:177
virtual uint16_t remotePort()
Returns the port of the sender of the current packet.
Definition EthernetUdp.h:252
virtual int endPacket()
Sends the data written since beginPacket() as one datagram.
Definition EthernetUdp.cpp:143
virtual IPAddress remoteIP()
Returns the IP address of the sender of the current packet.
Definition EthernetUdp.h:241
virtual int peek()
Returns the next byte of the current packet without consuming it.
Definition EthernetUdp.cpp:248
virtual void stop()
Closes the UDP socket.
Definition EthernetUdp.cpp:101
virtual uint8_t beginMulticast(IPAddress, uint16_t)
Opens a UDP socket that joins a multicast group.
Definition EthernetUdp.cpp:266
uint16_t _remaining
Definition EthernetUdp.h:66
virtual uint8_t begin(uint16_t)
Opens a unicast UDP socket on a local port.
Definition EthernetUdp.cpp:48
virtual void flush()
Intended to wait for the transmit buffer to empty.
Definition EthernetUdp.cpp:261
virtual int read(char *buffer, size_t len)
Reads up to len characters from the current packet into buffer.
Definition EthernetUdp.h:217
virtual size_t write(uint8_t)
Appends one byte to the outgoing packet.
Definition EthernetUdp.cpp:161
Include stm32_eth source files.
Definition stm32_eth.h:68