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