Update to the fix for Issue #436 - UdpClass renamed to UDP and the constructor moved into the .cpp to prevent compilation errors in certain conditions if w5100.h hasn't been included before Udp.h

This commit is contained in:
amcewen 2011-01-01 21:42:23 +00:00
parent bc0f3c4fe1
commit ca07ac18f3
4 changed files with 16 additions and 13 deletions

View File

@ -31,8 +31,11 @@
#include "Ethernet.h" #include "Ethernet.h"
#include "Udp.h" #include "Udp.h"
/* Constructor */
UDP::UDP() : _sock(MAX_SOCK_NUM) {}
/* Start UDP socket, listening at local port PORT */ /* Start UDP socket, listening at local port PORT */
uint8_t UdpClass::begin(uint16_t port) { uint8_t UDP::begin(uint16_t port) {
if (_sock != MAX_SOCK_NUM) if (_sock != MAX_SOCK_NUM)
return 0; return 0;
@ -56,13 +59,13 @@ uint8_t UdpClass::begin(uint16_t port) {
/* Send packet contained in buf of length len to peer at specified ip, and port */ /* 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*/ /* Use this function to transmit binary data that might contain 0x00 bytes*/
/* This function returns sent data size for success else -1. */ /* 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){ uint16_t UDP::sendPacket(uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
return sendto(_sock,(const uint8_t *)buf,len,ip,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 */ /* Send zero-terminated string str as packet to peer at specified ip, and port */
/* This function returns sent data size for success else -1. */ /* This function returns sent data size for success else -1. */
uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){ uint16_t UDP::sendPacket(const char str[], uint8_t * ip, uint16_t port){
// compute strlen // compute strlen
const char *s; const char *s;
for(s = str; *s; ++s); for(s = str; *s; ++s);
@ -72,7 +75,7 @@ uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){
} }
/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes. /* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes.
* returned value includes 8 byte UDP header!*/ * returned value includes 8 byte UDP header!*/
int UdpClass::available() { int UDP::available() {
return W5100.getRXReceivedSize(_sock); return W5100.getRXReceivedSize(_sock);
} }
@ -82,7 +85,7 @@ int UdpClass::available() {
/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/ /* 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. */ /* 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 */ /* returns number of bytes read, or negative number of bytes we would have needed if we truncated */
int UdpClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) { int UDP::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
int packetLen = available()-8; //skip UDP header; int packetLen = available()-8; //skip UDP header;
if(packetLen < 0 ) return 0; // no real data here if(packetLen < 0 ) return 0; // no real data here
if(packetLen > (int)bufLen) { if(packetLen > (int)bufLen) {
@ -131,21 +134,21 @@ int UdpClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *
} }
/* Read a received packet, throw away peer's ip and port. See note above. */ /* Read a received packet, throw away peer's ip and port. See note above. */
int UdpClass::readPacket(uint8_t * buf, uint16_t len) { int UDP::readPacket(uint8_t * buf, uint16_t len) {
uint8_t ip[4]; uint8_t ip[4];
uint16_t port[1]; uint16_t port[1];
return recvfrom(_sock,buf,len,ip,port); return recvfrom(_sock,buf,len,ip,port);
} }
int UdpClass::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) { int UDP::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
uint16_t myPort; uint16_t myPort;
uint16_t ret = readPacket( (byte*)buf, bufLen, ip, &myPort); uint16_t ret = readPacket( (byte*)buf, bufLen, ip, &myPort);
port = myPort; port = myPort;
return ret; return ret;
} }
/* Release any resources being used by this UdpClass instance */ /* Release any resources being used by this UDP instance */
void UdpClass::stop() void UDP::stop()
{ {
if (_sock == MAX_SOCK_NUM) if (_sock == MAX_SOCK_NUM)
return; return;

View File

@ -39,13 +39,13 @@
#define UDP_TX_PACKET_MAX_SIZE 24 #define UDP_TX_PACKET_MAX_SIZE 24
class UdpClass { class UDP {
private: private:
uint8_t _sock; // socket ID for Wiz5100 uint8_t _sock; // socket ID for Wiz5100
uint16_t _port; // local port to listen on uint16_t _port; // local port to listen on
public: public:
UdpClass() : _sock(MAX_SOCK_NUM) {}; UDP();
uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
int available(); // has data been received? int available(); // has data been received?

View File

@ -36,7 +36,7 @@ char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back char ReplyBuffer[] = "acknowledged"; // a string to send back
// A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
UdpClass Udp; UDP Udp;
void setup() { void setup() {
// start the Ethernet and UDP: // start the Ethernet and UDP:

View File

@ -38,7 +38,7 @@ const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
UdpClass Udp; UDP Udp;
void setup() void setup()
{ {