bjoern avatar bjoern committed c7b00cd

added bytewise packet assembly and example

Comments (0)

Files changed (3)

libraries/Ethernet/Udp.cpp

 void UdpClass::begin(uint16_t port) {
 	_port = port;
 	_sock = 0; //TODO: should not be hardcoded
+	_index =0;
 	socket(_sock,Sn_MR_UDP,_port,0);
 }
 
 	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;

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
 #ifndef Udp_h
 #define Udp_h
 
-class UdpClass {
+#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 
 	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;

libraries/Ethernet/examples/UdpSendBytewise/UdpSendBytewise.pde

+#include <Ethernet.h>
+#include <Udp.h>
+/* UdpSendBytewise.pde: Example how to send packets over UDP 
+ * by assembling packets byte-by-byte
+ * to check for received packets on Unix-ish setup, execute:
+ * sudo tcpdump -A -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;
+
+int i=0;
+
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  Udp.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();
+  delay(1000);
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.