connection

Dependencies:   Socket lwip-eth-ansond-patched lwip-sys lwip-ansond-patched

Dependents:   BLE_HeartRate

Fork of EthernetInterface-ansond-patched by Doug Anson

Committer:
mbed_official
Date:
Wed Oct 09 12:00:18 2013 +0100
Revision:
35:cba86db5ab96
Parent:
33:c21b055c45b8
Synchronized with git revision d3963de05d9ddff98b9da2b2a1fee0d51fd9481e

Who changed what in which revision?

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