diff --git a/hardware/arduino/cores/arduino/Client.h b/hardware/arduino/cores/arduino/Client.h new file mode 100644 index 000000000..ed9e9b48c --- /dev/null +++ b/hardware/arduino/cores/arduino/Client.h @@ -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 diff --git a/libraries/Ethernet/IPAddress.cpp b/hardware/arduino/cores/arduino/IPAddress.cpp similarity index 100% rename from libraries/Ethernet/IPAddress.cpp rename to hardware/arduino/cores/arduino/IPAddress.cpp diff --git a/libraries/Ethernet/IPAddress.h b/hardware/arduino/cores/arduino/IPAddress.h similarity index 100% rename from libraries/Ethernet/IPAddress.h rename to hardware/arduino/cores/arduino/IPAddress.h diff --git a/hardware/arduino/cores/arduino/Printable.h b/hardware/arduino/cores/arduino/Printable.h index e22e87ea2..d03c9af62 100644 --- a/hardware/arduino/cores/arduino/Printable.h +++ b/hardware/arduino/cores/arduino/Printable.h @@ -20,6 +20,8 @@ #ifndef Printable_h #define Printable_h +#include + class Print; /** 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 Print::print and Print::println methods. */ + class Printable { public: diff --git a/hardware/arduino/cores/arduino/Server.h b/hardware/arduino/cores/arduino/Server.h new file mode 100644 index 000000000..edab726be --- /dev/null +++ b/hardware/arduino/cores/arduino/Server.h @@ -0,0 +1,9 @@ +#ifndef server_h +#define server_h + +class Server { +public: + virtual void begin() =0; +}; + +#endif diff --git a/hardware/arduino/cores/arduino/Udp.h b/hardware/arduino/cores/arduino/Udp.h new file mode 100644 index 000000000..1fb9cd3cf --- /dev/null +++ b/hardware/arduino/cores/arduino/Udp.h @@ -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 +#include + +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 diff --git a/hardware/arduino/cores/arduino/new.cpp b/hardware/arduino/cores/arduino/new.cpp new file mode 100644 index 000000000..0f6d4220e --- /dev/null +++ b/hardware/arduino/cores/arduino/new.cpp @@ -0,0 +1,18 @@ +#include + +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) {}; + diff --git a/hardware/arduino/cores/arduino/new.h b/hardware/arduino/cores/arduino/new.h new file mode 100644 index 000000000..cd940ce8b --- /dev/null +++ b/hardware/arduino/cores/arduino/new.h @@ -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 + +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 + diff --git a/libraries/Ethernet/Dhcp.cpp b/libraries/Ethernet/Dhcp.cpp index c20d2e61d..29db89098 100755 --- a/libraries/Ethernet/Dhcp.cpp +++ b/libraries/Ethernet/Dhcp.cpp @@ -271,11 +271,19 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr case routersOnSubnet : opt_len = _dhcpUdpSocket.read(); _dhcpUdpSocket.read(_dhcpGatewayIp, 4); + for (int i = 0; i < opt_len-4; i++) + { + _dhcpUdpSocket.read(); + } break; case dns : opt_len = _dhcpUdpSocket.read(); _dhcpUdpSocket.read(_dhcpDnsServerIp, 4); + for (int i = 0; i < opt_len-4; i++) + { + _dhcpUdpSocket.read(); + } break; case dhcpServerIdentifier : diff --git a/libraries/Ethernet/Dhcp.h b/libraries/Ethernet/Dhcp.h index c0034940c..f87719959 100755 --- a/libraries/Ethernet/Dhcp.h +++ b/libraries/Ethernet/Dhcp.h @@ -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); diff --git a/libraries/Ethernet/Dns.cpp b/libraries/Ethernet/Dns.cpp index a049c7633..86fd0177a 100644 --- a/libraries/Ethernet/Dns.cpp +++ b/libraries/Ethernet/Dns.cpp @@ -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" diff --git a/libraries/Ethernet/Dns.h b/libraries/Ethernet/Dns.h index 9582351f2..d4f49769b 100644 --- a/libraries/Ethernet/Dns.h +++ b/libraries/Ethernet/Dns.h @@ -5,7 +5,7 @@ #ifndef DNSClient_h #define DNSClient_h -#include +#include class DNSClient { @@ -35,7 +35,7 @@ protected: IPAddress iDNSServer; uint16_t iRequestId; - UDP iUdp; + EthernetUDP iUdp; }; #endif diff --git a/libraries/Ethernet/Ethernet.cpp b/libraries/Ethernet/Ethernet.cpp index 937f5b4f9..c298f3d17 100644 --- a/libraries/Ethernet/Ethernet.cpp +++ b/libraries/Ethernet/Ethernet.cpp @@ -33,27 +33,37 @@ int EthernetClass::begin(uint8_t *mac_address) } 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 // but with last octet being '1' IPAddress gateway = local_ip; 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); - 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.setMACAddress(mac); W5100.setIPAddress(local_ip._address); W5100.setGatewayIp(gateway._address); W5100.setSubnetMask(subnet._address); + _dnsServerAddress = dns_server; } IPAddress EthernetClass::localIP() diff --git a/libraries/Ethernet/Ethernet.h b/libraries/Ethernet/Ethernet.h index fdf0b7f39..c916ddae5 100644 --- a/libraries/Ethernet/Ethernet.h +++ b/libraries/Ethernet/Ethernet.h @@ -4,8 +4,8 @@ #include //#include "w5100.h" #include "IPAddress.h" -#include "Client.h" -#include "Server.h" +#include "EthernetClient.h" +#include "EthernetServer.h" #define MAX_SOCK_NUM 4 @@ -20,16 +20,17 @@ public: // Returns 0 if the DHCP configuration failed, and 1 if it succeeded int begin(uint8_t *mac_address); 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 gateway, IPAddress subnet); + void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server); + 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 subnetMask(); IPAddress gatewayIP(); IPAddress dnsServerIP(); - friend class Client; - friend class Server; + friend class EthernetClient; + friend class EthernetServer; }; extern EthernetClass Ethernet; diff --git a/libraries/Ethernet/Client.cpp b/libraries/Ethernet/EthernetClient.cpp similarity index 73% rename from libraries/Ethernet/Client.cpp rename to libraries/Ethernet/EthernetClient.cpp index 3c1c2503b..42c2c6cc1 100644 --- a/libraries/Ethernet/Client.cpp +++ b/libraries/Ethernet/EthernetClient.cpp @@ -8,19 +8,19 @@ extern "C" { #include "Arduino.h" #include "Ethernet.h" -#include "Client.h" -#include "Server.h" +#include "EthernetClient.h" +#include "EthernetServer.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 int ret = 0; 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) return 0; @@ -54,7 +54,7 @@ int Client::connect(IPAddress ip, uint16_t port) { if (_srcport == 0) _srcport = 1024; socket(_sock, SnMR::TCP, _srcport, 0); - if (!::connect(_sock, ip.raw_address(), port)) { + if (!::connect(_sock, rawIPAddress(ip), port)) { _sock = MAX_SOCK_NUM; return 0; } @@ -70,15 +70,15 @@ int Client::connect(IPAddress ip, uint16_t port) { return 1; } -size_t Client::write(uint8_t b) { +size_t EthernetClient::write(uint8_t b) { 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)); } -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) { setWriteError(); return 0; @@ -90,13 +90,13 @@ size_t Client::write(const uint8_t *buf, size_t size) { return size; } -int Client::available() { +int EthernetClient::available() { if (_sock != MAX_SOCK_NUM) return W5100.getRXReceivedSize(_sock); return 0; } -int Client::read() { +int EthernetClient::read() { uint8_t b; 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); } -int Client::peek() { +int EthernetClient::peek() { uint8_t b; // Unlike recv, peek doesn't check to see if there's any data available, so we must if (!available()) @@ -123,12 +123,12 @@ int Client::peek() { return b; } -void Client::flush() { +void EthernetClient::flush() { while (available()) read(); } -void Client::stop() { +void EthernetClient::stop() { if (_sock == MAX_SOCK_NUM) return; @@ -148,7 +148,7 @@ void Client::stop() { _sock = MAX_SOCK_NUM; } -uint8_t Client::connected() { +uint8_t EthernetClient::connected() { if (_sock == MAX_SOCK_NUM) return 0; uint8_t s = status(); @@ -156,14 +156,14 @@ uint8_t Client::connected() { (s == SnSR::CLOSE_WAIT && !available())); } -uint8_t Client::status() { +uint8_t EthernetClient::status() { if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; return W5100.readSnSR(_sock); } // 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; } diff --git a/libraries/Ethernet/Client.h b/libraries/Ethernet/EthernetClient.h similarity index 50% rename from libraries/Ethernet/Client.h rename to libraries/Ethernet/EthernetClient.h index a8dd6fa42..f68a3b4d9 100644 --- a/libraries/Ethernet/Client.h +++ b/libraries/Ethernet/EthernetClient.h @@ -1,17 +1,19 @@ -#ifndef client_h -#define client_h +#ifndef ethernetclient_h +#define ethernetclient_h #include "Arduino.h" #include "Print.h" +#include "Client.h" +#include "IPAddress.h" -class Client : public Stream { +class EthernetClient : public Client { public: - Client(); - Client(uint8_t sock); + EthernetClient(); + EthernetClient(uint8_t sock); uint8_t status(); - int connect(IPAddress ip, uint16_t port); - int connect(const char *host, uint16_t port); + virtual int connect(IPAddress ip, uint16_t port); + virtual int connect(const char *host, uint16_t port); virtual size_t write(uint8_t); virtual size_t write(const char *str); 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 peek(); virtual void flush(); - void stop(); - uint8_t connected(); - operator bool(); + virtual void stop(); + virtual uint8_t connected(); + virtual operator bool(); - friend class Server; + friend class EthernetServer; private: static uint16_t _srcport; diff --git a/libraries/Ethernet/Server.cpp b/libraries/Ethernet/EthernetServer.cpp similarity index 72% rename from libraries/Ethernet/Server.cpp rename to libraries/Ethernet/EthernetServer.cpp index 1ac75d507..9ae86f39e 100644 --- a/libraries/Ethernet/Server.cpp +++ b/libraries/Ethernet/EthernetServer.cpp @@ -5,18 +5,18 @@ extern "C" { } #include "Ethernet.h" -#include "Client.h" -#include "Server.h" +#include "EthernetClient.h" +#include "EthernetServer.h" -Server::Server(uint16_t port) +EthernetServer::EthernetServer(uint16_t port) { _port = port; } -void Server::begin() +void EthernetServer::begin() { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { - Client client(sock); + EthernetClient client(sock); if (client.status() == SnSR::CLOSED) { socket(sock, SnMR::TCP, _port, 0); listen(sock); @@ -26,12 +26,12 @@ void Server::begin() } } -void Server::accept() +void EthernetServer::accept() { int listening = 0; for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { - Client client(sock); + EthernetClient client(sock); if (EthernetClass::_server_port[sock] == _port) { if (client.status() == SnSR::LISTEN) { @@ -48,12 +48,12 @@ void Server::accept() } } -Client Server::available() +EthernetClient EthernetServer::available() { accept(); for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { - Client client(sock); + EthernetClient client(sock); if (EthernetClass::_server_port[sock] == _port && (client.status() == SnSR::ESTABLISHED || 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); } -size_t Server::write(const char *str) +size_t EthernetServer::write(const char *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; accept(); for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { - Client client(sock); + EthernetClient client(sock); if (EthernetClass::_server_port[sock] == _port && client.status() == SnSR::ESTABLISHED) { diff --git a/libraries/Ethernet/EthernetServer.h b/libraries/Ethernet/EthernetServer.h new file mode 100644 index 000000000..ced5ed649 --- /dev/null +++ b/libraries/Ethernet/EthernetServer.h @@ -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 diff --git a/libraries/Ethernet/Udp.cpp b/libraries/Ethernet/EthernetUdp.cpp similarity index 83% rename from libraries/Ethernet/Udp.cpp rename to libraries/Ethernet/EthernetUdp.cpp index a1244ffb9..9ca650986 100644 --- a/libraries/Ethernet/Udp.cpp +++ b/libraries/Ethernet/EthernetUdp.cpp @@ -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()) { diff --git a/libraries/Ethernet/Udp.h b/libraries/Ethernet/EthernetUdp.h similarity index 87% rename from libraries/Ethernet/Udp.h rename to libraries/Ethernet/EthernetUdp.h index 59238c1ae..64e30275f 100644 --- a/libraries/Ethernet/Udp.h +++ b/libraries/Ethernet/EthernetUdp.h @@ -34,15 +34,14 @@ * bjoern@cs.stanford.edu 12/30/2008 */ -#ifndef udp_h -#define udp_h +#ifndef ethernetudp_h +#define ethernetudp_h -#include -#include +#include #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 diff --git a/libraries/Ethernet/Server.h b/libraries/Ethernet/Server.h deleted file mode 100644 index fa4a56d88..000000000 --- a/libraries/Ethernet/Server.h +++ /dev/null @@ -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 diff --git a/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino b/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino index 3f43d96db..bfbcb6d4a 100644 --- a/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino +++ b/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino @@ -39,7 +39,7 @@ IPAddress subnet(255, 255, 255, 0); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): -Server server(80); +EthernetServer server(80); //Sensor's memory register addresses: @@ -96,7 +96,7 @@ void loop() { } // listen for incoming Ethernet connections: - listenForClients(); + listenForEthernetClients(); } @@ -124,9 +124,9 @@ void getData() { Serial.println(" Pa"); } -void listenForClients() { +void listenForEthernetClients() { // listen for incoming clients - Client client = server.available(); + EthernetClient client = server.available(); if (client) { Serial.println("Got a client"); // an http request ends with a blank line diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino index 8267a5dd4..9f819fd5a 100644 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino @@ -29,7 +29,7 @@ IPAddress gateway(192,168,1, 1); IPAddress subnet(255, 255, 0, 0); // 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 void setup() { @@ -43,7 +43,7 @@ void setup() { void loop() { // wait for a new client: - Client client = server.available(); + EthernetClient client = server.available(); // when the client sends the first byte, say hello: if (client) { diff --git a/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino b/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino index 50a557dcc..630dd1711 100644 --- a/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino +++ b/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino @@ -24,7 +24,7 @@ byte mac[] = { // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): -Client client; +EthernetClient client; void setup() { // start the serial library: diff --git a/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino b/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino index c3e581387..50820542c 100644 --- a/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino +++ b/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino @@ -30,7 +30,7 @@ IPAddress gateway(192,168,1, 1); IPAddress subnet(255, 255, 0, 0); // 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 void setup() { @@ -59,7 +59,7 @@ void setup() { void loop() { // wait for a new client: - Client client = server.available(); + EthernetClient client = server.available(); // when the client sends the first byte, say hello: if (client) { diff --git a/libraries/Ethernet/examples/DnsWebClient/DnsWebClient.ino b/libraries/Ethernet/examples/DnsWebClient/DnsWebClient.ino index 7bec73a83..5c7a53a65 100644 --- a/libraries/Ethernet/examples/DnsWebClient/DnsWebClient.ino +++ b/libraries/Ethernet/examples/DnsWebClient/DnsWebClient.ino @@ -25,7 +25,7 @@ char serverName[] = "www.google.com"; // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): -Client client; +EthernetClient client; void setup() { // start the serial library: diff --git a/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino b/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino index af9bf1f05..e626113b0 100644 --- a/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino +++ b/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino @@ -29,7 +29,7 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // initialize the library instance: -Client client; +EthernetClient client; 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 diff --git a/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino b/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino index e6dbf09ea..4b97c4175 100644 --- a/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino +++ b/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino @@ -30,7 +30,7 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // initialize the library instance: -Client client; +EthernetClient client; 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 diff --git a/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino b/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino index 7502d218d..5cf1ad875 100644 --- a/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino +++ b/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino @@ -33,7 +33,7 @@ IPAddress server(1,1,1,1); // with the IP address and port of the server // that you want to connect to (port 23 is default for telnet; // if you're using Processing's ChatServer, use port 10002): -Client client; +EthernetClient client; void setup() { // start the Ethernet connection: diff --git a/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino b/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino index 399e76bc3..f113e17b9 100644 --- a/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino +++ b/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino @@ -33,7 +33,7 @@ byte mac[] = { IPAddress ip(192,168,1,20); // initialize the library instance: -Client client; +EthernetClient client; const int requestInterval = 60000; // delay between requests diff --git a/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino b/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino index 081d69114..4d4045cac 100644 --- a/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino +++ b/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino @@ -15,7 +15,7 @@ #include // needed for Arduino versions later than 0018 #include -#include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 +#include // 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: diff --git a/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino b/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino index 7c2d3ea9e..b4e24b8ce 100644 --- a/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino +++ b/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino @@ -18,7 +18,7 @@ #include #include -#include +#include // 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() { diff --git a/libraries/Ethernet/examples/WebClient/WebClient.ino b/libraries/Ethernet/examples/WebClient/WebClient.ino index 646c3aadd..18068541f 100644 --- a/libraries/Ethernet/examples/WebClient/WebClient.ino +++ b/libraries/Ethernet/examples/WebClient/WebClient.ino @@ -23,7 +23,7 @@ IPAddress server(173,194,33,104); // Google // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): -Client client; +EthernetClient client; void setup() { // start the serial library: diff --git a/libraries/Ethernet/examples/WebServer/WebServer.ino b/libraries/Ethernet/examples/WebServer/WebServer.ino index c69a56a40..6837f8320 100644 --- a/libraries/Ethernet/examples/WebServer/WebServer.ino +++ b/libraries/Ethernet/examples/WebServer/WebServer.ino @@ -1,5 +1,5 @@ /* - Web Server + Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. @@ -26,7 +26,7 @@ IPAddress ip(192,168,1, 177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): -Server server(80); +EthernetServer server(80); void setup() { @@ -38,7 +38,7 @@ void setup() void loop() { // listen for incoming clients - Client client = server.available(); + EthernetClient client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; diff --git a/libraries/Ethernet/keywords.txt b/libraries/Ethernet/keywords.txt index 7fdcedf09..6b37cbe05 100644 --- a/libraries/Ethernet/keywords.txt +++ b/libraries/Ethernet/keywords.txt @@ -7,8 +7,8 @@ ####################################### Ethernet KEYWORD1 -Client KEYWORD1 -Server KEYWORD1 +EthernetClient KEYWORD1 +EthernetServer KEYWORD1 IPAddress KEYWORD1 ####################################### diff --git a/libraries/Ethernet/utility/w5100.h b/libraries/Ethernet/utility/w5100.h index 9872c7ccb..35d23ed41 100755 --- a/libraries/Ethernet/utility/w5100.h +++ b/libraries/Ethernet/utility/w5100.h @@ -324,6 +324,10 @@ private: inline static void initSS() { DDRB |= _BV(4); }; inline static void setSS() { 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 inline static void initSS() { DDRB |= _BV(2); }; inline static void setSS() { PORTB &= ~_BV(2); };