123
UipEthernet.h@11:647d53d146f1, 2019-08-30 (annotated)
- Committer:
- hudakz
- Date:
- Fri Aug 30 08:11:40 2019 +0000
- Revision:
- 11:647d53d146f1
- Parent:
- 9:a156d3de5647
- Child:
- 13:95c00132cd98
"set_network" method of Mbed style added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 9:a156d3de5647 | 1 | /* |
hudakz | 9:a156d3de5647 | 2 | UipEthernet.h - Arduino implementation of a UIP wrapper class. |
hudakz | 9:a156d3de5647 | 3 | Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> |
hudakz | 9:a156d3de5647 | 4 | All rights reserved. |
hudakz | 9:a156d3de5647 | 5 | |
hudakz | 9:a156d3de5647 | 6 | Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> |
hudakz | 9:a156d3de5647 | 7 | |
hudakz | 9:a156d3de5647 | 8 | This program is free software: you can redistribute it and/or modify |
hudakz | 9:a156d3de5647 | 9 | it under the terms of the GNU General Public License as published by |
hudakz | 9:a156d3de5647 | 10 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 9:a156d3de5647 | 11 | (at your option) any later version. |
hudakz | 9:a156d3de5647 | 12 | |
hudakz | 9:a156d3de5647 | 13 | This program is distributed in the hope that it will be useful, |
hudakz | 9:a156d3de5647 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 9:a156d3de5647 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 9:a156d3de5647 | 16 | GNU General Public License for more details. |
hudakz | 9:a156d3de5647 | 17 | |
hudakz | 9:a156d3de5647 | 18 | You should have received a copy of the GNU General Public License |
hudakz | 9:a156d3de5647 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 9:a156d3de5647 | 20 | */ |
hudakz | 11:647d53d146f1 | 21 | #ifndef UIPETHERNET_h |
hudakz | 11:647d53d146f1 | 22 | #define UIPETHERNET_h |
hudakz | 9:a156d3de5647 | 23 | |
hudakz | 9:a156d3de5647 | 24 | //#define UIPETHERNET_DEBUG |
hudakz | 9:a156d3de5647 | 25 | //#define UIPETHERNET_DEBUG_CHKSUM |
hudakz | 9:a156d3de5647 | 26 | //#define UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 27 | //#define UIPETHERNET_DEBUG_CLIENT |
hudakz | 9:a156d3de5647 | 28 | #include "mbed.h" |
hudakz | 9:a156d3de5647 | 29 | #include "DhcpClient.h" |
hudakz | 9:a156d3de5647 | 30 | #include "IpAddress.h" |
hudakz | 11:647d53d146f1 | 31 | #include "utility/Enc28j60Eth.h" |
hudakz | 9:a156d3de5647 | 32 | #include "TcpClient.h" |
hudakz | 9:a156d3de5647 | 33 | #include "TcpServer.h" |
hudakz | 9:a156d3de5647 | 34 | #include "UdpSocket.h" |
hudakz | 9:a156d3de5647 | 35 | |
hudakz | 9:a156d3de5647 | 36 | extern "C" |
hudakz | 9:a156d3de5647 | 37 | { |
hudakz | 9:a156d3de5647 | 38 | #include "utility/uip_timer.h" |
hudakz | 9:a156d3de5647 | 39 | #include "utility/uip.h" |
hudakz | 9:a156d3de5647 | 40 | } |
hudakz | 9:a156d3de5647 | 41 | #define UIPETHERNET_FREEPACKET 1 |
hudakz | 9:a156d3de5647 | 42 | #define UIPETHERNET_SENDPACKET 2 |
hudakz | 9:a156d3de5647 | 43 | |
hudakz | 9:a156d3de5647 | 44 | #define uip_ip_addr(addr, ip) \ |
hudakz | 9:a156d3de5647 | 45 | do { \ |
hudakz | 9:a156d3de5647 | 46 | ((u16_t *) (addr))[0] = (((ip[1]) << 8) | (ip[0])); \ |
hudakz | 9:a156d3de5647 | 47 | ((u16_t *) (addr))[1] = (((ip[3]) << 8) | (ip[2])); \ |
hudakz | 9:a156d3de5647 | 48 | } while (0) |
hudakz | 9:a156d3de5647 | 49 | #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 | 9:a156d3de5647 | 50 | |
hudakz | 9:a156d3de5647 | 51 | #define uip_seteth_addr(eaddr) \ |
hudakz | 9:a156d3de5647 | 52 | do { \ |
hudakz | 9:a156d3de5647 | 53 | uip_ethaddr.addr[0] = eaddr[0]; \ |
hudakz | 9:a156d3de5647 | 54 | uip_ethaddr.addr[1] = eaddr[1]; \ |
hudakz | 9:a156d3de5647 | 55 | uip_ethaddr.addr[2] = eaddr[2]; \ |
hudakz | 9:a156d3de5647 | 56 | uip_ethaddr.addr[3] = eaddr[3]; \ |
hudakz | 9:a156d3de5647 | 57 | uip_ethaddr.addr[4] = eaddr[4]; \ |
hudakz | 9:a156d3de5647 | 58 | uip_ethaddr.addr[5] = eaddr[5]; \ |
hudakz | 9:a156d3de5647 | 59 | } while (0) |
hudakz | 9:a156d3de5647 | 60 | #define BUF ((struct uip_tcpip_hdr*) &uip_buf[UIP_LLH_LEN]) |
hudakz | 9:a156d3de5647 | 61 | |
hudakz | 9:a156d3de5647 | 62 | class UipEthernet |
hudakz | 9:a156d3de5647 | 63 | { |
hudakz | 9:a156d3de5647 | 64 | public: |
hudakz | 9:a156d3de5647 | 65 | static UipEthernet* ethernet; |
hudakz | 9:a156d3de5647 | 66 | static IpAddress dnsServerAddress; |
hudakz | 11:647d53d146f1 | 67 | Enc28j60Eth enc28j60Eth; |
hudakz | 9:a156d3de5647 | 68 | |
hudakz | 9:a156d3de5647 | 69 | UipEthernet (const uint8_t mac[6], PinName mosi, PinName miso, PinName sck, PinName cs); |
hudakz | 9:a156d3de5647 | 70 | |
hudakz | 9:a156d3de5647 | 71 | int connect(); |
hudakz | 9:a156d3de5647 | 72 | void set_network(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4); |
hudakz | 9:a156d3de5647 | 73 | void set_network(IpAddress ip); |
hudakz | 9:a156d3de5647 | 74 | void set_network(IpAddress ip, IpAddress dns); |
hudakz | 9:a156d3de5647 | 75 | void set_network(IpAddress ip, IpAddress dns, IpAddress gateway); |
hudakz | 9:a156d3de5647 | 76 | void set_network(IpAddress ip, IpAddress dns, IpAddress gateway, IpAddress subnet); |
hudakz | 11:647d53d146f1 | 77 | void set_network(const char *ip_address, const char *netmask, const char *gateway); |
hudakz | 9:a156d3de5647 | 78 | void tick(); |
hudakz | 9:a156d3de5647 | 79 | IpAddress localIP(); |
hudakz | 9:a156d3de5647 | 80 | IpAddress subnetMask(); |
hudakz | 9:a156d3de5647 | 81 | IpAddress gatewayIP(); |
hudakz | 9:a156d3de5647 | 82 | IpAddress dnsServerIP(); |
hudakz | 9:a156d3de5647 | 83 | const char* get_ip_address(); |
hudakz | 9:a156d3de5647 | 84 | const char* get_netmask(); |
hudakz | 9:a156d3de5647 | 85 | const char* get_gateway(); |
hudakz | 9:a156d3de5647 | 86 | static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len); |
hudakz | 9:a156d3de5647 | 87 | static uint16_t ipchksum(); |
hudakz | 11:647d53d146f1 | 88 | bool stoip4(const char *ip4addr, size_t len, void *dest); |
hudakz | 9:a156d3de5647 | 89 | private: |
hudakz | 9:a156d3de5647 | 90 | uint8_t *const _mac; |
hudakz | 9:a156d3de5647 | 91 | IpAddress _ip; |
hudakz | 9:a156d3de5647 | 92 | IpAddress _dns; |
hudakz | 9:a156d3de5647 | 93 | IpAddress _gateway; |
hudakz | 9:a156d3de5647 | 94 | IpAddress _subnet; |
hudakz | 9:a156d3de5647 | 95 | static memhandle inPacket; |
hudakz | 9:a156d3de5647 | 96 | static memhandle uipPacket; |
hudakz | 9:a156d3de5647 | 97 | static uint8_t uipHeaderLen; |
hudakz | 9:a156d3de5647 | 98 | static uint8_t packetState; |
hudakz | 9:a156d3de5647 | 99 | DhcpClient dhcpClient; |
hudakz | 9:a156d3de5647 | 100 | Timer periodicTimer; |
hudakz | 9:a156d3de5647 | 101 | void init(const uint8_t* mac); |
hudakz | 9:a156d3de5647 | 102 | bool network_send(); |
hudakz | 9:a156d3de5647 | 103 | friend class TcpServer; |
hudakz | 9:a156d3de5647 | 104 | friend class TcpClient; |
hudakz | 9:a156d3de5647 | 105 | friend class UdpSocket; |
hudakz | 9:a156d3de5647 | 106 | #if UIP_UDP |
hudakz | 9:a156d3de5647 | 107 | uint16_t upper_layer_chksum(uint8_t proto); |
hudakz | 9:a156d3de5647 | 108 | #endif |
hudakz | 9:a156d3de5647 | 109 | friend uint16_t uip_ipchksum(); |
hudakz | 9:a156d3de5647 | 110 | friend uint16_t uip_tcpchksum(); |
hudakz | 9:a156d3de5647 | 111 | friend uint16_t uip_udpchksum(); |
hudakz | 9:a156d3de5647 | 112 | friend void uipclient_appcall(); |
hudakz | 9:a156d3de5647 | 113 | friend void uipudp_appcall(); |
hudakz | 9:a156d3de5647 | 114 | |
hudakz | 9:a156d3de5647 | 115 | #if UIP_CONF_IPV6 |
hudakz | 9:a156d3de5647 | 116 | uint16_t uip_icmp6chksum(); |
hudakz | 9:a156d3de5647 | 117 | #endif |
hudakz | 9:a156d3de5647 | 118 | }; |
hudakz | 11:647d53d146f1 | 119 | |
hudakz | 9:a156d3de5647 | 120 | #endif |