Skip to content

Commit 88e858f

Browse files
committed
Fix for issue 439. UDP API changed to derive from Stream. The old sendPacket and readPacket calls have been removed, and replaced with Stream-derived alternatives which provide more commonality with other communications classes and to allow both buffered and full-packet-at-a-time uses. Also includes the introduction of an IPAddress class to make passing them around easier (and require fewer pointers to be exposed)
1 parent 5009fc1 commit 88e858f

File tree

11 files changed

+355
-116
lines changed

11 files changed

+355
-116
lines changed

libraries/Ethernet/IPAddress.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#include <WProgram.h>
3+
#include <IPAddress.h>
4+
5+
IPAddress::IPAddress()
6+
{
7+
memset(_address, 0, sizeof(_address));
8+
}
9+
10+
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
11+
{
12+
_address[0] = first_octet;
13+
_address[1] = second_octet;
14+
_address[2] = third_octet;
15+
_address[3] = fourth_octet;
16+
}
17+
18+
IPAddress::IPAddress(uint32_t address)
19+
{
20+
memcpy(_address, &address, sizeof(_address));
21+
}
22+
23+
IPAddress::IPAddress(const uint8_t *address)
24+
{
25+
memcpy(_address, address, sizeof(_address));
26+
}
27+
28+
IPAddress& IPAddress::operator=(const uint8_t *address)
29+
{
30+
memcpy(_address, address, sizeof(_address));
31+
return *this;
32+
}
33+
34+
IPAddress& IPAddress::operator=(uint32_t address)
35+
{
36+
memcpy(_address, (const uint8_t *)&address, sizeof(_address));
37+
return *this;
38+
}
39+

libraries/Ethernet/IPAddress.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
*
3+
* MIT License:
4+
* Copyright (c) 2011 Adrian McEwen
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
* adrianm@mcqn.com 1/1/2011
24+
*/
25+
26+
#ifndef IPAddress_h
27+
#define IPAddress_h
28+
29+
// A class to make it easier to handle and pass around IP addresses
30+
31+
class IPAddress {
32+
private:
33+
uint8_t _address[4]; // IPv4 address
34+
35+
public:
36+
// Constructors
37+
IPAddress();
38+
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
39+
IPAddress(uint32_t address);
40+
IPAddress(const uint8_t *address);
41+
42+
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
43+
// to a four-byte uint8_t array is expected
44+
operator uint8_t*() { return _address; };
45+
46+
// Overloaded index operator to allow getting and setting individual octets of the address
47+
uint8_t operator[](int index) const { return _address[index]; };
48+
uint8_t& operator[](int index) { return _address[index]; };
49+
50+
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
51+
IPAddress& operator=(const uint8_t *address);
52+
IPAddress& operator=(uint32_t address);
53+
};
54+
55+
#endif

libraries/Ethernet/Udp.cpp

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -56,106 +56,111 @@ uint8_t UDP::begin(uint16_t port) {
5656
return 1;
5757
}
5858

59-
/* Send packet contained in buf of length len to peer at specified ip, and port */
60-
/* Use this function to transmit binary data that might contain 0x00 bytes*/
61-
/* This function returns sent data size for success else -1. */
62-
uint16_t UDP::sendPacket(uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
63-
return sendto(_sock,(const uint8_t *)buf,len,ip,port);
64-
}
65-
66-
/* Send zero-terminated string str as packet to peer at specified ip, and port */
67-
/* This function returns sent data size for success else -1. */
68-
uint16_t UDP::sendPacket(const char str[], uint8_t * ip, uint16_t port){
69-
// compute strlen
70-
const char *s;
71-
for(s = str; *s; ++s);
72-
uint16_t len = (s-str);
73-
// send packet
74-
return sendto(_sock,(const uint8_t *)str,len,ip,port);
75-
}
7659
/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes.
7760
* returned value includes 8 byte UDP header!*/
7861
int UDP::available() {
7962
return W5100.getRXReceivedSize(_sock);
8063
}
8164

