Commits

bjoern  committed f8a4a16

Split Udp library into 3 proposals (raw,string,bytewise), adjusted examples accordingly

  • Participants
  • Parent commits c7b00cd

Comments (0)

Files changed (16)

File arduino_osc_udp.pde

 #include <Ethernet.h>
-#include <Udp.h>
+#include <UdpRaw.h>
 
 
 /**************************************************************
   } 
   
   Ethernet.begin(mac,ip,gw);
-  Udp.begin(localPort);
+  UdpRaw.begin(localPort);
   //DEBUG: Serial.begin(9600);
 }
 
 	}
 
   //if there's data available, read a packet
-  if(Udp.available()) {
-    oscRxMsgSize = Udp.readPacket(rcvBuffer,MAX_LENGTH,rcvIp,(uint16_t *)&rcvPort);
+  if(UdpRaw.available()) {
+    oscRxMsgSize = UdpRaw.readPacket(rcvBuffer,MAX_LENGTH,rcvIp,(uint16_t *)&rcvPort);
     if(oscRxMsgSize <= MAX_LENGTH) {
       oscHandleRxPacket();
     } else {
   oscBuffer[offset++]=*(((unsigned char *)(&value))+0);
 
   //send message as one packet
-  Udp.sendPacket((byte *)oscBuffer,offset,targetIp,targetPort);
+  UdpRaw.sendPacket((byte *)oscBuffer,offset,targetIp,targetPort);
 
 }
 

File libraries/Ethernet/Udp.cpp

-/*
- *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
- *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
- *
- * MIT License:
- * Copyright (c) 2008 Bjoern Hartmann
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * bjoern@cs.stanford.edu 12/29/2008
- */
-
-extern "C" {
-#include "types.h"
-#include "w5100.h"
-#include "socket.h"
-}
-
-#include "Ethernet.h"
-#include "Udp.h"
-
-/* Start UDP socket, listening at local port PORT */
-void UdpClass::begin(uint16_t port) {
-	_port = port;
-	_sock = 0; //TODO: should not be hardcoded
-	_index =0;
-	socket(_sock,Sn_MR_UDP,_port,0);
-}
-
-
-/* Send packet contained in buf of length len to peer at specified ip, and port */
-/* Use this function to transmit binary data that might contain 0x00 bytes*/
-/* This function returns sent data size for success else -1. */
-uint16_t UdpClass::sendPacket(uint8_t * buf, uint16_t len,  uint8_t * ip, uint16_t port){
-	return sendto(_sock,(const uint8_t *)buf,len,ip,port);
-}
-
-/* Send  zero-terminated string str as packet to peer at specified ip, and port */
-/* This function returns sent data size for success else -1. */
-uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){	
-	// compute strlen
-	const char *s;
-	for(s = str; *s; ++s);
-	uint16_t len = (s-str);
-	// send packet
-	return sendto(_sock,(const uint8_t *)str,len,ip,port);
-}
-/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes. */
-int UdpClass::available() {
-	return getSn_RX_RSR(_sock);
-}
-
-
-/* Read a received packet into buffer buf (whis is of maximum length len); */
-/* store calling ip and port as well. Call available() to make sure data is ready first. */
-/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
-/*       so it's easy to overflow buf. */
-uint16_t UdpClass::readPacket(uint8_t * buf, uint16_t len, uint8_t *ip, uint16_t *port) {
-	return recvfrom(_sock,buf,len,ip,port);
-}
-
-/* Read a received packet, throw away peer's ip and port.  See note above. */
-uint16_t UdpClass::readPacket(uint8_t * buf, uint16_t len) {
-	uint8_t ip[4];
-	uint16_t port[1];
-	return recvfrom(_sock,buf,len,ip,port);
-}
-
-
-
-uint8_t UdpClass::beginPacket(uint8_t *ip, uint16_t port) { // returns 1 on success, 0 if we already started a packet
-	if(_index==0) {
-		_remoteIp[0]=ip[0];
-		_remoteIp[1]=ip[1];
-		_remoteIp[2]=ip[2];
-		_remoteIp[3]=ip[3];
-		_remotePort = port;
-		return 1;
-	}
-	else {
-		//we already started a packet
-		return 0;
-	}
-}
-
-
-
-// TODO: how do we indicate that we can't add to full buffer?
-// or do we just send a full packet and start the next?
-void UdpClass::write(uint8_t b) {// add a byte to the currently assembled packet if there is space
-	if(_index>= UDP_TX_PACKET_MAX_SIZE)
-		return;		
-	_buffer[_index++] = b;
-}
-
-uint16_t UdpClass::endPacket(){ // returns # of bytes sent on success, 0 if there's nothing to send
-	// send the packet
-	uint16_t result = sendPacket(_buffer,_index,_remoteIp,_remotePort);
-	// reset buffer index
-	_index=0;
-	// return sent bytes
-	return result;
-}
-
-/* Create one global object */
-UdpClass Udp;

