mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-07 00:04:36 +03:00
Created an abstract base class UDP to match the Client and Server classes, and reworked the Ethernet library to use it and derive EthernetUDP.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#ifndef Dhcp_h
|
||||
#define Dhcp_h
|
||||
|
||||
#include "Udp.h"
|
||||
#include "EthernetUdp.h"
|
||||
|
||||
/* DHCP state machine. */
|
||||
#define STATE_DHCP_START 0
|
||||
@@ -139,7 +139,7 @@ private:
|
||||
uint8_t _dhcpGatewayIp[4];
|
||||
uint8_t _dhcpDhcpServerIp[4];
|
||||
uint8_t _dhcpDnsServerIp[4];
|
||||
UDP _dhcpUdpSocket;
|
||||
EthernetUDP _dhcpUdpSocket;
|
||||
|
||||
void presend_DHCP();
|
||||
void send_DHCP_MESSAGE(uint8_t, uint16_t);
|
||||
|
@@ -3,7 +3,7 @@
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#include "w5100.h"
|
||||
#include "Udp.h"
|
||||
#include "EthernetUdp.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "Dns.h"
|
||||
|
@@ -5,7 +5,7 @@
|
||||
#ifndef DNSClient_h
|
||||
#define DNSClient_h
|
||||
|
||||
#include <Udp.h>
|
||||
#include <EthernetUdp.h>
|
||||
|
||||
class DNSClient
|
||||
{
|
||||
@@ -35,7 +35,7 @@ protected:
|
||||
|
||||
IPAddress iDNSServer;
|
||||
uint16_t iRequestId;
|
||||
UDP iUdp;
|
||||
EthernetUDP iUdp;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -33,10 +33,10 @@
|
||||
#include "Dns.h"
|
||||
|
||||
/* Constructor */
|
||||
UDP::UDP() : _sock(MAX_SOCK_NUM) {}
|
||||
EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {}
|
||||
|
||||
/* Start UDP socket, listening at local port PORT */
|
||||
uint8_t UDP::begin(uint16_t port) {
|
||||
/* Start EthernetUDP socket, listening at local port PORT */
|
||||
uint8_t EthernetUDP::begin(uint16_t port) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
@@ -59,12 +59,12 @@ uint8_t UDP::begin(uint16_t 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 UDP::available() {
|
||||
int EthernetUDP::available() {
|
||||
return W5100.getRXReceivedSize(_sock);
|
||||
}
|
||||
|
||||
/* Release any resources being used by this UDP instance */
|
||||
void UDP::stop()
|
||||
/* Release any resources being used by this EthernetUDP instance */
|
||||
void EthernetUDP::stop()
|
||||
{
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return;
|
||||
@@ -75,7 +75,7 @@ void UDP::stop()
|
||||
_sock = MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
int UDP::beginPacket(const char *host, uint16_t port)
|
||||
int EthernetUDP::beginPacket(const char *host, uint16_t port)
|
||||
{
|
||||
// Look up the host first
|
||||
int ret = 0;
|
||||
@@ -91,36 +91,36 @@ int UDP::beginPacket(const char *host, uint16_t port)
|
||||
}
|
||||
}
|
||||
|
||||
int UDP::beginPacket(IPAddress ip, uint16_t port)
|
||||
int EthernetUDP::beginPacket(IPAddress ip, uint16_t port)
|
||||
{
|
||||
_offset = 0;
|
||||
return startUDP(_sock, ip.raw_address(), port);
|
||||
return startUDP(_sock, rawIPAddress(ip), port);
|
||||
}
|
||||
|
||||
int UDP::endPacket()
|
||||
int EthernetUDP::endPacket()
|
||||
{
|
||||
return sendUDP(_sock);
|
||||
}
|
||||
|
||||
size_t UDP::write(uint8_t byte)
|
||||
size_t EthernetUDP::write(uint8_t byte)
|
||||
{
|
||||
return write(&byte, 1);
|
||||
}
|
||||
|
||||
size_t UDP::write(const char *str)
|
||||
size_t EthernetUDP::write(const char *str)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
return write((const uint8_t *)str, len);
|
||||
}
|
||||
|
||||
size_t UDP::write(const uint8_t *buffer, size_t size)
|
||||
size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
|
||||
_offset += bytes_written;
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
int UDP::parsePacket()
|
||||
int EthernetUDP::parsePacket()
|
||||
{
|
||||
if (available() > 0)
|
||||
{
|
||||
@@ -143,7 +143,7 @@ int UDP::parsePacket()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int UDP::read()
|
||||
int EthernetUDP::read()
|
||||
{
|
||||
uint8_t byte;
|
||||
if (recv(_sock, &byte, 1) > 0)
|
||||
@@ -155,7 +155,7 @@ int UDP::read()
|
||||
return -1;
|
||||
}
|
||||
|
||||
int UDP::read(unsigned char* buffer, size_t len)
|
||||
int EthernetUDP::read(unsigned char* buffer, size_t len)
|
||||
{
|
||||
/* In the readPacket that copes with truncating packets, the buffer was
|
||||
filled with this code. Not sure why it loops round reading out a byte
|
||||
@@ -169,7 +169,7 @@ int UDP::read(unsigned char* buffer, size_t len)
|
||||
return recv(_sock, buffer, len);
|
||||
}
|
||||
|
||||
int UDP::peek()
|
||||
int EthernetUDP::peek()
|
||||
{
|
||||
uint8_t b;
|
||||
// Unlike recv, peek doesn't check to see if there's any data available, so we must
|
||||
@@ -179,7 +179,7 @@ int UDP::peek()
|
||||
return b;
|
||||
}
|
||||
|
||||
void UDP::flush()
|
||||
void EthernetUDP::flush()
|
||||
{
|
||||
while (available())
|
||||
{
|
@@ -34,15 +34,14 @@
|
||||
* bjoern@cs.stanford.edu 12/30/2008
|
||||
*/
|
||||
|
||||
#ifndef udp_h
|
||||
#define udp_h
|
||||
#ifndef ethernetudp_h
|
||||
#define ethernetudp_h
|
||||
|
||||
#include <Stream.h>
|
||||
#include <IPAddress.h>
|
||||
#include <Udp.h>
|
||||
|
||||
#define UDP_TX_PACKET_MAX_SIZE 24
|
||||
|
||||
class UDP : public Stream {
|
||||
class EthernetUDP : public UDP {
|
||||
private:
|
||||
uint8_t _sock; // socket ID for Wiz5100
|
||||
uint16_t _port; // local port to listen on
|
||||
@@ -51,21 +50,21 @@ private:
|
||||
uint16_t _offset; // offset into the packet being sent
|
||||
|
||||
public:
|
||||
UDP(); // Constructor
|
||||
uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
|
||||
void stop(); // Finish with the UDP socket
|
||||
EthernetUDP(); // Constructor
|
||||
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
|
||||
virtual void stop(); // Finish with the UDP socket
|
||||
|
||||
// Sending UDP packets
|
||||
|
||||
// Start building up a packet to send to the remote host specific in ip and port
|
||||
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
|
||||
int beginPacket(IPAddress ip, uint16_t port);
|
||||
virtual int beginPacket(IPAddress ip, uint16_t port);
|
||||
// Start building up a packet to send to the remote host specific in host and port
|
||||
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
|
||||
int beginPacket(const char *host, uint16_t port);
|
||||
virtual int beginPacket(const char *host, uint16_t port);
|
||||
// Finish off this packet and send it
|
||||
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
||||
int endPacket();
|
||||
virtual int endPacket();
|
||||
// Write a single byte into the packet
|
||||
virtual size_t write(uint8_t);
|
||||
// Write a string of characters into the packet
|
||||
@@ -75,7 +74,7 @@ public:
|
||||
|
||||
// Start processing the next available incoming packet
|
||||
// Returns the size of the packet in bytes, or 0 if no packets are available
|
||||
int parsePacket();
|
||||
virtual int parsePacket();
|
||||
// Number of bytes remaining in the current packet
|
||||
virtual int available();
|
||||
// Read a single byte from the current packet
|
||||
@@ -91,9 +90,9 @@ public:
|
||||
virtual void flush(); // Finish reading the current packet
|
||||
|
||||
// Return the IP address of the host who sent the current incoming packet
|
||||
IPAddress remoteIP() { return _remoteIP; };
|
||||
virtual IPAddress remoteIP() { return _remoteIP; };
|
||||
// Return the port of the host who sent the current incoming packet
|
||||
uint16_t remotePort() { return _remotePort; };
|
||||
virtual uint16_t remotePort() { return _remotePort; };
|
||||
};
|
||||
|
||||
#endif
|
@@ -15,7 +15,7 @@
|
||||
|
||||
#include <SPI.h> // needed for Arduino versions later than 0018
|
||||
#include <Ethernet.h>
|
||||
#include <Udp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
|
||||
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
|
||||
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
@@ -30,8 +30,8 @@ unsigned int localPort = 8888; // local port to listen on
|
||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
|
||||
char ReplyBuffer[] = "acknowledged"; // a string to send back
|
||||
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
UDP Udp;
|
||||
// An EthernetUDP instance to let us send and receive packets over UDP
|
||||
EthernetUDP Udp;
|
||||
|
||||
void setup() {
|
||||
// start the Ethernet and UDP:
|
||||
|
@@ -18,7 +18,7 @@
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <Udp.h>
|
||||
#include <EthernetUdp.h>
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
||||
@@ -34,7 +34,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
|
||||
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
UDP Udp;
|
||||
EthernetUDP Udp;
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
Reference in New Issue
Block a user