adds getMACAddress() like getIPAddress(). Initializes in constructor.
Dependencies: Socket lwip-eth lwip-sys lwip
Fork of EthernetInterface by
Diff: EthernetInterface.cpp
- Revision:
- 26:132037eec2ba
- Parent:
- 22:2a797ba9babe
diff -r a0ee3ae75cfa -r 132037eec2ba EthernetInterface.cpp --- a/EthernetInterface.cpp Mon Aug 13 09:38:08 2012 +0000 +++ b/EthernetInterface.cpp Sat Sep 01 11:15:56 2012 +0000 @@ -35,34 +35,48 @@ static Semaphore netif_inited(0); static char ip_addr[16]; -static bool connected; +static char mac_addr[18]; +static bool connected = false; static bool use_dhcp = false; -static void tcpip_init_done(void *arg) { +static void tcpip_init_done(void *arg) +{ tcpip_inited.release(); } -static void netif_status_callback(struct netif *netif) { + +static void netif_status_callback(struct netif *netif) +{ strcpy(ip_addr, inet_ntoa(netif->ip_addr)); connected = netif_is_up(netif) ? true : false; netif_inited.release(); } -static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { +static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) +{ tcpip_init(tcpip_init_done, NULL); tcpip_inited.wait(); - + memset((void*) &lpcNetif, 0, sizeof(lpcNetif)); netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input); netif_set_default(&lpcNetif); netif_set_status_callback(&lpcNetif, netif_status_callback); } -int EthernetInterface::init() { +EthernetInterface::EthernetInterface() +{ + char mac[6]; + mbed_mac_address(mac); + snprintf(mac_addr, 18, "%02x:%02x:%02x:%02x:%02x:%02x\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + +int EthernetInterface::init() +{ use_dhcp = true; init_netif(NULL, NULL, NULL); return 0; } -int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { +int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) +{ use_dhcp = false; ip_addr_t ip_n, mask_n, gateway_n; inet_aton(ip, &ip_n); @@ -72,34 +86,42 @@ return 0; } -int EthernetInterface::connect(unsigned int timeout_ms) { +int EthernetInterface::connect(unsigned int timeout_ms) +{ NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); NVIC_EnableIRQ(ENET_IRQn); - + if (use_dhcp) { dhcp_start(&lpcNetif); } else { netif_set_up(&lpcNetif); } - + // -1: error, 0: timeout int inited = netif_inited.wait(timeout_ms); return (inited > 0) ? (0) : (-1); } -int EthernetInterface::disconnect() { +int EthernetInterface::disconnect() +{ if (use_dhcp) { dhcp_release(&lpcNetif); dhcp_stop(&lpcNetif); } else { netif_set_down(&lpcNetif); } - + NVIC_DisableIRQ(ENET_IRQn); - + return 0; } -char* EthernetInterface::getIPAddress() { +char* EthernetInterface::getIPAddress() +{ return (connected) ? (ip_addr) : (NULL); } + +char* EthernetInterface::getMACAddress() +{ + return mac_addr; +}