📄 api_lib.c
字号:
#endif /* LWIP_SO_RCVTIMEO*/#if LWIP_TCP if (conn->type == NETCONN_TCP) { if (!netconn_get_noautorecved(conn) || (buf == NULL)) { /* Let the stack know that we have taken the data. */ /* TODO: Speedup: Don't block and wait for the answer here (to prevent multiple thread-switches). */ msg.function = do_recv; msg.msg.conn = conn; if (buf != NULL) { msg.msg.msg.r.len = ((struct pbuf *)buf)->tot_len; } else { msg.msg.msg.r.len = 1; } /* don't care for the return value of do_recv */ TCPIP_APIMSG(&msg); } /* If we are closed, we indicate that we no longer wish to use the socket */ if (buf == NULL) { API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0); /* Avoid to lose any previous error code */ NETCONN_SET_SAFE_ERR(conn, ERR_CLSD); return ERR_CLSD; } len = ((struct pbuf *)buf)->tot_len; }#endif /* LWIP_TCP */#if LWIP_TCP && (LWIP_UDP || LWIP_RAW) else#endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */#if (LWIP_UDP || LWIP_RAW) { LWIP_ASSERT("buf != NULL", buf != NULL); len = netbuf_len((struct netbuf *)buf); }#endif /* (LWIP_UDP || LWIP_RAW) */#if LWIP_SO_RCVBUF SYS_ARCH_DEC(conn->recv_avail, len);#endif /* LWIP_SO_RCVBUF */ /* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVMINUS, len); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len)); *new_buf = buf; /* don't set conn->last_err: it's only ERR_OK, anyway */ return ERR_OK;}/** * Receive data (in form of a pbuf) from a TCP netconn * * @param conn the netconn from which to receive data * @param new_buf pointer where a new pbuf is stored when received data * @return ERR_OK if data has been received, an error code otherwise (timeout, * memory error or another error) * ERR_ARG if conn is not a TCP netconn */err_tnetconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf){ LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) && netconn_type(conn) == NETCONN_TCP, return ERR_ARG;); return netconn_recv_data(conn, (void **)new_buf);}/** * Receive data (in form of a netbuf containing a packet buffer) from a netconn * * @param conn the netconn from which to receive data * @param new_buf pointer where a new netbuf is stored when received data * @return ERR_OK if data has been received, an error code otherwise (timeout, * memory error or another error) */err_tnetconn_recv(struct netconn *conn, struct netbuf **new_buf){#if LWIP_TCP struct netbuf *buf = NULL; err_t err;#endif /* LWIP_TCP */ LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;); *new_buf = NULL; LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);#if LWIP_TCP if (conn->type == NETCONN_TCP) { struct pbuf *p = NULL; /* This is not a listening netconn, since recvmbox is set */ buf = (struct netbuf *)memp_malloc(MEMP_NETBUF); if (buf == NULL) { NETCONN_SET_SAFE_ERR(conn, ERR_MEM); return ERR_MEM; } err = netconn_recv_data(conn, (void **)&p); if (err != ERR_OK) { memp_free(MEMP_NETBUF, buf); return err; } LWIP_ASSERT("p != NULL", p != NULL); buf->p = p; buf->ptr = p; buf->port = 0; ip_addr_set_any(&buf->addr); *new_buf = buf; /* don't set conn->last_err: it's only ERR_OK, anyway */ return ERR_OK; } else#endif /* LWIP_TCP */ {#if (LWIP_UDP || LWIP_RAW) return netconn_recv_data(conn, (void **)new_buf);#endif /* (LWIP_UDP || LWIP_RAW) */ }}/** * TCP: update the receive window: by calling this, the application * tells the stack that it has processed data and is able to accept * new data. * ATTENTION: use with care, this is mainly used for sockets! * Can only be used when calling netconn_set_noautorecved(conn, 1) before. * * @param conn the netconn for which to update the receive window * @param length amount of data processed (ATTENTION: this must be accurate!) */voidnetconn_recved(struct netconn *conn, u32_t length){#if LWIP_TCP if ((conn != NULL) && (conn->type == NETCONN_TCP) && (netconn_get_noautorecved(conn))) { struct api_msg msg; /* Let the stack know that we have taken the data. */ /* TODO: Speedup: Don't block and wait for the answer here (to prevent multiple thread-switches). */ msg.function = do_recv; msg.msg.conn = conn; msg.msg.msg.r.len = length; /* don't care for the return value of do_recv */ TCPIP_APIMSG(&msg); }#else /* LWIP_TCP */ LWIP_UNUSED_ARG(conn); LWIP_UNUSED_ARG(length);#endif /* LWIP_TCP */}/** * Send data (in form of a netbuf) to a specific remote IP address and port. * Only to be used for UDP and RAW netconns (not TCP). * * @param conn the netconn over which to send data * @param buf a netbuf containing the data to send * @param addr the remote IP address to which to send the data * @param port the remote port to which to send the data * @return ERR_OK if data was sent, any other err_t on error */err_tnetconn_sendto(struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port){ if (buf != NULL) { ip_addr_set(&buf->addr, addr); buf->port = port; return netconn_send(conn, buf); } return ERR_VAL;}/** * Send data over a UDP or RAW netconn (that is already connected). * * @param conn the UDP or RAW netconn over which to send data * @param buf a netbuf containing the data to send * @return ERR_OK if data was sent, any other err_t on error */err_tnetconn_send(struct netconn *conn, struct netbuf *buf){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len)); msg.function = do_send; msg.msg.conn = conn; msg.msg.msg.b = buf; err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Send data over a TCP netconn. * * @param conn the TCP netconn over which to send data * @param dataptr pointer to the application buffer that contains the data to send * @param size size of the application data to send * @param apiflags combination of following flags : * - NETCONN_COPY: data will be copied into memory belonging to the stack * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once * @return ERR_OK if data was sent, any other err_t on error */err_tnetconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_ERROR("netconn_write: invalid conn->type", (conn->type == NETCONN_TCP), return ERR_VAL;); if (size == 0) { return ERR_OK; } /* @todo: for non-blocking write, check if 'size' would ever fit into snd_queue or snd_buf */ msg.function = do_write; msg.msg.conn = conn; msg.msg.msg.w.dataptr = dataptr; msg.msg.msg.w.apiflags = apiflags; msg.msg.msg.w.len = size; /* For locking the core: this _can_ be delayed on low memory/low send buffer, but if it is, this is done inside api_msg.c:do_write(), so we can use the non-blocking version here. */ err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Close ot shutdown a TCP netconn (doesn't delete it). * * @param conn the TCP netconn to close or shutdown * @param how fully close or only shutdown one side? * @return ERR_OK if the netconn was closed, any other err_t on error */static err_tnetconn_close_shutdown(struct netconn *conn, u8_t how){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_close; msg.msg.conn = conn; /* shutting down both ends is the same as closing */ msg.msg.msg.sd.shut = how; /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close, don't use TCPIP_APIMSG here */ err = tcpip_apimsg(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Close a TCP netconn (doesn't delete it). * * @param conn the TCP netconn to close * @return ERR_OK if the netconn was closed, any other err_t on error */err_tnetconn_close(struct netconn *conn){ /* shutting down both ends is the same as closing */ return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);}/** * Shut down one or both sides of a TCP netconn (doesn't delete it). * * @param conn the TCP netconn to shut down * @return ERR_OK if the netconn was closed, any other err_t on error */err_tnetconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx){ return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));}#if LWIP_IGMP/** * Join multicast groups for UDP netconns. * * @param conn the UDP netconn for which to change multicast addresses * @param multiaddr IP address of the multicast group to join or leave * @param netif_addr the IP address of the network interface on which to send * the igmp message * @param join_or_leave flag whether to send a join- or leave-message * @return ERR_OK if the action was taken, any err_t on error */err_tnetconn_join_leave_group(struct netconn *conn, ip_addr_t *multiaddr, ip_addr_t *netif_addr, enum netconn_igmp join_or_leave){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_join_leave_group; msg.msg.conn = conn; msg.msg.msg.jl.multiaddr = multiaddr; msg.msg.msg.jl.netif_addr = netif_addr; msg.msg.msg.jl.join_or_leave = join_or_leave; err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}#endif /* LWIP_IGMP */#if LWIP_DNS/** * Execute a DNS query, only one IP address is returned * * @param name a string representation of the DNS host name to query * @param addr a preallocated ip_addr_t where to store the resolved IP address * @return ERR_OK: resolving succeeded * ERR_MEM: memory error, try again later * ERR_ARG: dns client not initialized or invalid hostname * ERR_VAL: dns server response was invalid */err_tnetconn_gethostbyname(const char *name, ip_addr_t *addr){ struct dns_api_msg msg; err_t err; sys_sem_t sem; LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;); LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;); err = sys_sem_new(&sem, 0); if (err != ERR_OK) { return err; } msg.name = name; msg.addr = addr; msg.err = &err; msg.sem = &sem; tcpip_callback(do_gethostbyname, &msg); sys_sem_wait(&sem); sys_sem_free(&sem); return err;}#endif /* LWIP_DNS*/#endif /* LWIP_NETCONN */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -