| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #include "chip.h"
- #include "board.h"
- #include "FreeRTOS.h"
- #include "FreeRTOS_CLI.h"
- #include "tcpip.h"
- #include "api.h"
- #include "dhcp.h"
- typedef struct
- {
- void (*ItemFunc)(void);
- char *ItemName;
- }TTestItem;
- static int test_index = -1;
- 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);
- extern void mqtt_test(void);
- extern void tcp_speed_test(void);
- extern void udp_speed_test(void);
- extern err_t ethernetif_init(struct netif *netif);
- static struct netif g_lwip_netif; /* 定义一个全局的网络接口 */
- static const 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"},
- {mqtt_test, "mqtt_demo"},
- {tcp_speed_test, "tcp speed test"},
- {udp_speed_test, "udp speed test"},
- };
- static void tcpip_init_done(void *arg)
- {
- printf("%s\n", __func__);
- }
- #if LWIP_DHCP
- static int dhcp_status = 0;
- static int link_change = 0;
- static void link_status_change(struct netif *netif)
- {
- link_change = 1;
- }
- static void dhcp_change_check_task(void *param)
- {
- struct netif *netif = (struct netif *)param;
- char string[40];
- ip_addr_t ip_addr;
- ip_addr.addr = 0;
- int link_status = 0;
- int pinf = 0;
- while (1) {
- if (dhcp_supplied_address(netif) && netif_is_link_up(netif)) {
- if ((ip_addr.addr != netif->ip_addr.addr) || link_change) {
- ip_addr = netif->ip_addr;
- link_change = 0;
- printf("dhcp client ip : %s\n", ipaddr_ntoa_r(&netif->ip_addr, string, 40));
- printf("dhcp client netmask: %s\n", ipaddr_ntoa_r(&netif->netmask, string, 40));
- printf("dhcp client gw : %s\n", ipaddr_ntoa_r(&netif->gw, string, 40));
- }
- dhcp_status = 1;
- } else {
- dhcp_status = 0;
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- }
- }
- #endif
- void eth_test_task(void *param)
- {
- INT32 i;
- INT32 nItemCount;
- ip_addr_t ip;
- ip_addr_t mask;
- ip_addr_t gw;
- char chr;
- int dhcp_tout = 20;
- IP4_ADDR(&ip, 192, 168, 137, 10);
- IP4_ADDR(&mask, 255, 255, 255, 0);
- IP4_ADDR(&gw, 192, 168, 137, 1);
- // 设置主机名称
- netif_set_hostname(&g_lwip_netif, "hv160");
- 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);
- netif_set_up(&g_lwip_netif);
- #if LWIP_DHCP
- netif_set_link_callback(&g_lwip_netif, link_status_change);
- // 启动 DHCP Client
- dhcp_start(&g_lwip_netif);
- xTaskCreate(dhcp_change_check_task, "dhcp change", configMINIMAL_STACK_SIZE, (void *)&g_lwip_netif,
- configMAX_PRIORITIES / 2, NULL);
- printf("waiting dhcp...\n");
- while (!dhcp_status) {
- vTaskDelay(pdMS_TO_TICKS(500));
- }
- #else
- while (!netif_is_link_up(&g_lwip_netif)) vTaskDelay(pdMS_TO_TICKS(100));
- #endif
- nItemCount = 0;
- printf("TEST ETH \r\n");
- nItemCount = sizeof(stItems)/sizeof(stItems[0]);
- while(1){
- 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));
- }
- }
- 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;
- }
- void eth_test_demo(void)
- {
- char *pcHelpString = NULL;
- uint32_t pos = 0;
- int i, str_len = 0;
- static 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\n5:mqtt_demo\r\n",
- prvEthTestCommand, /* The function to run. */
- 1 /* One parameter is expected. Valid values are "start" and "stop". */
- };
- str_len += 2;
- str_len += strlen(xStartStopTrace.pcCommand) + 2;
- for (i = 0; i < sizeof(stItems) / sizeof(stItems[0]); i++) {
- if (stItems[i].ItemName) {
- str_len += strlen(stItems[i].ItemName) + 3 + 2;
- }
- }
- pcHelpString = pvPortMalloc(str_len);
- sprintf(pcHelpString, "\r\n%s\r\n", xStartStopTrace.pcCommand);
- pos = strlen(xStartStopTrace.pcCommand) + 4;
- for (i = 0; i < sizeof(stItems) / sizeof(stItems[0]); i++) {
- if (stItems[i].ItemName) {
- sprintf((char *)(pcHelpString + pos), "%2d:%s\r\n", i, stItems[i].ItemName);
- pos += strlen(stItems[i].ItemName) + 3 + 2;
- }
- }
- *((char **)(&xStartStopTrace.pcHelpString)) = pcHelpString;
- xTaskCreate(eth_test_task, "eth_test_task", configMINIMAL_STACK_SIZE * 10, NULL,
- configMAX_PRIORITIES - 3, NULL);
- FreeRTOS_CLIRegisterCommand(&xStartStopTrace);
- }
|