Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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