code that uses Watchdog to reset Mbed every 30seconds. After 30-60mins, the ethernet interface fails to setup() after WatchDog reset.

Dependencies:   mbed

Committer:
eqon
Date:
Thu Jun 07 05:44:29 2012 +0000
Revision:
0:0ce833f21e63

        

Who changed what in which revision?

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