File libraries/Ethernet/Udp.h

-/*
- *  Udp.h: Library to send/receive UDP packets with the Arduino ethernet shield.
- *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
- *
- * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
- * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
- * might not happen often in practice, but in larger network topologies, a UDP
- * packet can be received out of sequence. 
- * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
- * aware of it. Again, this may not be a concern in practice on small local networks.
- * For more information, see http://www.cafeaulait.org/course/week12/35.html
- *
- * MIT License:
- * Copyright (c) 2008 Bjoern Hartmann
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * bjoern@cs.stanford.edu 12/29/2008
- */
-
-#ifndef Udp_h
-#define Udp_h
-
-#include "Print.h"
-#define UDP_TX_PACKET_MAX_SIZE 24
-
-class UdpClass : public Print {
-private:
-	uint8_t _sock;  // socket ID for Wiz5100
-	uint16_t _port; // local port to listen on
-	uint8_t _buffer[UDP_TX_PACKET_MAX_SIZE];
-	uint8_t _index;
-	uint8_t _remoteIp[4];
-	uint16_t _remotePort;
-public:
-	void begin(uint16_t);				// initialize, start listening on specified port
-	uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer 
-	uint16_t sendPacket(const char[], uint8_t *, uint16_t);  //send a string as a packet to specified peer
-	int available();								// has data been received?
-	uint16_t readPacket(uint8_t *, uint16_t);		// read a received packet 
-	uint16_t readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet, also return sender's ip and port 
-	
-	//byte-oriented functions:
-	uint8_t beginPacket(uint8_t *, uint16_t); // returns 1 on success, 0 if we already started a packet
-	virtual void write(uint8_t); // add a byte to the currently assembled packet (if there's space)
-	uint16_t endPacket(); // returns # of bytes sent on success, 0 if there's nothing to send
-	
-};
-
-extern UdpClass Udp;
-
-#endif

File libraries/Ethernet/UdpBytewise.cpp

+/*
+ *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+extern "C" {
+#include "types.h"
+#include "w5100.h"
+#include "socket.h"
+}
+
+#include "Ethernet.h"
+#include "UdpBytewise.h"
+
+/* Start UDP socket, listening at local port PORT */
+void UdpBytewiseClass::begin(uint16_t port) {
+	_port = port;
+	_sock = 0; //TODO: should not be hardcoded
+	_index =0;
+	socket(_sock,Sn_MR_UDP,_port,0);
+}
+
+
+/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes. */
+int UdpBytewiseClass::available() {
+	return getSn_RX_RSR(_sock);
+}
+
+
+
+int UdpBytewiseClass::beginPacket(uint8_t *ip, unsigned int port) { // returns 1 on success, 0 if we already started a packet
+	if(_index==0) {
+		_remoteIp[0]=ip[0];
+		_remoteIp[1]=ip[1];
+		_remoteIp[2]=ip[2];
+		_remoteIp[3]=ip[3];
+		_remotePort = port;
+		return 1;
+	}
+	else {
+		//we already started a packet
+		return 0;
+	}
+}
+
+
+
+// TODO: how do we indicate that we can't add to full buffer?
+// or do we just send a full packet and start the next?
+void UdpBytewiseClass::write(uint8_t b) {// add a byte to the currently assembled packet if there is space
+	if(_index>= UDP_TX_PACKET_MAX_SIZE)
+		return;		
+	_buffer[_index++] = b;
+}
+
+int UdpBytewiseClass::endPacket(){ // returns # of bytes sent on success, 0 if there's nothing to send
+	// send the packet
+	uint16_t result = sendto(_sock,(const uint8_t *)_buffer,_index,_remoteIp,_remotePort);
+	// reset buffer index
+	_index=0;
+	// return sent bytes
+	return (int)result;
+}
+
+
+
+/* Create one global object */
+UdpBytewiseClass UdpBytewise;

