[136] | 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 | * - 10 Apr. 2015
|
---|
| 37 | * Added support for Arduino Ethernet Shield 2
|
---|
| 38 | * by Arduino.org team
|
---|
| 39 | *
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | #ifndef ethernetudp_h
|
---|
| 44 | #define ethernetudp_h
|
---|
| 45 |
|
---|
| 46 | #include <Udp.h>
|
---|
| 47 |
|
---|
| 48 | #define UDP_TX_PACKET_MAX_SIZE 24
|
---|
| 49 |
|
---|
| 50 | class EthernetUDP : public UDP {
|
---|
| 51 | private:
|
---|
| 52 | uint8_t _sock; // socket ID for Wiz5100
|
---|
| 53 | uint16_t _port; // local port to listen on
|
---|
| 54 | IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
|
---|
| 55 | uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
|
---|
| 56 | uint16_t _offset; // offset into the packet being sent
|
---|
| 57 | uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
|
---|
| 58 |
|
---|
| 59 | public:
|
---|
| 60 | EthernetUDP(); // Constructor
|
---|
| 61 | 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
|
---|
| 62 | virtual void stop(); // Finish with the UDP socket
|
---|
| 63 |
|
---|
| 64 | // Sending UDP packets
|
---|
| 65 |
|
---|
| 66 | // Start building up a packet to send to the remote host specific in ip and port
|
---|
| 67 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
|
---|
| 68 | virtual int beginPacket(IPAddress ip, uint16_t port);
|
---|
| 69 | // Start building up a packet to send to the remote host specific in host and port
|
---|
| 70 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
|
---|
| 71 | virtual int beginPacket(const char *host, uint16_t port);
|
---|
| 72 | // Finish off this packet and send it
|
---|
| 73 | // Returns 1 if the packet was sent successfully, 0 if there was an error
|
---|
| 74 | virtual int endPacket();
|
---|
| 75 | // Write a single byte into the packet
|
---|
| 76 | virtual size_t write(uint8_t);
|
---|
| 77 | // Write size bytes from buffer into the packet
|
---|
| 78 | virtual size_t write(const uint8_t *buffer, size_t size);
|
---|
| 79 |
|
---|
| 80 | using Print::write;
|
---|
| 81 |
|
---|
| 82 | // Start processing the next available incoming packet
|
---|
| 83 | // Returns the size of the packet in bytes, or 0 if no packets are available
|
---|
| 84 | virtual int parsePacket();
|
---|
| 85 | // Number of bytes remaining in the current packet
|
---|
| 86 | virtual int available();
|
---|
| 87 | // Read a single byte from the current packet
|
---|
| 88 | virtual int read();
|
---|
| 89 | // Read up to len bytes from the current packet and place them into buffer
|
---|
| 90 | // Returns the number of bytes read, or 0 if none are available
|
---|
| 91 | virtual int read(unsigned char* buffer, size_t len);
|
---|
| 92 | // Read up to len characters from the current packet and place them into buffer
|
---|
| 93 | // Returns the number of characters read, or 0 if none are available
|
---|
| 94 | virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
|
---|
| 95 | // Return the next byte from the current packet without moving on to the next byte
|
---|
| 96 | virtual int peek();
|
---|
| 97 | virtual void flush(); // Finish reading the current packet
|
---|
| 98 |
|
---|
| 99 | // Return the IP address of the host who sent the current incoming packet
|
---|
| 100 | virtual IPAddress remoteIP() { return _remoteIP; };
|
---|
| 101 | // Return the port of the host who sent the current incoming packet
|
---|
| 102 | virtual uint16_t remotePort() { return _remotePort; };
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | #endif
|
---|