static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *handle, nsapi_addr_t *addr, uint16_t *port) { struct lwip_socket *s = (struct lwip_socket *)server; struct lwip_socket *ns = mbed_lwip_arena_alloc(); if (!ns) { return NSAPI_ERROR_NO_SOCKET; } if (s->conn->pcb.tcp->state != LISTEN) { return NSAPI_ERROR_PARAMETER; } err_t err = netconn_accept(s->conn, &ns->conn); if (err != ERR_OK) { mbed_lwip_arena_dealloc(ns); return mbed_lwip_err_remap(err); } netconn_set_recvtimeout(ns->conn, 1); *(struct lwip_socket **)handle = ns; ip_addr_t peer_addr; (void) netconn_peer(ns->conn, &peer_addr, port); convert_lwip_addr_to_mbed(addr, &peer_addr); netconn_set_nonblocking(ns->conn, true); return 0; }
static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto) { // check if network is connected if (lwip_connected == NSAPI_STATUS_DISCONNECTED) { return NSAPI_ERROR_NO_CONNECTION; } // allocate a socket struct lwip_socket *s = mbed_lwip_arena_alloc(); if (!s) { return NSAPI_ERROR_NO_SOCKET; } enum netconn_type lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP; #if LWIP_IPV6 // Enable IPv6 (or dual-stack) lwip_proto |= NETCONN_TYPE_IPV6; #endif s->conn = netconn_new_with_callback(lwip_proto, mbed_lwip_socket_callback); if (!s->conn) { mbed_lwip_arena_dealloc(s); return NSAPI_ERROR_NO_SOCKET; } netconn_set_recvtimeout(s->conn, 1); *(struct lwip_socket **)handle = s; return 0; }
static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto) { // check if network is connected if (!lwip_connected) { return NSAPI_ERROR_NO_CONNECTION; } // allocate a socket struct lwip_socket *s = mbed_lwip_arena_alloc(); if (!s) { return NSAPI_ERROR_NO_SOCKET; } u8_t lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP; #if LWIP_IPV6 && LWIP_IPV4 const ip_addr_t *ip_addr; ip_addr = mbed_lwip_get_ip_addr(true, &lwip_netif); if (IP_IS_V6(ip_addr)) { // Enable IPv6 (or dual-stack). LWIP dual-stack support is // currently incomplete as of 2.0.0rc2 - eg we will only be able // to do a UDP sendto to an address matching the type selected // here. Matching "get_ip_addr" and DNS logic, use v4 if // available. lwip_proto |= NETCONN_TYPE_IPV6; } #elif LWIP_IPV6 lwip_proto |= NETCONN_TYPE_IPV6; #endif s->conn = netconn_new_with_callback(lwip_proto, mbed_lwip_socket_callback); if (!s->conn) { mbed_lwip_arena_dealloc(s); return NSAPI_ERROR_NO_SOCKET; } netconn_set_recvtimeout(s->conn, 1); *(struct lwip_socket **)handle = s; return 0; }