File libraries/Ethernet/UdpBytewise.h

+/*
+ *  Udp.h: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
+ *
+ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
+ * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
+ * might not happen often in practice, but in larger network topologies, a UDP
+ * packet can be received out of sequence. 
+ * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
+ * aware of it. Again, this may not be a concern in practice on small local networks.
+ * For more information, see http://www.cafeaulait.org/course/week12/35.html
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+#ifndef UdpBytewise_h
+#define UdpBytewise_h
+
+#include "Print.h"
+
+#define UDP_TX_PACKET_MAX_SIZE 24
+
+class UdpBytewiseClass: public Print {
+private:
+	uint8_t _sock;  // socket ID for Wiz5100
+	uint16_t _port; // local port to listen on
+	uint8_t _buffer[UDP_TX_PACKET_MAX_SIZE];
+	uint8_t _index;
+	uint8_t _remoteIp[4];
+	uint16_t _remotePort;
+public:
+	void begin(uint16_t);				// initialize, start listening on specified port
+	int available();								// has data been received?
+
+	// Single byte-oriented functions:
+	int beginPacket(uint8_t *ip, unsigned int port); // returns 1 on success, 0 if we already started a packet
+	virtual void write(uint8_t); // add a byte to the currently assembled packet (if there's space)
+	int endPacket(); // returns # of bytes sent on success, 0 if there's nothing to send
+	
+};
+
+extern UdpBytewiseClass UdpBytewise;
+#endif

File libraries/Ethernet/UdpRaw.cpp

+/*
+ *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+extern "C" {
+#include "types.h"
+#include "w5100.h"
+#include "socket.h"
+}
+
+#include "Ethernet.h"
+#include "UdpRaw.h"
+
+/* Start UDP socket, listening at local port PORT */
+void UdpRawClass::begin(uint16_t port) {
+	_port = port;
+	_sock = 0; //TODO: should not be hardcoded
+	socket(_sock,Sn_MR_UDP,_port,0);
+}
+
+/* Send packet contained in buf of length len to peer at specified ip, and port */
+/* Use this function to transmit binary data that might contain 0x00 bytes*/
+/* This function returns sent data size for success else -1. */
+uint16_t UdpRawClass::sendPacket(uint8_t * buf, uint16_t len,  uint8_t * ip, uint16_t port){
+	return sendto(_sock,(const uint8_t *)buf,len,ip,port);
+}
+
+/* Send  zero-terminated string str as packet to peer at specified ip, and port */
+/* This function returns sent data size for success else -1. */
+uint16_t UdpRawClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){	
+	// compute strlen
+	const char *s;
+	for(s = str; *s; ++s);
+	uint16_t len = (s-str);
+	// send packet
+	return sendto(_sock,(const uint8_t *)str,len,ip,port);
+}
+/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes. */
+int UdpRawClass::available() {
+	return getSn_RX_RSR(_sock);
+}
+
+
+/* Read a received packet into buffer buf (whis is of maximum length len); */
+/* store calling ip and port as well. Call available() to make sure data is ready first. */
+/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
+/*       so it's easy to overflow buf. */
+uint16_t UdpRawClass::readPacket(uint8_t * buf, uint16_t len, uint8_t *ip, uint16_t *port) {
+	return recvfrom(_sock,buf,len,ip,port);
+}
+
+/* Read a received packet, throw away peer's ip and port.  See note above. */
+uint16_t UdpRawClass::readPacket(uint8_t * buf, uint16_t len) {
+	uint8_t ip[4];
+	uint16_t port[1];
+	return recvfrom(_sock,buf,len,ip,port);
+}
+
+
+
+
+/* Create one global object */
+UdpRawClass UdpRaw;

