sanguino/cores/arduino/Udp.h

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

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

mercurial