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