Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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