00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __LWIP_API_H__
00033 #define __LWIP_API_H__
00034
00035 #include "lwip/opt.h"
00036
00037 #if LWIP_NETCONN
00038
00039 #include <stddef.h>
00040
00041 #include "lwip/netbuf.h"
00042 #include "lwip/sys.h"
00043 #include "lwip/ip_addr.h"
00044 #include "lwip/err.h"
00045
00046 #ifdef __cplusplus
00047 extern "C" {
00048 #endif
00049
00050
00051
00052
00053
00054
00055 #define NETCONN_NOFLAG 0x00
00056 #define NETCONN_NOCOPY 0x00
00057 #define NETCONN_COPY 0x01
00058 #define NETCONN_MORE 0x02
00059 #define NETCONN_DONTBLOCK 0x04
00060
00061
00062
00063
00064
00065 #define NETCONN_FLAG_WRITE_DELAYED 0x01
00066
00067 #define NETCONN_FLAG_NON_BLOCKING 0x02
00068
00069 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
00070
00071
00072 #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
00073
00074
00075 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
00076
00077
00078
00079 #define NETCONNTYPE_GROUP(t) (t&0xF0)
00080 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
00081
00082
00083 enum netconn_type {
00084 NETCONN_INVALID = 0,
00085
00086 NETCONN_TCP = 0x10,
00087
00088 NETCONN_UDP = 0x20,
00089 NETCONN_UDPLITE = 0x21,
00090 NETCONN_UDPNOCHKSUM= 0x22,
00091
00092 NETCONN_RAW = 0x40
00093 };
00094
00095
00096
00097 enum netconn_state {
00098 NETCONN_NONE,
00099 NETCONN_WRITE,
00100 NETCONN_LISTEN,
00101 NETCONN_CONNECT,
00102 NETCONN_CLOSE
00103 };
00104
00105
00106 enum netconn_evt {
00107 NETCONN_EVT_RCVPLUS,
00108 NETCONN_EVT_RCVMINUS,
00109 NETCONN_EVT_SENDPLUS,
00110 NETCONN_EVT_SENDMINUS,
00111 NETCONN_EVT_ERROR
00112 };
00113
00114 #if LWIP_IGMP
00115
00116 enum netconn_igmp {
00117 NETCONN_JOIN,
00118 NETCONN_LEAVE
00119 };
00120 #endif
00121
00122
00123 struct ip_pcb;
00124 struct tcp_pcb;
00125 struct udp_pcb;
00126 struct raw_pcb;
00127 struct netconn;
00128 struct api_msg_msg;
00129
00130
00131 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
00132
00133
00134 struct netconn {
00135
00136 enum netconn_type type;
00137
00138 enum netconn_state state;
00139
00140 union {
00141 struct ip_pcb *ip;
00142 struct tcp_pcb *tcp;
00143 struct udp_pcb *udp;
00144 struct raw_pcb *raw;
00145 } pcb;
00146
00147 err_t last_err;
00148
00149 sys_sem_t op_completed;
00150
00151
00152 sys_mbox_t recvmbox;
00153 #if LWIP_TCP
00154
00155
00156 sys_mbox_t acceptmbox;
00157 #endif
00158
00159 #if LWIP_SOCKET
00160 int socket;
00161 #endif
00162 #if LWIP_SO_SNDTIMEO
00163
00164
00165 s32_t send_timeout;
00166 #endif
00167 #if LWIP_SO_RCVTIMEO
00168
00169
00170 int recv_timeout;
00171 #endif
00172 #if LWIP_SO_RCVBUF
00173
00174
00175 int recv_bufsize;
00176
00177
00178
00179 s16_t recv_avail;
00180 #endif
00181
00182 u8_t flags;
00183 #if LWIP_TCP
00184
00185
00186 size_t write_offset;
00187
00188
00189
00190 struct api_msg_msg *current_msg;
00191 #endif
00192
00193 netconn_callback callback;
00194 };
00195
00196
00197 #define API_EVENT(c,e,l) if (c->callback) { \
00198 (*c->callback)(c, e, l); \
00199 }
00200
00201
00202 #define NETCONN_SET_SAFE_ERR(conn, err) do { \
00203 SYS_ARCH_DECL_PROTECT(lev); \
00204 SYS_ARCH_PROTECT(lev); \
00205 if (!ERR_IS_FATAL((conn)->last_err)) { \
00206 (conn)->last_err = err; \
00207 } \
00208 SYS_ARCH_UNPROTECT(lev); \
00209 } while(0);
00210
00211
00212 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
00213 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
00214 struct
00215 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
00216 netconn_callback callback);
00217 err_t netconn_delete(struct netconn *conn);
00218
00219 #define netconn_type(conn) (conn->type)
00220
00221 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
00222 u16_t *port, u8_t local);
00223 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
00224 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
00225
00226 err_t netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port);
00227 err_t netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port);
00228 err_t netconn_disconnect (struct netconn *conn);
00229 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
00230 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
00231 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
00232 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
00233 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
00234 void netconn_recved(struct netconn *conn, u32_t length);
00235 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
00236 ip_addr_t *addr, u16_t port);
00237 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
00238 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
00239 u8_t apiflags, size_t *bytes_written);
00240 #define netconn_write(conn, dataptr, size, apiflags) \
00241 netconn_write_partly(conn, dataptr, size, apiflags, NULL)
00242 err_t netconn_close(struct netconn *conn);
00243 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
00244
00245 #if LWIP_IGMP
00246 err_t netconn_join_leave_group(struct netconn *conn, ip_addr_t *multiaddr,
00247 ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
00248 #endif
00249 #if LWIP_DNS
00250 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
00251 #endif
00252
00253 #define netconn_err(conn) ((conn)->last_err)
00254 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
00255
00256
00257 #define netconn_set_nonblocking(conn, val) do { if(val) { \
00258 (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
00259 } else { \
00260 (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
00261
00262 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
00263
00264
00265 #define netconn_set_noautorecved(conn, val) do { if(val) { \
00266 (conn)->flags |= NETCONN_FLAG_NO_AUTO_RECVED; \
00267 } else { \
00268 (conn)->flags &= ~ NETCONN_FLAG_NO_AUTO_RECVED; }} while(0)
00269
00270 #define netconn_get_noautorecved(conn) (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)
00271
00272 #if LWIP_SO_SNDTIMEO
00273
00274 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout))
00275
00276 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout)
00277 #endif
00278 #if LWIP_SO_RCVTIMEO
00279
00280 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
00281
00282 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
00283 #endif
00284 #if LWIP_SO_RCVBUF
00285
00286 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
00287
00288 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
00289 #endif
00290
00291 #ifdef __cplusplus
00292 }
00293 #endif
00294
00295 #endif
00296
00297 #endif