Skip to content

Commit ca07ac1

Browse files
committed
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
1 parent bc0f3c4 commit ca07ac1

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

libraries/Ethernet/Udp.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
#include "Ethernet.h"
3232
#include "Udp.h"
3333

34+
/* Constructor */
35+
UDP::UDP() : _sock(MAX_SOCK_NUM) {}
36+
3437
/* Start UDP socket, listening at local port PORT */
35-
uint8_t UdpClass::begin(uint16_t port) {
38+
uint8_t UDP::begin(uint16_t port) {
3639
if (_sock != MAX_SOCK_NUM)
3740
return 0;
3841

@@ -56,13 +59,13 @@ uint8_t UdpClass::begin(uint16_t port) {
5659
/* Send packet contained in buf of length len to peer at specified ip, and port */
5760
/* Use this function to transmit binary data that might contain 0x00 bytes*/
5861
/* This function returns sent data size for success else -1. */
59-
uint16_t UdpClass::sendPacket(uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
62+
uint16_t UDP::sendPacket(uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
6063
return sendto(_sock,(const uint8_t *)buf,len,ip,port);
6164
}
6265

6366
/* Send zero-terminated string str as packet to peer at specified ip, and port */
6467
/* This function returns sent data size for success else -1. */
65-
uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){
68+
uint16_t UDP::sendPacket(const char str[], uint8_t * ip, uint16_t port){
6669
// compute strlen
6770
const char *s;
6871
for(s = str; *s; ++s);
@@ -72,7 +75,7 @@ uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){
7275
}
7376
/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes.
7477
* returned value includes 8 byte UDP header!*/
75-
int UdpClass::available() {
78+
int UDP::available() {
7679
return W5100.getRXReceivedSize(_sock);
7780
}
7881

@@ -82,7 +85,7 @@ int UdpClass::available() {
8285
/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
8386
/* so it's easy to overflow buffer. so we check and truncate. */
8487
/* returns number of bytes read, or negative number of bytes we would have needed if we truncated */
85-
int UdpClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
88+
int UDP::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
8689
int packetLen = available()-8; //skip UDP header;
8790
if(packetLen < 0 ) return 0; // no real data here
8891
if(packetLen > (int)bufLen) {
@@ -131,21 +134,21 @@ int UdpClass::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *
131134
}
132135

133136
/* Read a received packet, throw away peer's ip and port. See note above. */
134-
int UdpClass::readPacket(uint8_t * buf, uint16_t len) {
137+
int UDP::readPacket(uint8_t * buf, uint16_t len) {
135138
uint8_t ip[4];
136139
uint16_t port[1];
137140
return recvfrom(_sock,buf,len,ip,port);
138141
}
139142

140-
int UdpClass::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
143+
int UDP::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
141144
uint16_t myPort;
142145
uint16_t ret = readPacket( (byte*)buf, bufLen, ip, &myPort);
143146
port = myPort;
144147
return ret;
145148
}
146149

147-
/* Release any resources being used by this UdpClass instance */
148-
void UdpClass::stop()
150+
/* Release any resources being used by this UDP instance */
151+
void UDP::stop()
149152
{
150153
if (_sock == MAX_SOCK_NUM)
151154
return;

libraries/Ethernet/Udp.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939

4040
#define UDP_TX_PACKET_MAX_SIZE 24
4141

42-
class UdpClass {
42+
class UDP {
4343
private:
4444
uint8_t _sock; // socket ID for Wiz5100
4545
uint16_t _port; // local port to listen on
4646

4747
public:
48-
UdpClass() : _sock(MAX_SOCK_NUM) {};
48+
UDP();
4949
uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
5050
int available(); // has data been received?
5151

libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.pde

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
3636
char ReplyBuffer[] = "acknowledged"; // a string to send back
3737

3838
// A UDP instance to let us send and receive packets over UDP
39-
UdpClass Udp;
39+
UDP Udp;
4040

4141
void setup() {
4242
// start the Ethernet and UDP:

libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.pde

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the
3838
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
3939

4040
// A UDP instance to let us send and receive packets over UDP
41-
UdpClass Udp;
41+
UDP Udp;
4242

4343
void setup()
4444
{

0 commit comments

Comments
 (0)