![](/media/cache/profiles/debfdc81854eac26ec993b55a659c6e1.jpg.50x50_q85.jpg)
mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
EthernetInterface/EthernetInterface.cpp@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* EthernetInterface.cpp */ |
mbedAustin | 11:cada08fc8a70 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
mbedAustin | 11:cada08fc8a70 | 3 | * |
mbedAustin | 11:cada08fc8a70 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbedAustin | 11:cada08fc8a70 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
mbedAustin | 11:cada08fc8a70 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mbedAustin | 11:cada08fc8a70 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mbedAustin | 11:cada08fc8a70 | 8 | * furnished to do so, subject to the following conditions: |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
mbedAustin | 11:cada08fc8a70 | 11 | * substantial portions of the Software. |
mbedAustin | 11:cada08fc8a70 | 12 | * |
mbedAustin | 11:cada08fc8a70 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbedAustin | 11:cada08fc8a70 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbedAustin | 11:cada08fc8a70 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbedAustin | 11:cada08fc8a70 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbedAustin | 11:cada08fc8a70 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbedAustin | 11:cada08fc8a70 | 18 | */ |
mbedAustin | 11:cada08fc8a70 | 19 | #include "EthernetInterface.h" |
mbedAustin | 11:cada08fc8a70 | 20 | |
mbedAustin | 11:cada08fc8a70 | 21 | #include "lwip/inet.h" |
mbedAustin | 11:cada08fc8a70 | 22 | #include "lwip/netif.h" |
mbedAustin | 11:cada08fc8a70 | 23 | #include "netif/etharp.h" |
mbedAustin | 11:cada08fc8a70 | 24 | #include "lwip/dhcp.h" |
mbedAustin | 11:cada08fc8a70 | 25 | #include "eth_arch.h" |
mbedAustin | 11:cada08fc8a70 | 26 | #include "lwip/tcpip.h" |
mbedAustin | 11:cada08fc8a70 | 27 | |
mbedAustin | 11:cada08fc8a70 | 28 | #include "mbed.h" |
mbedAustin | 11:cada08fc8a70 | 29 | |
mbedAustin | 11:cada08fc8a70 | 30 | /* TCP/IP and Network Interface Initialisation */ |
mbedAustin | 11:cada08fc8a70 | 31 | static struct netif netif; |
mbedAustin | 11:cada08fc8a70 | 32 | |
mbedAustin | 11:cada08fc8a70 | 33 | static char mac_addr[19]; |
mbedAustin | 11:cada08fc8a70 | 34 | static char ip_addr[17] = "\0"; |
mbedAustin | 11:cada08fc8a70 | 35 | static char gateway[17] = "\0"; |
mbedAustin | 11:cada08fc8a70 | 36 | static char networkmask[17] = "\0"; |
mbedAustin | 11:cada08fc8a70 | 37 | static bool use_dhcp = false; |
mbedAustin | 11:cada08fc8a70 | 38 | |
mbedAustin | 11:cada08fc8a70 | 39 | static Semaphore tcpip_inited(0); |
mbedAustin | 11:cada08fc8a70 | 40 | static Semaphore netif_linked(0); |
mbedAustin | 11:cada08fc8a70 | 41 | static Semaphore netif_up(0); |
mbedAustin | 11:cada08fc8a70 | 42 | |
mbedAustin | 11:cada08fc8a70 | 43 | static void tcpip_init_done(void *arg) { |
mbedAustin | 11:cada08fc8a70 | 44 | tcpip_inited.release(); |
mbedAustin | 11:cada08fc8a70 | 45 | } |
mbedAustin | 11:cada08fc8a70 | 46 | |
mbedAustin | 11:cada08fc8a70 | 47 | static void netif_link_callback(struct netif *netif) { |
mbedAustin | 11:cada08fc8a70 | 48 | if (netif_is_link_up(netif)) { |
mbedAustin | 11:cada08fc8a70 | 49 | netif_linked.release(); |
mbedAustin | 11:cada08fc8a70 | 50 | } |
mbedAustin | 11:cada08fc8a70 | 51 | } |
mbedAustin | 11:cada08fc8a70 | 52 | |
mbedAustin | 11:cada08fc8a70 | 53 | static void netif_status_callback(struct netif *netif) { |
mbedAustin | 11:cada08fc8a70 | 54 | if (netif_is_up(netif)) { |
mbedAustin | 11:cada08fc8a70 | 55 | strcpy(ip_addr, inet_ntoa(netif->ip_addr)); |
mbedAustin | 11:cada08fc8a70 | 56 | strcpy(gateway, inet_ntoa(netif->gw)); |
mbedAustin | 11:cada08fc8a70 | 57 | strcpy(networkmask, inet_ntoa(netif->netmask)); |
mbedAustin | 11:cada08fc8a70 | 58 | netif_up.release(); |
mbedAustin | 11:cada08fc8a70 | 59 | } |
mbedAustin | 11:cada08fc8a70 | 60 | } |
mbedAustin | 11:cada08fc8a70 | 61 | |
mbedAustin | 11:cada08fc8a70 | 62 | static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { |
mbedAustin | 11:cada08fc8a70 | 63 | tcpip_init(tcpip_init_done, NULL); |
mbedAustin | 11:cada08fc8a70 | 64 | tcpip_inited.wait(); |
mbedAustin | 11:cada08fc8a70 | 65 | |
mbedAustin | 11:cada08fc8a70 | 66 | memset((void*) &netif, 0, sizeof(netif)); |
mbedAustin | 11:cada08fc8a70 | 67 | netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input); |
mbedAustin | 11:cada08fc8a70 | 68 | netif_set_default(&netif); |
mbedAustin | 11:cada08fc8a70 | 69 | |
mbedAustin | 11:cada08fc8a70 | 70 | netif_set_link_callback (&netif, netif_link_callback); |
mbedAustin | 11:cada08fc8a70 | 71 | netif_set_status_callback(&netif, netif_status_callback); |
mbedAustin | 11:cada08fc8a70 | 72 | } |
mbedAustin | 11:cada08fc8a70 | 73 | |
mbedAustin | 11:cada08fc8a70 | 74 | static void set_mac_address(void) { |
mbedAustin | 11:cada08fc8a70 | 75 | #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE) |
mbedAustin | 11:cada08fc8a70 | 76 | snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2, |
mbedAustin | 11:cada08fc8a70 | 77 | MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5); |
mbedAustin | 11:cada08fc8a70 | 78 | #else |
mbedAustin | 11:cada08fc8a70 | 79 | char mac[6]; |
mbedAustin | 11:cada08fc8a70 | 80 | mbed_mac_address(mac); |
mbedAustin | 11:cada08fc8a70 | 81 | snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
mbedAustin | 11:cada08fc8a70 | 82 | #endif |
mbedAustin | 11:cada08fc8a70 | 83 | } |
mbedAustin | 11:cada08fc8a70 | 84 | |
mbedAustin | 11:cada08fc8a70 | 85 | int EthernetInterface::init() { |
mbedAustin | 11:cada08fc8a70 | 86 | use_dhcp = true; |
mbedAustin | 11:cada08fc8a70 | 87 | set_mac_address(); |
mbedAustin | 11:cada08fc8a70 | 88 | init_netif(NULL, NULL, NULL); |
mbedAustin | 11:cada08fc8a70 | 89 | return 0; |
mbedAustin | 11:cada08fc8a70 | 90 | } |
mbedAustin | 11:cada08fc8a70 | 91 | |
mbedAustin | 11:cada08fc8a70 | 92 | int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { |
mbedAustin | 11:cada08fc8a70 | 93 | use_dhcp = false; |
mbedAustin | 11:cada08fc8a70 | 94 | |
mbedAustin | 11:cada08fc8a70 | 95 | set_mac_address(); |
mbedAustin | 11:cada08fc8a70 | 96 | strcpy(ip_addr, ip); |
mbedAustin | 11:cada08fc8a70 | 97 | |
mbedAustin | 11:cada08fc8a70 | 98 | ip_addr_t ip_n, mask_n, gateway_n; |
mbedAustin | 11:cada08fc8a70 | 99 | inet_aton(ip, &ip_n); |
mbedAustin | 11:cada08fc8a70 | 100 | inet_aton(mask, &mask_n); |
mbedAustin | 11:cada08fc8a70 | 101 | inet_aton(gateway, &gateway_n); |
mbedAustin | 11:cada08fc8a70 | 102 | init_netif(&ip_n, &mask_n, &gateway_n); |
mbedAustin | 11:cada08fc8a70 | 103 | |
mbedAustin | 11:cada08fc8a70 | 104 | return 0; |
mbedAustin | 11:cada08fc8a70 | 105 | } |
mbedAustin | 11:cada08fc8a70 | 106 | |
mbedAustin | 11:cada08fc8a70 | 107 | int EthernetInterface::connect(unsigned int timeout_ms) { |
mbedAustin | 11:cada08fc8a70 | 108 | eth_arch_enable_interrupts(); |
mbedAustin | 11:cada08fc8a70 | 109 | |
mbedAustin | 11:cada08fc8a70 | 110 | int inited; |
mbedAustin | 11:cada08fc8a70 | 111 | if (use_dhcp) { |
mbedAustin | 11:cada08fc8a70 | 112 | dhcp_start(&netif); |
mbedAustin | 11:cada08fc8a70 | 113 | |
mbedAustin | 11:cada08fc8a70 | 114 | // Wait for an IP Address |
mbedAustin | 11:cada08fc8a70 | 115 | // -1: error, 0: timeout |
mbedAustin | 11:cada08fc8a70 | 116 | inited = netif_up.wait(timeout_ms); |
mbedAustin | 11:cada08fc8a70 | 117 | } else { |
mbedAustin | 11:cada08fc8a70 | 118 | netif_set_up(&netif); |
mbedAustin | 11:cada08fc8a70 | 119 | |
mbedAustin | 11:cada08fc8a70 | 120 | // Wait for the link up |
mbedAustin | 11:cada08fc8a70 | 121 | inited = netif_linked.wait(timeout_ms); |
mbedAustin | 11:cada08fc8a70 | 122 | } |
mbedAustin | 11:cada08fc8a70 | 123 | |
mbedAustin | 11:cada08fc8a70 | 124 | return (inited > 0) ? (0) : (-1); |
mbedAustin | 11:cada08fc8a70 | 125 | } |
mbedAustin | 11:cada08fc8a70 | 126 | |
mbedAustin | 11:cada08fc8a70 | 127 | int EthernetInterface::disconnect() { |
mbedAustin | 11:cada08fc8a70 | 128 | if (use_dhcp) { |
mbedAustin | 11:cada08fc8a70 | 129 | dhcp_release(&netif); |
mbedAustin | 11:cada08fc8a70 | 130 | dhcp_stop(&netif); |
mbedAustin | 11:cada08fc8a70 | 131 | } else { |
mbedAustin | 11:cada08fc8a70 | 132 | netif_set_down(&netif); |
mbedAustin | 11:cada08fc8a70 | 133 | } |
mbedAustin | 11:cada08fc8a70 | 134 | |
mbedAustin | 11:cada08fc8a70 | 135 | eth_arch_disable_interrupts(); |
mbedAustin | 11:cada08fc8a70 | 136 | |
mbedAustin | 11:cada08fc8a70 | 137 | return 0; |
mbedAustin | 11:cada08fc8a70 | 138 | } |
mbedAustin | 11:cada08fc8a70 | 139 | |
mbedAustin | 11:cada08fc8a70 | 140 | char* EthernetInterface::getMACAddress() { |
mbedAustin | 11:cada08fc8a70 | 141 | return mac_addr; |
mbedAustin | 11:cada08fc8a70 | 142 | } |
mbedAustin | 11:cada08fc8a70 | 143 | |
mbedAustin | 11:cada08fc8a70 | 144 | char* EthernetInterface::getIPAddress() { |
mbedAustin | 11:cada08fc8a70 | 145 | return ip_addr; |
mbedAustin | 11:cada08fc8a70 | 146 | } |
mbedAustin | 11:cada08fc8a70 | 147 | |
mbedAustin | 11:cada08fc8a70 | 148 | char* EthernetInterface::getGateway() { |
mbedAustin | 11:cada08fc8a70 | 149 | return gateway; |
mbedAustin | 11:cada08fc8a70 | 150 | } |
mbedAustin | 11:cada08fc8a70 | 151 | |
mbedAustin | 11:cada08fc8a70 | 152 | char* EthernetInterface::getNetworkMask() { |
mbedAustin | 11:cada08fc8a70 | 153 | return networkmask; |
mbedAustin | 11:cada08fc8a70 | 154 | } |
mbedAustin | 11:cada08fc8a70 | 155 | |
mbedAustin | 11:cada08fc8a70 | 156 |