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.

/media/uploads/hudakz/enc28j60_module01.jpg

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" to main.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.

DhcpClient.h

Committer:
hudakz
Date:
2020-07-23
Revision:
18:8d5738a6646e
Parent:
11:647d53d146f1

File content as of revision 18:8d5738a6646e:

// DHCP Library v0.3 - April 25, 2009
// Author: Jordan Terrell - blog.jordanterrell.com
#ifndef DHCPCLIENT_h
#define DHCPCLIENT_h

#include "UdpSocket.h"
#include "IpAddress.h"

/* DHCP state machine. */

#define STATE_DHCP_START        0
#define STATE_DHCP_DISCOVER     1
#define STATE_DHCP_REQUEST      2
#define STATE_DHCP_LEASED       3
#define STATE_DHCP_REREQUEST    4
#define STATE_DHCP_RELEASE      5

#define DHCP_FLAGSBROADCAST     0x8000

/* UDP port numbers for DHCP */

#define DHCP_SERVER_PORT        67          /* from server to client */

#define DHCP_CLIENT_PORT        68          /* from client to server */

/* DHCP message OP code */

#define DHCP_BOOTREQUEST        1
#define DHCP_BOOTREPLY          2

/* DHCP message type */

#define DHCP_DISCOVER           1
#define DHCP_OFFER              2
#define DHCP_REQUEST            3
#define DHCP_DECLINE            4
#define DHCP_ACK                5
#define DHCP_NAK                6
#define DHCP_RELEASE            7
#define DHCP_INFORM             8

#define DHCP_HTYPE10MB          1
#define DHCP_HTYPE100MB         2

#define DHCP_HLENETHERNET       6
#define DHCP_HOPS               0
#define DHCP_SECS               0

#define MAGIC_COOKIE            0x63825363
#define MAX_DHCP_OPT            16

#define HOST_NAME               "ENC28J"
#define DEFAULT_LEASE           (900)   //default lease time in seconds

#define DHCP_CHECK_NONE         (0)
#define DHCP_CHECK_RENEW_FAIL   (1)
#define DHCP_CHECK_RENEW_OK     (2)
#define DHCP_CHECK_REBIND_FAIL  (3)
#define DHCP_CHECK_REBIND_OK    (4)

enum
{
    padOption               = 0,
    subnetMask              = 1,
    timerOffset             = 2,
    routersOnSubnet         = 3,
    timeServer              = 4,
    nameServer              = 5,
    dns                     = 6,
    logServer               = 7,
    cookieServer            = 8,
    lprServer               = 9,
    impressServer           = 10,
    resourceLocationServer  = 11,
    hostName                = 12,
    bootFileSize            = 13,
    meritDumpFile           = 14,
    domainName              = 15,
    swapServer              = 16,
    rootPath                = 17,
    extentionsPath          = 18,
    IPforwarding            = 19,
    nonLocalSourceRouting   = 20,
    policyFilter            = 21,
    maxDgramReasmSize       = 22,
    defaultIPTTL            = 23,
    pathMTUagingTimeout     = 24,
    pathMTUplateauTable     = 25,
    ifMTU                   = 26,
    allSubnetsLocal         = 27,
    broadcastAddr           = 28,
    performMaskDiscovery    = 29,
    maskSupplier            = 30,
    performRouterDiscovery  = 31,
    routerSolicitationAddr  = 32,
    staticRoute             = 33,
    trailerEncapsulation    = 34,
    arpCacheTimeout         = 35,
    ethernetEncapsulation   = 36,
    tcpDefaultTTL           = 37,
    tcpKeepaliveInterval    = 38,
    tcpKeepaliveGarbage     = 39,
    nisDomainName           = 40,
    nisServers              = 41,
    ntpServers              = 42,
    vendorSpecificInfo      = 43,
    netBIOSnameServer       = 44,
    netBIOSdgramDistServer  = 45,
    netBIOSnodeType         = 46,
    netBIOSscope            = 47,
    xFontServer             = 48,
    xDisplayManager         = 49,
    dhcpRequestedIPaddr     = 50,
    dhcpIPaddrLeaseTime     = 51,
    dhcpOptionOverload      = 52,
    dhcpMessageType         = 53,
    dhcpServerIdentifier    = 54,
    dhcpParamRequest        = 55,
    dhcpMsg                 = 56,
    dhcpMaxMsgSize          = 57,
    dhcpT1value             = 58,
    dhcpT2value             = 59,
    dhcpClassIdentifier     = 60,
    dhcpClientIdentifier    = 61,
    endOption               = 255
};

typedef struct _RIP_MSG_FIXED
{
    uint8_t     op;
    uint8_t     htype;
    uint8_t     hlen;
    uint8_t     hops;
    uint32_t    xid;
    uint16_t    secs;
    uint16_t    flags;
    uint8_t     ciaddr[4];
    uint8_t     yiaddr[4];
    uint8_t     siaddr[4];
    uint8_t     giaddr[4];
    uint8_t     chaddr[6];
} RIP_MSG_FIXED;

class   DhcpClient
{
private:
    uint32_t    _dhcpInitialTransactionId;
    uint32_t    _dhcpTransactionId;
    uint8_t     _dhcpMacAddr[6];
    uint8_t     _dhcpLocalIp[4];
    uint8_t     _dhcpSubnetMask[4];
    uint8_t     _dhcpGatewayIp[4];
    uint8_t     _dhcpDhcpServerIp[4];
    uint8_t     _dhcpDnsServerIp[4];
    uint32_t    _dhcpLeaseTime;
    uint32_t    _dhcpT1, _dhcpT2;
    time_t      _renewInSec;
    time_t      _rebindInSec;
    time_t      _lastCheck;
    time_t      _timeout;
    time_t      _responseTimeout;
    time_t      _secTimeout;
    uint8_t     _dhcp_state;
    UdpSocket   _dhcpUdpSocket;

    int         requestDhcpLease();
    void        resetDhcpLease();
    void        presendDhcp();
    void        sendDhcpMessage(uint8_t, uint16_t);
    void        printByte(char* , uint8_t);

    uint8_t     parseDhcpResponse(time_t responseTimeout, uint32_t& transactionId);

public:
    IpAddress   getLocalIp();
    IpAddress   getSubnetMask();
    IpAddress   getGatewayIp();
    IpAddress   getDhcpServerIp();
    IpAddress   getDnsServerIp();

    int         begin(uint8_t* mac, unsigned long timeout = 60, unsigned long responseTimeout = 4);
    int         checkLease();
};
#endif