net_suite.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Net-suite test code
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event_loop.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "driver/uart.h"
  17. #include "esp_console.h"
  18. #include "esp_vfs_dev.h"
  19. #include "linenoise/linenoise.h"
  20. #include "lwip/err.h"
  21. #include "lwip/sys.h"
  22. #include "lwip/debug.h"
  23. #include "lwip/stats.h"
  24. #include "lwip/tcp.h"
  25. void nettestif_input(void *buffer, u16_t len);
  26. /* these data configures ARP cache so the test IPs are knows */
  27. static char arp1[] = {
  28. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06,
  29. 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 0x00, 0x01
  30. };
  31. /* Test data (ICMP packet) for verification of tcp ip test netif
  32. 00-00-00-00-00-01-00-00-00-00-00-02-08-00-45-00-00-1c-00-00-00-00-ff-01-a7-de-0a-00-00-02-0a-00-00-01-08-00-f7-fd-00-01-00-01
  33. */
  34. /* creating test pcb */
  35. static struct tcp_pcb *test_pcb;
  36. err_t test_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  37. {
  38. return ERR_OK;
  39. }
  40. void test_error(void *arg, err_t err)
  41. {
  42. printf("Error CB from pcb %d\n", err);
  43. }
  44. err_t test_poll(void *arg, struct tcp_pcb *tpcb)
  45. {
  46. return ERR_OK;
  47. }
  48. err_t test_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
  49. {
  50. LWIP_UNUSED_ARG(arg);
  51. LWIP_UNUSED_ARG(err);
  52. tcp_setprio(newpcb, TCP_PRIO_MIN);
  53. tcp_arg(newpcb, NULL);
  54. tcp_recv(newpcb, test_recv);
  55. tcp_err(newpcb, test_error);
  56. tcp_poll(newpcb, test_poll, 0);
  57. return ERR_OK;
  58. }
  59. void test_tcp_init(void)
  60. {
  61. test_pcb = tcp_new();
  62. if (test_pcb != NULL) {
  63. err_t err;
  64. /* Binding this test_pcb to 4242 to accept connections on this port
  65. * - this has to be configured as DUT endpoint
  66. * - all network traffic from and to network stack is tracked in nettestif
  67. */
  68. err = tcp_bind(test_pcb, IP_ADDR_ANY, 4242);
  69. if (err == ERR_OK) {
  70. test_pcb = tcp_listen(test_pcb);
  71. tcp_accept(test_pcb, test_accept);
  72. } else {
  73. printf("cannot bind test_pcb\n");
  74. abort();
  75. }
  76. } else {
  77. printf("cannot create test_pcb\n");
  78. abort();
  79. }
  80. }
  81. /**
  82. * @brief Process line read from serial input, character by character
  83. *
  84. * Converts from hex string to byte stream, so it can be processed
  85. * in test network interface
  86. *
  87. * @param line
  88. * @param packet
  89. *
  90. * @return size of packet
  91. */
  92. static size_t process_line(char* line, char* packet)
  93. {
  94. size_t count = 0;
  95. size_t i;
  96. for (i=0; i< strlen(line); i++) {
  97. char c = line[i];
  98. // accept both separators between bytes
  99. if (c == '-' || c == ' ') {
  100. ++count;
  101. // Processing numeric characters
  102. } else if (c >= '0' && c <= '9') {
  103. packet[count] *= 16;
  104. packet[count] += c - '0';
  105. // Processing alpha-numeric hex characters
  106. } else if (c >= 'a' && c <= 'f') {
  107. packet[count] *= 16;
  108. packet[count] += c - 'a' + 10;
  109. }
  110. }
  111. if (i>0 && strlen(line)>0) {
  112. count++;
  113. }
  114. return count;
  115. }
  116. void app_main()
  117. {
  118. char packet[128];
  119. tcpip_adapter_ip_info_t ip_info;
  120. uint8_t ap_mac[6] = { 0,0,0,0,0,1};
  121. IP4_ADDR(&ip_info.ip, 10, 0 , 0, 1);
  122. IP4_ADDR(&ip_info.gw, 10, 0 , 0, 1);
  123. IP4_ADDR(&ip_info.netmask, 255, 255 , 255, 0);
  124. tcpip_adapter_init();
  125. tcpip_adapter_test_start(ap_mac, &ip_info);
  126. // initializes TCP endpoint on DUT per https://github.com/intel/net-test-suites#21-endpoints
  127. test_tcp_init();
  128. // Inject ARP packet to let the network stack know about IP/MAC of the counterpart
  129. nettestif_input(arp1, sizeof(arp1));
  130. // Initialize VFS & UART so we can use std::cout/cin
  131. setvbuf(stdin, NULL, _IONBF, 0);
  132. setvbuf(stdout, NULL, _IONBF, 0);
  133. /* Install UART driver for interrupt-driven reads and writes */
  134. ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM,
  135. 256, 0, 0, NULL, 0) );
  136. /* Tell VFS to use UART driver */
  137. esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
  138. esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
  139. /* Move the caret to the beginning of the next line on '\n' */
  140. esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
  141. linenoiseSetDumbMode(1);
  142. /* Now read from stdin and pass the data to test netif */
  143. while (1) {
  144. size_t size;
  145. char* line = linenoise("");
  146. if (!line) {
  147. continue;
  148. }
  149. size = process_line(line, packet);
  150. nettestif_input(packet, size);
  151. linenoiseFree(line);
  152. }
  153. }