mbed IP library over Ethernet
Fork of EthernetInterface by
Embed:
(wiki syntax)
Show/hide line numbers
EthernetInterface.cpp
00001 /* EthernetInterface.cpp */ 00002 /* Copyright (C) 2012 mbed.org, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 #include "EthernetInterface.h" 00020 00021 #include "lwip/inet.h" 00022 #include "lwip/netif.h" 00023 #include "netif/etharp.h" 00024 #include "lwip/dhcp.h" 00025 #include "arch/lpc17_emac.h" 00026 #include "lpc_phy.h" 00027 #include "lwip/tcpip.h" 00028 00029 #include "mbed.h" 00030 00031 /* TCP/IP and Network Interface Initialisation */ 00032 static struct netif lpcNetif; 00033 00034 static char mac_addr[19]; 00035 static char ip_addr[17] = "\0"; 00036 static bool use_dhcp = false; 00037 00038 static Semaphore tcpip_inited(0); 00039 static Semaphore netif_linked(0); 00040 static Semaphore netif_up(0); 00041 00042 static void tcpip_init_done(void *arg) { 00043 tcpip_inited.release(); 00044 } 00045 00046 static void netif_link_callback(struct netif *netif) { 00047 if (netif_is_link_up(netif)) { 00048 netif_linked.release(); 00049 } 00050 } 00051 00052 static void netif_status_callback(struct netif *netif) { 00053 if (netif_is_up(netif)) { 00054 strcpy(ip_addr, inet_ntoa(netif->ip_addr)); 00055 netif_up.release(); 00056 } 00057 } 00058 00059 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { 00060 tcpip_init(tcpip_init_done, NULL); 00061 tcpip_inited.wait(); 00062 00063 memset((void*) &lpcNetif, 0, sizeof(lpcNetif)); 00064 netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input); 00065 netif_set_default(&lpcNetif); 00066 00067 netif_set_link_callback (&lpcNetif, netif_link_callback); 00068 netif_set_status_callback(&lpcNetif, netif_status_callback); 00069 } 00070 00071 static void set_mac_address(void) { 00072 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) 00073 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2, 00074 MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5); 00075 #else 00076 char mac[6]; 00077 mbed_mac_address(mac); 00078 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 00079 #endif 00080 } 00081 00082 int EthernetInterface::init() { 00083 use_dhcp = true; 00084 set_mac_address(); 00085 init_netif(NULL, NULL, NULL); 00086 return 0; 00087 } 00088 00089 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { 00090 use_dhcp = false; 00091 00092 set_mac_address(); 00093 strcpy(ip_addr, ip); 00094 00095 ip_addr_t ip_n, mask_n, gateway_n; 00096 inet_aton(ip, &ip_n); 00097 inet_aton(mask, &mask_n); 00098 inet_aton(gateway, &gateway_n); 00099 init_netif(&ip_n, &mask_n, &gateway_n); 00100 00101 return 0; 00102 } 00103 00104 int EthernetInterface::connect(unsigned int timeout_ms) { 00105 NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); 00106 NVIC_EnableIRQ(ENET_IRQn); 00107 00108 int inited; 00109 if (use_dhcp) { 00110 dhcp_start(&lpcNetif); 00111 00112 // Wait for an IP Address 00113 // -1: error, 0: timeout 00114 inited = netif_up.wait(timeout_ms); 00115 } else { 00116 netif_set_up(&lpcNetif); 00117 00118 // Wait for the link up 00119 inited = netif_linked.wait(timeout_ms); 00120 } 00121 00122 return (inited > 0) ? (0) : (-1); 00123 } 00124 00125 int EthernetInterface::disconnect() { 00126 if (use_dhcp) { 00127 dhcp_release(&lpcNetif); 00128 dhcp_stop(&lpcNetif); 00129 } else { 00130 netif_set_down(&lpcNetif); 00131 } 00132 00133 NVIC_DisableIRQ(ENET_IRQn); 00134 00135 return 0; 00136 } 00137 00138 char* EthernetInterface::getMACAddress() { 00139 return mac_addr; 00140 } 00141 00142 char* EthernetInterface::getIPAddress() { 00143 return ip_addr; 00144 }
Generated on Fri Jul 15 2022 10:23:35 by 1.7.2