mirror of
https://github.com/KevinMidboe/Arduino.git
synced 2026-01-30 12:56:00 +00:00
Init commit with many years of arduino sketches and projects. I dont know if the esp8266 includes much, but there are also libraries. I hope they dont have crazy automatic versioning through the Arduino IDE.
This commit is contained in:
7
Projects/libraries/Installed_libs/Ethernet/README-ETHERNET.txt
Executable file
7
Projects/libraries/Installed_libs/Ethernet/README-ETHERNET.txt
Executable file
@@ -0,0 +1,7 @@
|
||||
These files augment the Ethernet library as distributed with Arduino0012, which in turn appears to include
|
||||
application sample code from WIZnet. Eventually, we'll have to modify the underlying socket code of that library to fix buffer overflow risks and to better conserve precious Arduino RAM.
|
||||
|
||||
For more information, look at:
|
||||
http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ
|
||||
http://trac.mlalonde.net/cral/browser/branches/follower/wiz810mj
|
||||
12/31/08
|
||||
156
Projects/libraries/Installed_libs/Ethernet/UdpBytewise.cpp
Executable file
156
Projects/libraries/Installed_libs/Ethernet/UdpBytewise.cpp
Executable file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* UdpBytewise.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
||||
* Drop UdpBytewise.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/
|
||||
* TODO: should protect buffer access with critical sections
|
||||
*
|
||||
* 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/30/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
|
||||
_txIndex =0;
|
||||
_rxIndex =0;
|
||||
_rxSize = 0;
|
||||
_txOverflowStrategy = UDP_TX_OVERFLOW_SPLIT;
|
||||
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() {
|
||||
if(_rxSize==0 || _rxSize-_rxIndex==0) {
|
||||
//if local buffer is empty or depleted
|
||||
//check wiz5100 buffer for new packet
|
||||
_rxSize = getSn_RX_RSR(_sock); //note: return value is inflated by 8 byte header
|
||||
if(_rxSize){
|
||||
//if we have a new packet there
|
||||
//reset buffer index
|
||||
_rxIndex=0;
|
||||
//copy packet into our local buffer
|
||||
_rxSize = recvfrom(_sock,_rxBuffer,_rxSize-8,_rxIp,&_rxPort);
|
||||
} else {
|
||||
//else do nothing and rxsize is still 0
|
||||
;
|
||||
}
|
||||
return _rxSize; //return the new number of bytes in our buffer
|
||||
} else{
|
||||
//if buffer is not empty, return remaining # of bytes
|
||||
return (_rxSize-_rxIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Start a new packet with given target ip and port
|
||||
* returns 1 on success, 0 if we already started a packet */
|
||||
int UdpBytewiseClass::beginPacket(uint8_t *ip, unsigned int port) {
|
||||
if(_txIndex==0) {
|
||||
//ok to start new packet - copy ip and port
|
||||
_txIp[0]=ip[0];
|
||||
_txIp[1]=ip[1];
|
||||
_txIp[2]=ip[2];
|
||||
_txIp[3]=ip[3];
|
||||
_txPort = port;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
//we already started a packet and have data in it
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Add a byte to the currently assembled packet if there is space
|
||||
* if there isn't space, either truncate (ignore) or split the packet.
|
||||
*/
|
||||
void UdpBytewiseClass::write(uint8_t b) {
|
||||
if(_txIndex>= UDP_TX_PACKET_MAX_SIZE) {
|
||||
//buffer is full - we can either truncate the packet or split in two
|
||||
switch (_txOverflowStrategy) {
|
||||
case UDP_TX_OVERFLOW_SPLIT:
|
||||
endPacket();
|
||||
beginPacket(_txIp,_txPort);
|
||||
//fall through to normal add of byte to buffer below
|
||||
break;
|
||||
case UDP_TX_OVERFLOW_TRUNCATE:
|
||||
default:
|
||||
//don't add - just ignore bytes past buffer size
|
||||
return;
|
||||
}
|
||||
}
|
||||
_txBuffer[_txIndex++] = b;
|
||||
}
|
||||
|
||||
/* send an assembled packet out
|
||||
* returns # of bytes sent on success, 0 if there's nothing to send */
|
||||
int UdpBytewiseClass::endPacket() {
|
||||
// send the packet
|
||||
uint16_t result = sendto(_sock,(const uint8_t *)_txBuffer,_txIndex,_txIp,_txPort);
|
||||
// reset buffer index
|
||||
_txIndex=0;
|
||||
// return sent bytes
|
||||
return (int)result;
|
||||
}
|
||||
|
||||
/* read the next byte of the last rececived packet */
|
||||
int UdpBytewiseClass::read() {
|
||||
if(_rxIndex < _rxSize) {
|
||||
// if there is something to be read, return the next byte
|
||||
return _rxBuffer[_rxIndex++];
|
||||
} else {
|
||||
//we already sent the last byte - nothing to do
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void UdpBytewiseClass::getSenderIp(uint8_t*ip) {
|
||||
ip[0]=_rxIp[0];
|
||||
ip[1]=_rxIp[1];
|
||||
ip[2]=_rxIp[2];
|
||||
ip[3]=_rxIp[3];
|
||||
}
|
||||
|
||||
unsigned int UdpBytewiseClass::getSenderPort() {
|
||||
return _rxPort;
|
||||
}
|
||||
|
||||
/* what should we do when we try to add to a full outgoing packet?
|
||||
* UDP_TX_OVERFLOW_TRUNCATE - throw overflow bytes away
|
||||
* UDP_TX_OVERFLOW_SPLIT - split into multiple packets
|
||||
*/
|
||||
void UdpBytewiseClass::setOverflowStrategy(uint8_t strategy) {
|
||||
_txOverflowStrategy = strategy;
|
||||
}
|
||||
|
||||
/* Create one global object */
|
||||
UdpBytewiseClass UdpBytewise;
|
||||
81
Projects/libraries/Installed_libs/Ethernet/UdpBytewise.h
Executable file
81
Projects/libraries/Installed_libs/Ethernet/UdpBytewise.h
Executable file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* UdpBytewise.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
||||
* Drop UdpBytewise.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/30/2008
|
||||
*/
|
||||
|
||||
#ifndef UdpBytewise_h
|
||||
#define UdpBytewise_h
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
#define UDP_TX_PACKET_MAX_SIZE 32
|
||||
#define UDP_RX_PACKET_MAX_SIZE 32
|
||||
|
||||
#define UDP_TX_OVERFLOW_TRUNCATE 0
|
||||
#define UDP_TX_OVERFLOW_SPLIT 1
|
||||
|
||||
class UdpBytewiseClass: public Print {
|
||||
private:
|
||||
uint8_t _sock; // socket ID for Wiz5100
|
||||
uint16_t _port; // local port to listen on
|
||||
|
||||
uint8_t _txBuffer[UDP_TX_PACKET_MAX_SIZE];
|
||||
uint8_t _txIndex;
|
||||
uint8_t _txIp[4];
|
||||
uint16_t _txPort;
|
||||
uint8_t _txOverflowStrategy;
|
||||
|
||||
uint8_t _rxBuffer[UDP_RX_PACKET_MAX_SIZE];
|
||||
uint8_t _rxIndex;
|
||||
int _rxSize;
|
||||
uint8_t _rxIp[4];
|
||||
uint16_t _rxPort;
|
||||
|
||||
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
|
||||
|
||||
int read(); //read a byte if available - returns -1 if no data available
|
||||
|
||||
void getSenderIp(uint8_t * ip); //get remote IP of the packet we're currently reading from
|
||||
unsigned int getSenderPort(); //get remote port# of the packet we're currently reading from
|
||||
void setOverflowStrategy(uint8_t); // what to do when our packet is full and we try to add more?
|
||||
};
|
||||
|
||||
extern UdpBytewiseClass UdpBytewise;
|
||||
#endif
|
||||
133
Projects/libraries/Installed_libs/Ethernet/UdpRaw.cpp
Executable file
133
Projects/libraries/Installed_libs/Ethernet/UdpRaw.cpp
Executable file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* UdpRaw.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
||||
* This version only offers minimal wrapping of socket.c/socket.h
|
||||
* Drop UdpRaw.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/30/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.
|
||||
* returned value includes 8 byte UDP header!*/
|
||||
int UdpRawClass::available() {
|
||||
return getSn_RX_RSR(_sock);
|
||||
}
|
||||
|
||||
|
||||
/* Read a received packet into buffer buf (which 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 buffer. so we check and truncate. */
|
||||
/* returns number of bytes read, or negative number of bytes we would have needed if we truncated */
|
||||
int UdpRawClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
|
||||
int packetLen = available()-8; //skip UDP header;
|
||||
if(packetLen < 0 ) return 0; // no real data here
|
||||
if(packetLen > (int)bufLen) {
|
||||
//packet is too large - truncate
|
||||
//HACK - hand-parse the UDP packet using TCP recv method
|
||||
uint8_t tmpBuf[8];
|
||||
int i;
|
||||
//read 8 header bytes and get IP and port from it
|
||||
recv(_sock,tmpBuf,8);
|
||||
ip[0] = tmpBuf[0];
|
||||
ip[1] = tmpBuf[1];
|
||||
ip[2] = tmpBuf[2];
|
||||
ip[3] = tmpBuf[3];
|
||||
*port = tmpBuf[4];
|
||||
*port = (*port << 8) + tmpBuf[5];
|
||||
|
||||
//now copy first (bufLen) bytes into buf
|
||||
for(i=0;i<(int)bufLen;i++) {
|
||||
recv(_sock,tmpBuf,1);
|
||||
buf[i]=tmpBuf[0];
|
||||
}
|
||||
|
||||
//and just read the rest byte by byte and throw it away
|
||||
while(available()) {
|
||||
recv(_sock,tmpBuf,1);
|
||||
}
|
||||
|
||||
return (-1*packetLen);
|
||||
|
||||
//ALTERNATIVE: requires stdlib - takes a bunch of space
|
||||
/*//create new buffer and read everything into it
|
||||
uint8_t * tmpBuf = (uint8_t *)malloc(packetLen);
|
||||
recvfrom(_sock,tmpBuf,packetLen,ip,port);
|
||||
if(!tmpBuf) return 0; //couldn't allocate
|
||||
// copy first bufLen bytes
|
||||
for(unsigned int i=0; i<bufLen; i++) {
|
||||
buf[i]=tmpBuf[i];
|
||||
}
|
||||
//free temp buffer
|
||||
free(tmpBuf);
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
return recvfrom(_sock,buf,bufLen,ip,port);
|
||||
}
|
||||
|
||||
/* Read a received packet, throw away peer's ip and port. See note above. */
|
||||
int 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;
|
||||
60
Projects/libraries/Installed_libs/Ethernet/UdpRaw.h
Executable file
60
Projects/libraries/Installed_libs/Ethernet/UdpRaw.h
Executable file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* UdpRaw.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
||||
* This version only offers minimal wrapping of socket.c/socket.h
|
||||
* Drop UdpRaw.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/30/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
|
||||
int readPacket(uint8_t *, uint16_t); // read a received packet
|
||||
int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *); // read a received packet, also return sender's ip and port
|
||||
};
|
||||
|
||||
extern UdpRawClass UdpRaw;
|
||||
|
||||
#endif
|
||||
83
Projects/libraries/Installed_libs/Ethernet/UdpString.cpp
Executable file
83
Projects/libraries/Installed_libs/Ethernet/UdpString.cpp
Executable file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* UdpString.cpp: Library to send/receive UDP packets with the Arduino ethernet shield
|
||||
* using Tom Igoe/Hernando Barragan's WString library.
|
||||
* Drop UdpString.h/.cpp from this distribution into the Ethernet library directory at
|
||||
* hardware/libraries/Ethernet/
|
||||
* Then copy directory /libraries/String from this distribution to /hardware/libraries/String in
|
||||
* your Arduino installation.
|
||||
*
|
||||
* 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/30/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);
|
||||
}
|
||||
|
||||
/* read packet into String str - if str is too short, expand its capacity */
|
||||
int UdpStringClass::readPacket(String &str, byte * ip, unsigned int *port) {
|
||||
int len = available() -8; //skip UDP header
|
||||
if(len <= 0) return 0;
|
||||
if(len > str.capacity()) {
|
||||
//packet is longer than string capacity -
|
||||
//resize string - this is very implementation dependent on WString
|
||||
str = String(len);
|
||||
//fall through to read which should be ok now
|
||||
}
|
||||
return (int)recvfrom(_sock,(byte *)(str.cstr()),(uint16_t)str.capacity(),ip,port);
|
||||
}
|
||||
|
||||
/* Create one global object */
|
||||
UdpStringClass UdpString;
|
||||
70
Projects/libraries/Installed_libs/Ethernet/UdpString.h
Executable file
70
Projects/libraries/Installed_libs/Ethernet/UdpString.h
Executable file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* UdpString.h: Library to send/receive UDP packets with the Arduino ethernet shield
|
||||
* using Tom Igoe/Hernando Barragan's WString library.
|
||||
* Drop UdpString.h/.cpp from this distribution into the Ethernet library directory at
|
||||
* hardware/libraries/Ethernet/
|
||||
* Then copy directory /libraries/String from this distribution to /hardware/libraries/String in
|
||||
* your Arduino installation.
|
||||
*
|
||||
* 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/30/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
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <Ethernet.h>
|
||||
#include <UdpBytewise.h>
|
||||
|
||||
/* UdpReceiveBytewise.pde: Example how to receive packets over UDP using UdpBytewise 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
|
||||
unsigned 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 recieved 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);
|
||||
UdpBytewise.begin(localPort);
|
||||
Serial.begin(38400);
|
||||
}
|
||||
/* LOOP: wait for incoming packets and print each packet to the serial port */
|
||||
void loop() {
|
||||
// if there's data available, read a packet
|
||||
if(packetSize = UdpBytewise.available()) {
|
||||
Serial.print("Received packet of size ");
|
||||
Serial.println(packetSize);
|
||||
|
||||
UdpBytewise.getSenderIp(remoteIp);
|
||||
Serial.print("From IP ");
|
||||
for(i=0; i<4; i++) {
|
||||
Serial.print(remoteIp[i],DEC);
|
||||
if(i<3) Serial.print(".");
|
||||
}
|
||||
|
||||
remotePort = UdpBytewise.getSenderPort();
|
||||
Serial.print(" Port ");
|
||||
Serial.println(remotePort);
|
||||
|
||||
Serial.println("Contents:");
|
||||
while(UdpBytewise.available()) {
|
||||
Serial.print(UdpBytewise.read(),BYTE);
|
||||
}
|
||||
}
|
||||
//wait a bit
|
||||
delay(10);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <Ethernet.h>
|
||||
#include <UdpRaw.h>
|
||||
|
||||
/* UdpReceiveRaw.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(38400);
|
||||
}
|
||||
/* 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);
|
||||
|
||||
Serial.print("Received packet of size ");
|
||||
Serial.println(abs(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);
|
||||
|
||||
if(packetSize < 0) {
|
||||
// if return value <0 the packet was truncated to fit into our buffer
|
||||
Serial.print("ERROR: Packet was truncated from ");
|
||||
Serial.print(packetSize*-1);
|
||||
Serial.print(" to ");
|
||||
Serial.print(MAX_SIZE);
|
||||
Serial.println(" bytes.");
|
||||
}
|
||||
|
||||
Serial.println("Contents:");
|
||||
for(i=0; i<min(MAX_SIZE,abs(packetSize)); i++) {
|
||||
Serial.print(packetBuffer[i],BYTE);
|
||||
}
|
||||
Serial.println("");
|
||||
|
||||
}
|
||||
//wait a bit
|
||||
delay(10);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#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(38400);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <Ethernet.h>
|
||||
#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"
|
||||
* 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);
|
||||
UdpBytewise.begin(localPort);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// this version of sendPacket sends a zero-terminated string.
|
||||
UdpBytewise.beginPacket(targetIp,targetPort);
|
||||
UdpBytewise.print("Hello, World! ");
|
||||
UdpBytewise.print(i++);
|
||||
UdpBytewise.endPacket();
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#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);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user