Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.
Dependencies: mbed Socket lwip-eth lwip-sys lwip
EthernetInterface/EthernetInterface.cpp@0:e7ca326e76ee, 2013-04-04 (annotated)
- Committer:
- mkersh3
- Date:
- Thu Apr 04 05:26:09 2013 +0000
- Revision:
- 0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mkersh3 | 0:e7ca326e76ee | 1 | /* EthernetInterface.cpp */ |
mkersh3 | 0:e7ca326e76ee | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
mkersh3 | 0:e7ca326e76ee | 3 | * |
mkersh3 | 0:e7ca326e76ee | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mkersh3 | 0:e7ca326e76ee | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
mkersh3 | 0:e7ca326e76ee | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mkersh3 | 0:e7ca326e76ee | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mkersh3 | 0:e7ca326e76ee | 8 | * furnished to do so, subject to the following conditions: |
mkersh3 | 0:e7ca326e76ee | 9 | * |
mkersh3 | 0:e7ca326e76ee | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
mkersh3 | 0:e7ca326e76ee | 11 | * substantial portions of the Software. |
mkersh3 | 0:e7ca326e76ee | 12 | * |
mkersh3 | 0:e7ca326e76ee | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mkersh3 | 0:e7ca326e76ee | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mkersh3 | 0:e7ca326e76ee | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mkersh3 | 0:e7ca326e76ee | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mkersh3 | 0:e7ca326e76ee | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mkersh3 | 0:e7ca326e76ee | 18 | */ |
mkersh3 | 0:e7ca326e76ee | 19 | #include "EthernetInterface.h" |
mkersh3 | 0:e7ca326e76ee | 20 | |
mkersh3 | 0:e7ca326e76ee | 21 | #include "lwip/inet.h" |
mkersh3 | 0:e7ca326e76ee | 22 | #include "lwip/netif.h" |
mkersh3 | 0:e7ca326e76ee | 23 | #include "netif/etharp.h" |
mkersh3 | 0:e7ca326e76ee | 24 | #include "lwip/dhcp.h" |
mkersh3 | 0:e7ca326e76ee | 25 | #include "arch/lpc17_emac.h" |
mkersh3 | 0:e7ca326e76ee | 26 | #include "lpc_phy.h" |
mkersh3 | 0:e7ca326e76ee | 27 | #include "lwip/tcpip.h" |
mkersh3 | 0:e7ca326e76ee | 28 | |
mkersh3 | 0:e7ca326e76ee | 29 | #include "mbed.h" |
mkersh3 | 0:e7ca326e76ee | 30 | |
mkersh3 | 0:e7ca326e76ee | 31 | /* TCP/IP and Network Interface Initialisation */ |
mkersh3 | 0:e7ca326e76ee | 32 | static struct netif lpcNetif; |
mkersh3 | 0:e7ca326e76ee | 33 | |
mkersh3 | 0:e7ca326e76ee | 34 | static Semaphore tcpip_inited(0); |
mkersh3 | 0:e7ca326e76ee | 35 | static Semaphore netif_inited(0); |
mkersh3 | 0:e7ca326e76ee | 36 | |
mkersh3 | 0:e7ca326e76ee | 37 | static char ip_addr[16]; |
mkersh3 | 0:e7ca326e76ee | 38 | static bool connected; |
mkersh3 | 0:e7ca326e76ee | 39 | static bool use_dhcp = false; |
mkersh3 | 0:e7ca326e76ee | 40 | |
mkersh3 | 0:e7ca326e76ee | 41 | static void tcpip_init_done(void *arg) { |
mkersh3 | 0:e7ca326e76ee | 42 | tcpip_inited.release(); |
mkersh3 | 0:e7ca326e76ee | 43 | } |
mkersh3 | 0:e7ca326e76ee | 44 | static void netif_status_callback(struct netif *netif) { |
mkersh3 | 0:e7ca326e76ee | 45 | strcpy(ip_addr, inet_ntoa(netif->ip_addr)); |
mkersh3 | 0:e7ca326e76ee | 46 | connected = netif_is_up(netif) ? true : false; |
mkersh3 | 0:e7ca326e76ee | 47 | netif_inited.release(); |
mkersh3 | 0:e7ca326e76ee | 48 | } |
mkersh3 | 0:e7ca326e76ee | 49 | static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { |
mkersh3 | 0:e7ca326e76ee | 50 | tcpip_init(tcpip_init_done, NULL); |
mkersh3 | 0:e7ca326e76ee | 51 | tcpip_inited.wait(); |
mkersh3 | 0:e7ca326e76ee | 52 | |
mkersh3 | 0:e7ca326e76ee | 53 | memset((void*) &lpcNetif, 0, sizeof(lpcNetif)); |
mkersh3 | 0:e7ca326e76ee | 54 | netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input); |
mkersh3 | 0:e7ca326e76ee | 55 | netif_set_default(&lpcNetif); |
mkersh3 | 0:e7ca326e76ee | 56 | netif_set_status_callback(&lpcNetif, netif_status_callback); |
mkersh3 | 0:e7ca326e76ee | 57 | } |
mkersh3 | 0:e7ca326e76ee | 58 | |
mkersh3 | 0:e7ca326e76ee | 59 | int EthernetInterface::init() { |
mkersh3 | 0:e7ca326e76ee | 60 | use_dhcp = true; |
mkersh3 | 0:e7ca326e76ee | 61 | init_netif(NULL, NULL, NULL); |
mkersh3 | 0:e7ca326e76ee | 62 | return 0; |
mkersh3 | 0:e7ca326e76ee | 63 | } |
mkersh3 | 0:e7ca326e76ee | 64 | |
mkersh3 | 0:e7ca326e76ee | 65 | int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { |
mkersh3 | 0:e7ca326e76ee | 66 | use_dhcp = false; |
mkersh3 | 0:e7ca326e76ee | 67 | ip_addr_t ip_n, mask_n, gateway_n; |
mkersh3 | 0:e7ca326e76ee | 68 | inet_aton(ip, &ip_n); |
mkersh3 | 0:e7ca326e76ee | 69 | inet_aton(mask, &mask_n); |
mkersh3 | 0:e7ca326e76ee | 70 | inet_aton(gateway, &gateway_n); |
mkersh3 | 0:e7ca326e76ee | 71 | init_netif(&ip_n, &mask_n, &gateway_n); |
mkersh3 | 0:e7ca326e76ee | 72 | return 0; |
mkersh3 | 0:e7ca326e76ee | 73 | } |
mkersh3 | 0:e7ca326e76ee | 74 | |
mkersh3 | 0:e7ca326e76ee | 75 | int EthernetInterface::connect(unsigned int timeout_ms) { |
mkersh3 | 0:e7ca326e76ee | 76 | NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); |
mkersh3 | 0:e7ca326e76ee | 77 | NVIC_EnableIRQ(ENET_IRQn); |
mkersh3 | 0:e7ca326e76ee | 78 | |
mkersh3 | 0:e7ca326e76ee | 79 | if (use_dhcp) { |
mkersh3 | 0:e7ca326e76ee | 80 | dhcp_start(&lpcNetif); |
mkersh3 | 0:e7ca326e76ee | 81 | } else { |
mkersh3 | 0:e7ca326e76ee | 82 | netif_set_up(&lpcNetif); |
mkersh3 | 0:e7ca326e76ee | 83 | } |
mkersh3 | 0:e7ca326e76ee | 84 | |
mkersh3 | 0:e7ca326e76ee | 85 | // -1: error, 0: timeout |
mkersh3 | 0:e7ca326e76ee | 86 | int inited = netif_inited.wait(timeout_ms); |
mkersh3 | 0:e7ca326e76ee | 87 | return (inited > 0) ? (0) : (-1); |
mkersh3 | 0:e7ca326e76ee | 88 | } |
mkersh3 | 0:e7ca326e76ee | 89 | |
mkersh3 | 0:e7ca326e76ee | 90 | int EthernetInterface::disconnect() { |
mkersh3 | 0:e7ca326e76ee | 91 | if (use_dhcp) { |
mkersh3 | 0:e7ca326e76ee | 92 | dhcp_release(&lpcNetif); |
mkersh3 | 0:e7ca326e76ee | 93 | dhcp_stop(&lpcNetif); |
mkersh3 | 0:e7ca326e76ee | 94 | } else { |
mkersh3 | 0:e7ca326e76ee | 95 | netif_set_down(&lpcNetif); |
mkersh3 | 0:e7ca326e76ee | 96 | } |
mkersh3 | 0:e7ca326e76ee | 97 | |
mkersh3 | 0:e7ca326e76ee | 98 | NVIC_DisableIRQ(ENET_IRQn); |
mkersh3 | 0:e7ca326e76ee | 99 | |
mkersh3 | 0:e7ca326e76ee | 100 | return 0; |
mkersh3 | 0:e7ca326e76ee | 101 | } |
mkersh3 | 0:e7ca326e76ee | 102 | |
mkersh3 | 0:e7ca326e76ee | 103 | char* EthernetInterface::getIPAddress() { |
mkersh3 | 0:e7ca326e76ee | 104 | return (connected) ? (ip_addr) : (NULL); |
mkersh3 | 0:e7ca326e76ee | 105 | } |