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