mbed IP library over Ethernet K64F static IP FIXED
Dependencies: Socket lwip-eth lwip-sys lwip
Fork of EthernetInterface by
EthernetInterface.cpp@55:ab0511be5528, 2016-10-24 (annotated)
- Committer:
- pinfred
- Date:
- Mon Oct 24 16:25:18 2016 +0000
- Revision:
- 55:ab0511be5528
- Parent:
- 37:926eb6517318
fix static IP issue
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 | #include "EthernetInterface.h" |
donatien | 3:f5776537f27f | 20 | |
donatien | 3:f5776537f27f | 21 | #include "lwip/inet.h" |
donatien | 3:f5776537f27f | 22 | #include "lwip/netif.h" |
donatien | 3:f5776537f27f | 23 | #include "netif/etharp.h" |
donatien | 3:f5776537f27f | 24 | #include "lwip/dhcp.h" |
pinfred | 55:ab0511be5528 | 25 | #include "lwip/dns.h" |
mbed_official | 37:926eb6517318 | 26 | #include "eth_arch.h" |
donatien | 3:f5776537f27f | 27 | #include "lwip/tcpip.h" |
donatien | 3:f5776537f27f | 28 | |
donatien | 3:f5776537f27f | 29 | #include "mbed.h" |
donatien | 3:f5776537f27f | 30 | |
emilmont | 14:cec293071eed | 31 | /* TCP/IP and Network Interface Initialisation */ |
mbed_official | 37:926eb6517318 | 32 | static struct netif netif; |
donatien | 3:f5776537f27f | 33 | |
emilmont | 27:2124eae946f3 | 34 | static char mac_addr[19]; |
emilmont | 27:2124eae946f3 | 35 | static char ip_addr[17] = "\0"; |
mbed_official | 35:cba86db5ab96 | 36 | static char gateway[17] = "\0"; |
pinfred | 55:ab0511be5528 | 37 | static char dns_addr[17] = "\0"; |
mbed_official | 35:cba86db5ab96 | 38 | static char networkmask[17] = "\0"; |
emilmont | 26:dd9794ce1d64 | 39 | static bool use_dhcp = false; |
pinfred | 55:ab0511be5528 | 40 | static bool set_dns = false; |
donatien | 3:f5776537f27f | 41 | |
emilmont | 26:dd9794ce1d64 | 42 | static Semaphore tcpip_inited(0); |
emilmont | 26:dd9794ce1d64 | 43 | static Semaphore netif_linked(0); |
emilmont | 26:dd9794ce1d64 | 44 | static Semaphore netif_up(0); |
donatien | 3:f5776537f27f | 45 | |
emilmont | 14:cec293071eed | 46 | static void tcpip_init_done(void *arg) { |
emilmont | 14:cec293071eed | 47 | tcpip_inited.release(); |
emilmont | 14:cec293071eed | 48 | } |
emilmont | 26:dd9794ce1d64 | 49 | |
emilmont | 26:dd9794ce1d64 | 50 | static void netif_link_callback(struct netif *netif) { |
emilmont | 26:dd9794ce1d64 | 51 | if (netif_is_link_up(netif)) { |
emilmont | 26:dd9794ce1d64 | 52 | netif_linked.release(); |
emilmont | 26:dd9794ce1d64 | 53 | } |
emilmont | 26:dd9794ce1d64 | 54 | } |
emilmont | 26:dd9794ce1d64 | 55 | |
emilmont | 14:cec293071eed | 56 | static void netif_status_callback(struct netif *netif) { |
emilmont | 26:dd9794ce1d64 | 57 | if (netif_is_up(netif)) { |
emilmont | 26:dd9794ce1d64 | 58 | strcpy(ip_addr, inet_ntoa(netif->ip_addr)); |
mbed_official | 35:cba86db5ab96 | 59 | strcpy(gateway, inet_ntoa(netif->gw)); |
mbed_official | 35:cba86db5ab96 | 60 | strcpy(networkmask, inet_ntoa(netif->netmask)); |
emilmont | 26:dd9794ce1d64 | 61 | netif_up.release(); |
emilmont | 26:dd9794ce1d64 | 62 | } |
emilmont | 14:cec293071eed | 63 | } |
emilmont | 26:dd9794ce1d64 | 64 | |
emilmont | 14:cec293071eed | 65 | static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { |
emilmont | 14:cec293071eed | 66 | tcpip_init(tcpip_init_done, NULL); |
emilmont | 14:cec293071eed | 67 | tcpip_inited.wait(); |
emilmont | 14:cec293071eed | 68 | |
mbed_official | 37:926eb6517318 | 69 | memset((void*) &netif, 0, sizeof(netif)); |
mbed_official | 37:926eb6517318 | 70 | netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input); |
mbed_official | 37:926eb6517318 | 71 | netif_set_default(&netif); |
mbed_official | 37:926eb6517318 | 72 | netif_set_link_callback (&netif, netif_link_callback); |
mbed_official | 37:926eb6517318 | 73 | netif_set_status_callback(&netif, netif_status_callback); |
donatien | 3:f5776537f27f | 74 | } |
donatien | 3:f5776537f27f | 75 | |
emilmont | 27:2124eae946f3 | 76 | static void set_mac_address(void) { |
bogdanm | 33:c21b055c45b8 | 77 | #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) |
bogdanm | 33:c21b055c45b8 | 78 | snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2, |
bogdanm | 33:c21b055c45b8 | 79 | MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5); |
bogdanm | 33:c21b055c45b8 | 80 | #else |
emilmont | 27:2124eae946f3 | 81 | char mac[6]; |
emilmont | 27:2124eae946f3 | 82 | mbed_mac_address(mac); |
emilmont | 27:2124eae946f3 | 83 | snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
bogdanm | 33:c21b055c45b8 | 84 | #endif |
emilmont | 27:2124eae946f3 | 85 | } |
emilmont | 27:2124eae946f3 | 86 | |
emilmont | 14:cec293071eed | 87 | int EthernetInterface::init() { |
emilmont | 14:cec293071eed | 88 | use_dhcp = true; |
emilmont | 27:2124eae946f3 | 89 | set_mac_address(); |
emilmont | 14:cec293071eed | 90 | init_netif(NULL, NULL, NULL); |
emilmont | 15:fd9597f1b81b | 91 | return 0; |
donatien | 3:f5776537f27f | 92 | } |
donatien | 3:f5776537f27f | 93 | |
pinfred | 55:ab0511be5528 | 94 | int EthernetInterface::init(const char* ip, const char* mask, const char* gateway, const char * dns) { |
emilmont | 14:cec293071eed | 95 | use_dhcp = false; |
emilmont | 27:2124eae946f3 | 96 | |
emilmont | 27:2124eae946f3 | 97 | set_mac_address(); |
emilmont | 26:dd9794ce1d64 | 98 | strcpy(ip_addr, ip); |
emilmont | 26:dd9794ce1d64 | 99 | |
pinfred | 55:ab0511be5528 | 100 | if (dns != NULL) { |
pinfred | 55:ab0511be5528 | 101 | set_dns = true; |
pinfred | 55:ab0511be5528 | 102 | strcpy(dns_addr, dns); |
pinfred | 55:ab0511be5528 | 103 | } |
pinfred | 55:ab0511be5528 | 104 | |
emilmont | 14:cec293071eed | 105 | ip_addr_t ip_n, mask_n, gateway_n; |
emilmont | 14:cec293071eed | 106 | inet_aton(ip, &ip_n); |
emilmont | 14:cec293071eed | 107 | inet_aton(mask, &mask_n); |
emilmont | 14:cec293071eed | 108 | inet_aton(gateway, &gateway_n); |
emilmont | 14:cec293071eed | 109 | init_netif(&ip_n, &mask_n, &gateway_n); |
emilmont | 26:dd9794ce1d64 | 110 | |
emilmont | 15:fd9597f1b81b | 111 | return 0; |
donatien | 3:f5776537f27f | 112 | } |
donatien | 3:f5776537f27f | 113 | |
emilmont | 21:8cd2de462559 | 114 | int EthernetInterface::connect(unsigned int timeout_ms) { |
mbed_official | 37:926eb6517318 | 115 | eth_arch_enable_interrupts(); |
mbed_official | 37:926eb6517318 | 116 | |
emilmont | 26:dd9794ce1d64 | 117 | int inited; |
emilmont | 14:cec293071eed | 118 | if (use_dhcp) { |
mbed_official | 37:926eb6517318 | 119 | dhcp_start(&netif); |
emilmont | 26:dd9794ce1d64 | 120 | |
emilmont | 27:2124eae946f3 | 121 | // Wait for an IP Address |
emilmont | 26:dd9794ce1d64 | 122 | // -1: error, 0: timeout |
emilmont | 26:dd9794ce1d64 | 123 | inited = netif_up.wait(timeout_ms); |
emilmont | 14:cec293071eed | 124 | } else { |
mbed_official | 37:926eb6517318 | 125 | netif_set_up(&netif); |
emilmont | 26:dd9794ce1d64 | 126 | |
emilmont | 26:dd9794ce1d64 | 127 | // Wait for the link up |
pinfred | 55:ab0511be5528 | 128 | Timer timeout; |
pinfred | 55:ab0511be5528 | 129 | timeout.start(); |
pinfred | 55:ab0511be5528 | 130 | while(timeout.read_ms() < timeout_ms) { |
pinfred | 55:ab0511be5528 | 131 | if (netif.flags & NETIF_FLAG_LINK_UP) { |
pinfred | 55:ab0511be5528 | 132 | inited = 1; |
pinfred | 55:ab0511be5528 | 133 | break; |
pinfred | 55:ab0511be5528 | 134 | } |
pinfred | 55:ab0511be5528 | 135 | } |
pinfred | 55:ab0511be5528 | 136 | if (set_dns) { |
pinfred | 55:ab0511be5528 | 137 | ip_addr_t dns_n; |
pinfred | 55:ab0511be5528 | 138 | inet_aton(dns_addr, &dns_n); |
pinfred | 55:ab0511be5528 | 139 | dns_setserver(0, &dns_n); |
pinfred | 55:ab0511be5528 | 140 | } |
emilmont | 14:cec293071eed | 141 | } |
emilmont | 14:cec293071eed | 142 | |
emilmont | 21:8cd2de462559 | 143 | return (inited > 0) ? (0) : (-1); |
donatien | 3:f5776537f27f | 144 | } |
donatien | 3:f5776537f27f | 145 | |
emilmont | 14:cec293071eed | 146 | int EthernetInterface::disconnect() { |
emilmont | 14:cec293071eed | 147 | if (use_dhcp) { |
mbed_official | 37:926eb6517318 | 148 | dhcp_release(&netif); |
mbed_official | 37:926eb6517318 | 149 | dhcp_stop(&netif); |
emilmont | 14:cec293071eed | 150 | } else { |
mbed_official | 37:926eb6517318 | 151 | netif_set_down(&netif); |
emilmont | 14:cec293071eed | 152 | } |
emilmont | 14:cec293071eed | 153 | |
mbed_official | 37:926eb6517318 | 154 | eth_arch_disable_interrupts(); |
emilmont | 14:cec293071eed | 155 | |
emilmont | 15:fd9597f1b81b | 156 | return 0; |
donatien | 3:f5776537f27f | 157 | } |
donatien | 3:f5776537f27f | 158 | |
emilmont | 27:2124eae946f3 | 159 | char* EthernetInterface::getMACAddress() { |
emilmont | 27:2124eae946f3 | 160 | return mac_addr; |
emilmont | 27:2124eae946f3 | 161 | } |
emilmont | 27:2124eae946f3 | 162 | |
emilmont | 14:cec293071eed | 163 | char* EthernetInterface::getIPAddress() { |
emilmont | 26:dd9794ce1d64 | 164 | return ip_addr; |
emilmont | 14:cec293071eed | 165 | } |
mbed_official | 35:cba86db5ab96 | 166 | |
mbed_official | 35:cba86db5ab96 | 167 | char* EthernetInterface::getGateway() { |
mbed_official | 35:cba86db5ab96 | 168 | return gateway; |
mbed_official | 35:cba86db5ab96 | 169 | } |
mbed_official | 35:cba86db5ab96 | 170 | |
mbed_official | 35:cba86db5ab96 | 171 | char* EthernetInterface::getNetworkMask() { |
mbed_official | 35:cba86db5ab96 | 172 | return networkmask; |
mbed_official | 35:cba86db5ab96 | 173 | } |
mbed_official | 35:cba86db5ab96 | 174 | |
mbed_official | 35:cba86db5ab96 | 175 |