📄 api_lib.c
字号:
/* Let the stack know that we have accepted the connection. */ struct api_msg msg; msg.function = do_recv; msg.msg.conn = conn; TCPIP_APIMSG(&msg); }#endif /* TCP_LISTEN_BACKLOG */ } return newconn;}/** * Receive data (in form of a netbuf containing a packet buffer) from a netconn * * @param conn the netconn from which to receive data * @return a new netbuf containing received data or NULL on memory error or timeout */struct netbuf *netconn_recv(struct netconn *conn){ struct api_msg msg; struct netbuf *buf = NULL; struct pbuf *p; u16_t len; LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return NULL;); if (conn->recvmbox == SYS_MBOX_NULL) { /* @todo: should calling netconn_recv on a TCP listen conn be fatal (ERR_CONN)?? */ /* TCP listen conns don't have a recvmbox! */ conn->err = ERR_CONN; return NULL; } if (ERR_IS_FATAL(conn->err)) { return NULL; } if (conn->type == NETCONN_TCP) {#if LWIP_TCP if (conn->state == NETCONN_LISTEN) { /* @todo: should calling netconn_recv on a TCP listen conn be fatal?? */ conn->err = ERR_CONN; return NULL; } buf = memp_malloc(MEMP_NETBUF); if (buf == NULL) { conn->err = ERR_MEM; return NULL; }#if LWIP_SO_RCVTIMEO if (sys_arch_mbox_fetch(conn->recvmbox, (void *)&p, conn->recv_timeout)==SYS_ARCH_TIMEOUT) { conn->err = ERR_TIMEOUT; p = NULL; }#else sys_arch_mbox_fetch(conn->recvmbox, (void *)&p, 0);#endif /* LWIP_SO_RCVTIMEO*/ if (p != NULL) { len = p->tot_len; SYS_ARCH_DEC(conn->recv_avail, len); } else { len = 0; } /* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVMINUS, len); /* If we are closed, we indicate that we no longer wish to use the socket */ if (p == NULL) { memp_free(MEMP_NETBUF, buf); /* Avoid to lose any previous error code */ if (conn->err == ERR_OK) { conn->err = ERR_CLSD; } return NULL; } buf->p = p; buf->ptr = p; buf->port = 0; buf->addr = NULL; /* Let the stack know that we have taken the data. */ msg.function = do_recv; msg.msg.conn = conn; if (buf != NULL) { msg.msg.msg.r.len = buf->p->tot_len; } else { msg.msg.msg.r.len = 1; } TCPIP_APIMSG(&msg);#endif /* LWIP_TCP */ } else {#if (LWIP_UDP || LWIP_RAW)#if LWIP_SO_RCVTIMEO if (sys_arch_mbox_fetch(conn->recvmbox, (void *)&buf, conn->recv_timeout)==SYS_ARCH_TIMEOUT) { buf = NULL; }#else sys_arch_mbox_fetch(conn->recvmbox, (void *)&buf, 0);#endif /* LWIP_SO_RCVTIMEO*/ if (buf!=NULL) { SYS_ARCH_DEC(conn->recv_avail, buf->p->tot_len); /* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len); }#endif /* (LWIP_UDP || LWIP_RAW) */ } LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err)); return buf;}/** * 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, struct ip_addr *addr, u16_t port){ if (buf != NULL) { 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; LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len)); msg.function = do_send; msg.msg.conn = conn; msg.msg.msg.b = buf; TCPIP_APIMSG(&msg); return conn->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 (0x01) data will be copied into memory belonging to the stack * - NETCONN_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent * @return ERR_OK if data was sent, any other err_t on error */err_tnetconn_write(struct netconn *conn, const void *dataptr, int size, u8_t apiflags){ struct api_msg msg; 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;); 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. */ TCPIP_APIMSG(&msg); return conn->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){ struct api_msg msg; LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_close; msg.msg.conn = conn; tcpip_apimsg(&msg); return conn->err;}#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 interface 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, struct ip_addr *multiaddr, struct ip_addr *interface, enum netconn_igmp join_or_leave){ struct api_msg msg; 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.interface = interface; msg.msg.msg.jl.join_or_leave = join_or_leave; TCPIP_APIMSG(&msg); return conn->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 struct ip_addr 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, struct ip_addr *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;); sem = sys_sem_new(0); if (sem == SYS_SEM_NULL) { return ERR_MEM; } 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 + -