Merge remote-tracking branch 'amcewen/wifly_integration' into new-extension

This commit is contained in:
David A. Mellis 2011-08-31 14:25:04 -04:00
commit da5ccf4eaf
36 changed files with 333 additions and 140 deletions

View File

@ -0,0 +1,27 @@
#ifndef client_h
#define client_h
#include "Print.h"
#include "Stream.h"
#include "IPAddress.h"
class Client : public Stream {
public:
virtual int connect(IPAddress ip, uint16_t port) =0;
virtual int connect(const char *host, uint16_t port) =0;
virtual size_t write(uint8_t) =0;
virtual size_t write(const char *str) =0;
virtual size_t write(const uint8_t *buf, size_t size) =0;
virtual int available() = 0;
virtual int read() = 0;
virtual int read(uint8_t *buf, size_t size) = 0;
virtual int peek() = 0;
virtual void flush() = 0;
virtual void stop() = 0;
virtual uint8_t connected() = 0;
virtual operator bool() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
};
#endif

View File

@ -20,6 +20,8 @@
#ifndef Printable_h #ifndef Printable_h
#define Printable_h #define Printable_h
#include <new.h>
class Print; class Print;
/** The Printable class provides a way for new classes to allow themselves to be printed. /** The Printable class provides a way for new classes to allow themselves to be printed.
@ -27,6 +29,7 @@ class Print;
for users to print out instances of this class by passing them into the usual for users to print out instances of this class by passing them into the usual
Print::print and Print::println methods. Print::print and Print::println methods.
*/ */
class Printable class Printable
{ {
public: public:

View File

@ -0,0 +1,9 @@
#ifndef server_h
#define server_h
class Server {
public:
virtual void begin() =0;
};
#endif

View File

@ -0,0 +1,90 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* 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 udp_h
#define udp_h
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
virtual void stop() =0; // 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
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// 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
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write a string of characters into the packet
virtual size_t write(const char *str) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
};
#endif

View File

@ -0,0 +1,18 @@
#include <new.h>
void * operator new(size_t size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
void __cxa_guard_abort (__guard *) {};
void __cxa_pure_virtual(void) {};

View File

@ -0,0 +1,22 @@
/* Header to define new/delete operators as they aren't provided by avr-gcc by default
Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453
*/
#ifndef NEW_H
#define NEW_H
#include <stdlib.h>
void * operator new(size_t size);
void operator delete(void * ptr);
__extension__ typedef int __guard __attribute__((mode (__DI__)));
extern "C" int __cxa_guard_acquire(__guard *);
extern "C" void __cxa_guard_release (__guard *);
extern "C" void __cxa_guard_abort (__guard *);
extern "C" void __cxa_pure_virtual(void);
#endif

View File

@ -271,11 +271,19 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
case routersOnSubnet : case routersOnSubnet :
opt_len = _dhcpUdpSocket.read(); opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpGatewayIp, 4); _dhcpUdpSocket.read(_dhcpGatewayIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
}
break; break;
case dns : case dns :
opt_len = _dhcpUdpSocket.read(); opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4); _dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
}
break; break;
case dhcpServerIdentifier : case dhcpServerIdentifier :

View File

@ -4,7 +4,7 @@
#ifndef Dhcp_h #ifndef Dhcp_h
#define Dhcp_h #define Dhcp_h
#include "Udp.h" #include "EthernetUdp.h"
/* DHCP state machine. */ /* DHCP state machine. */
#define STATE_DHCP_START 0 #define STATE_DHCP_START 0
@ -139,7 +139,7 @@ private:
uint8_t _dhcpGatewayIp[4]; uint8_t _dhcpGatewayIp[4];
uint8_t _dhcpDhcpServerIp[4]; uint8_t _dhcpDhcpServerIp[4];
uint8_t _dhcpDnsServerIp[4]; uint8_t _dhcpDnsServerIp[4];
UDP _dhcpUdpSocket; EthernetUDP _dhcpUdpSocket;
void presend_DHCP(); void presend_DHCP();
void send_DHCP_MESSAGE(uint8_t, uint16_t); void send_DHCP_MESSAGE(uint8_t, uint16_t);

