Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/

Committer:
AdamGreen
Date:
Sat Oct 26 08:51:36 2013 +0000
Revision:
1:eadc868c2acf
Parent:
0:3b00827bb0b7
Fix TCP checksum bug and stranded large TCP segments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AdamGreen 0:3b00827bb0b7 1 /* EthernetInterface.cpp */
AdamGreen 0:3b00827bb0b7 2 /* Copyright (C) 2012 mbed.org, MIT License
AdamGreen 0:3b00827bb0b7 3 *
AdamGreen 0:3b00827bb0b7 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
AdamGreen 0:3b00827bb0b7 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
AdamGreen 0:3b00827bb0b7 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
AdamGreen 0:3b00827bb0b7 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
AdamGreen 0:3b00827bb0b7 8 * furnished to do so, subject to the following conditions:
AdamGreen 0:3b00827bb0b7 9 *
AdamGreen 0:3b00827bb0b7 10 * The above copyright notice and this permission notice shall be included in all copies or
AdamGreen 0:3b00827bb0b7 11 * substantial portions of the Software.
AdamGreen 0:3b00827bb0b7 12 *
AdamGreen 0:3b00827bb0b7 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
AdamGreen 0:3b00827bb0b7 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
AdamGreen 0:3b00827bb0b7 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
AdamGreen 0:3b00827bb0b7 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AdamGreen 0:3b00827bb0b7 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AdamGreen 0:3b00827bb0b7 18 */
AdamGreen 0:3b00827bb0b7 19 #include "EthernetInterface.h"
AdamGreen 0:3b00827bb0b7 20
AdamGreen 0:3b00827bb0b7 21 #include "lwip/inet.h"
AdamGreen 0:3b00827bb0b7 22 #include "lwip/netif.h"
AdamGreen 0:3b00827bb0b7 23 #include "netif/etharp.h"
AdamGreen 0:3b00827bb0b7 24 #include "lwip/dhcp.h"
AdamGreen 0:3b00827bb0b7 25 #include "arch/lpc17_emac.h"
AdamGreen 0:3b00827bb0b7 26 #include "lpc_phy.h"
AdamGreen 0:3b00827bb0b7 27 #include "lwip/tcpip.h"
AdamGreen 0:3b00827bb0b7 28
AdamGreen 0:3b00827bb0b7 29 #include "mbed.h"
AdamGreen 0:3b00827bb0b7 30
AdamGreen 0:3b00827bb0b7 31 /* TCP/IP and Network Interface Initialisation */
AdamGreen 0:3b00827bb0b7 32 static struct netif lpcNetif;
AdamGreen 0:3b00827bb0b7 33
AdamGreen 0:3b00827bb0b7 34 static char mac_addr[19];
AdamGreen 0:3b00827bb0b7 35 static char ip_addr[17] = "\0";
AdamGreen 0:3b00827bb0b7 36 static bool use_dhcp = false;
AdamGreen 0:3b00827bb0b7 37
AdamGreen 0:3b00827bb0b7 38 static Semaphore tcpip_inited(0);
AdamGreen 0:3b00827bb0b7 39 static Semaphore netif_linked(0);
AdamGreen 0:3b00827bb0b7 40 static Semaphore netif_up(0);
AdamGreen 0:3b00827bb0b7 41
AdamGreen 0:3b00827bb0b7 42 static void tcpip_init_done(void *arg) {
AdamGreen 0:3b00827bb0b7 43 tcpip_inited.release();
AdamGreen 0:3b00827bb0b7 44 }
AdamGreen 0:3b00827bb0b7 45
AdamGreen 0:3b00827bb0b7 46 static void netif_link_callback(struct netif *netif) {
AdamGreen 0:3b00827bb0b7 47 if (netif_is_link_up(netif)) {
AdamGreen 0:3b00827bb0b7 48 netif_linked.release();
AdamGreen 0:3b00827bb0b7 49 }
AdamGreen 0:3b00827bb0b7 50 }
AdamGreen 0:3b00827bb0b7 51
AdamGreen 0:3b00827bb0b7 52 static void netif_status_callback(struct netif *netif) {
AdamGreen 0:3b00827bb0b7 53 if (netif_is_up(netif)) {
AdamGreen 0:3b00827bb0b7 54 strcpy(ip_addr, inet_ntoa(netif->ip_addr));
AdamGreen 0:3b00827bb0b7 55 netif_up.release();
AdamGreen 0:3b00827bb0b7 56 }
AdamGreen 0:3b00827bb0b7 57 }
AdamGreen 0:3b00827bb0b7 58
AdamGreen 0:3b00827bb0b7 59 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
AdamGreen 0:3b00827bb0b7 60 tcpip_init(tcpip_init_done, NULL);
AdamGreen 0:3b00827bb0b7 61 tcpip_inited.wait();
AdamGreen 0:3b00827bb0b7 62
AdamGreen 0:3b00827bb0b7 63 memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
AdamGreen 0:3b00827bb0b7 64 netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input);
AdamGreen 0:3b00827bb0b7 65 netif_set_default(&lpcNetif);
AdamGreen 0:3b00827bb0b7 66
AdamGreen 0:3b00827bb0b7 67 netif_set_link_callback (&lpcNetif, netif_link_callback);
AdamGreen 0:3b00827bb0b7 68 netif_set_status_callback(&lpcNetif, netif_status_callback);
AdamGreen 0:3b00827bb0b7 69 }
AdamGreen 0:3b00827bb0b7 70
AdamGreen 0:3b00827bb0b7 71 static void set_mac_address(void) {
AdamGreen 0:3b00827bb0b7 72 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
AdamGreen 0:3b00827bb0b7 73 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
AdamGreen 0:3b00827bb0b7 74 MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
AdamGreen 0:3b00827bb0b7 75 #else
AdamGreen 0:3b00827bb0b7 76 char mac[6];
AdamGreen 0:3b00827bb0b7 77 mbed_mac_address(mac);
AdamGreen 0:3b00827bb0b7 78 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
AdamGreen 0:3b00827bb0b7 79 #endif
AdamGreen 0:3b00827bb0b7 80 }
AdamGreen 0:3b00827bb0b7 81
AdamGreen 0:3b00827bb0b7 82 int EthernetInterface::init() {
AdamGreen 0:3b00827bb0b7 83 use_dhcp = true;
AdamGreen 0:3b00827bb0b7 84 set_mac_address();
AdamGreen 0:3b00827bb0b7 85 init_netif(NULL, NULL, NULL);
AdamGreen 0:3b00827bb0b7 86 return 0;
AdamGreen 0:3b00827bb0b7 87 }
AdamGreen 0:3b00827bb0b7 88
AdamGreen 0:3b00827bb0b7 89 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
AdamGreen 0:3b00827bb0b7 90 use_dhcp = false;
AdamGreen 0:3b00827bb0b7 91
AdamGreen 0:3b00827bb0b7 92 set_mac_address();
AdamGreen 0:3b00827bb0b7 93 strcpy(ip_addr, ip);
AdamGreen 0:3b00827bb0b7 94
AdamGreen 0:3b00827bb0b7 95 ip_addr_t ip_n, mask_n, gateway_n;
AdamGreen 0:3b00827bb0b7 96 inet_aton(ip, &ip_n);
AdamGreen 0:3b00827bb0b7 97 inet_aton(mask, &mask_n);
AdamGreen 0:3b00827bb0b7 98 inet_aton(gateway, &gateway_n);
AdamGreen 0:3b00827bb0b7 99 init_netif(&ip_n, &mask_n, &gateway_n);
AdamGreen 0:3b00827bb0b7 100
AdamGreen 0:3b00827bb0b7 101 return 0;
AdamGreen 0:3b00827bb0b7 102 }
AdamGreen 0:3b00827bb0b7 103
AdamGreen 0:3b00827bb0b7 104 int EthernetInterface::connect(unsigned int timeout_ms) {
AdamGreen 0:3b00827bb0b7 105 NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
AdamGreen 0:3b00827bb0b7 106 NVIC_EnableIRQ(ENET_IRQn);
AdamGreen 0:3b00827bb0b7 107
AdamGreen 0:3b00827bb0b7 108 int inited;
AdamGreen 0:3b00827bb0b7 109 if (use_dhcp) {
AdamGreen 0:3b00827bb0b7 110 dhcp_start(&lpcNetif);
AdamGreen 0:3b00827bb0b7 111
AdamGreen 0:3b00827bb0b7 112 // Wait for an IP Address
AdamGreen 0:3b00827bb0b7 113 // -1: error, 0: timeout
AdamGreen 0:3b00827bb0b7 114 inited = netif_up.wait(timeout_ms);
AdamGreen 0:3b00827bb0b7 115 } else {
AdamGreen 0:3b00827bb0b7 116 netif_set_up(&lpcNetif);
AdamGreen 0:3b00827bb0b7 117
AdamGreen 0:3b00827bb0b7 118 // Wait for the link up
AdamGreen 0:3b00827bb0b7 119 inited = netif_linked.wait(timeout_ms);
AdamGreen 0:3b00827bb0b7 120 }
AdamGreen 0:3b00827bb0b7 121
AdamGreen 0:3b00827bb0b7 122 return (inited > 0) ? (0) : (-1);
AdamGreen 0:3b00827bb0b7 123 }
AdamGreen 0:3b00827bb0b7 124
AdamGreen 0:3b00827bb0b7 125 int EthernetInterface::disconnect() {
AdamGreen 0:3b00827bb0b7 126 if (use_dhcp) {
AdamGreen 0:3b00827bb0b7 127 dhcp_release(&lpcNetif);
AdamGreen 0:3b00827bb0b7 128 dhcp_stop(&lpcNetif);
AdamGreen 0:3b00827bb0b7 129 } else {
AdamGreen 0:3b00827bb0b7 130 netif_set_down(&lpcNetif);
AdamGreen 0:3b00827bb0b7 131 }
AdamGreen 0:3b00827bb0b7 132
AdamGreen 0:3b00827bb0b7 133 NVIC_DisableIRQ(ENET_IRQn);
AdamGreen 0:3b00827bb0b7 134
AdamGreen 0:3b00827bb0b7 135 return 0;
AdamGreen 0:3b00827bb0b7 136 }
AdamGreen 0:3b00827bb0b7 137
AdamGreen 0:3b00827bb0b7 138 char* EthernetInterface::getMACAddress() {
AdamGreen 0:3b00827bb0b7 139 return mac_addr;
AdamGreen 0:3b00827bb0b7 140 }
AdamGreen 0:3b00827bb0b7 141
AdamGreen 0:3b00827bb0b7 142 char* EthernetInterface::getIPAddress() {
AdamGreen 0:3b00827bb0b7 143 return ip_addr;
AdamGreen 0:3b00827bb0b7 144 }