Adapted to Lora Semtech + Nucleo

Dependencies:   DebugLib

Dependents:   LoRaWAN-lmic-app LoRaWAN-lmic-app LoRaWAN-test-10secs LoRaPersonalizedDeviceForEverynet ... more

Fork of lwip_ppp_ethernet by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers api_lib.c Source File

api_lib.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Sequential API External module
00004  *
00005  */
00006  
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without modification, 
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission. 
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  * 
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 
00039 /* This is the part of the API that is linked with
00040    the application */
00041 
00042 #include "lwip/opt.h"
00043 
00044 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
00045 
00046 #include "lwip/api.h"
00047 #include "lwip/tcpip.h"
00048 #include "lwip/memp.h"
00049 
00050 #include "lwip/ip.h"
00051 #include "lwip/raw.h"
00052 #include "lwip/udp.h"
00053 #include "lwip/tcp.h"
00054 
00055 #include <string.h>
00056 
00057 /**
00058  * Create a new netconn (of a specific type) that has a callback function.
00059  * The corresponding pcb is also created.
00060  *
00061  * @param t the type of 'connection' to create (@see enum netconn_type)
00062  * @param proto the IP protocol for RAW IP pcbs
00063  * @param callback a function to call on status changes (RX available, TX'ed)
00064  * @return a newly allocated struct netconn or
00065  *         NULL on memory error
00066  */
00067 struct netconn*
00068 netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
00069 {
00070   struct netconn *conn;
00071   struct api_msg msg;
00072 
00073   conn = netconn_alloc(t, callback);
00074   if (conn != NULL) {
00075     LWIP_DEBUGF(API_LIB_DEBUG, ("Alloc OK"));
00076     msg.function = do_newconn;
00077     msg.msg.msg.n.proto = proto;
00078     msg.msg.conn = conn;
00079     if (TCPIP_APIMSG(&msg) != ERR_OK) {
00080       LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
00081       LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
00082       LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
00083 #if LWIP_TCP
00084       LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
00085 #endif /* LWIP_TCP */
00086       sys_sem_free(&conn->op_completed);
00087       sys_mbox_free(&conn->recvmbox);
00088       memp_free(MEMP_NETCONN, conn);
00089       return NULL;
00090     }
00091   }
00092   return conn;
00093 }
00094 
00095 /**
00096  * Close a netconn 'connection' and free its resources.
00097  * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
00098  * after this returns.
00099  *
00100  * @param conn the netconn to delete
00101  * @return ERR_OK if the connection was deleted
00102  */
00103 err_t
00104 netconn_delete(struct netconn *conn)
00105 {
00106   struct api_msg msg;
00107 
00108   /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
00109   if (conn == NULL) {
00110     return ERR_OK;
00111   }
00112 
00113   msg.function = do_delconn;
00114   msg.msg.conn = conn;
00115   tcpip_apimsg(&msg);
00116 
00117   netconn_free(conn);
00118 
00119   /* don't care for return value of do_delconn since it only calls void functions */
00120 
00121   return ERR_OK;
00122 }
00123 
00124 /**
00125  * Get the local or remote IP address and port of a netconn.
00126  * For RAW netconns, this returns the protocol instead of a port!
00127  *
00128  * @param conn the netconn to query
00129  * @param addr a pointer to which to save the IP address
00130  * @param port a pointer to which to save the port (or protocol for RAW)
00131  * @param local 1 to get the local IP address, 0 to get the remote one
00132  * @return ERR_CONN for invalid connections
00133  *         ERR_OK if the information was retrieved
00134  */
00135 err_t
00136 netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
00137 {
00138   struct api_msg msg;
00139   err_t err;
00140 
00141   LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
00142   LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
00143   LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
00144 
00145   msg.function = do_getaddr;
00146   msg.msg.conn = conn;
00147   msg.msg.msg.ad.ipaddr = addr;
00148   msg.msg.msg.ad.port = port;
00149   msg.msg.msg.ad.local = local;
00150   err = TCPIP_APIMSG(&msg);
00151 
00152   NETCONN_SET_SAFE_ERR(conn, err);
00153   return err;
00154 }
00155 
00156 /**
00157  * Bind a netconn to a specific local IP address and port.
00158  * Binding one netconn twice might not always be checked correctly!
00159  *
00160  * @param conn the netconn to bind
00161  * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
00162  *             to bind to all addresses)
00163  * @param port the local port to bind the netconn to (not used for RAW)
00164  * @return ERR_OK if bound, any other err_t on failure
00165  */
00166 err_t
00167 netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port)
00168 {
00169   struct api_msg msg;
00170   err_t err;
00171 
00172   LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
00173 
00174   msg.function = do_bind;
00175   msg.msg.conn = conn;
00176   msg.msg.msg.bc.ipaddr = addr;
00177   msg.msg.msg.bc.port = port;
00178   err = TCPIP_APIMSG(&msg);
00179 
00180   NETCONN_SET_SAFE_ERR(conn, err);
00181   return err;
00182 }
00183 
00184 /**
00185  * Connect a netconn to a specific remote IP address and port.
00186  *
00187  * @param conn the netconn to connect
00188  * @param addr the remote IP address to connect to
00189  * @param port the remote port to connect to (no used for RAW)
00190  * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
00191  */
00192 err_t
00193 netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port)
00194 {
00195   struct api_msg msg;
00196   err_t err;
00197 
00198   LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
00199 
00200   msg.function = do_connect;
00201   msg.msg.conn = conn;
00202   msg.msg.msg.bc.ipaddr = addr;
00203   msg.msg.msg.bc.port = port;
00204   /* This is the only function which need to not block tcpip_thread */
00205   err = tcpip_apimsg(&msg);
00206 
00207   NETCONN_SET_SAFE_ERR(conn, err);
00208   return err;
00209 }
00210 
00211 /**
00212  * Disconnect a netconn from its current peer (only valid for UDP netconns).
00213  *
00214  * @param conn the netconn to disconnect
00215  * @return TODO: return value is not set here...
00216  */
00217 err_t
00218 netconn_disconnect(struct netconn *conn)
00219 {
00220   struct api_msg msg;
00221   err_t err;
00222 
00223   LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
00224 
00225   msg.function = do_disconnect;
00226   msg.msg.conn = conn;
00227   err = TCPIP_APIMSG(&msg);
00228 
00229   NETCONN_SET_SAFE_ERR(conn, err);
00230   return err;
00231 }
00232 
00233 /**
00234  * Set a TCP netconn into listen mode
00235  *
00236  * @param conn the tcp netconn to set to listen mode
00237  * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
00238  * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
00239  *         don't return any error (yet?))
00240  */
00241 err_t
00242 netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
00243 {
00244 #if LWIP_TCP
00245   struct api_msg msg;
00246   err_t err;
00247 
00248   /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
00249   LWIP_UNUSED_ARG(backlog);
00250 
00251   LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
00252 
00253   msg.function = do_listen;
00254   msg.msg.conn = conn;
00255 #if TCP_LISTEN_BACKLOG
00256   msg.msg.msg.lb.backlog = backlog;
00257 #endif /* TCP_LISTEN_BACKLOG */
00258   err = TCPIP_APIMSG(&msg);
00259 
00260   NETCONN_SET_SAFE_ERR(conn, err);
00261   return err;
00262 #else /* LWIP_TCP */
00263   LWIP_UNUSED_ARG(conn);
00264   LWIP_UNUSED_ARG(backlog);
00265   return ERR_ARG;
00266 #endif /* LWIP_TCP */
00267 }
00268 
00269 /**
00270  * Accept a new connection on a TCP listening netconn.
00271  *
00272  * @param conn the TCP listen netconn
00273  * @param new_conn pointer where the new connection is stored
00274  * @return ERR_OK if a new connection has been received or an error
00275  *                code otherwise
00276  */
00277 err_t
00278 netconn_accept(struct netconn *conn, struct netconn **new_conn)
00279 {
00280 #if LWIP_TCP
00281   struct netconn *newconn;
00282   err_t err;
00283 #if TCP_LISTEN_BACKLOG
00284   struct api_msg msg;
00285 #endif /* TCP_LISTEN_BACKLOG */
00286 
00287   LWIP_ERROR("netconn_accept: invalid pointer",    (new_conn != NULL),                  return ERR_ARG;);
00288   *new_conn = NULL;
00289   LWIP_ERROR("netconn_accept: invalid conn",       (conn != NULL),                      return ERR_ARG;);
00290   LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox),   return ERR_ARG;);
00291 
00292   err = conn->last_err;
00293   if (ERR_IS_FATAL(err)) {
00294     /* don't recv on fatal errors: this might block the application task
00295        waiting on acceptmbox forever! */
00296     return err;
00297   }
00298 
00299 #if LWIP_SO_RCVTIMEO
00300   if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
00301     NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
00302     return ERR_TIMEOUT;
00303   }
00304 #else
00305   sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);
00306 #endif /* LWIP_SO_RCVTIMEO*/
00307   /* Register event with callback */
00308   API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
00309 
00310   if (newconn == NULL) {
00311     /* connection has been aborted */
00312     NETCONN_SET_SAFE_ERR(conn, ERR_ABRT);
00313     return ERR_ABRT;
00314   }
00315 #if TCP_LISTEN_BACKLOG
00316   /* Let the stack know that we have accepted the connection. */
00317   msg.function = do_recv;
00318   msg.msg.conn = conn;
00319   /* don't care for the return value of do_recv */
00320   TCPIP_APIMSG(&msg);
00321 #endif /* TCP_LISTEN_BACKLOG */
00322 
00323   *new_conn = newconn;
00324   /* don't set conn->last_err: it's only ERR_OK, anyway */
00325   return ERR_OK;
00326 #else /* LWIP_TCP */
00327   LWIP_UNUSED_ARG(conn);
00328   LWIP_UNUSED_ARG(new_conn);
00329   return ERR_ARG;
00330 #endif /* LWIP_TCP */
00331 }
00332 
00333 /**
00334  * Receive data: actual implementation that doesn't care whether pbuf or netbuf
00335  * is received
00336  *
00337  * @param conn the netconn from which to receive data
00338  * @param new_buf pointer where a new pbuf/netbuf is stored when received data
00339  * @return ERR_OK if data has been received, an error code otherwise (timeout,
00340  *                memory error or another error)
00341  */
00342 static err_t
00343 netconn_recv_data(struct netconn *conn, void **new_buf)
00344 {
00345   void *buf = NULL;
00346   u16_t len;
00347   err_t err;
00348 #if LWIP_TCP
00349   struct api_msg msg;
00350 #endif /* LWIP_TCP */
00351 
00352   LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
00353   *new_buf = NULL;
00354   LWIP_ERROR("netconn_recv: invalid conn",    (conn != NULL),    return ERR_ARG;);
00355   LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
00356 
00357   err = conn->last_err;
00358   if (ERR_IS_FATAL(err)) {
00359     /* don't recv on fatal errors: this might block the application task
00360        waiting on recvmbox forever! */
00361     /* @todo: this does not allow us to fetch data that has been put into recvmbox
00362        before the fatal error occurred - is that a problem? */
00363     return err;
00364   }
00365 
00366 #if LWIP_SO_RCVTIMEO
00367   if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
00368     NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
00369     return ERR_TIMEOUT;
00370   }
00371 #else
00372   sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
00373 #endif /* LWIP_SO_RCVTIMEO*/
00374 
00375 #if LWIP_TCP
00376   if (conn->type == NETCONN_TCP) {
00377     if (!netconn_get_noautorecved(conn) || (buf == NULL)) {
00378       /* Let the stack know that we have taken the data. */
00379       /* TODO: Speedup: Don't block and wait for the answer here
00380          (to prevent multiple thread-switches). */
00381       msg.function = do_recv;
00382       msg.msg.conn = conn;
00383       if (buf != NULL) {
00384         msg.msg.msg.r.len = ((struct pbuf *)buf)->tot_len;
00385       } else {
00386         msg.msg.msg.r.len = 1;
00387       }
00388       /* don't care for the return value of do_recv */
00389       TCPIP_APIMSG(&msg);
00390     }
00391 
00392     /* If we are closed, we indicate that we no longer wish to use the socket */
00393     if (buf == NULL) {
00394       API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
00395       /* Avoid to lose any previous error code */
00396       NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
00397       return ERR_CLSD;
00398     }
00399     len = ((struct pbuf *)buf)->tot_len;
00400   }
00401 #endif /* LWIP_TCP */
00402 #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
00403   else
00404 #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
00405 #if (LWIP_UDP || LWIP_RAW)
00406   {
00407     LWIP_ASSERT("buf != NULL", buf != NULL);
00408     len = netbuf_len((struct netbuf *)buf);
00409   }
00410 #endif /* (LWIP_UDP || LWIP_RAW) */
00411 
00412 #if LWIP_SO_RCVBUF
00413   SYS_ARCH_DEC(conn->recv_avail, len);
00414 #endif /* LWIP_SO_RCVBUF */
00415   /* Register event with callback */
00416   API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
00417 
00418   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
00419 
00420   *new_buf = buf;
00421   /* don't set conn->last_err: it's only ERR_OK, anyway */
00422   return ERR_OK;
00423 }
00424 
00425 /**
00426  * Receive data (in form of a pbuf) from a TCP netconn
00427  *
00428  * @param conn the netconn from which to receive data
00429  * @param new_buf pointer where a new pbuf is stored when received data
00430  * @return ERR_OK if data has been received, an error code otherwise (timeout,
00431  *                memory error or another error)
00432  *         ERR_ARG if conn is not a TCP netconn
00433  */
00434 err_t
00435 netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
00436 {
00437   LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
00438              netconn_type(conn) == NETCONN_TCP, return ERR_ARG;);
00439 
00440   return netconn_recv_data(conn, (void **)new_buf);
00441 }
00442 
00443 /**
00444  * Receive data (in form of a netbuf containing a packet buffer) from a netconn
00445  *
00446  * @param conn the netconn from which to receive data
00447  * @param new_buf pointer where a new netbuf is stored when received data
00448  * @return ERR_OK if data has been received, an error code otherwise (timeout,
00449  *                memory error or another error)
00450  */
00451 err_t
00452 netconn_recv(struct netconn *conn, struct netbuf **new_buf)
00453 {
00454 #if LWIP_TCP
00455   struct netbuf *buf = NULL;
00456   err_t err;
00457 #endif /* LWIP_TCP */
00458 
00459   LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
00460   *new_buf = NULL;
00461   LWIP_ERROR("netconn_recv: invalid conn",    (conn != NULL),    return ERR_ARG;);
00462   LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
00463 
00464 #if LWIP_TCP
00465   if (conn->type == NETCONN_TCP) {
00466     struct pbuf *p = NULL;
00467     /* This is not a listening netconn, since recvmbox is set */
00468 
00469     buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
00470     if (buf == NULL) {
00471       NETCONN_SET_SAFE_ERR(conn, ERR_MEM);
00472       return ERR_MEM;
00473     }
00474 
00475     err = netconn_recv_data(conn, (void **)&p);
00476     if (err != ERR_OK) {
00477       memp_free(MEMP_NETBUF, buf);
00478       return err;
00479     }
00480     LWIP_ASSERT("p != NULL", p != NULL);
00481 
00482     buf->p = p;
00483     buf->ptr = p;
00484     buf->port = 0;
00485     ip_addr_set_any(&buf->addr);
00486     *new_buf = buf;
00487     /* don't set conn->last_err: it's only ERR_OK, anyway */
00488     return ERR_OK;
00489   } else
00490 #endif /* LWIP_TCP */
00491   {
00492 #if (LWIP_UDP || LWIP_RAW)
00493     return netconn_recv_data(conn, (void **)new_buf);
00494 #endif /* (LWIP_UDP || LWIP_RAW) */
00495   }
00496 }
00497 
00498 /**
00499  * TCP: update the receive window: by calling this, the application
00500  * tells the stack that it has processed data and is able to accept
00501  * new data.
00502  * ATTENTION: use with care, this is mainly used for sockets!
00503  * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
00504  *
00505  * @param conn the netconn for which to update the receive window
00506  * @param length amount of data processed (ATTENTION: this must be accurate!)
00507  */
00508 void
00509 netconn_recved(struct netconn *conn, u32_t length)
00510 {
00511 #if LWIP_TCP
00512   if ((conn != NULL) && (conn->type == NETCONN_TCP) &&
00513       (netconn_get_noautorecved(conn))) {
00514     struct api_msg msg;
00515     /* Let the stack know that we have taken the data. */
00516     /* TODO: Speedup: Don't block and wait for the answer here
00517        (to prevent multiple thread-switches). */
00518     msg.function = do_recv;
00519     msg.msg.conn = conn;
00520     msg.msg.msg.r.len = length;
00521     /* don't care for the return value of do_recv */
00522     TCPIP_APIMSG(&msg);
00523   }
00524 #else /* LWIP_TCP */
00525   LWIP_UNUSED_ARG(conn);
00526   LWIP_UNUSED_ARG(length);
00527 #endif /* LWIP_TCP */
00528 }
00529 
00530 /**
00531  * Send data (in form of a netbuf) to a specific remote IP address and port.
00532  * Only to be used for UDP and RAW netconns (not TCP).
00533  *
00534  * @param conn the netconn over which to send data
00535  * @param buf a netbuf containing the data to send
00536  * @param addr the remote IP address to which to send the data
00537  * @param port the remote port to which to send the data
00538  * @return ERR_OK if data was sent, any other err_t on error
00539  */
00540 err_t
00541 netconn_sendto(struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port)
00542 {
00543   if (buf != NULL) {
00544     ip_addr_set(&buf->addr, addr);
00545     buf->port = port;
00546     return netconn_send(conn, buf);
00547   }
00548   return ERR_VAL;
00549 }
00550 
00551 /**
00552  * Send data over a UDP or RAW netconn (that is already connected).
00553  *
00554  * @param conn the UDP or RAW netconn over which to send data
00555  * @param buf a netbuf containing the data to send
00556  * @return ERR_OK if data was sent, any other err_t on error
00557  */
00558 err_t
00559 netconn_send(struct netconn *conn, struct netbuf *buf)
00560 {
00561   struct api_msg msg;
00562   err_t err;
00563 
00564   LWIP_ERROR("netconn_send: invalid conn",  (conn != NULL), return ERR_ARG;);
00565 
00566   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
00567   msg.function = do_send;
00568   msg.msg.conn = conn;
00569   msg.msg.msg.b = buf;
00570   err = TCPIP_APIMSG(&msg);
00571 
00572   NETCONN_SET_SAFE_ERR(conn, err);
00573   return err;
00574 }
00575 
00576 /**
00577  * Send data over a TCP netconn.
00578  *
00579  * @param conn the TCP netconn over which to send data
00580  * @param dataptr pointer to the application buffer that contains the data to send
00581  * @param size size of the application data to send
00582  * @param apiflags combination of following flags :
00583  * - NETCONN_COPY: data will be copied into memory belonging to the stack
00584  * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
00585  * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once
00586  * @return ERR_OK if data was sent, any other err_t on error
00587  */
00588 err_t
00589 netconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags)
00590 {
00591   struct api_msg msg;
00592   err_t err;
00593 
00594   LWIP_ERROR("netconn_write: invalid conn",  (conn != NULL), return ERR_ARG;);
00595   LWIP_ERROR("netconn_write: invalid conn->type",  (conn->type == NETCONN_TCP), return ERR_VAL;);
00596   if (size == 0) {
00597     return ERR_OK;
00598   }
00599 
00600   /* @todo: for non-blocking write, check if 'size' would ever fit into
00601             snd_queue or snd_buf */
00602   msg.function = do_write;
00603   msg.msg.conn = conn;
00604   msg.msg.msg.w.dataptr = dataptr;
00605   msg.msg.msg.w.apiflags = apiflags;
00606   msg.msg.msg.w.len = size;
00607   /* For locking the core: this _can_ be delayed on low memory/low send buffer,
00608      but if it is, this is done inside api_msg.c:do_write(), so we can use the
00609      non-blocking version here. */
00610   err = TCPIP_APIMSG(&msg);
00611 
00612   NETCONN_SET_SAFE_ERR(conn, err);
00613   return err;
00614 }
00615 
00616 /**
00617  * Close ot shutdown a TCP netconn (doesn't delete it).
00618  *
00619  * @param conn the TCP netconn to close or shutdown
00620  * @param how fully close or only shutdown one side?
00621  * @return ERR_OK if the netconn was closed, any other err_t on error
00622  */
00623 static err_t
00624 netconn_close_shutdown(struct netconn *conn, u8_t how)
00625 {
00626   struct api_msg msg;
00627   err_t err;
00628 
00629   LWIP_ERROR("netconn_close: invalid conn",  (conn != NULL), return ERR_ARG;);
00630 
00631   msg.function = do_close;
00632   msg.msg.conn = conn;
00633   /* shutting down both ends is the same as closing */
00634   msg.msg.msg.sd.shut = how;
00635   /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
00636      don't use TCPIP_APIMSG here */
00637   err = tcpip_apimsg(&msg);
00638 
00639   NETCONN_SET_SAFE_ERR(conn, err);
00640   return err;
00641 }
00642 
00643 /**
00644  * Close a TCP netconn (doesn't delete it).
00645  *
00646  * @param conn the TCP netconn to close
00647  * @return ERR_OK if the netconn was closed, any other err_t on error
00648  */
00649 err_t
00650 netconn_close(struct netconn *conn)
00651 {
00652   /* shutting down both ends is the same as closing */
00653   return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
00654 }
00655 
00656 /**
00657  * Shut down one or both sides of a TCP netconn (doesn't delete it).
00658  *
00659  * @param conn the TCP netconn to shut down
00660  * @return ERR_OK if the netconn was closed, any other err_t on error
00661  */
00662 err_t
00663 netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
00664 {
00665   return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
00666 }
00667 
00668 #if LWIP_IGMP
00669 /**
00670  * Join multicast groups for UDP netconns.
00671  *
00672  * @param conn the UDP netconn for which to change multicast addresses
00673  * @param multiaddr IP address of the multicast group to join or leave
00674  * @param netif_addr the IP address of the network interface on which to send
00675  *                  the igmp message
00676  * @param join_or_leave flag whether to send a join- or leave-message
00677  * @return ERR_OK if the action was taken, any err_t on error
00678  */
00679 err_t
00680 netconn_join_leave_group(struct netconn *conn,
00681                          ip_addr_t *multiaddr,
00682                          ip_addr_t *netif_addr,
00683                          enum netconn_igmp join_or_leave)
00684 {
00685   struct api_msg msg;
00686   err_t err;
00687 
00688   LWIP_ERROR("netconn_join_leave_group: invalid conn",  (conn != NULL), return ERR_ARG;);
00689 
00690   msg.function = do_join_leave_group;
00691   msg.msg.conn = conn;
00692   msg.msg.msg.jl.multiaddr = multiaddr;
00693   msg.msg.msg.jl.netif_addr = netif_addr;
00694   msg.msg.msg.jl.join_or_leave = join_or_leave;
00695   err = TCPIP_APIMSG(&msg);
00696 
00697   NETCONN_SET_SAFE_ERR(conn, err);
00698   return err;
00699 }
00700 #endif /* LWIP_IGMP */
00701 
00702 #if LWIP_DNS
00703 /**
00704  * Execute a DNS query, only one IP address is returned
00705  *
00706  * @param name a string representation of the DNS host name to query
00707  * @param addr a preallocated ip_addr_t where to store the resolved IP address
00708  * @return ERR_OK: resolving succeeded
00709  *         ERR_MEM: memory error, try again later
00710  *         ERR_ARG: dns client not initialized or invalid hostname
00711  *         ERR_VAL: dns server response was invalid
00712  */
00713 err_t
00714 netconn_gethostbyname(const char *name, ip_addr_t *addr)
00715 {
00716   struct dns_api_msg msg;
00717   err_t err;
00718   sys_sem_t sem;
00719 
00720   LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
00721   LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
00722 
00723   err = sys_sem_new(&sem, 0);
00724   if (err != ERR_OK) {
00725     return err;
00726   }
00727 
00728   msg.name = name;
00729   msg.addr = addr;
00730   msg.err = &err;
00731   msg.sem = &sem;
00732 
00733   tcpip_callback(do_gethostbyname, &msg);
00734   sys_sem_wait(&sem);
00735   sys_sem_free(&sem);
00736 
00737   return err;
00738 }
00739 #endif /* LWIP_DNS*/
00740 
00741 #endif /* LWIP_NETCONN */