eth_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "chip.h"
  2. #include "board.h"
  3. #include "FreeRTOS.h"
  4. #include "FreeRTOS_CLI.h"
  5. #include "tcpip.h"
  6. #include "api.h"
  7. #include "dhcp.h"
  8. typedef struct
  9. {
  10. void (*ItemFunc)(void);
  11. char *ItemName;
  12. }TTestItem;
  13. static int test_index = -1;
  14. extern void eth_tcp_server_test(void);
  15. extern void eth_tcp_client_test(void);
  16. extern void eth_udp_test(void);
  17. extern void https_client_test(void);
  18. extern void ping_test(void);
  19. extern void http_client_test(void);
  20. extern void mqtt_test(void);
  21. extern void tcp_speed_test(void);
  22. extern void udp_speed_test(void);
  23. extern err_t ethernetif_init(struct netif *netif);
  24. static struct netif g_lwip_netif; /* 定义一个全局的网络接口 */
  25. static const TTestItem stItems[] =
  26. {
  27. {eth_tcp_server_test,"tcp server test"},
  28. {eth_tcp_client_test,"tcp client test"},
  29. {eth_udp_test,"udp test"},
  30. {https_client_test, "https client test"},
  31. {http_client_test, "http client test"},
  32. {ping_test, "ping test"},
  33. {mqtt_test, "mqtt_demo"},
  34. {tcp_speed_test, "tcp speed test"},
  35. {udp_speed_test, "udp speed test"},
  36. };
  37. static void tcpip_init_done(void *arg)
  38. {
  39. printf("%s\n", __func__);
  40. }
  41. #if LWIP_DHCP
  42. static int dhcp_status = 0;
  43. static int link_change = 0;
  44. static void link_status_change(struct netif *netif)
  45. {
  46. link_change = 1;
  47. }
  48. static void dhcp_change_check_task(void *param)
  49. {
  50. struct netif *netif = (struct netif *)param;
  51. char string[40];
  52. ip_addr_t ip_addr;
  53. ip_addr.addr = 0;
  54. int link_status = 0;
  55. int pinf = 0;
  56. while (1) {
  57. if (dhcp_supplied_address(netif) && netif_is_link_up(netif)) {
  58. if ((ip_addr.addr != netif->ip_addr.addr) || link_change) {
  59. ip_addr = netif->ip_addr;
  60. link_change = 0;
  61. printf("dhcp client ip : %s\n", ipaddr_ntoa_r(&netif->ip_addr, string, 40));
  62. printf("dhcp client netmask: %s\n", ipaddr_ntoa_r(&netif->netmask, string, 40));
  63. printf("dhcp client gw : %s\n", ipaddr_ntoa_r(&netif->gw, string, 40));
  64. }
  65. dhcp_status = 1;
  66. } else {
  67. dhcp_status = 0;
  68. }
  69. vTaskDelay(pdMS_TO_TICKS(100));
  70. }
  71. }
  72. #endif
  73. void eth_test_task(void *param)
  74. {
  75. INT32 i;
  76. INT32 nItemCount;
  77. ip_addr_t ip;
  78. ip_addr_t mask;
  79. ip_addr_t gw;
  80. char chr;
  81. int dhcp_tout = 20;
  82. IP4_ADDR(&ip, 192, 168, 137, 10);
  83. IP4_ADDR(&mask, 255, 255, 255, 0);
  84. IP4_ADDR(&gw, 192, 168, 137, 1);
  85. // 设置主机名称
  86. netif_set_hostname(&g_lwip_netif, "hv160");
  87. tcpip_init(tcpip_init_done, NULL);
  88. if (netif_add(&g_lwip_netif, (const ip_addr_t *)&ip, (const ip_addr_t *)&mask, \
  89. (const ip_addr_t *)&gw, NULL, &ethernetif_init, &tcpip_input) == NULL){
  90. printf("add netif fail!\n");
  91. return;
  92. }
  93. netif_set_default(&g_lwip_netif);
  94. netif_set_up(&g_lwip_netif);
  95. #if LWIP_DHCP
  96. netif_set_link_callback(&g_lwip_netif, link_status_change);
  97. // 启动 DHCP Client
  98. dhcp_start(&g_lwip_netif);
  99. xTaskCreate(dhcp_change_check_task, "dhcp change", configMINIMAL_STACK_SIZE, (void *)&g_lwip_netif,
  100. configMAX_PRIORITIES / 2, NULL);
  101. printf("waiting dhcp...\n");
  102. while (!dhcp_status) {
  103. vTaskDelay(pdMS_TO_TICKS(500));
  104. }
  105. #else
  106. while (!netif_is_link_up(&g_lwip_netif)) vTaskDelay(pdMS_TO_TICKS(100));
  107. #endif
  108. nItemCount = 0;
  109. printf("TEST ETH \r\n");
  110. nItemCount = sizeof(stItems)/sizeof(stItems[0]);
  111. while(1){
  112. i = test_index;
  113. test_index = -1;
  114. if (i >= 0) {
  115. if(i<nItemCount){
  116. if(stItems[i].ItemFunc)
  117. stItems[i].ItemFunc();
  118. }
  119. else if(i == nItemCount) {
  120. printf("End of Test routing and exit.\r\n");
  121. break;
  122. }
  123. else {
  124. printf("You input a uncorrect item.\r\n");
  125. return;
  126. }
  127. }
  128. vTaskDelay(pdMS_TO_TICKS(100));
  129. }
  130. }
  131. static BaseType_t prvEthTestCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
  132. {
  133. const char *pcParameter;
  134. BaseType_t lParameterStringLength;
  135. /* Remove compile time warnings about unused parameters, and check the
  136. write buffer is not NULL. NOTE - for simplicity, this example assumes the
  137. write buffer length is adequate, so does not check for buffer overflows. */
  138. ( void ) pcCommandString;
  139. ( void ) xWriteBufferLen;
  140. configASSERT( pcWriteBuffer );
  141. /* Obtain the parameter string. */
  142. pcParameter = FreeRTOS_CLIGetParameter
  143. (
  144. pcCommandString, /* The command string itself. */
  145. 1, /* Return the first parameter. */
  146. &lParameterStringLength /* Store the parameter string length. */
  147. );
  148. /* Sanity check something was returned. */
  149. configASSERT( pcParameter );
  150. sscanf(pcParameter, "%d", &test_index);
  151. /* There is no more data to return after this single string, so return
  152. pdFALSE. */
  153. return pdFALSE;
  154. }
  155. void eth_test_demo(void)
  156. {
  157. char *pcHelpString = NULL;
  158. uint32_t pos = 0;
  159. int i, str_len = 0;
  160. static CLI_Command_Definition_t xStartStopTrace = {
  161. "eth-test",
  162. "\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",
  163. prvEthTestCommand, /* The function to run. */
  164. 1 /* One parameter is expected. Valid values are "start" and "stop". */
  165. };
  166. str_len += 2;
  167. str_len += strlen(xStartStopTrace.pcCommand) + 2;
  168. for (i = 0; i < sizeof(stItems) / sizeof(stItems[0]); i++) {
  169. if (stItems[i].ItemName) {
  170. str_len += strlen(stItems[i].ItemName) + 3 + 2;
  171. }
  172. }
  173. pcHelpString = pvPortMalloc(str_len);
  174. sprintf(pcHelpString, "\r\n%s\r\n", xStartStopTrace.pcCommand);
  175. pos = strlen(xStartStopTrace.pcCommand) + 4;
  176. for (i = 0; i < sizeof(stItems) / sizeof(stItems[0]); i++) {
  177. if (stItems[i].ItemName) {
  178. sprintf((char *)(pcHelpString + pos), "%2d:%s\r\n", i, stItems[i].ItemName);
  179. pos += strlen(stItems[i].ItemName) + 3 + 2;
  180. }
  181. }
  182. *((char **)(&xStartStopTrace.pcHelpString)) = pcHelpString;
  183. xTaskCreate(eth_test_task, "eth_test_task", configMINIMAL_STACK_SIZE * 10, NULL,
  184. configMAX_PRIORITIES - 3, NULL);
  185. FreeRTOS_CLIRegisterCommand(&xStartStopTrace);
  186. }