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 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
samdanbury 6:37b6d0d56190 3 * All rights reserved.
samdanbury 6:37b6d0d56190 4 *
samdanbury 6:37b6d0d56190 5 * Redistribution and use in source and binary forms, with or without modification,
samdanbury 6:37b6d0d56190 6 * are permitted provided that the following conditions are met:
samdanbury 6:37b6d0d56190 7 *
samdanbury 6:37b6d0d56190 8 * 1. Redistributions of source code must retain the above copyright notice,
samdanbury 6:37b6d0d56190 9 * this list of conditions and the following disclaimer.
samdanbury 6:37b6d0d56190 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
samdanbury 6:37b6d0d56190 11 * this list of conditions and the following disclaimer in the documentation
samdanbury 6:37b6d0d56190 12 * and/or other materials provided with the distribution.
samdanbury 6:37b6d0d56190 13 * 3. The name of the author may not be used to endorse or promote products
samdanbury 6:37b6d0d56190 14 * derived from this software without specific prior written permission.
samdanbury 6:37b6d0d56190 15 *
samdanbury 6:37b6d0d56190 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
samdanbury 6:37b6d0d56190 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
samdanbury 6:37b6d0d56190 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
samdanbury 6:37b6d0d56190 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
samdanbury 6:37b6d0d56190 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
samdanbury 6:37b6d0d56190 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
samdanbury 6:37b6d0d56190 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
samdanbury 6:37b6d0d56190 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
samdanbury 6:37b6d0d56190 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
samdanbury 6:37b6d0d56190 25 * OF SUCH DAMAGE.
samdanbury 6:37b6d0d56190 26 *
samdanbury 6:37b6d0d56190 27 * This file is part of the lwIP TCP/IP stack.
samdanbury 6:37b6d0d56190 28 *
samdanbury 6:37b6d0d56190 29 * Author: Adam Dunkels <adam@sics.se>
samdanbury 6:37b6d0d56190 30 *
samdanbury 6:37b6d0d56190 31 */
samdanbury 6:37b6d0d56190 32 #ifndef __LWIP_IP_H__
samdanbury 6:37b6d0d56190 33 #define __LWIP_IP_H__
samdanbury 6:37b6d0d56190 34
samdanbury 6:37b6d0d56190 35 #include "lwip/opt.h"
samdanbury 6:37b6d0d56190 36
samdanbury 6:37b6d0d56190 37 #include "lwip/def.h"
samdanbury 6:37b6d0d56190 38 #include "lwip/pbuf.h"
samdanbury 6:37b6d0d56190 39 #include "lwip/ip_addr.h"
samdanbury 6:37b6d0d56190 40 #include "lwip/err.h"
samdanbury 6:37b6d0d56190 41 #include "lwip/netif.h"
samdanbury 6:37b6d0d56190 42
samdanbury 6:37b6d0d56190 43 #ifdef __cplusplus
samdanbury 6:37b6d0d56190 44 extern "C" {
samdanbury 6:37b6d0d56190 45 #endif
samdanbury 6:37b6d0d56190 46
samdanbury 6:37b6d0d56190 47 /** Currently, the function ip_output_if_opt() is only used with IGMP */
samdanbury 6:37b6d0d56190 48 #define IP_OPTIONS_SEND LWIP_IGMP
samdanbury 6:37b6d0d56190 49
samdanbury 6:37b6d0d56190 50 #define IP_HLEN 20
samdanbury 6:37b6d0d56190 51
samdanbury 6:37b6d0d56190 52 #define IP_PROTO_ICMP 1
samdanbury 6:37b6d0d56190 53 #define IP_PROTO_IGMP 2
samdanbury 6:37b6d0d56190 54 #define IP_PROTO_UDP 17
samdanbury 6:37b6d0d56190 55 #define IP_PROTO_UDPLITE 136
samdanbury 6:37b6d0d56190 56 #define IP_PROTO_TCP 6
samdanbury 6:37b6d0d56190 57
samdanbury 6:37b6d0d56190 58 /* This is passed as the destination address to ip_output_if (not
samdanbury 6:37b6d0d56190 59 to ip_output), meaning that an IP header already is constructed
samdanbury 6:37b6d0d56190 60 in the pbuf. This is used when TCP retransmits. */
samdanbury 6:37b6d0d56190 61 #ifdef IP_HDRINCL
samdanbury 6:37b6d0d56190 62 #undef IP_HDRINCL
samdanbury 6:37b6d0d56190 63 #endif /* IP_HDRINCL */
samdanbury 6:37b6d0d56190 64 #define IP_HDRINCL NULL
samdanbury 6:37b6d0d56190 65
samdanbury 6:37b6d0d56190 66 #if LWIP_NETIF_HWADDRHINT
samdanbury 6:37b6d0d56190 67 #define IP_PCB_ADDRHINT ;u8_t addr_hint
samdanbury 6:37b6d0d56190 68 #else
samdanbury 6:37b6d0d56190 69 #define IP_PCB_ADDRHINT
samdanbury 6:37b6d0d56190 70 #endif /* LWIP_NETIF_HWADDRHINT */
samdanbury 6:37b6d0d56190 71
samdanbury 6:37b6d0d56190 72 /* This is the common part of all PCB types. It needs to be at the
samdanbury 6:37b6d0d56190 73 beginning of a PCB type definition. It is located here so that
samdanbury 6:37b6d0d56190 74 changes to this common part are made in one location instead of
samdanbury 6:37b6d0d56190 75 having to change all PCB structs. */
samdanbury 6:37b6d0d56190 76 #define IP_PCB \
samdanbury 6:37b6d0d56190 77 /* ip addresses in network byte order */ \
samdanbury 6:37b6d0d56190 78 ip_addr_t local_ip; \
samdanbury 6:37b6d0d56190 79 ip_addr_t remote_ip; \
samdanbury 6:37b6d0d56190 80 /* Socket options */ \
samdanbury 6:37b6d0d56190 81 u8_t so_options; \
samdanbury 6:37b6d0d56190 82 /* Type Of Service */ \
samdanbury 6:37b6d0d56190 83 u8_t tos; \
samdanbury 6:37b6d0d56190 84 /* Time To Live */ \
samdanbury 6:37b6d0d56190 85 u8_t ttl \
samdanbury 6:37b6d0d56190 86 /* link layer address resolution hint */ \
samdanbury 6:37b6d0d56190 87 IP_PCB_ADDRHINT
samdanbury 6:37b6d0d56190 88
samdanbury 6:37b6d0d56190 89 struct ip_pcb {
samdanbury 6:37b6d0d56190 90 /* Common members of all PCB types */
samdanbury 6:37b6d0d56190 91 IP_PCB;
samdanbury 6:37b6d0d56190 92 };
samdanbury 6:37b6d0d56190 93
samdanbury 6:37b6d0d56190 94 /*
samdanbury 6:37b6d0d56190 95 * Option flags per-socket. These are the same like SO_XXX.
samdanbury 6:37b6d0d56190 96 */
samdanbury 6:37b6d0d56190 97 /*#define SOF_DEBUG (u8_t)0x01U Unimplemented: turn on debugging info recording */
samdanbury 6:37b6d0d56190 98 #define SOF_ACCEPTCONN (u8_t)0x02U /* socket has had listen() */
samdanbury 6:37b6d0d56190 99 #define SOF_REUSEADDR (u8_t)0x04U /* allow local address reuse */
samdanbury 6:37b6d0d56190 100 #define SOF_KEEPALIVE (u8_t)0x08U /* keep connections alive */
samdanbury 6:37b6d0d56190 101 /*#define SOF_DONTROUTE (u8_t)0x10U Unimplemented: just use interface addresses */
samdanbury 6:37b6d0d56190 102 #define SOF_BROADCAST (u8_t)0x20U /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
samdanbury 6:37b6d0d56190 103 /*#define SOF_USELOOPBACK (u8_t)0x40U Unimplemented: bypass hardware when possible */
samdanbury 6:37b6d0d56190 104 #define SOF_LINGER (u8_t)0x80U /* linger on close if data present */
samdanbury 6:37b6d0d56190 105 /*#define SOF_OOBINLINE (u16_t)0x0100U Unimplemented: leave received OOB data in line */
samdanbury 6:37b6d0d56190 106 /*#define SOF_REUSEPORT (u16_t)0x0200U Unimplemented: allow local address & port reuse */
samdanbury 6:37b6d0d56190 107
samdanbury 6:37b6d0d56190 108 /* These flags are inherited (e.g. from a listen-pcb to a connection-pcb): */
samdanbury 6:37b6d0d56190 109 #define SOF_INHERITED (SOF_REUSEADDR|SOF_KEEPALIVE|SOF_LINGER/*|SOF_DEBUG|SOF_DONTROUTE|SOF_OOBINLINE*/)
samdanbury 6:37b6d0d56190 110
samdanbury 6:37b6d0d56190 111
samdanbury 6:37b6d0d56190 112 #ifdef PACK_STRUCT_USE_INCLUDES
samdanbury 6:37b6d0d56190 113 # include "arch/bpstruct.h"
samdanbury 6:37b6d0d56190 114 #endif
samdanbury 6:37b6d0d56190 115 PACK_STRUCT_BEGIN
samdanbury 6:37b6d0d56190 116 struct ip_hdr {
samdanbury 6:37b6d0d56190 117 /* version / header length / type of service */
samdanbury 6:37b6d0d56190 118 PACK_STRUCT_FIELD(u16_t _v_hl_tos);
samdanbury 6:37b6d0d56190 119 /* total length */
samdanbury 6:37b6d0d56190 120 PACK_STRUCT_FIELD(u16_t _len);
samdanbury 6:37b6d0d56190 121 /* identification */
samdanbury 6:37b6d0d56190 122 PACK_STRUCT_FIELD(u16_t _id);
samdanbury 6:37b6d0d56190 123 /* fragment offset field */
samdanbury 6:37b6d0d56190 124 PACK_STRUCT_FIELD(u16_t _offset);
samdanbury 6:37b6d0d56190 125 #define IP_RF 0x8000U /* reserved fragment flag */
samdanbury 6:37b6d0d56190 126 #define IP_DF 0x4000U /* dont fragment flag */
samdanbury 6:37b6d0d56190 127 #define IP_MF 0x2000U /* more fragments flag */
samdanbury 6:37b6d0d56190 128 #define IP_OFFMASK 0x1fffU /* mask for fragmenting bits */
samdanbury 6:37b6d0d56190 129 /* time to live */
samdanbury 6:37b6d0d56190 130 PACK_STRUCT_FIELD(u8_t _ttl);
samdanbury 6:37b6d0d56190 131 /* protocol*/
samdanbury 6:37b6d0d56190 132 PACK_STRUCT_FIELD(u8_t _proto);
samdanbury 6:37b6d0d56190 133 /* checksum */
samdanbury 6:37b6d0d56190 134 PACK_STRUCT_FIELD(u16_t _chksum);
samdanbury 6:37b6d0d56190 135 /* source and destination IP addresses */
samdanbury 6:37b6d0d56190 136 PACK_STRUCT_FIELD(ip_addr_p_t src);
samdanbury 6:37b6d0d56190 137 PACK_STRUCT_FIELD(ip_addr_p_t dest);
samdanbury 6:37b6d0d56190 138 } PACK_STRUCT_STRUCT;
samdanbury 6:37b6d0d56190 139 PACK_STRUCT_END
samdanbury 6:37b6d0d56190 140 #ifdef PACK_STRUCT_USE_INCLUDES
samdanbury 6:37b6d0d56190 141 # include "arch/epstruct.h"
samdanbury 6:37b6d0d56190 142 #endif
samdanbury 6:37b6d0d56190 143
samdanbury 6:37b6d0d56190 144 #define IPH_V(hdr) (ntohs((hdr)->_v_hl_tos) >> 12)
samdanbury 6:37b6d0d56190 145 #define IPH_HL(hdr) ((ntohs((hdr)->_v_hl_tos) >> 8) & 0x0f)
samdanbury 6:37b6d0d56190 146 #define IPH_TOS(hdr) (ntohs((hdr)->_v_hl_tos) & 0xff)
samdanbury 6:37b6d0d56190 147 #define IPH_LEN(hdr) ((hdr)->_len)
samdanbury 6:37b6d0d56190 148 #define IPH_ID(hdr) ((hdr)->_id)
samdanbury 6:37b6d0d56190 149 #define IPH_OFFSET(hdr) ((hdr)->_offset)
samdanbury 6:37b6d0d56190 150 #define IPH_TTL(hdr) ((hdr)->_ttl)
samdanbury 6:37b6d0d56190 151 #define IPH_PROTO(hdr) ((hdr)->_proto)
samdanbury 6:37b6d0d56190 152 #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
samdanbury 6:37b6d0d56190 153
samdanbury 6:37b6d0d56190 154 #define IPH_VHLTOS_SET(hdr, v, hl, tos) (hdr)->_v_hl_tos = (htons(((v) << 12) | ((hl) << 8) | (tos)))
samdanbury 6:37b6d0d56190 155 #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
samdanbury 6:37b6d0d56190 156 #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
samdanbury 6:37b6d0d56190 157 #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
samdanbury 6:37b6d0d56190 158 #define IPH_TTL_SET(hdr, ttl) (hdr)->_ttl = (u8_t)(ttl)
samdanbury 6:37b6d0d56190 159 #define IPH_PROTO_SET(hdr, proto) (hdr)->_proto = (u8_t)(proto)
samdanbury 6:37b6d0d56190 160 #define IPH_CHKSUM_SET(hdr, chksum) (hdr)->_chksum = (chksum)
samdanbury 6:37b6d0d56190 161
samdanbury 6:37b6d0d56190 162 /** The interface that provided the packet for the current callback invocation. */
samdanbury 6:37b6d0d56190 163 extern struct netif *current_netif;
samdanbury 6:37b6d0d56190 164 /** Header of the input packet currently being processed. */
samdanbury 6:37b6d0d56190 165 extern const struct ip_hdr *current_header;
samdanbury 6:37b6d0d56190 166 /** Source IP address of current_header */
samdanbury 6:37b6d0d56190 167 extern ip_addr_t current_iphdr_src;
samdanbury 6:37b6d0d56190 168 /** Destination IP address of current_header */
samdanbury 6:37b6d0d56190 169 extern ip_addr_t current_iphdr_dest;
samdanbury 6:37b6d0d56190 170
samdanbury 6:37b6d0d56190 171 #define ip_init() /* Compatibility define, not init needed. */
samdanbury 6:37b6d0d56190 172 struct netif *ip_route(ip_addr_t *dest);
samdanbury 6:37b6d0d56190 173 err_t ip_input(struct pbuf *p, struct netif *inp);
samdanbury 6:37b6d0d56190 174 err_t ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
samdanbury 6:37b6d0d56190 175 u8_t ttl, u8_t tos, u8_t proto);
samdanbury 6:37b6d0d56190 176 err_t ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
samdanbury 6:37b6d0d56190 177 u8_t ttl, u8_t tos, u8_t proto,
samdanbury 6:37b6d0d56190 178 struct netif *netif);
samdanbury 6:37b6d0d56190 179 #if LWIP_NETIF_HWADDRHINT
samdanbury 6:37b6d0d56190 180 err_t ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
samdanbury 6:37b6d0d56190 181 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint);
samdanbury 6:37b6d0d56190 182 #endif /* LWIP_NETIF_HWADDRHINT */
samdanbury 6:37b6d0d56190 183 #if IP_OPTIONS_SEND
samdanbury 6:37b6d0d56190 184 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
samdanbury 6:37b6d0d56190 185 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
samdanbury 6:37b6d0d56190 186 u16_t optlen);
samdanbury 6:37b6d0d56190 187 #endif /* IP_OPTIONS_SEND */
samdanbury 6:37b6d0d56190 188 /** Get the interface that received the current packet.
samdanbury 6:37b6d0d56190 189 * This function must only be called from a receive callback (udp_recv,
samdanbury 6:37b6d0d56190 190 * raw_recv, tcp_accept). It will return NULL otherwise. */
samdanbury 6:37b6d0d56190 191 #define ip_current_netif() (current_netif)
samdanbury 6:37b6d0d56190 192 /** Get the IP header of the current packet.
samdanbury 6:37b6d0d56190 193 * This function must only be called from a receive callback (udp_recv,
samdanbury 6:37b6d0d56190 194 * raw_recv, tcp_accept). It will return NULL otherwise. */
samdanbury 6:37b6d0d56190 195 #define ip_current_header() (current_header)
samdanbury 6:37b6d0d56190 196 /** Source IP address of current_header */
samdanbury 6:37b6d0d56190 197 #define ip_current_src_addr() (&current_iphdr_src)
samdanbury 6:37b6d0d56190 198 /** Destination IP address of current_header */
samdanbury 6:37b6d0d56190 199 #define ip_current_dest_addr() (&current_iphdr_dest)
samdanbury 6:37b6d0d56190 200
samdanbury 6:37b6d0d56190 201 #if IP_DEBUG
samdanbury 6:37b6d0d56190 202 void ip_debug_print(struct pbuf *p);
samdanbury 6:37b6d0d56190 203 #else
samdanbury 6:37b6d0d56190 204 #define ip_debug_print(p)
samdanbury 6:37b6d0d56190 205 #endif /* IP_DEBUG */
samdanbury 6:37b6d0d56190 206
samdanbury 6:37b6d0d56190 207 #ifdef __cplusplus
samdanbury 6:37b6d0d56190 208 }
samdanbury 6:37b6d0d56190 209 #endif
samdanbury 6:37b6d0d56190 210
samdanbury 6:37b6d0d56190 211 #endif /* __LWIP_IP_H__ */
samdanbury 6:37b6d0d56190 212
samdanbury 6:37b6d0d56190 213