mbed IP library over Ethernet

Dependencies:   Socket lwip-eth lwip-sys lwip

Fork of EthernetInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetInterface.cpp Source File

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 Semaphore tcpip_inited(0);
00035 static Semaphore netif_inited(0);
00036 
00037 static char ip_addr[16];
00038 static bool connected;
00039 static bool use_dhcp = false;
00040 
00041 static void tcpip_init_done(void *arg) {
00042     tcpip_inited.release();
00043 }
00044 static void netif_status_callback(struct netif *netif) {
00045     strcpy(ip_addr, inet_ntoa(netif->ip_addr));
00046     connected = netif_is_up(netif) ? true : false;
00047     netif_inited.release();
00048 }
00049 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
00050     tcpip_init(tcpip_init_done, NULL);
00051     tcpip_inited.wait();
00052     
00053     memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
00054     netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input);
00055     netif_set_default(&lpcNetif);
00056     netif_set_status_callback(&lpcNetif, netif_status_callback);
00057 }
00058 
00059 int EthernetInterface::init() {
00060     use_dhcp = true;
00061     init_netif(NULL, NULL, NULL);
00062     return 0;
00063 }
00064 
00065 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
00066     use_dhcp = false;
00067     ip_addr_t ip_n, mask_n, gateway_n;
00068     inet_aton(ip, &ip_n);
00069     inet_aton(mask, &mask_n);
00070     inet_aton(gateway, &gateway_n);
00071     init_netif(&ip_n, &mask_n, &gateway_n);
00072     return 0;
00073 }
00074 
00075 int EthernetInterface::connect(unsigned int timeout_ms) {
00076     NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
00077     NVIC_EnableIRQ(ENET_IRQn);
00078     
00079     if (use_dhcp) {
00080         dhcp_start(&lpcNetif);
00081     } else {
00082         netif_set_up(&lpcNetif);
00083     }
00084     
00085     // -1: error, 0: timeout
00086     int inited = netif_inited.wait(timeout_ms);
00087     return (inited > 0) ? (0) : (-1);
00088 }
00089 
00090 int EthernetInterface::disconnect() {
00091     if (use_dhcp) {
00092         dhcp_release(&lpcNetif);
00093         dhcp_stop(&lpcNetif);
00094     } else {
00095         netif_set_down(&lpcNetif);
00096     }
00097     
00098     NVIC_DisableIRQ(ENET_IRQn);
00099     
00100     return 0;
00101 }
00102 
00103 char* EthernetInterface::getIPAddress() {
00104     return (connected) ? (ip_addr) : (NULL);
00105 }