File libraries/Ethernet/UdpRaw.h

+/*
+ *  Udp.h: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
+ *
+ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
+ * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
+ * might not happen often in practice, but in larger network topologies, a UDP
+ * packet can be received out of sequence. 
+ * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
+ * aware of it. Again, this may not be a concern in practice on small local networks.
+ * For more information, see http://www.cafeaulait.org/course/week12/35.html
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+#ifndef UdpRaw_h
+#define UdpRaw_h
+
+#define UDP_TX_PACKET_MAX_SIZE 24
+
+class UdpRawClass {
+private:
+	uint8_t _sock;  // socket ID for Wiz5100
+	uint16_t _port; // local port to listen on
+
+public:
+	void begin(uint16_t);				// initialize, start listening on specified port
+	int available();								// has data been received?
+
+	// C-style buffer-oriented functions
+	uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer 
+	uint16_t sendPacket(const char[], uint8_t *, uint16_t);  //send a string as a packet to specified peer
+	uint16_t readPacket(uint8_t *, uint16_t);		// read a received packet 
+	uint16_t readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet, also return sender's ip and port 	
+};
+
+extern UdpRawClass UdpRaw;
+
+#endif

File libraries/Ethernet/UdpString.cpp

+/*
+ *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+extern "C" {
+#include "types.h"
+#include "w5100.h"
+#include "socket.h"
+}
+
+#include "Ethernet.h"
+#include "UdpString.h"
+
+/* Start UDP socket, listening at local port PORT */
+void UdpStringClass::begin(uint16_t port) {
+	_port = port;
+	_sock = 0; //TODO: should not be hardcoded
+	socket(_sock,Sn_MR_UDP,_port,0);
+}
+
+/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes. */
+int UdpStringClass::available() {
+	return getSn_RX_RSR(_sock);
+}
+
+/////////////////////////////////////////
+//Wstring functions
+int UdpStringClass::sendPacket(String str, byte * ip, unsigned int port) {
+	return (int)sendto(_sock,(const uint8_t *)str.cstr(),str.length(),ip,port);
+}
+
+int UdpStringClass::sendPacket(String str, int length, byte * ip, unsigned int port) {
+	return (int)sendto(_sock,(const uint8_t *)str.cstr(),length,ip,port);
+}
+
+int UdpStringClass::readPacket(String &str) {
+	uint8_t ip[4];
+	uint16_t port[1];
+	return readPacket(str,ip,port);
+}
+
+int UdpStringClass::readPacket(String &str, byte * ip, unsigned int *port) {
+	if(available() > str.capacity()) {
+		return 0; //can't read - string too small - this is BAD because we didn't drain the buffer
+	}
+	return (int)recvfrom(_sock,(byte *)(str.cstr()),(uint16_t)str.capacity(),ip,port);
+}
+
+/* Create one global object */
+UdpStringClass UdpString;

File libraries/Ethernet/UdpString.h

