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