Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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