/** * @brief http server thread * @param arg: pointer on argument(not used here) * @retval None */ static void http_ipcam_thread(void *arg) { // struct netconn *conn, *newconn; static __IO uint8_t connflag = 0; if(connflag == 0) { /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); /* Bind to port 80 (HTTP) with default IP address */ netconn_bind(conn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(conn); connflag = 1; } /* Infinite loop */ for ( ;;) { /* Accept any icoming connection */ newconn = netconn_accept(conn); if(newconn != NULL) { /* Serve the icoming connection */ http_ipcam_serve(newconn); } } }
int lwip_bind(int s, struct sockaddr *name, socklen_t namelen) { struct lwip_socket *sock; struct ip_addr local_addr; u16_t local_port; err_t err; sock = get_socket(s); if (!sock) { set_errno(EBADF); return -1; } local_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr; local_port = ((struct sockaddr_in *)name)->sin_port; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s)); ip_addr_debug_print(SOCKETS_DEBUG, &local_addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(local_port))); err = netconn_bind(sock->conn, &local_addr, ntohs(local_port)); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s)); sock_set_errno(sock, 0); return 0; }
static void uctsk_LWIP(void *pdata) { struct netconn *__pstConn, *__pstNewConn; //add by rezaee OSTimeDlyHMSM(0, 0, 0, 50); /* 500 MS */ Init_lwIP(); __pstConn = netconn_new(NETCONN_TCP); netconn_bind(__pstConn, NULL,80); netconn_listen(__pstConn); for(;;) { __pstNewConn = netconn_accept(__pstConn); if(__pstNewConn != NULL) { vHandler_HTTP(__pstNewConn); while(netconn_delete(__pstNewConn) != ERR_OK) { OSTimeDlyHMSM(0, 0, 0, 10); } } } }
bool LwIPTcpServer::listen(const QHostAddress &address, quint16 port) { Q_D(LwIPTcpServer); if(d->conn == 0) { d->conn = LwIPDispatcher::createConn(NETCONN_TCP, this); if(d->conn == 0) { return false; } struct ip_addr addr; addr.addr = qToBigEndian(address.toIPv4Address()); if(netconn_bind(d->conn, &addr, port) != ERR_OK) goto failure; if(netconn_listen(d->conn) != ERR_OK) goto failure; d->serverAddress = address; d->serverPort = port; return true; } else return false; failure: LwIPDispatcher::disposeConn(d->conn); d->conn = 0; return false; }
/*-----------------------------------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, 7); /* Tell connection to go into listening mode. */ netconn_listen(conn); memset(data, 'F', DATA_SIZE); while (1) { /* Grab new connection. */ err = netconn_accept(conn, &newconn); /*printf("accepted new connection %p\n", newconn);*/ /* Process the new connection. */ if (err == ERR_OK) { err = netconn_write(newconn, data, DATA_SIZE, NETCONN_COPY); if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } netconn_close(newconn); netconn_delete(newconn); } } }
/** * @brief http server thread * @param arg: pointer on argument(not used here) * @retval None */ static void http_server_netconn_thread(void *arg) { struct netconn *conn, *newconn; err_t err, accept_err; /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); if (conn!= NULL) { /* Bind to port 80 (HTTP) with default IP address */ err = netconn_bind(conn, NULL, 80); if (err == ERR_OK) { /* Put the connection into LISTEN state */ netconn_listen(conn); while(1) { /* accept any icoming connection */ accept_err = netconn_accept(conn, &newconn); if(accept_err == ERR_OK) { /* serve connection */ http_server_serve(newconn); /* delete connection */ netconn_delete(newconn); } } } } }
portTASK_FUNCTION( vBasicWEBServer, pvParameters ) { struct netconn *pxHTTPListener, *pxNewConnection; /* Create a new tcp connection handle */ pxHTTPListener = netconn_new( NETCONN_TCP ); netconn_bind(pxHTTPListener, NULL, webHTTP_PORT ); netconn_listen( pxHTTPListener ); /* Loop forever */ for( ;; ) { #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) ) /* Wait for a first connection. */ pxNewConnection = netconn_accept(pxHTTPListener); #else while(netconn_accept(pxHTTPListener, &pxNewConnection) != ERR_OK) { vTaskDelay( webSHORT_DELAY ); } #endif vParTestSetLED(webCONN_LED, pdTRUE); if(pxNewConnection != NULL) { prvweb_ParseHTMLRequest(pxNewConnection); }/* end if new connection */ vParTestSetLED(webCONN_LED, pdFALSE); } /* end infinite loop */ }
/*-----------------------------------------------------------------------------------*/ static void udpecho_thread(void *arg) { err_t err, recv_err; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); if (conn!= NULL) { err = netconn_bind(conn, IP_ADDR_ANY, 7); if (err == ERR_OK) { while (1) { recv_err = netconn_recv(conn, &buf); if (recv_err == ERR_OK) { addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); netconn_connect(conn, addr, port); buf->addr.addr = 0; netconn_send(conn,buf); netbuf_delete(buf); } } } else { netconn_delete(conn); } } }
/*-----------------------------------------------------------------------------------*/ static void udpecho_thread(void *arg) { static struct netconn *conn; static struct netbuf *buf; static struct ip_addr *addr; static unsigned short port; char buffer[256]; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); /* Create a new netconnect */ LWIP_ASSERT("con != NULL", conn != NULL); netconn_bind(conn, IP_ADDR_ANY, UDP_ECHO_PORT); /* udpecho using port 7 */ while (1) { buf = netconn_recv(conn); /* received data to buffer */ if (buf != NULL) { addr = netbuf_fromaddr(buf); /* get client's IP address */ port = netbuf_fromport(buf); /* get client's port */ netconn_connect(conn, addr, port); /* connect to client */ netbuf_copy(buf, buffer, buf->p->tot_len); /* set echo data */ buffer[buf->p->tot_len] = '\0'; buf->addr = NULL; netconn_send(conn, buf); /* send data back to client */ LWIP_DEBUGF(LWIP_DBG_ON, ("got %s\n", buffer)); netbuf_delete(buf); /* release buffer */ } } }
/** SNMP netconn API worker thread */ static void snmp_netconn_thread(void *arg) { struct netconn *conn; struct netbuf *buf; err_t err; LWIP_UNUSED_ARG(arg); /* Bind to SNMP port with default IP address */ #if LWIP_IPV6 conn = netconn_new(NETCONN_UDP_IPV6); netconn_bind(conn, IP6_ADDR_ANY, SNMP_IN_PORT); #else /* LWIP_IPV6 */ conn = netconn_new(NETCONN_UDP); netconn_bind(conn, IP4_ADDR_ANY, SNMP_IN_PORT); #endif /* LWIP_IPV6 */ LWIP_ERROR("snmp_netconn: invalid conn", (conn != NULL), return;);
void serial_init(int baudrate) { lwip_init(); ser_conn = netconn_new(NETCONN_UDP); netconn_bind(ser_conn, IP_ADDR_ANY, 1441); buf = netbuf_new(); putc_pos = oob_pos = 0; }
/** Tests the fragmented IP packets. * @bug Doesn't pass yet. */ char* fragmented_packet_test(void) { ip_addr_t destination, self_ip; printf("%s()\n", __FUNCTION__); struct netconn *conn; err_t err; struct netbuf *buf; void *data; u16_t len; /* Payload longer than MTU, should get split. */ char test_str[2001]; test_str[0] = 0; char data_str[2001]; data_str[0] = 0; /* Fills the data pattern. */ while (strlen(test_str) < 1900) strcat(test_str, "data"); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Sets the device we want to connect to. */ IP4_ADDR(&destination, 10, 0, 0, 2); IP4_ADDR(&self_ip, 10, 0, 0, 3); /* Bind connection to well known port number 7. */ netconn_bind(conn, &self_ip, 1235); printf("Connecting...\n"); err = netconn_connect(conn, &destination, 1235); TEST_ASSERT("TCP connection failed.", err == ERR_OK); /* Don't send final \0 */ err = netconn_write(conn, test_str, strlen(test_str), NETCONN_NOCOPY); TEST_ASSERT("Netconn write failed.\n", err == ERR_OK); /* Reads whole response. */ int sum=0; while(sum < strlen(test_str) && (err = netconn_recv(conn, &buf)) == ERR_OK) { do { netbuf_data(buf, &data, &len); strncat(data_str, data, len); sum += len; } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } TEST_ASSERT("Data is not echoed correctly", !strcmp(data_str, test_str)); netconn_close(conn); return TEST_SUCCESS; }
/* Private functions ---------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err, accept_err; struct netbuf *buf; void *data; u16_t len; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); if (conn!=NULL) { /* Bind connection to well known port number 7. */ err = netconn_bind(conn, NULL, 7); if (err == ERR_OK) { /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ accept_err = netconn_accept(conn, &newconn); /* Process the new connection. */ if (accept_err == ERR_OK) { HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET); while (netconn_recv(newconn, &buf) == ERR_OK) { do { netbuf_data(buf, &data, &len); netconn_write(newconn, data, len, NETCONN_COPY); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET); } } } else { netconn_delete(newconn); } } }
static void tcptask(void* arg) { static uint16_t port = 30000; HTTPClient* self = (HTTPClient*) arg; TCPData tcpData; for (;;) { if (xQueueReceive(self->qHandle, &tcpData, 100)) { port++; struct netconn *conn = netconn_new(NETCONN_TCP); err_t err; if (conn != NULL) { // Bind connection to the specified number os_printf("Binding port %d\n", port); err = netconn_bind(conn, NULL, port); if (err == ERR_OK) { struct ip_addr ip; ip.addr = tcpData.serverIP; os_printf("Connecting port %d\n", tcpData.serverPort); err = netconn_connect (conn, &ip, tcpData.serverPort); if (err == ERR_OK) { os_printf("Writing data!\n"); netconn_write(conn, tcpData.data, TCP_DATA_SIZE, NETCONN_COPY); struct netbuf *buf; char *data; u16_t len; uint32_t offset = 0; if ((buf = netconn_recv(conn)) != NULL) { do { netbuf_data(buf, (void**)&data, &len); if (self->rxBuffer != NULL && data != NULL) memcpy(self->rxBuffer + offset, data, len); else os_printf("HTTPClient::tcpTask self->rxBuffer or data is NULL!\n"); offset += len; os_printf("Netconn received %d bytes\n", len); } while (netbuf_next(buf) >= 0); self->onReceive(tcpData.receiveCallback, tcpData.obj, self->rxBuffer); netbuf_delete(buf); } } } } netconn_close (conn ); netconn_delete (conn ); } } vTaskDelete(NULL); }
void vBasicWEBServer( void *pvParameters ) { struct netconn *pxNewConnection; //struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); int ret = ERR_OK; /* Parameters are not used - suppress compiler error. */ ( void )pvParameters; /* Create a new tcp connection handle */ pxHTTPListener = netconn_new( NETCONN_TCP ); ip_set_option(pxHTTPListener->pcb.ip, SOF_REUSEADDR); netconn_bind( pxHTTPListener, NULL, webHTTP_PORT ); netconn_listen( pxHTTPListener ); #if CONFIG_READ_FLASH /* Load wifi_config */ LoadWifiConfig(); RestartSoftAP(); #endif //printf("\r\n-0\n"); /* Loop forever */ for( ;; ) { if(webs_terminate) break; //printf("\r\n%d:-1\n", xTaskGetTickCount()); /* Wait for connection. */ // Evan mopdified for adapt two version lwip api diff port_netconn_accept( pxHTTPListener , pxNewConnection, ret); //printf("\r\n%d:-2\n", xTaskGetTickCount()); if( pxNewConnection != NULL && ret == ERR_OK) { /* Service connection. */ vProcessConnection( pxNewConnection ); while( netconn_delete( pxNewConnection ) != ERR_OK ) { vTaskDelay( webSHORT_DELAY ); } } //printf("\r\n%d:-3\n", xTaskGetTickCount()); } //printf("\r\n-4\n"); if(pxHTTPListener) { netconn_close(pxHTTPListener); netconn_delete(pxHTTPListener); pxHTTPListener = NULL; } //printf("\r\nExit Web Server Thread!\n"); xSemaphoreGive(webs_sema); }
static int lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) { struct lwip_socket *s = (struct lwip_socket *)handle; if (addr.version != NSAPI_IPv4) { return NSAPI_ERROR_PARAMETER; } err_t err = netconn_bind(s->conn, (ip_addr_t *)addr.bytes, port); return lwip_err_remap(err); }
void tcp_task(void) { struct netconn *conn, *newconn; err_t err; conn = netconn_new(NETCONN_TCP); if(conn != NULL) { err = netconn_bind(conn, NULL, 7); if (err == ERR_OK) { /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ newconn = netconn_accept(conn); /* Process the new connection. */ if (newconn) { struct netbuf *buf; void *data; u16_t len; while ((buf = netconn_recv(newconn)) != NULL) { do { netbuf_data(buf, &data, &len); netconn_write(newconn, data, len, NETCONN_COPY); } while(netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } } else { printf(" can not bind TCP netconn"); } } else { printf("can not create TCP netconn"); } }
msg_t data_udp_send_thread(void *p) { void * arg __attribute__ ((unused)) = p; err_t err; uint8_t count = 0; struct netconn *conn; struct netbuf *buf; char* data; char msg[DATA_UDP_MSG_SIZE] ; ip_addr_t ip_addr_sensor; ip_addr_t ip_addr_fc; IMU_A_IP_ADDR(&ip_addr_sensor); IP_PSAS_FC(&ip_addr_fc); chRegSetThreadName("data_udp_send_thread"); conn = netconn_new( NETCONN_UDP ); /* Bind to the local address, or to ANY address */ // netconn_bind(conn, NULL, DATA_UDP_TX_THREAD_PORT ); //local port, NULL is bind to ALL ADDRESSES! (IP_ADDR_ANY) err = netconn_bind(conn, &ip_addr_sensor, IMU_A_TX_PORT ); //local port if (err == ERR_OK) { /* Connect to specific address or a broadcast address */ /* * \todo Understand why a UDP needs a connect... * This may be a LwIP thing that chooses between tcp_/udp_/raw_ connections internally. * */ // netconn_connect(conn, IP_ADDR_BROADCAST, DATA_UDP_TX_THREAD_PORT ); err = netconn_connect(conn, &ip_addr_fc, FC_LISTEN_PORT_IMU_A ); if(err == ERR_OK) { for( ;; ){ buf = netbuf_new(); data = netbuf_alloc(buf, sizeof(msg)); sprintf(msg, "sensor tx: %d", count++); memcpy (data, msg, sizeof (msg)); netconn_send(conn, buf); netbuf_delete(buf); // De-allocate packet buffer chThdSleepMilliseconds(500); } return RDY_OK; } else { return RDY_RESET; } } else { return RDY_RESET; } }
static void run_webserver(void *p) { struct ip_addr listenaddr = { 0 }; struct netconn *listener; struct netconn *session; struct timeval tv; err_t rc; DEBUG("Opening connection fir webserver \n"); listener = netconn_new(NETCONN_TCP); DEBUG("Connection at %x\n", &listener); rc = netconn_bind(listener, &listenaddr, 80); if (rc != ERR_OK) { DEBUG("Failed to bind connection: %i\n", rc); return; } rc = netconn_listen(listener); if (rc != ERR_OK) { DEBUG("Failed to listen on connection: %i\n", rc); return; } DEBUG("sucessfully listening the webserver \n"); while (1) { int i; session = netconn_accept(listener); if (session == NULL) continue; ut_sprintf(message, "<html><body><pre> Jiny Kernel Dmesg max_len:%d curr_len:%d \n",MAX_DMESG_LOG,g_dmesg_index); (void) netconn_write(session, message, ut_strlen(message), NETCONN_COPY); i=0; while (i<g_dmesg_index) { if (g_dmesg_index < MAX_DMESG_LOG) (void) netconn_write(session, &g_dmesg[i],100, NETCONN_COPY); i=i+100; } ut_sprintf(message, "</pre></body></html>"); (void) netconn_write(session, message, ut_strlen(message), NETCONN_COPY); (void) netconn_disconnect(session); (void) netconn_delete(session); } }
/** * Init tcp server whit kfile interface. * * Start a tcp server on registered ethernet interface, and it calls the user handler * when receive data from accepted connection socket. * * \note Use the tcpsocket_serverPoll() function to process al connections. * * \param socket tcp socket context. * \param local_addr device ip address. * \param listen_addr ip address to listen * \param port tcp socket port to listen * \param handler user handler that server calls every time we recive a data from * socket. To the handler the server will pass the kfile context and user could choose * to make an explicit connection close, otherwise the server keep alive the connection. */ void tcpsocket_serverInit(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *listen_addr, uint16_t port, tcphandler_t handler) { tcpsocket_init(socket, local_addr, listen_addr, port); socket->handler = handler; socket->server_sock = netconn_new(NETCONN_TCP); socket->error = netconn_bind(socket->server_sock, listen_addr, port); socket->error = netconn_listen(socket->server_sock); if(socket->error != ERR_OK) LOG_ERR("Init server\n"); }
/** * \brief HTTP task core function. * * \param pvParameters Junk parameter. */ static void http_task(void *pvParameters) { struct netconn *conn, *newconn; err_t err; /* Just to avoid compiler warnings. */ UNUSED(pvParameters); /* Wait for user to read instructions. */ WAIT_FOR_TOUCH_EVENT; #if LWIP_STATS configure_timer_for_bandwidth_stats(); #endif /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); if (conn == NULL) { printf("http_task: invalid conn\n"); /* Delete the calling task. */ vTaskDelete(NULL); } /* Bind to port 80 (HTTP) with default IP address */ netconn_bind(conn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(conn); do { err = netconn_accept(conn, &newconn); if (err == ERR_OK) { /* Try to instanciate a new HTTP-req task to handle the HTTP request. */ if (NULL == sys_thread_new("HTTP-req", http_request, newconn, mainHTTP_TASK_STACK_SIZE, mainHTTP_TASK_PRIORITY)) { /* Failed to instanciate task, free netconn socket. */ netconn_close(newconn); netconn_delete(newconn); } } } while (err == ERR_OK); printf("http_task: netconn_accept received error %d, shutting down\n", err); /* Free Netconn resource. */ netconn_close(conn); netconn_delete(conn); /* Delete the calling task. */ vTaskDelete(NULL); }
void ICACHE_FLASH_ATTR _setBulbState(int idx, uint16_t state) { struct netconn * sendconn; struct netbuf * sendbuf; char * data; err_t ret; if (!network_ready) return; sendconn = netconn_new( NETCONN_UDP ); if (sendconn == NULL) { os_printf("[setbulb] Couldn't get netconn\r\n"); return; } sendbuf = netbuf_new(); if (sendbuf == NULL) { os_printf("[setbulb] couldn't get sendbuff\r\n"); return; } data = netbuf_alloc(sendbuf, 38); if (data == NULL) { os_printf("[setbulb] couldn't alloc data\r\n"); return; } getPktSetBulbPower(data, 38, idx, state); ret = netconn_bind(sendconn, &myaddr, 34435); if (ret != ERR_OK) { os_printf("[setbulb] connect error: %d\r\n", ret); return; } netconn_set_recvtimeout( sendconn, 1000); ret = netconn_connect(sendconn, getBulbAddr(idx), 56700); if (ret != ERR_OK) { os_printf("[setbulb] connect error: %d\r\n", ret); return; } ret = netconn_send(sendconn, sendbuf); if (ret != ERR_OK) { os_printf("[setbulb] send error: %d\r\n", ret); } else { } netbuf_free(sendbuf); netbuf_delete(sendbuf); netconn_disconnect(sendconn); netconn_delete(sendconn); }
int main(void) { struct netconn *server; /* Hardware initialization */ init(); proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL); dhcp_start(&netif); /* * Here we wait for an ip address, but it's not strictly * necessary. The address is obtained in background and * as long as we don't use network functions, we could go * on with initialization */ while (!netif.ip_addr.addr) timer_delay(200); kprintf(">>> dhcp ok: ip = ip = %s (kernel %s)\n", ip_ntoa(&netif.ip_addr.addr), CONFIG_KERN_PREEMPT ? "preempt" : "coop"); server = netconn_new(NETCONN_TCP); netconn_bind(server, IP_ADDR_ANY, 80); netconn_listen(server); while (1) { struct netconn *client; struct netbuf *rx_buf_conn; char *rx_buf; u16_t len; client = netconn_accept(server); if (!client) continue; tot_req++; rx_buf_conn = netconn_recv(client); if (rx_buf_conn) { netbuf_data(rx_buf_conn, (void **)&rx_buf, &len); if (rx_buf) netconn_write(client, rx_buf, len, NETCONN_COPY); netbuf_delete(rx_buf_conn); } while (netconn_delete(client) != ERR_OK) cpu_relax(); } }
static int lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) { struct lwip_socket *s = (struct lwip_socket *)handle; if (addr.version != NSAPI_IPv4) { return NSAPI_ERROR_PARAMETER; } if ((s->conn->type == NETCONN_TCP && s->conn->pcb.tcp->local_port != 0) || (s->conn->type == NETCONN_UDP && s->conn->pcb.udp->local_port != 0)) { return NSAPI_ERROR_PARAMETER; } err_t err = netconn_bind(s->conn, (ip_addr_t *)addr.bytes, port); return lwip_err_remap(err); }
/*-----------------------------------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number TCP_ECHO_PORT. */ netconn_bind(conn, IP_ADDR_ANY, TCP_ECHO_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ newconn = netconn_accept(conn); printf("accepted new connection %p\n", newconn); /* Process the new connection. */ if (newconn != NULL) { struct netbuf *buf; void *data; u16_t len; while ((buf = netconn_recv(newconn)) != NULL) { printf("Recved\n"); do { netbuf_data(buf, &data, &len); err = netconn_write(newconn, data, len, NETCONN_COPY); #if 0 if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } #endif } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } printf("Got EOF, looping\n"); /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } }
void vBasicWEBServer( void *pvParameters ) { struct netconn *pxHTTPListener, *pxNewConnection; struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); static struct netif EMAC_if; /* Parameters are not used - suppress compiler error. */ ( void ) pvParameters; /* Create and configure the EMAC interface. */ IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3); IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3); IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3); netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input); /* make it the default interface */ netif_set_default(&EMAC_if); /* bring it up */ netif_set_up(&EMAC_if); /* Create a new tcp connection handle */ pxHTTPListener = netconn_new( NETCONN_TCP ); netconn_bind(pxHTTPListener, NULL, webHTTP_PORT ); netconn_listen( pxHTTPListener ); /* Loop forever */ for( ;; ) { /* Wait for connection. */ pxNewConnection = netconn_accept(pxHTTPListener); if(pxNewConnection != NULL) { /* Service connection. */ vProcessConnection( pxNewConnection ); while( netconn_delete( pxNewConnection ) != ERR_OK ) { vTaskDelay( webSHORT_DELAY ); } } } }
static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port) { struct lwip_socket *s = (struct lwip_socket *)handle; ip_addr_t ip_addr; if ((s->conn->type == NETCONN_TCP && s->conn->pcb.tcp->local_port != 0) || (s->conn->type == NETCONN_UDP && s->conn->pcb.udp->local_port != 0)) { return NSAPI_ERROR_PARAMETER; } if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { return NSAPI_ERROR_PARAMETER; } err_t err = netconn_bind(s->conn, &ip_addr, port); return mbed_lwip_err_remap(err); }
void uart_wifi_debug_init() { uart_wifi_debug_buf=netbuf_new(); // int ret; if(NULL == (uart_wifi_debug_base_conn = netconn_new(NETCONN_UDP))) { LOG_WARN( "conn new err...\r\n"); // ret = STATE_ERROR; } if(netconn_bind(uart_wifi_debug_base_conn, IP_ADDR_ANY, 1112) != ERR_OK) { LOG_WARN( "bind err...\r\n"); } }
msg_t data_udp_send_thread(void *p) { void * arg __attribute__ ((unused)) = p; static const evhandler_t evhndl_mpu9150[] = { data_udp_send_mpu9150_data }; struct EventListener evl_mpu9150; err_t err; ip_addr_t ip_addr_sensor; ip_addr_t ip_addr_fc; chRegSetThreadName("data_udp_send_thread"); chEvtRegister(&mpu9150_data_event, &evl_mpu9150, 0); IMU_A_IP_ADDR(&ip_addr_sensor); IP_PSAS_FC(&ip_addr_fc); mpu9150_mac_info.conn = netconn_new( NETCONN_UDP ); /* Bind to the local address, or to ANY address */ // netconn_bind(conn, NULL, DATA_UDP_TX_THREAD_PORT ); //local port, NULL is bind to ALL ADDRESSES! (IP_ADDR_ANY) err = netconn_bind(mpu9150_mac_info.conn, &ip_addr_sensor, IMU_A_TX_PORT ); //local port if (err == ERR_OK) { /* Connect to specific address or a broadcast address */ /* * \todo Understand why a UDP needs a connect... * This may be a LwIP thing that chooses between tcp_/udp_/raw_ connections internally. * */ // netconn_connect(conn, IP_ADDR_BROADCAST, DATA_UDP_TX_THREAD_PORT ); err = netconn_connect(mpu9150_mac_info.conn, &ip_addr_fc, FC_LISTEN_PORT_IMU_A ); if(err == ERR_OK) { while (TRUE) { chEvtDispatch(evhndl_mpu9150, chEvtWaitOneTimeout(EVENT_MASK(0), MS2ST(50))); } } return RDY_RESET; } return RDY_RESET; }
static bool tcpsocket_reconnect(TcpSocket *socket) { LOG_ERR("Reconnecting..\n"); /* Close socket if was open */ close_socket(socket); /* If we are in server mode we do nothing */ if (socket->handler) return false; /* Start with new connection */ socket->sock = netconn_new(NETCONN_TCP); if(!socket->sock) { LOG_ERR("Unabe to alloc new connection\n"); socket->error = -1; goto error; } socket->error = netconn_bind(socket->sock, socket->local_addr, socket->port); if(socket->error != ERR_OK) { LOG_ERR("Connection error\n"); goto error; } socket->error = netconn_connect(socket->sock, socket->remote_addr, socket->port); if(socket->error != ERR_OK) { LOG_ERR("Cannot create socket\n"); goto error; } LOG_INFO("connected ip=%d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(socket->remote_addr->addr)); return true; error: netconn_delete(socket->sock); socket->sock = NULL; return false; }