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.
Diff: TcpClient.cpp
- Revision:
- 11:647d53d146f1
- Parent:
- 10:e4ddab81e6a8
- Child:
- 12:1dd995402b99
--- a/TcpClient.cpp Tue Aug 27 22:08:54 2019 +0000 +++ b/TcpClient.cpp Fri Aug 30 08:11:40 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::send(uint8_t c) +int TcpClient::send(uint8_t c) { return _write(data, &c, 1); } @@ -205,7 +205,7 @@ * @param * @retval */ -size_t TcpClient::send(const uint8_t* buf, size_t size) +int TcpClient::send(const uint8_t* buf, size_t size) { return _write(data, buf, size); } @@ -216,9 +216,9 @@ * @param * @retval */ -size_t TcpClient::_write(uip_userdata_t* data, const uint8_t* buf, size_t size) +int TcpClient::_write(uip_userdata_t* data, const uint8_t* buf, size_t size) { - int remain = size; + size_t remain = size; uint16_t written; #if UIP_ATTEMPTS_ON_WRITE > 0 uint16_t attempts = UIP_ATTEMPTS_ON_WRITE; @@ -229,7 +229,7 @@ if (data->packets_out[p] == NOBLOCK) { newpacket: - data->packets_out[p] = UipEthernet::ethernet->phy.allocBlock(UIP_SOCKET_DATALEN); + data->packets_out[p] = UipEthernet::ethernet->enc28j60Eth.allocBlock(UIP_SOCKET_DATALEN); if (data->packets_out[p] == NOBLOCK) { #if UIP_ATTEMPTS_ON_WRITE > 0 @@ -254,7 +254,7 @@ remain ); #endif - written = UipEthernet::ethernet->phy.writePacket + written = UipEthernet::ethernet->enc28j60Eth.writePacket ( data->packets_out[p], data->out_pos, @@ -310,7 +310,7 @@ { size_t len = 0; for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { - len += UipEthernet::ethernet->phy.blockSize(u->packets_in[i]); + len += UipEthernet::ethernet->enc28j60Eth.blockSize(u->packets_in[i]); } return len; @@ -331,8 +331,8 @@ uint16_t read; do { - read = UipEthernet::ethernet->phy.readPacket(data->packets_in[0], 0, buf + size - remain, remain); - if (read == UipEthernet::ethernet->phy.blockSize(data->packets_in[0])) { + read = UipEthernet::ethernet->enc28j60Eth.readPacket(data->packets_in[0], 0, buf + size - remain, remain); + if (read == UipEthernet::ethernet->enc28j60Eth.blockSize(data->packets_in[0])) { remain -= read; _eatBlock(&data->packets_in[0]); if @@ -350,7 +350,7 @@ } } else { - UipEthernet::ethernet->phy.resizeBlock(data->packets_in[0], read); + UipEthernet::ethernet->enc28j60Eth.resizeBlock(data->packets_in[0], read); break; } } while (remain > 0); @@ -366,7 +366,7 @@ * @param * @retval */ -size_t TcpClient::recv() +int TcpClient::recv() { static uint8_t c; if (recv(&c, 1) < 0) @@ -385,7 +385,7 @@ static uint8_t c; if (*this) { if (data->packets_in[0] != NOBLOCK) { - UipEthernet::ethernet->phy.readPacket(data->packets_in[0], 0, &c, 1); + UipEthernet::ethernet->enc28j60Eth.readPacket(data->packets_in[0], 0, &c, 1); return c; } } @@ -412,12 +412,9 @@ * @param * @retval */ -const char* TcpClient::getpeername() +IpAddress TcpClient::getRemoteIp() { - static char buf[16]; - IpAddress remoteIp = ip_addr_uip(data->ripaddr); - - return remoteIp.toString(buf); + return ip_addr_uip(data->ripaddr); } /** @@ -426,9 +423,11 @@ * @param * @retval */ -IpAddress TcpClient::getRemoteIp() +const char* TcpClient::getpeername() { - return ip_addr_uip(data->ripaddr); + static char buf[16]; + + return getRemoteIp().toString(buf); } /** @@ -481,9 +480,9 @@ if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) { for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { if (u->packets_in[i] == NOBLOCK) { - u->packets_in[i] = UipEthernet::ethernet->phy.allocBlock(uip_len); + u->packets_in[i] = UipEthernet::ethernet->enc28j60Eth.allocBlock(uip_len); if (u->packets_in[i] != NOBLOCK) { - UipEthernet::ethernet->phy.copyPacket + UipEthernet::ethernet->enc28j60Eth.copyPacket ( u->packets_in[i], 0, @@ -552,16 +551,16 @@ if (u->packets_out[1] == NOBLOCK) { send_len = u->out_pos; if (send_len > 0) { - UipEthernet::ethernet->phy.resizeBlock(u->packets_out[0], 0, send_len); + UipEthernet::ethernet->enc28j60Eth.resizeBlock(u->packets_out[0], 0, send_len); } } else - send_len = UipEthernet::ethernet->phy.blockSize(u->packets_out[0]); + send_len = UipEthernet::ethernet->enc28j60Eth.blockSize(u->packets_out[0]); if (send_len > 0) { UipEthernet::uipHeaderLen = ((uint8_t*)uip_appdata) - uip_buf; - UipEthernet::uipPacket = UipEthernet::ethernet->phy.allocBlock(UipEthernet::uipHeaderLen + send_len); + UipEthernet::uipPacket = UipEthernet::ethernet->enc28j60Eth.allocBlock(UipEthernet::uipHeaderLen + send_len); if (UipEthernet::uipPacket != NOBLOCK) { - UipEthernet::ethernet->phy.copyPacket + UipEthernet::ethernet->enc28j60Eth.copyPacket ( UipEthernet::uipPacket, UipEthernet::uipHeaderLen, @@ -620,6 +619,8 @@ if (!data->state) { data->pollTimer.reset(); data->state = sock | UIP_CLIENT_CONNECTED; + data->ripaddr[0] = 0; + data->ripaddr[1] = 0; memset(data->packets_in, 0, sizeof(data->packets_in) / sizeof(data->packets_in[0])); memset(&data->packets_out, 0, sizeof(data->packets_out) / sizeof(data->packets_out[0])); data->out_pos = 0; @@ -663,7 +664,7 @@ printf("-> "); #endif - UipEthernet::ethernet->phy.freeBlock(block[0]); + UipEthernet::ethernet->enc28j60Eth.freeBlock(block[0]); for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS - 1; i++) { block[i] = block[i + 1]; } @@ -687,7 +688,7 @@ void TcpClient::_flushBlocks(memhandle* block) { for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { - UipEthernet::ethernet->phy.freeBlock(block[i]); + UipEthernet::ethernet->enc28j60Eth.freeBlock(block[i]); block[i] = NOBLOCK; } }