/*-----------------------------------------------------------------------------------*/ 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); 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. */ err_t xErr = netconn_accept(conn, &newconn); /* Process the new connection. */ if (xErr== ERR_OK) { struct netbuf *buf; void *data; u16_t len; while ((xErr = 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); } } } else { printf(" can not bind TCP netconn"); } } else { printf("can not create TCP netconn"); } }
/* telnet server thread entry */ void telnet_thread(void* parameter) { volatile rt_err_t result,err; rt_uint32_t event; struct netbuf *buf; struct netconn *conn, *newconn; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, TELNET_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); /* register telnet device */ telnet->device.type = RT_Device_Class_Char; telnet->device.init = telnet_init; telnet->device.open = telnet_open; telnet->device.close = telnet_close; telnet->device.read = telnet_read; telnet->device.write = telnet_write; telnet->device.control = telnet_control; /* no private */ telnet->device.user_data = RT_NULL; /* register telnet device */ rt_device_register(&telnet->device, "telnet", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM); while (1) { rt_kprintf("telnet server waiting for connection\n"); /* Grab new connection. */ err = netconn_accept(conn, &newconn); //if (err == RT_EOK) continue; /* set network rx call back */ newconn->callback = rx_callback; rt_kprintf("new telnet connection, switch console to telnet...\n"); /* Process the new connection. */ /* set console */ rt_console_set_device("telnet"); /* set finsh device */ finsh_set_device("telnet"); /* set init state */ telnet->state = STATE_NORMAL; telnet->echo_mode = finsh_get_echo(); /* disable echo mode */ finsh_set_echo(0); while (1) { /* try to send all data in tx ringbuffer */ telnet_process_tx(telnet, newconn); /* receive network event */ result = rt_event_recv(telnet->nw_event, NW_MASK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_TICK_PER_SECOND, &event); if (result == RT_EOK) { /* get event successfully */ if (event & NW_RX) { /* do a rx procedure */ err= netconn_recv(newconn, &buf); if (buf != RT_NULL) { rt_uint8_t *data; rt_uint16_t length; /* get data */ netbuf_data(buf, (void**)&data, &length); telnet_process_rx(telnet, data, length); /* release buffer */ netbuf_delete(buf); } else { if (newconn->last_err == ERR_CLSD) { /* close connection */ telnet_process_close(telnet, newconn); break; } } } if (event & NW_TX) telnet_process_tx(telnet, newconn); if (event & NW_CLOSED) { /* process close */ telnet_process_close(telnet, newconn); break; } } else if (result == -RT_ETIMEOUT) { if (newconn->state == NETCONN_CLOSE) { telnet_process_close(telnet, newconn); break; } } } } }
/** * Ethernet to PERF task * * @param none * @return none */ void BRIDGE_PERF_Task( void *pvParameters ) { struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;//enable PIT /* Parameters are not used - suppress compiler error */ ( void )pvParameters; tcpip_init( NULL, NULL ); /* Create and configure the FEC interface. */ IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 ); IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 ); IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 ); netif_add( &ENET_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input ); /* make it the default interface */ netif_set_default( &ENET_if ); /* bring it up */ netif_set_up( &ENET_if ); #if BENCHMARK_PROTOCOL //**********************************UDP**********************************// { struct udp_pcb *remote_pcb; remote_pcb = udp_new(); if(!remote_pcb) vTaskDelete(NULL); /*FSL: error during buffer creation*/ #if BENCHMARK_SEND_TO_PC //**********************************TX***********************************// { uint16 i; /*client mode: connect to this server address*/ struct ip_addr ServerIPAddr; struct pbuf *p; /*Fill the array*/ for(i=0;i<sizeof(array);i++) array[i] = i%256;//dummy sequence /*Server IP address to connect*/ IP4_ADDR(&ServerIPAddr,192,168,0,1); printf("Starting UDP TX Client\n"); if( udp_connect(remote_pcb, &ServerIPAddr, (u16_t)BENCHMARK_PORT) != ERR_OK ) vTaskDelete(NULL); /*FSL: error during socket creation*/ if( (p = pbuf_alloc(PBUF_TRANSPORT,0,PBUF_REF)) == NULL ) vTaskDelete(NULL); /*FSL: error during buffer creation*/ p->payload = &array[0]; p->len = p->tot_len = BENCHMARK_PACKET_LENGTH; /*FSL: send selected number of packets*/ for(pkt_counter=0;pkt_counter<BENCHMARK_NUMBER_OF_PACKETS;pkt_counter++) { if( udp_send(remote_pcb, p) != ERR_OK ) break;//error: abort } /*FSL: send ending frames*/ p->len = p->tot_len = 1; /*FSL: send 10 times*/ udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); //FSL: remove connection pbuf_free(p); udp_remove(remote_pcb); printf("Leaving UDP Tx Benchmark Test\n"); vTaskDelete(NULL); /*FSL: task no longer needed*/ } #else //**********************************RX***********************************// { printf("Starting UDP RX Server\n"); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer PIT_MCR &= ~PIT_MCR_MDIS_MASK; PIT_LDVAL0 = 0xFFFFFFFF;//max value load_value = PIT_LDVAL0; PIT_TCTRL0 = 0; if( udp_bind(remote_pcb, IP_ADDR_ANY, BENCHMARK_PORT) != ERR_OK ) printf("Wrong bind to UDP port\n"); udp_recv(remote_pcb, udp_receive_fnc, NULL); /*Task no longer needed*/ vTaskDelete(NULL); } #endif } #else //**********************************TCP**********************************// #if BENCHMARK_SEND_TO_PC //**********************************TX***********************************// { struct netconn *conn; uint16 i; /*client mode: connect to this server address*/ struct ip_addr ServerIPAddr; /*Fill the array*/ for(i=0;i<sizeof(array);i++) array[i] = i%256;//dummy sequence /*Server IP address to connect*/ IP4_ADDR(&ServerIPAddr,192,168,0,1); printf("Starting TCP Tx Benchmark Test\n"); /*FSL: wait forever until getting a connection to a valid server*/ do { printf("Trying to connect to server...\n"); /* Create a new TCP PCB. */ conn = netconn_new(NETCONN_TCP); /*client connection*/ if( netconn_connect(conn, &ServerIPAddr, (uint16)BENCHMARK_PORT) != ERR_OK ) { /*close connection for a new SYN process*/ netconn_close(conn); netconn_delete(conn); vTaskDelay(2000); } else break; } while(1);/*infinite loop until getting a valid SYN/SYN-ACK/ACK*/ printf("Connected to server\n"); /*FSL: send selected number of packets*/ pkt_counter = BENCHMARK_NUMBER_OF_PACKETS;//will send in a moment /*FSL: only send when connection is established, otherwise close*/ while( pkt_counter-- ) { netconn_write(conn, &array[0], sizeof(array), NETCONN_NOCOPY); } /*FSL: no more packets*/ netconn_close(conn); netconn_delete(conn); printf("Leaving TCP Tx Benchmark Test\n"); /*FSL: good bye*/ vTaskDelete(NULL); } #else //**********************************RX***********************************// for(;;) { struct netconn *conn, *newconn; struct netbuf *inbuf; uint32 total_length_received = 0; double final_result; /* Create a new TCP PCB. */ conn = netconn_new(NETCONN_TCP); if( netconn_bind(conn,IP_ADDR_ANY,(uint16)BENCHMARK_PORT) != ERR_OK ) { /*close connection for a new SYN process*/ netconn_close(conn); netconn_delete(conn); /*FSL: good bye*/ vTaskDelete(NULL); } printf("Starting TCP Rx Benchmark Test\n"); /* Put the connection into LISTEN state. */ netconn_listen(conn); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer PIT_MCR &= ~PIT_MCR_MDIS_MASK; PIT_LDVAL0 = 0xFFFFFFFF;//max value load_value = PIT_LDVAL0; PIT_TCTRL0 = 0; /* waiting for connection from client */ newconn = netconn_accept(conn); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer for(;;) { if( (inbuf = netconn_recv(newconn)) != NULL ) { /*data is not touch: inbuf->ptr and inbuf->ptr->tot_len*/ total_length_received += inbuf->ptr->tot_len; /*free pbuf memory*/ netbuf_delete(inbuf); } else { current_timer = PIT_CVAL0;//get last time break;/*connection closed*/ } } /*FSL: no more packets*/ netconn_close(newconn); netconn_delete(newconn); netconn_close(conn); netconn_delete(conn); /*calculate*/ time_sfw = (load_value + 1 - current_timer); PIT_TCTRL0 = 0;//stop the timer printf("Benchmark results for TCP Rx:\n\n"); printf("Number of bytes received = %d\n",total_length_received); printf("Number of ticks = %d\n",time_sfw); printf("Frequency of ticks = %d\n",(configCPU_CLOCK_HZ/2)); printf("Calculate benchmark: Bytes/sec\n\n"); final_result = (double)total_length_received * (configCPU_CLOCK_HZ/2) /(double)time_sfw; printf("Measured throughput is %9.2f Bytes/sec\n",final_result); } #endif #endif }