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