Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.
Dependents: mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more
Library for ENC28J60 Ethernet modules.
Ported to mbed from Norbert Truchsess's UIPEthernet library for Arduino. Thank you Norbert!
- Full support for persistent (streaming) TCP/IP and UDP connections Client and Server each, ARP, ICMP, DHCP and DNS.
- Works with both Mbed OS 2 and Mbed OS 5.
Usage:
- Import the library into your project.
- Add
#include "UipEthernet.h"
tomain.cpp
- Create one instance of the UipEthernet class initialized with the MAC address you'd like to use and SPI pins of the connected Mbed board.
Example programs:
Import programWebSwitch_ENC28J60
HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.
Import programHTTPServer_Echo_ENC28J60
A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.
Import programTcpServer_ENC28J60
Simple TCP/IP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programTcpClient_ENC28J60
Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programUdpServer_ENC28J60
Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programUdpClient_ENC28J60
Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
Import programMQTT_Hello_ENC28J60
MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Revision 10:e4ddab81e6a8, committed 2019-08-27
- Comitter:
- hudakz
- Date:
- Tue Aug 27 22:08:54 2019 +0000
- Parent:
- 9:a156d3de5647
- Child:
- 11:647d53d146f1
- Commit message:
- Added methods for TcpClient to get remote client's IP address and peer name.
Changed in this revision
--- a/IpAddress.cpp Tue Aug 27 15:01:10 2019 +0000 +++ b/IpAddress.cpp Tue Aug 27 22:08:54 2019 +0000 @@ -118,4 +118,3 @@ buf[j] = '\0'; return buf; } -
--- a/Readme.txt Tue Aug 27 15:01:10 2019 +0000 +++ b/Readme.txt Tue Aug 27 22:08:54 2019 +0000 @@ -1,9 +1,9 @@ -This is UIPEthernet version 1.09 +This is UIPEthernet for Mbed OS 2 and Mbed OS 5 -An plugin-replacement of the stock Arduino Ethernet library for ENC28J60 shields and breakout boards. Full support for persistent (streaming) TCP-connections and UDP (Client and Server each), ARP, ICMP, DHCP and DNS. -Just include 'UIPEthernet.h' instead of 'Ethernet.h' and use all your code written for the stock Arduino Ethernet lib! +Full support for persistent (streaming) TCP-connections and UDP (Client and Server each), ARP, ICMP, DHCP and DNS. -UIPEthernet is written as a wrapper around the mature uIP Stack by Adam Dunkels, which provides the low-level implementation for all supported protocols. To overcome the memory-constrains (a 'regular' uIP-application does all processing in RAM) the ENC28J60 internal memory is used for all stream buffers (in and out). Only 400-600 Bytes of Arduinos RAM are used (depending on the number of concurrently open connections). As of Flash-memory a ATmega368-based Arduino is the minimum requirenment. +UIPEthernet is written as a wrapper around the mature uIP Stack by Adam Dunkels, which provides the low-level implementation for all supported protocols. +To overcome the memory-constrains (a 'regular' uIP-application does all processing in RAM) the ENC28J60 internal memory is used for all stream buffers (in and out). This library is written by Norbert Truchsess <norbert.truchsess@t-online.de> @@ -11,26 +11,7 @@ This library was inspired by the SerialIP implementation by Adam Nielsen <malvineous@shikadi.net>, actually I took this code as a starting point, but in the latest versions there are very few lines left. -This library was ported to mbed by Zoltan Hudak <hudakz@inbox.com> - -Installation ------------- - -To install the libraries, you need to place them into your "libraries" folder. You can find it within your Arduino IDE distribution within the "hardware" folder. - - C:\> cd [path to Arduino distribution]\libraries - C:\> git clone https://github.com/ntruchsess/arduino_uip UIPEthernet - -Be sure to restart the IDE if it was running. - -On a Mac, you will want to create a folder named "libraries" in in the "Documents" -> "Arduino" folder within your home directory. Clone the project there (and restart the IDE, if it was running during this process). - - $ cd ~/Documents/Arduino/libraries - $ git clone https://github.com/ntruchsess/arduino_uip UIPEthernet - -Or you download the zipped version of the library from https://github.com/ntruchsess/arduino_uip/releases, and copy the contained directory UIPEthernet to [path to Arduino distribution]\libraries\UIPEthernet. - -If you are running Arduino-IDE 1.5.x use release-version 1.59 or checkout branch 'Arduino_1.5.x' +This library was ported to mbed by Zoltan Hudak <hudakz@outlook.com> Additional information can be found on the Arduino website: http://www.arduino.cc/en/Hacking/Libraries @@ -44,7 +25,6 @@ - Arduino Ethernet library description http://arduino.cc/en/Reference/Ethernet - (Arduino_uip uses the same API as that, just include "UIPEthernet.h", "UIPClient.h", "UIPServer.h" and "UIPUDP.h" instead of the stock "Ethernet.h", "EthernetClient.h", "EthernetServer.h " and "EthernetUDP.h") - uIP API reference: http://www.sics.se/~adam/uip/uip-1.0-refman/ @@ -58,25 +38,21 @@ Licenses ------------- -UIPEthernet.h -UIPEthernet.cpp -UIPUdp.h -UIPClient.cpp +UipEthernet.h +UipEthernet.cpp +UdpSocket.h +UdpSocket.cpp +TcpClient.h +TcpClient.cpp +TcpServer.h +TcpServer.cpp +utility/MemPool.h +utility/MemPool.cpp Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> All rights reserved. -Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> - -UIPServer.h -UIPServer.cpp -UIPClient.h -UIPUdp.cpp -utility/mempool.h -utility/mempool.cpp - -Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> -All rights reserved. +Modified (ported to mbed) by Zoltan Hudak <hudakz@outlook.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -95,21 +71,21 @@ utility/enc28j60.h -Author : Pascal Stang (c)2005 +Author: Pascal Stang (c)2005 Modified by Norbert Truchsess Copyright: GPL V2 -------------- -utility/Enc28J60Network.h -utility/Enc28J60Network.cpp +utility/Enc28j60Py.h +utility/Enc28j60Py.cpp Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> All rights reserved. inspired and based on enc28j60.c file from the AVRlib library by Pascal Stang. -Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> +Modified (ported to mbed) by Zoltan Hudak <hudakz@outlook.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -199,17 +175,19 @@ -------------- -Dhcp.h -Dhcp.cpp +DhcpClient.h +DhcpClient.cpp DHCP Library v0.3 - April 25, 2009 Author: Jordan Terrell - blog.jordanterrell.com - as included in Arduinos stock Ethernet-library, no special licence mentioned here +Modified (ported to mbed) by Zoltan Hudak <hudakz@outlook.com> + -------------- -Dns.h -Dns.cpp +DnsClient.h +DnsClient.cpp (c) Copyright 2009-2010 MCQN Ltd. Released under Apache License, version 2.0 @@ -238,13 +216,11 @@ -------------- -Client.h -IPAddress.h -IPAddress.cpp -Server.h +IpAddress.h +IpAddress.cpp Copyright (c) 2011 Adrian McEwen. All right reserved. -Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> +Modified (ported to mbed) by Zoltan Hudak <hudakz@outlook.com> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -259,24 +235,3 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - --------------- - -utility/millis.h -utility/millis.cpp - -Copyright (c) 2015 Zoltan Hudak <hudakz@inbox.com>. -All right reserved. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see <http://www.gnu.org/licenses/>.
--- a/TcpClient.cpp Tue Aug 27 15:01:10 2019 +0000 +++ b/TcpClient.cpp Tue Aug 27 22:08:54 2019 +0000 @@ -31,7 +31,7 @@ #define UIP_TCP_PHYH_LEN UIP_LLH_LEN + UIP_IPTCPH_LEN -uip_userdata_t TcpClient::all_data[UIP_CONNS]; +uip_userdata_t TcpClient:: all_data[UIP_CONNS]; /** * @brief @@ -194,7 +194,7 @@ * @param * @retval */ -size_t TcpClient::write(uint8_t c) +size_t TcpClient::send(uint8_t c) { return _write(data, &c, 1); } @@ -205,7 +205,7 @@ * @param * @retval */ -size_t TcpClient::write(const uint8_t* buf, size_t size) +size_t TcpClient::send(const uint8_t* buf, size_t size) { return _write(data, buf, size); } @@ -293,7 +293,7 @@ * @param * @retval */ -int TcpClient::available() +size_t TcpClient::available() { if (*this) return _available(data); @@ -306,9 +306,9 @@ * @param * @retval */ -int TcpClient::_available(uip_userdata_t* u) +size_t TcpClient::_available(uip_userdata_t* u) { - int len = 0; + size_t len = 0; for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { len += UipEthernet::ethernet->phy.blockSize(u->packets_in[i]); } @@ -322,7 +322,7 @@ * @param * @retval */ -int TcpClient::read(uint8_t* buf, size_t size) +int TcpClient::recv(uint8_t* buf, size_t size) { if (*this) { uint16_t remain = size; @@ -366,10 +366,10 @@ * @param * @retval */ -int TcpClient::read() +size_t TcpClient::recv() { static uint8_t c; - if (read(&c, 1) < 0) + if (recv(&c, 1) < 0) return -1; return c; } @@ -412,6 +412,31 @@ * @param * @retval */ +const char* TcpClient::getpeername() +{ + static char buf[16]; + IpAddress remoteIp = ip_addr_uip(data->ripaddr); + + return remoteIp.toString(buf); +} + +/** + * @brief + * @note + * @param + * @retval + */ +IpAddress TcpClient::getRemoteIp() +{ + return ip_addr_uip(data->ripaddr); +} + +/** + * @brief + * @note + * @param + * @retval + */ void TcpClient::close() { delete this;
--- a/TcpClient.h Tue Aug 27 15:01:10 2019 +0000 +++ b/TcpClient.h Tue Aug 27 22:08:54 2019 +0000 @@ -20,7 +20,7 @@ #define UIPCLIENT_H #include "mbed.h" -#include "Client.h" +#include "IpAddress.h" #include "utility/MemPool.h" extern "C" @@ -44,53 +44,56 @@ typedef struct { - uint8_t state; - memhandle packets_in[UIP_SOCKET_NUMPACKETS]; - uint16_t lport; /**< The local TCP port, in network byte order. */ + uint8_t state; + memhandle packets_in[UIP_SOCKET_NUMPACKETS]; + uint16_t lport; /**< The local TCP port, in network byte order. */ } uip_userdata_closed_t; typedef struct { - Timer pollTimer; - uint8_t state; - memhandle packets_in[UIP_SOCKET_NUMPACKETS]; - memhandle packets_out[UIP_SOCKET_NUMPACKETS]; - memaddress out_pos; + Timer pollTimer; + uint8_t state; + uip_ipaddr_t ripaddr; /**< The IP address of the remote host. */ + memhandle packets_in[UIP_SOCKET_NUMPACKETS]; + memhandle packets_out[UIP_SOCKET_NUMPACKETS]; + memaddress out_pos; } uip_userdata_t; -class TcpClient : public Client +class TcpClient { public: TcpClient(); - //TcpClient(UIPEthernet* ethernet); - //TcpClient(struct uip_conn* _conn); - //TcpClient(struct uip_conn* _conn, UIPEthernet* ethernet); TcpClient(uip_userdata_t* conn_data); - //TcpClient(uip_userdata_t* conn_data, UIPEthernet* ethernet); virtual ~TcpClient() {} + int connect(IpAddress ip, uint16_t port); int connect(const char* host, uint16_t port); - int read(uint8_t* buf, size_t size); + int recv(uint8_t* buf, size_t size); void stop(); uint8_t connected(); operator bool(); virtual bool operator ==(const TcpClient& ); virtual bool operator !=(const TcpClient& rhs) { return !this->operator ==(rhs); } - size_t write(uint8_t); - size_t write(const uint8_t* buf, size_t size); - int available(); - int read(); + size_t send(uint8_t); + size_t send(const uint8_t* buf, size_t size); + size_t available(); + size_t recv(); int peek(); void flush(); + const char* getpeername(); + IpAddress getRemoteIp(); void close(); static uip_userdata_t all_data[UIP_CONNS]; static size_t _write(uip_userdata_t* , const uint8_t* buf, size_t size); +protected: + uint8_t* rawIPAddress(IpAddress& addr) { return addr.rawAddress(); } + private: uip_userdata_t* data; static uip_userdata_t* _allocateData(); - static int _available(uip_userdata_t* ); + static size_t _available(uip_userdata_t* ); static uint8_t _currentBlock(memhandle* blocks); static void _eatBlock(memhandle* blocks); static void _flushBlocks(memhandle* blocks);
--- a/TcpServer.cpp Tue Aug 27 15:01:10 2019 +0000 +++ b/TcpServer.cpp Tue Aug 27 22:08:54 2019 +0000 @@ -49,11 +49,13 @@ ((data->state & UIP_CLIENT_REMOTECLOSED) && ((uip_userdata_closed_t*)data)->lport == _port) ) ) { + data->ripaddr[0] = uip_conns[data->state & UIP_CLIENT_SOCKETS].ripaddr[0]; + data->ripaddr[1] = uip_conns[data->state & UIP_CLIENT_SOCKETS].ripaddr[1]; return(new TcpClient(data)); } } - return(new TcpClient()); + return NULL; } /** @@ -63,9 +65,7 @@ * @retval */ void TcpServer::open(UipEthernet* ethernet) -{ - //UIPEthernet::ethernet = ethernet; -} +{ } /** * @brief
--- a/TcpServer.h Tue Aug 27 15:01:10 2019 +0000 +++ b/TcpServer.h Tue Aug 27 22:08:54 2019 +0000 @@ -19,7 +19,6 @@ #ifndef UIPSERVER_H #define UIPSERVER_H -#include "Server.h" #include "TcpClient.h" class UipEthernet;
--- a/utility/Client.h Tue Aug 27 15:01:10 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* - Client.h - Base class that provides Client - Copyright (c) 2011 Adrian McEwen. All right reserved. - - Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#ifndef client_h -#define client_h -#include "IpAddress.h" -#include <stddef.h> - -class Client -{ -public: - virtual int connect(IpAddress ip, uint16_t port) = 0; - virtual int connect(const char* host, uint16_t port) = 0; - virtual size_t write(uint8_t) = 0; - virtual size_t write(const uint8_t* buf, size_t size) = 0; - virtual int available(void) = 0; - virtual int read(void) = 0; - virtual int read(uint8_t* buf, size_t size) = 0; - virtual int peek(void) = 0; - virtual void flush(void) = 0; - virtual void stop(void) = 0; - virtual uint8_t connected(void) = 0; - virtual operator bool(void) = 0; -protected: - uint8_t* rawIPAddress(IpAddress& addr) { return addr.rawAddress(); }; -}; -#endif
--- a/utility/Server.h Tue Aug 27 15:01:10 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -/* - Server.h - Base class that provides Server - Copyright (c) 2011 Adrian McEwen. All right reserved. - - Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#ifndef SERVER_H -#define SERVER_H - -class Server -{ -public: - virtual void open(void) = 0; -}; -#endif