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