00001 /* 00002 * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 00003 * This version only offers minimal wrapping of socket.c/socket.h 00004 * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 00005 * 00006 * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 00007 * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 00008 * might not happen often in practice, but in larger network topologies, a UDP 00009 * packet can be received out of sequence. 00010 * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 00011 * aware of it. Again, this may not be a concern in practice on small local networks. 00012 * For more information, see http://www.cafeaulait.org/course/week12/35.html 00013 * 00014 * MIT License: 00015 * Copyright (c) 2008 Bjoern Hartmann 00016 * Permission is hereby granted, free of charge, to any person obtaining a copy 00017 * of this software and associated documentation files (the "Software"), to deal 00018 * in the Software without restriction, including without limitation the rights 00019 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00020 * copies of the Software, and to permit persons to whom the Software is 00021 * furnished to do so, subject to the following conditions: 00022 * 00023 * The above copyright notice and this permission notice shall be included in 00024 * all copies or substantial portions of the Software. 00025 * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00027 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00028 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00029 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00030 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00031 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00032 * THE SOFTWARE. 00033 * 00034 * bjoern@cs.stanford.edu 12/30/2008 00035 */ 00036 00037 #ifndef udp_h 00038 #define udp_h 00039 00040 #define UDP_TX_PACKET_MAX_SIZE 24 00041 00042 class UdpClass { 00043 private: 00044 uint8_t _sock; // socket ID for Wiz5100 00045 uint16_t _port; // local port to listen on 00046 00047 public: 00048 void begin(uint16_t); // initialize, start listening on specified port 00049 int available(); // has data been received? 00050 00051 // C-style buffer-oriented functions 00052 uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer 00053 uint16_t sendPacket(const char[], uint8_t *, uint16_t); //send a string as a packet to specified peer 00054 int readPacket(uint8_t *, uint16_t); // read a received packet 00055 int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *); // read a received packet, also return sender's ip and port 00056 // readPacket that fills a character string buffer 00057 int readPacket(char *, uint16_t, uint8_t *, uint16_t &); 00058 00059 }; 00060 00061 extern UdpClass Udp; 00062 00063 #endif