├── src ├── util.h ├── EthernetServer.h ├── EthernetClient.h ├── Twitter.h ├── Dns.h ├── utility │ ├── socket.h │ ├── w5500.cpp │ ├── w5100.cpp │ ├── w5200.cpp │ ├── socket.cpp │ ├── w5200.h │ ├── w5100.h │ └── w5500.h ├── EthernetIndustruino.h ├── EthernetServer.cpp ├── Twitter.cpp ├── EthernetClient.cpp ├── Dhcp.h ├── EthernetUdp.h ├── EthernetIndustruino.cpp ├── EthernetUdp.cpp ├── Dns.cpp └── Dhcp.cpp ├── .gitignore ├── library.properties ├── README.md └── examples ├── fram └── fram.ino ├── WebClient └── WebClient.ino ├── WebServer └── WebServer.ino └── Emon └── Emon.ino /src/util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H 3 | 4 | #define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) ) 5 | #define ntohs(x) htons(x) 6 | 7 | #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ 8 | ((x)<< 8 & 0x00FF0000UL) | \ 9 | ((x)>> 8 & 0x0000FF00UL) | \ 10 | ((x)>>24 & 0x000000FFUL) ) 11 | #define ntohl(x) htonl(x) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /src/EthernetServer.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetserver_h 2 | #define ethernetserver_h 3 | 4 | #include "Server.h" 5 | 6 | class EthernetClient; 7 | 8 | class EthernetServer : 9 | public Server { 10 | private: 11 | uint16_t _port; 12 | void accept(); 13 | public: 14 | EthernetServer(uint16_t); 15 | EthernetClient available(); 16 | virtual void begin(); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | using Print::write; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .AppleDouble 3 | .LSOverride 4 | .directory 5 | 6 | # Icon must end with two \r. 7 | Icon 8 | 9 | 10 | # Thumbnails 11 | ._* 12 | 13 | # Files that might appear on external disk 14 | .Spotlight-V100 15 | .Trashes 16 | 17 | # Windows image file caches 18 | Thumbs.db 19 | ehthumbs.db 20 | 21 | # Folder config file 22 | Desktop.ini 23 | 24 | # Recycle Bin used on file shares 25 | $RECYCLE.BIN/ 26 | 27 | # Windows Installer files 28 | *.cab 29 | *.msi 30 | *.msm 31 | *.msp 32 | 33 | *~ 34 | .~* 35 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=EthernetIndustruino 2 | version=1.0.0 3 | author=Industruino and others 4 | maintainer=Industruino 5 | sentence=Library for the Industruino Ethernet module 6 | paragraph=With this library you can use the Industruino Ethernet module to communicate over Ethernet and to connect to Internet. The library provides both client and server functionalities, allows to connect to a local network (also with DHCP) and to resolve DNS. 7 | category=Communication 8 | url=https://github.com/Industruino/Indio 9 | architectures=avr 10 | includes=EthernetIndustruino.h,SPI.h 11 | -------------------------------------------------------------------------------- /src/EthernetClient.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetclient_h 2 | #define ethernetclient_h 3 | #include "Arduino.h" 4 | #include "Print.h" 5 | #include "Client.h" 6 | #include "IPAddress.h" 7 | 8 | class EthernetClient : public Client { 9 | 10 | public: 11 | EthernetClient(); 12 | EthernetClient(uint8_t sock); 13 | 14 | uint8_t status(); 15 | virtual int connect(IPAddress ip, uint16_t port); 16 | virtual int connect(const char *host, uint16_t port); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | virtual int available(); 20 | virtual int read(); 21 | virtual int read(uint8_t *buf, size_t size); 22 | virtual int peek(); 23 | virtual void flush(); 24 | virtual void stop(); 25 | virtual uint8_t connected(); 26 | virtual operator bool(); 27 | 28 | friend class EthernetServer; 29 | 30 | using Print::write; 31 | 32 | private: 33 | static uint16_t _srcport; 34 | uint8_t _sock; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /src/Twitter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter.cpp - Arduino library to Post messages to Twitter using OAuth. 3 | Copyright (c) NeoCat 2010-2011. All right reserved. 4 | 5 | This library is distributed in the hope that it will be useful, 6 | but WITHOUT ANY WARRANTY; without even the implied warranty of 7 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | */ 9 | 10 | // ver1.2 - Use to support IDE 0019 or later 11 | // ver1.3 - Support IDE 1.0 12 | 13 | #ifndef TWITTER_H 14 | #define TWITTER_H 15 | 16 | #include 17 | #include 18 | #if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later 19 | #include 20 | #endif 21 | #include 22 | #if defined(ARDUINO) && ARDUINO < 100 // earlier than Arduino 1.0 23 | #include 24 | #endif 25 | 26 | class Twitter 27 | { 28 | private: 29 | uint8_t parseStatus; 30 | int statusCode; 31 | const char *token; 32 | #if defined(ARDUINO) && ARDUINO < 100 33 | Client client; 34 | #else 35 | EthernetClient client; 36 | #endif 37 | public: 38 | Twitter(const char *user_and_passwd); 39 | 40 | bool post(const char *msg); 41 | bool checkStatus(Print *debug = NULL); 42 | int wait(Print *debug = NULL); 43 | int status(void) { return statusCode; } 44 | }; 45 | 46 | #endif //TWITTER_H 47 | -------------------------------------------------------------------------------- /src/Dns.h: -------------------------------------------------------------------------------- 1 | // Arduino DNS client for WizNet5100-based Ethernet shield 2 | // (c) Copyright 2009-2010 MCQN Ltd. 3 | // Released under Apache License, version 2.0 4 | 5 | #ifndef DNSClient_h 6 | #define DNSClient_h 7 | 8 | #include 9 | 10 | class DNSClient 11 | { 12 | public: 13 | // ctor 14 | void begin(const IPAddress& aDNSServer); 15 | 16 | /** Convert a numeric IP address string into a four-byte IP address. 17 | @param aIPAddrString IP address to convert 18 | @param aResult IPAddress structure to store the returned IP address 19 | @result 1 if aIPAddrString was successfully converted to an IP address, 20 | else error code 21 | */ 22 | int inet_aton(const char *aIPAddrString, IPAddress& aResult); 23 | 24 | /** Resolve the given hostname to an IP address. 25 | @param aHostname Name to be resolved 26 | @param aResult IPAddress structure to store the returned IP address 27 | @result 1 if aIPAddrString was successfully converted to an IP address, 28 | else error code 29 | */ 30 | int getHostByName(const char* aHostname, IPAddress& aResult); 31 | 32 | protected: 33 | uint16_t BuildRequest(const char* aName); 34 | uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress); 35 | 36 | IPAddress iDNSServer; 37 | uint16_t iRequestId; 38 | EthernetUDP iUdp; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EthernetIndustruino 2 | If you are using the Industruino Ethernet module, you will need this library which is based on the standard Arduino Ethernet library. The Ethernet module is connected over SPI, so we also need the SPI library. 3 | ``` 4 | #include 5 | #include 6 | ``` 7 | The Ethernet module also includes FRAM; see the example in the library on how to use this. If you want to use the FRAM together with the Ethernet, there is no need to include the SPI settings as in the FRAM example, because this is taken care of in the Ethernet library. So you can just include the 2 libraries with the above 2 lines; DO NOT include the SPI settings as in the FRAM example: 8 | ``` 9 | //Setting up the SPI bus -- NO NEED when using the EthernetIndustruino library 10 | SPI.begin(); 11 | SPI.setDataMode(SPI_MODE0); 12 | SPI.setBitOrder(MSBFIRST); 13 | SPI.setClockDivider(SPI_CLOCK_DIV2); 14 | ``` 15 | 16 | When using the Ethernet module with the Industruino PROTO, it is important to be aware of the I/O pins it is using, and which should not be used for other I/O functions: 17 | 18 | | IDC pin number | Module function | Arduino pin | Default connected | Required for standard functions | 19 | | --- | --- | --- | --- | --- | 20 | | 1 |MISO | D14 | yes | yes | 21 | | 2 | +5V | +5V | yes | yes | 22 | | 3 | SCLK | D15/SCLK | yes | yes | 23 | | 4 | MOSI | D16/MOSI | yes | yes | 24 | | 5 | Ethernet CS | D10 | yes | yes | 25 | | 6 | GND | GND | yes | yes | 26 | | 7 | SD CS | D4 | yes | yes | 27 | | 8 | Ethernet ext reset | D5 | no | no | 28 | | 9 | Ethernet IRQ | D7 | yes | no | 29 | | 10 | FRAM CS | D6 | yes | yes | 30 | | 11 | / | D0/RX | no | no | 31 | | 12 | / | D1/TX | no | no | 32 | | 13 | / | D2/SDA | no | no | 33 | | 14 | / | D3/SCL | no | no | 34 | -------------------------------------------------------------------------------- /src/utility/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "w5100.h" 5 | 6 | extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode) 7 | extern void close(SOCKET s); // Close socket 8 | extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection) 9 | extern void disconnect(SOCKET s); // disconnect the connection 10 | extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection) 11 | extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP) 12 | extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP) 13 | extern uint16_t peek(SOCKET s, uint8_t *buf); 14 | extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW) 15 | extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW) 16 | 17 | extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len); 18 | 19 | // Functions to allow buffered UDP send (i.e. where the UDP datagram is built up over a 20 | // number of calls before being sent 21 | /* 22 | @brief This function sets up a UDP datagram, the data for which will be provided by one 23 | or more calls to bufferData and then finally sent with sendUDP. 24 | @return 1 if the datagram was successfully set up, or 0 if there was an error 25 | */ 26 | extern int startUDP(SOCKET s, uint8_t* addr, uint16_t port); 27 | /* 28 | @brief This function copies up to len bytes of data from buf into a UDP datagram to be 29 | sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls. 30 | @return Number of bytes successfully buffered 31 | */ 32 | uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len); 33 | /* 34 | @brief Send a UDP datagram built up from a sequence of startUDP followed by one or more 35 | calls to bufferData. 36 | @return 1 if the datagram was successfully sent, or 0 if there was an error 37 | */ 38 | int sendUDP(SOCKET s); 39 | 40 | #endif 41 | /* _SOCKET_H_ */ 42 | -------------------------------------------------------------------------------- /src/EthernetIndustruino.h: -------------------------------------------------------------------------------- 1 | /* 2 | modified 12 Aug 2013 3 | by Soohwan Kim (suhwan@wiznet.co.kr) 4 | */ 5 | #ifndef ethernet_h 6 | #define ethernet_h 7 | 8 | #include 9 | #include "utility/w5100.h" 10 | #include "IPAddress.h" 11 | #include "EthernetClient.h" 12 | #include "EthernetServer.h" 13 | #include "Dhcp.h" 14 | 15 | 16 | 17 | class EthernetClass { 18 | private: 19 | IPAddress _dnsServerAddress; 20 | DhcpClass* _dhcp; 21 | public: 22 | static uint8_t _state[MAX_SOCK_NUM]; 23 | static uint16_t _server_port[MAX_SOCK_NUM]; 24 | 25 | #if defined(WIZ550io_WITH_MACADDRESS) 26 | // Initialize function when use the ioShield serise (included WIZ550io) 27 | // WIZ550io has a MAC address which is written after reset. 28 | // Default IP, Gateway and subnet address are also writen. 29 | // so, It needs some initial time. please refer WIZ550io Datasheet in details. 30 | int begin(uint8_t *mac_address); 31 | void begin(uint8_t *mac_address, IPAddress local_ip); 32 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server); 33 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway); 34 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); 35 | #else 36 | // Initialize the Ethernet shield to use the provided MAC address and gain the rest of the 37 | // configuration through DHCP. 38 | // Returns 0 if the DHCP configuration failed, and 1 if it succeeded 39 | int begin(uint8_t *mac_address); 40 | void begin(uint8_t *mac_address, IPAddress local_ip); 41 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server); 42 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway); 43 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); 44 | 45 | #endif 46 | 47 | int maintain(); 48 | 49 | IPAddress localIP(); 50 | IPAddress subnetMask(); 51 | IPAddress gatewayIP(); 52 | IPAddress dnsServerIP(); 53 | 54 | friend class EthernetClient; 55 | friend class EthernetServer; 56 | }; 57 | 58 | extern EthernetClass Ethernet; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/EthernetServer.cpp: -------------------------------------------------------------------------------- 1 | #include "utility/w5100.h" 2 | #include "utility/socket.h" 3 | extern "C" { 4 | #include "string.h" 5 | } 6 | 7 | #include "EthernetIndustruino.h" 8 | #include "EthernetClient.h" 9 | #include "EthernetServer.h" 10 | 11 | EthernetServer::EthernetServer(uint16_t port) 12 | { 13 | _port = port; 14 | } 15 | 16 | void EthernetServer::begin() 17 | { 18 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 19 | EthernetClient client(sock); 20 | if (client.status() == SnSR::CLOSED) { 21 | socket(sock, SnMR::TCP, _port, 0); 22 | listen(sock); 23 | EthernetClass::_server_port[sock] = _port; 24 | break; 25 | } 26 | } 27 | } 28 | 29 | void EthernetServer::accept() 30 | { 31 | int listening = 0; 32 | 33 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 34 | EthernetClient client(sock); 35 | 36 | if (EthernetClass::_server_port[sock] == _port) { 37 | if (client.status() == SnSR::LISTEN) { 38 | listening = 1; 39 | } 40 | else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) { 41 | client.stop(); 42 | } 43 | } 44 | } 45 | 46 | if (!listening) { 47 | begin(); 48 | } 49 | } 50 | 51 | EthernetClient EthernetServer::available() 52 | { 53 | accept(); 54 | 55 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 56 | EthernetClient client(sock); 57 | if (EthernetClass::_server_port[sock] == _port && 58 | (client.status() == SnSR::ESTABLISHED || 59 | client.status() == SnSR::CLOSE_WAIT)) { 60 | if (client.available()) { 61 | // XXX: don't always pick the lowest numbered socket. 62 | return client; 63 | } 64 | } 65 | } 66 | 67 | return EthernetClient(MAX_SOCK_NUM); 68 | } 69 | 70 | size_t EthernetServer::write(uint8_t b) 71 | { 72 | return write(&b, 1); 73 | } 74 | 75 | size_t EthernetServer::write(const uint8_t *buffer, size_t size) 76 | { 77 | size_t n = 0; 78 | 79 | accept(); 80 | 81 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 82 | EthernetClient client(sock); 83 | 84 | if (EthernetClass::_server_port[sock] == _port && 85 | client.status() == SnSR::ESTABLISHED) { 86 | n += client.write(buffer, size); 87 | } 88 | } 89 | 90 | return n; 91 | } 92 | -------------------------------------------------------------------------------- /src/Twitter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter.cpp - Arduino library to Post messages to Twitter using OAuth. 3 | Copyright (c) NeoCat 2010-2011. All right reserved. 4 | 5 | This library is distributed in the hope that it will be useful, 6 | but WITHOUT ANY WARRANTY; without even the implied warranty of 7 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | */ 9 | 10 | // ver1.2 - Use 11 | // ver1.3 - Support IDE 1.0 12 | 13 | #include 14 | #include "Twitter.h" 15 | 16 | #define LIB_DOMAIN "arduino-tweet.appspot.com" 17 | 18 | #if defined(ARDUINO) && ARDUINO < 100 19 | static uint8_t server[] = {0,0,0,0}; // IP address of LIB_DOMAIN 20 | Twitter::Twitter(const char *token) : client(server, 80), token(token) 21 | { 22 | } 23 | #else 24 | Twitter::Twitter(const char *token) : token(token) 25 | { 26 | } 27 | #endif 28 | 29 | bool Twitter::post(const char *msg) 30 | { 31 | #if defined(ARDUINO) && ARDUINO < 100 32 | DNSError err = EthernetDNS.resolveHostName(LIB_DOMAIN, server); 33 | if (err != DNSSuccess) { 34 | return false; 35 | } 36 | #endif 37 | parseStatus = 0; 38 | statusCode = 0; 39 | #if defined(ARDUINO) && ARDUINO < 100 40 | if (client.connect()) { 41 | #else 42 | if (client.connect(LIB_DOMAIN, 80)) { 43 | #endif 44 | client.println("POST http://" LIB_DOMAIN "/update HTTP/1.0"); 45 | client.print("Content-Length: "); 46 | client.println(strlen(msg)+strlen(token)+14); 47 | client.println(); 48 | client.print("token="); 49 | client.print(token); 50 | client.print("&status="); 51 | client.println(msg); 52 | } else { 53 | return false; 54 | } 55 | return true; 56 | } 57 | 58 | bool Twitter::checkStatus(Print *debug) 59 | { 60 | if (!client.connected()) { 61 | if (debug) 62 | while(client.available()) 63 | debug->print((char)client.read()); 64 | client.flush(); 65 | client.stop(); 66 | return false; 67 | } 68 | if (!client.available()) 69 | return true; 70 | char c = client.read(); 71 | if (debug) 72 | debug->print(c); 73 | switch(parseStatus) { 74 | case 0: 75 | if (c == ' ') parseStatus++; break; // skip "HTTP/1.1 " 76 | case 1: 77 | if (c >= '0' && c <= '9') { 78 | statusCode *= 10; 79 | statusCode += c - '0'; 80 | } else { 81 | parseStatus++; 82 | } 83 | } 84 | return true; 85 | } 86 | 87 | int Twitter::wait(Print *debug) 88 | { 89 | while (checkStatus(debug)); 90 | return statusCode; 91 | } 92 | -------------------------------------------------------------------------------- /examples/fram/fram.ino: -------------------------------------------------------------------------------- 1 | //this sketch writes the string "This is a test" to the FRAM memory, reads it back and post the result to the serial terminal. 2 | 3 | #include 4 | 5 | const byte CMD_WREN = 0x06; //0000 0110 Set Write Enable Latch 6 | const byte CMD_WRDI = 0x04; //0000 0100 Write Disable 7 | const byte CMD_RDSR = 0x05; //0000 0101 Read Status Register 8 | const byte CMD_WRSR = 0x01; //0000 0001 Write Status Register 9 | const byte CMD_READ = 0x03; //0000 0011 Read Memory Data 10 | const byte CMD_WRITE = 0x02; //0000 0010 Write Memory Data 11 | 12 | const int FRAM_CS1 = 6; //chip select 1 13 | 14 | /** 15 | * Write to FRAM (assuming 2 FM25C160 are used) 16 | * addr: starting address 17 | * buf: pointer to data 18 | * count: data length. 19 | * If this parameter is omitted, it is defaulted to one byte. 20 | * returns: 0 operation is successful 21 | * 1 address out of range 22 | */ 23 | int FRAMWrite(int addr, byte *buf, int count=1) 24 | { 25 | 26 | 27 | if (addr > 0x7ff) return -1; 28 | 29 | byte addrMSB = (addr >> 8) & 0xff; 30 | byte addrLSB = addr & 0xff; 31 | 32 | digitalWrite(FRAM_CS1, LOW); 33 | SPI.transfer(CMD_WREN); //write enable 34 | digitalWrite(FRAM_CS1, HIGH); 35 | 36 | digitalWrite(FRAM_CS1, LOW); 37 | SPI.transfer(CMD_WRITE); //write command 38 | SPI.transfer(addrMSB); 39 | SPI.transfer(addrLSB); 40 | 41 | for (int i = 0;i < count;i++) SPI.transfer(buf[i]); 42 | 43 | digitalWrite(FRAM_CS1, HIGH); 44 | 45 | return 0; 46 | } 47 | 48 | /** 49 | * Read from FRAM (assuming 2 FM25C160 are used) 50 | * addr: starting address 51 | * buf: pointer to data 52 | * count: data length. 53 | * If this parameter is omitted, it is defaulted to one byte. 54 | * returns: 0 operation is successful 55 | * 1 address out of range 56 | */ 57 | int FRAMRead(int addr, byte *buf, int count=1) 58 | { 59 | 60 | if (addr > 0x7ff) return -1; 61 | 62 | byte addrMSB = (addr >> 8) & 0xff; 63 | byte addrLSB = addr & 0xff; 64 | 65 | digitalWrite(FRAM_CS1, LOW); 66 | 67 | SPI.transfer(CMD_READ); 68 | SPI.transfer(addrMSB); 69 | SPI.transfer(addrLSB); 70 | 71 | for (int i=0; i < count; i++) buf[i] = SPI.transfer(0x00); 72 | 73 | digitalWrite(FRAM_CS1, HIGH); 74 | 75 | return 0; 76 | } 77 | 78 | void setup() 79 | { 80 | Serial.begin(9600); 81 | pinMode(FRAM_CS1, OUTPUT); 82 | digitalWrite(FRAM_CS1, HIGH); 83 | 84 | 85 | //Setting up the SPI bus 86 | SPI.begin(); 87 | SPI.setDataMode(SPI_MODE0); 88 | SPI.setBitOrder(MSBFIRST); 89 | SPI.setClockDivider(SPI_CLOCK_DIV2); 90 | 91 | //Test 92 | char buf[]="This is a test"; 93 | 94 | FRAMWrite(1, (byte*) buf, 14); 95 | FRAMRead(1, (byte*) buf, 14); 96 | 97 | for (int i = 0; i < 14; i++) Serial.print(buf[i]); 98 | } 99 | 100 | void loop() 101 | { 102 | } 103 | 104 | -------------------------------------------------------------------------------- /examples/WebClient/WebClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web client 3 | 4 | This sketch connects to a website (http://www.google.com) 5 | using an Arduino Wiznet Ethernet shield. 6 | 7 | Circuit: 8 | * Ethernet shield attached to pins 10, 11, 12, 13 9 | 10 | created 18 Dec 2009 11 | by David A. Mellis 12 | modified 9 Apr 2012 13 | by Tom Igoe, based on work by Adrian McEwen 14 | modified 12 Aug 2013 15 | by Soohwan Kim 16 | 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | // Enter a MAC address for your controller below. 23 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 24 | 25 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 26 | 27 | // if you don't want to use DNS (and reduce your sketch size) 28 | // use the numeric IP instead of the name for the server: 29 | //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) 30 | char server[] = "www.google.com"; // name address for Google (using DNS) 31 | 32 | // Set the static IP address to use if the DHCP fails to assign 33 | IPAddress ip(192,168,1,145); 34 | 35 | // Initialize the Ethernet client library 36 | // with the IP address and port of the server 37 | // that you want to connect to (port 80 is default for HTTP): 38 | EthernetClient client; 39 | 40 | void setup() { 41 | // Open serial communications and wait for port to open: 42 | Serial.begin(9600); 43 | while (!Serial) { 44 | ; // wait for serial port to connect. Needed for Leonardo only 45 | } 46 | 47 | // start the Ethernet connection: 48 | 49 | if (Ethernet.begin(mac) == 0) { 50 | 51 | Serial.println("Failed to configure Ethernet using DHCP"); 52 | // no point in carrying on, so do nothing forevermore: 53 | // try to congifure using IP address instead of DHCP: 54 | Ethernet.begin(mac, ip); 55 | 56 | } 57 | // give the Ethernet shield a second to initialize: 58 | delay(1000); 59 | Serial.println("connecting..."); 60 | 61 | // if you get a connection, report back via serial: 62 | if (client.connect(server, 80)) { 63 | Serial.println("connected"); 64 | // Make a HTTP request: 65 | client.println("GET /search?q=arduino HTTP/1.1"); 66 | client.println("Host: www.google.com"); 67 | client.println("Connection: close"); 68 | client.println(); 69 | } 70 | else { 71 | // if you didn't get a connection to the server: 72 | Serial.println("connection failed"); 73 | } 74 | } 75 | 76 | void loop() 77 | { 78 | // if there are incoming bytes available 79 | // from the server, read them and print them: 80 | if (client.available()) { 81 | char c = client.read(); 82 | Serial.print(c); 83 | } 84 | 85 | // if the server's disconnected, stop the client: 86 | if (!client.connected()) { 87 | Serial.println(); 88 | Serial.println("disconnecting."); 89 | client.stop(); 90 | 91 | // do nothing forevermore: 92 | while(true); 93 | } 94 | } 95 | 96 | -------------------------------------------------------------------------------- /examples/WebServer/WebServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web Server 3 | 4 | A simple web server that shows the value of the analog input pins. 5 | using an Arduino Wiznet Ethernet shield. 6 | 7 | Circuit: 8 | * Ethernet shield attached to pins 10, 11, 12, 13 9 | * Analog inputs attached to pins A0 through A5 (optional) 10 | 11 | created 18 Dec 2009 12 | by David A. Mellis 13 | modified 9 Apr 2012 14 | by Tom Igoe 15 | modified 12 Aug 2013 16 | by Soohwan Kim 17 | 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | // Enter a MAC address and IP address for your controller below. 24 | 25 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 26 | IPAddress ip(192,168,1,177); 27 | 28 | // Initialize the Ethernet server library 29 | // with the IP address and port you want to use 30 | // (port 80 is default for HTTP): 31 | EthernetServer server(80); 32 | 33 | void setup() { 34 | pinMode(10, OUTPUT); // change this to 53 on a mega 35 | pinMode(4, OUTPUT); // change this to 53 on a mega 36 | pinMode(6, OUTPUT); // change this to 53 on a mega 37 | digitalWrite(10, HIGH); 38 | digitalWrite(6, HIGH); 39 | digitalWrite(4, HIGH); 40 | delay(5000); 41 | // Open serial communications and wait for port to open: 42 | Serial.begin(9600); 43 | Ethernet.begin(mac, ip); 44 | server.begin(); 45 | Serial.print("server is at "); 46 | Serial.println(Ethernet.localIP()); 47 | } 48 | 49 | 50 | void loop() { 51 | // listen for incoming clients 52 | EthernetClient client = server.available(); 53 | if (client) { 54 | Serial.println("new client"); 55 | // an http request ends with a blank line 56 | boolean currentLineIsBlank = true; 57 | while (client.connected()) { 58 | if (client.available()) { 59 | char c = client.read(); 60 | Serial.write(c); 61 | // if you've gotten to the end of the line (received a newline 62 | // character) and the line is blank, the http request has ended, 63 | // so you can send a reply 64 | if (c == '\n' && currentLineIsBlank) { 65 | // send a standard http response header 66 | client.println("HTTP/1.1 200 OK"); 67 | client.println("Content-Type: text/html"); 68 | client.println("Connection: close"); // the connection will be closed after completion of the response 69 | client.println("Refresh: 5"); // refresh the page automatically every 5 sec 70 | client.println(); 71 | client.println(""); 72 | client.println(""); 73 | // output the value of each analog input pin 74 | for (int analogChannel = 0; analogChannel < 6; analogChannel++) { 75 | int sensorReading = analogRead(analogChannel); 76 | client.print("analog input "); 77 | client.print(analogChannel); 78 | client.print(" is "); 79 | client.print(sensorReading); 80 | client.println("
"); 81 | } 82 | client.println(""); 83 | break; 84 | } 85 | if (c == '\n') { 86 | // you're starting a new line 87 | currentLineIsBlank = true; 88 | } 89 | else if (c != '\r') { 90 | // you've gotten a character on the current line 91 | currentLineIsBlank = false; 92 | } 93 | } 94 | } 95 | // give the web browser time to receive the data 96 | delay(1); 97 | // close the connection: 98 | client.stop(); 99 | Serial.println("client disonnected"); 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /src/utility/w5500.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "w5100.h" 15 | #if defined(W5500_ETHERNET_SHIELD) 16 | 17 | // W5500 controller instance 18 | W5500Class W5100; 19 | 20 | 21 | void W5500Class::init(void) 22 | { 23 | 24 | initSS(); 25 | delay(300); 26 | SPI.begin(); 27 | 28 | for (int i=0; i> 8); 101 | SPI.transfer(_addr & 0xFF); 102 | SPI.transfer(_cb); 103 | SPI.transfer(_data); 104 | resetSS(); 105 | return 1; 106 | } 107 | 108 | uint16_t W5500Class::write(uint16_t _addr, uint8_t _cb, const uint8_t *_buf, uint16_t _len) 109 | { 110 | setSS(); 111 | SPI.transfer(_addr >> 8); 112 | SPI.transfer(_addr & 0xFF); 113 | SPI.transfer(_cb); 114 | for (uint16_t i=0; i<_len; i++){ 115 | SPI.transfer(_buf[i]); 116 | } 117 | resetSS(); 118 | return _len; 119 | } 120 | 121 | uint8_t W5500Class::read(uint16_t _addr, uint8_t _cb) 122 | { 123 | setSS(); 124 | SPI.transfer(_addr >> 8); 125 | SPI.transfer(_addr & 0xFF); 126 | SPI.transfer(_cb); 127 | uint8_t _data = SPI.transfer(0); 128 | resetSS(); 129 | return _data; 130 | } 131 | 132 | uint16_t W5500Class::read(uint16_t _addr, uint8_t _cb, uint8_t *_buf, uint16_t _len) 133 | { 134 | setSS(); 135 | SPI.transfer(_addr >> 8); 136 | SPI.transfer(_addr & 0xFF); 137 | SPI.transfer(_cb); 138 | for (uint16_t i=0; i<_len; i++){ 139 | _buf[i] = SPI.transfer(0); 140 | } 141 | resetSS(); 142 | 143 | return _len; 144 | } 145 | 146 | void W5500Class::execCmdSn(SOCKET s, SockCMD _cmd) { 147 | // Send command to socket 148 | writeSnCR(s, _cmd); 149 | // Wait for command to complete 150 | while (readSnCR(s)) 151 | ; 152 | } 153 | #endif -------------------------------------------------------------------------------- /src/EthernetClient.cpp: -------------------------------------------------------------------------------- 1 | #include "utility/w5100.h" 2 | #include "utility/socket.h" 3 | 4 | extern "C" { 5 | #include "string.h" 6 | } 7 | 8 | #include "Arduino.h" 9 | 10 | #include "EthernetIndustruino.h" 11 | #include "EthernetClient.h" 12 | #include "EthernetServer.h" 13 | #include "Dns.h" 14 | 15 | uint16_t EthernetClient::_srcport = 1024; 16 | 17 | EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) { 18 | } 19 | 20 | EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) { 21 | } 22 | 23 | int EthernetClient::connect(const char* host, uint16_t port) { 24 | // Look up the host first 25 | int ret = 0; 26 | DNSClient dns; 27 | IPAddress remote_addr; 28 | 29 | dns.begin(Ethernet.dnsServerIP()); 30 | ret = dns.getHostByName(host, remote_addr); 31 | if (ret == 1) { 32 | return connect(remote_addr, port); 33 | } else { 34 | return ret; 35 | } 36 | } 37 | 38 | int EthernetClient::connect(IPAddress ip, uint16_t port) { 39 | if (_sock != MAX_SOCK_NUM) 40 | return 0; 41 | 42 | for (int i = 0; i < MAX_SOCK_NUM; i++) { 43 | uint8_t s = W5100.readSnSR(i); 44 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) { 45 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { 46 | _sock = i; 47 | break; 48 | } 49 | } 50 | } 51 | 52 | if (_sock == MAX_SOCK_NUM) 53 | return 0; 54 | 55 | _srcport++; 56 | if (_srcport == 0) _srcport = 1024; 57 | socket(_sock, SnMR::TCP, _srcport, 0); 58 | 59 | if (!::connect(_sock, rawIPAddress(ip), port)) { 60 | _sock = MAX_SOCK_NUM; 61 | return 0; 62 | } 63 | 64 | while (status() != SnSR::ESTABLISHED) { 65 | delay(1); 66 | if (status() == SnSR::CLOSED) { 67 | _sock = MAX_SOCK_NUM; 68 | return 0; 69 | } 70 | } 71 | 72 | return 1; 73 | } 74 | 75 | size_t EthernetClient::write(uint8_t b) { 76 | return write(&b, 1); 77 | } 78 | 79 | size_t EthernetClient::write(const uint8_t *buf, size_t size) { 80 | if (_sock == MAX_SOCK_NUM) { 81 | setWriteError(); 82 | return 0; 83 | } 84 | if (!send(_sock, buf, size)) { 85 | setWriteError(); 86 | return 0; 87 | } 88 | return size; 89 | } 90 | 91 | int EthernetClient::available() { 92 | if (_sock != MAX_SOCK_NUM) 93 | return W5100.getRXReceivedSize(_sock); 94 | return 0; 95 | } 96 | 97 | int EthernetClient::read() { 98 | uint8_t b; 99 | if ( recv(_sock, &b, 1) > 0 ) 100 | { 101 | // recv worked 102 | return b; 103 | } 104 | else 105 | { 106 | // No data available 107 | return -1; 108 | } 109 | } 110 | 111 | int EthernetClient::read(uint8_t *buf, size_t size) { 112 | return recv(_sock, buf, size); 113 | } 114 | 115 | int EthernetClient::peek() { 116 | uint8_t b; 117 | // Unlike recv, peek doesn't check to see if there's any data available, so we must 118 | if (!available()) 119 | return -1; 120 | ::peek(_sock, &b); 121 | return b; 122 | } 123 | 124 | void EthernetClient::flush() { 125 | while (available()) 126 | read(); 127 | } 128 | 129 | void EthernetClient::stop() { 130 | if (_sock == MAX_SOCK_NUM) 131 | return; 132 | 133 | // attempt to close the connection gracefully (send a FIN to other side) 134 | disconnect(_sock); 135 | unsigned long start = millis(); 136 | 137 | // wait a second for the connection to close 138 | while (status() != SnSR::CLOSED && millis() - start < 1000) 139 | delay(1); 140 | 141 | // if it hasn't closed, close it forcefully 142 | if (status() != SnSR::CLOSED) 143 | close(_sock); 144 | 145 | EthernetClass::_server_port[_sock] = 0; 146 | _sock = MAX_SOCK_NUM; 147 | } 148 | 149 | uint8_t EthernetClient::connected() { 150 | if (_sock == MAX_SOCK_NUM) return 0; 151 | 152 | uint8_t s = status(); 153 | return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT || 154 | (s == SnSR::CLOSE_WAIT && !available())); 155 | } 156 | 157 | uint8_t EthernetClient::status() { 158 | if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; 159 | return W5100.readSnSR(_sock); 160 | } 161 | 162 | // the next function allows us to use the client returned by 163 | // EthernetServer::available() as the condition in an if-statement. 164 | 165 | EthernetClient::operator bool() { 166 | return _sock != MAX_SOCK_NUM; 167 | } 168 | -------------------------------------------------------------------------------- /src/utility/w5100.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by Cristian Maglie 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include "w5100.h" 16 | 17 | #if defined(W5100_ETHERNET_SHIELD) 18 | 19 | // W5100 controller instance 20 | W5100Class W5100; 21 | 22 | #define TX_RX_MAX_BUF_SIZE 2048 23 | #define TX_BUF 0x1100 24 | #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) 25 | 26 | #define TXBUF_BASE 0x4000 27 | #define RXBUF_BASE 0x6000 28 | 29 | void W5100Class::init(void) 30 | { 31 | delay(300); 32 | 33 | SPI.begin(); 34 | initSS(); 35 | 36 | writeMR(1< SSIZE) 85 | { 86 | // Wrap around circular buffer 87 | uint16_t size = SSIZE - offset; 88 | write(dstAddr, data, size); 89 | write(SBASE[s], data + size, len - size); 90 | } 91 | else { 92 | write(dstAddr, data, len); 93 | } 94 | 95 | ptr += len; 96 | writeSnTX_WR(s, ptr); 97 | } 98 | 99 | 100 | void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) 101 | { 102 | uint16_t ptr; 103 | ptr = readSnRX_RD(s); 104 | read_data(s, (uint8_t *)ptr, data, len); 105 | if (!peek) 106 | { 107 | ptr += len; 108 | writeSnRX_RD(s, ptr); 109 | } 110 | } 111 | 112 | void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len) 113 | { 114 | uint16_t size; 115 | uint16_t src_mask; 116 | uint16_t src_ptr; 117 | 118 | src_mask = (uint16_t)src & RMASK; 119 | src_ptr = RBASE[s] + src_mask; 120 | 121 | if( (src_mask + len) > RSIZE ) 122 | { 123 | size = RSIZE - src_mask; 124 | read(src_ptr, (uint8_t *)dst, size); 125 | dst += size; 126 | read(RBASE[s], (uint8_t *) dst, len - size); 127 | } 128 | else 129 | read(src_ptr, (uint8_t *) dst, len); 130 | } 131 | 132 | 133 | uint8_t W5100Class::write(uint16_t _addr, uint8_t _data) 134 | { 135 | setSS(); 136 | SPI.transfer(0xF0); 137 | SPI.transfer(_addr >> 8); 138 | SPI.transfer(_addr & 0xFF); 139 | SPI.transfer(_data); 140 | resetSS(); 141 | return 1; 142 | } 143 | 144 | uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) 145 | { 146 | for (uint16_t i=0; i<_len; i++) 147 | { 148 | setSS(); 149 | SPI.transfer(0xF0); 150 | SPI.transfer(_addr >> 8); 151 | SPI.transfer(_addr & 0xFF); 152 | _addr++; 153 | SPI.transfer(_buf[i]); 154 | resetSS(); 155 | } 156 | return _len; 157 | } 158 | 159 | uint8_t W5100Class::read(uint16_t _addr) 160 | { 161 | setSS(); 162 | SPI.transfer(0x0F); 163 | SPI.transfer(_addr >> 8); 164 | SPI.transfer(_addr & 0xFF); 165 | uint8_t _data = SPI.transfer(0); 166 | resetSS(); 167 | return _data; 168 | } 169 | 170 | uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) 171 | { 172 | for (uint16_t i=0; i<_len; i++) 173 | { 174 | setSS(); 175 | SPI.transfer(0x0F); 176 | SPI.transfer(_addr >> 8); 177 | SPI.transfer(_addr & 0xFF); 178 | _addr++; 179 | _buf[i] = SPI.transfer(0); 180 | resetSS(); 181 | } 182 | return _len; 183 | } 184 | 185 | void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) { 186 | // Send command to socket 187 | writeSnCR(s, _cmd); 188 | // Wait for command to complete 189 | while (readSnCR(s)) 190 | ; 191 | } 192 | 193 | #endif -------------------------------------------------------------------------------- /src/Dhcp.h: -------------------------------------------------------------------------------- 1 | // DHCP Library v0.3 - April 25, 2009 2 | // Author: Jordan Terrell - blog.jordanterrell.com 3 | 4 | #ifndef Dhcp_h 5 | #define Dhcp_h 6 | 7 | #include "EthernetUdp.h" 8 | 9 | /* DHCP state machine. */ 10 | #define STATE_DHCP_START 0 11 | #define STATE_DHCP_DISCOVER 1 12 | #define STATE_DHCP_REQUEST 2 13 | #define STATE_DHCP_LEASED 3 14 | #define STATE_DHCP_REREQUEST 4 15 | #define STATE_DHCP_RELEASE 5 16 | 17 | #define DHCP_FLAGSBROADCAST 0x8000 18 | 19 | /* UDP port numbers for DHCP */ 20 | #define DHCP_SERVER_PORT 67 /* from server to client */ 21 | #define DHCP_CLIENT_PORT 68 /* from client to server */ 22 | 23 | /* DHCP message OP code */ 24 | #define DHCP_BOOTREQUEST 1 25 | #define DHCP_BOOTREPLY 2 26 | 27 | /* DHCP message type */ 28 | #define DHCP_DISCOVER 1 29 | #define DHCP_OFFER 2 30 | #define DHCP_REQUEST 3 31 | #define DHCP_DECLINE 4 32 | #define DHCP_ACK 5 33 | #define DHCP_NAK 6 34 | #define DHCP_RELEASE 7 35 | #define DHCP_INFORM 8 36 | 37 | #define DHCP_HTYPE10MB 1 38 | #define DHCP_HTYPE100MB 2 39 | 40 | #define DHCP_HLENETHERNET 6 41 | #define DHCP_HOPS 0 42 | #define DHCP_SECS 0 43 | 44 | #define MAGIC_COOKIE 0x63825363 45 | #define MAX_DHCP_OPT 16 46 | 47 | #define HOST_NAME "WIZnet" 48 | #define DEFAULT_LEASE (900) //default lease time in seconds 49 | 50 | #define DHCP_CHECK_NONE (0) 51 | #define DHCP_CHECK_RENEW_FAIL (1) 52 | #define DHCP_CHECK_RENEW_OK (2) 53 | #define DHCP_CHECK_REBIND_FAIL (3) 54 | #define DHCP_CHECK_REBIND_OK (4) 55 | 56 | enum 57 | { 58 | padOption = 0, 59 | subnetMask = 1, 60 | timerOffset = 2, 61 | routersOnSubnet = 3, 62 | /* timeServer = 4, 63 | nameServer = 5,*/ 64 | dns = 6, 65 | /*logServer = 7, 66 | cookieServer = 8, 67 | lprServer = 9, 68 | impressServer = 10, 69 | resourceLocationServer = 11,*/ 70 | hostName = 12, 71 | /*bootFileSize = 13, 72 | meritDumpFile = 14,*/ 73 | domainName = 15, 74 | /*swapServer = 16, 75 | rootPath = 17, 76 | extentionsPath = 18, 77 | IPforwarding = 19, 78 | nonLocalSourceRouting = 20, 79 | policyFilter = 21, 80 | maxDgramReasmSize = 22, 81 | defaultIPTTL = 23, 82 | pathMTUagingTimeout = 24, 83 | pathMTUplateauTable = 25, 84 | ifMTU = 26, 85 | allSubnetsLocal = 27, 86 | broadcastAddr = 28, 87 | performMaskDiscovery = 29, 88 | maskSupplier = 30, 89 | performRouterDiscovery = 31, 90 | routerSolicitationAddr = 32, 91 | staticRoute = 33, 92 | trailerEncapsulation = 34, 93 | arpCacheTimeout = 35, 94 | ethernetEncapsulation = 36, 95 | tcpDefaultTTL = 37, 96 | tcpKeepaliveInterval = 38, 97 | tcpKeepaliveGarbage = 39, 98 | nisDomainName = 40, 99 | nisServers = 41, 100 | ntpServers = 42, 101 | vendorSpecificInfo = 43, 102 | netBIOSnameServer = 44, 103 | netBIOSdgramDistServer = 45, 104 | netBIOSnodeType = 46, 105 | netBIOSscope = 47, 106 | xFontServer = 48, 107 | xDisplayManager = 49,*/ 108 | dhcpRequestedIPaddr = 50, 109 | dhcpIPaddrLeaseTime = 51, 110 | /*dhcpOptionOverload = 52,*/ 111 | dhcpMessageType = 53, 112 | dhcpServerIdentifier = 54, 113 | dhcpParamRequest = 55, 114 | /*dhcpMsg = 56, 115 | dhcpMaxMsgSize = 57,*/ 116 | dhcpT1value = 58, 117 | dhcpT2value = 59, 118 | /*dhcpClassIdentifier = 60,*/ 119 | dhcpClientIdentifier = 61, 120 | endOption = 255 121 | }; 122 | 123 | typedef struct _RIP_MSG_FIXED 124 | { 125 | uint8_t op; 126 | uint8_t htype; 127 | uint8_t hlen; 128 | uint8_t hops; 129 | uint32_t xid; 130 | uint16_t secs; 131 | uint16_t flags; 132 | uint8_t ciaddr[4]; 133 | uint8_t yiaddr[4]; 134 | uint8_t siaddr[4]; 135 | uint8_t giaddr[4]; 136 | uint8_t chaddr[6]; 137 | }RIP_MSG_FIXED; 138 | 139 | class DhcpClass { 140 | private: 141 | uint32_t _dhcpInitialTransactionId; 142 | uint32_t _dhcpTransactionId; 143 | uint8_t _dhcpMacAddr[6]; 144 | uint8_t _dhcpLocalIp[4]; 145 | uint8_t _dhcpSubnetMask[4]; 146 | uint8_t _dhcpGatewayIp[4]; 147 | uint8_t _dhcpDhcpServerIp[4]; 148 | uint8_t _dhcpDnsServerIp[4]; 149 | uint32_t _dhcpLeaseTime; 150 | uint32_t _dhcpT1, _dhcpT2; 151 | signed long _renewInSec; 152 | signed long _rebindInSec; 153 | signed long _lastCheck; 154 | unsigned long _timeout; 155 | unsigned long _responseTimeout; 156 | unsigned long _secTimeout; 157 | uint8_t _dhcp_state; 158 | EthernetUDP _dhcpUdpSocket; 159 | 160 | int request_DHCP_lease(); 161 | void reset_DHCP_lease(); 162 | void presend_DHCP(); 163 | void send_DHCP_MESSAGE(uint8_t, uint16_t); 164 | void printByte(char *, uint8_t); 165 | 166 | uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); 167 | public: 168 | IPAddress getLocalIp(); 169 | IPAddress getSubnetMask(); 170 | IPAddress getGatewayIp(); 171 | IPAddress getDhcpServerIp(); 172 | IPAddress getDnsServerIp(); 173 | 174 | int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); 175 | int checkLease(); 176 | }; 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /src/utility/w5200.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "w5100.h" 14 | 15 | #if defined(W5200_ETHERNET_SHIELD) 16 | // W5200 controller instance 17 | W5200Class W5100; 18 | 19 | #define TX_RX_MAX_BUF_SIZE 2048 20 | #define TX_BUF 0x1100 21 | #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) 22 | 23 | 24 | #define TXBUF_BASE 0x8000 25 | #define RXBUF_BASE 0xC000 26 | 27 | void W5200Class::init(void) 28 | { 29 | delay(300); 30 | 31 | SPI.begin(); 32 | initSS(); 33 | 34 | writeMR(1< SSIZE) 85 | { 86 | // Wrap around circular buffer 87 | uint16_t size = SSIZE - offset; 88 | write(dstAddr, data, size); 89 | write(SBASE[s], data + size, len - size); 90 | } 91 | else { 92 | write(dstAddr, data, len); 93 | } 94 | 95 | ptr += len; 96 | writeSnTX_WR(s, ptr); 97 | } 98 | 99 | 100 | void W5200Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) 101 | { 102 | uint16_t ptr; 103 | ptr = readSnRX_RD(s); 104 | read_data(s, (uint8_t *)ptr, data, len); 105 | if (!peek) 106 | { 107 | ptr += len; 108 | writeSnRX_RD(s, ptr); 109 | } 110 | } 111 | 112 | void W5200Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len) 113 | { 114 | uint16_t size; 115 | uint16_t src_mask; 116 | uint16_t src_ptr; 117 | 118 | src_mask = (uint16_t)src & RMASK; 119 | src_ptr = RBASE[s] + src_mask; 120 | 121 | if( (src_mask + len) > RSIZE ) 122 | { 123 | size = RSIZE - src_mask; 124 | read(src_ptr, (uint8_t *)dst, size); 125 | dst += size; 126 | read(RBASE[s], (uint8_t *) dst, len - size); 127 | } 128 | else 129 | read(src_ptr, (uint8_t *) dst, len); 130 | } 131 | 132 | 133 | uint8_t W5200Class::write(uint16_t _addr, uint8_t _data) 134 | { 135 | setSS(); 136 | 137 | SPI.transfer(_addr >> 8); 138 | SPI.transfer(_addr & 0xFF); 139 | SPI.transfer(0x80); 140 | SPI.transfer(0x01); 141 | SPI.transfer(_data); 142 | resetSS(); 143 | return 1; 144 | } 145 | 146 | uint16_t W5200Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) 147 | { 148 | 149 | setSS(); 150 | SPI.transfer(_addr >> 8); 151 | SPI.transfer(_addr & 0xFF); 152 | SPI.transfer((0x80 | ((_len & 0x7F00) >> 8))); 153 | SPI.transfer(_len & 0x00FF); 154 | 155 | for (uint16_t i=0; i<_len; i++) 156 | { 157 | SPI.transfer(_buf[i]); 158 | 159 | } 160 | resetSS(); 161 | return _len; 162 | } 163 | 164 | uint8_t W5200Class::read(uint16_t _addr) 165 | { 166 | setSS(); 167 | SPI.transfer(_addr >> 8); 168 | SPI.transfer(_addr & 0xFF); 169 | SPI.transfer(0x00); 170 | SPI.transfer(0x01); 171 | uint8_t _data = SPI.transfer(0); 172 | resetSS(); 173 | return _data; 174 | } 175 | 176 | uint16_t W5200Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) 177 | { 178 | setSS(); 179 | SPI.transfer(_addr >> 8); 180 | SPI.transfer(_addr & 0xFF); 181 | SPI.transfer((0x00 | ((_len & 0x7F00) >> 8))); 182 | SPI.transfer(_len & 0x00FF); 183 | 184 | for (uint16_t i=0; i<_len; i++) 185 | { 186 | _buf[i] = SPI.transfer(0); 187 | 188 | } 189 | resetSS(); 190 | return _len; 191 | } 192 | 193 | void W5200Class::execCmdSn(SOCKET s, SockCMD _cmd) { 194 | // Send command to socket 195 | writeSnCR(s, _cmd); 196 | // Wait for command to complete 197 | while (readSnCR(s)) 198 | ; 199 | } 200 | #endif -------------------------------------------------------------------------------- /src/EthernetUdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 3 | * This version only offers minimal wrapping of socket.c/socket.h 4 | * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 5 | * 6 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 7 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 8 | * might not happen often in practice, but in larger network topologies, a UDP 9 | * packet can be received out of sequence. 10 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 11 | * aware of it. Again, this may not be a concern in practice on small local networks. 12 | * For more information, see http://www.cafeaulait.org/course/week12/35.html 13 | * 14 | * MIT License: 15 | * Copyright (c) 2008 Bjoern Hartmann 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in 24 | * all copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 32 | * THE SOFTWARE. 33 | * 34 | * bjoern@cs.stanford.edu 12/30/2008 35 | */ 36 | 37 | #ifndef ethernetudp_h 38 | #define ethernetudp_h 39 | 40 | #include 41 | 42 | #define UDP_TX_PACKET_MAX_SIZE 24 43 | 44 | class EthernetUDP : public UDP { 45 | private: 46 | uint8_t _sock; // socket ID for Wiz5100 47 | uint16_t _port; // local port to listen on 48 | IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed 49 | uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed 50 | uint16_t _offset; // offset into the packet being sent 51 | uint16_t _remaining; // remaining bytes of incoming packet yet to be processed 52 | 53 | public: 54 | EthernetUDP(); // Constructor 55 | 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 56 | virtual 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 | virtual int beginPacket(IPAddress ip, uint16_t port); 63 | // Start building up a packet to send to the remote host specific in host and port 64 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port 65 | virtual int beginPacket(const char *host, uint16_t port); 66 | // Finish off this packet and send it 67 | // Returns 1 if the packet was sent successfully, 0 if there was an error 68 | virtual int endPacket(); 69 | // Write a single byte into the packet 70 | virtual size_t write(uint8_t); 71 | // Write size bytes from buffer into the packet 72 | virtual size_t write(const uint8_t *buffer, size_t size); 73 | 74 | using Print::write; 75 | 76 | // Start processing the next available incoming packet 77 | // Returns the size of the packet in bytes, or 0 if no packets are available 78 | virtual int parsePacket(); 79 | // Number of bytes remaining in the current packet 80 | virtual int available(); 81 | // Read a single byte from the current packet 82 | virtual int read(); 83 | // Read up to len bytes from the current packet and place them into buffer 84 | // Returns the number of bytes read, or 0 if none are available 85 | virtual int read(unsigned char* buffer, size_t len); 86 | // Read up to len characters from the current packet and place them into buffer 87 | // Returns the number of characters read, or 0 if none are available 88 | virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); }; 89 | // Return the next byte from the current packet without moving on to the next byte 90 | virtual int peek(); 91 | virtual void flush(); // Finish reading the current packet 92 | 93 | // Return the IP address of the host who sent the current incoming packet 94 | virtual IPAddress remoteIP() { return _remoteIP; }; 95 | // Return the port of the host who sent the current incoming packet 96 | virtual uint16_t remotePort() { return _remotePort; }; 97 | }; 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /examples/Emon/Emon.ino: -------------------------------------------------------------------------------- 1 | //This sketch will read analog input A0 and A11 and post its value to EmonCMS.org every 5 seconds. To use this sketch, first make a free user account at emoncms.org. 2 | 3 | char foo; //without a simple variable declaration, use of #ifdef at the top of your code raises an error! 4 | 5 | typedef uint8_t SOCKET; 6 | #include 7 | #include 8 | #define W5500_ETHERNET_SHIELD 9 | // Include Emon Library 10 | 11 | 12 | //network configuration, WIRED or WIFI 13 | 14 | //MAC address of your Ethernet module 15 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 16 | 17 | // fill in an available IP address on your network here, 18 | IPAddress ip(192, 168, 1, 130); 19 | 20 | EthernetClient client; //Ethernet client mode 21 | // The analog sensor pins to be read 22 | const int lightSensorPin = A0; 23 | int tempSensorPin = A11; 24 | 25 | float tempValue = 0; 26 | int lightValue = 0; 27 | 28 | // Create an Emon instance 29 | //Emoncms configurations 30 | char server[] = "emoncms.org"; // name address for emoncms.org 31 | //IPAddress server(213, 138, 101, 177); // numeric IP for emoncms.org (no DNS) 32 | 33 | String apikey = "01278441cb103fca172410a5e1061a29"; //change this to your api key (can be found on the API help page of your EmonCMS.org account). 34 | int node = 0; //if 0, not used 35 | 36 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 37 | boolean lastConnected = false; // state of the connection last time through the main loop 38 | const unsigned long postingInterval = 5000; // delay between updates, in milliseconds 39 | 40 | void setup() { 41 | //Setup of Ethernet module chip select lines 42 | pinMode(10, OUTPUT); 43 | pinMode(6, OUTPUT); 44 | pinMode(4, OUTPUT); 45 | digitalWrite(10, LOW); 46 | digitalWrite(6, HIGH); 47 | digitalWrite(4, HIGH); 48 | 49 | delay(5000); //wait 5 seconds to make sure Ethernet is running. 50 | // Open serial communications 51 | Serial.begin(9600); 52 | 53 | // start the Ethernet connection: 54 | if (Ethernet.begin(mac) == 0) { 55 | 56 | Serial.println("Failed to configure Ethernet using DHCP"); 57 | // no point in carrying on, so do nothing forevermore: 58 | // try to congifure using IP address instead of DHCP: 59 | Ethernet.begin(mac, ip); 60 | 61 | } 62 | // give the Ethernet shield a second to initialize: 63 | delay(1000); 64 | Serial.println("starting.."); 65 | 66 | } 67 | 68 | 69 | 70 | void loop() { 71 | 72 | // if you're not connected, and at least milliseconds have 73 | // passed sinceyour last connection, then connect again and 74 | // send data: 75 | if ((millis() - lastConnectionTime > postingInterval)) { 76 | 77 | //read sensors 78 | lightValue = analogRead(lightSensorPin); 79 | tempValue = analogRead(tempSensorPin); 80 | 81 | //Print values (debug) 82 | Serial.println(); 83 | Serial.print("Temp : "); 84 | Serial.print(tempValue); 85 | Serial.print(" ; Light : "); 86 | Serial.print(lightValue); 87 | //send values 88 | sendData(); 89 | } 90 | // store the state of the connection for next time through 91 | // the loop: 92 | lastConnected = client.connected(); 93 | } 94 | 95 | // this method makes a HTTP connection to the server: 96 | void sendData() { 97 | // if there's a successful connection: 98 | if (client.connect(server, 80)) { 99 | Serial.println("Connecting..."); 100 | // send the HTTP GET request: 101 | client.print("GET /api/post?apikey="); 102 | client.print(apikey); 103 | if (node > 0) { 104 | client.print("&node="); 105 | client.print(node); 106 | } 107 | client.print("&json={temp"); 108 | client.print(":"); 109 | client.print(tempValue); 110 | client.print(",light:"); 111 | client.print(lightValue); 112 | client.println("} HTTP/1.1"); 113 | client.println("Host:emoncms.org"); 114 | client.println("User-Agent: Arduino-ethernet"); 115 | client.println("Connection: close"); 116 | client.println(); 117 | 118 | // note the time that the connection was made: 119 | lastConnectionTime = millis(); 120 | Serial.println(freeRam()); 121 | Serial.println("Disconnecting..."); 122 | client.stop(); 123 | } 124 | else { 125 | // if you couldn't make a connection: 126 | Serial.println("Connection failed"); 127 | } 128 | } 129 | 130 | 131 | void printStatus() { 132 | #ifdef WIFI 133 | // print the SSID of the network you're attached to: 134 | Serial.print("SSID: "); 135 | Serial.println(WiFi.SSID()); 136 | 137 | // print your WiFi shield's IP address: 138 | IPAddress ip = WiFi.localIP(); 139 | Serial.print("IP Address: "); 140 | Serial.println(ip); 141 | 142 | // print the received signal strength: 143 | long rssi = WiFi.RSSI(); 144 | Serial.print("signal strength (RSSI):"); 145 | Serial.print(rssi); 146 | Serial.print(" dBm"); 147 | #else 148 | // print your local IP address: 149 | Serial.print("IP address: "); 150 | for (byte thisByte = 0; thisByte < 4; thisByte++) { 151 | // print the value of each byte of the IP address: 152 | Serial.print(Ethernet.localIP()[thisByte], DEC); 153 | Serial.print("."); 154 | } 155 | #endif 156 | Serial.println(); 157 | } 158 | 159 | float getCelsius(int sensorValue) { 160 | /* 161 | created by Federico Vanzati for TinkerKit Thermistor Library 162 | */ 163 | const static float ADCres = 1023.0; 164 | const static int Beta = 3950; // Beta parameter 165 | const static float Kelvin = 273.15; // 0°C = 273.15 K 166 | const static int Rb = 10000; // 10 kOhm 167 | const static float Ginf = 120.6685; // Ginf = 1/Rinf 168 | 169 | float Rthermistor = Rb * (ADCres / sensorValue - 1); 170 | float _temperatureC = Beta / (log( Rthermistor * Ginf )) ; 171 | return _temperatureC - Kelvin; 172 | } 173 | 174 | 175 | int freeRam () { 176 | extern int __heap_start, *__brkval; 177 | int v; 178 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 179 | } 180 | -------------------------------------------------------------------------------- /src/EthernetIndustruino.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | modified 12 Aug 2013 3 | by Soohwan Kim (suhwan@wiznet.co.kr) 4 | */ 5 | #include "EthernetIndustruino.h" 6 | #include "Dhcp.h" 7 | 8 | // XXX: don't make assumptions about the value of MAX_SOCK_NUM. 9 | uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, }; 10 | uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, }; 11 | 12 | 13 | 14 | #if defined(WIZ550io_WITH_MACADDRESS) 15 | int EthernetClass::begin(uint8_t *mac_address) 16 | { 17 | static DhcpClass s_dhcp; 18 | _dhcp = &s_dhcp; 19 | 20 | // Initialise the basic info 21 | W5100.init(); 22 | W5100.setMACAddress(mac_address); 23 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); 24 | 25 | // Now try to get our config info from a DHCP server 26 | int ret = _dhcp->beginWithDHCP(mac_address); 27 | if(ret == 1) 28 | { 29 | // We've successfully found a DHCP server and got our configuration info, so set things 30 | // accordingly 31 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 32 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 33 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 34 | _dnsServerAddress = _dhcp->getDnsServerIp(); 35 | } 36 | 37 | return ret; 38 | } 39 | 40 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip) 41 | { 42 | // Assume the DNS server will be the machine on the same network as the local IP 43 | // but with last octet being '1' 44 | IPAddress dns_server = local_ip; 45 | dns_server[3] = 1; 46 | begin(mac_address, local_ip, dns_server); 47 | } 48 | 49 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server) 50 | { 51 | // Assume the gateway will be the machine on the same network as the local IP 52 | // but with last octet being '1' 53 | IPAddress gateway = local_ip; 54 | gateway[3] = 1; 55 | begin(mac_address, local_ip, dns_server, gateway); 56 | } 57 | 58 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) 59 | { 60 | IPAddress subnet(255, 255, 255, 0); 61 | begin(mac_address, local_ip, dns_server, gateway, subnet); 62 | } 63 | 64 | void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) 65 | { 66 | W5100.init(); 67 | W5100.setMACAddress(mac); 68 | W5100.setIPAddress(local_ip.raw_address()); 69 | W5100.setGatewayIp(gateway.raw_address()); 70 | W5100.setSubnetMask(subnet.raw_address()); 71 | _dnsServerAddress = dns_server; 72 | } 73 | 74 | #else 75 | int EthernetClass::begin(uint8_t *mac_address) 76 | { 77 | _dhcp = new DhcpClass(); 78 | 79 | // Initialise the basic info 80 | W5100.init(); 81 | W5100.setMACAddress(mac_address); 82 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); 83 | 84 | // Now try to get our config info from a DHCP server 85 | int ret = _dhcp->beginWithDHCP(mac_address); 86 | if(ret == 1) 87 | { 88 | // We've successfully found a DHCP server and got our configuration info, so set things 89 | // accordingly 90 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 91 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 92 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 93 | _dnsServerAddress = _dhcp->getDnsServerIp(); 94 | } 95 | 96 | return ret; 97 | } 98 | 99 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip) 100 | { 101 | // Assume the DNS server will be the machine on the same network as the local IP 102 | // but with last octet being '1' 103 | IPAddress dns_server = local_ip; 104 | dns_server[3] = 1; 105 | begin(mac_address, local_ip, dns_server); 106 | } 107 | 108 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server) 109 | { 110 | // Assume the gateway will be the machine on the same network as the local IP 111 | // but with last octet being '1' 112 | IPAddress gateway = local_ip; 113 | gateway[3] = 1; 114 | begin(mac_address, local_ip, dns_server, gateway); 115 | } 116 | 117 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) 118 | { 119 | IPAddress subnet(255, 255, 255, 0); 120 | begin(mac_address, local_ip, dns_server, gateway, subnet); 121 | } 122 | 123 | void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) 124 | { 125 | W5100.init(); 126 | W5100.setMACAddress(mac); 127 | W5100.setIPAddress(local_ip.raw_address()); 128 | W5100.setGatewayIp(gateway.raw_address()); 129 | W5100.setSubnetMask(subnet.raw_address()); 130 | _dnsServerAddress = dns_server; 131 | } 132 | 133 | #endif 134 | 135 | int EthernetClass::maintain(){ 136 | int rc = DHCP_CHECK_NONE; 137 | if(_dhcp != NULL){ 138 | //we have a pointer to dhcp, use it 139 | rc = _dhcp->checkLease(); 140 | switch ( rc ){ 141 | case DHCP_CHECK_NONE: 142 | //nothing done 143 | break; 144 | case DHCP_CHECK_RENEW_OK: 145 | case DHCP_CHECK_REBIND_OK: 146 | //we might have got a new IP. 147 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 148 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 149 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 150 | _dnsServerAddress = _dhcp->getDnsServerIp(); 151 | break; 152 | default: 153 | //this is actually a error, it will retry though 154 | break; 155 | } 156 | } 157 | return rc; 158 | } 159 | 160 | IPAddress EthernetClass::localIP() 161 | { 162 | IPAddress ret; 163 | W5100.getIPAddress(ret.raw_address()); 164 | return ret; 165 | } 166 | 167 | IPAddress EthernetClass::subnetMask() 168 | { 169 | IPAddress ret; 170 | W5100.getSubnetMask(ret.raw_address()); 171 | return ret; 172 | } 173 | 174 | IPAddress EthernetClass::gatewayIP() 175 | { 176 | IPAddress ret; 177 | W5100.getGatewayIp(ret.raw_address()); 178 | return ret; 179 | } 180 | 181 | IPAddress EthernetClass::dnsServerIP() 182 | { 183 | return _dnsServerAddress; 184 | } 185 | 186 | EthernetClass Ethernet; 187 | -------------------------------------------------------------------------------- /src/EthernetUdp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 3 | * This version only offers minimal wrapping of socket.c/socket.h 4 | * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 5 | * 6 | * MIT License: 7 | * Copyright (c) 2008 Bjoern Hartmann 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | * bjoern@cs.stanford.edu 12/30/2008 27 | */ 28 | 29 | #include "utility/w5100.h" 30 | #include "utility/socket.h" 31 | #include "EthernetIndustruino.h" 32 | #include "Udp.h" 33 | #include "Dns.h" 34 | 35 | /* Constructor */ 36 | EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {} 37 | 38 | /* Start EthernetUDP socket, listening at local port PORT */ 39 | uint8_t EthernetUDP::begin(uint16_t port) { 40 | if (_sock != MAX_SOCK_NUM) 41 | return 0; 42 | 43 | for (int i = 0; i < MAX_SOCK_NUM; i++) { 44 | uint8_t s = W5100.readSnSR(i); 45 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { 46 | _sock = i; 47 | break; 48 | } 49 | } 50 | 51 | if (_sock == MAX_SOCK_NUM) 52 | return 0; 53 | 54 | _port = port; 55 | _remaining = 0; 56 | socket(_sock, SnMR::UDP, _port, 0); 57 | 58 | return 1; 59 | } 60 | 61 | /* return number of bytes available in the current packet, 62 | will return zero if parsePacket hasn't been called yet */ 63 | int EthernetUDP::available() { 64 | return _remaining; 65 | } 66 | 67 | /* Release any resources being used by this EthernetUDP instance */ 68 | void EthernetUDP::stop() 69 | { 70 | if (_sock == MAX_SOCK_NUM) 71 | return; 72 | 73 | close(_sock); 74 | 75 | EthernetClass::_server_port[_sock] = 0; 76 | _sock = MAX_SOCK_NUM; 77 | } 78 | 79 | int EthernetUDP::beginPacket(const char *host, uint16_t port) 80 | { 81 | // Look up the host first 82 | int ret = 0; 83 | DNSClient dns; 84 | IPAddress remote_addr; 85 | 86 | dns.begin(Ethernet.dnsServerIP()); 87 | ret = dns.getHostByName(host, remote_addr); 88 | if (ret == 1) { 89 | return beginPacket(remote_addr, port); 90 | } else { 91 | return ret; 92 | } 93 | } 94 | 95 | int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) 96 | { 97 | _offset = 0; 98 | return startUDP(_sock, rawIPAddress(ip), port); 99 | } 100 | 101 | int EthernetUDP::endPacket() 102 | { 103 | return sendUDP(_sock); 104 | } 105 | 106 | size_t EthernetUDP::write(uint8_t byte) 107 | { 108 | return write(&byte, 1); 109 | } 110 | 111 | size_t EthernetUDP::write(const uint8_t *buffer, size_t size) 112 | { 113 | uint16_t bytes_written = bufferData(_sock, _offset, buffer, size); 114 | _offset += bytes_written; 115 | return bytes_written; 116 | } 117 | 118 | int EthernetUDP::parsePacket() 119 | { 120 | // discard any remaining bytes in the last packet 121 | flush(); 122 | 123 | if (W5100.getRXReceivedSize(_sock) > 0) 124 | { 125 | //HACK - hand-parse the UDP packet using TCP recv method 126 | uint8_t tmpBuf[8]; 127 | int ret =0; 128 | //read 8 header bytes and get IP and port from it 129 | ret = recv(_sock,tmpBuf,8); 130 | if (ret > 0) 131 | { 132 | _remoteIP = tmpBuf; 133 | _remotePort = tmpBuf[4]; 134 | _remotePort = (_remotePort << 8) + tmpBuf[5]; 135 | _remaining = tmpBuf[6]; 136 | _remaining = (_remaining << 8) + tmpBuf[7]; 137 | 138 | // When we get here, any remaining bytes are the data 139 | ret = _remaining; 140 | } 141 | return ret; 142 | } 143 | // There aren't any packets available 144 | return 0; 145 | } 146 | 147 | int EthernetUDP::read() 148 | { 149 | uint8_t byte; 150 | 151 | if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0)) 152 | { 153 | // We read things without any problems 154 | _remaining--; 155 | return byte; 156 | } 157 | 158 | // If we get here, there's no data available 159 | return -1; 160 | } 161 | 162 | int EthernetUDP::read(unsigned char* buffer, size_t len) 163 | { 164 | 165 | if (_remaining > 0) 166 | { 167 | 168 | int got; 169 | 170 | if (_remaining <= len) 171 | { 172 | // data should fit in the buffer 173 | got = recv(_sock, buffer, _remaining); 174 | } 175 | else 176 | { 177 | // too much data for the buffer, 178 | // grab as much as will fit 179 | got = recv(_sock, buffer, len); 180 | } 181 | 182 | if (got > 0) 183 | { 184 | _remaining -= got; 185 | return got; 186 | } 187 | 188 | } 189 | 190 | // If we get here, there's no data available or recv failed 191 | return -1; 192 | 193 | } 194 | 195 | int EthernetUDP::peek() 196 | { 197 | uint8_t b; 198 | // Unlike recv, peek doesn't check to see if there's any data available, so we must. 199 | // If the user hasn't called parsePacket yet then return nothing otherwise they 200 | // may get the UDP header 201 | if (!_remaining) 202 | return -1; 203 | ::peek(_sock, &b); 204 | return b; 205 | } 206 | 207 | void EthernetUDP::flush() 208 | { 209 | // could this fail (loop endlessly) if _remaining > 0 and recv in read fails? 210 | // should only occur if recv fails after telling us the data is there, lets 211 | // hope the w5100 always behaves :) 212 | 213 | while (_remaining) 214 | { 215 | read(); 216 | } 217 | } 218 | 219 | -------------------------------------------------------------------------------- /src/utility/socket.cpp: -------------------------------------------------------------------------------- 1 | #include "w5100.h" 2 | #include "socket.h" 3 | 4 | static uint16_t local_port; 5 | 6 | /** 7 | * @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it. 8 | * @return 1 for success else 0. 9 | */ 10 | uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag) 11 | { 12 | if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE)) 13 | { 14 | close(s); 15 | W5100.writeSnMR(s, protocol | flag); 16 | if (port != 0) { 17 | W5100.writeSnPORT(s, port); 18 | } 19 | else { 20 | local_port++; // if don't set the source port, set local_port number. 21 | W5100.writeSnPORT(s, local_port); 22 | } 23 | 24 | W5100.execCmdSn(s, Sock_OPEN); 25 | 26 | return 1; 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | 33 | /** 34 | * @brief This function close the socket and parameter is "s" which represent the socket number 35 | */ 36 | void close(SOCKET s) 37 | { 38 | W5100.execCmdSn(s, Sock_CLOSE); 39 | W5100.writeSnIR(s, 0xFF); 40 | } 41 | 42 | 43 | /** 44 | * @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer. 45 | * @return 1 for success else 0. 46 | */ 47 | uint8_t listen(SOCKET s) 48 | { 49 | if (W5100.readSnSR(s) != SnSR::INIT) 50 | return 0; 51 | W5100.execCmdSn(s, Sock_LISTEN); 52 | return 1; 53 | } 54 | 55 | 56 | /** 57 | * @brief This function established the connection for the channel in Active (client) mode. 58 | * This function waits for the untill the connection is established. 59 | * 60 | * @return 1 for success else 0. 61 | */ 62 | uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port) 63 | { 64 | if 65 | ( 66 | ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) || 67 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 68 | (port == 0x00) 69 | ) 70 | return 0; 71 | 72 | // set destination IP 73 | W5100.writeSnDIPR(s, addr); 74 | W5100.writeSnDPORT(s, port); 75 | W5100.execCmdSn(s, Sock_CONNECT); 76 | 77 | return 1; 78 | } 79 | 80 | 81 | 82 | /** 83 | * @brief This function used for disconnect the socket and parameter is "s" which represent the socket number 84 | * @return 1 for success else 0. 85 | */ 86 | void disconnect(SOCKET s) 87 | { 88 | W5100.execCmdSn(s, Sock_DISCON); 89 | } 90 | 91 | 92 | /** 93 | * @brief This function used to send the data in TCP mode 94 | * @return 1 for success else 0. 95 | */ 96 | uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len) 97 | { 98 | uint8_t status=0; 99 | uint16_t ret=0; 100 | uint16_t freesize=0; 101 | 102 | if (len > W5100.SSIZE) 103 | ret = W5100.SSIZE; // check size not to exceed MAX size. 104 | else 105 | ret = len; 106 | 107 | // if freebuf is available, start. 108 | do 109 | { 110 | freesize = W5100.getTXFreeSize(s); 111 | status = W5100.readSnSR(s); 112 | if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT)) 113 | { 114 | ret = 0; 115 | break; 116 | } 117 | } 118 | while (freesize < ret); 119 | 120 | // copy data 121 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 122 | W5100.execCmdSn(s, Sock_SEND); 123 | 124 | /* +2008.01 bj */ 125 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 126 | { 127 | /* m2008.01 [bj] : reduce code */ 128 | if ( W5100.readSnSR(s) == SnSR::CLOSED ) 129 | { 130 | close(s); 131 | return 0; 132 | } 133 | } 134 | /* +2008.01 bj */ 135 | W5100.writeSnIR(s, SnIR::SEND_OK); 136 | return ret; 137 | } 138 | 139 | 140 | /** 141 | * @brief This function is an application I/F function which is used to receive the data in TCP mode. 142 | * It continues to wait for data as much as the application wants to receive. 143 | * 144 | * @return received data size for success else -1. 145 | */ 146 | int16_t recv(SOCKET s, uint8_t *buf, int16_t len) 147 | { 148 | // Check how much data is available 149 | int16_t ret = W5100.getRXReceivedSize(s); 150 | if ( ret == 0 ) 151 | { 152 | // No data available. 153 | uint8_t status = W5100.readSnSR(s); 154 | if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT ) 155 | { 156 | // The remote end has closed its side of the connection, so this is the eof state 157 | ret = 0; 158 | } 159 | else 160 | { 161 | // The connection is still up, but there's no data waiting to be read 162 | ret = -1; 163 | } 164 | } 165 | else if (ret > len) 166 | { 167 | ret = len; 168 | } 169 | 170 | if ( ret > 0 ) 171 | { 172 | W5100.recv_data_processing(s, buf, ret); 173 | W5100.execCmdSn(s, Sock_RECV); 174 | } 175 | return ret; 176 | } 177 | 178 | 179 | /** 180 | * @brief Returns the first byte in the receive queue (no checking) 181 | * 182 | * @return 183 | */ 184 | uint16_t peek(SOCKET s, uint8_t *buf) 185 | { 186 | W5100.recv_data_processing(s, buf, 1, 1); 187 | 188 | return 1; 189 | } 190 | 191 | 192 | /** 193 | * @brief This function is an application I/F function which is used to send the data for other then TCP mode. 194 | * Unlike TCP transmission, The peer's destination address and the port is needed. 195 | * 196 | * @return This function return send data size for success else -1. 197 | */ 198 | uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port) 199 | { 200 | uint16_t ret=0; 201 | 202 | if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size. 203 | else ret = len; 204 | 205 | if 206 | ( 207 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 208 | ((port == 0x00)) ||(ret == 0) 209 | ) 210 | { 211 | /* +2008.01 [bj] : added return value */ 212 | ret = 0; 213 | } 214 | else 215 | { 216 | W5100.writeSnDIPR(s, addr); 217 | W5100.writeSnDPORT(s, port); 218 | 219 | // copy data 220 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 221 | W5100.execCmdSn(s, Sock_SEND); 222 | 223 | /* +2008.01 bj */ 224 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 225 | { 226 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 227 | { 228 | /* +2008.01 [bj]: clear interrupt */ 229 | W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */ 230 | return 0; 231 | } 232 | } 233 | 234 | /* +2008.01 bj */ 235 | W5100.writeSnIR(s, SnIR::SEND_OK); 236 | } 237 | return ret; 238 | } 239 | 240 | 241 | /** 242 | * @brief This function is an application I/F function which is used to receive the data in other then 243 | * TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well. 244 | * 245 | * @return This function return received data size for success else -1. 246 | */ 247 | uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port) 248 | { 249 | uint8_t head[8]; 250 | uint16_t data_len=0; 251 | uint16_t ptr=0; 252 | 253 | if ( len > 0 ) 254 | { 255 | ptr = W5100.readSnRX_RD(s); 256 | switch (W5100.readSnMR(s) & 0x07) 257 | { 258 | case SnMR::UDP : 259 | W5100.read_data(s, (uint8_t *)ptr, head, 0x08); 260 | ptr += 8; 261 | // read peer's IP address, port number. 262 | addr[0] = head[0]; 263 | addr[1] = head[1]; 264 | addr[2] = head[2]; 265 | addr[3] = head[3]; 266 | *port = head[4]; 267 | *port = (*port << 8) + head[5]; 268 | data_len = head[6]; 269 | data_len = (data_len << 8) + head[7]; 270 | 271 | W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy. 272 | ptr += data_len; 273 | 274 | W5100.writeSnRX_RD(s, ptr); 275 | break; 276 | 277 | case SnMR::IPRAW : 278 | W5100.read_data(s, (uint8_t *)ptr, head, 0x06); 279 | ptr += 6; 280 | 281 | addr[0] = head[0]; 282 | addr[1] = head[1]; 283 | addr[2] = head[2]; 284 | addr[3] = head[3]; 285 | data_len = head[4]; 286 | data_len = (data_len << 8) + head[5]; 287 | 288 | W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy. 289 | ptr += data_len; 290 | 291 | W5100.writeSnRX_RD(s, ptr); 292 | break; 293 | 294 | case SnMR::MACRAW: 295 | W5100.read_data(s,(uint8_t*)ptr,head,2); 296 | ptr+=2; 297 | data_len = head[0]; 298 | data_len = (data_len<<8) + head[1] - 2; 299 | 300 | W5100.read_data(s,(uint8_t*) ptr,buf,data_len); 301 | ptr += data_len; 302 | W5100.writeSnRX_RD(s, ptr); 303 | break; 304 | 305 | default : 306 | break; 307 | } 308 | W5100.execCmdSn(s, Sock_RECV); 309 | } 310 | return data_len; 311 | } 312 | 313 | 314 | uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len) 315 | { 316 | uint8_t status=0; 317 | uint16_t ret=0; 318 | 319 | if (len > W5100.SSIZE) 320 | ret = W5100.SSIZE; // check size not to exceed MAX size. 321 | else 322 | ret = len; 323 | 324 | if (ret == 0) 325 | return 0; 326 | 327 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 328 | W5100.execCmdSn(s, Sock_SEND); 329 | 330 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 331 | { 332 | status = W5100.readSnSR(s); 333 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 334 | { 335 | /* in case of igmp, if send fails, then socket closed */ 336 | /* if you want change, remove this code. */ 337 | close(s); 338 | return 0; 339 | } 340 | } 341 | 342 | W5100.writeSnIR(s, SnIR::SEND_OK); 343 | return ret; 344 | } 345 | 346 | uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len) 347 | { 348 | uint16_t ret =0; 349 | if (len > W5100.getTXFreeSize(s)) 350 | { 351 | ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size. 352 | } 353 | else 354 | { 355 | ret = len; 356 | } 357 | W5100.send_data_processing_offset(s, offset, buf, ret); 358 | return ret; 359 | } 360 | 361 | int startUDP(SOCKET s, uint8_t* addr, uint16_t port) 362 | { 363 | if 364 | ( 365 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 366 | ((port == 0x00)) 367 | ) 368 | { 369 | return 0; 370 | } 371 | else 372 | { 373 | W5100.writeSnDIPR(s, addr); 374 | W5100.writeSnDPORT(s, port); 375 | return 1; 376 | } 377 | } 378 | 379 | int sendUDP(SOCKET s) 380 | { 381 | W5100.execCmdSn(s, Sock_SEND); 382 | 383 | /* +2008.01 bj */ 384 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 385 | { 386 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 387 | { 388 | /* +2008.01 [bj]: clear interrupt */ 389 | W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT)); 390 | return 0; 391 | } 392 | } 393 | 394 | /* +2008.01 bj */ 395 | W5100.writeSnIR(s, SnIR::SEND_OK); 396 | 397 | /* Sent ok */ 398 | return 1; 399 | } 400 | 401 | -------------------------------------------------------------------------------- /src/Dns.cpp: -------------------------------------------------------------------------------- 1 | // Arduino DNS client for WizNet5100-based Ethernet shield 2 | // (c) Copyright 2009-2010 MCQN Ltd. 3 | // Released under Apache License, version 2.0 4 | 5 | #include "utility/w5100.h" 6 | #include "EthernetUdp.h" 7 | #include "util.h" 8 | 9 | #include "Dns.h" 10 | #include 11 | //#include 12 | #include "Arduino.h" 13 | 14 | 15 | #define SOCKET_NONE 255 16 | // Various flags and header field values for a DNS message 17 | #define UDP_HEADER_SIZE 8 18 | #define DNS_HEADER_SIZE 12 19 | #define TTL_SIZE 4 20 | #define QUERY_FLAG (0) 21 | #define RESPONSE_FLAG (1<<15) 22 | #define QUERY_RESPONSE_MASK (1<<15) 23 | #define OPCODE_STANDARD_QUERY (0) 24 | #define OPCODE_INVERSE_QUERY (1<<11) 25 | #define OPCODE_STATUS_REQUEST (2<<11) 26 | #define OPCODE_MASK (15<<11) 27 | #define AUTHORITATIVE_FLAG (1<<10) 28 | #define TRUNCATION_FLAG (1<<9) 29 | #define RECURSION_DESIRED_FLAG (1<<8) 30 | #define RECURSION_AVAILABLE_FLAG (1<<7) 31 | #define RESP_NO_ERROR (0) 32 | #define RESP_FORMAT_ERROR (1) 33 | #define RESP_SERVER_FAILURE (2) 34 | #define RESP_NAME_ERROR (3) 35 | #define RESP_NOT_IMPLEMENTED (4) 36 | #define RESP_REFUSED (5) 37 | #define RESP_MASK (15) 38 | #define TYPE_A (0x0001) 39 | #define CLASS_IN (0x0001) 40 | #define LABEL_COMPRESSION_MASK (0xC0) 41 | // Port number that DNS servers listen on 42 | #define DNS_PORT 53 43 | 44 | // Possible return codes from ProcessResponse 45 | #define SUCCESS 1 46 | #define TIMED_OUT -1 47 | #define INVALID_SERVER -2 48 | #define TRUNCATED -3 49 | #define INVALID_RESPONSE -4 50 | 51 | void DNSClient::begin(const IPAddress& aDNSServer) 52 | { 53 | iDNSServer = aDNSServer; 54 | iRequestId = 0; 55 | } 56 | 57 | 58 | int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult) 59 | { 60 | // See if we've been given a valid IP address 61 | const char* p =aIPAddrString; 62 | while (*p && 63 | ( (*p == '.') || (*p >= '0') || (*p <= '9') )) 64 | { 65 | p++; 66 | } 67 | 68 | if (*p == '\0') 69 | { 70 | // It's looking promising, we haven't found any invalid characters 71 | p = aIPAddrString; 72 | int segment =0; 73 | int segmentValue =0; 74 | while (*p && (segment < 4)) 75 | { 76 | if (*p == '.') 77 | { 78 | // We've reached the end of a segment 79 | if (segmentValue > 255) 80 | { 81 | // You can't have IP address segments that don't fit in a byte 82 | return 0; 83 | } 84 | else 85 | { 86 | aResult[segment] = (byte)segmentValue; 87 | segment++; 88 | segmentValue = 0; 89 | } 90 | } 91 | else 92 | { 93 | // Next digit 94 | segmentValue = (segmentValue*10)+(*p - '0'); 95 | } 96 | p++; 97 | } 98 | // We've reached the end of address, but there'll still be the last 99 | // segment to deal with 100 | if ((segmentValue > 255) || (segment > 3)) 101 | { 102 | // You can't have IP address segments that don't fit in a byte, 103 | // or more than four segments 104 | return 0; 105 | } 106 | else 107 | { 108 | aResult[segment] = (byte)segmentValue; 109 | return 1; 110 | } 111 | } 112 | else 113 | { 114 | return 0; 115 | } 116 | } 117 | 118 | int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) 119 | { 120 | int ret =0; 121 | 122 | // See if it's a numeric IP address 123 | if (inet_aton(aHostname, aResult)) 124 | { 125 | // It is, our work here is done 126 | return 1; 127 | } 128 | 129 | // Check we've got a valid DNS server to use 130 | if (iDNSServer == INADDR_NONE) 131 | { 132 | return INVALID_SERVER; 133 | } 134 | 135 | // Find a socket to use 136 | if (iUdp.begin(1024+(millis() & 0xF)) == 1) 137 | { 138 | // Try up to three times 139 | int retries = 0; 140 | // while ((retries < 3) && (ret <= 0)) 141 | { 142 | // Send DNS request 143 | ret = iUdp.beginPacket(iDNSServer, DNS_PORT); 144 | if (ret != 0) 145 | { 146 | // Now output the request data 147 | ret = BuildRequest(aHostname); 148 | if (ret != 0) 149 | { 150 | // And finally send the request 151 | ret = iUdp.endPacket(); 152 | if (ret != 0) 153 | { 154 | // Now wait for a response 155 | int wait_retries = 0; 156 | ret = TIMED_OUT; 157 | while ((wait_retries < 3) && (ret == TIMED_OUT)) 158 | { 159 | ret = ProcessResponse(5000, aResult); 160 | wait_retries++; 161 | } 162 | } 163 | } 164 | } 165 | retries++; 166 | } 167 | 168 | // We're done with the socket now 169 | iUdp.stop(); 170 | } 171 | 172 | return ret; 173 | } 174 | 175 | uint16_t DNSClient::BuildRequest(const char* aName) 176 | { 177 | // Build header 178 | // 1 1 1 1 1 1 179 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 180 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 181 | // | ID | 182 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 183 | // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | 184 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 185 | // | QDCOUNT | 186 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 187 | // | ANCOUNT | 188 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 189 | // | NSCOUNT | 190 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 191 | // | ARCOUNT | 192 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 193 | // As we only support one request at a time at present, we can simplify 194 | // some of this header 195 | iRequestId = millis(); // generate a random ID 196 | uint16_t twoByteBuffer; 197 | 198 | // FIXME We should also check that there's enough space available to write to, rather 199 | // FIXME than assume there's enough space (as the code does at present) 200 | iUdp.write((uint8_t*)&iRequestId, sizeof(iRequestId)); 201 | 202 | twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG); 203 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 204 | 205 | twoByteBuffer = htons(1); // One question record 206 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 207 | 208 | twoByteBuffer = 0; // Zero answer records 209 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 210 | 211 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 212 | // and zero additional records 213 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 214 | 215 | // Build question 216 | const char* start =aName; 217 | const char* end =start; 218 | uint8_t len; 219 | // Run through the name being requested 220 | while (*end) 221 | { 222 | // Find out how long this section of the name is 223 | end = start; 224 | while (*end && (*end != '.') ) 225 | { 226 | end++; 227 | } 228 | 229 | if (end-start > 0) 230 | { 231 | // Write out the size of this section 232 | len = end-start; 233 | iUdp.write(&len, sizeof(len)); 234 | // And then write out the section 235 | iUdp.write((uint8_t*)start, end-start); 236 | } 237 | start = end+1; 238 | } 239 | 240 | // We've got to the end of the question name, so 241 | // terminate it with a zero-length section 242 | len = 0; 243 | iUdp.write(&len, sizeof(len)); 244 | // Finally the type and class of question 245 | twoByteBuffer = htons(TYPE_A); 246 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 247 | 248 | twoByteBuffer = htons(CLASS_IN); // Internet class of question 249 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 250 | // Success! Everything buffered okay 251 | return 1; 252 | } 253 | 254 | 255 | uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) 256 | { 257 | uint32_t startTime = millis(); 258 | 259 | // Wait for a response packet 260 | while(iUdp.parsePacket() <= 0) 261 | { 262 | if((millis() - startTime) > aTimeout) 263 | return TIMED_OUT; 264 | delay(50); 265 | } 266 | 267 | // We've had a reply! 268 | // Read the UDP header 269 | uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header 270 | // Check that it's a response from the right server and the right port 271 | if ( (iDNSServer != iUdp.remoteIP()) || 272 | (iUdp.remotePort() != DNS_PORT) ) 273 | { 274 | // It's not from who we expected 275 | return INVALID_SERVER; 276 | } 277 | 278 | // Read through the rest of the response 279 | if (iUdp.available() < DNS_HEADER_SIZE) 280 | { 281 | return TRUNCATED; 282 | } 283 | iUdp.read(header, DNS_HEADER_SIZE); 284 | 285 | uint16_t header_flags = htons(*((uint16_t*)&header[2])); 286 | // Check that it's a response to this request 287 | if ( ( iRequestId != (*((uint16_t*)&header[0])) ) || 288 | ((header_flags & QUERY_RESPONSE_MASK) != (uint16_t)RESPONSE_FLAG) ) 289 | { 290 | // Mark the entire packet as read 291 | iUdp.flush(); 292 | return INVALID_RESPONSE; 293 | } 294 | // Check for any errors in the response (or in our request) 295 | // although we don't do anything to get round these 296 | if ( (header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK) ) 297 | { 298 | // Mark the entire packet as read 299 | iUdp.flush(); 300 | return -5; //INVALID_RESPONSE; 301 | } 302 | 303 | // And make sure we've got (at least) one answer 304 | uint16_t answerCount = htons(*((uint16_t*)&header[6])); 305 | if (answerCount == 0 ) 306 | { 307 | // Mark the entire packet as read 308 | iUdp.flush(); 309 | return -6; //INVALID_RESPONSE; 310 | } 311 | 312 | // Skip over any questions 313 | for (uint16_t i =0; i < htons(*((uint16_t*)&header[4])); i++) 314 | { 315 | // Skip over the name 316 | uint8_t len; 317 | do 318 | { 319 | iUdp.read(&len, sizeof(len)); 320 | if (len > 0) 321 | { 322 | // Don't need to actually read the data out for the string, just 323 | // advance ptr to beyond it 324 | while(len--) 325 | { 326 | iUdp.read(); // we don't care about the returned byte 327 | } 328 | } 329 | } while (len != 0); 330 | 331 | // Now jump over the type and class 332 | for (int i =0; i < 4; i++) 333 | { 334 | iUdp.read(); // we don't care about the returned byte 335 | } 336 | } 337 | 338 | // Now we're up to the bit we're interested in, the answer 339 | // There might be more than one answer (although we'll just use the first 340 | // type A answer) and some authority and additional resource records but 341 | // we're going to ignore all of them. 342 | 343 | for (uint16_t i =0; i < answerCount; i++) 344 | { 345 | // Skip the name 346 | uint8_t len; 347 | do 348 | { 349 | iUdp.read(&len, sizeof(len)); 350 | if ((len & LABEL_COMPRESSION_MASK) == 0) 351 | { 352 | // It's just a normal label 353 | if (len > 0) 354 | { 355 | // And it's got a length 356 | // Don't need to actually read the data out for the string, 357 | // just advance ptr to beyond it 358 | while(len--) 359 | { 360 | iUdp.read(); // we don't care about the returned byte 361 | } 362 | } 363 | } 364 | else 365 | { 366 | // This is a pointer to a somewhere else in the message for the 367 | // rest of the name. We don't care about the name, and RFC1035 368 | // says that a name is either a sequence of labels ended with a 369 | // 0 length octet or a pointer or a sequence of labels ending in 370 | // a pointer. Either way, when we get here we're at the end of 371 | // the name 372 | // Skip over the pointer 373 | iUdp.read(); // we don't care about the returned byte 374 | // And set len so that we drop out of the name loop 375 | len = 0; 376 | } 377 | } while (len != 0); 378 | 379 | // Check the type and class 380 | uint16_t answerType; 381 | uint16_t answerClass; 382 | iUdp.read((uint8_t*)&answerType, sizeof(answerType)); 383 | iUdp.read((uint8_t*)&answerClass, sizeof(answerClass)); 384 | 385 | // Ignore the Time-To-Live as we don't do any caching 386 | for (int i =0; i < TTL_SIZE; i++) 387 | { 388 | iUdp.read(); // we don't care about the returned byte 389 | } 390 | 391 | // And read out the length of this answer 392 | // Don't need header_flags anymore, so we can reuse it here 393 | iUdp.read((uint8_t*)&header_flags, sizeof(header_flags)); 394 | 395 | if ( (htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN) ) 396 | { 397 | if (htons(header_flags) != 4) 398 | { 399 | // It's a weird size 400 | // Mark the entire packet as read 401 | iUdp.flush(); 402 | return -9;//INVALID_RESPONSE; 403 | } 404 | iUdp.read(aAddress.raw_address(), 4); 405 | return SUCCESS; 406 | } 407 | else 408 | { 409 | // This isn't an answer type we're after, move onto the next one 410 | for (uint16_t i =0; i < htons(header_flags); i++) 411 | { 412 | iUdp.read(); // we don't care about the returned byte 413 | } 414 | } 415 | } 416 | 417 | // Mark the entire packet as read 418 | iUdp.flush(); 419 | 420 | // If we get here then we haven't found an answer 421 | return -10;//INVALID_RESPONSE; 422 | } 423 | 424 | -------------------------------------------------------------------------------- /src/utility/w5200.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #ifndef W5200_H_INCLUDED 11 | #define W5200_H_INCLUDED 12 | 13 | //#include 14 | //#include 15 | 16 | #define MAX_SOCK_NUM 8 17 | //typedef uint8_t SOCKET; 18 | /* 19 | class MR { 20 | public: 21 | static const uint8_t RST = 0x80; 22 | static const uint8_t PB = 0x10; 23 | static const uint8_t PPPOE = 0x08; 24 | static const uint8_t LB = 0x04; 25 | static const uint8_t AI = 0x02; 26 | static const uint8_t IND = 0x01; 27 | }; 28 | */ 29 | /* 30 | class IR { 31 | public: 32 | static const uint8_t CONFLICT = 0x80; 33 | static const uint8_t UNREACH = 0x40; 34 | static const uint8_t PPPoE = 0x20; 35 | static const uint8_t SOCK0 = 0x01; 36 | static const uint8_t SOCK1 = 0x02; 37 | static const uint8_t SOCK2 = 0x04; 38 | static const uint8_t SOCK3 = 0x08; 39 | static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); }; 40 | }; 41 | */ 42 | 43 | class SnMR { 44 | public: 45 | static const uint8_t CLOSE = 0x00; 46 | static const uint8_t TCP = 0x01; 47 | static const uint8_t UDP = 0x02; 48 | static const uint8_t IPRAW = 0x03; 49 | static const uint8_t MACRAW = 0x04; 50 | static const uint8_t PPPOE = 0x05; 51 | static const uint8_t ND = 0x20; 52 | static const uint8_t MULTI = 0x80; 53 | }; 54 | 55 | enum SockCMD { 56 | Sock_OPEN = 0x01, 57 | Sock_LISTEN = 0x02, 58 | Sock_CONNECT = 0x04, 59 | Sock_DISCON = 0x08, 60 | Sock_CLOSE = 0x10, 61 | Sock_SEND = 0x20, 62 | Sock_SEND_MAC = 0x21, 63 | Sock_SEND_KEEP = 0x22, 64 | Sock_RECV = 0x40 65 | }; 66 | 67 | /*class SnCmd { 68 | public: 69 | static const uint8_t OPEN = 0x01; 70 | static const uint8_t LISTEN = 0x02; 71 | static const uint8_t CONNECT = 0x04; 72 | static const uint8_t DISCON = 0x08; 73 | static const uint8_t CLOSE = 0x10; 74 | static const uint8_t SEND = 0x20; 75 | static const uint8_t SEND_MAC = 0x21; 76 | static const uint8_t SEND_KEEP = 0x22; 77 | static const uint8_t RECV = 0x40; 78 | }; 79 | */ 80 | 81 | class SnIR { 82 | public: 83 | static const uint8_t SEND_OK = 0x10; 84 | static const uint8_t TIMEOUT = 0x08; 85 | static const uint8_t RECV = 0x04; 86 | static const uint8_t DISCON = 0x02; 87 | static const uint8_t CON = 0x01; 88 | }; 89 | 90 | class SnSR { 91 | public: 92 | static const uint8_t CLOSED = 0x00; 93 | static const uint8_t INIT = 0x13; 94 | static const uint8_t LISTEN = 0x14; 95 | static const uint8_t SYNSENT = 0x15; 96 | static const uint8_t SYNRECV = 0x16; 97 | static const uint8_t ESTABLISHED = 0x17; 98 | static const uint8_t FIN_WAIT = 0x18; 99 | static const uint8_t CLOSING = 0x1A; 100 | static const uint8_t TIME_WAIT = 0x1B; 101 | static const uint8_t CLOSE_WAIT = 0x1C; 102 | static const uint8_t LAST_ACK = 0x1D; 103 | static const uint8_t UDP = 0x22; 104 | static const uint8_t IPRAW = 0x32; 105 | static const uint8_t MACRAW = 0x42; 106 | static const uint8_t PPPOE = 0x5F; 107 | }; 108 | 109 | class IPPROTO { 110 | public: 111 | static const uint8_t IP = 0; 112 | static const uint8_t ICMP = 1; 113 | static const uint8_t IGMP = 2; 114 | static const uint8_t GGP = 3; 115 | static const uint8_t TCP = 6; 116 | static const uint8_t PUP = 12; 117 | static const uint8_t UDP = 17; 118 | static const uint8_t IDP = 22; 119 | static const uint8_t ND = 77; 120 | static const uint8_t RAW = 255; 121 | }; 122 | 123 | class W5200Class { 124 | 125 | public: 126 | void init(); 127 | 128 | /** 129 | * @brief This function is being used for copy the data form Receive buffer of the chip to application buffer. 130 | * 131 | * It calculate the actual physical address where one has to read 132 | * the data from Receive buffer. Here also take care of the condition while it exceed 133 | * the Rx memory uper-bound of socket. 134 | */ 135 | void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len); 136 | 137 | /** 138 | * @brief This function is being called by send() and sendto() function also. 139 | * 140 | * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer 141 | * register. User should read upper byte first and lower byte later to get proper value. 142 | */ 143 | void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len); 144 | /** 145 | * @brief A copy of send_data_processing that uses the provided ptr for the 146 | * write offset. Only needed for the "streaming" UDP API, where 147 | * a single UDP packet is built up over a number of calls to 148 | * send_data_processing_ptr, because TX_WR doesn't seem to get updated 149 | * correctly in those scenarios 150 | * @param ptr value to use in place of TX_WR. If 0, then the value is read 151 | * in from TX_WR 152 | * @return New value for ptr, to be used in the next call 153 | */ 154 | // FIXME Update documentation 155 | void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len); 156 | 157 | /** 158 | * @brief This function is being called by recv() also. 159 | * 160 | * This function read the Rx read pointer register 161 | * and after copy the data from receive buffer update the Rx write pointer register. 162 | * User should read upper byte first and lower byte later to get proper value. 163 | */ 164 | void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0); 165 | 166 | inline void setGatewayIp(uint8_t *_addr); 167 | inline void getGatewayIp(uint8_t *_addr); 168 | 169 | inline void setSubnetMask(uint8_t *_addr); 170 | inline void getSubnetMask(uint8_t *_addr); 171 | 172 | inline void setMACAddress(uint8_t * addr); 173 | inline void getMACAddress(uint8_t * addr); 174 | 175 | inline void setIPAddress(uint8_t * addr); 176 | inline void getIPAddress(uint8_t * addr); 177 | 178 | inline void setRetransmissionTime(uint16_t timeout); 179 | inline void setRetransmissionCount(uint8_t _retry); 180 | 181 | void execCmdSn(SOCKET s, SockCMD _cmd); 182 | 183 | uint16_t getTXFreeSize(SOCKET s); 184 | uint16_t getRXReceivedSize(SOCKET s); 185 | 186 | 187 | // W5100 Registers 188 | // --------------- 189 | private: 190 | static uint8_t write(uint16_t _addr, uint8_t _data); 191 | static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len); 192 | static uint8_t read(uint16_t addr); 193 | static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len); 194 | 195 | #define __GP_REGISTER8(name, address) \ 196 | static inline void write##name(uint8_t _data) { \ 197 | write(address, _data); \ 198 | } \ 199 | static inline uint8_t read##name() { \ 200 | return read(address); \ 201 | } 202 | #define __GP_REGISTER16(name, address) \ 203 | static void write##name(uint16_t _data) { \ 204 | write(address, _data >> 8); \ 205 | write(address+1, _data & 0xFF); \ 206 | } \ 207 | static uint16_t read##name() { \ 208 | uint16_t res = read(address); \ 209 | res = (res << 8) + read(address + 1); \ 210 | return res; \ 211 | } 212 | #define __GP_REGISTER_N(name, address, size) \ 213 | static uint16_t write##name(uint8_t *_buff) { \ 214 | return write(address, _buff, size); \ 215 | } \ 216 | static uint16_t read##name(uint8_t *_buff) { \ 217 | return read(address, _buff, size); \ 218 | } 219 | 220 | public: 221 | __GP_REGISTER8 (MR, 0x0000); // Mode 222 | __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address 223 | __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address 224 | __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address 225 | __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address 226 | __GP_REGISTER8 (IR, 0x0015); // Interrupt 227 | __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask 228 | __GP_REGISTER16(RTR, 0x0017); // Timeout address 229 | __GP_REGISTER8 (RCR, 0x0019); // Retry count 230 | __GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode 231 | __GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer 232 | __GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number 233 | 234 | #undef __GP_REGISTER8 235 | #undef __GP_REGISTER16 236 | #undef __GP_REGISTER_N 237 | 238 | // W5100 Socket registers 239 | // ---------------------- 240 | private: 241 | static inline uint8_t readSn(SOCKET _s, uint16_t _addr); 242 | static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data); 243 | static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 244 | static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 245 | 246 | 247 | static const uint16_t CH_BASE = 0x4000; 248 | static const uint16_t CH_SIZE = 0x0100; 249 | 250 | #define __SOCKET_REGISTER8(name, address) \ 251 | static inline void write##name(SOCKET _s, uint8_t _data) { \ 252 | writeSn(_s, address, _data); \ 253 | } \ 254 | static inline uint8_t read##name(SOCKET _s) { \ 255 | return readSn(_s, address); \ 256 | } 257 | #define __SOCKET_REGISTER16(name, address) \ 258 | static void write##name(SOCKET _s, uint16_t _data) { \ 259 | writeSn(_s, address, _data >> 8); \ 260 | writeSn(_s, address+1, _data & 0xFF); \ 261 | } \ 262 | static uint16_t read##name(SOCKET _s) { \ 263 | uint16_t res = readSn(_s, address); \ 264 | uint16_t res2 = readSn(_s,address + 1); \ 265 | res = res << 8; \ 266 | res2 = res2 & 0xFF; \ 267 | res = res | res2; \ 268 | return res; \ 269 | } 270 | #define __SOCKET_REGISTER_N(name, address, size) \ 271 | static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ 272 | return writeSn(_s, address, _buff, size); \ 273 | } \ 274 | static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ 275 | return readSn(_s, address, _buff, size); \ 276 | } 277 | 278 | public: 279 | __SOCKET_REGISTER8(SnMR, 0x0000) // Mode 280 | __SOCKET_REGISTER8(SnCR, 0x0001) // Command 281 | __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt 282 | __SOCKET_REGISTER8(SnSR, 0x0003) // Status 283 | __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port 284 | __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr 285 | __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr 286 | __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port 287 | __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size 288 | __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode 289 | __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS 290 | __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL 291 | __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size 292 | __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer 293 | __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer 294 | __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size 295 | __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer 296 | __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) 297 | 298 | #undef __SOCKET_REGISTER8 299 | #undef __SOCKET_REGISTER16 300 | #undef __SOCKET_REGISTER_N 301 | 302 | 303 | private: 304 | static const uint8_t RST = 7; // Reset BIT 305 | static const int SOCKETS = 8; 306 | static const uint16_t SMASK = 0x07FF; // Tx buffer MASK 307 | static const uint16_t RMASK = 0x07FF; // Rx buffer MASK 308 | public: 309 | static const uint16_t SSIZE = 2048; // Max Tx buffer size 310 | private: 311 | static const uint16_t RSIZE = 2048; // Max Rx buffer size 312 | uint16_t SBASE[SOCKETS]; // Tx buffer base address 313 | uint16_t RBASE[SOCKETS]; // Rx buffer base address 314 | 315 | private: 316 | #if defined(REL_GR_KURUMI) || defined(REL_GR_KURUMI_PROTOTYPE) 317 | inline static void initSS() { pinMode(SS, OUTPUT); \ 318 | digitalWrite(SS, HIGH); }; 319 | inline static void setSS() { digitalWrite(SS, LOW); }; 320 | inline static void resetSS() { digitalWrite(SS, HIGH); }; 321 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 322 | inline static void initSS() { DDRB |= _BV(4); }; 323 | inline static void setSS() { PORTB &= ~_BV(4); }; 324 | inline static void resetSS() { PORTB |= _BV(4); }; 325 | #elif defined(__AVR_ATmega32U4__) 326 | inline static void initSS() { DDRB |= _BV(6); }; 327 | inline static void setSS() { PORTB &= ~_BV(6); }; 328 | inline static void resetSS() { PORTB |= _BV(6); }; 329 | #elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__) 330 | inline static void initSS() { DDRB |= _BV(0); }; 331 | inline static void setSS() { PORTB &= ~_BV(0); }; 332 | inline static void resetSS() { PORTB |= _BV(0); }; 333 | #else 334 | inline static void initSS() { DDRB |= _BV(2); }; 335 | inline static void setSS() { PORTB &= ~_BV(2); }; 336 | inline static void resetSS() { PORTB |= _BV(2); }; 337 | #endif 338 | 339 | }; 340 | 341 | extern W5200Class W5100; 342 | 343 | uint8_t W5200Class::readSn(SOCKET _s, uint16_t _addr) { 344 | return read(CH_BASE + _s * CH_SIZE + _addr); 345 | } 346 | 347 | uint8_t W5200Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) { 348 | return write(CH_BASE + _s * CH_SIZE + _addr, _data); 349 | } 350 | 351 | uint16_t W5200Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 352 | return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len); 353 | } 354 | 355 | uint16_t W5200Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 356 | return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len); 357 | } 358 | 359 | void W5200Class::getGatewayIp(uint8_t *_addr) { 360 | readGAR(_addr); 361 | } 362 | 363 | void W5200Class::setGatewayIp(uint8_t *_addr) { 364 | writeGAR(_addr); 365 | } 366 | 367 | void W5200Class::getSubnetMask(uint8_t *_addr) { 368 | readSUBR(_addr); 369 | } 370 | 371 | void W5200Class::setSubnetMask(uint8_t *_addr) { 372 | writeSUBR(_addr); 373 | } 374 | 375 | void W5200Class::getMACAddress(uint8_t *_addr) { 376 | readSHAR(_addr); 377 | } 378 | 379 | void W5200Class::setMACAddress(uint8_t *_addr) { 380 | writeSHAR(_addr); 381 | } 382 | 383 | void W5200Class::getIPAddress(uint8_t *_addr) { 384 | readSIPR(_addr); 385 | } 386 | 387 | void W5200Class::setIPAddress(uint8_t *_addr) { 388 | writeSIPR(_addr); 389 | } 390 | 391 | void W5200Class::setRetransmissionTime(uint16_t _timeout) { 392 | writeRTR(_timeout); 393 | } 394 | 395 | void W5200Class::setRetransmissionCount(uint8_t _retry) { 396 | writeRCR(_retry); 397 | } 398 | 399 | #endif 400 | -------------------------------------------------------------------------------- /src/Dhcp.cpp: -------------------------------------------------------------------------------- 1 | // DHCP Library v0.3 - April 25, 2009 2 | // Author: Jordan Terrell - blog.jordanterrell.com 3 | 4 | #include "utility/w5100.h" 5 | 6 | #include 7 | #include 8 | #include "Dhcp.h" 9 | #include "Arduino.h" 10 | #include "util.h" 11 | 12 | int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) 13 | { 14 | _dhcpLeaseTime=0; 15 | _dhcpT1=0; 16 | _dhcpT2=0; 17 | _lastCheck=0; 18 | _timeout = timeout; 19 | _responseTimeout = responseTimeout; 20 | 21 | // zero out _dhcpMacAddr 22 | memset(_dhcpMacAddr, 0, 6); 23 | reset_DHCP_lease(); 24 | 25 | memcpy((void*)_dhcpMacAddr, (void*)mac, 6); 26 | _dhcp_state = STATE_DHCP_START; 27 | return request_DHCP_lease(); 28 | } 29 | 30 | void DhcpClass::reset_DHCP_lease(){ 31 | // zero out _dhcpSubnetMask, _dhcpGatewayIp, _dhcpLocalIp, _dhcpDhcpServerIp, _dhcpDnsServerIp 32 | memset(_dhcpLocalIp, 0, 20); 33 | } 34 | 35 | //return:0 on error, 1 if request is sent and response is received 36 | int DhcpClass::request_DHCP_lease(){ 37 | 38 | uint8_t messageType = 0; 39 | 40 | 41 | 42 | // Pick an initial transaction ID 43 | _dhcpTransactionId = random(1UL, 2000UL); 44 | _dhcpInitialTransactionId = _dhcpTransactionId; 45 | 46 | if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) 47 | { 48 | // Couldn't get a socket 49 | return 0; 50 | } 51 | 52 | presend_DHCP(); 53 | 54 | int result = 0; 55 | 56 | unsigned long startTime = millis(); 57 | 58 | while(_dhcp_state != STATE_DHCP_LEASED) 59 | { 60 | if(_dhcp_state == STATE_DHCP_START) 61 | { 62 | _dhcpTransactionId++; 63 | 64 | send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - startTime) / 1000)); 65 | _dhcp_state = STATE_DHCP_DISCOVER; 66 | } 67 | else if(_dhcp_state == STATE_DHCP_REREQUEST){ 68 | _dhcpTransactionId++; 69 | send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime)/1000)); 70 | _dhcp_state = STATE_DHCP_REQUEST; 71 | } 72 | else if(_dhcp_state == STATE_DHCP_DISCOVER) 73 | { 74 | uint32_t respId; 75 | messageType = parseDHCPResponse(_responseTimeout, respId); 76 | if(messageType == DHCP_OFFER) 77 | { 78 | // We'll use the transaction ID that the offer came with, 79 | // rather than the one we were up to 80 | _dhcpTransactionId = respId; 81 | send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime) / 1000)); 82 | _dhcp_state = STATE_DHCP_REQUEST; 83 | } 84 | } 85 | else if(_dhcp_state == STATE_DHCP_REQUEST) 86 | { 87 | uint32_t respId; 88 | messageType = parseDHCPResponse(_responseTimeout, respId); 89 | if(messageType == DHCP_ACK) 90 | { 91 | _dhcp_state = STATE_DHCP_LEASED; 92 | result = 1; 93 | //use default lease time if we didn't get it 94 | if(_dhcpLeaseTime == 0){ 95 | _dhcpLeaseTime = DEFAULT_LEASE; 96 | } 97 | //calculate T1 & T2 if we didn't get it 98 | if(_dhcpT1 == 0){ 99 | //T1 should be 50% of _dhcpLeaseTime 100 | _dhcpT1 = _dhcpLeaseTime >> 1; 101 | } 102 | if(_dhcpT2 == 0){ 103 | //T2 should be 87.5% (7/8ths) of _dhcpLeaseTime 104 | _dhcpT2 = _dhcpT1 << 1; 105 | } 106 | _renewInSec = _dhcpT1; 107 | _rebindInSec = _dhcpT2; 108 | } 109 | else if(messageType == DHCP_NAK) 110 | _dhcp_state = STATE_DHCP_START; 111 | } 112 | 113 | if(messageType == 255) 114 | { 115 | messageType = 0; 116 | _dhcp_state = STATE_DHCP_START; 117 | } 118 | 119 | if(result != 1 && ((millis() - startTime) > _timeout)) 120 | break; 121 | } 122 | 123 | // We're done with the socket now 124 | _dhcpUdpSocket.stop(); 125 | _dhcpTransactionId++; 126 | 127 | return result; 128 | } 129 | 130 | void DhcpClass::presend_DHCP() 131 | { 132 | } 133 | 134 | void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) 135 | { 136 | uint8_t buffer[32]; 137 | memset(buffer, 0, 32); 138 | IPAddress dest_addr( 255, 255, 255, 255 ); // Broadcast address 139 | 140 | if (-1 == _dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT)) 141 | { 142 | // FIXME Need to return errors 143 | return; 144 | } 145 | 146 | buffer[0] = DHCP_BOOTREQUEST; // op 147 | buffer[1] = DHCP_HTYPE10MB; // htype 148 | buffer[2] = DHCP_HLENETHERNET; // hlen 149 | buffer[3] = DHCP_HOPS; // hops 150 | 151 | // xid 152 | unsigned long xid = htonl(_dhcpTransactionId); 153 | memcpy(buffer + 4, &(xid), 4); 154 | 155 | // 8, 9 - seconds elapsed 156 | buffer[8] = ((secondsElapsed & 0xff00) >> 8); 157 | buffer[9] = (secondsElapsed & 0x00ff); 158 | 159 | // flags 160 | unsigned short flags = htons(DHCP_FLAGSBROADCAST); 161 | memcpy(buffer + 10, &(flags), 2); 162 | 163 | // ciaddr: already zeroed 164 | // yiaddr: already zeroed 165 | // siaddr: already zeroed 166 | // giaddr: already zeroed 167 | 168 | //put data in W5100 transmit buffer 169 | _dhcpUdpSocket.write(buffer, 28); 170 | 171 | memset(buffer, 0, 32); // clear local buffer 172 | 173 | memcpy(buffer, _dhcpMacAddr, 6); // chaddr 174 | 175 | //put data in W5100 transmit buffer 176 | _dhcpUdpSocket.write(buffer, 16); 177 | 178 | memset(buffer, 0, 32); // clear local buffer 179 | 180 | // leave zeroed out for sname && file 181 | // put in W5100 transmit buffer x 6 (192 bytes) 182 | 183 | for(int i = 0; i < 6; i++) { 184 | _dhcpUdpSocket.write(buffer, 32); 185 | } 186 | 187 | // OPT - Magic Cookie 188 | buffer[0] = (uint8_t)((MAGIC_COOKIE >> 24)& 0xFF); 189 | buffer[1] = (uint8_t)((MAGIC_COOKIE >> 16)& 0xFF); 190 | buffer[2] = (uint8_t)((MAGIC_COOKIE >> 8)& 0xFF); 191 | buffer[3] = (uint8_t)(MAGIC_COOKIE& 0xFF); 192 | 193 | // OPT - message type 194 | buffer[4] = dhcpMessageType; 195 | buffer[5] = 0x01; 196 | buffer[6] = messageType; //DHCP_REQUEST; 197 | 198 | // OPT - client identifier 199 | buffer[7] = dhcpClientIdentifier; 200 | buffer[8] = 0x07; 201 | buffer[9] = 0x01; 202 | memcpy(buffer + 10, _dhcpMacAddr, 6); 203 | 204 | // OPT - host name 205 | buffer[16] = hostName; 206 | buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address 207 | strcpy((char*)&(buffer[18]), HOST_NAME); 208 | 209 | printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); 210 | printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); 211 | printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); 212 | 213 | //put data in W5100 transmit buffer 214 | _dhcpUdpSocket.write(buffer, 30); 215 | 216 | if(messageType == DHCP_REQUEST) 217 | { 218 | buffer[0] = dhcpRequestedIPaddr; 219 | buffer[1] = 0x04; 220 | buffer[2] = _dhcpLocalIp[0]; 221 | buffer[3] = _dhcpLocalIp[1]; 222 | buffer[4] = _dhcpLocalIp[2]; 223 | buffer[5] = _dhcpLocalIp[3]; 224 | 225 | buffer[6] = dhcpServerIdentifier; 226 | buffer[7] = 0x04; 227 | buffer[8] = _dhcpDhcpServerIp[0]; 228 | buffer[9] = _dhcpDhcpServerIp[1]; 229 | buffer[10] = _dhcpDhcpServerIp[2]; 230 | buffer[11] = _dhcpDhcpServerIp[3]; 231 | 232 | //put data in W5100 transmit buffer 233 | _dhcpUdpSocket.write(buffer, 12); 234 | } 235 | 236 | buffer[0] = dhcpParamRequest; 237 | buffer[1] = 0x06; 238 | buffer[2] = subnetMask; 239 | buffer[3] = routersOnSubnet; 240 | buffer[4] = dns; 241 | buffer[5] = domainName; 242 | buffer[6] = dhcpT1value; 243 | buffer[7] = dhcpT2value; 244 | buffer[8] = endOption; 245 | 246 | //put data in W5100 transmit buffer 247 | _dhcpUdpSocket.write(buffer, 9); 248 | 249 | _dhcpUdpSocket.endPacket(); 250 | } 251 | 252 | uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId) 253 | { 254 | uint8_t type = 0; 255 | uint8_t opt_len = 0; 256 | 257 | unsigned long startTime = millis(); 258 | 259 | while(_dhcpUdpSocket.parsePacket() <= 0) 260 | { 261 | if((millis() - startTime) > responseTimeout) 262 | { 263 | return 255; 264 | } 265 | delay(50); 266 | } 267 | // start reading in the packet 268 | RIP_MSG_FIXED fixedMsg; 269 | _dhcpUdpSocket.read((uint8_t*)&fixedMsg, sizeof(RIP_MSG_FIXED)); 270 | 271 | if(fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT) 272 | { 273 | transactionId = ntohl(fixedMsg.xid); 274 | if(memcmp(fixedMsg.chaddr, _dhcpMacAddr, 6) != 0 || (transactionId < _dhcpInitialTransactionId) || (transactionId > _dhcpTransactionId)) 275 | { 276 | // Need to read the rest of the packet here regardless 277 | _dhcpUdpSocket.flush(); 278 | return 0; 279 | } 280 | 281 | memcpy(_dhcpLocalIp, fixedMsg.yiaddr, 4); 282 | 283 | // Skip to the option part 284 | // Doing this a byte at a time so we don't have to put a big buffer 285 | // on the stack (as we don't have lots of memory lying around) 286 | for (int i =0; i < (240 - (int)sizeof(RIP_MSG_FIXED)); i++) 287 | { 288 | _dhcpUdpSocket.read(); // we don't care about the returned byte 289 | } 290 | 291 | while (_dhcpUdpSocket.available() > 0) 292 | { 293 | switch (_dhcpUdpSocket.read()) 294 | { 295 | case endOption : 296 | break; 297 | 298 | case padOption : 299 | break; 300 | 301 | case dhcpMessageType : 302 | opt_len = _dhcpUdpSocket.read(); 303 | type = _dhcpUdpSocket.read(); 304 | break; 305 | 306 | case subnetMask : 307 | opt_len = _dhcpUdpSocket.read(); 308 | _dhcpUdpSocket.read(_dhcpSubnetMask, 4); 309 | break; 310 | 311 | case routersOnSubnet : 312 | opt_len = _dhcpUdpSocket.read(); 313 | _dhcpUdpSocket.read(_dhcpGatewayIp, 4); 314 | for (int i = 0; i < opt_len-4; i++) 315 | { 316 | _dhcpUdpSocket.read(); 317 | } 318 | break; 319 | 320 | case dns : 321 | opt_len = _dhcpUdpSocket.read(); 322 | _dhcpUdpSocket.read(_dhcpDnsServerIp, 4); 323 | for (int i = 0; i < opt_len-4; i++) 324 | { 325 | _dhcpUdpSocket.read(); 326 | } 327 | break; 328 | 329 | case dhcpServerIdentifier : 330 | opt_len = _dhcpUdpSocket.read(); 331 | if( *((uint32_t*)_dhcpDhcpServerIp) == 0 || 332 | IPAddress(_dhcpDhcpServerIp) == _dhcpUdpSocket.remoteIP() ) 333 | { 334 | _dhcpUdpSocket.read(_dhcpDhcpServerIp, sizeof(_dhcpDhcpServerIp)); 335 | } 336 | else 337 | { 338 | // Skip over the rest of this option 339 | while (opt_len--) 340 | { 341 | _dhcpUdpSocket.read(); 342 | } 343 | } 344 | break; 345 | 346 | case dhcpT1value : 347 | opt_len = _dhcpUdpSocket.read(); 348 | _dhcpUdpSocket.read((uint8_t*)&_dhcpT1, sizeof(_dhcpT1)); 349 | _dhcpT1 = ntohl(_dhcpT1); 350 | break; 351 | 352 | case dhcpT2value : 353 | opt_len = _dhcpUdpSocket.read(); 354 | _dhcpUdpSocket.read((uint8_t*)&_dhcpT2, sizeof(_dhcpT2)); 355 | _dhcpT2 = ntohl(_dhcpT2); 356 | break; 357 | 358 | case dhcpIPaddrLeaseTime : 359 | opt_len = _dhcpUdpSocket.read(); 360 | _dhcpUdpSocket.read((uint8_t*)&_dhcpLeaseTime, sizeof(_dhcpLeaseTime)); 361 | _dhcpLeaseTime = ntohl(_dhcpLeaseTime); 362 | _renewInSec = _dhcpLeaseTime; 363 | break; 364 | 365 | default : 366 | opt_len = _dhcpUdpSocket.read(); 367 | // Skip over the rest of this option 368 | while (opt_len--) 369 | { 370 | _dhcpUdpSocket.read(); 371 | } 372 | break; 373 | } 374 | } 375 | } 376 | 377 | // Need to skip to end of the packet regardless here 378 | _dhcpUdpSocket.flush(); 379 | 380 | return type; 381 | } 382 | 383 | 384 | /* 385 | returns: 386 | 0/DHCP_CHECK_NONE: nothing happened 387 | 1/DHCP_CHECK_RENEW_FAIL: renew failed 388 | 2/DHCP_CHECK_RENEW_OK: renew success 389 | 3/DHCP_CHECK_REBIND_FAIL: rebind fail 390 | 4/DHCP_CHECK_REBIND_OK: rebind success 391 | */ 392 | int DhcpClass::checkLease(){ 393 | //this uses a signed / unsigned trick to deal with millis overflow 394 | unsigned long now = millis(); 395 | signed long snow = (long)now; 396 | int rc=DHCP_CHECK_NONE; 397 | if (_lastCheck != 0){ 398 | signed long factor; 399 | //calc how many ms past the timeout we are 400 | factor = snow - (long)_secTimeout; 401 | //if on or passed the timeout, reduce the counters 402 | if ( factor >= 0 ){ 403 | //next timeout should be now plus 1000 ms minus parts of second in factor 404 | _secTimeout = snow + 1000 - factor % 1000; 405 | //how many seconds late are we, minimum 1 406 | factor = factor / 1000 +1; 407 | 408 | //reduce the counters by that mouch 409 | //if we can assume that the cycle time (factor) is fairly constant 410 | //and if the remainder is less than cycle time * 2 411 | //do it early instead of late 412 | if(_renewInSec < factor*2 ) 413 | _renewInSec = 0; 414 | else 415 | _renewInSec -= factor; 416 | 417 | if(_rebindInSec < factor*2 ) 418 | _rebindInSec = 0; 419 | else 420 | _rebindInSec -= factor; 421 | } 422 | 423 | //if we have a lease but should renew, do it 424 | if (_dhcp_state == STATE_DHCP_LEASED && _renewInSec <=0){ 425 | _dhcp_state = STATE_DHCP_REREQUEST; 426 | rc = 1 + request_DHCP_lease(); 427 | } 428 | 429 | //if we have a lease or is renewing but should bind, do it 430 | if( (_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START) && _rebindInSec <=0){ 431 | //this should basically restart completely 432 | _dhcp_state = STATE_DHCP_START; 433 | reset_DHCP_lease(); 434 | rc = 3 + request_DHCP_lease(); 435 | } 436 | } 437 | else{ 438 | _secTimeout = snow + 1000; 439 | } 440 | 441 | _lastCheck = now; 442 | return rc; 443 | } 444 | 445 | IPAddress DhcpClass::getLocalIp() 446 | { 447 | return IPAddress(_dhcpLocalIp); 448 | } 449 | 450 | IPAddress DhcpClass::getSubnetMask() 451 | { 452 | return IPAddress(_dhcpSubnetMask); 453 | } 454 | 455 | IPAddress DhcpClass::getGatewayIp() 456 | { 457 | return IPAddress(_dhcpGatewayIp); 458 | } 459 | 460 | IPAddress DhcpClass::getDhcpServerIp() 461 | { 462 | return IPAddress(_dhcpDhcpServerIp); 463 | } 464 | 465 | IPAddress DhcpClass::getDnsServerIp() 466 | { 467 | return IPAddress(_dhcpDnsServerIp); 468 | } 469 | 470 | void DhcpClass::printByte(char * buf, uint8_t n ) { 471 | char *str = &buf[1]; 472 | buf[0]='0'; 473 | do { 474 | unsigned long m = n; 475 | n /= 16; 476 | char c = m - 16 * n; 477 | *str-- = c < 10 ? c + '0' : c + 'A' - 10; 478 | } while(n); 479 | } 480 | -------------------------------------------------------------------------------- /src/utility/w5100.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #ifndef W5100_H_INCLUDED 11 | #define W5100_H_INCLUDED 12 | 13 | #include 14 | #include 15 | 16 | typedef uint8_t SOCKET; 17 | 18 | //#define W5100_ETHERNET_SHIELD // Arduino Ethenret Shield and Compatibles ... 19 | //#define W5200_ETHERNET_SHIELD // WIZ820io, W5200 Ethernet Shield 20 | #define W5500_ETHERNET_SHIELD // WIZ550io, ioShield series of WIZnet 21 | 22 | #if defined(W5500_ETHERNET_SHIELD) 23 | #define WIZ550io_WITH_MACADDRESS // Use assigned MAC address of WIZ550io 24 | #include "w5500.h" 25 | #endif 26 | 27 | #if defined(W5200_ETHERNET_SHIELD) 28 | #include "w5200.h" 29 | #endif 30 | 31 | #if defined(W5100_ETHERNET_SHIELD) 32 | #define MAX_SOCK_NUM 4 33 | 34 | #define IDM_OR 0x8000 35 | #define IDM_AR0 0x8001 36 | #define IDM_AR1 0x8002 37 | #define IDM_DR 0x8003 38 | /* 39 | class MR { 40 | public: 41 | static const uint8_t RST = 0x80; 42 | static const uint8_t PB = 0x10; 43 | static const uint8_t PPPOE = 0x08; 44 | static const uint8_t LB = 0x04; 45 | static const uint8_t AI = 0x02; 46 | static const uint8_t IND = 0x01; 47 | }; 48 | */ 49 | /* 50 | class IR { 51 | public: 52 | static const uint8_t CONFLICT = 0x80; 53 | static const uint8_t UNREACH = 0x40; 54 | static const uint8_t PPPoE = 0x20; 55 | static const uint8_t SOCK0 = 0x01; 56 | static const uint8_t SOCK1 = 0x02; 57 | static const uint8_t SOCK2 = 0x04; 58 | static const uint8_t SOCK3 = 0x08; 59 | static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); }; 60 | }; 61 | */ 62 | 63 | class SnMR { 64 | public: 65 | static const uint8_t CLOSE = 0x00; 66 | static const uint8_t TCP = 0x01; 67 | static const uint8_t UDP = 0x02; 68 | static const uint8_t IPRAW = 0x03; 69 | static const uint8_t MACRAW = 0x04; 70 | static const uint8_t PPPOE = 0x05; 71 | static const uint8_t ND = 0x20; 72 | static const uint8_t MULTI = 0x80; 73 | }; 74 | 75 | enum SockCMD { 76 | Sock_OPEN = 0x01, 77 | Sock_LISTEN = 0x02, 78 | Sock_CONNECT = 0x04, 79 | Sock_DISCON = 0x08, 80 | Sock_CLOSE = 0x10, 81 | Sock_SEND = 0x20, 82 | Sock_SEND_MAC = 0x21, 83 | Sock_SEND_KEEP = 0x22, 84 | Sock_RECV = 0x40 85 | }; 86 | 87 | /*class SnCmd { 88 | public: 89 | static const uint8_t OPEN = 0x01; 90 | static const uint8_t LISTEN = 0x02; 91 | static const uint8_t CONNECT = 0x04; 92 | static const uint8_t DISCON = 0x08; 93 | static const uint8_t CLOSE = 0x10; 94 | static const uint8_t SEND = 0x20; 95 | static const uint8_t SEND_MAC = 0x21; 96 | static const uint8_t SEND_KEEP = 0x22; 97 | static const uint8_t RECV = 0x40; 98 | }; 99 | */ 100 | 101 | class SnIR { 102 | public: 103 | static const uint8_t SEND_OK = 0x10; 104 | static const uint8_t TIMEOUT = 0x08; 105 | static const uint8_t RECV = 0x04; 106 | static const uint8_t DISCON = 0x02; 107 | static const uint8_t CON = 0x01; 108 | }; 109 | 110 | class SnSR { 111 | public: 112 | static const uint8_t CLOSED = 0x00; 113 | static const uint8_t INIT = 0x13; 114 | static const uint8_t LISTEN = 0x14; 115 | static const uint8_t SYNSENT = 0x15; 116 | static const uint8_t SYNRECV = 0x16; 117 | static const uint8_t ESTABLISHED = 0x17; 118 | static const uint8_t FIN_WAIT = 0x18; 119 | static const uint8_t CLOSING = 0x1A; 120 | static const uint8_t TIME_WAIT = 0x1B; 121 | static const uint8_t CLOSE_WAIT = 0x1C; 122 | static const uint8_t LAST_ACK = 0x1D; 123 | static const uint8_t UDP = 0x22; 124 | static const uint8_t IPRAW = 0x32; 125 | static const uint8_t MACRAW = 0x42; 126 | static const uint8_t PPPOE = 0x5F; 127 | }; 128 | 129 | class IPPROTO { 130 | public: 131 | static const uint8_t IP = 0; 132 | static const uint8_t ICMP = 1; 133 | static const uint8_t IGMP = 2; 134 | static const uint8_t GGP = 3; 135 | static const uint8_t TCP = 6; 136 | static const uint8_t PUP = 12; 137 | static const uint8_t UDP = 17; 138 | static const uint8_t IDP = 22; 139 | static const uint8_t ND = 77; 140 | static const uint8_t RAW = 255; 141 | }; 142 | 143 | class W5100Class { 144 | 145 | public: 146 | void init(); 147 | 148 | /** 149 | * @brief This function is being used for copy the data form Receive buffer of the chip to application buffer. 150 | * 151 | * It calculate the actual physical address where one has to read 152 | * the data from Receive buffer. Here also take care of the condition while it exceed 153 | * the Rx memory uper-bound of socket. 154 | */ 155 | void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len); 156 | 157 | /** 158 | * @brief This function is being called by send() and sendto() function also. 159 | * 160 | * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer 161 | * register. User should read upper byte first and lower byte later to get proper value. 162 | */ 163 | void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len); 164 | /** 165 | * @brief A copy of send_data_processing that uses the provided ptr for the 166 | * write offset. Only needed for the "streaming" UDP API, where 167 | * a single UDP packet is built up over a number of calls to 168 | * send_data_processing_ptr, because TX_WR doesn't seem to get updated 169 | * correctly in those scenarios 170 | * @param ptr value to use in place of TX_WR. If 0, then the value is read 171 | * in from TX_WR 172 | * @return New value for ptr, to be used in the next call 173 | */ 174 | // FIXME Update documentation 175 | void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len); 176 | 177 | /** 178 | * @brief This function is being called by recv() also. 179 | * 180 | * This function read the Rx read pointer register 181 | * and after copy the data from receive buffer update the Rx write pointer register. 182 | * User should read upper byte first and lower byte later to get proper value. 183 | */ 184 | void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0); 185 | 186 | inline void setGatewayIp(uint8_t *_addr); 187 | inline void getGatewayIp(uint8_t *_addr); 188 | 189 | inline void setSubnetMask(uint8_t *_addr); 190 | inline void getSubnetMask(uint8_t *_addr); 191 | 192 | inline void setMACAddress(uint8_t *_addr); 193 | inline void getMACAddress(uint8_t *_addr); 194 | 195 | inline void setIPAddress(uint8_t *_addr); 196 | inline void getIPAddress(uint8_t *_addr); 197 | 198 | inline void setRetransmissionTime(uint16_t timeout); 199 | inline void setRetransmissionCount(uint8_t _retry); 200 | 201 | void execCmdSn(SOCKET s, SockCMD _cmd); 202 | 203 | uint16_t getTXFreeSize(SOCKET s); 204 | uint16_t getRXReceivedSize(SOCKET s); 205 | 206 | 207 | // W5100 Registers 208 | // --------------- 209 | private: 210 | static uint8_t write(uint16_t _addr, uint8_t _data); 211 | static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len); 212 | static uint8_t read(uint16_t addr); 213 | static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len); 214 | 215 | #define __GP_REGISTER8(name, address) \ 216 | static inline void write##name(uint8_t _data) { \ 217 | write(address, _data); \ 218 | } \ 219 | static inline uint8_t read##name() { \ 220 | return read(address); \ 221 | } 222 | #define __GP_REGISTER16(name, address) \ 223 | static void write##name(uint16_t _data) { \ 224 | write(address, _data >> 8); \ 225 | write(address+1, _data & 0xFF); \ 226 | } \ 227 | static uint16_t read##name() { \ 228 | uint16_t res = read(address); \ 229 | res = (res << 8) + read(address + 1); \ 230 | return res; \ 231 | } 232 | #define __GP_REGISTER_N(name, address, size) \ 233 | static uint16_t write##name(uint8_t *_buff) { \ 234 | return write(address, _buff, size); \ 235 | } \ 236 | static uint16_t read##name(uint8_t *_buff) { \ 237 | return read(address, _buff, size); \ 238 | } 239 | 240 | public: 241 | __GP_REGISTER8 (MR, 0x0000); // Mode 242 | __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address 243 | __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address 244 | __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address 245 | __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address 246 | __GP_REGISTER8 (IR, 0x0015); // Interrupt 247 | __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask 248 | __GP_REGISTER16(RTR, 0x0017); // Timeout address 249 | __GP_REGISTER8 (RCR, 0x0019); // Retry count 250 | __GP_REGISTER8 (RMSR, 0x001A); // Receive memory size 251 | __GP_REGISTER8 (TMSR, 0x001B); // Transmit memory size 252 | __GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode 253 | __GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer 254 | __GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number 255 | __GP_REGISTER_N(UIPR, 0x002A, 4); // Unreachable IP address in UDP mode 256 | __GP_REGISTER16(UPORT, 0x002E); // Unreachable Port address in UDP mode 257 | 258 | #undef __GP_REGISTER8 259 | #undef __GP_REGISTER16 260 | #undef __GP_REGISTER_N 261 | 262 | // W5100 Socket registers 263 | // ---------------------- 264 | private: 265 | static inline uint8_t readSn(SOCKET _s, uint16_t _addr); 266 | static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data); 267 | static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 268 | static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 269 | 270 | static const uint16_t CH_BASE = 0x0400; 271 | static const uint16_t CH_SIZE = 0x0100; 272 | 273 | #define __SOCKET_REGISTER8(name, address) \ 274 | static inline void write##name(SOCKET _s, uint8_t _data) { \ 275 | writeSn(_s, address, _data); \ 276 | } \ 277 | static inline uint8_t read##name(SOCKET _s) { \ 278 | return readSn(_s, address); \ 279 | } 280 | #define __SOCKET_REGISTER16(name, address) \ 281 | static void write##name(SOCKET _s, uint16_t _data) { \ 282 | writeSn(_s, address, _data >> 8); \ 283 | writeSn(_s, address+1, _data & 0xFF); \ 284 | } \ 285 | static uint16_t read##name(SOCKET _s) { \ 286 | uint16_t res = readSn(_s, address); \ 287 | uint16_t res2 = readSn(_s,address + 1); \ 288 | res = res << 8; \ 289 | res2 = res2 & 0xFF; \ 290 | res = res | res2; \ 291 | return res; \ 292 | } 293 | #define __SOCKET_REGISTER_N(name, address, size) \ 294 | static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ 295 | return writeSn(_s, address, _buff, size); \ 296 | } \ 297 | static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ 298 | return readSn(_s, address, _buff, size); \ 299 | } 300 | 301 | public: 302 | __SOCKET_REGISTER8(SnMR, 0x0000) // Mode 303 | __SOCKET_REGISTER8(SnCR, 0x0001) // Command 304 | __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt 305 | __SOCKET_REGISTER8(SnSR, 0x0003) // Status 306 | __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port 307 | __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr 308 | __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr 309 | __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port 310 | __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size 311 | __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode 312 | __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS 313 | __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL 314 | __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size 315 | __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer 316 | __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer 317 | __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size 318 | __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer 319 | __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) 320 | 321 | #undef __SOCKET_REGISTER8 322 | #undef __SOCKET_REGISTER16 323 | #undef __SOCKET_REGISTER_N 324 | 325 | 326 | private: 327 | static const uint8_t RST = 7; // Reset BIT 328 | 329 | static const int SOCKETS = 4; 330 | static const uint16_t SMASK = 0x07FF; // Tx buffer MASK 331 | static const uint16_t RMASK = 0x07FF; // Rx buffer MASK 332 | public: 333 | static const uint16_t SSIZE = 2048; // Max Tx buffer size 334 | private: 335 | static const uint16_t RSIZE = 2048; // Max Rx buffer size 336 | uint16_t SBASE[SOCKETS]; // Tx buffer base address 337 | uint16_t RBASE[SOCKETS]; // Rx buffer base address 338 | 339 | private: 340 | 341 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 342 | inline static void initSS() { DDRB |= _BV(4); }; 343 | inline static void setSS() { PORTB &= ~_BV(4); }; 344 | inline static void resetSS() { PORTB |= _BV(4); }; 345 | #elif defined(__AVR_ATmega32U4__) 346 | inline static void initSS() { DDRB |= _BV(6); }; 347 | inline static void setSS() { PORTB &= ~_BV(6); }; 348 | inline static void resetSS() { PORTB |= _BV(6); }; 349 | #elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__) 350 | inline static void initSS() { DDRB |= _BV(0); }; 351 | inline static void setSS() { PORTB &= ~_BV(0); }; 352 | inline static void resetSS() { PORTB |= _BV(0); }; 353 | #elif defined(REL_GR_KURUMI) || defined(REL_GR_KURUMI_PROTOTYPE) 354 | inline static void initSS() { pinMode(SS, OUTPUT); \ 355 | digitalWrite(SS, HIGH); }; 356 | inline static void setSS() { digitalWrite(SS, LOW); }; 357 | inline static void resetSS() { digitalWrite(SS, HIGH); }; 358 | #else 359 | inline static void initSS() { DDRB |= _BV(2); }; 360 | inline static void setSS() { PORTB &= ~_BV(2); }; 361 | inline static void resetSS() { PORTB |= _BV(2); }; 362 | #endif 363 | 364 | }; 365 | 366 | extern W5100Class W5100; 367 | 368 | uint8_t W5100Class::readSn(SOCKET _s, uint16_t _addr) { 369 | return read(CH_BASE + _s * CH_SIZE + _addr); 370 | } 371 | 372 | uint8_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) { 373 | return write(CH_BASE + _s * CH_SIZE + _addr, _data); 374 | } 375 | 376 | uint16_t W5100Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 377 | return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len); 378 | } 379 | 380 | uint16_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 381 | return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len); 382 | } 383 | 384 | void W5100Class::getGatewayIp(uint8_t *_addr) { 385 | readGAR(_addr); 386 | } 387 | 388 | void W5100Class::setGatewayIp(uint8_t *_addr) { 389 | writeGAR(_addr); 390 | } 391 | 392 | void W5100Class::getSubnetMask(uint8_t *_addr) { 393 | readSUBR(_addr); 394 | } 395 | 396 | void W5100Class::setSubnetMask(uint8_t *_addr) { 397 | writeSUBR(_addr); 398 | } 399 | 400 | void W5100Class::getMACAddress(uint8_t *_addr) { 401 | readSHAR(_addr); 402 | } 403 | 404 | void W5100Class::setMACAddress(uint8_t *_addr) { 405 | writeSHAR(_addr); 406 | } 407 | 408 | void W5100Class::getIPAddress(uint8_t *_addr) { 409 | readSIPR(_addr); 410 | } 411 | 412 | void W5100Class::setIPAddress(uint8_t *_addr) { 413 | writeSIPR(_addr); 414 | } 415 | 416 | void W5100Class::setRetransmissionTime(uint16_t _timeout) { 417 | writeRTR(_timeout); 418 | } 419 | 420 | void W5100Class::setRetransmissionCount(uint8_t _retry) { 421 | writeRCR(_retry); 422 | } 423 | #endif 424 | 425 | #endif 426 | -------------------------------------------------------------------------------- /src/utility/w5500.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #ifndef W5500_H_INCLUDED 11 | #define W5500_H_INCLUDED 12 | 13 | //#include 14 | //#include 15 | 16 | 17 | #define MAX_SOCK_NUM 8 18 | //typedef uint8_t SOCKET; 19 | 20 | 21 | /* 22 | class MR { 23 | public: 24 | static const uint8_t RST = 0x80; 25 | static const uint8_t PB = 0x10; 26 | static const uint8_t PPPOE = 0x08; 27 | static const uint8_t LB = 0x04; 28 | static const uint8_t AI = 0x02; 29 | static const uint8_t IND = 0x01; 30 | }; 31 | */ 32 | /* 33 | class IR { 34 | public: 35 | static const uint8_t CONFLICT = 0x80; 36 | static const uint8_t UNREACH = 0x40; 37 | static const uint8_t PPPoE = 0x20; 38 | static const uint8_t SOCK0 = 0x01; 39 | static const uint8_t SOCK1 = 0x02; 40 | static const uint8_t SOCK2 = 0x04; 41 | static const uint8_t SOCK3 = 0x08; 42 | static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); }; 43 | }; 44 | */ 45 | 46 | class SnMR { 47 | public: 48 | static const uint8_t CLOSE = 0x00; 49 | static const uint8_t TCP = 0x01; 50 | static const uint8_t UDP = 0x02; 51 | static const uint8_t IPRAW = 0x03; 52 | static const uint8_t MACRAW = 0x04; 53 | static const uint8_t PPPOE = 0x05; 54 | static const uint8_t ND = 0x20; 55 | static const uint8_t MULTI = 0x80; 56 | }; 57 | 58 | enum SockCMD { 59 | Sock_OPEN = 0x01, 60 | Sock_LISTEN = 0x02, 61 | Sock_CONNECT = 0x04, 62 | Sock_DISCON = 0x08, 63 | Sock_CLOSE = 0x10, 64 | Sock_SEND = 0x20, 65 | Sock_SEND_MAC = 0x21, 66 | Sock_SEND_KEEP = 0x22, 67 | Sock_RECV = 0x40 68 | }; 69 | 70 | /*class SnCmd { 71 | public: 72 | static const uint8_t OPEN = 0x01; 73 | static const uint8_t LISTEN = 0x02; 74 | static const uint8_t CONNECT = 0x04; 75 | static const uint8_t DISCON = 0x08; 76 | static const uint8_t CLOSE = 0x10; 77 | static const uint8_t SEND = 0x20; 78 | static const uint8_t SEND_MAC = 0x21; 79 | static const uint8_t SEND_KEEP = 0x22; 80 | static const uint8_t RECV = 0x40; 81 | }; 82 | */ 83 | 84 | class SnIR { 85 | public: 86 | static const uint8_t SEND_OK = 0x10; 87 | static const uint8_t TIMEOUT = 0x08; 88 | static const uint8_t RECV = 0x04; 89 | static const uint8_t DISCON = 0x02; 90 | static const uint8_t CON = 0x01; 91 | }; 92 | 93 | class SnSR { 94 | public: 95 | static const uint8_t CLOSED = 0x00; 96 | static const uint8_t INIT = 0x13; 97 | static const uint8_t LISTEN = 0x14; 98 | static const uint8_t SYNSENT = 0x15; 99 | static const uint8_t SYNRECV = 0x16; 100 | static const uint8_t ESTABLISHED = 0x17; 101 | static const uint8_t FIN_WAIT = 0x18; 102 | static const uint8_t CLOSING = 0x1A; 103 | static const uint8_t TIME_WAIT = 0x1B; 104 | static const uint8_t CLOSE_WAIT = 0x1C; 105 | static const uint8_t LAST_ACK = 0x1D; 106 | static const uint8_t UDP = 0x22; 107 | static const uint8_t IPRAW = 0x32; 108 | static const uint8_t MACRAW = 0x42; 109 | static const uint8_t PPPOE = 0x5F; 110 | }; 111 | 112 | class IPPROTO { 113 | public: 114 | static const uint8_t IP = 0; 115 | static const uint8_t ICMP = 1; 116 | static const uint8_t IGMP = 2; 117 | static const uint8_t GGP = 3; 118 | static const uint8_t TCP = 6; 119 | static const uint8_t PUP = 12; 120 | static const uint8_t UDP = 17; 121 | static const uint8_t IDP = 22; 122 | static const uint8_t ND = 77; 123 | static const uint8_t RAW = 255; 124 | }; 125 | 126 | class W5500Class { 127 | 128 | public: 129 | void init(); 130 | 131 | /** 132 | * @brief This function is being used for copy the data form Receive buffer of the chip to application buffer. 133 | * 134 | * It calculate the actual physical address where one has to read 135 | * the data from Receive buffer. Here also take care of the condition while it exceed 136 | * the Rx memory uper-bound of socket. 137 | */ 138 | void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len); 139 | 140 | /** 141 | * @brief This function is being called by send() and sendto() function also. 142 | * 143 | * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer 144 | * register. User should read upper byte first and lower byte later to get proper value. 145 | */ 146 | void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len); 147 | /** 148 | * @brief A copy of send_data_processing that uses the provided ptr for the 149 | * write offset. Only needed for the "streaming" UDP API, where 150 | * a single UDP packet is built up over a number of calls to 151 | * send_data_processing_ptr, because TX_WR doesn't seem to get updated 152 | * correctly in those scenarios 153 | * @param ptr value to use in place of TX_WR. If 0, then the value is read 154 | * in from TX_WR 155 | * @return New value for ptr, to be used in the next call 156 | */ 157 | // FIXME Update documentation 158 | void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len); 159 | 160 | /** 161 | * @brief This function is being called by recv() also. 162 | * 163 | * This function read the Rx read pointer register 164 | * and after copy the data from receive buffer update the Rx write pointer register. 165 | * User should read upper byte first and lower byte later to get proper value. 166 | */ 167 | void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0); 168 | 169 | inline void setGatewayIp(uint8_t *_addr); 170 | inline void getGatewayIp(uint8_t *_addr); 171 | 172 | inline void setSubnetMask(uint8_t *_addr); 173 | inline void getSubnetMask(uint8_t *_addr); 174 | 175 | inline void setMACAddress(uint8_t *_addr); 176 | inline void getMACAddress(uint8_t *_addr); 177 | 178 | inline void setIPAddress(uint8_t *_addr); 179 | inline void getIPAddress(uint8_t *_addr); 180 | 181 | inline void setRetransmissionTime(uint16_t timeout); 182 | inline void setRetransmissionCount(uint8_t _retry); 183 | 184 | inline void setPHYCFGR(uint8_t _val); 185 | inline uint8_t getPHYCFGR(); 186 | 187 | void execCmdSn(SOCKET s, SockCMD _cmd); 188 | 189 | uint16_t getTXFreeSize(SOCKET s); 190 | uint16_t getRXReceivedSize(SOCKET s); 191 | 192 | 193 | // W5500 Registers 194 | // --------------- 195 | private: 196 | static uint8_t write(uint16_t _addr, uint8_t _cb, uint8_t _data); 197 | static uint16_t write(uint16_t _addr, uint8_t _cb, const uint8_t *buf, uint16_t len); 198 | static uint8_t read(uint16_t _addr, uint8_t _cb ); 199 | static uint16_t read(uint16_t _addr, uint8_t _cb, uint8_t *buf, uint16_t len); 200 | 201 | #define __GP_REGISTER8(name, address) \ 202 | static inline void write##name(uint8_t _data) { \ 203 | write(address, 0x04, _data); \ 204 | } \ 205 | static inline uint8_t read##name() { \ 206 | return read(address, 0x00); \ 207 | } 208 | #define __GP_REGISTER16(name, address) \ 209 | static void write##name(uint16_t _data) { \ 210 | write(address, 0x04, _data >> 8); \ 211 | write(address+1, 0x04, _data & 0xFF); \ 212 | } \ 213 | static uint16_t read##name() { \ 214 | uint16_t res = read(address, 0x00); \ 215 | res = (res << 8) + read(address + 1, 0x00); \ 216 | return res; \ 217 | } 218 | #define __GP_REGISTER_N(name, address, size) \ 219 | static uint16_t write##name(uint8_t *_buff) { \ 220 | return write(address, 0x04, _buff, size); \ 221 | } \ 222 | static uint16_t read##name(uint8_t *_buff) { \ 223 | return read(address, 0x00, _buff, size); \ 224 | } 225 | 226 | public: 227 | __GP_REGISTER8 (MR, 0x0000); // Mode 228 | __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address 229 | __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address 230 | __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address 231 | __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address 232 | __GP_REGISTER8 (IR, 0x0015); // Interrupt 233 | __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask 234 | __GP_REGISTER16(RTR, 0x0019); // Timeout address 235 | __GP_REGISTER8 (RCR, 0x001B); // Retry count 236 | __GP_REGISTER_N(UIPR, 0x0028, 4); // Unreachable IP address in UDP mode 237 | __GP_REGISTER16(UPORT, 0x002C); // Unreachable Port address in UDP mode 238 | __GP_REGISTER8 (PHYCFGR, 0x002E); // PHY Configuration register, default value: 0b 1011 1xxx 239 | 240 | 241 | #undef __GP_REGISTER8 242 | #undef __GP_REGISTER16 243 | #undef __GP_REGISTER_N 244 | 245 | // W5500 Socket registers 246 | // ---------------------- 247 | private: 248 | static inline uint8_t readSn(SOCKET _s, uint16_t _addr); 249 | static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data); 250 | static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 251 | static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 252 | 253 | //static const uint16_t CH_BASE = 0x0000; 254 | //static const uint16_t CH_SIZE = 0x0000; 255 | 256 | #define __SOCKET_REGISTER8(name, address) \ 257 | static inline void write##name(SOCKET _s, uint8_t _data) { \ 258 | writeSn(_s, address, _data); \ 259 | } \ 260 | static inline uint8_t read##name(SOCKET _s) { \ 261 | return readSn(_s, address); \ 262 | } 263 | #if defined(REL_GR_KURUMI) || defined(REL_GR_KURUMI_PROTOTYPE) 264 | #define __SOCKET_REGISTER16(name, address) \ 265 | static void write##name(SOCKET _s, uint16_t _data) { \ 266 | writeSn(_s, address, _data >> 8); \ 267 | writeSn(_s, address+1, _data & 0xFF); \ 268 | } \ 269 | static uint16_t read##name(SOCKET _s) { \ 270 | uint16_t res = readSn(_s, address); \ 271 | uint16_t res2 = readSn(_s,address + 1); \ 272 | res = res << 8; \ 273 | res2 = res2 & 0xFF; \ 274 | res = res | res2; \ 275 | return res; \ 276 | } 277 | #else 278 | #define __SOCKET_REGISTER16(name, address) \ 279 | static void write##name(SOCKET _s, uint16_t _data) { \ 280 | writeSn(_s, address, _data >> 8); \ 281 | writeSn(_s, address+1, _data & 0xFF); \ 282 | } \ 283 | static uint16_t read##name(SOCKET _s) { \ 284 | uint16_t res = readSn(_s, address); \ 285 | res = (res << 8) + readSn(_s, address + 1); \ 286 | return res; \ 287 | } 288 | #endif 289 | #define __SOCKET_REGISTER_N(name, address, size) \ 290 | static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ 291 | return writeSn(_s, address, _buff, size); \ 292 | } \ 293 | static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ 294 | return readSn(_s, address, _buff, size); \ 295 | } 296 | 297 | public: 298 | __SOCKET_REGISTER8(SnMR, 0x0000) // Mode 299 | __SOCKET_REGISTER8(SnCR, 0x0001) // Command 300 | __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt 301 | __SOCKET_REGISTER8(SnSR, 0x0003) // Status 302 | __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port 303 | __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr 304 | __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr 305 | __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port 306 | __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size 307 | __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode 308 | __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS 309 | __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL 310 | __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size 311 | __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer 312 | __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer 313 | __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size 314 | __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer 315 | __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) 316 | 317 | #undef __SOCKET_REGISTER8 318 | #undef __SOCKET_REGISTER16 319 | #undef __SOCKET_REGISTER_N 320 | 321 | 322 | private: 323 | static const uint8_t RST = 7; // Reset BIT 324 | static const int SOCKETS = 8; 325 | 326 | public: 327 | static const uint16_t SSIZE = 2048; // Max Tx buffer size 328 | private: 329 | static const uint16_t RSIZE = 2048; // Max Rx buffer size 330 | 331 | private: 332 | #if defined(REL_GR_KURUMI) || defined(REL_GR_KURUMI_PROTOTYPE) 333 | inline static void initSS() { pinMode(SS, OUTPUT); \ 334 | digitalWrite(SS, HIGH); }; 335 | inline static void setSS() { digitalWrite(SS, LOW); }; 336 | inline static void resetSS() { digitalWrite(SS, HIGH); }; 337 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 338 | inline static void initSS() { DDRB |= _BV(4); }; 339 | inline static void setSS() { PORTB &= ~_BV(4); }; 340 | inline static void resetSS() { PORTB |= _BV(4); }; 341 | #elif defined(__AVR_ATmega32U4__) 342 | inline static void initSS() { DDRB |= _BV(6); }; 343 | inline static void setSS() { PORTB &= ~_BV(6); }; 344 | inline static void resetSS() { PORTB |= _BV(6); }; 345 | #elif defined(__AVR_AT90USB1286__) 346 | inline static void initSS() { DDRF |= _BV(7); }; 347 | inline static void setSS() { PORTF &= ~_BV(7); }; 348 | inline static void resetSS() { PORTF |= _BV(7); }; 349 | #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__) 350 | inline static void initSS() { DDRB |= _BV(0); }; 351 | inline static void setSS() { PORTB &= ~_BV(0); }; 352 | inline static void resetSS() { PORTB |= _BV(0); }; 353 | #else 354 | inline static void initSS() { DDRB |= _BV(2); }; 355 | inline static void setSS() { PORTB &= ~_BV(2); }; 356 | inline static void resetSS() { PORTB |= _BV(2); }; 357 | #endif 358 | 359 | }; 360 | 361 | extern W5500Class W5100; 362 | 363 | uint8_t W5500Class::readSn(SOCKET _s, uint16_t _addr) { 364 | uint8_t cntl_byte = (_s<<5)+0x08; 365 | return read(_addr, cntl_byte); 366 | } 367 | 368 | uint8_t W5500Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) { 369 | uint8_t cntl_byte = (_s<<5)+0x0C; 370 | return write(_addr, cntl_byte, _data); 371 | } 372 | 373 | uint16_t W5500Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 374 | uint8_t cntl_byte = (_s<<5)+0x08; 375 | return read(_addr, cntl_byte, _buf, _len ); 376 | } 377 | 378 | uint16_t W5500Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 379 | uint8_t cntl_byte = (_s<<5)+0x0C; 380 | return write(_addr, cntl_byte, _buf, _len); 381 | } 382 | 383 | void W5500Class::getGatewayIp(uint8_t *_addr) { 384 | readGAR(_addr); 385 | } 386 | 387 | void W5500Class::setGatewayIp(uint8_t *_addr) { 388 | writeGAR(_addr); 389 | } 390 | 391 | void W5500Class::getSubnetMask(uint8_t *_addr) { 392 | readSUBR(_addr); 393 | } 394 | 395 | void W5500Class::setSubnetMask(uint8_t *_addr) { 396 | writeSUBR(_addr); 397 | } 398 | 399 | void W5500Class::getMACAddress(uint8_t *_addr) { 400 | readSHAR(_addr); 401 | } 402 | 403 | void W5500Class::setMACAddress(uint8_t *_addr) { 404 | writeSHAR(_addr); 405 | } 406 | 407 | void W5500Class::getIPAddress(uint8_t *_addr) { 408 | readSIPR(_addr); 409 | } 410 | 411 | void W5500Class::setIPAddress(uint8_t *_addr) { 412 | writeSIPR(_addr); 413 | } 414 | 415 | void W5500Class::setRetransmissionTime(uint16_t _timeout) { 416 | writeRTR(_timeout); 417 | } 418 | 419 | void W5500Class::setRetransmissionCount(uint8_t _retry) { 420 | writeRCR(_retry); 421 | } 422 | 423 | void W5500Class::setPHYCFGR(uint8_t _val) { 424 | writePHYCFGR(_val); 425 | } 426 | 427 | uint8_t W5500Class::getPHYCFGR() { 428 | // readPHYCFGR(); 429 | return read(0x002E, 0x00); 430 | } 431 | 432 | #endif 433 | --------------------------------------------------------------------------------