+/*
+ *  Udp.h: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
+ *
+ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
+ * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
+ * might not happen often in practice, but in larger network topologies, a UDP
+ * packet can be received out of sequence. 
+ * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
+ * aware of it. Again, this may not be a concern in practice on small local networks.
+ * For more information, see http://www.cafeaulait.org/course/week12/35.html
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+#ifndef UdpString_h
+#define UdpString_h
+
+#include "WString.h"
+
+
+
+class UdpStringClass {
+private:
+	uint8_t _sock;  // socket ID for Wiz5100
+	uint16_t _port; // local port to listen on
+public:
+	void begin(uint16_t);				// initialize, start listening on specified port
+	int available();								// has data been received?
+
+	// C-style buffer-oriented functions
+	uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer 
+	uint16_t sendPacket(const char[], uint8_t *, uint16_t);  //send a string as a packet to specified peer
+	uint16_t readPacket(uint8_t *, uint16_t);		// read a received packet 
+	uint16_t readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet, also return sender's ip and port 
+	
+	// WString functions
+	int sendPacket(String str, byte * ip, unsigned int port);
+	int sendPacket(String str, int length, byte * ip, unsigned int port);
+	int readPacket(String &str);
+	int readPacket(String &str, byte * ip, unsigned int * port);
+};
+
+extern UdpStringClass UdpString;
+
+#endif

File libraries/Ethernet/examples/UdpReceive/UdpReceive.pde

-#include <Ethernet.h>
-#include <Udp.h>
-
-/* UdpReceive.pde: Example how to receive packets over UDP 
- * prints received packet to serial port
- * bjoern@cs.stanford.edu 12/29/2008
- */
-
-/* ETHERNET SHIELD CONFIGURATION  
- * set MAC, IP address of Ethernet shield, its gateway,
- * and local port to listen on for incoming packets 
- */
-byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address to use
-byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
-byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
-int localPort = 8888; // local port to listen on
-
-#define MAX_SIZE 32 // maximum packet size
-byte packetBuffer[MAX_SIZE]; //buffer to hold incoming packet
-int packetSize; // holds received packet size
-byte remoteIp[4]; // holds recvieved packet's originating IP
-unsigned int remotePort; // holds received packet's originating port
-
-int i;
-
-/* SETUP: init Ethernet shield, start UDP listening, open serial port */
-void setup() {
-  Ethernet.begin(mac,ip,gw);
-  Udp.begin(localPort);
-  Serial.begin(9600); 
-}
-/* LOOP: wait for incoming packets and print each packet to the serial port */
-void loop() {  
-  
-  // if there's data available, read a packet
-  if(Udp.available()) {
-    packetSize = Udp.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);
-    if(packetSize <= MAX_SIZE) {
-      
-      Serial.print("Received packet of size ");
-      Serial.println(packetSize);
-      
-      Serial.print("From IP ");
-      for(i=0; i<3; i++) {
-        Serial.print(remoteIp[i],DEC);
-        Serial.print(".");
-      }
-      Serial.print(remoteIp[3],DEC);
-      
-      Serial.print(" Port ");
-      Serial.println(remotePort); 
-      
-      Serial.println("Contents:");
-      for(i=0; i<packetSize; i++) {
-        Serial.print(packetBuffer[i],BYTE);
-      }
-      Serial.println("");
-      
-    } else {
-      // PANIC - packet too long!
-      // we've already clobbered mem past our buffer boundary
-    }
-  }
-  //wait a bit
-  delay(10);  
-}

File libraries/Ethernet/examples/UdpReceiveRaw/UdpReceiveRaw.pde

+#include <Ethernet.h>
+#include <UdpRaw.h>
+
+/* UdpReceive.pde: Example how to receive packets over UDP using UdpRaw library
+ * prints received packet to serial port
+ * bjoern@cs.stanford.edu 12/30/2008
+ */
+
+/* ETHERNET SHIELD CONFIGURATION  
+ * set MAC, IP address of Ethernet shield, its gateway,
+ * and local port to listen on for incoming packets 
+ */
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address to use
+byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
+byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; // local port to listen on
+
+#define MAX_SIZE 32 // maximum packet size
+byte packetBuffer[MAX_SIZE]; //buffer to hold incoming packet
+int packetSize; // holds received packet size
+byte remoteIp[4]; // holds recvieved packet's originating IP
+unsigned int remotePort; // holds received packet's originating port
+
+int i;
+
+/* SETUP: init Ethernet shield, start UDP listening, open serial port */
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  UdpRaw.begin(localPort);
+  Serial.begin(9600); 
+}
+/* LOOP: wait for incoming packets and print each packet to the serial port */
+void loop() {  
+  
+  // if there's data available, read a packet
+  if(UdpRaw.available()) {
+    packetSize = UdpRaw.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);
+    if(packetSize <= MAX_SIZE) {
+      
+      Serial.print("Received packet of size ");
+      Serial.println(packetSize);
+      
+      Serial.print("From IP ");
+      for(i=0; i<3; i++) {
+        Serial.print(remoteIp[i],DEC);
+        Serial.print(".");
+      }
+      Serial.print(remoteIp[3],DEC);
+      
+      Serial.print(" Port ");
+      Serial.println(remotePort); 
+      
+      Serial.println("Contents:");
+      for(i=0; i<packetSize; i++) {
+        Serial.print(packetBuffer[i],BYTE);
+      }
+      Serial.println("");
+      
+    } else {
+      // PANIC - packet too long!
+      // we've already clobbered mem past our buffer boundary
+    }
+  }
+  //wait a bit
+  delay(10);  
+}

