1 /*
2  *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
3  *  This version only offers minimal wrapping of socket.c/socket.h
4  *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/
5  *
6  * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
7  * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
8  * might not happen often in practice, but in larger network topologies, a UDP
9  * packet can be received out of sequence.
10  * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
11  * aware of it. Again, this may not be a concern in practice on small local networks.
12  * For more information, see http://www.cafeaulait.org/course/week12/35.html
13  *
14  * MIT License:
15  * Copyright (c) 2008 Bjoern Hartmann
16  * Permission is hereby granted, free of charge, to any person obtaining a copy
17  * of this software and associated documentation files (the "Software"), to deal
18  * in the Software without restriction, including without limitation the rights
19  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20  * copies of the Software, and to permit persons to whom the Software is
21  * furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be included in
24  * all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32  * THE SOFTWARE.
33  *
34  * bjoern@cs.stanford.edu 12/30/2008
35  */
36 
37 #ifndef ethernetudp_h
38 #define ethernetudp_h
39 
40 #include <Udp.h>
41 
42 #define UDP_TX_PACKET_MAX_SIZE 24
43 
44 class EthernetUDP : public UDP {
45 private:
46   uint16_t _port; // local port to listen on
47   IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
48   uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
49   uint16_t _offset; // offset into the packet being sent
50 
51 protected:
52   uint8_t _sock;  // socket ID for Wiz5100
53   uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
54 
55 public:
56   EthernetUDP();  // Constructor
57   virtual uint8_t begin(uint16_t);	// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
58   virtual uint8_t beginMulticast(IPAddress, uint16_t);	// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
59   virtual void stop();  // Finish with the UDP socket
60 
61   // Sending UDP packets
62 
63   // Start building up a packet to send to the remote host specific in ip and port
64   // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
65   virtual int beginPacket(IPAddress ip, uint16_t port);
66   // Start building up a packet to send to the remote host specific in host and port
67   // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
68   virtual int beginPacket(const char *host, uint16_t port);
69   // Finish off this packet and send it
70   // Returns 1 if the packet was sent successfully, 0 if there was an error
71   virtual int endPacket();
72   // Write a single byte into the packet
73   virtual size_t write(uint8_t);
74   // Write size bytes from buffer into the packet
75   virtual size_t write(const uint8_t *buffer, size_t size);
76 
77   using Print::write;
78 
79   // Start processing the next available incoming packet
80   // Returns the size of the packet in bytes, or 0 if no packets are available
81   virtual int parsePacket();
82   // Number of bytes remaining in the current packet
83   virtual int available();
84   // Read a single byte from the current packet
85   virtual int read();
86   // Read up to len bytes from the current packet and place them into buffer
87   // Returns the number of bytes read, or 0 if none are available
88   virtual int read(unsigned char* buffer, size_t len);
89   // Read up to len characters from the current packet and place them into buffer
90   // Returns the number of characters read, or 0 if none are available
read(char * buffer,size_t len)91   virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
92   // Return the next byte from the current packet without moving on to the next byte
93   virtual int peek();
94   virtual void flush();	// Finish reading the current packet
95 
96   // Return the IP address of the host who sent the current incoming packet
remoteIP()97   virtual IPAddress remoteIP() { return _remoteIP; };
98   // Return the port of the host who sent the current incoming packet
remotePort()99   virtual uint16_t remotePort() { return _remotePort; };
100 };
101 
102 #endif
103