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 /**
samdanbury 6:37b6d0d56190 2 * @file
samdanbury 6:37b6d0d56190 3 * Network Interface Sequential API module
samdanbury 6:37b6d0d56190 4 *
samdanbury 6:37b6d0d56190 5 */
samdanbury 6:37b6d0d56190 6
samdanbury 6:37b6d0d56190 7 /*
samdanbury 6:37b6d0d56190 8 * Redistribution and use in source and binary forms, with or without modification,
samdanbury 6:37b6d0d56190 9 * are permitted provided that the following conditions are met:
samdanbury 6:37b6d0d56190 10 *
samdanbury 6:37b6d0d56190 11 * 1. Redistributions of source code must retain the above copyright notice,
samdanbury 6:37b6d0d56190 12 * this list of conditions and the following disclaimer.
samdanbury 6:37b6d0d56190 13 * 2. Redistributions in binary form must reproduce the above copyright notice,
samdanbury 6:37b6d0d56190 14 * this list of conditions and the following disclaimer in the documentation
samdanbury 6:37b6d0d56190 15 * and/or other materials provided with the distribution.
samdanbury 6:37b6d0d56190 16 * 3. The name of the author may not be used to endorse or promote products
samdanbury 6:37b6d0d56190 17 * derived from this software without specific prior written permission.
samdanbury 6:37b6d0d56190 18 *
samdanbury 6:37b6d0d56190 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
samdanbury 6:37b6d0d56190 20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
samdanbury 6:37b6d0d56190 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
samdanbury 6:37b6d0d56190 22 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
samdanbury 6:37b6d0d56190 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
samdanbury 6:37b6d0d56190 24 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
samdanbury 6:37b6d0d56190 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
samdanbury 6:37b6d0d56190 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
samdanbury 6:37b6d0d56190 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
samdanbury 6:37b6d0d56190 28 * OF SUCH DAMAGE.
samdanbury 6:37b6d0d56190 29 *
samdanbury 6:37b6d0d56190 30 * This file is part of the lwIP TCP/IP stack.
samdanbury 6:37b6d0d56190 31 *
samdanbury 6:37b6d0d56190 32 */
samdanbury 6:37b6d0d56190 33
samdanbury 6:37b6d0d56190 34 #include "lwip/opt.h"
samdanbury 6:37b6d0d56190 35
samdanbury 6:37b6d0d56190 36 #if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */
samdanbury 6:37b6d0d56190 37
samdanbury 6:37b6d0d56190 38 #include "lwip/netifapi.h"
samdanbury 6:37b6d0d56190 39 #include "lwip/tcpip.h"
samdanbury 6:37b6d0d56190 40
samdanbury 6:37b6d0d56190 41 /**
samdanbury 6:37b6d0d56190 42 * Call netif_add() inside the tcpip_thread context.
samdanbury 6:37b6d0d56190 43 */
samdanbury 6:37b6d0d56190 44 void
samdanbury 6:37b6d0d56190 45 do_netifapi_netif_add(struct netifapi_msg_msg *msg)
samdanbury 6:37b6d0d56190 46 {
samdanbury 6:37b6d0d56190 47 if (!netif_add( msg->netif,
samdanbury 6:37b6d0d56190 48 msg->msg.add.ipaddr,
samdanbury 6:37b6d0d56190 49 msg->msg.add.netmask,
samdanbury 6:37b6d0d56190 50 msg->msg.add.gw,
samdanbury 6:37b6d0d56190 51 msg->msg.add.state,
samdanbury 6:37b6d0d56190 52 msg->msg.add.init,
samdanbury 6:37b6d0d56190 53 msg->msg.add.input)) {
samdanbury 6:37b6d0d56190 54 msg->err = ERR_IF;
samdanbury 6:37b6d0d56190 55 } else {
samdanbury 6:37b6d0d56190 56 msg->err = ERR_OK;
samdanbury 6:37b6d0d56190 57 }
samdanbury 6:37b6d0d56190 58 TCPIP_NETIFAPI_ACK(msg);
samdanbury 6:37b6d0d56190 59 }
samdanbury 6:37b6d0d56190 60
samdanbury 6:37b6d0d56190 61 /**
samdanbury 6:37b6d0d56190 62 * Call netif_set_addr() inside the tcpip_thread context.
samdanbury 6:37b6d0d56190 63 */
samdanbury 6:37b6d0d56190 64 void
samdanbury 6:37b6d0d56190 65 do_netifapi_netif_set_addr(struct netifapi_msg_msg *msg)
samdanbury 6:37b6d0d56190 66 {
samdanbury 6:37b6d0d56190 67 netif_set_addr( msg->netif,
samdanbury 6:37b6d0d56190 68 msg->msg.add.ipaddr,
samdanbury 6:37b6d0d56190 69 msg->msg.add.netmask,
samdanbury 6:37b6d0d56190 70 msg->msg.add.gw);
samdanbury 6:37b6d0d56190 71 msg->err = ERR_OK;
samdanbury 6:37b6d0d56190 72 TCPIP_NETIFAPI_ACK(msg);
samdanbury 6:37b6d0d56190 73 }
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75 /**
samdanbury 6:37b6d0d56190 76 * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the
samdanbury 6:37b6d0d56190 77 * tcpip_thread context.
samdanbury 6:37b6d0d56190 78 */
samdanbury 6:37b6d0d56190 79 void
samdanbury 6:37b6d0d56190 80 do_netifapi_netif_common(struct netifapi_msg_msg *msg)
samdanbury 6:37b6d0d56190 81 {
samdanbury 6:37b6d0d56190 82 if (msg->msg.common.errtfunc != NULL) {
samdanbury 6:37b6d0d56190 83 msg->err = msg->msg.common.errtfunc(msg->netif);
samdanbury 6:37b6d0d56190 84 } else {
samdanbury 6:37b6d0d56190 85 msg->err = ERR_OK;
samdanbury 6:37b6d0d56190 86 msg->msg.common.voidfunc(msg->netif);
samdanbury 6:37b6d0d56190 87 }
samdanbury 6:37b6d0d56190 88 TCPIP_NETIFAPI_ACK(msg);
samdanbury 6:37b6d0d56190 89 }
samdanbury 6:37b6d0d56190 90
samdanbury 6:37b6d0d56190 91 /**
samdanbury 6:37b6d0d56190 92 * Call netif_add() in a thread-safe way by running that function inside the
samdanbury 6:37b6d0d56190 93 * tcpip_thread context.
samdanbury 6:37b6d0d56190 94 *
samdanbury 6:37b6d0d56190 95 * @note for params @see netif_add()
samdanbury 6:37b6d0d56190 96 */
samdanbury 6:37b6d0d56190 97 err_t
samdanbury 6:37b6d0d56190 98 netifapi_netif_add(struct netif *netif,
samdanbury 6:37b6d0d56190 99 ip_addr_t *ipaddr,
samdanbury 6:37b6d0d56190 100 ip_addr_t *netmask,
samdanbury 6:37b6d0d56190 101 ip_addr_t *gw,
samdanbury 6:37b6d0d56190 102 void *state,
samdanbury 6:37b6d0d56190 103 netif_init_fn init,
samdanbury 6:37b6d0d56190 104 netif_input_fn input)
samdanbury 6:37b6d0d56190 105 {
samdanbury 6:37b6d0d56190 106 struct netifapi_msg msg;
samdanbury 6:37b6d0d56190 107 msg.function = do_netifapi_netif_add;
samdanbury 6:37b6d0d56190 108 msg.msg.netif = netif;
samdanbury 6:37b6d0d56190 109 msg.msg.msg.add.ipaddr = ipaddr;
samdanbury 6:37b6d0d56190 110 msg.msg.msg.add.netmask = netmask;
samdanbury 6:37b6d0d56190 111 msg.msg.msg.add.gw = gw;
samdanbury 6:37b6d0d56190 112 msg.msg.msg.add.state = state;
samdanbury 6:37b6d0d56190 113 msg.msg.msg.add.init = init;
samdanbury 6:37b6d0d56190 114 msg.msg.msg.add.input = input;
samdanbury 6:37b6d0d56190 115 TCPIP_NETIFAPI(&msg);
samdanbury 6:37b6d0d56190 116 return msg.msg.err;
samdanbury 6:37b6d0d56190 117 }
samdanbury 6:37b6d0d56190 118
samdanbury 6:37b6d0d56190 119 /**
samdanbury 6:37b6d0d56190 120 * Call netif_set_addr() in a thread-safe way by running that function inside the
samdanbury 6:37b6d0d56190 121 * tcpip_thread context.
samdanbury 6:37b6d0d56190 122 *
samdanbury 6:37b6d0d56190 123 * @note for params @see netif_set_addr()
samdanbury 6:37b6d0d56190 124 */
samdanbury 6:37b6d0d56190 125 err_t
samdanbury 6:37b6d0d56190 126 netifapi_netif_set_addr(struct netif *netif,
samdanbury 6:37b6d0d56190 127 ip_addr_t *ipaddr,
samdanbury 6:37b6d0d56190 128 ip_addr_t *netmask,
samdanbury 6:37b6d0d56190 129 ip_addr_t *gw)
samdanbury 6:37b6d0d56190 130 {
samdanbury 6:37b6d0d56190 131 struct netifapi_msg msg;
samdanbury 6:37b6d0d56190 132 msg.function = do_netifapi_netif_set_addr;
samdanbury 6:37b6d0d56190 133 msg.msg.netif = netif;
samdanbury 6:37b6d0d56190 134 msg.msg.msg.add.ipaddr = ipaddr;
samdanbury 6:37b6d0d56190 135 msg.msg.msg.add.netmask = netmask;
samdanbury 6:37b6d0d56190 136 msg.msg.msg.add.gw = gw;
samdanbury 6:37b6d0d56190 137 TCPIP_NETIFAPI(&msg);
samdanbury 6:37b6d0d56190 138 return msg.msg.err;
samdanbury 6:37b6d0d56190 139 }
samdanbury 6:37b6d0d56190 140
samdanbury 6:37b6d0d56190 141 /**
samdanbury 6:37b6d0d56190 142 * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe
samdanbury 6:37b6d0d56190 143 * way by running that function inside the tcpip_thread context.
samdanbury 6:37b6d0d56190 144 *
samdanbury 6:37b6d0d56190 145 * @note use only for functions where there is only "netif" parameter.
samdanbury 6:37b6d0d56190 146 */
samdanbury 6:37b6d0d56190 147 err_t
samdanbury 6:37b6d0d56190 148 netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
samdanbury 6:37b6d0d56190 149 netifapi_errt_fn errtfunc)
samdanbury 6:37b6d0d56190 150 {
samdanbury 6:37b6d0d56190 151 struct netifapi_msg msg;
samdanbury 6:37b6d0d56190 152 msg.function = do_netifapi_netif_common;
samdanbury 6:37b6d0d56190 153 msg.msg.netif = netif;
samdanbury 6:37b6d0d56190 154 msg.msg.msg.common.voidfunc = voidfunc;
samdanbury 6:37b6d0d56190 155 msg.msg.msg.common.errtfunc = errtfunc;
samdanbury 6:37b6d0d56190 156 TCPIP_NETIFAPI(&msg);
samdanbury 6:37b6d0d56190 157 return msg.msg.err;
samdanbury 6:37b6d0d56190 158 }
samdanbury 6:37b6d0d56190 159
samdanbury 6:37b6d0d56190 160 #endif /* LWIP_NETIF_API */