net_suite.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "esp_event.h"
  9. #include "esp_log.h"
  10. #include "stdinout.h"
  11. #include "lwip/err.h"
  12. #include "lwip/debug.h"
  13. #include "lwip/tcp.h"
  14. /* these test data are used to populate the ARP cache so the IPs are known */
  15. static char arp1[] = {
  16. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x01,
  17. 0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x00, 0x00, 0x02,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x00, 0x00, 0x01
  19. };
  20. /* Test data (ICMP packet) for verification of tcp ip test netif
  21. 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
  22. */
  23. /* creating test pcb */
  24. static struct tcp_pcb *test_pcb;
  25. err_t test_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  26. {
  27. return ERR_OK;
  28. }
  29. void test_error(void *arg, err_t err)
  30. {
  31. printf("Error CB from pcb %d\n", err);
  32. }
  33. err_t test_poll(void *arg, struct tcp_pcb *tpcb)
  34. {
  35. return ERR_OK;
  36. }
  37. err_t test_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
  38. {
  39. LWIP_UNUSED_ARG(arg);
  40. LWIP_UNUSED_ARG(err);
  41. tcp_setprio(newpcb, TCP_PRIO_MIN);
  42. tcp_arg(newpcb, NULL);
  43. tcp_recv(newpcb, test_recv);
  44. tcp_err(newpcb, test_error);
  45. tcp_poll(newpcb, test_poll, 0);
  46. return ERR_OK;
  47. }
  48. void test_tcp_init(void)
  49. {
  50. test_pcb = tcp_new();
  51. if (test_pcb != NULL) {
  52. err_t err;
  53. /* Binding this test_pcb to 4242 to accept connections on this port
  54. * - this has to be configured as DUT endpoint
  55. * - all network traffic from and to network stack is tracked in nettestif
  56. */
  57. err = tcp_bind(test_pcb, IP_ADDR_ANY, 4242);
  58. if (err == ERR_OK) {
  59. test_pcb = tcp_listen(test_pcb);
  60. tcp_accept(test_pcb, test_accept);
  61. } else {
  62. printf("cannot bind test_pcb\n");
  63. abort();
  64. }
  65. } else {
  66. printf("cannot create test_pcb\n");
  67. abort();
  68. }
  69. }
  70. void app_main(void)
  71. {
  72. char packet[128];
  73. // Netif configs
  74. //
  75. esp_netif_ip_info_t ip_info;
  76. uint8_t mac[] = { 0,0,0,0,0,1};
  77. esp_netif_inherent_config_t netif_common_config = {
  78. .flags = ESP_NETIF_FLAG_AUTOUP,
  79. .ip_info = (esp_netif_ip_info_t*)&ip_info,
  80. .if_key = "TEST",
  81. .if_desc = "net_test_if"
  82. };
  83. esp_netif_set_ip4_addr(&ip_info.ip, 10, 0 , 0, 1);
  84. esp_netif_set_ip4_addr(&ip_info.gw, 10, 0 , 0, 1);
  85. esp_netif_set_ip4_addr(&ip_info.netmask, 255, 255 , 255, 0);
  86. esp_netif_config_t config = {
  87. .base = &netif_common_config, // use specific behaviour configuration
  88. .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, // use default WIFI-like network stack configuration
  89. };
  90. // Netif creation and configuration
  91. //
  92. ESP_ERROR_CHECK(esp_netif_init());
  93. esp_netif_t* netif = esp_netif_new(&config);
  94. assert(netif);
  95. esp_netif_attach(netif, netsuite_io_new());
  96. // Start the netif in a manual way, no need for events
  97. //
  98. esp_netif_set_mac(netif, mac);
  99. esp_netif_action_start(netif, NULL, 0, NULL);
  100. // initializes TCP endpoint on DUT per https://github.com/intel/net-test-suites#21-endpoints
  101. test_tcp_init();
  102. // Inject ARP packet to let the network stack know about IP/MAC of the counterpart
  103. esp_netif_receive(netif, arp1, sizeof(arp1), NULL);
  104. /* Now read from stdin and pass the data to test netif */
  105. while (1) {
  106. /* read one packet from the I/O object */
  107. ssize_t len = netsuite_io_get_packet(packet, sizeof(packet));
  108. if (len > 0) {
  109. /* input the packet to esp-netif */
  110. esp_netif_receive(netif, packet, len, NULL);
  111. }
  112. }
  113. }