j,j
Dependencies: Socket lwip-eth lwip-sys lwip
Fork of EthernetInterface by
EthernetInterface.cpp@7:73a6916d991e, 2012-07-02 (annotated)
- Committer:
- emilmont
- Date:
- Mon Jul 02 10:51:59 2012 +0000
- Revision:
- 7:73a6916d991e
- Parent:
- 4:9a52c802be61
- Child:
- 10:8d1c7a87c17b
Do not force debug on the EthernetInterface users, in particular do not change the default settings of their serial port.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
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 | LwIPInterface::init(); //Init LwIP, NOT including PPP |
donatien | 3:f5776537f27f | 51 | |
donatien | 3:f5776537f27f | 52 | memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif)); |
donatien | 3:f5776537f27f | 53 | netif_add(&m_lpcNetif, NULL, NULL, NULL, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/); |
donatien | 3:f5776537f27f | 54 | netif_set_default(&m_lpcNetif); |
donatien | 3:f5776537f27f | 55 | |
donatien | 3:f5776537f27f | 56 | netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb); |
donatien | 3:f5776537f27f | 57 | |
donatien | 3:f5776537f27f | 58 | return OK; |
donatien | 3:f5776537f27f | 59 | } |
donatien | 3:f5776537f27f | 60 | |
donatien | 3:f5776537f27f | 61 | int EthernetInterface::init(const char* ip, const char* mask, const char* gateway, |
donatien | 3:f5776537f27f | 62 | const char* dns1, const char* dns2) //No DHCP |
donatien | 3:f5776537f27f | 63 | { |
donatien | 3:f5776537f27f | 64 | ip_addr_t ip_n, mask_n, gateway_n, dns1_n, dns2_n; |
donatien | 3:f5776537f27f | 65 | |
donatien | 3:f5776537f27f | 66 | m_useDHCP = false; |
donatien | 3:f5776537f27f | 67 | |
donatien | 3:f5776537f27f | 68 | LwIPInterface::init(); //Init LwIP |
donatien | 3:f5776537f27f | 69 | |
donatien | 3:f5776537f27f | 70 | inet_aton(ip, &ip_n); |
donatien | 3:f5776537f27f | 71 | inet_aton(mask, &mask_n); |
donatien | 3:f5776537f27f | 72 | inet_aton(gateway, &gateway_n); |
donatien | 3:f5776537f27f | 73 | inet_aton(dns1, &dns1_n); |
donatien | 3:f5776537f27f | 74 | inet_aton(dns2, &dns2_n); |
donatien | 3:f5776537f27f | 75 | |
donatien | 3:f5776537f27f | 76 | memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif)); |
donatien | 3:f5776537f27f | 77 | netif_add(&m_lpcNetif, &ip_n, &mask_n, &gateway_n, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/); |
donatien | 3:f5776537f27f | 78 | netif_set_default(&m_lpcNetif); |
donatien | 3:f5776537f27f | 79 | |
donatien | 3:f5776537f27f | 80 | netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb); |
donatien | 3:f5776537f27f | 81 | |
donatien | 3:f5776537f27f | 82 | return OK; |
donatien | 3:f5776537f27f | 83 | } |
donatien | 3:f5776537f27f | 84 | |
donatien | 3:f5776537f27f | 85 | int EthernetInterface::connect() |
donatien | 3:f5776537f27f | 86 | { |
donatien | 3:f5776537f27f | 87 | m_netifStatusSphre.wait(0); |
donatien | 3:f5776537f27f | 88 | |
donatien | 3:f5776537f27f | 89 | NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); |
donatien | 3:f5776537f27f | 90 | NVIC_EnableIRQ(ENET_IRQn); |
donatien | 3:f5776537f27f | 91 | |
donatien | 3:f5776537f27f | 92 | if(m_useDHCP) |
donatien | 3:f5776537f27f | 93 | { |
donatien | 3:f5776537f27f | 94 | dhcp_start(&m_lpcNetif); //The DHCP client will set the interface up once the IP/Netmask/Getway/DNS Servers are recovered |
donatien | 3:f5776537f27f | 95 | } |
donatien | 3:f5776537f27f | 96 | else |
donatien | 3:f5776537f27f | 97 | { |
donatien | 3:f5776537f27f | 98 | netif_set_up(&m_lpcNetif); //Set interface up |
donatien | 3:f5776537f27f | 99 | } |
donatien | 3:f5776537f27f | 100 | |
donatien | 3:f5776537f27f | 101 | m_netifStatusSphre.wait(); |
donatien | 3:f5776537f27f | 102 | |
donatien | 3:f5776537f27f | 103 | return OK; |
donatien | 3:f5776537f27f | 104 | } |
donatien | 3:f5776537f27f | 105 | |
donatien | 3:f5776537f27f | 106 | int EthernetInterface::disconnect() |
donatien | 3:f5776537f27f | 107 | { |
donatien | 3:f5776537f27f | 108 | m_netifStatusSphre.wait(0); |
donatien | 3:f5776537f27f | 109 | |
donatien | 3:f5776537f27f | 110 | if(m_useDHCP) |
donatien | 3:f5776537f27f | 111 | { |
donatien | 3:f5776537f27f | 112 | dhcp_release(&m_lpcNetif); //Release the lease & bring interface down |
donatien | 3:f5776537f27f | 113 | dhcp_stop(&m_lpcNetif); |
donatien | 3:f5776537f27f | 114 | } |
donatien | 3:f5776537f27f | 115 | else |
donatien | 3:f5776537f27f | 116 | { |
donatien | 3:f5776537f27f | 117 | netif_set_down(&m_lpcNetif); |
donatien | 3:f5776537f27f | 118 | } |
donatien | 3:f5776537f27f | 119 | |
donatien | 3:f5776537f27f | 120 | NVIC_DisableIRQ(ENET_IRQn); |
donatien | 3:f5776537f27f | 121 | |
donatien | 3:f5776537f27f | 122 | m_netifStatusSphre.wait(); |
donatien | 3:f5776537f27f | 123 | |
donatien | 3:f5776537f27f | 124 | return OK; |
donatien | 3:f5776537f27f | 125 | } |
donatien | 3:f5776537f27f | 126 | |
donatien | 3:f5776537f27f | 127 | /*static*/ void EthernetInterface::netifStatusCb(struct netif *netif) |
donatien | 3:f5776537f27f | 128 | { |
donatien | 3:f5776537f27f | 129 | EthernetInterface* pIf = (EthernetInterface*)( ((int32_t)netif) + ((int32_t)s_lpcNetifOff) ); //Bad kludge |
donatien | 3:f5776537f27f | 130 | pIf->setIPAddress(inet_ntoa(netif->ip_addr)); |
donatien | 3:f5776537f27f | 131 | pIf->setConnected(netif_is_up(netif)?true:false); |
donatien | 3:f5776537f27f | 132 | pIf->m_netifStatusSphre.wait(0); //Clear if pending |
donatien | 3:f5776537f27f | 133 | pIf->m_netifStatusSphre.release(); |
donatien | 3:f5776537f27f | 134 | } |
donatien | 3:f5776537f27f | 135 | |
donatien | 3:f5776537f27f | 136 | /*static*/ int32_t EthernetInterface::s_lpcNetifOff = 0; |