Ethernet Interface Library
Dependencies: Socket lwip-eth lwip-sys lwip
Fork of EthernetInterface by
EthernetInterface.cpp@3:f5776537f27f, 2012-06-26 (annotated)
- Committer:
- donatien
- Date:
- Tue Jun 26 14:30:34 2012 +0000
- Revision:
- 3:f5776537f27f
- Child:
- 4:9a52c802be61
Update underlying libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 3:f5776537f27f | 1 | /* Ethernetnetwork.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 | |
donatien | 3:f5776537f27f | 20 | #define __DEBUG__ 4 |
donatien | 3:f5776537f27f | 21 | #ifndef __MODULE__ |
donatien | 3:f5776537f27f | 22 | #define __MODULE__ "EthernetInterface.cpp" |
donatien | 3:f5776537f27f | 23 | #endif |
donatien | 3:f5776537f27f | 24 | |
donatien | 3:f5776537f27f | 25 | #include "core/fwk.h" |
donatien | 3:f5776537f27f | 26 | |
donatien | 3:f5776537f27f | 27 | #include "EthernetInterface.h" |
donatien | 3:f5776537f27f | 28 | |
donatien | 3:f5776537f27f | 29 | #include "lwip/inet.h" |
donatien | 3:f5776537f27f | 30 | #include "lwip/netif.h" |
donatien | 3:f5776537f27f | 31 | #include "netif/etharp.h" |
donatien | 3:f5776537f27f | 32 | #include "lwip/dhcp.h" |
donatien | 3:f5776537f27f | 33 | #include "arch/lpc17_emac.h" |
donatien | 3:f5776537f27f | 34 | #include "lpc_phy.h" /* For the PHY monitor support */ |
donatien | 3:f5776537f27f | 35 | #include "lwip/tcpip.h" |
donatien | 3:f5776537f27f | 36 | |
donatien | 3:f5776537f27f | 37 | #include "mbed.h" |
donatien | 3:f5776537f27f | 38 | |
donatien | 3:f5776537f27f | 39 | |
donatien | 3:f5776537f27f | 40 | EthernetInterface::EthernetInterface() : LwIPInterface(), m_lpcNetif(), m_netifStatusSphre(1), m_useDHCP(false) |
donatien | 3:f5776537f27f | 41 | { |
donatien | 3:f5776537f27f | 42 | s_lpcNetifOff = (int32_t)( ((char*)this) - ((char*)&m_lpcNetif) ); |
donatien | 3:f5776537f27f | 43 | m_netifStatusSphre.wait(0); |
donatien | 3:f5776537f27f | 44 | } |
donatien | 3:f5776537f27f | 45 | |
donatien | 3:f5776537f27f | 46 | int EthernetInterface::init() //With DHCP |
donatien | 3:f5776537f27f | 47 | { |
donatien | 3:f5776537f27f | 48 | m_useDHCP = true; |
donatien | 3:f5776537f27f | 49 | |
donatien | 3:f5776537f27f | 50 | DBG("Initializing LwIP"); |
donatien | 3:f5776537f27f | 51 | LwIPInterface::init(); //Init LwIP, NOT including PPP |
donatien | 3:f5776537f27f | 52 | |
donatien | 3:f5776537f27f | 53 | DBG("DHCP IP assignment"); |
donatien | 3:f5776537f27f | 54 | |
donatien | 3:f5776537f27f | 55 | memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif)); |
donatien | 3:f5776537f27f | 56 | netif_add(&m_lpcNetif, NULL, NULL, NULL, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/); |
donatien | 3:f5776537f27f | 57 | netif_set_default(&m_lpcNetif); |
donatien | 3:f5776537f27f | 58 | |
donatien | 3:f5776537f27f | 59 | netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb); |
donatien | 3:f5776537f27f | 60 | |
donatien | 3:f5776537f27f | 61 | return OK; |
donatien | 3:f5776537f27f | 62 | } |
donatien | 3:f5776537f27f | 63 | |
donatien | 3:f5776537f27f | 64 | int EthernetInterface::init(const char* ip, const char* mask, const char* gateway, |
donatien | 3:f5776537f27f | 65 | const char* dns1, const char* dns2) //No DHCP |
donatien | 3:f5776537f27f | 66 | { |
donatien | 3:f5776537f27f | 67 | ip_addr_t ip_n, mask_n, gateway_n, dns1_n, dns2_n; |
donatien | 3:f5776537f27f | 68 | |
donatien | 3:f5776537f27f | 69 | m_useDHCP = false; |
donatien | 3:f5776537f27f | 70 | |
donatien | 3:f5776537f27f | 71 | DBG("Initializing LwIP"); |
donatien | 3:f5776537f27f | 72 | LwIPInterface::init(); //Init LwIP |
donatien | 3:f5776537f27f | 73 | |
donatien | 3:f5776537f27f | 74 | DBG("Static IP assignment"); |
donatien | 3:f5776537f27f | 75 | inet_aton(ip, &ip_n); |
donatien | 3:f5776537f27f | 76 | inet_aton(mask, &mask_n); |
donatien | 3:f5776537f27f | 77 | inet_aton(gateway, &gateway_n); |
donatien | 3:f5776537f27f | 78 | inet_aton(dns1, &dns1_n); |
donatien | 3:f5776537f27f | 79 | inet_aton(dns2, &dns2_n); |
donatien | 3:f5776537f27f | 80 | |
donatien | 3:f5776537f27f | 81 | memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif)); |
donatien | 3:f5776537f27f | 82 | netif_add(&m_lpcNetif, &ip_n, &mask_n, &gateway_n, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/); |
donatien | 3:f5776537f27f | 83 | netif_set_default(&m_lpcNetif); |
donatien | 3:f5776537f27f | 84 | |
donatien | 3:f5776537f27f | 85 | netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb); |
donatien | 3:f5776537f27f | 86 | |
donatien | 3:f5776537f27f | 87 | return OK; |
donatien | 3:f5776537f27f | 88 | } |
donatien | 3:f5776537f27f | 89 | |
donatien | 3:f5776537f27f | 90 | int EthernetInterface::connect() |
donatien | 3:f5776537f27f | 91 | { |
donatien | 3:f5776537f27f | 92 | m_netifStatusSphre.wait(0); |
donatien | 3:f5776537f27f | 93 | |
donatien | 3:f5776537f27f | 94 | DBG("Enable MAC interrupts"); |
donatien | 3:f5776537f27f | 95 | NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); |
donatien | 3:f5776537f27f | 96 | NVIC_EnableIRQ(ENET_IRQn); |
donatien | 3:f5776537f27f | 97 | |
donatien | 3:f5776537f27f | 98 | if(m_useDHCP) |
donatien | 3:f5776537f27f | 99 | { |
donatien | 3:f5776537f27f | 100 | dhcp_start(&m_lpcNetif); //The DHCP client will set the interface up once the IP/Netmask/Getway/DNS Servers are recovered |
donatien | 3:f5776537f27f | 101 | } |
donatien | 3:f5776537f27f | 102 | else |
donatien | 3:f5776537f27f | 103 | { |
donatien | 3:f5776537f27f | 104 | netif_set_up(&m_lpcNetif); //Set interface up |
donatien | 3:f5776537f27f | 105 | } |
donatien | 3:f5776537f27f | 106 | |
donatien | 3:f5776537f27f | 107 | m_netifStatusSphre.wait(); |
donatien | 3:f5776537f27f | 108 | |
donatien | 3:f5776537f27f | 109 | DBG("Connected with IP %s", getIPAddress()); |
donatien | 3:f5776537f27f | 110 | |
donatien | 3:f5776537f27f | 111 | return OK; |
donatien | 3:f5776537f27f | 112 | } |
donatien | 3:f5776537f27f | 113 | |
donatien | 3:f5776537f27f | 114 | int EthernetInterface::disconnect() |
donatien | 3:f5776537f27f | 115 | { |
donatien | 3:f5776537f27f | 116 | m_netifStatusSphre.wait(0); |
donatien | 3:f5776537f27f | 117 | |
donatien | 3:f5776537f27f | 118 | if(m_useDHCP) |
donatien | 3:f5776537f27f | 119 | { |
donatien | 3:f5776537f27f | 120 | dhcp_release(&m_lpcNetif); //Release the lease & bring interface down |
donatien | 3:f5776537f27f | 121 | dhcp_stop(&m_lpcNetif); |
donatien | 3:f5776537f27f | 122 | } |
donatien | 3:f5776537f27f | 123 | else |
donatien | 3:f5776537f27f | 124 | { |
donatien | 3:f5776537f27f | 125 | netif_set_down(&m_lpcNetif); |
donatien | 3:f5776537f27f | 126 | } |
donatien | 3:f5776537f27f | 127 | |
donatien | 3:f5776537f27f | 128 | DBG("Disable MAC interrupts"); |
donatien | 3:f5776537f27f | 129 | NVIC_DisableIRQ(ENET_IRQn); |
donatien | 3:f5776537f27f | 130 | |
donatien | 3:f5776537f27f | 131 | m_netifStatusSphre.wait(); |
donatien | 3:f5776537f27f | 132 | |
donatien | 3:f5776537f27f | 133 | return OK; |
donatien | 3:f5776537f27f | 134 | } |
donatien | 3:f5776537f27f | 135 | |
donatien | 3:f5776537f27f | 136 | /*static*/ void EthernetInterface::netifStatusCb(struct netif *netif) |
donatien | 3:f5776537f27f | 137 | { |
donatien | 3:f5776537f27f | 138 | EthernetInterface* pIf = (EthernetInterface*)( ((int32_t)netif) + ((int32_t)s_lpcNetifOff) ); //Bad kludge |
donatien | 3:f5776537f27f | 139 | pIf->setIPAddress(inet_ntoa(netif->ip_addr)); |
donatien | 3:f5776537f27f | 140 | pIf->setConnected(netif_is_up(netif)?true:false); |
donatien | 3:f5776537f27f | 141 | pIf->m_netifStatusSphre.wait(0); //Clear if pending |
donatien | 3:f5776537f27f | 142 | pIf->m_netifStatusSphre.release(); |
donatien | 3:f5776537f27f | 143 | } |
donatien | 3:f5776537f27f | 144 | |
donatien | 3:f5776537f27f | 145 | /*static*/ int32_t EthernetInterface::s_lpcNetifOff = 0; |