UIPEthernet library for Arduino IDE, Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32,Nordic nRF51,Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network chip. Compatible with Wiznet W5100 Ethernet library API. Compiled and tested on Nucleo-F302R8. Master repository is: https://github.com/UIPEthernet/UIPEthernet/
mbed/Udp.h@1:34b5616a463d, 2016-12-27 (annotated)
- Committer:
- cassyarduino
- Date:
- Tue Dec 27 11:47:13 2016 +0100
- Revision:
- 1:34b5616a463d
- Child:
- 19:e416943f7119
changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cassyarduino | 1:34b5616a463d | 1 | /* |
cassyarduino | 1:34b5616a463d | 2 | * Udp.cpp: Library to send/receive UDP packets. |
cassyarduino | 1:34b5616a463d | 3 | * |
cassyarduino | 1:34b5616a463d | 4 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) |
cassyarduino | 1:34b5616a463d | 5 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This |
cassyarduino | 1:34b5616a463d | 6 | * might not happen often in practice, but in larger network topologies, a UDP |
cassyarduino | 1:34b5616a463d | 7 | * packet can be received out of sequence. |
cassyarduino | 1:34b5616a463d | 8 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being |
cassyarduino | 1:34b5616a463d | 9 | * aware of it. Again, this may not be a concern in practice on small local networks. |
cassyarduino | 1:34b5616a463d | 10 | * For more information, see http://www.cafeaulait.org/course/week12/35.html |
cassyarduino | 1:34b5616a463d | 11 | * |
cassyarduino | 1:34b5616a463d | 12 | * MIT License: |
cassyarduino | 1:34b5616a463d | 13 | * Copyright (c) 2008 Bjoern Hartmann |
cassyarduino | 1:34b5616a463d | 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
cassyarduino | 1:34b5616a463d | 15 | * of this software and associated documentation files (the "Software"), to deal |
cassyarduino | 1:34b5616a463d | 16 | * in the Software without restriction, including without limitation the rights |
cassyarduino | 1:34b5616a463d | 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
cassyarduino | 1:34b5616a463d | 18 | * copies of the Software, and to permit persons to whom the Software is |
cassyarduino | 1:34b5616a463d | 19 | * furnished to do so, subject to the following conditions: |
cassyarduino | 1:34b5616a463d | 20 | * |
cassyarduino | 1:34b5616a463d | 21 | * The above copyright notice and this permission notice shall be included in |
cassyarduino | 1:34b5616a463d | 22 | * all copies or substantial portions of the Software. |
cassyarduino | 1:34b5616a463d | 23 | * |
cassyarduino | 1:34b5616a463d | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
cassyarduino | 1:34b5616a463d | 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
cassyarduino | 1:34b5616a463d | 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
cassyarduino | 1:34b5616a463d | 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
cassyarduino | 1:34b5616a463d | 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
cassyarduino | 1:34b5616a463d | 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
cassyarduino | 1:34b5616a463d | 30 | * THE SOFTWARE. |
cassyarduino | 1:34b5616a463d | 31 | * |
cassyarduino | 1:34b5616a463d | 32 | * bjoern@cs.stanford.edu 12/30/2008 |
cassyarduino | 1:34b5616a463d | 33 | */ |
cassyarduino | 1:34b5616a463d | 34 | #ifndef udp_h |
cassyarduino | 1:34b5616a463d | 35 | #define udp_h |
cassyarduino | 1:34b5616a463d | 36 | |
cassyarduino | 1:34b5616a463d | 37 | #include <mbed.h> |
cassyarduino | 1:34b5616a463d | 38 | #include "IPAddress.h" |
cassyarduino | 1:34b5616a463d | 39 | |
cassyarduino | 1:34b5616a463d | 40 | //class UDP : public Stream { |
cassyarduino | 1:34b5616a463d | 41 | class UDP |
cassyarduino | 1:34b5616a463d | 42 | { |
cassyarduino | 1:34b5616a463d | 43 | public: |
cassyarduino | 1:34b5616a463d | 44 | virtual uint8_t begin(uint16_t) = 0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use |
cassyarduino | 1:34b5616a463d | 45 | virtual void stop(void) = 0; // Finish with the UDP socket |
cassyarduino | 1:34b5616a463d | 46 | |
cassyarduino | 1:34b5616a463d | 47 | // Sending UDP packets |
cassyarduino | 1:34b5616a463d | 48 | // Start building up a packet to send to the remote host specific in ip and port |
cassyarduino | 1:34b5616a463d | 49 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port |
cassyarduino | 1:34b5616a463d | 50 | virtual int beginPacket(IPAddress ip, uint16_t port) = 0; |
cassyarduino | 1:34b5616a463d | 51 | |
cassyarduino | 1:34b5616a463d | 52 | // Start building up a packet to send to the remote host specific in host and port |
cassyarduino | 1:34b5616a463d | 53 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port |
cassyarduino | 1:34b5616a463d | 54 | virtual int beginPacket(const char* host, uint16_t port) = 0; |
cassyarduino | 1:34b5616a463d | 55 | |
cassyarduino | 1:34b5616a463d | 56 | // Finish off this packet and send it |
cassyarduino | 1:34b5616a463d | 57 | // Returns 1 if the packet was sent successfully, 0 if there was an error |
cassyarduino | 1:34b5616a463d | 58 | virtual int endPacket(void) = 0; |
cassyarduino | 1:34b5616a463d | 59 | |
cassyarduino | 1:34b5616a463d | 60 | // Write a single byte into the packet |
cassyarduino | 1:34b5616a463d | 61 | virtual size_t write(uint8_t) = 0; |
cassyarduino | 1:34b5616a463d | 62 | |
cassyarduino | 1:34b5616a463d | 63 | // Write size bytes from buffer into the packet |
cassyarduino | 1:34b5616a463d | 64 | virtual size_t write(const uint8_t* buffer, size_t size) = 0; |
cassyarduino | 1:34b5616a463d | 65 | |
cassyarduino | 1:34b5616a463d | 66 | // Start processing the next available incoming packet |
cassyarduino | 1:34b5616a463d | 67 | // Returns the size of the packet in bytes, or 0 if no packets are available |
cassyarduino | 1:34b5616a463d | 68 | virtual int parsePacket(void) = 0; |
cassyarduino | 1:34b5616a463d | 69 | |
cassyarduino | 1:34b5616a463d | 70 | // Number of bytes remaining in the current packet |
cassyarduino | 1:34b5616a463d | 71 | virtual int available(void) = 0; |
cassyarduino | 1:34b5616a463d | 72 | |
cassyarduino | 1:34b5616a463d | 73 | // Read a single byte from the current packet |
cassyarduino | 1:34b5616a463d | 74 | virtual int read(void) = 0; |
cassyarduino | 1:34b5616a463d | 75 | |
cassyarduino | 1:34b5616a463d | 76 | // Read up to len bytes from the current packet and place them into buffer |
cassyarduino | 1:34b5616a463d | 77 | // Returns the number of bytes read, or 0 if none are available |
cassyarduino | 1:34b5616a463d | 78 | virtual int read(unsigned char* buffer, size_t len) = 0; |
cassyarduino | 1:34b5616a463d | 79 | |
cassyarduino | 1:34b5616a463d | 80 | // Read up to len characters from the current packet and place them into buffer |
cassyarduino | 1:34b5616a463d | 81 | // Returns the number of characters read, or 0 if none are available |
cassyarduino | 1:34b5616a463d | 82 | virtual int read(char* buffer, size_t len) = 0; |
cassyarduino | 1:34b5616a463d | 83 | |
cassyarduino | 1:34b5616a463d | 84 | // Return the next byte from the current packet without moving on to the next byte |
cassyarduino | 1:34b5616a463d | 85 | virtual int peek(void) = 0; |
cassyarduino | 1:34b5616a463d | 86 | virtual void flush(void) = 0; // Finish reading the current packet |
cassyarduino | 1:34b5616a463d | 87 | |
cassyarduino | 1:34b5616a463d | 88 | // Return the IP address of the host who sent the current incoming packet |
cassyarduino | 1:34b5616a463d | 89 | virtual IPAddress remoteIP(void) = 0; |
cassyarduino | 1:34b5616a463d | 90 | |
cassyarduino | 1:34b5616a463d | 91 | // Return the port of the host who sent the current incoming packet |
cassyarduino | 1:34b5616a463d | 92 | virtual uint16_t remotePort(void) = 0; |
cassyarduino | 1:34b5616a463d | 93 | protected: |
cassyarduino | 1:34b5616a463d | 94 | uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; |
cassyarduino | 1:34b5616a463d | 95 | }; |
cassyarduino | 1:34b5616a463d | 96 | #endif |