source: UsbWattMeter/trunk/lwip-1.4.1/src/api/api_lib.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

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