Implementation of the NetworkSocketAPI for LWIP
Dependencies: lwip-eth lwip-sys lwip
Dependents: HelloLWIPInterface HelloLWIPInterfaceNonBlocking LWIPInterfaceTests SimpleHTTPExample ... more
Diff: LWIPInterface.cpp
- Revision:
- 5:2c7d2186543c
- Parent:
- 4:a7349bd7776c
- Child:
- 8:cef01e812975
--- a/LWIPInterface.cpp Mon Feb 29 23:01:54 2016 +0000 +++ b/LWIPInterface.cpp Wed Mar 02 17:21:54 2016 +0000 @@ -31,9 +31,9 @@ /* TCP/IP and Network Interface Initialisation */ -static LWIPInterface *iface = 0; static struct netif netif; +static char ip_addr[NS_IP_SIZE] = "\0"; static char mac_addr[NS_MAC_SIZE] = "\0"; static Semaphore tcpip_inited(0); @@ -52,9 +52,7 @@ static void netif_status_callback(struct netif *netif) { if (netif_is_up(netif)) { - iface->setIPAddress (inet_ntoa(netif->ip_addr)); - iface->setNetworkMask(inet_ntoa(netif->netmask)); - iface->setGateway (inet_ntoa(netif->gw)); + strcpy(ip_addr, inet_ntoa(netif->ip_addr)); netif_up.release(); } } @@ -86,44 +84,19 @@ // LWIPInterface implementation int32_t LWIPInterface::connect() { - // Only one instance of LWIP is currently supported - if (iface) { - return NS_ERROR_DEVICE_ERROR; - } - - iface = this; - // Set up network set_mac_address(); - - if (getDHCP()) { - init_netif(0, 0, 0); - } else { - ip_addr_t ip_n, mask_n, gateway_n; - inet_aton(getIPAddress(), &ip_n); - inet_aton(getNetworkMask(), &mask_n); - inet_aton(getGateway(), &gateway_n); - init_netif(&ip_n, &mask_n, &gateway_n); - } + init_netif(0, 0, 0); // Connect to network eth_arch_enable_interrupts(); - if (getDHCP()) { - dhcp_start(&netif); + dhcp_start(&netif); - // Wait for an IP Address - // -1: error, 0: timeout - if (netif_up.wait(LWIP_TIMEOUT) < 0) { - return NS_ERROR_TIMEOUT; - } - } else { - netif_set_up(&netif); - - // Wait for the link up - if (netif_linked.wait(LWIP_TIMEOUT) < 0) { - return NS_ERROR_TIMEOUT; - } + // Wait for an IP Address + // -1: error, 0: timeout + if (netif_up.wait(LWIP_TIMEOUT) < 0) { + return NS_ERROR_TIMEOUT; } return 0; @@ -131,18 +104,19 @@ int32_t LWIPInterface::disconnect() { - if (getDHCP()) { - dhcp_release(&netif); - dhcp_stop(&netif); - } else { - netif_set_down(&netif); - } + dhcp_release(&netif); + dhcp_stop(&netif); eth_arch_disable_interrupts(); return 0; } +const char *LWIPInterface::getIPAddress() +{ + return ip_addr; +} + const char *LWIPInterface::getMACAddress() { return mac_addr;