Example program with HTTPServer and sensor data streaming over TCPSockets, using Donatien Garnier's Net APIs and services code on top of LWIP. Files StreamServer.h and .cpp encapsulate streaming over TCPSockets. Broadcast is done by sendToAll(), and all incoming data is echoed back to the client. Echo code can be replaced with some remote control of the streaming interface. See main() that shows how to periodically send some data to all subscribed clients. To subscribe, a client should open a socket at <mbed_ip> port 123. I used few lines in TCL code to set up a quick sink for the data. HTTP files are served on port 80 concurrently to the streaming.

Dependencies:   mbed

Committer:
iva2k
Date:
Sat Jun 12 06:01:50 2010 +0000
Revision:
0:e614f7875b60

        

Who changed what in which revision?

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