The EthernetInterface library as found on mbed

Dependencies:   Socket lwip-eth lwip-sys lwip

Fork of EthernetInterface by Jonathon Fletcher

Committer:
jonathonfletcher
Date:
Sat Sep 01 11:15:56 2012 +0000
Revision:
26:132037eec2ba
Parent:
22:2a797ba9babe
add getMACAddress() in the style of getIPAddress(). Initialize the MAC string from the constructor.

Who changed what in which revision?

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