linux_connect.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_err.h"
  7. #include "esp_netif.h" // esp-netif
  8. #include "tapio.h" // esp-netif's driver side
  9. #include "lwip/tapif.h" // esp-netif's network stack side
  10. esp_err_t example_connect(void)
  11. {
  12. #if CONFIG_EXAMPLE_CONNECT_LWIP_TAPIF
  13. // configure linux tapio
  14. esp_netif_driver_ifconfig_t driver_cfg = {
  15. .handle = tapio_create(),
  16. .transmit = tapio_output,
  17. };
  18. // configure lwip netif for the tapif
  19. struct esp_netif_netstack_config stack_cfg = {
  20. .lwip = {
  21. .init_fn = lwip_tapif_init,
  22. .input_fn = lwip_tapif_input,
  23. }
  24. };
  25. // configure inherent esp-netif parameters
  26. esp_netif_ip_info_t ip_info = {};
  27. ip_info.ip.addr = ipaddr_addr(CONFIG_EXAMPLE_CONNECT_TAPIF_IP_ADDR);
  28. ip_info.netmask.addr = ipaddr_addr(CONFIG_EXAMPLE_CONNECT_TAPIF_NETMASK);
  29. ip_info.gw.addr = ipaddr_addr(CONFIG_EXAMPLE_CONNECT_TAPIF_GW);
  30. esp_netif_inherent_config_t base_cfg = {
  31. .if_key = "TAP",
  32. .flags = ESP_NETIF_FLAG_AUTOUP,
  33. .ip_info = &ip_info,
  34. .route_prio = 100
  35. };
  36. // put all configs together
  37. esp_netif_config_t cfg = {
  38. .base = &base_cfg,
  39. .driver = &driver_cfg,
  40. .stack = &stack_cfg
  41. };
  42. // create the interface and attach it to the tapio-handle
  43. esp_netif_t *tap_netif = esp_netif_new(&cfg);
  44. esp_netif_attach(tap_netif, driver_cfg.handle);
  45. #endif // EXAMPLE_CONNECT_LWIP_TAPIF
  46. return ESP_OK;
  47. }