Mark Radbourne / Mbed 2 deprecated FXOS8700CQ_To_Azure_IoT

Dependencies:   azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetInterface.cpp Source File

EthernetInterface.cpp

00001 /* EthernetInterface.cpp */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 #include "EthernetInterface.h"
00020 
00021 #include "lwip/inet.h"
00022 #include "lwip/netif.h"
00023 #include "netif/etharp.h"
00024 #include "lwip/dhcp.h"
00025 #include "eth_arch.h"
00026 #include "lwip/tcpip.h"
00027 
00028 #include "mbed.h"
00029 
00030 /* TCP/IP and Network Interface Initialisation */
00031 static struct netif netif;
00032 
00033 static char mac_addr[19];
00034 static char ip_addr[17] = "\0";
00035 static char gateway[17] = "\0";
00036 static char networkmask[17] = "\0";
00037 static bool use_dhcp = false;
00038 
00039 static Semaphore tcpip_inited(0);
00040 static Semaphore netif_linked(0);
00041 static Semaphore netif_up(0);
00042 
00043 static void tcpip_init_done(void *arg) {
00044     tcpip_inited.release();
00045 }
00046 
00047 static void netif_link_callback(struct netif *netif) {
00048     if (netif_is_link_up(netif)) {
00049         netif_linked.release();
00050         netif_up.release();
00051         printf("netif_link_callback: link is up\r\n");
00052     }
00053     else {
00054         printf("netif_link_callback: link is down\r\n");
00055     }
00056 }
00057 
00058 static void netif_status_callback(struct netif *netif) {
00059     if (netif_is_up(netif)) {
00060         strcpy(ip_addr, inet_ntoa(netif->ip_addr));
00061         strcpy(gateway, inet_ntoa(netif->gw));
00062         strcpy(networkmask, inet_ntoa(netif->netmask));
00063         netif_up.release();
00064         printf("netif_status_callback: link is up\r\n");
00065     }
00066     else {
00067         printf("netif_status_callback: link is down\r\n");
00068     }
00069 }
00070 
00071 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
00072     static bool tcpip_initialized = false;
00073     
00074     if (!tcpip_initialized) {
00075         printf("tcpip_init\r\n");
00076         tcpip_init(tcpip_init_done, NULL);
00077         printf("waiting...\r\n");
00078         tcpip_inited.wait();
00079         tcpip_initialized = true;
00080     }
00081     
00082     memset((void*) &netif, 0, sizeof(netif));
00083     printf("netif_add\r\n");
00084     
00085     if (NULL == netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input))
00086         printf("netif_add failed\r\n");
00087     
00088     printf("netif_set_default\r\n");
00089     netif_set_default(&netif);
00090     
00091     printf("netif_set_link_callback\r\n");
00092     netif_set_link_callback  (&netif, netif_link_callback);
00093     printf("netif_set_status_callback\r\n");
00094     netif_set_status_callback(&netif, netif_status_callback);
00095 }
00096 
00097 static void set_mac_address(void) {
00098 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
00099     snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
00100              MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
00101 #else
00102     char mac[6];
00103     mbed_mac_address(mac);
00104     snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00105 #endif
00106 }
00107 
00108 int EthernetInterface::init() {
00109     use_dhcp = true;
00110     set_mac_address();
00111     init_netif(NULL, NULL, NULL);
00112     return 0;
00113 }
00114 
00115 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
00116     use_dhcp = false;
00117     
00118     set_mac_address();
00119     strcpy(ip_addr, ip);
00120     
00121     ip_addr_t ip_n, mask_n, gateway_n;
00122     inet_aton(ip, &ip_n);
00123     inet_aton(mask, &mask_n);
00124     inet_aton(gateway, &gateway_n);
00125     init_netif(&ip_n, &mask_n, &gateway_n);
00126     
00127     return 0;
00128 }
00129 
00130 int EthernetInterface::connect(unsigned int timeout_ms) {
00131     eth_arch_enable_interrupts();
00132 
00133     int inited;
00134     if (use_dhcp) {
00135         printf("dhcp_start\r\n");
00136         dhcp_start(&netif);
00137         
00138         // Wait for an IP Address
00139         // -1: error, 0: timeout
00140         printf("netif_up.wait\r\n");
00141         inited = netif_up.wait(timeout_ms);
00142     } else {
00143         netif_set_up(&netif);
00144         
00145         // Wait for the link up
00146         inited = netif_linked.wait(timeout_ms);
00147     }
00148     
00149     return (inited > 0) ? (0) : (-1);
00150 }
00151 
00152 int EthernetInterface::disconnect() {
00153     if (use_dhcp) {
00154         dhcp_release(&netif);
00155         dhcp_stop(&netif);
00156     } else {
00157         netif_set_down(&netif);
00158     }
00159     
00160     eth_arch_disable_interrupts();
00161     
00162     return 0;
00163 }
00164 
00165 char* EthernetInterface::getMACAddress() {
00166     return mac_addr;
00167 }
00168 
00169 char* EthernetInterface::getIPAddress() {
00170     return ip_addr;
00171 }
00172 
00173 char* EthernetInterface::getGateway() {
00174     return gateway;
00175 }
00176 
00177 char* EthernetInterface::getNetworkMask() {
00178     return networkmask;
00179 }
00180 
00181