View File

@ -3,7 +3,7 @@
// Released under Apache License, version 2.0 // Released under Apache License, version 2.0
#include "w5100.h" #include "w5100.h"
#include "Udp.h" #include "EthernetUdp.h"
#include "util.h" #include "util.h"
#include "Dns.h" #include "Dns.h"

View File

@ -5,7 +5,7 @@
#ifndef DNSClient_h #ifndef DNSClient_h
#define DNSClient_h #define DNSClient_h
#include <Udp.h> #include <EthernetUdp.h>
class DNSClient class DNSClient
{ {
@ -35,7 +35,7 @@ protected:
IPAddress iDNSServer; IPAddress iDNSServer;
uint16_t iRequestId; uint16_t iRequestId;
UDP iUdp; EthernetUDP iUdp;
}; };
#endif #endif

View File

@ -33,27 +33,37 @@ int EthernetClass::begin(uint8_t *mac_address)
} }
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip) void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
{
// Assume the DNS server will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress dns_server = local_ip;
dns_server[3] = 1;
begin(mac_address, local_ip, dns_server);
}
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
{ {
// Assume the gateway will be the machine on the same network as the local IP // Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1' // but with last octet being '1'
IPAddress gateway = local_ip; IPAddress gateway = local_ip;
gateway[3] = 1; gateway[3] = 1;
begin(mac_address, local_ip, gateway); begin(mac_address, local_ip, dns_server, gateway);
} }
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway) void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{ {
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
begin(mac_address, local_ip, gateway, subnet); begin(mac_address, local_ip, dns_server, gateway, subnet);
} }
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress gateway, IPAddress subnet) void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{ {
W5100.init(); W5100.init();
W5100.setMACAddress(mac); W5100.setMACAddress(mac);
W5100.setIPAddress(local_ip._address); W5100.setIPAddress(local_ip._address);
W5100.setGatewayIp(gateway._address); W5100.setGatewayIp(gateway._address);
W5100.setSubnetMask(subnet._address); W5100.setSubnetMask(subnet._address);
_dnsServerAddress = dns_server;
} }
IPAddress EthernetClass::localIP() IPAddress EthernetClass::localIP()

View File

@ -4,8 +4,8 @@
#include <inttypes.h> #include <inttypes.h>
//#include "w5100.h" //#include "w5100.h"
#include "IPAddress.h" #include "IPAddress.h"
#include "Client.h" #include "EthernetClient.h"
#include "Server.h" #include "EthernetServer.h"
#define MAX_SOCK_NUM 4 #define MAX_SOCK_NUM 4
@ -20,16 +20,17 @@ public:
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded // Returns 0 if the DHCP configuration failed, and 1 if it succeeded
int begin(uint8_t *mac_address); int begin(uint8_t *mac_address);
void begin(uint8_t *mac_address, IPAddress local_ip); void begin(uint8_t *mac_address, IPAddress local_ip);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway); void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway, IPAddress subnet); void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
IPAddress localIP(); IPAddress localIP();
IPAddress subnetMask(); IPAddress subnetMask();
IPAddress gatewayIP(); IPAddress gatewayIP();
IPAddress dnsServerIP(); IPAddress dnsServerIP();
friend class Client; friend class EthernetClient;
friend class Server; friend class EthernetServer;
}; };
extern EthernetClass Ethernet; extern EthernetClass Ethernet;

View File

