mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UIPEthernet.h Source File

UIPEthernet.h

00001 /*
00002  UIPEthernet.h - Arduino implementation of a UIP wrapper class.
00003  Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
00004  All rights reserved.
00005 
00006  Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com>
00007 
00008  This program is free software: you can redistribute it and/or modify
00009  it under the terms of the GNU General Public License as published by
00010  the Free Software Foundation, either version 3 of the License, or
00011  (at your option) any later version.
00012 
00013  This program is distributed in the hope that it will be useful,
00014  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  GNU General Public License for more details.
00017 
00018  You should have received a copy of the GNU General Public License
00019  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020   */
00021 #ifndef UIPETHERNET_H
00022 #define UIPETHERNET_H
00023 
00024 //#define UIPETHERNET_DEBUG
00025 
00026 //#define UIPETHERNET_DEBUG_CHKSUM
00027 //#define UIPETHERNET_DEBUG_UDP
00028 //#define UIPETHERNET_DEBUG_CLIENT
00029 #include "ethernet_comp.h"
00030 #include "mbed.h"
00031 #include "Dhcp.h"
00032 #include "IPAddress.h"
00033 #include "utility/Enc28J60Network.h"
00034 #include "UIPClient.h"
00035 #include "UIPServer.h"
00036 #include "UIPUdp.h"
00037 
00038 extern "C"
00039 {
00040 #include "utility/uip_timer.h"
00041 #include "utility/uip.h"
00042 }
00043 #define UIPETHERNET_FREEPACKET  1
00044 #define UIPETHERNET_SENDPACKET  2
00045 
00046 #define uip_ip_addr(addr, ip) \
00047     do { \
00048         ((u16_t *) (addr))[0] = HTONS(((ip[0]) << 8) | (ip[1])); \
00049         ((u16_t *) (addr))[1] = HTONS(((ip[2]) << 8) | (ip[3])); \
00050     } while (0)
00051 #define ip_addr_uip(a)  IPAddress(a[0] & 0xFF, a[0] >> 8, a[1] & 0xFF, a[1] >> 8)   //TODO this is not IPV6 capable
00052 
00053 #define uip_seteth_addr(eaddr) \
00054     do { \
00055         uip_ethaddr.addr[0] = eaddr[0]; \
00056         uip_ethaddr.addr[1] = eaddr[1]; \
00057         uip_ethaddr.addr[2] = eaddr[2]; \
00058         uip_ethaddr.addr[3] = eaddr[3]; \
00059         uip_ethaddr.addr[4] = eaddr[4]; \
00060         uip_ethaddr.addr[5] = eaddr[5]; \
00061     } while (0)
00062 #define BUF ((struct uip_tcpip_hdr*) &uip_buf[UIP_LLH_LEN])
00063         class   UIPEthernet
00064     {
00065     public:
00066         Enc28J60Network network;
00067 
00068         UIPEthernet(PinName mosi, PinName miso, PinName sck, PinName cs);
00069 
00070         int         begin(const uint8_t* mac);
00071         void        begin(const uint8_t* mac, IPAddress ip);
00072         void        begin(const uint8_t* mac, IPAddress ip, IPAddress dns);
00073         void        begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway);
00074         void        begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
00075 
00076         // maintain() must be called at regular intervals to process the incoming serial
00077         // data and issue IP events to the sketch.  It does not return until all IP
00078         // events have been processed. Renews dhcp-lease if required.
00079         int         maintain(void);
00080 
00081         IPAddress   localIP(void);
00082         IPAddress   subnetMask(void);
00083         IPAddress   gatewayIP(void);
00084         IPAddress   dnsServerIP(void);
00085     private:
00086         static memhandle        in_packet;
00087         static memhandle        uip_packet;
00088         static uint8_t          uip_hdrlen;
00089         static uint8_t          packetstate;
00090 
00091         static IPAddress        _dnsServerAddress;
00092         static DhcpClass*       _dhcp;
00093 
00094         static unsigned long    periodic_timer;
00095 
00096         void                    init(const uint8_t* mac);
00097         static void             configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
00098 
00099         void                    tick(void);
00100 
00101         bool                    network_send(void);
00102 
00103         friend class            UIPServer;
00104 
00105         friend class            UIPClient;
00106 
00107         friend class            UIPUDP;
00108 
00109         static uint16_t         chksum(uint16_t sum, const uint8_t* data, uint16_t len);
00110         static uint16_t         ipchksum(void);
00111 #if UIP_UDP
00112         uint16_t                upper_layer_chksum(uint8_t proto);
00113 #endif
00114         friend uint16_t         uip_ipchksum(void);
00115         friend uint16_t         uip_tcpchksum(void);
00116         friend uint16_t         uip_udpchksum(void);
00117 
00118         friend void             uipclient_appcall(void);
00119         friend void             uipudp_appcall(void);
00120 
00121 #if UIP_CONF_IPV6
00122         uint16_t                uip_icmp6chksum (void);
00123 #endif /* UIP_CONF_IPV6 */
00124     };
00125 
00126 extern UIPEthernet  uIPEthernet;
00127 #endif