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