File libraries/Ethernet/examples/UdpReceiveString/UdpReceiveString.pde

+#include <Ethernet.h>
+#include <UdpString.h>
+#include <WString.h>
+/* UdpReceiveString.pde: Example how to receive packets over UDP using the UdpString library
+ * prints received packet to serial port
+ * bjoern@cs.stanford.edu 12/30/2008
+ */
+
+/* ETHERNET SHIELD CONFIGURATION  
+ * set MAC, IP address of Ethernet shield, its gateway,
+ * and local port to listen on for incoming packets 
+ */
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address to use
+byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
+byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; // local port to listen on
+
+String packet(32); //packet can be max 32 bytes long
+byte remoteIp[4]; // holds recvieved packet's originating IP
+unsigned int remotePort[1]; // holds received packet's originating port
+
+int i;
+
+/* SETUP: init Ethernet shield, start UDP listening, open serial port */
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  UdpString.begin(localPort);
+  Serial.begin(9600); 
+}
+
+/* LOOP: wait for incoming packets and print each packet to the serial port */
+void loop() {  
+
+  // if there's data available, read a packet
+  if(UdpString.available()) {
+    int packetSize = UdpString.readPacket(packet,remoteIp,remotePort);
+
+    Serial.print("Received packet of size ");
+    Serial.println(packetSize);
+
+    Serial.print("From IP ");
+    for(i=0; i<3; i++) {
+      Serial.print(remoteIp[i],DEC);
+      Serial.print(".");
+    }
+    Serial.print(remoteIp[3],DEC);
+
+    Serial.print(" Port ");
+    Serial.println(remotePort[0]); 
+
+    Serial.println("Contents:");
+    Serial.println(packet);
+
+
+  }
+  //wait a bit
+  delay(10);  
+}

File libraries/Ethernet/examples/UdpSend/UdpSend.pde

-#include <Ethernet.h>
-#include <Udp.h>
-/* UdpSend.pde: Example how to send packets over UDP 
- * to check for received packets on Unix-ish setup, execute:
- * sudo tcpdump -ien0 "udp port 8000"
- * bjoern@cs.stanford.edu 12/29/2008 */
-
-/* ETHERNET CONFIGURATION 
- * ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
- * and local port to listen on for incoming packets */
-byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
-byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
-byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
-int localPort = 8888; //local port to listen on
-
-/* TARGET: set this to IP/Port of computer that will receive
- * UDP messages from Arduino */
-byte targetIp[] = { 192, 168, 11, 15};
-int targetPort = 8000;
-
-/* A binary packet we'll send to our target - can contain 0x00 */
-byte packet[] = { 'h','e','l','l','o','\0','w','o','r','l','d','\0' };
-int packetLen = 12;
-
-
-void setup() {
-  Ethernet.begin(mac,ip,gw);
-  Udp.begin(localPort);
-}
-
-void loop() {
-  // this version of sendPacket sends a zero-terminated string.
-  Udp.sendPacket("hello, world.",targetIp,targetPort);
-  delay(1000);
-  
-  // this version sends an arbitrary buffer with specified length;
-  // buffer can contain '\0'
-  Udp.sendPacket(packet,packetLen,targetIp,targetPort);
-  delay(1000);
-}

File libraries/Ethernet/examples/UdpSendBytewise/UdpSendBytewise.pde

 #include <Ethernet.h>