@ -8,19 +8,19 @@ extern "C" {
#include "Arduino.h" #include "Arduino.h"
#include "Ethernet.h" #include "Ethernet.h"
#include "Client.h" #include "EthernetClient.h"
#include "Server.h" #include "EthernetServer.h"
#include "Dns.h" #include "Dns.h"
uint16_t Client::_srcport = 1024; uint16_t EthernetClient::_srcport = 1024;
Client::Client() : _sock(MAX_SOCK_NUM) { EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) {
} }
Client::Client(uint8_t sock) : _sock(sock) { EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) {
} }
int Client::connect(const char* host, uint16_t port) { int EthernetClient::connect(const char* host, uint16_t port) {
// Look up the host first // Look up the host first
int ret = 0; int ret = 0;
DNSClient dns; DNSClient dns;
@ -35,7 +35,7 @@ int Client::connect(const char* host, uint16_t port) {
} }
} }
int Client::connect(IPAddress ip, uint16_t port) { int EthernetClient::connect(IPAddress ip, uint16_t port) {
if (_sock != MAX_SOCK_NUM) if (_sock != MAX_SOCK_NUM)
return 0; return 0;
@ -54,7 +54,7 @@ int Client::connect(IPAddress ip, uint16_t port) {
if (_srcport == 0) _srcport = 1024; if (_srcport == 0) _srcport = 1024;
socket(_sock, SnMR::TCP, _srcport, 0); socket(_sock, SnMR::TCP, _srcport, 0);
if (!::connect(_sock, ip.raw_address(), port)) { if (!::connect(_sock, rawIPAddress(ip), port)) {
_sock = MAX_SOCK_NUM; _sock = MAX_SOCK_NUM;
return 0; return 0;
} }
@ -70,15 +70,15 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1; return 1;
} }
size_t Client::write(uint8_t b) { size_t EthernetClient::write(uint8_t b) {
return write(&b, 1); return write(&b, 1);
} }
size_t Client::write(const char *str) { size_t EthernetClient::write(const char *str) {
return write((const uint8_t *) str, strlen(str)); return write((const uint8_t *) str, strlen(str));
} }
size_t Client::write(const uint8_t *buf, size_t size) { size_t EthernetClient::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) { if (_sock == MAX_SOCK_NUM) {
setWriteError(); setWriteError();
return 0; return 0;
@ -90,13 +90,13 @@ size_t Client::write(const uint8_t *buf, size_t size) {
return size; return size;
} }
int Client::available() { int EthernetClient::available() {
if (_sock != MAX_SOCK_NUM) if (_sock != MAX_SOCK_NUM)
return W5100.getRXReceivedSize(_sock); return W5100.getRXReceivedSize(_sock);
return 0; return 0;
} }
int Client::read() { int EthernetClient::read() {
uint8_t b; uint8_t b;
if ( recv(_sock, &b, 1) > 0 ) if ( recv(_sock, &b, 1) > 0 )
{ {
@ -110,11 +110,11 @@ int Client::read() {
} }
} }
int Client::read(uint8_t *buf, size_t size) { int EthernetClient::read(uint8_t *buf, size_t size) {
return recv(_sock, buf, size); return recv(_sock, buf, size);
} }
int Client::peek() { int EthernetClient::peek() {
uint8_t b; uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must // Unlike recv, peek doesn't check to see if there's any data available, so we must
if (!available()) if (!available())
@ -123,12 +123,12 @@ int Client::peek() {
return b; return b;
} }
void Client::flush() { void EthernetClient::flush() {
while (available()) while (available())
read(); read();
} }
void Client::stop() { void EthernetClient::stop() {
if (_sock == MAX_SOCK_NUM) if (_sock == MAX_SOCK_NUM)
return; return;
@ -148,7 +148,7 @@ void Client::stop() {
_sock = MAX_SOCK_NUM; _sock = MAX_SOCK_NUM;
} }
uint8_t Client::connected() { uint8_t EthernetClient::connected() {
if (_sock == MAX_SOCK_NUM) return 0; if (_sock == MAX_SOCK_NUM) return 0;
uint8_t s = status(); uint8_t s = status();
@ -156,14 +156,14 @@ uint8_t Client::connected() {
(s == SnSR::CLOSE_WAIT && !available())); (s == SnSR::CLOSE_WAIT && !available()));
} }
uint8_t Client::status() { uint8_t EthernetClient::status() {
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
return W5100.readSnSR(_sock); return W5100.readSnSR(_sock);
} }
// the next function allows us to use the client returned by // the next function allows us to use the client returned by
// Server::available() as the condition in an if-statement. // EthernetServer::available() as the condition in an if-statement.
Client::operator bool() { EthernetClient::operator bool() {
return _sock != MAX_SOCK_NUM; return _sock != MAX_SOCK_NUM;
} }

View File

@ -1,17 +1,19 @@
#ifndef client_h #ifndef ethernetclient_h
#define client_h #define ethernetclient_h
#include "Arduino.h" #include "Arduino.h"
#include "Print.h" #include "Print.h"
#include "Client.h"
#include "IPAddress.h"
class Client : public Stream { class EthernetClient : public Client {
public: public:
Client(); EthernetClient();
Client(uint8_t sock); EthernetClient(uint8_t sock);
uint8_t status(); uint8_t status();
int connect(IPAddress ip, uint16_t port); virtual int connect(IPAddress ip, uint16_t port);
int connect(const char *host, uint16_t port); virtual int connect(const char *host, uint16_t port);
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
virtual size_t write(const char *str); virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size); virtual size_t write(const uint8_t *buf, size_t size);
@ -20,11 +22,11 @@ public:
virtual int read(uint8_t *buf, size_t size); virtual int read(uint8_t *buf, size_t size);
virtual int peek(); virtual int peek();
virtual void flush(); virtual void flush();
void stop(); virtual void stop();
uint8_t connected(); virtual uint8_t connected();
operator bool(); virtual operator bool();
friend class Server; friend class EthernetServer;
private: private:
static uint16_t _srcport; static uint16_t _srcport;

View File

@ -5,18 +5,18 @@ extern "C" {
} }
#include "Ethernet.h" #include "Ethernet.h"
#include "Client.h" #include "EthernetClient.h"
#include "Server.h" #include "EthernetServer.h"
Server::Server(uint16_t port) EthernetServer::EthernetServer(uint16_t port)
{ {
_port = port; _port = port;
} }
void Server::begin() void EthernetServer::begin()
{ {
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock); EthernetClient client(sock);
if (client.status() == SnSR::CLOSED) { if (client.status() == SnSR::CLOSED) {
socket(sock, SnMR::TCP, _port, 0); socket(sock, SnMR::TCP, _port, 0);
listen(sock); listen(sock);
@ -26,12 +26,12 @@ void Server::begin()
} }
} }
void Server::accept() void EthernetServer::accept()
{ {
int listening = 0; int listening = 0;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock); EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port) { if (EthernetClass::_server_port[sock] == _port) {
if (client.status() == SnSR::LISTEN) { if (client.status() == SnSR::LISTEN) {
@ -48,12 +48,12 @@ void Server::accept()
} }
} }
Client Server::available() EthernetClient EthernetServer::available()
{ {
accept(); accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock); EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port && if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED || (client.status() == SnSR::ESTABLISHED ||
client.status() == SnSR::CLOSE_WAIT)) { client.status() == SnSR::CLOSE_WAIT)) {
@ -64,27 +64,27 @@ Client Server::available()
} }
} }
return Client(MAX_SOCK_NUM); return EthernetClient(MAX_SOCK_NUM);
} }
size_t Server::write(uint8_t b) size_t EthernetServer::write(uint8_t b)
{ {
write(&b, 1); write(&b, 1);
} }
size_t Server::write(const char *str) size_t EthernetServer::write(const char *str)
{ {
write((const uint8_t *)str, strlen(str)); write((const uint8_t *)str, strlen(str));
} }
size_t Server::write(const uint8_t *buffer, size_t size) size_t EthernetServer::write(const uint8_t *buffer, size_t size)
{ {
size_t n = 0; size_t n = 0;
accept(); accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock); EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port && if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) { client.status() == SnSR::ESTABLISHED) {

View File

@ -0,0 +1,22 @@
#ifndef ethernetserver_h
#define ethernetserver_h
#include "Server.h"
class EthernetClient;
class EthernetServer :
public Server {
private:
uint16_t _port;
void accept();
public:
EthernetServer(uint16_t);
EthernetClient available();
virtual void begin();
virtual size_t write(uint8_t);
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
};
#endif

View File

@ -33,10 +33,10 @@
#include "Dns.h" #include "Dns.h"
/* Constructor */ /* Constructor */
UDP::UDP() : _sock(MAX_SOCK_NUM) {} EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {}
/* Start UDP socket, listening at local port PORT */ /* Start EthernetUDP socket, listening at local port PORT */
uint8_t UDP::begin(uint16_t port) { uint8_t EthernetUDP::begin(uint16_t port) {
if (_sock != MAX_SOCK_NUM) if (_sock != MAX_SOCK_NUM)
return 0; 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. /* 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 UDP::available() { int EthernetUDP::available() {
return W5100.getRXReceivedSize(_sock); return W5100.getRXReceivedSize(_sock);
} }
/* Release any resources being used by this UDP instance */ /* Release any resources being used by this EthernetUDP instance */
void UDP::stop() void EthernetUDP::stop()
{ {
if (_sock == MAX_SOCK_NUM) if (_sock == MAX_SOCK_NUM)
return; return;
@ -75,7 +75,7 @@ void UDP::stop()
_sock = MAX_SOCK_NUM; _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 // Look up the host first
int ret = 0; 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; _offset = 0;
return startUDP(_sock, ip.raw_address(), port); return startUDP(_sock, rawIPAddress(ip), port);
} }
int UDP::endPacket() int EthernetUDP::endPacket()
{ {
return sendUDP(_sock); return sendUDP(_sock);
} }
size_t UDP::write(uint8_t byte) size_t EthernetUDP::write(uint8_t byte)
{ {
return write(&byte, 1); return write(&byte, 1);
} }
size_t UDP::write(const char *str) size_t EthernetUDP::write(const char *str)
{ {
size_t len = strlen(str); size_t len = strlen(str);
return write((const uint8_t *)str, len); 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); uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
_offset += bytes_written; _offset += bytes_written;
return bytes_written; return bytes_written;
} }
int UDP::parsePacket() int EthernetUDP::parsePacket()
{ {
if (available() > 0) if (available() > 0)
{ {
@ -143,7 +143,7 @@ int UDP::parsePacket()
return 0; return 0;
} }
int UDP::read() int EthernetUDP::read()
{ {
uint8_t byte; uint8_t byte;
if (recv(_sock, &byte, 1) > 0) if (recv(_sock, &byte, 1) > 0)
@ -155,7 +155,7 @@ int UDP::read()
return -1; 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 /* 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 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); return recv(_sock, buffer, len);
} }
int UDP::peek() int EthernetUDP::peek()
{ {
uint8_t b; uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must // 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; return b;
} }
void UDP::flush() void EthernetUDP::flush()
{ {
while (available()) while (available())
{ {

View File

@ -34,15 +34,14 @@
* bjoern@cs.stanford.edu 12/30/2008 * bjoern@cs.stanford.edu 12/30/2008
*/ */
#ifndef udp_h #ifndef ethernetudp_h
#define udp_h #define ethernetudp_h
#include <Stream.h> #include <Udp.h>
#include <IPAddress.h>
#define UDP_TX_PACKET_MAX_SIZE 24 #define UDP_TX_PACKET_MAX_SIZE 24
class UDP : public Stream { class EthernetUDP : public 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
@ -51,21 +50,21 @@ private:
uint16_t _offset; // offset into the packet being sent uint16_t _offset; // offset into the packet being sent
public: public:
UDP(); // Constructor EthernetUDP(); // 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 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
void stop(); // Finish with the UDP socket virtual void stop(); // Finish with the UDP socket
// Sending UDP packets // Sending UDP packets
// Start building up a packet to send to the remote host specific in ip and port // 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 // 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 // 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 // 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 // Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error // 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 // Write a single byte into the packet
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
// Write a string of characters into the packet // Write a string of characters into the packet
@ -75,7 +74,7 @@ public:
// Start processing the next available incoming packet // Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available // 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 // Number of bytes remaining in the current packet
virtual int available(); virtual int available();
// Read a single byte from the current packet // Read a single byte from the current packet
@ -91,9 +90,9 @@ public:
virtual void flush(); // Finish reading the current packet virtual void flush(); // Finish reading the current packet
// Return the IP address of the host who sent the current incoming 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 // Return the port of the host who sent the current incoming packet
uint16_t remotePort() { return _remotePort; }; virtual uint16_t remotePort() { return _remotePort; };
}; };
#endif #endif

View File

@ -1,22 +0,0 @@
#ifndef server_h
#define server_h
#include "Print.h"
class Client;
class Server :
public Print {
private:
uint16_t _port;
void accept();
public:
Server(uint16_t);
Client available();
void begin();
virtual size_t write(uint8_t);
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
};
#endif

View File

@ -39,7 +39,7 @@ IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library // Initialize the Ethernet server library
// with the IP address and port you want to use // with the IP address and port you want to use
// (port 80 is default for HTTP): // (port 80 is default for HTTP):
Server server(80); EthernetServer server(80);
//Sensor's memory register addresses: //Sensor's memory register addresses:
@ -96,7 +96,7 @@ void loop() {
} }
// listen for incoming Ethernet connections: // listen for incoming Ethernet connections:
listenForClients(); listenForEthernetClients();
} }
@ -124,9 +124,9 @@ void getData() {
Serial.println(" Pa"); Serial.println(" Pa");
} }
void listenForClients() { void listenForEthernetClients() {
// listen for incoming clients // listen for incoming clients
Client client = server.available(); EthernetClient client = server.available();
if (client) { if (client) {
Serial.println("Got a client"); Serial.println("Got a client");
// an http request ends with a blank line // an http request ends with a blank line

View File

@ -29,7 +29,7 @@ IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0); IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23 // telnet defaults to port 23
Server server(23); EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() { void setup() {
@ -43,7 +43,7 @@ void setup() {
void loop() { void loop() {
// wait for a new client: // wait for a new client:
Client client = server.available(); EthernetClient client = server.available();
// when the client sends the first byte, say hello: // when the client sends the first byte, say hello:
if (client) { if (client) {

View File

@ -24,7 +24,7 @@ byte mac[] = {
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP): // that you want to connect to (port 80 is default for HTTP):
Client client; EthernetClient client;
void setup() { void setup() {
// start the serial library: // start the serial library:

View File

@ -30,7 +30,7 @@ IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0); IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23 // telnet defaults to port 23
Server server(23); EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() { void setup() {
@ -59,7 +59,7 @@ void setup() {
void loop() { void loop() {
// wait for a new client: // wait for a new client:
Client client = server.available(); EthernetClient client = server.available();
// when the client sends the first byte, say hello: // when the client sends the first byte, say hello:
if (client) { if (client) {

View File

@ -25,7 +25,7 @@ char serverName[] = "www.google.com";
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP): // that you want to connect to (port 80 is default for HTTP):
Client client; EthernetClient client;
void setup() { void setup() {
// start the serial library: // start the serial library:

View File

@ -29,7 +29,7 @@ byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance: // initialize the library instance:
Client client; EthernetClient client;
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop boolean lastConnected = false; // state of the connection last time through the main loop

View File

@ -30,7 +30,7 @@ byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance: // initialize the library instance:
Client client; EthernetClient client;
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop boolean lastConnected = false; // state of the connection last time through the main loop

View File

@ -33,7 +33,7 @@ IPAddress server(1,1,1,1);
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet; // that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use port 10002): // if you're using Processing's ChatServer, use port 10002):
Client client; EthernetClient client;
void setup() { void setup() {
// start the Ethernet connection: // start the Ethernet connection:

View File

@ -33,7 +33,7 @@ byte mac[] = {
IPAddress ip(192,168,1,20); IPAddress ip(192,168,1,20);
// initialize the library instance: // initialize the library instance:
Client client; EthernetClient client;
const int requestInterval = 60000; // delay between requests const int requestInterval = 60000; // delay between requests

View File

@ -15,7 +15,7 @@
#include <SPI.h> // needed for Arduino versions later than 0018 #include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h> #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. // 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 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 // An EthernetUDP instance to let us send and receive packets over UDP
UDP Udp; EthernetUDP Udp;
void setup() { void setup() {
// start the Ethernet and UDP: // start the Ethernet and UDP:

View File

@ -18,7 +18,7 @@
#include <SPI.h> #include <SPI.h>
#include <Ethernet.h> #include <Ethernet.h>
#include <Udp.h> #include <EthernetUdp.h>
// Enter a MAC address for your controller below. // Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield // 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 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
UDP Udp; EthernetUDP Udp;
void setup() void setup()
{ {

View File

@ -23,7 +23,7 @@ IPAddress server(173,194,33,104); // Google
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP): // that you want to connect to (port 80 is default for HTTP):
Client client; EthernetClient client;
void setup() { void setup() {
// start the serial library: // start the serial library:

View File

@ -1,5 +1,5 @@
/* /*
Web Server Web Server
A simple web server that shows the value of the analog input pins. A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield. using an Arduino Wiznet Ethernet shield.
@ -26,7 +26,7 @@ IPAddress ip(192,168,1, 177);
// Initialize the Ethernet server library // Initialize the Ethernet server library
// with the IP address and port you want to use // with the IP address and port you want to use
// (port 80 is default for HTTP): // (port 80 is default for HTTP):
Server server(80); EthernetServer server(80);
void setup() void setup()
{ {
@ -38,7 +38,7 @@ void setup()
void loop() void loop()
{ {
// listen for incoming clients // listen for incoming clients
Client client = server.available(); EthernetClient client = server.available();
if (client) { if (client) {
// an http request ends with a blank line // an http request ends with a blank line
boolean currentLineIsBlank = true; boolean currentLineIsBlank = true;

View File

@ -7,8 +7,8 @@
####################################### #######################################
Ethernet KEYWORD1 Ethernet KEYWORD1
Client KEYWORD1 EthernetClient KEYWORD1
Server KEYWORD1 EthernetServer KEYWORD1
IPAddress KEYWORD1 IPAddress KEYWORD1
####################################### #######################################

View File

@ -324,6 +324,10 @@ private:
inline static void initSS() { DDRB |= _BV(4); }; inline static void initSS() { DDRB |= _BV(4); };
inline static void setSS() { PORTB &= ~_BV(4); }; inline static void setSS() { PORTB &= ~_BV(4); };
inline static void resetSS() { PORTB |= _BV(4); }; inline static void resetSS() { PORTB |= _BV(4); };
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
inline static void initSS() { DDRB |= _BV(0); };
inline static void setSS() { PORTB &= ~_BV(0); };
inline static void resetSS() { PORTB |= _BV(0); };
#else #else
inline static void initSS() { DDRB |= _BV(2); }; inline static void initSS() { DDRB |= _BV(2); };
inline static void setSS() { PORTB &= ~_BV(2); }; inline static void setSS() { PORTB &= ~_BV(2); };