| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- #include "chip.h"
- #include "board.h"
- #include "FreeRTOS.h"
- #include "FreeRTOS_IP.h"
- #include "FreeRTOS_Sockets.h"
- #include "FreeRTOS_DHCP.h"
- #include "FreeRTOS_CLI.h"
- typedef struct
- {
- void (*ItemFunc)(void);
- char *ItemName;
- }TTestItem;
- typedef void * xComPortHandle;
- static int test_index = -1;
- #if !USE_LWIP
- #define mainHOST_NAME "HV160"
- static uint8_t ucMACAddress[ 6 ] =
- {
- 0x11,
- 0x22,
- 0x33,
- 0x44,
- 0x55,
- 0x99,
- };
- static uint8_t ucIPAddress[ 4 ] =
- {
- 192,
- 168,
- 137,
- 4,
- };
- static uint8_t ucNetMask[ 4 ] =
- {
- 255,
- 255,
- 255,
- 0
- };
- static uint8_t ucGatewayAddress[ 4 ] =
- {
- 192,
- 168,
- 137,
- 1,
- };
- static uint8_t ucDNSServerAddress[ 4 ] =
- {
- 0,
- 0,
- 0,
- 0
- };
- extern void eth_tcp_server_test(void);
- extern void eth_tcp_client_test(void);
- extern void eth_udp_test(void);
- extern void https_client_test(void);
- extern void http_client_test(void);
- static void ETH_IRQHandler(void *param)
- {
- ETH_HandleTypeDef *eth = (ETH_HandleTypeDef *)param;
- printf("ETH_IRQHandler DMASR:0x%x \n",eth->Instance->DMASR);
- }
- ETH_DMADescTypeDef tx_des_tab[5] = {0};
- static uint8_t Tx_Buff[ 5 ][ ETH_TX_BUF_SIZE ];
- static int dhcp_state = 0;
- #if FREERTOS_TCP_ETH_EN
- void eth_test_task(void *param)
- {
- INT32 i;
- char chr;
- INT8 string[30];
- INT32 nItemCount;
- int dhcp_tout = 10;
- dhcp_state = 0;
- #if 1
- TTestItem stItems[] =
- {
- {eth_tcp_server_test,"tcp server test"},
- {eth_tcp_client_test,"tcp client test"},
- {eth_udp_test,"udp test"},
- {https_client_test, "https client test"},
- {http_client_test, "http client teset"}
- };
- setDhcpClientState(1); // dhcp enable
- if(FreeRTOS_IPInit(
- ucIPAddress,
- ucNetMask,
- ucGatewayAddress,
- ucDNSServerAddress,
- ucMACAddress ) != pdPASS){
- printf("FREERTOS IP INIT FAIL!\n");
- return ;
- }else{
- printf("FREERTOS IP INIT OK!\n");
- }
- if (getDhcpClientState()) {
- while (dhcp_state == 0) {
- vTaskDelay(pdMS_TO_TICKS(1000));
- dhcp_tout--;
- if (dhcp_tout == 0) {
- printf("dhcp time out!\n");
- while (1) {
- vTaskDelay(portMAX_DELAY);
- }
- }
- }
- }
- nItemCount = 0;
- printf("TEST ETH \r\n");
- nItemCount = sizeof(stItems)/sizeof(stItems[0]);
- while(1){
- #if 0
- for(i=0;i<nItemCount;i++){
- printf("%d: %s\r\n", i, stItems[i].ItemName);
- }
- printf("%d: Exit\r\n", i);
- printf("Please select one Item to test...\r\n");
- extern signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime );
- extern xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength );
- do {
- xSerialGetChar((void *)xSerialPortInitMinimal(NULL, NULL), (signed char *)&chr, portMAX_DELAY);
- } while((chr < '0') || (chr > '9'));
- i = chr - '0';
- #endif
- i = test_index;
- test_index = -1;
- if (i >= 0) {
- if(i<nItemCount){
- if(stItems[i].ItemFunc)
- stItems[i].ItemFunc();
- }
- else if(i == nItemCount) {
- printf("End of Test routing and exit.\r\n");
- break;
- }
- else {
- printf("You input a uncorrect item.\r\n");
- return;
- }
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- }
- #else
- NetworkBufferDescriptor_t tx_buf;
- xNetworkInterfaceInitialise();
- tx_buf.pucEthernetBuffer = pvPortMalloc(256);
- tx_buf.xDataLength = 256;
- for(i=0;i<256;i++){
- tx_buf.pucEthernetBuffer[i] = i;
- }
- while(1){
- //xNetworkInterfaceOutput(&tx_buf,0);
- vTaskDelay(pdMS_TO_TICKS(4000));
- }
- #endif
- }
- #if ipconfigUSE_NETWORK_EVENT_HOOK
- void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
- {
- uint32_t pulIPAddress;
- uint32_t pulNetMask;
- uint32_t pulGatewayAddress;
- uint32_t pulDNSServerAddress;
- static BaseType_t xTasksAlreadyCreated = pdFALSE;
- char cBuffer[16];
- /* If the network has just come up...*/
- if( ( eNetworkEvent == eNetworkUp ) && ( xTasksAlreadyCreated == pdFALSE ) )
- {
- /* Do nothing. Just a stub. */
- xTasksAlreadyCreated = pdTRUE;
- }
- if(eNetworkEvent == eNetworkUp){
- printf("NetworkUp \n");
- FreeRTOS_GetAddressConfiguration(&pulIPAddress, &pulNetMask, &pulGatewayAddress, &pulDNSServerAddress);
- FreeRTOS_inet_ntoa(pulIPAddress, cBuffer);
- printf("IP:%s \n",cBuffer);
- FreeRTOS_inet_ntoa(pulNetMask, cBuffer);
- printf("Mask:%s \n",cBuffer);
- FreeRTOS_inet_ntoa(pulGatewayAddress, cBuffer);
- printf("Gateway:%s \n",cBuffer);
- FreeRTOS_inet_ntoa(pulDNSServerAddress, cBuffer);
- printf("DNS:%s \n",cBuffer);
- }
- }
- #endif
- #if ( ( ipconfigUSE_LLMNR != 0 ) || \
- ( ipconfigUSE_NBNS != 0 ) || \
- ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) )
- const char * pcApplicationHostnameHook( void )
- {
- /* This function will be called during the DHCP: the machine will be registered
- * with an IP address plus this name. */
- //printf("pcApplicationHostnameHook \n");
- return mainHOST_NAME;
- }
- #endif
- #if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )
- BaseType_t xApplicationDNSQueryHook( const char * pcName )
- {
- BaseType_t xReturn;
- /* Determine if a name lookup is for this node. Two names are given
- * to this node: that returned by pcApplicationHostnameHook() and that set
- * by mainDEVICE_NICK_NAME. */
- if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )
- {
- xReturn = pdPASS;
- }
- else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )
- {
- xReturn = pdPASS;
- }
- else
- {
- xReturn = pdFAIL;
- }
- return xReturn;
- }
- #endif
- static UBaseType_t ulNextRand;
- UBaseType_t uxRand( void )
- {
- const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
- /* Utility function to generate a pseudo random number. */
- ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
- return( ( int ) ( ulNextRand ) & 0x7fffUL );
- }
- BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber )
- {
- *pulNumber = uxRand();
- return pdTRUE;
- }
- /*
- * Callback that provides the inputs necessary to generate a randomized TCP
- * Initial Sequence Number per RFC 6528. THIS IS ONLY A DUMMY IMPLEMENTATION
- * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION
- * SYSTEMS.
- */
- extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
- uint16_t usSourcePort,
- uint32_t ulDestinationAddress,
- uint16_t usDestinationPort )
- {
- ( void ) ulSourceAddress;
- ( void ) usSourcePort;
- ( void ) ulDestinationAddress;
- ( void ) usDestinationPort;
- return uxRand();
- }
- #if ( ( ipconfigUSE_TCP == 1 ) && ( ipconfigUSE_DHCP_HOOK != 0 ) )
- __weak eDHCPCallbackAnswer_t xApplicationDHCPHook( eDHCPCallbackPhase_t eDHCPPhase,
- uint32_t ulIPAddress )
- {
- /* Provide a stub for this function. */
- char ip_str[20] = {0};
- if (getDhcpClientState() == 0) {
- sprintf(ip_str, "%d.%d.%d.%d", (ulIPAddress >> 0) & 0xFF,
- (ulIPAddress >> 8) & 0xFF, (ulIPAddress >> 16) & 0xFF, (ulIPAddress >> 24) & 0xFF);
- printf("eDHCPPhase:%d ulIPAddress:%s state:%d \r\n", eDHCPPhase, ip_str, getDhcpClientState());
- return eDHCPStopNoChanges;
- } else if (eDHCPPhase == eDHCPPhaseFinished) {
- sprintf(ip_str, "%d.%d.%d.%d", (ulIPAddress >> 0) & 0xFF,
- (ulIPAddress >> 8) & 0xFF, (ulIPAddress >> 16) & 0xFF, (ulIPAddress >> 24) & 0xFF);
- printf("eDHCPPhase:%d ulIPAddress:%s state:%d \r\n", eDHCPPhase, ip_str, getDhcpClientState());
- dhcp_state = 1;
- }
- return eDHCPContinue;
- }
- #endif
- void vApplicationPingReplyHook( ePingReplyStatus_t eStatus,
- uint16_t usIdentifier )
- {
- /* Provide a stub for this function. */
- }
- #else
- void eth_test_task(void *param)
- {
- printf("------------ FREERTOS_TCP_ETH_EN == 0 ------------\n");
- while(1) {
- vTaskDelay(portMAX_DELAY);
- }
- }
- #endif
- #else
- #include "tcpip.h"
- #include "api.h"
- #include "dhcp.h"
- static void tcpip_init_done(void *arg)
- {
- printf("%s\n", __func__);
- }
- extern void eth_tcp_server_test(void);
- extern void eth_tcp_client_test(void);
- extern void eth_udp_test(void);
- extern void https_client_test(void);
- extern void ping_test(void);
- extern void http_client_test(void);
- static struct netif g_lwip_netif; /* 定义一个全局的网络接口 */
- static int dhcp_state;
- static void dhcp_client_status_callback(struct netif *netif, int status, const ip_addr_t *server_addr)
- {
- char addr_str[40];
- if (&g_lwip_netif == netif) {
- printf("dhcp server ip : %s\n", ipaddr_ntoa_r(server_addr, addr_str, 40));
- printf("dhcp client ip : %s\n", ipaddr_ntoa_r(&netif->ip_addr, addr_str, 40));
- printf("dhcp client netmask: %s\n", ipaddr_ntoa_r(&netif->netmask, addr_str, 40));
- printf("dhcp client gw : %s\n", ipaddr_ntoa_r(&netif->gw, addr_str, 40));
- dhcp_state = 1;
- }
- }
- void eth_test_task(void *param)
- {
- INT32 i;
- INT8 string[30];
- INT32 nItemCount;
- ip_addr_t ip;
- ip_addr_t mask;
- ip_addr_t gw;
- char chr;
- int dhcp_tout = 10;
- TTestItem stItems[] =
- {
- {eth_tcp_server_test,"tcp server test"},
- {eth_tcp_client_test,"tcp client test"},
- {eth_udp_test,"udp test"},
- {https_client_test, "https client test"},
- {http_client_test, "http client test"},
- {ping_test, "ping test"}
- };
- IP4_ADDR(&ip, 192, 168, 137, 2);
- IP4_ADDR(&mask, 255, 255, 255, 0);
- IP4_ADDR(&gw, 192, 168, 137, 1);
- #if LWIP_DHCP
- dhcp_state = 0;
- dhcp_regisger_status_callback(dhcp_client_status_callback);
- #endif
- extern err_t ethernetif_init(struct netif *netif);
- tcpip_init(tcpip_init_done, NULL);
- if (netif_add(&g_lwip_netif, (const ip_addr_t *)&ip, (const ip_addr_t *)&mask, \
- (const ip_addr_t *)&gw, NULL, ðernetif_init, &tcpip_input) == NULL){
- printf("add netif fail!\n");
- return;
- }
- netif_set_default(&g_lwip_netif);
- if (netif_is_link_up(&g_lwip_netif)) {
- netif_set_up(&g_lwip_netif); /* 打开netif网口 */
- } else {
- netif_set_down(&g_lwip_netif);
- }
- // 设置主机名称
- netif_set_hostname(&g_lwip_netif, "hv160");
- #if LWIP_DHCP
- // 启动 DHCP Client
- dhcp_start(&g_lwip_netif);
- while (dhcp_state == 0) {
- vTaskDelay(pdMS_TO_TICKS(1000));
- dhcp_tout--;
- if (dhcp_tout == 0) {
- printf("dhcp time out!\n");
- while (1) {
- vTaskDelay(portMAX_DELAY);
- }
- }
- }
- #endif
- nItemCount = 0;
- printf("TEST ETH \r\n");
- nItemCount = sizeof(stItems)/sizeof(stItems[0]);
- while(1){
- #if 0
- for(i=0;i<nItemCount;i++){
- printf("%d: %s\r\n", i, stItems[i].ItemName);
- }
- printf("%d: Exit\r\n", i);
- printf("Please select one Item to test...\r\n");
- extern signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime );
- extern xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength );
- do {
- xSerialGetChar((void *)xSerialPortInitMinimal(NULL, NULL), (signed char *)&chr, portMAX_DELAY);
- } while((chr < '0') || (chr > '9'));
- i = chr - '0';
- #endif
- i = test_index;
- test_index = -1;
- if (i >= 0) {
- if(i<nItemCount){
- if(stItems[i].ItemFunc)
- stItems[i].ItemFunc();
- }
- else if(i == nItemCount) {
- printf("End of Test routing and exit.\r\n");
- break;
- }
- else {
- printf("You input a uncorrect item.\r\n");
- return;
- }
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- }
- }
- #endif
- static BaseType_t prvEthTestCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
- {
- const char *pcParameter;
- BaseType_t lParameterStringLength;
- /* Remove compile time warnings about unused parameters, and check the
- write buffer is not NULL. NOTE - for simplicity, this example assumes the
- write buffer length is adequate, so does not check for buffer overflows. */
- ( void ) pcCommandString;
- ( void ) xWriteBufferLen;
- configASSERT( pcWriteBuffer );
- /* Obtain the parameter string. */
- pcParameter = FreeRTOS_CLIGetParameter
- (
- pcCommandString, /* The command string itself. */
- 1, /* Return the first parameter. */
- &lParameterStringLength /* Store the parameter string length. */
- );
- /* Sanity check something was returned. */
- configASSERT( pcParameter );
- sscanf(pcParameter, "%d", &test_index);
- /* There is no more data to return after this single string, so return
- pdFALSE. */
- return pdFALSE;
- }
- static const CLI_Command_Definition_t xStartStopTrace =
- {
- "eth-test",
- "\r\neth-test:\r\n0:tcp server test\r\n1:tcp client test\r\n2:udp test\r\n3:https client test\r\n4:http client teset\r\n",
- prvEthTestCommand, /* The function to run. */
- 1 /* One parameter is expected. Valid values are "start" and "stop". */
- };
- void eth_test_demo(void)
- {
- xTaskCreate(eth_test_task, "eth_test_task", configMINIMAL_STACK_SIZE * 10, NULL,
- configMAX_PRIORITIES - 3, NULL);
- FreeRTOS_CLIRegisterCommand(&xStartStopTrace);
- }
|