Connect MBED through Ethernet port

Dependencies:   Socket lwip-eth lwip-sys lwip

Committer:
MohamadNazrin
Date:
Wed Feb 21 07:26:52 2018 +0000
Revision:
0:00d2ed14e5a0
display IP in LCD

Who changed what in which revision?

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