#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