Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robert_jw 0:b2805b6888dc 1 /**
robert_jw 0:b2805b6888dc 2 * @file
robert_jw 0:b2805b6888dc 3 * Sequential API External module
robert_jw 0:b2805b6888dc 4 *
robert_jw 0:b2805b6888dc 5 */
robert_jw 0:b2805b6888dc 6
robert_jw 0:b2805b6888dc 7 /*
robert_jw 0:b2805b6888dc 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
robert_jw 0:b2805b6888dc 9 * All rights reserved.
robert_jw 0:b2805b6888dc 10 *
robert_jw 0:b2805b6888dc 11 * Redistribution and use in source and binary forms, with or without modification,
robert_jw 0:b2805b6888dc 12 * are permitted provided that the following conditions are met:
robert_jw 0:b2805b6888dc 13 *
robert_jw 0:b2805b6888dc 14 * 1. Redistributions of source code must retain the above copyright notice,
robert_jw 0:b2805b6888dc 15 * this list of conditions and the following disclaimer.
robert_jw 0:b2805b6888dc 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
robert_jw 0:b2805b6888dc 17 * this list of conditions and the following disclaimer in the documentation
robert_jw 0:b2805b6888dc 18 * and/or other materials provided with the distribution.
robert_jw 0:b2805b6888dc 19 * 3. The name of the author may not be used to endorse or promote products
robert_jw 0:b2805b6888dc 20 * derived from this software without specific prior written permission.
robert_jw 0:b2805b6888dc 21 *
robert_jw 0:b2805b6888dc 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
robert_jw 0:b2805b6888dc 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
robert_jw 0:b2805b6888dc 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
robert_jw 0:b2805b6888dc 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
robert_jw 0:b2805b6888dc 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
robert_jw 0:b2805b6888dc 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
robert_jw 0:b2805b6888dc 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
robert_jw 0:b2805b6888dc 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
robert_jw 0:b2805b6888dc 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
robert_jw 0:b2805b6888dc 31 * OF SUCH DAMAGE.
robert_jw 0:b2805b6888dc 32 *
robert_jw 0:b2805b6888dc 33 * This file is part of the lwIP TCP/IP stack.
robert_jw 0:b2805b6888dc 34 *
robert_jw 0:b2805b6888dc 35 * Author: Adam Dunkels <adam@sics.se>
robert_jw 0:b2805b6888dc 36 *
robert_jw 0:b2805b6888dc 37 */
robert_jw 0:b2805b6888dc 38
robert_jw 0:b2805b6888dc 39 /* This is the part of the API that is linked with
robert_jw 0:b2805b6888dc 40 the application */
robert_jw 0:b2805b6888dc 41
robert_jw 0:b2805b6888dc 42 #include "lwip/opt.h"
robert_jw 0:b2805b6888dc 43
robert_jw 0:b2805b6888dc 44 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
robert_jw 0:b2805b6888dc 45
robert_jw 0:b2805b6888dc 46 #include "lwip/api.h"
robert_jw 0:b2805b6888dc 47 #include "lwip/tcpip.h"
robert_jw 0:b2805b6888dc 48 #include "lwip/memp.h"
robert_jw 0:b2805b6888dc 49
robert_jw 0:b2805b6888dc 50 #include "lwip/ip.h"
robert_jw 0:b2805b6888dc 51 #include "lwip/raw.h"
robert_jw 0:b2805b6888dc 52 #include "lwip/udp.h"
robert_jw 0:b2805b6888dc 53 #include "lwip/tcp.h"
robert_jw 0:b2805b6888dc 54
robert_jw 0:b2805b6888dc 55 #include <string.h>
robert_jw 0:b2805b6888dc 56
robert_jw 0:b2805b6888dc 57 /**
robert_jw 0:b2805b6888dc 58 * Create a new netconn (of a specific type) that has a callback function.
robert_jw 0:b2805b6888dc 59 * The corresponding pcb is also created.
robert_jw 0:b2805b6888dc 60 *
robert_jw 0:b2805b6888dc 61 * @param t the type of 'connection' to create (@see enum netconn_type)
robert_jw 0:b2805b6888dc 62 * @param proto the IP protocol for RAW IP pcbs
robert_jw 0:b2805b6888dc 63 * @param callback a function to call on status changes (RX available, TX'ed)
robert_jw 0:b2805b6888dc 64 * @return a newly allocated struct netconn or
robert_jw 0:b2805b6888dc 65 * NULL on memory error
robert_jw 0:b2805b6888dc 66 */
robert_jw 0:b2805b6888dc 67 struct netconn*
robert_jw 0:b2805b6888dc 68 netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
robert_jw 0:b2805b6888dc 69 {
robert_jw 0:b2805b6888dc 70 struct netconn *conn;
robert_jw 0:b2805b6888dc 71 struct api_msg msg;
robert_jw 0:b2805b6888dc 72
robert_jw 0:b2805b6888dc 73 conn = netconn_alloc(t, callback);
robert_jw 0:b2805b6888dc 74 if (conn != NULL) {
robert_jw 0:b2805b6888dc 75 msg.function = do_newconn;
robert_jw 0:b2805b6888dc 76 msg.msg.msg.n.proto = proto;
robert_jw 0:b2805b6888dc 77 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 78 if (TCPIP_APIMSG(&msg) != ERR_OK) {
robert_jw 0:b2805b6888dc 79 LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
robert_jw 0:b2805b6888dc 80 LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
robert_jw 0:b2805b6888dc 81 LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
robert_jw 0:b2805b6888dc 82 #if LWIP_TCP
robert_jw 0:b2805b6888dc 83 LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
robert_jw 0:b2805b6888dc 84 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 85 sys_sem_free(&conn->op_completed);
robert_jw 0:b2805b6888dc 86 sys_mbox_free(&conn->recvmbox);
robert_jw 0:b2805b6888dc 87 memp_free(MEMP_NETCONN, conn);
robert_jw 0:b2805b6888dc 88 return NULL;
robert_jw 0:b2805b6888dc 89 }
robert_jw 0:b2805b6888dc 90 }
robert_jw 0:b2805b6888dc 91 return conn;
robert_jw 0:b2805b6888dc 92 }
robert_jw 0:b2805b6888dc 93
robert_jw 0:b2805b6888dc 94 /**
robert_jw 0:b2805b6888dc 95 * Close a netconn 'connection' and free its resources.
robert_jw 0:b2805b6888dc 96 * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
robert_jw 0:b2805b6888dc 97 * after this returns.
robert_jw 0:b2805b6888dc 98 *
robert_jw 0:b2805b6888dc 99 * @param conn the netconn to delete
robert_jw 0:b2805b6888dc 100 * @return ERR_OK if the connection was deleted
robert_jw 0:b2805b6888dc 101 */
robert_jw 0:b2805b6888dc 102 err_t
robert_jw 0:b2805b6888dc 103 netconn_delete(struct netconn *conn)
robert_jw 0:b2805b6888dc 104 {
robert_jw 0:b2805b6888dc 105 struct api_msg msg;
robert_jw 0:b2805b6888dc 106
robert_jw 0:b2805b6888dc 107 /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
robert_jw 0:b2805b6888dc 108 if (conn == NULL) {
robert_jw 0:b2805b6888dc 109 return ERR_OK;
robert_jw 0:b2805b6888dc 110 }
robert_jw 0:b2805b6888dc 111
robert_jw 0:b2805b6888dc 112 msg.function = do_delconn;
robert_jw 0:b2805b6888dc 113 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 114 tcpip_apimsg(&msg);
robert_jw 0:b2805b6888dc 115
robert_jw 0:b2805b6888dc 116 netconn_free(conn);
robert_jw 0:b2805b6888dc 117
robert_jw 0:b2805b6888dc 118 /* don't care for return value of do_delconn since it only calls void functions */
robert_jw 0:b2805b6888dc 119
robert_jw 0:b2805b6888dc 120 return ERR_OK;
robert_jw 0:b2805b6888dc 121 }
robert_jw 0:b2805b6888dc 122
robert_jw 0:b2805b6888dc 123 /**
robert_jw 0:b2805b6888dc 124 * Get the local or remote IP address and port of a netconn.
robert_jw 0:b2805b6888dc 125 * For RAW netconns, this returns the protocol instead of a port!
robert_jw 0:b2805b6888dc 126 *
robert_jw 0:b2805b6888dc 127 * @param conn the netconn to query
robert_jw 0:b2805b6888dc 128 * @param addr a pointer to which to save the IP address
robert_jw 0:b2805b6888dc 129 * @param port a pointer to which to save the port (or protocol for RAW)
robert_jw 0:b2805b6888dc 130 * @param local 1 to get the local IP address, 0 to get the remote one
robert_jw 0:b2805b6888dc 131 * @return ERR_CONN for invalid connections
robert_jw 0:b2805b6888dc 132 * ERR_OK if the information was retrieved
robert_jw 0:b2805b6888dc 133 */
robert_jw 0:b2805b6888dc 134 err_t
robert_jw 0:b2805b6888dc 135 netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
robert_jw 0:b2805b6888dc 136 {
robert_jw 0:b2805b6888dc 137 struct api_msg msg;
robert_jw 0:b2805b6888dc 138 err_t err;
robert_jw 0:b2805b6888dc 139
robert_jw 0:b2805b6888dc 140 LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 141 LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 142 LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 143
robert_jw 0:b2805b6888dc 144 msg.function = do_getaddr;
robert_jw 0:b2805b6888dc 145 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 146 msg.msg.msg.ad.ipaddr = addr;
robert_jw 0:b2805b6888dc 147 msg.msg.msg.ad.port = port;
robert_jw 0:b2805b6888dc 148 msg.msg.msg.ad.local = local;
robert_jw 0:b2805b6888dc 149 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 150
robert_jw 0:b2805b6888dc 151 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 152 return err;
robert_jw 0:b2805b6888dc 153 }
robert_jw 0:b2805b6888dc 154
robert_jw 0:b2805b6888dc 155 /**
robert_jw 0:b2805b6888dc 156 * Bind a netconn to a specific local IP address and port.
robert_jw 0:b2805b6888dc 157 * Binding one netconn twice might not always be checked correctly!
robert_jw 0:b2805b6888dc 158 *
robert_jw 0:b2805b6888dc 159 * @param conn the netconn to bind
robert_jw 0:b2805b6888dc 160 * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
robert_jw 0:b2805b6888dc 161 * to bind to all addresses)
robert_jw 0:b2805b6888dc 162 * @param port the local port to bind the netconn to (not used for RAW)
robert_jw 0:b2805b6888dc 163 * @return ERR_OK if bound, any other err_t on failure
robert_jw 0:b2805b6888dc 164 */
robert_jw 0:b2805b6888dc 165 err_t
robert_jw 0:b2805b6888dc 166 netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port)
robert_jw 0:b2805b6888dc 167 {
robert_jw 0:b2805b6888dc 168 struct api_msg msg;
robert_jw 0:b2805b6888dc 169 err_t err;
robert_jw 0:b2805b6888dc 170
robert_jw 0:b2805b6888dc 171 LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 172
robert_jw 0:b2805b6888dc 173 msg.function = do_bind;
robert_jw 0:b2805b6888dc 174 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 175 msg.msg.msg.bc.ipaddr = addr;
robert_jw 0:b2805b6888dc 176 msg.msg.msg.bc.port = port;
robert_jw 0:b2805b6888dc 177 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 178
robert_jw 0:b2805b6888dc 179 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 180 return err;
robert_jw 0:b2805b6888dc 181 }
robert_jw 0:b2805b6888dc 182
robert_jw 0:b2805b6888dc 183 /**
robert_jw 0:b2805b6888dc 184 * Connect a netconn to a specific remote IP address and port.
robert_jw 0:b2805b6888dc 185 *
robert_jw 0:b2805b6888dc 186 * @param conn the netconn to connect
robert_jw 0:b2805b6888dc 187 * @param addr the remote IP address to connect to
robert_jw 0:b2805b6888dc 188 * @param port the remote port to connect to (no used for RAW)
robert_jw 0:b2805b6888dc 189 * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
robert_jw 0:b2805b6888dc 190 */
robert_jw 0:b2805b6888dc 191 err_t
robert_jw 0:b2805b6888dc 192 netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port)
robert_jw 0:b2805b6888dc 193 {
robert_jw 0:b2805b6888dc 194 struct api_msg msg;
robert_jw 0:b2805b6888dc 195 err_t err;
robert_jw 0:b2805b6888dc 196
robert_jw 0:b2805b6888dc 197 LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 198
robert_jw 0:b2805b6888dc 199 msg.function = do_connect;
robert_jw 0:b2805b6888dc 200 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 201 msg.msg.msg.bc.ipaddr = addr;
robert_jw 0:b2805b6888dc 202 msg.msg.msg.bc.port = port;
robert_jw 0:b2805b6888dc 203 /* This is the only function which need to not block tcpip_thread */
robert_jw 0:b2805b6888dc 204 err = tcpip_apimsg(&msg);
robert_jw 0:b2805b6888dc 205
robert_jw 0:b2805b6888dc 206 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 207 return err;
robert_jw 0:b2805b6888dc 208 }
robert_jw 0:b2805b6888dc 209
robert_jw 0:b2805b6888dc 210 /**
robert_jw 0:b2805b6888dc 211 * Disconnect a netconn from its current peer (only valid for UDP netconns).
robert_jw 0:b2805b6888dc 212 *
robert_jw 0:b2805b6888dc 213 * @param conn the netconn to disconnect
robert_jw 0:b2805b6888dc 214 * @return TODO: return value is not set here...
robert_jw 0:b2805b6888dc 215 */
robert_jw 0:b2805b6888dc 216 err_t
robert_jw 0:b2805b6888dc 217 netconn_disconnect(struct netconn *conn)
robert_jw 0:b2805b6888dc 218 {
robert_jw 0:b2805b6888dc 219 struct api_msg msg;
robert_jw 0:b2805b6888dc 220 err_t err;
robert_jw 0:b2805b6888dc 221
robert_jw 0:b2805b6888dc 222 LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 223
robert_jw 0:b2805b6888dc 224 msg.function = do_disconnect;
robert_jw 0:b2805b6888dc 225 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 226 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 227
robert_jw 0:b2805b6888dc 228 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 229 return err;
robert_jw 0:b2805b6888dc 230 }
robert_jw 0:b2805b6888dc 231
robert_jw 0:b2805b6888dc 232 /**
robert_jw 0:b2805b6888dc 233 * Set a TCP netconn into listen mode
robert_jw 0:b2805b6888dc 234 *
robert_jw 0:b2805b6888dc 235 * @param conn the tcp netconn to set to listen mode
robert_jw 0:b2805b6888dc 236 * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
robert_jw 0:b2805b6888dc 237 * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
robert_jw 0:b2805b6888dc 238 * don't return any error (yet?))
robert_jw 0:b2805b6888dc 239 */
robert_jw 0:b2805b6888dc 240 err_t
robert_jw 0:b2805b6888dc 241 netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
robert_jw 0:b2805b6888dc 242 {
robert_jw 0:b2805b6888dc 243 #if LWIP_TCP
robert_jw 0:b2805b6888dc 244 struct api_msg msg;
robert_jw 0:b2805b6888dc 245 err_t err;
robert_jw 0:b2805b6888dc 246
robert_jw 0:b2805b6888dc 247 /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
robert_jw 0:b2805b6888dc 248 LWIP_UNUSED_ARG(backlog);
robert_jw 0:b2805b6888dc 249
robert_jw 0:b2805b6888dc 250 LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 251
robert_jw 0:b2805b6888dc 252 msg.function = do_listen;
robert_jw 0:b2805b6888dc 253 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 254 #if TCP_LISTEN_BACKLOG
robert_jw 0:b2805b6888dc 255 msg.msg.msg.lb.backlog = backlog;
robert_jw 0:b2805b6888dc 256 #endif /* TCP_LISTEN_BACKLOG */
robert_jw 0:b2805b6888dc 257 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 258
robert_jw 0:b2805b6888dc 259 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 260 return err;
robert_jw 0:b2805b6888dc 261 #else /* LWIP_TCP */
robert_jw 0:b2805b6888dc 262 LWIP_UNUSED_ARG(conn);
robert_jw 0:b2805b6888dc 263 LWIP_UNUSED_ARG(backlog);
robert_jw 0:b2805b6888dc 264 return ERR_ARG;
robert_jw 0:b2805b6888dc 265 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 266 }
robert_jw 0:b2805b6888dc 267
robert_jw 0:b2805b6888dc 268 /**
robert_jw 0:b2805b6888dc 269 * Accept a new connection on a TCP listening netconn.
robert_jw 0:b2805b6888dc 270 *
robert_jw 0:b2805b6888dc 271 * @param conn the TCP listen netconn
robert_jw 0:b2805b6888dc 272 * @param new_conn pointer where the new connection is stored
robert_jw 0:b2805b6888dc 273 * @return ERR_OK if a new connection has been received or an error
robert_jw 0:b2805b6888dc 274 * code otherwise
robert_jw 0:b2805b6888dc 275 */
robert_jw 0:b2805b6888dc 276 err_t
robert_jw 0:b2805b6888dc 277 netconn_accept(struct netconn *conn, struct netconn **new_conn)
robert_jw 0:b2805b6888dc 278 {
robert_jw 0:b2805b6888dc 279 #if LWIP_TCP
robert_jw 0:b2805b6888dc 280 struct netconn *newconn;
robert_jw 0:b2805b6888dc 281 err_t err;
robert_jw 0:b2805b6888dc 282 #if TCP_LISTEN_BACKLOG
robert_jw 0:b2805b6888dc 283 struct api_msg msg;
robert_jw 0:b2805b6888dc 284 #endif /* TCP_LISTEN_BACKLOG */
robert_jw 0:b2805b6888dc 285
robert_jw 0:b2805b6888dc 286 LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 287 *new_conn = NULL;
robert_jw 0:b2805b6888dc 288 LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 289 LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox), return ERR_ARG;);
robert_jw 0:b2805b6888dc 290
robert_jw 0:b2805b6888dc 291 err = conn->last_err;
robert_jw 0:b2805b6888dc 292 if (ERR_IS_FATAL(err)) {
robert_jw 0:b2805b6888dc 293 /* don't recv on fatal errors: this might block the application task
robert_jw 0:b2805b6888dc 294 waiting on acceptmbox forever! */
robert_jw 0:b2805b6888dc 295 return err;
robert_jw 0:b2805b6888dc 296 }
robert_jw 0:b2805b6888dc 297
robert_jw 0:b2805b6888dc 298 #if LWIP_SO_RCVTIMEO
robert_jw 0:b2805b6888dc 299 if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
robert_jw 0:b2805b6888dc 300 NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
robert_jw 0:b2805b6888dc 301 return ERR_TIMEOUT;
robert_jw 0:b2805b6888dc 302 }
robert_jw 0:b2805b6888dc 303 #else
robert_jw 0:b2805b6888dc 304 sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);
robert_jw 0:b2805b6888dc 305 #endif /* LWIP_SO_RCVTIMEO*/
robert_jw 0:b2805b6888dc 306 /* Register event with callback */
robert_jw 0:b2805b6888dc 307 API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
robert_jw 0:b2805b6888dc 308
robert_jw 0:b2805b6888dc 309 if (newconn == NULL) {
robert_jw 0:b2805b6888dc 310 /* connection has been aborted */
robert_jw 0:b2805b6888dc 311 NETCONN_SET_SAFE_ERR(conn, ERR_ABRT);
robert_jw 0:b2805b6888dc 312 return ERR_ABRT;
robert_jw 0:b2805b6888dc 313 }
robert_jw 0:b2805b6888dc 314 #if TCP_LISTEN_BACKLOG
robert_jw 0:b2805b6888dc 315 /* Let the stack know that we have accepted the connection. */
robert_jw 0:b2805b6888dc 316 msg.function = do_recv;
robert_jw 0:b2805b6888dc 317 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 318 /* don't care for the return value of do_recv */
robert_jw 0:b2805b6888dc 319 TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 320 #endif /* TCP_LISTEN_BACKLOG */
robert_jw 0:b2805b6888dc 321
robert_jw 0:b2805b6888dc 322 *new_conn = newconn;
robert_jw 0:b2805b6888dc 323 /* don't set conn->last_err: it's only ERR_OK, anyway */
robert_jw 0:b2805b6888dc 324 return ERR_OK;
robert_jw 0:b2805b6888dc 325 #else /* LWIP_TCP */
robert_jw 0:b2805b6888dc 326 LWIP_UNUSED_ARG(conn);
robert_jw 0:b2805b6888dc 327 LWIP_UNUSED_ARG(new_conn);
robert_jw 0:b2805b6888dc 328 return ERR_ARG;
robert_jw 0:b2805b6888dc 329 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 330 }
robert_jw 0:b2805b6888dc 331
robert_jw 0:b2805b6888dc 332 /**
robert_jw 0:b2805b6888dc 333 * Receive data: actual implementation that doesn't care whether pbuf or netbuf
robert_jw 0:b2805b6888dc 334 * is received
robert_jw 0:b2805b6888dc 335 *
robert_jw 0:b2805b6888dc 336 * @param conn the netconn from which to receive data
robert_jw 0:b2805b6888dc 337 * @param new_buf pointer where a new pbuf/netbuf is stored when received data
robert_jw 0:b2805b6888dc 338 * @return ERR_OK if data has been received, an error code otherwise (timeout,
robert_jw 0:b2805b6888dc 339 * memory error or another error)
robert_jw 0:b2805b6888dc 340 */
robert_jw 0:b2805b6888dc 341 static err_t
robert_jw 0:b2805b6888dc 342 netconn_recv_data(struct netconn *conn, void **new_buf)
robert_jw 0:b2805b6888dc 343 {
robert_jw 0:b2805b6888dc 344 void *buf = NULL;
robert_jw 0:b2805b6888dc 345 u16_t len;
robert_jw 0:b2805b6888dc 346 err_t err;
robert_jw 0:b2805b6888dc 347 #if LWIP_TCP
robert_jw 0:b2805b6888dc 348 struct api_msg msg;
robert_jw 0:b2805b6888dc 349 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 350
robert_jw 0:b2805b6888dc 351 LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 352 *new_buf = NULL;
robert_jw 0:b2805b6888dc 353 LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 354 LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
robert_jw 0:b2805b6888dc 355
robert_jw 0:b2805b6888dc 356 err = conn->last_err;
robert_jw 0:b2805b6888dc 357 if (ERR_IS_FATAL(err)) {
robert_jw 0:b2805b6888dc 358 /* don't recv on fatal errors: this might block the application task
robert_jw 0:b2805b6888dc 359 waiting on recvmbox forever! */
robert_jw 0:b2805b6888dc 360 /* @todo: this does not allow us to fetch data that has been put into recvmbox
robert_jw 0:b2805b6888dc 361 before the fatal error occurred - is that a problem? */
robert_jw 0:b2805b6888dc 362 return err;
robert_jw 0:b2805b6888dc 363 }
robert_jw 0:b2805b6888dc 364
robert_jw 0:b2805b6888dc 365 #if LWIP_SO_RCVTIMEO
robert_jw 0:b2805b6888dc 366 if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
robert_jw 0:b2805b6888dc 367 NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
robert_jw 0:b2805b6888dc 368 return ERR_TIMEOUT;
robert_jw 0:b2805b6888dc 369 }
robert_jw 0:b2805b6888dc 370 #else
robert_jw 0:b2805b6888dc 371 sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
robert_jw 0:b2805b6888dc 372 #endif /* LWIP_SO_RCVTIMEO*/
robert_jw 0:b2805b6888dc 373
robert_jw 0:b2805b6888dc 374 #if LWIP_TCP
robert_jw 0:b2805b6888dc 375 if (conn->type == NETCONN_TCP) {
robert_jw 0:b2805b6888dc 376 if (!netconn_get_noautorecved(conn) || (buf == NULL)) {
robert_jw 0:b2805b6888dc 377 /* Let the stack know that we have taken the data. */
robert_jw 0:b2805b6888dc 378 /* TODO: Speedup: Don't block and wait for the answer here
robert_jw 0:b2805b6888dc 379 (to prevent multiple thread-switches). */
robert_jw 0:b2805b6888dc 380 msg.function = do_recv;
robert_jw 0:b2805b6888dc 381 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 382 if (buf != NULL) {
robert_jw 0:b2805b6888dc 383 msg.msg.msg.r.len = ((struct pbuf *)buf)->tot_len;
robert_jw 0:b2805b6888dc 384 } else {
robert_jw 0:b2805b6888dc 385 msg.msg.msg.r.len = 1;
robert_jw 0:b2805b6888dc 386 }
robert_jw 0:b2805b6888dc 387 /* don't care for the return value of do_recv */
robert_jw 0:b2805b6888dc 388 TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 389 }
robert_jw 0:b2805b6888dc 390
robert_jw 0:b2805b6888dc 391 /* If we are closed, we indicate that we no longer wish to use the socket */
robert_jw 0:b2805b6888dc 392 if (buf == NULL) {
robert_jw 0:b2805b6888dc 393 API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
robert_jw 0:b2805b6888dc 394 /* Avoid to lose any previous error code */
robert_jw 0:b2805b6888dc 395 NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
robert_jw 0:b2805b6888dc 396 return ERR_CLSD;
robert_jw 0:b2805b6888dc 397 }
robert_jw 0:b2805b6888dc 398 len = ((struct pbuf *)buf)->tot_len;
robert_jw 0:b2805b6888dc 399 }
robert_jw 0:b2805b6888dc 400 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 401 #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
robert_jw 0:b2805b6888dc 402 else
robert_jw 0:b2805b6888dc 403 #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
robert_jw 0:b2805b6888dc 404 #if (LWIP_UDP || LWIP_RAW)
robert_jw 0:b2805b6888dc 405 {
robert_jw 0:b2805b6888dc 406 LWIP_ASSERT("buf != NULL", buf != NULL);
robert_jw 0:b2805b6888dc 407 len = netbuf_len((struct netbuf *)buf);
robert_jw 0:b2805b6888dc 408 }
robert_jw 0:b2805b6888dc 409 #endif /* (LWIP_UDP || LWIP_RAW) */
robert_jw 0:b2805b6888dc 410
robert_jw 0:b2805b6888dc 411 #if LWIP_SO_RCVBUF
robert_jw 0:b2805b6888dc 412 SYS_ARCH_DEC(conn->recv_avail, len);
robert_jw 0:b2805b6888dc 413 #endif /* LWIP_SO_RCVBUF */
robert_jw 0:b2805b6888dc 414 /* Register event with callback */
robert_jw 0:b2805b6888dc 415 API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
robert_jw 0:b2805b6888dc 416
robert_jw 0:b2805b6888dc 417 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
robert_jw 0:b2805b6888dc 418
robert_jw 0:b2805b6888dc 419 *new_buf = buf;
robert_jw 0:b2805b6888dc 420 /* don't set conn->last_err: it's only ERR_OK, anyway */
robert_jw 0:b2805b6888dc 421 return ERR_OK;
robert_jw 0:b2805b6888dc 422 }
robert_jw 0:b2805b6888dc 423
robert_jw 0:b2805b6888dc 424 /**
robert_jw 0:b2805b6888dc 425 * Receive data (in form of a pbuf) from a TCP netconn
robert_jw 0:b2805b6888dc 426 *
robert_jw 0:b2805b6888dc 427 * @param conn the netconn from which to receive data
robert_jw 0:b2805b6888dc 428 * @param new_buf pointer where a new pbuf is stored when received data
robert_jw 0:b2805b6888dc 429 * @return ERR_OK if data has been received, an error code otherwise (timeout,
robert_jw 0:b2805b6888dc 430 * memory error or another error)
robert_jw 0:b2805b6888dc 431 * ERR_ARG if conn is not a TCP netconn
robert_jw 0:b2805b6888dc 432 */
robert_jw 0:b2805b6888dc 433 err_t
robert_jw 0:b2805b6888dc 434 netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
robert_jw 0:b2805b6888dc 435 {
robert_jw 0:b2805b6888dc 436 LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
robert_jw 0:b2805b6888dc 437 netconn_type(conn) == NETCONN_TCP, return ERR_ARG;);
robert_jw 0:b2805b6888dc 438
robert_jw 0:b2805b6888dc 439 return netconn_recv_data(conn, (void **)new_buf);
robert_jw 0:b2805b6888dc 440 }
robert_jw 0:b2805b6888dc 441
robert_jw 0:b2805b6888dc 442 /**
robert_jw 0:b2805b6888dc 443 * Receive data (in form of a netbuf containing a packet buffer) from a netconn
robert_jw 0:b2805b6888dc 444 *
robert_jw 0:b2805b6888dc 445 * @param conn the netconn from which to receive data
robert_jw 0:b2805b6888dc 446 * @param new_buf pointer where a new netbuf is stored when received data
robert_jw 0:b2805b6888dc 447 * @return ERR_OK if data has been received, an error code otherwise (timeout,
robert_jw 0:b2805b6888dc 448 * memory error or another error)
robert_jw 0:b2805b6888dc 449 */
robert_jw 0:b2805b6888dc 450 err_t
robert_jw 0:b2805b6888dc 451 netconn_recv(struct netconn *conn, struct netbuf **new_buf)
robert_jw 0:b2805b6888dc 452 {
robert_jw 0:b2805b6888dc 453 #if LWIP_TCP
robert_jw 0:b2805b6888dc 454 struct netbuf *buf = NULL;
robert_jw 0:b2805b6888dc 455 err_t err;
robert_jw 0:b2805b6888dc 456 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 457
robert_jw 0:b2805b6888dc 458 LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 459 *new_buf = NULL;
robert_jw 0:b2805b6888dc 460 LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 461 LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
robert_jw 0:b2805b6888dc 462
robert_jw 0:b2805b6888dc 463 #if LWIP_TCP
robert_jw 0:b2805b6888dc 464 if (conn->type == NETCONN_TCP) {
robert_jw 0:b2805b6888dc 465 struct pbuf *p = NULL;
robert_jw 0:b2805b6888dc 466 /* This is not a listening netconn, since recvmbox is set */
robert_jw 0:b2805b6888dc 467
robert_jw 0:b2805b6888dc 468 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
robert_jw 0:b2805b6888dc 469 if (buf == NULL) {
robert_jw 0:b2805b6888dc 470 NETCONN_SET_SAFE_ERR(conn, ERR_MEM);
robert_jw 0:b2805b6888dc 471 return ERR_MEM;
robert_jw 0:b2805b6888dc 472 }
robert_jw 0:b2805b6888dc 473
robert_jw 0:b2805b6888dc 474 err = netconn_recv_data(conn, (void **)&p);
robert_jw 0:b2805b6888dc 475 if (err != ERR_OK) {
robert_jw 0:b2805b6888dc 476 memp_free(MEMP_NETBUF, buf);
robert_jw 0:b2805b6888dc 477 return err;
robert_jw 0:b2805b6888dc 478 }
robert_jw 0:b2805b6888dc 479 LWIP_ASSERT("p != NULL", p != NULL);
robert_jw 0:b2805b6888dc 480
robert_jw 0:b2805b6888dc 481 buf->p = p;
robert_jw 0:b2805b6888dc 482 buf->ptr = p;
robert_jw 0:b2805b6888dc 483 buf->port = 0;
robert_jw 0:b2805b6888dc 484 ip_addr_set_any(&buf->addr);
robert_jw 0:b2805b6888dc 485 *new_buf = buf;
robert_jw 0:b2805b6888dc 486 /* don't set conn->last_err: it's only ERR_OK, anyway */
robert_jw 0:b2805b6888dc 487 return ERR_OK;
robert_jw 0:b2805b6888dc 488 } else
robert_jw 0:b2805b6888dc 489 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 490 {
robert_jw 0:b2805b6888dc 491 #if (LWIP_UDP || LWIP_RAW)
robert_jw 0:b2805b6888dc 492 return netconn_recv_data(conn, (void **)new_buf);
robert_jw 0:b2805b6888dc 493 #endif /* (LWIP_UDP || LWIP_RAW) */
robert_jw 0:b2805b6888dc 494 }
robert_jw 0:b2805b6888dc 495 }
robert_jw 0:b2805b6888dc 496
robert_jw 0:b2805b6888dc 497 /**
robert_jw 0:b2805b6888dc 498 * TCP: update the receive window: by calling this, the application
robert_jw 0:b2805b6888dc 499 * tells the stack that it has processed data and is able to accept
robert_jw 0:b2805b6888dc 500 * new data.
robert_jw 0:b2805b6888dc 501 * ATTENTION: use with care, this is mainly used for sockets!
robert_jw 0:b2805b6888dc 502 * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
robert_jw 0:b2805b6888dc 503 *
robert_jw 0:b2805b6888dc 504 * @param conn the netconn for which to update the receive window
robert_jw 0:b2805b6888dc 505 * @param length amount of data processed (ATTENTION: this must be accurate!)
robert_jw 0:b2805b6888dc 506 */
robert_jw 0:b2805b6888dc 507 void
robert_jw 0:b2805b6888dc 508 netconn_recved(struct netconn *conn, u32_t length)
robert_jw 0:b2805b6888dc 509 {
robert_jw 0:b2805b6888dc 510 #if LWIP_TCP
robert_jw 0:b2805b6888dc 511 if ((conn != NULL) && (conn->type == NETCONN_TCP) &&
robert_jw 0:b2805b6888dc 512 (netconn_get_noautorecved(conn))) {
robert_jw 0:b2805b6888dc 513 struct api_msg msg;
robert_jw 0:b2805b6888dc 514 /* Let the stack know that we have taken the data. */
robert_jw 0:b2805b6888dc 515 /* TODO: Speedup: Don't block and wait for the answer here
robert_jw 0:b2805b6888dc 516 (to prevent multiple thread-switches). */
robert_jw 0:b2805b6888dc 517 msg.function = do_recv;
robert_jw 0:b2805b6888dc 518 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 519 msg.msg.msg.r.len = length;
robert_jw 0:b2805b6888dc 520 /* don't care for the return value of do_recv */
robert_jw 0:b2805b6888dc 521 TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 522 }
robert_jw 0:b2805b6888dc 523 #else /* LWIP_TCP */
robert_jw 0:b2805b6888dc 524 LWIP_UNUSED_ARG(conn);
robert_jw 0:b2805b6888dc 525 LWIP_UNUSED_ARG(length);
robert_jw 0:b2805b6888dc 526 #endif /* LWIP_TCP */
robert_jw 0:b2805b6888dc 527 }
robert_jw 0:b2805b6888dc 528
robert_jw 0:b2805b6888dc 529 /**
robert_jw 0:b2805b6888dc 530 * Send data (in form of a netbuf) to a specific remote IP address and port.
robert_jw 0:b2805b6888dc 531 * Only to be used for UDP and RAW netconns (not TCP).
robert_jw 0:b2805b6888dc 532 *
robert_jw 0:b2805b6888dc 533 * @param conn the netconn over which to send data
robert_jw 0:b2805b6888dc 534 * @param buf a netbuf containing the data to send
robert_jw 0:b2805b6888dc 535 * @param addr the remote IP address to which to send the data
robert_jw 0:b2805b6888dc 536 * @param port the remote port to which to send the data
robert_jw 0:b2805b6888dc 537 * @return ERR_OK if data was sent, any other err_t on error
robert_jw 0:b2805b6888dc 538 */
robert_jw 0:b2805b6888dc 539 err_t
robert_jw 0:b2805b6888dc 540 netconn_sendto(struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port)
robert_jw 0:b2805b6888dc 541 {
robert_jw 0:b2805b6888dc 542 if (buf != NULL) {
robert_jw 0:b2805b6888dc 543 ip_addr_set(&buf->addr, addr);
robert_jw 0:b2805b6888dc 544 buf->port = port;
robert_jw 0:b2805b6888dc 545 return netconn_send(conn, buf);
robert_jw 0:b2805b6888dc 546 }
robert_jw 0:b2805b6888dc 547 return ERR_VAL;
robert_jw 0:b2805b6888dc 548 }
robert_jw 0:b2805b6888dc 549
robert_jw 0:b2805b6888dc 550 /**
robert_jw 0:b2805b6888dc 551 * Send data over a UDP or RAW netconn (that is already connected).
robert_jw 0:b2805b6888dc 552 *
robert_jw 0:b2805b6888dc 553 * @param conn the UDP or RAW netconn over which to send data
robert_jw 0:b2805b6888dc 554 * @param buf a netbuf containing the data to send
robert_jw 0:b2805b6888dc 555 * @return ERR_OK if data was sent, any other err_t on error
robert_jw 0:b2805b6888dc 556 */
robert_jw 0:b2805b6888dc 557 err_t
robert_jw 0:b2805b6888dc 558 netconn_send(struct netconn *conn, struct netbuf *buf)
robert_jw 0:b2805b6888dc 559 {
robert_jw 0:b2805b6888dc 560 struct api_msg msg;
robert_jw 0:b2805b6888dc 561 err_t err;
robert_jw 0:b2805b6888dc 562
robert_jw 0:b2805b6888dc 563 LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 564
robert_jw 0:b2805b6888dc 565 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
robert_jw 0:b2805b6888dc 566 msg.function = do_send;
robert_jw 0:b2805b6888dc 567 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 568 msg.msg.msg.b = buf;
robert_jw 0:b2805b6888dc 569 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 570
robert_jw 0:b2805b6888dc 571 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 572 return err;
robert_jw 0:b2805b6888dc 573 }
robert_jw 0:b2805b6888dc 574
robert_jw 0:b2805b6888dc 575 /**
robert_jw 0:b2805b6888dc 576 * Send data over a TCP netconn.
robert_jw 0:b2805b6888dc 577 *
robert_jw 0:b2805b6888dc 578 * @param conn the TCP netconn over which to send data
robert_jw 0:b2805b6888dc 579 * @param dataptr pointer to the application buffer that contains the data to send
robert_jw 0:b2805b6888dc 580 * @param size size of the application data to send
robert_jw 0:b2805b6888dc 581 * @param apiflags combination of following flags :
robert_jw 0:b2805b6888dc 582 * - NETCONN_COPY: data will be copied into memory belonging to the stack
robert_jw 0:b2805b6888dc 583 * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
robert_jw 0:b2805b6888dc 584 * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once
robert_jw 0:b2805b6888dc 585 * @return ERR_OK if data was sent, any other err_t on error
robert_jw 0:b2805b6888dc 586 */
robert_jw 0:b2805b6888dc 587 err_t
robert_jw 0:b2805b6888dc 588 netconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags)
robert_jw 0:b2805b6888dc 589 {
robert_jw 0:b2805b6888dc 590 struct api_msg msg;
robert_jw 0:b2805b6888dc 591 err_t err;
robert_jw 0:b2805b6888dc 592
robert_jw 0:b2805b6888dc 593 LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 594 LWIP_ERROR("netconn_write: invalid conn->type", (conn->type == NETCONN_TCP), return ERR_VAL;);
robert_jw 0:b2805b6888dc 595 if (size == 0) {
robert_jw 0:b2805b6888dc 596 return ERR_OK;
robert_jw 0:b2805b6888dc 597 }
robert_jw 0:b2805b6888dc 598
robert_jw 0:b2805b6888dc 599 /* @todo: for non-blocking write, check if 'size' would ever fit into
robert_jw 0:b2805b6888dc 600 snd_queue or snd_buf */
robert_jw 0:b2805b6888dc 601 msg.function = do_write;
robert_jw 0:b2805b6888dc 602 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 603 msg.msg.msg.w.dataptr = dataptr;
robert_jw 0:b2805b6888dc 604 msg.msg.msg.w.apiflags = apiflags;
robert_jw 0:b2805b6888dc 605 msg.msg.msg.w.len = size;
robert_jw 0:b2805b6888dc 606 /* For locking the core: this _can_ be delayed on low memory/low send buffer,
robert_jw 0:b2805b6888dc 607 but if it is, this is done inside api_msg.c:do_write(), so we can use the
robert_jw 0:b2805b6888dc 608 non-blocking version here. */
robert_jw 0:b2805b6888dc 609 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 610
robert_jw 0:b2805b6888dc 611 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 612 return err;
robert_jw 0:b2805b6888dc 613 }
robert_jw 0:b2805b6888dc 614
robert_jw 0:b2805b6888dc 615 /**
robert_jw 0:b2805b6888dc 616 * Close ot shutdown a TCP netconn (doesn't delete it).
robert_jw 0:b2805b6888dc 617 *
robert_jw 0:b2805b6888dc 618 * @param conn the TCP netconn to close or shutdown
robert_jw 0:b2805b6888dc 619 * @param how fully close or only shutdown one side?
robert_jw 0:b2805b6888dc 620 * @return ERR_OK if the netconn was closed, any other err_t on error
robert_jw 0:b2805b6888dc 621 */
robert_jw 0:b2805b6888dc 622 static err_t
robert_jw 0:b2805b6888dc 623 netconn_close_shutdown(struct netconn *conn, u8_t how)
robert_jw 0:b2805b6888dc 624 {
robert_jw 0:b2805b6888dc 625 struct api_msg msg;
robert_jw 0:b2805b6888dc 626 err_t err;
robert_jw 0:b2805b6888dc 627
robert_jw 0:b2805b6888dc 628 LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 629
robert_jw 0:b2805b6888dc 630 msg.function = do_close;
robert_jw 0:b2805b6888dc 631 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 632 /* shutting down both ends is the same as closing */
robert_jw 0:b2805b6888dc 633 msg.msg.msg.sd.shut = how;
robert_jw 0:b2805b6888dc 634 /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
robert_jw 0:b2805b6888dc 635 don't use TCPIP_APIMSG here */
robert_jw 0:b2805b6888dc 636 err = tcpip_apimsg(&msg);
robert_jw 0:b2805b6888dc 637
robert_jw 0:b2805b6888dc 638 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 639 return err;
robert_jw 0:b2805b6888dc 640 }
robert_jw 0:b2805b6888dc 641
robert_jw 0:b2805b6888dc 642 /**
robert_jw 0:b2805b6888dc 643 * Close a TCP netconn (doesn't delete it).
robert_jw 0:b2805b6888dc 644 *
robert_jw 0:b2805b6888dc 645 * @param conn the TCP netconn to close
robert_jw 0:b2805b6888dc 646 * @return ERR_OK if the netconn was closed, any other err_t on error
robert_jw 0:b2805b6888dc 647 */
robert_jw 0:b2805b6888dc 648 err_t
robert_jw 0:b2805b6888dc 649 netconn_close(struct netconn *conn)
robert_jw 0:b2805b6888dc 650 {
robert_jw 0:b2805b6888dc 651 /* shutting down both ends is the same as closing */
robert_jw 0:b2805b6888dc 652 return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
robert_jw 0:b2805b6888dc 653 }
robert_jw 0:b2805b6888dc 654
robert_jw 0:b2805b6888dc 655 /**
robert_jw 0:b2805b6888dc 656 * Shut down one or both sides of a TCP netconn (doesn't delete it).
robert_jw 0:b2805b6888dc 657 *
robert_jw 0:b2805b6888dc 658 * @param conn the TCP netconn to shut down
robert_jw 0:b2805b6888dc 659 * @return ERR_OK if the netconn was closed, any other err_t on error
robert_jw 0:b2805b6888dc 660 */
robert_jw 0:b2805b6888dc 661 err_t
robert_jw 0:b2805b6888dc 662 netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
robert_jw 0:b2805b6888dc 663 {
robert_jw 0:b2805b6888dc 664 return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
robert_jw 0:b2805b6888dc 665 }
robert_jw 0:b2805b6888dc 666
robert_jw 0:b2805b6888dc 667 #if LWIP_IGMP
robert_jw 0:b2805b6888dc 668 /**
robert_jw 0:b2805b6888dc 669 * Join multicast groups for UDP netconns.
robert_jw 0:b2805b6888dc 670 *
robert_jw 0:b2805b6888dc 671 * @param conn the UDP netconn for which to change multicast addresses
robert_jw 0:b2805b6888dc 672 * @param multiaddr IP address of the multicast group to join or leave
robert_jw 0:b2805b6888dc 673 * @param netif_addr the IP address of the network interface on which to send
robert_jw 0:b2805b6888dc 674 * the igmp message
robert_jw 0:b2805b6888dc 675 * @param join_or_leave flag whether to send a join- or leave-message
robert_jw 0:b2805b6888dc 676 * @return ERR_OK if the action was taken, any err_t on error
robert_jw 0:b2805b6888dc 677 */
robert_jw 0:b2805b6888dc 678 err_t
robert_jw 0:b2805b6888dc 679 netconn_join_leave_group(struct netconn *conn,
robert_jw 0:b2805b6888dc 680 ip_addr_t *multiaddr,
robert_jw 0:b2805b6888dc 681 ip_addr_t *netif_addr,
robert_jw 0:b2805b6888dc 682 enum netconn_igmp join_or_leave)
robert_jw 0:b2805b6888dc 683 {
robert_jw 0:b2805b6888dc 684 struct api_msg msg;
robert_jw 0:b2805b6888dc 685 err_t err;
robert_jw 0:b2805b6888dc 686
robert_jw 0:b2805b6888dc 687 LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 688
robert_jw 0:b2805b6888dc 689 msg.function = do_join_leave_group;
robert_jw 0:b2805b6888dc 690 msg.msg.conn = conn;
robert_jw 0:b2805b6888dc 691 msg.msg.msg.jl.multiaddr = multiaddr;
robert_jw 0:b2805b6888dc 692 msg.msg.msg.jl.netif_addr = netif_addr;
robert_jw 0:b2805b6888dc 693 msg.msg.msg.jl.join_or_leave = join_or_leave;
robert_jw 0:b2805b6888dc 694 err = TCPIP_APIMSG(&msg);
robert_jw 0:b2805b6888dc 695
robert_jw 0:b2805b6888dc 696 NETCONN_SET_SAFE_ERR(conn, err);
robert_jw 0:b2805b6888dc 697 return err;
robert_jw 0:b2805b6888dc 698 }
robert_jw 0:b2805b6888dc 699 #endif /* LWIP_IGMP */
robert_jw 0:b2805b6888dc 700
robert_jw 0:b2805b6888dc 701 #if LWIP_DNS
robert_jw 0:b2805b6888dc 702 /**
robert_jw 0:b2805b6888dc 703 * Execute a DNS query, only one IP address is returned
robert_jw 0:b2805b6888dc 704 *
robert_jw 0:b2805b6888dc 705 * @param name a string representation of the DNS host name to query
robert_jw 0:b2805b6888dc 706 * @param addr a preallocated ip_addr_t where to store the resolved IP address
robert_jw 0:b2805b6888dc 707 * @return ERR_OK: resolving succeeded
robert_jw 0:b2805b6888dc 708 * ERR_MEM: memory error, try again later
robert_jw 0:b2805b6888dc 709 * ERR_ARG: dns client not initialized or invalid hostname
robert_jw 0:b2805b6888dc 710 * ERR_VAL: dns server response was invalid
robert_jw 0:b2805b6888dc 711 */
robert_jw 0:b2805b6888dc 712 err_t
robert_jw 0:b2805b6888dc 713 netconn_gethostbyname(const char *name, ip_addr_t *addr)
robert_jw 0:b2805b6888dc 714 {
robert_jw 0:b2805b6888dc 715 struct dns_api_msg msg;
robert_jw 0:b2805b6888dc 716 err_t err;
robert_jw 0:b2805b6888dc 717 sys_sem_t sem;
robert_jw 0:b2805b6888dc 718
robert_jw 0:b2805b6888dc 719 LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 720 LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
robert_jw 0:b2805b6888dc 721
robert_jw 0:b2805b6888dc 722 err = sys_sem_new(&sem, 0);
robert_jw 0:b2805b6888dc 723 if (err != ERR_OK) {
robert_jw 0:b2805b6888dc 724 return err;
robert_jw 0:b2805b6888dc 725 }
robert_jw 0:b2805b6888dc 726
robert_jw 0:b2805b6888dc 727 msg.name = name;
robert_jw 0:b2805b6888dc 728 msg.addr = addr;
robert_jw 0:b2805b6888dc 729 msg.err = &err;
robert_jw 0:b2805b6888dc 730 msg.sem = &sem;
robert_jw 0:b2805b6888dc 731
robert_jw 0:b2805b6888dc 732 tcpip_callback(do_gethostbyname, &msg);
robert_jw 0:b2805b6888dc 733 sys_sem_wait(&sem);
robert_jw 0:b2805b6888dc 734 sys_sem_free(&sem);
robert_jw 0:b2805b6888dc 735
robert_jw 0:b2805b6888dc 736 return err;
robert_jw 0:b2805b6888dc 737 }
robert_jw 0:b2805b6888dc 738 #endif /* LWIP_DNS*/
robert_jw 0:b2805b6888dc 739
robert_jw 0:b2805b6888dc 740 #endif /* LWIP_NETCONN */