-#include <Udp.h>
-/* UdpSendBytewise.pde: Example how to send packets over UDP 
+#include <UdpBytewise.h>
+/* UdpSendBytewise.pde: Example how to send packets over UDP using the UdpBytewise library
  * by assembling packets byte-by-byte
  * to check for received packets on Unix-ish setup, execute:
  * sudo tcpdump -A -ien0 "udp port 8000"
 
 void setup() {
   Ethernet.begin(mac,ip,gw);
-  Udp.begin(localPort);
+  UdpBytewise.begin(localPort);
 }
 
 void loop() {
   // this version of sendPacket sends a zero-terminated string.
-  Udp.beginPacket(targetIp,targetPort);
-  Udp.print("Hello, World! ");
-  Udp.print(i++);
-  Udp.endPacket();
+  UdpBytewise.beginPacket(targetIp,targetPort);
+  UdpBytewise.print("Hello, World! ");
+  UdpBytewise.print(i++);
+  UdpBytewise.endPacket();
   delay(1000);
 }

File libraries/Ethernet/examples/UdpSendRaw/UdpSendRaw.pde

+#include <Ethernet.h>
+#include <UdpRaw.h>
+/* UdpSendRaw.pde: Example how to send packets over UDP using the UdpRaw library 
+ * to check for received packets on Unix-ish setup, execute:
+ * sudo tcpdump -ien0 "udp port 8000"
+ * bjoern@cs.stanford.edu 12/30/2008 */
+
+/* ETHERNET CONFIGURATION 
+ * ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
+ * and local port to listen on for incoming packets */
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
+byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
+byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; //local port to listen on
+
+/* TARGET: set this to IP/Port of computer that will receive
+ * UDP messages from Arduino */
+byte targetIp[] = { 192, 168, 11, 15};
+int targetPort = 8000;
+
+/* A binary packet we'll send to our target - can contain 0x00 */
+byte packet[] = { 'h','e','l','l','o','\0','w','o','r','l','d','\0' };
+int packetLen = 12;
+
+
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  UdpRaw.begin(localPort);
+}
+
+void loop() {
+  // this version of sendPacket sends a zero-terminated string.
+  UdpRaw.sendPacket("hello, world.",targetIp,targetPort);
+  delay(1000);
+  
+  // this version sends an arbitrary buffer with specified length;
+  // buffer can contain '\0'
+  UdpRaw.sendPacket(packet,packetLen,targetIp,targetPort);
+  delay(1000);
+}

File libraries/Ethernet/examples/UdpSendString/UdpSendString.pde

+#include <Ethernet.h>
+#include <UdpString.h>
+#include <WString.h>
+
+/* UdpSendString.pde: Example how to send packets over UDP using the String library 
+ * to check for received packets on Unix-ish setup, execute:
+ * sudo tcpdump -A -ien0 "udp port 8000"
+ * bjoern@cs.stanford.edu 12/30/2008 */
+
+/* ETHERNET CONFIGURATION 
+ * ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
+ * and local port to listen on for incoming packets */
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
+byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
+byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; //local port to listen on
+
+/* TARGET: set this to IP/Port of computer that will receive
+ * UDP messages from Arduino */
+byte targetIp[] = { 192, 168, 11, 15};
+int targetPort = 8000;
+
+/* Strings hold the packets we want to send */
+String asciiString;
+String binaryString(4); // if we want to send non-ASCII, we have to provide a capacity for the String
+
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  UdpString.begin(localPort);
+  
+  asciiString = "Hello, World";
+  
+  binaryString.getBytes()[0]=0x00;
+  binaryString.getBytes()[1]=0x01;
+  binaryString.getBytes()[2]=0x02;
+  binaryString.getBytes()[3]=0x03;
+}
+
+void loop() {
+  // send a normal, zero-terminated string.
+  UdpString.sendPacket(asciiString,targetIp,targetPort);
+  delay(1000);
+  
+  // sends a binary string that can contain 0x00 in the middle
+  // you have to specify the length;
+  UdpString.sendPacket(binaryString,binaryString.capacity(),targetIp,targetPort);
+  delay(1000);
+}