static void rawd_new(struct netconn *nc) { int i; int free = -1; chMtxLock(&rawd_mutex); /* check for existing connections */ for (i = 0; i < NC_COUNT; i++) { if (ncs[i] == NULL) { free = i; } else if (ncs[i]->pcb.tcp->state != ESTABLISHED) { /* kill stale connections */ netconn_close(ncs[i]); netconn_delete(ncs[i]); ncs[i] = NULL; free = i; } } if (free < 0) { netconn_write(nc, RAWD_FULL, sizeof(RAWD_FULL) - 1, NETCONN_COPY); netconn_close(nc); netconn_delete(nc); chMtxUnlock(); return; } ncs[free] = nc; chMtxUnlock(); netconn_write(nc, RAWD_READY, sizeof(RAWD_READY) - 1, NETCONN_COPY); }
/** * \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 stop_web_server() { //printf("\r\nWEB:Enter stop web server!\n"); webs_terminate = 1; if(pxHTTPListener) netconn_abort(pxHTTPListener); if(webs_sema) { if(xSemaphoreTake(webs_sema, 15 * configTICK_RATE_HZ) != pdTRUE) { if(pxHTTPListener) { netconn_close(pxHTTPListener); netconn_delete(pxHTTPListener); pxHTTPListener = NULL; } printf("\r\nWEB: Take webs sema(%p) failed!!!!!!!!!!!\n", webs_sema); } vSemaphoreDelete(webs_sema); webs_sema = NULL; } if(webs_task) { vTaskDelete(webs_task); webs_task = NULL; } printf("\r\nWEB:Exit stop web server!\n"); }
/* This function processes an incomming connection */ static void process_connection(struct netconn *conn) { struct netbuf *inbuf; char *rq; int len; /* Read data from the connection into the netbuf inbuf. * We assume that the full request is in the netbuf. */ inbuf = netconn_recv(conn); /* Get the pointer to the dta in the first netbuf * fragment which we hope contains the request.*/ netbuf_data(inbuf, &rq, &len); /* check if the request was an HTTP "GET"/\r\n". */ if(rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T' && rq[3] == ' ' && rq[4] == '/' && rq[5] == '\r' && rq[6] == '\n') { /* send the header.*/ netconn_write(conn, http_html_hdr, sizeof(http_html_hdr), NETCONN_NOCOPY); /* send the acutal web pages */ netconn_write(conn, indexdata, size(indexdata), NETCONN_NOCOPY); /* Close the connection */ netconn_close(conn); } }
void http_server(void *pdata) { struct netconn *HTTPNetConn = NULL, *xHTTPNetConn; err_t rc1; if (xSemaphoreTake(xDhcpCmplSemaphore_2, portMAX_DELAY) == pdTRUE) { /* Create a new TCP connection handle */ HTTPNetConn = netconn_new(NETCONN_TCP); // netconn_set_recvtimeout(HTTPNetConn, 100); if (HTTPNetConn == NULL) { /*No memory for new connection? */ UART_PrintStr("No mem for new HTTP con\r\n"); } /* Bind to port 80 (HTTP) with default IP address */ netconn_bind(HTTPNetConn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(HTTPNetConn); while (1) { rc1 = netconn_accept(HTTPNetConn, &xHTTPNetConn); if(rc1 == ERR_OK){ // http_server_serve(xHTTPNetConn); UART_PrintStr("netconn accept\r\n"); netconn_close(xHTTPNetConn); netconn_delete(xHTTPNetConn); }else{ PrintERR(rc1); } } } while (1) vTaskDelay(1000/portTICK_RATE_MS); }
void TaskLWIP(void * pvArg) { struct netconn *__pstConn, *__pstNewConn; struct netbuf *__pstNetbuf; char *rq; u16 len; // 初始化LwIP vlwIPInit(); // 设置LwIP,包括添加配置网络接口、建立接收任务等工作 SetLwIP(); __pstConn = netconn_new(NETCONN_TCP); netconn_bind(__pstConn, NULL,80); netconn_listen(__pstConn); while(1) { __pstNewConn = netconn_accept(__pstConn); if(__pstNewConn != NULL) { __pstNetbuf = netconn_recv(__pstNewConn); if(__pstNetbuf != NULL) { netbuf_data(__pstNetbuf,&rq,&len); netconn_write(__pstNewConn, rq, len, NETCONN_COPY); netbuf_delete(__pstNetbuf); } netconn_close(__pstNewConn); // while(netconn_delete(__pstNewConn) != ERR_OK) // OSTimeDly(10); } } }
static void data_udp_rx_serve(struct netconn *conn) { BaseSequentialStream *chp = getActiveUsbSerialStream(); static uint8_t count = 0; struct netbuf *inbuf; char *buf; uint16_t buflen = 0; uint16_t i = 0; err_t err; /* * Read the data from the port, blocking if nothing yet there. * We assume the request (the part we care about) is in one netbuf */ err = netconn_recv(conn, &inbuf); if (err == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); chprintf(chp, "\r\nsensor rx (from FC): %d ", count++); for(i=0; i<buflen; ++i) { chprintf(chp, "%c", buf[i]); } chprintf(chp, "\r\n"); } netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, * so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
/*! \brief process an incoming connection * parse the request and close/delete the connection * * \note Input. The netconn to use to send and receive data. * */ portTASK_FUNCTION( prvweb_ProcessSingleConnection, pvParameters ) { struct netconn * pxNewConnection; portSHORT i; portLONG err_count; /* while( xSemaphoreTake(xMutexNbHTTPConn , portMAX_DELAY ) != pdTRUE ); sCurrentNbHTTPConn++; // Release the mutex. xSemaphoreGive( xMutexNbHTTPConn ); */ pxNewConnection = ( struct netconn *)pvParameters; prvweb_ParseHTMLRequest( pxNewConnection ); // Since we cannot know when the client has closed the connection(lwIP bug), // we set a 1000ms delay (the time for the file transfer to end), then we close // the connection ourselves. // NOTE FOR IMPROVMENTS: we could set this delay depending on the amount of // data that was sent to the client(the more the longer). vTaskDelay( 50 ); err_count = 4; while( netconn_close( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } //vTaskDelay( 100 ); err_count = 4; while( netconn_delete( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } // netconn_close( pxNewConnection ); // netconn_delete( pxNewConnection ); while( xSemaphoreTake(xMutexNbHTTPConn , portMAX_DELAY ) != pdTRUE ); for (i = 0 ; i < webHTTP_NB_CONN ; i++) { if (tTaskHandle[i] == xTaskGetCurrentTaskHandle()) { tTaskHandle[i] = NULL; break; } } sCurrentNbHTTPConn--; // Release the xWEBMutex if there are no other active connections. if( 0 == sCurrentNbHTTPConn ) { x_supervisor_SemaphoreGive( xWEBMutex ); } // Release the mutex. xSemaphoreGive( xMutexNbHTTPConn ); vTaskDelete(NULL); /* nothing after delete task : this will not be reached */ }
/*-----------------------------------------------------------------------------------*/ 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); } } }
static void closeSrqIo(user_data_t * user_data) { // control connection closed netconn_close(user_data->control_io); netconn_delete(user_data->control_io); user_data->control_io = NULL; iprintf("***Control Connection closed\r\n"); }
T_uezError Network_lwIP_SocketClose( void *aWorkspace, T_uezNetworkSocket aSocket) { T_Network_lwIP_Workspace *p = (T_Network_lwIP_Workspace *)aWorkspace; T_uezError error = UEZ_ERROR_UNKNOWN; T_lwIPSocket *p_socket = p->iSockets + aSocket; // Only valid sockets if ((aSocket == 0) || (aSocket > NETWORK_LWIP_NUM_SOCKETS)) return UEZ_ERROR_HANDLE_INVALID; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); if (p_socket->iState == SOCKET_STATE_FREE) { error = UEZ_ERROR_HANDLE_INVALID; } else if ((p_socket->iFlags & SOCKET_FLAG_CONNECTED) == 0) { error = UEZ_ERROR_NOT_OPEN; } else { // Delete any receive buffers still open if (p_socket->iReceiveNetBuf) { netbuf_delete(p_socket->iReceiveNetBuf); } p_socket->iReceiveData = 0; p_socket->iReceiveLength = 0; p_socket->iReceiveRemaining = 0; p_socket->iReceiveNetBuf = 0; p_socket->iFlags = 0; error = IConvertErrorCode(netconn_close(p_socket->iNetconn)); } UEZSemaphoreRelease(p->iSem); return error; }
/* 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); } } }
/** 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; }
static void http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen; err_t err; char strBuf[64]; int strLen; /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ err = netconn_recv(conn, &inbuf); if (err == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since there are other formats for GET, and we're keeping it very simple )*/ if (buflen>=5 && buf[0]=='G' && buf[1]=='E' && buf[2]=='T' && buf[3]==' ' && buf[4]=='/' ) { /* Send the HTML header * subtract 1 from the size, since we dont send the \0 in the string * NETCONN_NOCOPY: our data is const static, so no need to copy it */ chThdSleep(100); netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html_1, sizeof(http_index_html_1)-1, NETCONN_NOCOPY); strLen = sprintf(strBuf, "%c% 2.2f", currentTemp > 0 ? '+' : '-', currentTemp); netconn_write(conn, strBuf, strLen, NETCONN_COPY); netconn_write(conn, http_index_html_2, sizeof(http_index_html_2)-1, NETCONN_NOCOPY); for (int i = 0; i < 12; i++) { strLen = sprintf(strBuf, "<tr><td>%u hour</td><td>%c% 2.2f°</td></tr>", i + 1, lastReadings[i] > 0 ? '+' : '-', lastReadings[i]); netconn_write(conn, strBuf, strLen, NETCONN_COPY); } netconn_write(conn, http_index_html_3, sizeof(http_index_html_3)-1, NETCONN_NOCOPY); } } /* Close the connection (server closes in HTTP) */ netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
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 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); }
/*! \brief parse the incoming request * parse the HTML request and send file * * \param pxNetCon Input. The netconn to use to send and receive data. * */ static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon ) { struct netbuf *pxRxBuffer; portCHAR *pcRxString; unsigned portSHORT usLength; static unsigned portLONG ulPageHits = 0; #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) ) /* We expect to immediately get data. */ pxRxBuffer = netconn_recv( pxNetCon ); #else while(netconn_recv( pxNetCon, &pxRxBuffer) != ERR_OK) { vTaskDelay( webSHORT_DELAY ); } #endif if( pxRxBuffer != NULL ) { /* Where is the data? */ netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength ); /* Is this a GET? We don't handle anything else. */ if(( NULL != pcRxString ) && ( !strncmp( pcRxString, "GET", 3 ) )) { /* Update the hit count. */ ulPageHits++; sprintf( cPageHits, "%d", (int)ulPageHits ); /* Write out the HTTP OK header. */ netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY ); /* Generate the dynamic page... First the page header. */ strcpy( cDynamicPage, webHTML_START ); /* ... Then the hit count... */ strcat( cDynamicPage, cPageHits ); strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" ); /* ... Then the list of tasks and their status... */ vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) ); /* ... Finally the page footer. */ strcat( cDynamicPage, webHTML_END ); /* Write out the dynamically generated page. */ netconn_write( pxNetCon, cDynamicPage, (u16_t) strlen( cDynamicPage ), NETCONN_COPY ); } netbuf_delete( pxRxBuffer ); } netconn_close( pxNetCon ); netconn_delete( pxNetCon ); }
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"); } }
/* * Process an incoming connection on port 80. * * This simply checks to see if the incoming data contains a GET request, and * if so sends back a single dynamically created page. The connection is then * closed. A more complete implementation could create a task for each * connection. */ static void vProcessConnection( struct netconn *pxNetCon ) { static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11]; struct netbuf *pxRxBuffer; char *pcRxString; unsigned short usLength; static unsigned long ulPageHits = 0; /* We expect to immediately get data. */ pxRxBuffer = netconn_recv( pxNetCon ); if( pxRxBuffer != NULL ) { /* Where is the data? */ netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength ); /* Is this a GET? We don't handle anything else. */ if( !strncmp( pcRxString, "GET", 3 ) ) { pcRxString = cDynamicPage; /* Update the hit count. */ ulPageHits++; sprintf( cPageHits, "%lu", ulPageHits ); /* Write out the HTTP OK header. */ netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY ); /* Generate the dynamic page... ... First the page header. */ strcpy( cDynamicPage, webHTML_START ); /* ... Then the hit count... */ strcat( cDynamicPage, cPageHits ); strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" ); /* ... Then the list of tasks and their status... */ vTaskList( ( signed char * )cDynamicPage + strlen( cDynamicPage ) ); /* ... Finally the page footer. */ strcat( cDynamicPage, webHTML_END ); /* Write out the dynamically generated page. */ netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY ); } netbuf_delete( pxRxBuffer ); } netconn_close( pxNetCon ); }
/* process netconn close */ void telnet_process_close(struct telnet_session* telnet, struct netconn *conn) { /* set console */ rt_console_set_device("uart1"); /* set finsh device */ finsh_set_device("uart1"); /* close connection */ netconn_close(conn); /* restore shell option */ finsh_set_echo(telnet->echo_mode); rt_kprintf("resume console to uart0\n"); }
int netstack_socket_close(struct netstack_socket *sk) { err_t err; if (!sk || !sk->priv) { return VMM_EINVALID; } err = netconn_close(sk->priv); if (err != ERR_OK) { return VMM_EFAIL; } return VMM_OK; }
static int processSrqIoListen(user_data_t * user_data) { struct netconn *newconn; if (netconn_accept(user_data->control_io_listen, &newconn) == ERR_OK) { if (user_data->control_io) { netconn_close(newconn); netconn_delete(newconn); } else { // control connection established iprintf("***Control Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); user_data->control_io = newconn; } } return 0; }
/************************************************************************* * t1_timer_run *************************************************************************/ void t1_timer_run(struct iecsock *s) { extern struct netconn *newconn; USART_TRACE("!!! нужно активное закрытие соединения таймаут t1 !!!\n"); s->t1.evnt = 0; s->t1.run = 0; // попробуем закрыть s->vr = 0; s->vs = 0; s->va = 0; USART_TRACE("active close TCP/IP\n"); netconn_close(newconn); // закрываем и освобождаем соединение netconn_delete(newconn); }
// telnet-like server, reads text commands from an open network connection static void canBridge_serve (struct netconn *conn) { struct netbuf *inbuf; currConn = conn; for (;;) { err_t err = netconn_recv(conn, &inbuf); if (err != ERR_OK) break; char *buf; u16_t buflen; netbuf_data(inbuf, (void **)&buf, &buflen); parseCanCmds(buf, buflen); netbuf_delete(inbuf); } currConn = 0; netconn_close(conn); }
/* **************关闭并删除某连接************** */ void delete_conn_u(struct netconn *conn) { int i; netconn_close(conn); netconn_delete(conn); for(i=0;i<MAX_CLIENT_NUM;i++) { if(user_info[i].conn == conn) { user_info[i].conn = NULL; user_info[i].is_used =0; client_used--; break; } } }
/*-----------------------------------------------------------------------------------*/ 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 http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf, *ptr; u16_t buflen; /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ if (netconn_recv(conn, &inbuf) == ERR_OK) { netbuf_data(inbuf, (void **) &buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since there are other formats for GET, and we're keeping it very simple )*/ if (buflen >= 17 && (ptr = strstr(buf, "/?nazwa=OPT")) != NULL){ if(*(ptr+11) == '1') LED_On(4); else LED_Off(4); netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html, sizeof(http_index_html) - 1, NETCONN_NOCOPY); netconn_write(conn, Title, strlen(Title), NETCONN_COPY); netconn_write(conn, http_index_html_2, sizeof(http_index_html_2) - 1, NETCONN_NOCOPY); }else if (buflen >= 5 && buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T' && buf[3] == ' ' && buf[4] == '/') { /* Send the HTML header * subtract 1 from the size, since we dont send the \0 in the string * NETCONN_NOCOPY: our data is const static, so no need to copy it */ netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html, sizeof(http_index_html) - 1, NETCONN_NOCOPY); netconn_write(conn, Title, strlen(Title), NETCONN_COPY); netconn_write(conn, http_index_html_2, sizeof(http_index_html_2) - 1, NETCONN_NOCOPY); } } /* Close the connection (server closes in HTTP) */ netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
/******************************************************************************* * Function Name : vHandler_HTTP * Description : HTTP处理 * Input : - pstConn: 指向struct netconn结构的指针 * Output : None * Return : None * Attention : None *******************************************************************************/ static void vHandler_HTTP(struct netconn *pstConn) { struct netbuf *__pstNetbuf; INT8S *__pbData; u16_t __s32Len; __pstNetbuf = netconn_recv(pstConn); if(__pstNetbuf != NULL) { netbuf_data (__pstNetbuf, (void *)&__pbData, &__s32Len ); if( strstr( (void *)__pbData, "GET" ) != NULL ) { __Handler_HTTPGet(pstConn); } } netbuf_delete(__pstNetbuf); netconn_close(pstConn); }
static void data_udp_rx_serve(struct netconn *conn) { #if DEBUG_SENSOR_UDP static uint8_t count = 0; BaseSequentialStream *chp = getActiveUsbSerialStream(); #endif struct netbuf *inbuf; char *buf; uint16_t buflen = 0; err_t err; /* * Read the data from the port, blocking if nothing yet there. * We assume the request (the part we care about) is in one netbuf */ err = netconn_recv(conn, &inbuf); if (err == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); //palClearPad(TIMEINPUT_PORT, TIMEINPUT_PIN); // negative pulse for input. #if DEBUG_SENSOR_UDP chprintf(chp, "\r\nsensor rx (from FC): %d ", count++); #endif //palSetPad(TIMEINPUT_PORT, TIMEINPUT_PIN); #if DEBUG_SENSOR_UDP uint16_t i = 0; for(i=0; i<buflen; ++i) { chprintf(chp, "%c", buf[i]); } chprintf(chp, "\r\n"); #endif data_udp_process_rx(buf, buflen); } netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, * so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }
/** Serve one HTTP connection accepted in the http thread */ static void http_server_netconn_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen; err_t err; /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ err = netconn_recv(conn, &inbuf); if (err == ERR_OK) { netbuf_data(inbuf, (void**)&buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since there are other formats for GET, and we're keeping it very simple )*/ if (buflen>=5 && buf[0]=='G' && buf[1]=='E' && buf[2]=='T' && buf[3]==' ' && buf[4]=='/' ) { /* Send the HTML header * subtract 1 from the size, since we dont send the \0 in the string * NETCONN_NOCOPY: our data is const static, so no need to copy it */ printf(" Recieved a proper HTTP request sending out the http headers\n"); int ret = netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY); /* Send our HTML page */ netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY); } } /* Close the connection (server closes in HTTP) */ netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); }