TwitterExample with newer library (2012Aug)

Dependencies:   EthernetNetIf HTTPClient mbed

Committer:
nxpfan
Date:
Wed Aug 29 03:50:19 2012 +0000
Revision:
0:075157567b0c
simple twitter example with newer library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxpfan 0:075157567b0c 1 /*
nxpfan 0:075157567b0c 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
nxpfan 0:075157567b0c 3 * All rights reserved.
nxpfan 0:075157567b0c 4 *
nxpfan 0:075157567b0c 5 * Redistribution and use in source and binary forms, with or without modification,
nxpfan 0:075157567b0c 6 * are permitted provided that the following conditions are met:
nxpfan 0:075157567b0c 7 *
nxpfan 0:075157567b0c 8 * 1. Redistributions of source code must retain the above copyright notice,
nxpfan 0:075157567b0c 9 * this list of conditions and the following disclaimer.
nxpfan 0:075157567b0c 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
nxpfan 0:075157567b0c 11 * this list of conditions and the following disclaimer in the documentation
nxpfan 0:075157567b0c 12 * and/or other materials provided with the distribution.
nxpfan 0:075157567b0c 13 * 3. The name of the author may not be used to endorse or promote products
nxpfan 0:075157567b0c 14 * derived from this software without specific prior written permission.
nxpfan 0:075157567b0c 15 *
nxpfan 0:075157567b0c 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
nxpfan 0:075157567b0c 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
nxpfan 0:075157567b0c 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
nxpfan 0:075157567b0c 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
nxpfan 0:075157567b0c 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
nxpfan 0:075157567b0c 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
nxpfan 0:075157567b0c 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
nxpfan 0:075157567b0c 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
nxpfan 0:075157567b0c 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
nxpfan 0:075157567b0c 25 * OF SUCH DAMAGE.
nxpfan 0:075157567b0c 26 *
nxpfan 0:075157567b0c 27 * This file is part of the lwIP TCP/IP stack.
nxpfan 0:075157567b0c 28 *
nxpfan 0:075157567b0c 29 * Author: Adam Dunkels <adam@sics.se>
nxpfan 0:075157567b0c 30 *
nxpfan 0:075157567b0c 31 */
nxpfan 0:075157567b0c 32 #ifndef __LWIP_API_H__
nxpfan 0:075157567b0c 33 #define __LWIP_API_H__
nxpfan 0:075157567b0c 34
nxpfan 0:075157567b0c 35 #include "lwip/opt.h"
nxpfan 0:075157567b0c 36
nxpfan 0:075157567b0c 37 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
nxpfan 0:075157567b0c 38
nxpfan 0:075157567b0c 39 #include <stddef.h> /* for size_t */
nxpfan 0:075157567b0c 40
nxpfan 0:075157567b0c 41 #include "lwip/netbuf.h"
nxpfan 0:075157567b0c 42 #include "lwip/sys.h"
nxpfan 0:075157567b0c 43 #include "lwip/ip_addr.h"
nxpfan 0:075157567b0c 44 #include "lwip/err.h"
nxpfan 0:075157567b0c 45
nxpfan 0:075157567b0c 46 #ifdef __cplusplus
nxpfan 0:075157567b0c 47 extern "C" {
nxpfan 0:075157567b0c 48 #endif
nxpfan 0:075157567b0c 49
nxpfan 0:075157567b0c 50 /* Throughout this file, IP addresses and port numbers are expected to be in
nxpfan 0:075157567b0c 51 * the same byte order as in the corresponding pcb.
nxpfan 0:075157567b0c 52 */
nxpfan 0:075157567b0c 53
nxpfan 0:075157567b0c 54 /* Flags for netconn_write (u8_t) */
nxpfan 0:075157567b0c 55 #define NETCONN_NOFLAG 0x00
nxpfan 0:075157567b0c 56 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
nxpfan 0:075157567b0c 57 #define NETCONN_COPY 0x01
nxpfan 0:075157567b0c 58 #define NETCONN_MORE 0x02
nxpfan 0:075157567b0c 59 #define NETCONN_DONTBLOCK 0x04
nxpfan 0:075157567b0c 60
nxpfan 0:075157567b0c 61 /* Flags for struct netconn.flags (u8_t) */
nxpfan 0:075157567b0c 62 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
nxpfan 0:075157567b0c 63 this temporarily stores whether to wake up the original application task
nxpfan 0:075157567b0c 64 if data couldn't be sent in the first try. */
nxpfan 0:075157567b0c 65 #define NETCONN_FLAG_WRITE_DELAYED 0x01
nxpfan 0:075157567b0c 66 /** Should this netconn avoid blocking? */
nxpfan 0:075157567b0c 67 #define NETCONN_FLAG_NON_BLOCKING 0x02
nxpfan 0:075157567b0c 68 /** Was the last connect action a non-blocking one? */
nxpfan 0:075157567b0c 69 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
nxpfan 0:075157567b0c 70 /** If this is set, a TCP netconn must call netconn_recved() to update
nxpfan 0:075157567b0c 71 the TCP receive window (done automatically if not set). */
nxpfan 0:075157567b0c 72 #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
nxpfan 0:075157567b0c 73 /** If a nonblocking write has been rejected before, poll_tcp needs to
nxpfan 0:075157567b0c 74 check if the netconn is writable again */
nxpfan 0:075157567b0c 75 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
nxpfan 0:075157567b0c 76
nxpfan 0:075157567b0c 77
nxpfan 0:075157567b0c 78 /* Helpers to process several netconn_types by the same code */
nxpfan 0:075157567b0c 79 #define NETCONNTYPE_GROUP(t) (t&0xF0)
nxpfan 0:075157567b0c 80 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
nxpfan 0:075157567b0c 81
nxpfan 0:075157567b0c 82 /** Protocol family and type of the netconn */
nxpfan 0:075157567b0c 83 enum netconn_type {
nxpfan 0:075157567b0c 84 NETCONN_INVALID = 0,
nxpfan 0:075157567b0c 85 /* NETCONN_TCP Group */
nxpfan 0:075157567b0c 86 NETCONN_TCP = 0x10,
nxpfan 0:075157567b0c 87 /* NETCONN_UDP Group */
nxpfan 0:075157567b0c 88 NETCONN_UDP = 0x20,
nxpfan 0:075157567b0c 89 NETCONN_UDPLITE = 0x21,
nxpfan 0:075157567b0c 90 NETCONN_UDPNOCHKSUM= 0x22,
nxpfan 0:075157567b0c 91 /* NETCONN_RAW Group */
nxpfan 0:075157567b0c 92 NETCONN_RAW = 0x40
nxpfan 0:075157567b0c 93 };
nxpfan 0:075157567b0c 94
nxpfan 0:075157567b0c 95 /** Current state of the netconn. Non-TCP netconns are always
nxpfan 0:075157567b0c 96 * in state NETCONN_NONE! */
nxpfan 0:075157567b0c 97 enum netconn_state {
nxpfan 0:075157567b0c 98 NETCONN_NONE,
nxpfan 0:075157567b0c 99 NETCONN_WRITE,
nxpfan 0:075157567b0c 100 NETCONN_LISTEN,
nxpfan 0:075157567b0c 101 NETCONN_CONNECT,
nxpfan 0:075157567b0c 102 NETCONN_CLOSE
nxpfan 0:075157567b0c 103 };
nxpfan 0:075157567b0c 104
nxpfan 0:075157567b0c 105 /** Use to inform the callback function about changes */
nxpfan 0:075157567b0c 106 enum netconn_evt {
nxpfan 0:075157567b0c 107 NETCONN_EVT_RCVPLUS,
nxpfan 0:075157567b0c 108 NETCONN_EVT_RCVMINUS,
nxpfan 0:075157567b0c 109 NETCONN_EVT_SENDPLUS,
nxpfan 0:075157567b0c 110 NETCONN_EVT_SENDMINUS,
nxpfan 0:075157567b0c 111 NETCONN_EVT_ERROR
nxpfan 0:075157567b0c 112 };
nxpfan 0:075157567b0c 113
nxpfan 0:075157567b0c 114 #if LWIP_IGMP
nxpfan 0:075157567b0c 115 /** Used for netconn_join_leave_group() */
nxpfan 0:075157567b0c 116 enum netconn_igmp {
nxpfan 0:075157567b0c 117 NETCONN_JOIN,
nxpfan 0:075157567b0c 118 NETCONN_LEAVE
nxpfan 0:075157567b0c 119 };
nxpfan 0:075157567b0c 120 #endif /* LWIP_IGMP */
nxpfan 0:075157567b0c 121
nxpfan 0:075157567b0c 122 /* forward-declare some structs to avoid to include their headers */
nxpfan 0:075157567b0c 123 struct ip_pcb;
nxpfan 0:075157567b0c 124 struct tcp_pcb;
nxpfan 0:075157567b0c 125 struct udp_pcb;
nxpfan 0:075157567b0c 126 struct raw_pcb;
nxpfan 0:075157567b0c 127 struct netconn;
nxpfan 0:075157567b0c 128 struct api_msg_msg;
nxpfan 0:075157567b0c 129
nxpfan 0:075157567b0c 130 /** A callback prototype to inform about events for a netconn */
nxpfan 0:075157567b0c 131 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
nxpfan 0:075157567b0c 132
nxpfan 0:075157567b0c 133 /** A netconn descriptor */
nxpfan 0:075157567b0c 134 struct netconn {
nxpfan 0:075157567b0c 135 /** type of the netconn (TCP, UDP or RAW) */
nxpfan 0:075157567b0c 136 enum netconn_type type;
nxpfan 0:075157567b0c 137 /** current state of the netconn */
nxpfan 0:075157567b0c 138 enum netconn_state state;
nxpfan 0:075157567b0c 139 /** the lwIP internal protocol control block */
nxpfan 0:075157567b0c 140 union {
nxpfan 0:075157567b0c 141 struct ip_pcb *ip;
nxpfan 0:075157567b0c 142 struct tcp_pcb *tcp;
nxpfan 0:075157567b0c 143 struct udp_pcb *udp;
nxpfan 0:075157567b0c 144 struct raw_pcb *raw;
nxpfan 0:075157567b0c 145 } pcb;
nxpfan 0:075157567b0c 146 /** the last error this netconn had */
nxpfan 0:075157567b0c 147 err_t last_err;
nxpfan 0:075157567b0c 148 /** sem that is used to synchroneously execute functions in the core context */
nxpfan 0:075157567b0c 149 sys_sem_t op_completed;
nxpfan 0:075157567b0c 150 /** mbox where received packets are stored until they are fetched
nxpfan 0:075157567b0c 151 by the netconn application thread (can grow quite big) */
nxpfan 0:075157567b0c 152 sys_mbox_t recvmbox;
nxpfan 0:075157567b0c 153 #if LWIP_TCP
nxpfan 0:075157567b0c 154 /** mbox where new connections are stored until processed
nxpfan 0:075157567b0c 155 by the application thread */
nxpfan 0:075157567b0c 156 sys_mbox_t acceptmbox;
nxpfan 0:075157567b0c 157 #endif /* LWIP_TCP */
nxpfan 0:075157567b0c 158 /** only used for socket layer */
nxpfan 0:075157567b0c 159 #if LWIP_SOCKET
nxpfan 0:075157567b0c 160 int socket;
nxpfan 0:075157567b0c 161 #endif /* LWIP_SOCKET */
nxpfan 0:075157567b0c 162 #if LWIP_SO_RCVTIMEO
nxpfan 0:075157567b0c 163 /** timeout to wait for new data to be received
nxpfan 0:075157567b0c 164 (or connections to arrive for listening netconns) */
nxpfan 0:075157567b0c 165 int recv_timeout;
nxpfan 0:075157567b0c 166 #endif /* LWIP_SO_RCVTIMEO */
nxpfan 0:075157567b0c 167 #if LWIP_SO_RCVBUF
nxpfan 0:075157567b0c 168 /** maximum amount of bytes queued in recvmbox
nxpfan 0:075157567b0c 169 not used for TCP: adjust TCP_WND instead! */
nxpfan 0:075157567b0c 170 int recv_bufsize;
nxpfan 0:075157567b0c 171 #endif /* LWIP_SO_RCVBUF */
nxpfan 0:075157567b0c 172 /** number of bytes currently in recvmbox to be received,
nxpfan 0:075157567b0c 173 tested against recv_bufsize to limit bytes on recvmbox
nxpfan 0:075157567b0c 174 for UDP and RAW
nxpfan 0:075157567b0c 175 @todo: should only be necessary with LWIP_SO_RCVBUF==1 */
nxpfan 0:075157567b0c 176 s16_t recv_avail;
nxpfan 0:075157567b0c 177 /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */
nxpfan 0:075157567b0c 178 u8_t flags;
nxpfan 0:075157567b0c 179 #if LWIP_TCP
nxpfan 0:075157567b0c 180 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
nxpfan 0:075157567b0c 181 this temporarily stores how much is already sent. */
nxpfan 0:075157567b0c 182 size_t write_offset;
nxpfan 0:075157567b0c 183 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
nxpfan 0:075157567b0c 184 this temporarily stores the message.
nxpfan 0:075157567b0c 185 Also used during connect and close. */
nxpfan 0:075157567b0c 186 struct api_msg_msg *current_msg;
nxpfan 0:075157567b0c 187 #endif /* LWIP_TCP */
nxpfan 0:075157567b0c 188 /** A callback function that is informed about events for this netconn */
nxpfan 0:075157567b0c 189 netconn_callback callback;
nxpfan 0:075157567b0c 190 };
nxpfan 0:075157567b0c 191
nxpfan 0:075157567b0c 192 /** Register an Network connection event */
nxpfan 0:075157567b0c 193 #define API_EVENT(c,e,l) if (c->callback) { \
nxpfan 0:075157567b0c 194 (*c->callback)(c, e, l); \
nxpfan 0:075157567b0c 195 }
nxpfan 0:075157567b0c 196
nxpfan 0:075157567b0c 197 /** Set conn->last_err to err but don't overwrite fatal errors */
nxpfan 0:075157567b0c 198 #define NETCONN_SET_SAFE_ERR(conn, err) do { \
nxpfan 0:075157567b0c 199 SYS_ARCH_DECL_PROTECT(lev); \
nxpfan 0:075157567b0c 200 SYS_ARCH_PROTECT(lev); \
nxpfan 0:075157567b0c 201 if (!ERR_IS_FATAL((conn)->last_err)) { \
nxpfan 0:075157567b0c 202 (conn)->last_err = err; \
nxpfan 0:075157567b0c 203 } \
nxpfan 0:075157567b0c 204 SYS_ARCH_UNPROTECT(lev); \
nxpfan 0:075157567b0c 205 } while(0);
nxpfan 0:075157567b0c 206
nxpfan 0:075157567b0c 207 /* Network connection functions: */
nxpfan 0:075157567b0c 208 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
nxpfan 0:075157567b0c 209 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
nxpfan 0:075157567b0c 210 struct
nxpfan 0:075157567b0c 211 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
nxpfan 0:075157567b0c 212 netconn_callback callback);
nxpfan 0:075157567b0c 213 err_t netconn_delete(struct netconn *conn);
nxpfan 0:075157567b0c 214 /** Get the type of a netconn (as enum netconn_type). */
nxpfan 0:075157567b0c 215 #define netconn_type(conn) (conn->type)
nxpfan 0:075157567b0c 216
nxpfan 0:075157567b0c 217 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
nxpfan 0:075157567b0c 218 u16_t *port, u8_t local);
nxpfan 0:075157567b0c 219 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
nxpfan 0:075157567b0c 220 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
nxpfan 0:075157567b0c 221
nxpfan 0:075157567b0c 222 err_t netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port);
nxpfan 0:075157567b0c 223 err_t netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port);
nxpfan 0:075157567b0c 224 err_t netconn_disconnect (struct netconn *conn);
nxpfan 0:075157567b0c 225 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
nxpfan 0:075157567b0c 226 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
nxpfan 0:075157567b0c 227 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
nxpfan 0:075157567b0c 228 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
nxpfan 0:075157567b0c 229 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
nxpfan 0:075157567b0c 230 void netconn_recved(struct netconn *conn, u32_t length);
nxpfan 0:075157567b0c 231 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
nxpfan 0:075157567b0c 232 ip_addr_t *addr, u16_t port);
nxpfan 0:075157567b0c 233 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
nxpfan 0:075157567b0c 234 err_t netconn_write(struct netconn *conn, const void *dataptr, size_t size,
nxpfan 0:075157567b0c 235 u8_t apiflags);
nxpfan 0:075157567b0c 236 err_t netconn_close(struct netconn *conn);
nxpfan 0:075157567b0c 237 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
nxpfan 0:075157567b0c 238
nxpfan 0:075157567b0c 239 #if LWIP_IGMP
nxpfan 0:075157567b0c 240 err_t netconn_join_leave_group(struct netconn *conn, ip_addr_t *multiaddr,
nxpfan 0:075157567b0c 241 ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
nxpfan 0:075157567b0c 242 #endif /* LWIP_IGMP */
nxpfan 0:075157567b0c 243 #if LWIP_DNS
nxpfan 0:075157567b0c 244 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
nxpfan 0:075157567b0c 245 #endif /* LWIP_DNS */
nxpfan 0:075157567b0c 246
nxpfan 0:075157567b0c 247 #define netconn_err(conn) ((conn)->last_err)
nxpfan 0:075157567b0c 248 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
nxpfan 0:075157567b0c 249
nxpfan 0:075157567b0c 250 /** Set the blocking status of netconn calls (@todo: write/send is missing) */
nxpfan 0:075157567b0c 251 #define netconn_set_nonblocking(conn, val) do { if(val) { \
nxpfan 0:075157567b0c 252 (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
nxpfan 0:075157567b0c 253 } else { \
nxpfan 0:075157567b0c 254 (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
nxpfan 0:075157567b0c 255 /** Get the blocking status of netconn calls (@todo: write/send is missing) */
nxpfan 0:075157567b0c 256 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
nxpfan 0:075157567b0c 257
nxpfan 0:075157567b0c 258 /** TCP: Set the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) */
nxpfan 0:075157567b0c 259 #define netconn_set_noautorecved(conn, val) do { if(val) { \
nxpfan 0:075157567b0c 260 (conn)->flags |= NETCONN_FLAG_NO_AUTO_RECVED; \
nxpfan 0:075157567b0c 261 } else { \
nxpfan 0:075157567b0c 262 (conn)->flags &= ~ NETCONN_FLAG_NO_AUTO_RECVED; }} while(0)
nxpfan 0:075157567b0c 263 /** TCP: Get the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) */
nxpfan 0:075157567b0c 264 #define netconn_get_noautorecved(conn) (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)
nxpfan 0:075157567b0c 265
nxpfan 0:075157567b0c 266 #if LWIP_SO_RCVTIMEO
nxpfan 0:075157567b0c 267 /** Set the receive timeout in milliseconds */
nxpfan 0:075157567b0c 268 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
nxpfan 0:075157567b0c 269 /** Get the receive timeout in milliseconds */
nxpfan 0:075157567b0c 270 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
nxpfan 0:075157567b0c 271 #endif /* LWIP_SO_RCVTIMEO */
nxpfan 0:075157567b0c 272 #if LWIP_SO_RCVBUF
nxpfan 0:075157567b0c 273 /** Set the receive buffer in bytes */
nxpfan 0:075157567b0c 274 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
nxpfan 0:075157567b0c 275 /** Get the receive buffer in bytes */
nxpfan 0:075157567b0c 276 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
nxpfan 0:075157567b0c 277 #endif /* LWIP_SO_RCVBUF*/
nxpfan 0:075157567b0c 278
nxpfan 0:075157567b0c 279 #ifdef __cplusplus
nxpfan 0:075157567b0c 280 }
nxpfan 0:075157567b0c 281 #endif
nxpfan 0:075157567b0c 282
nxpfan 0:075157567b0c 283 #endif /* LWIP_NETCONN */
nxpfan 0:075157567b0c 284
nxpfan 0:075157567b0c 285 #endif /* __LWIP_API_H__ */