Fork for fixes

Committer:
hudakz
Date:
Mon Sep 15 11:12:30 2014 +0000
Revision:
0:5350a66d5279
rev. 00

Who changed what in which revision?

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