/* LWIP network stack implementation */ static nsapi_addr_t lwip_getaddr(nsapi_stack_t *stack) { if (!lwip_get_ip_address()) { return (nsapi_addr_t){0}; } nsapi_addr_t addr; addr.version = NSAPI_IPv4; inet_aton(lwip_get_ip_address(), (ip_addr_t *)addr.bytes); return addr; }
static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto) { // check if network is connected if (!lwip_get_ip_address()) { return NSAPI_ERROR_NO_CONNECTION; } // allocate a socket struct lwip_socket *s = lwip_arena_alloc(); if (!s) { return NSAPI_ERROR_NO_SOCKET; } s->conn = netconn_new_with_callback( proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP, lwip_socket_callback); if (!s->conn) { lwip_arena_dealloc(s); return NSAPI_ERROR_NO_SOCKET; } netconn_set_recvtimeout(s->conn, 1); *(struct lwip_socket **)handle = s; return 0; }
int lwip_bringdown(void) { // Check if we've connected if (!lwip_get_ip_address()) { return NSAPI_ERROR_PARAMETER; } // Disconnect from the network dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_ip_addr[0] = '\0'; return 0; }
int lwip_bringup(void) { // Check if we've already connected if (lwip_get_ip_address()) { return NSAPI_ERROR_PARAMETER; } // Check if we've already brought up lwip if (!lwip_get_mac_address()) { // Set up network lwip_set_mac_address(); sys_sem_new(&lwip_tcpip_inited, 0); sys_sem_new(&lwip_netif_linked, 0); sys_sem_new(&lwip_netif_up, 0); tcpip_init(lwip_tcpip_init_irq, NULL); sys_arch_sem_wait(&lwip_tcpip_inited, 0); memset(&lwip_netif, 0, sizeof lwip_netif); netif_add(&lwip_netif, 0, 0, 0, NULL, eth_arch_enetif_init, tcpip_input); netif_set_default(&lwip_netif); netif_set_link_callback (&lwip_netif, lwip_netif_link_irq); netif_set_status_callback(&lwip_netif, lwip_netif_status_irq); eth_arch_enable_interrupts(); } // Zero out socket set lwip_arena_init(); // Connect to the network dhcp_start(&lwip_netif); // Wait for an IP Address u32_t ret = sys_arch_sem_wait(&lwip_netif_up, 15000); if (ret == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DHCP_FAILURE; } return 0; }
const char *EthernetInterface::get_ip_address() { return lwip_get_ip_address(); }