net_suite.c 4.1 KB

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