Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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