65+
/* Release any resources being used by this UDP instance */
66+
void UDP::stop()
67+
{
68+
if (_sock == MAX_SOCK_NUM)
69+
return;
8270

83-
/* Read a received packet into buffer buf (which is of maximum length len); */
84-
/* store calling ip and port as well. Call available() to make sure data is ready first. */
85-
/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
86-
/* so it's easy to overflow buffer. so we check and truncate. */
87-
/* returns number of bytes read, or negative number of bytes we would have needed if we truncated */
88-
int UDP::readPacket(uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
89-
int packetLen = available()-8; //skip UDP header;
90-
if(packetLen < 0 ) return 0; // no real data here
91-
if(packetLen > (int)bufLen) {
92-
//packet is too large - truncate
93-
//HACK - hand-parse the UDP packet using TCP recv method
94-
uint8_t tmpBuf[8];
95-
int i;
96-
//read 8 header bytes and get IP and port from it
97-
recv(_sock,tmpBuf,8);
98-
ip[0] = tmpBuf[0];
99-
ip[1] = tmpBuf[1];
100-
ip[2] = tmpBuf[2];
101-
ip[3] = tmpBuf[3];
102-
*port = tmpBuf[4];
103-
*port = (*port << 8) + tmpBuf[5];
104-
105-
//now copy first (bufLen) bytes into buf
106-
for(i=0;i<(int)bufLen;i++) {
107-
recv(_sock,tmpBuf,1);
108-
buf[i]=tmpBuf[0];
109-
}
71+
close(_sock);
11072

111-
//and just read the rest byte by byte and throw it away
112-
while(available()) {
113-
recv(_sock,tmpBuf,1);
114-
}
73+
EthernetClass::_server_port[_sock] = 0;
74+
_sock = MAX_SOCK_NUM;
75+
}
11576

116-
return (-1*packetLen);
77+
int UDP::beginPacket(IPAddress ip, uint16_t port)
78+
{
79+
_offset = 0;
80+
return startUDP(_sock, ip, port);
81+
}
11782

118-
//ALTERNATIVE: requires stdlib - takes a bunch of space
119-
/*//create new buffer and read everything into it
120-
uint8_t * tmpBuf = (uint8_t *)malloc(packetLen);
121-
recvfrom(_sock,tmpBuf,packetLen,ip,port);
122-
if(!tmpBuf) return 0; //couldn't allocate
123-
// copy first bufLen bytes
124-
for(unsigned int i=0; i<bufLen; i++) {
125-
buf[i]=tmpBuf[i];
126-
}
127-
//free temp buffer
128-
free(tmpBuf);
129-
*/
83+
int UDP::endPacket()
84+
{
85+
return sendUDP(_sock);
86+
}
13087

88+
void UDP::write(uint8_t byte)
89+
{
90+
write(&byte, 1);
91+
}
13192

132-
}
133-
return recvfrom(_sock,buf,bufLen,ip,port);
93+
void UDP::write(const char *str)
94+
{
95+
size_t len = strlen(str);
96+
write((const uint8_t *)str, len);
13497
}
13598

136-
/* Read a received packet, throw away peer's ip and port. See note above. */
137-
int UDP::readPacket(uint8_t * buf, uint16_t len) {
138-
uint8_t ip[4];
139-
uint16_t port[1];
140-
return recvfrom(_sock,buf,len,ip,port);
99+
void UDP::write(const uint8_t *buffer, size_t size)
100+
{
101+
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
102+
_offset += bytes_written;
141103
}
142104

143-
int UDP::readPacket(char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
144-
uint16_t myPort;
145-
uint16_t ret = readPacket( (byte*)buf, bufLen, ip, &myPort);
146-
port = myPort;
147-
return ret;
105+
int UDP::parsePacket()
106+
{
107+
//HACK - hand-parse the UDP packet using TCP recv method
108+
uint8_t tmpBuf[8];
109+
int ret =0;
110+
//read 8 header bytes and get IP and port from it
111+
ret = recv(_sock,tmpBuf,8);
112+
if (ret > 0)
113+
{
114+
_remoteIP = tmpBuf;
115+
_remotePort = tmpBuf[4];
116+
_remotePort = (_remotePort << 8) + tmpBuf[5];
117+
// When we get here, any remaining bytes are the data
118+
ret = available();
119+
}
120+
return ret;
148121
}
149122

150-
/* Release any resources being used by this UDP instance */
151-
void UDP::stop()
123+
int UDP::read()
152124
{
153-
if (_sock == MAX_SOCK_NUM)
154-
return;
125+
uint8_t byte;
126+
if (recv(_sock, &byte, 1) > 0)
127+
{
128+
// We read things without any problems
129+
return byte;
130+
}
131+
// If we get here, there's no data available
132+
return -1;
133+
}
155134

156-
close(_sock);
135+
int UDP::read(unsigned char* buffer, size_t len)
136+
{
137+
/* In the readPacket that copes with truncating packets, the buffer was
138+
filled with this code. Not sure why it loops round reading out a byte
139+
at a time.
140+
int i;
141+
for(i=0;i<(int)bufLen;i++) {
142+
recv(_sock,tmpBuf,1);
143+
buf[i]=tmpBuf[0];
144+
}
145+
*/
146+
return recv(_sock, buffer, len);
147+
}
157148

158-
EthernetClass::_server_port[_sock] = 0;
159-
_sock = MAX_SOCK_NUM;
149+
int UDP::peek()
150+
{
151+
uint8_t b;
152+
// Unlike recv, peek doesn't check to see if there's any data available, so we must
153+
if (!available())
154+
return -1;
155+
::peek(_sock, &b);
156+
return b;
157+
}
158+
159+
void UDP::flush()
160+
{
161+
while (available())
162+
{
163+
read();
164+
}
160165
}
161166

libraries/Ethernet/Udp.h

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,57 @@
3737
#ifndef udp_h
3838
#define udp_h
3939

40+
#include <Stream.h>
41+
#include <IPAddress.h>
42+
4043
#define UDP_TX_PACKET_MAX_SIZE 24
4144

42-
class UDP {
45+
class UDP : public Stream {
4346
private:
4447
uint8_t _sock; // socket ID for Wiz5100
4548
uint16_t _port; // local port to listen on
49+
IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
50+
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
51+
uint16_t _offset; // offset into the packet being sent
4652

4753
public:
48-
UDP();
54+
UDP(); // Constructor
4955
uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
50-
int available(); // has data been received?
56+
void stop(); // Finish with the UDP socket
57+
58+
// Sending UDP packets
59+
60+
// Start building up a packet to send to the remote host specific in ip and port
61+
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
62+
int beginPacket(IPAddress ip, uint16_t port);
63+
// Finish off this packet and send it
64+
// Returns 1 if the packet was sent successfully, 0 if there was an error
65+
int endPacket();
66+
// Write a single byte into the packet
67+
virtual void write(uint8_t);
68+
// Write a string of characters into the packet
69+
virtual void write(const char *str);
70+
// Write size bytes from buffer into the packet
71+
virtual void write(const uint8_t *buffer, size_t size);
5172

52-
// C-style buffer-oriented functions
53-
uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer
54-
uint16_t sendPacket(const char[], uint8_t *, uint16_t); //send a string as a packet to specified peer
55-
int readPacket(uint8_t *, uint16_t); // read a received packet
56-
int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *); // read a received packet, also return sender's ip and port
57-
// readPacket that fills a character string buffer
58-
int readPacket(char *, uint16_t, uint8_t *, uint16_t &);
73+
// Start processing the next available incoming packet
74+
// Returns the size of the packet in bytes, or 0 if no packets are available
75+
int parsePacket();
76+
// Number of bytes remaining in the current packet
77+
virtual int available();
78+
// Read a single byte from the current packet
79+
virtual int read();
80+
// Read up to len bytes from the current packet and place them into buffer
81+
// Returns the number of bytes read, or 0 if none are available
82+
virtual int read(unsigned char* buffer, size_t len);
83+
// Return the next byte from the current packet without moving on to the next byte
84+
virtual int peek();
85+
virtual void flush(); // Finish reading the current packet
5986

60-
// Finish with the UDP socket
61-
void stop();
87+
// Return the IP address of the host who sent the current incoming packet
88+
IPAddress remoteIP() { return _remoteIP; };
89+
// Return the port of the host who sent the current incoming packet
90+
uint16_t remotePort() { return _remotePort; };
6291
};
6392

6493
#endif

0 commit comments

Comments
 (0)