mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Committer:
hudakz
Date:
Sat Dec 20 11:08:11 2014 +0000
Revision:
2:049ce85163c5
Parent:
0:5350a66d5279
Child:
4:d774541a34da
02 Name clash with "Ethernet" fixed for LPC1768

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:5350a66d5279 1 /*
hudakz 0:5350a66d5279 2 UIPEthernet.h - Arduino implementation of a uIP wrapper class.
hudakz 0:5350a66d5279 3 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
hudakz 0:5350a66d5279 4 All rights reserved.
hudakz 0:5350a66d5279 5
hudakz 0:5350a66d5279 6 Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com>
hudakz 0:5350a66d5279 7
hudakz 0:5350a66d5279 8 This program is free software: you can redistribute it and/or modify
hudakz 0:5350a66d5279 9 it under the terms of the GNU General Public License as published by
hudakz 0:5350a66d5279 10 the Free Software Foundation, either version 3 of the License, or
hudakz 0:5350a66d5279 11 (at your option) any later version.
hudakz 0:5350a66d5279 12
hudakz 0:5350a66d5279 13 This program is distributed in the hope that it will be useful,
hudakz 0:5350a66d5279 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:5350a66d5279 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:5350a66d5279 16 GNU General Public License for more details.
hudakz 0:5350a66d5279 17
hudakz 0:5350a66d5279 18 You should have received a copy of the GNU General Public License
hudakz 0:5350a66d5279 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:5350a66d5279 20 */
hudakz 0:5350a66d5279 21 #pragma once
hudakz 0:5350a66d5279 22 #ifndef UIPETHERNET_H
hudakz 0:5350a66d5279 23 #define UIPETHERNET_H
hudakz 0:5350a66d5279 24
hudakz 0:5350a66d5279 25 #include "ethernet_comp.h"
hudakz 0:5350a66d5279 26 #include <mbed.h>
hudakz 0:5350a66d5279 27 #include "Dhcp.h"
hudakz 2:049ce85163c5 28 #include "utility/IPAddress.h"
hudakz 2:049ce85163c5 29 #include "utility/Enc28J60Network.h"
hudakz 0:5350a66d5279 30 #include "UIPClient.h"
hudakz 0:5350a66d5279 31 #include "UIPServer.h"
hudakz 0:5350a66d5279 32 #include "UIPUdp.h"
hudakz 0:5350a66d5279 33
hudakz 0:5350a66d5279 34 extern "C"
hudakz 0:5350a66d5279 35 {
hudakz 2:049ce85163c5 36 #include "utility/uip_timer.h"
hudakz 2:049ce85163c5 37 #include "utility/uip.h"
hudakz 0:5350a66d5279 38 }
hudakz 0:5350a66d5279 39 //#define UIPETHERNET_DEBUG
hudakz 0:5350a66d5279 40 //#define UIPETHERNET_DEBUG_CHKSUM
hudakz 0:5350a66d5279 41 //#define UIPETHERNET_DEBUG_UDP
hudakz 0:5350a66d5279 42 //#define UIPETHERNET_DEBUG_CLIENT
hudakz 0:5350a66d5279 43 #define UIPETHERNET_FREEPACKET 1
hudakz 0:5350a66d5279 44 #define UIPETHERNET_SENDPACKET 2
hudakz 0:5350a66d5279 45 #define UIPETHERNET_BUFFERREAD 4
hudakz 0:5350a66d5279 46
hudakz 0:5350a66d5279 47 #define uip_ip_addr(addr, ip) \
hudakz 0:5350a66d5279 48 do \
hudakz 0:5350a66d5279 49 { \
hudakz 0:5350a66d5279 50 ((u16_t *) (addr))[0] = HTONS(((ip[0]) << 8) | (ip[1])); \
hudakz 0:5350a66d5279 51 ((u16_t *) (addr))[1] = HTONS(((ip[2]) << 8) | (ip[3])); \
hudakz 0:5350a66d5279 52 } while(0)
hudakz 0:5350a66d5279 53 #define ip_addr_uip(a) IPAddress(a[0] & 0xFF, a[0] >> 8, a[1] & 0xFF, a[1] >> 8); //TODO this is not IPV6 capable
hudakz 0:5350a66d5279 54
hudakz 0:5350a66d5279 55 #define uip_seteth_addr(eaddr) \
hudakz 0:5350a66d5279 56 do \
hudakz 0:5350a66d5279 57 { \
hudakz 0:5350a66d5279 58 uip_ethaddr.addr[0] = eaddr[0]; \
hudakz 0:5350a66d5279 59 uip_ethaddr.addr[1] = eaddr[1]; \
hudakz 0:5350a66d5279 60 uip_ethaddr.addr[2] = eaddr[2]; \
hudakz 0:5350a66d5279 61 uip_ethaddr.addr[3] = eaddr[3]; \
hudakz 0:5350a66d5279 62 uip_ethaddr.addr[4] = eaddr[4]; \
hudakz 0:5350a66d5279 63 uip_ethaddr.addr[5] = eaddr[5]; \
hudakz 0:5350a66d5279 64 } while(0)
hudakz 0:5350a66d5279 65
hudakz 0:5350a66d5279 66 typedef void (*fn_uip_cb_t) (uip_tcp_appstate_t * conn);
hudakz 0:5350a66d5279 67
hudakz 0:5350a66d5279 68 typedef void (*fn_uip_udp_cb_t) (uip_udp_appstate_t * conn);
hudakz 0:5350a66d5279 69
hudakz 0:5350a66d5279 70 #define BUF ((struct uip_tcpip_hdr*) &uip_buf[UIP_LLH_LEN])
hudakz 0:5350a66d5279 71
hudakz 0:5350a66d5279 72 class UIPEthernetClass
hudakz 0:5350a66d5279 73 {
hudakz 0:5350a66d5279 74 public:
hudakz 0:5350a66d5279 75 UIPEthernetClass(PinName mosi, PinName miso, PinName sck, PinName cs);
hudakz 0:5350a66d5279 76
hudakz 0:5350a66d5279 77 int begin(const uint8_t* mac);
hudakz 0:5350a66d5279 78 void begin(const uint8_t* mac, IPAddress ip);
hudakz 0:5350a66d5279 79 void begin(const uint8_t* mac, IPAddress ip, IPAddress dns);
hudakz 0:5350a66d5279 80 void begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway);
hudakz 0:5350a66d5279 81 void begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
hudakz 0:5350a66d5279 82
hudakz 0:5350a66d5279 83 // maintain() must be called at regular intervals to process the incoming serial
hudakz 0:5350a66d5279 84 // data and issue IP events to the sketch. It does not return until all IP
hudakz 0:5350a66d5279 85 // events have been processed. Renews dhcp-lease if required.
hudakz 0:5350a66d5279 86 int maintain(void);
hudakz 0:5350a66d5279 87
hudakz 0:5350a66d5279 88 IPAddress localIP(void);
hudakz 0:5350a66d5279 89 IPAddress subnetMask(void);
hudakz 0:5350a66d5279 90 IPAddress gatewayIP(void);
hudakz 0:5350a66d5279 91 IPAddress dnsServerIP(void);
hudakz 0:5350a66d5279 92
hudakz 0:5350a66d5279 93 // Set a user function to handle raw uIP events as they happen. The
hudakz 0:5350a66d5279 94 // callback function can only use uIP functions, but it can also use uIP's
hudakz 0:5350a66d5279 95 // protosockets.
hudakz 0:5350a66d5279 96 void set_uip_callback(fn_uip_cb_t fn);
hudakz 0:5350a66d5279 97 void set_uip_udp_callback(fn_uip_udp_cb_t fn);
hudakz 0:5350a66d5279 98 private:
hudakz 0:5350a66d5279 99 IPAddress _dnsServerAddress;
hudakz 0:5350a66d5279 100 DhcpClass* _dhcp;
hudakz 0:5350a66d5279 101
hudakz 0:5350a66d5279 102 struct uip_timer periodic_timer;
hudakz 0:5350a66d5279 103 fn_uip_cb_t fn_uip_cb;
hudakz 0:5350a66d5279 104 fn_uip_udp_cb_t fn_uip_udp_cb;
hudakz 0:5350a66d5279 105
hudakz 0:5350a66d5279 106 memhandle in_packet;
hudakz 0:5350a66d5279 107 memhandle uip_packet;
hudakz 0:5350a66d5279 108 uint8_t uip_hdrlen;
hudakz 0:5350a66d5279 109 uint8_t packetstate;
hudakz 0:5350a66d5279 110
hudakz 0:5350a66d5279 111 Enc28J60Network network;
hudakz 0:5350a66d5279 112
hudakz 0:5350a66d5279 113 void init(const uint8_t* mac);
hudakz 0:5350a66d5279 114 void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
hudakz 0:5350a66d5279 115 void tick(void);
hudakz 0:5350a66d5279 116 bool network_send(void);
hudakz 0:5350a66d5279 117 void uip_callback(void);
hudakz 0:5350a66d5279 118 friend void uipethernet_appcall(void);
hudakz 0:5350a66d5279 119 void uip_udp_callback(void);
hudakz 0:5350a66d5279 120 friend void uipudp_appcall(void);
hudakz 0:5350a66d5279 121 friend class UIPServer;
hudakz 0:5350a66d5279 122 friend class UIPClient;
hudakz 0:5350a66d5279 123 friend class UIPUDP;
hudakz 0:5350a66d5279 124
hudakz 0:5350a66d5279 125 static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len);
hudakz 0:5350a66d5279 126 static uint16_t ipchksum(void);
hudakz 0:5350a66d5279 127 uint16_t upper_layer_chksum(uint8_t proto);
hudakz 0:5350a66d5279 128
hudakz 0:5350a66d5279 129 friend uint16_t uip_ipchksum(void);
hudakz 0:5350a66d5279 130 friend uint16_t uip_tcpchksum(void);
hudakz 0:5350a66d5279 131 friend uint16_t uip_udpchksum(void);
hudakz 0:5350a66d5279 132
hudakz 0:5350a66d5279 133 #if UIP_CONF_IPV6
hudakz 0:5350a66d5279 134 uint16_t uip_icmp6chksum(void);
hudakz 0:5350a66d5279 135 #endif /* UIP_CONF_IPV6 */
hudakz 0:5350a66d5279 136 };
hudakz 0:5350a66d5279 137
hudakz 0:5350a66d5279 138 extern UIPEthernetClass UIPEthernet;
hudakz 0:5350a66d5279 139 #endif
hudakz 0:5350a66d5279 140