esp_netif.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ESP-NETIF
  2. =========
  3. The purpose of ESP-NETIF library is twofold:
  4. - It provides an abstraction layer for the application on top of the TCP/IP stack. This will allow applications to choose between IP stacks in the future.
  5. - The APIs it provides are thread safe, even if the underlying TCP/IP stack APIs are not.
  6. ESP-IDF currently implements ESP-NETIF for the lwIP TCP/IP stack only. However, the adapter itself is TCP/IP implementation agnostic and different implementations are possible.
  7. Some ESP-NETIF API functions are intended to be called by application code, for example to get/set interface IP addresses, configure DHCP. Other functions are intended for internal ESP-IDF use by the network driver layer.
  8. In many cases, applications do not need to call ESP-NETIF APIs directly as they are called from the default network event handlers.
  9. ESP-NETIF component is a successor of the tcpip_adapter, former network interface abstraction, which has become deprecated since IDF v4.1.
  10. Please refer to the :doc:`/api-reference/network/tcpip_adapter_migration` section in case existing applications to be ported to use the esp-netif API instead.
  11. ESP-NETIF architecture
  12. ----------------------
  13. .. code-block:: text
  14. | (A) USER CODE |
  15. | |
  16. .............| init settings events |
  17. . +----------------------------------------+
  18. . . | *
  19. . . | *
  20. --------+ +===========================+ * +-----------------------+
  21. | | new/config get/set | * | |
  22. | | |...*.....| init |
  23. | |---------------------------| * | |
  24. init | | |**** | |
  25. start |********| event handler |*********| DHCP |
  26. stop | | | | |
  27. | |---------------------------| | |
  28. | | | | NETIF |
  29. +-----| | | +-----------------+ |
  30. | glue|----<---| esp_netif_transmit |--<------| netif_output | |
  31. | | | | | | |
  32. | |---->---| esp_netif_receive |-->------| netif_input | |
  33. | | | | + ----------------+ |
  34. | |....<...| esp_netif_free_rx_buffer |...<.....| packet buffer |
  35. +-----| | | | |
  36. | | | | (D) |
  37. (B) | | (C) | +-----------------------+
  38. --------+ +===========================+
  39. communication NETWORK STACK
  40. DRIVER ESP-NETIF
  41. Data and event flow in the diagram
  42. ----------------------------------
  43. * ``........`` Initialization line from user code to ESP-NETIF and communication driver
  44. * ``--<--->--`` Data packets going from communication media to TCP/IP stack and back
  45. * ``********`` Events aggregated in ESP-NETIF propagates to driver, user code and network stack
  46. * ``|`` User settings and runtime configuration
  47. ESP-NETIF interaction
  48. ---------------------
  49. A) User code, boiler plate
  50. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  51. Overall application interaction with a specific IO driver for communication media and configured TCP/IP network stack
  52. is abstracted using ESP-NETIF APIs and outlined as below:
  53. A) Initialization code
  54. 1) Initializes IO driver
  55. 2) Creates a new instance of ESP-NETIF and configure with
  56. * ESP-NETIF specific options (flags, behaviour, name)
  57. * Network stack options (netif init and input functions, not publicly available)
  58. * IO driver specific options (transmit, free rx buffer functions, IO driver handle)
  59. 3) Attaches the IO driver handle to the ESP-NETIF instance created in the above steps
  60. 4) Configures event handlers
  61. * use default handlers for common interfaces defined in IO drivers; or define a specific handlers for customised behaviour/new interfaces
  62. * register handlers for app related events (such as IP lost/acquired)
  63. B) Interaction with network interfaces using ESP-NETIF API
  64. * Getting and setting TCP/IP related parameters (DHCP, IP, etc)
  65. * Receiving IP events (connect/disconnect)
  66. * Controlling application lifecycle (set interface up/down)
  67. B) Communication driver, IO driver, media driver
  68. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  69. Communication driver plays these two important roles in relation with ESP-NETIF:
  70. 1) Event handlers: Define behaviour patterns of interaction with ESP-NETIF (for example: ethernet link-up -> turn netif on)
  71. 2) Glue IO layer: Adapts the input/output functions to use ESP-NETIF transmit, receive and free receive buffer
  72. * Installs driver_transmit to appropriate ESP-NETIF object, so that outgoing packets from network stack are passed to the IO driver
  73. * Calls :cpp:func:`esp_netif_receive()` to pass incoming data to network stack
  74. C) ESP-NETIF, former tcpip_adapter
  75. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  76. ESP-NETIF is an intermediary between an IO driver and a network stack, connecting packet data path between these two.
  77. As that it provides a set of interfaces for attaching a driver to ESP-NETIF object (runtime) and
  78. configuring a network stack (compile time). In addition to that a set of API is provided to control network interface lifecycle
  79. and its TCP/IP properties. As an overview, the ESP-NETIF public interface could be divided into these 6 groups:
  80. 1) Initialization APIs (to create and configure ESP-NETIF instance)
  81. 2) Input/Output API (for passing data between IO driver and network stack)
  82. 3) Event or Action API
  83. * Used for network interface lifecycle management
  84. * ESP-NETIF provides building blocks for designing event handlers
  85. 4) Setters and Getters for basic network interface properties
  86. 5) Network stack abstraction: enabling user interaction with TCP/IP stack
  87. * Set interface up or down
  88. * DHCP server and client API
  89. * DNS API
  90. 6) Driver conversion utilities
  91. D) Network stack
  92. ^^^^^^^^^^^^^^^^
  93. Network stack has no public interaction with application code with regard to public interfaces and shall be fully abstracted by
  94. ESP-NETIF API.
  95. ESP-NETIF programmer's manual
  96. -----------------------------
  97. Please refer to the example section for basic initialization of default interfaces:
  98. - WiFi Station: :example_file:`wifi/getting_started/station/main/station_example_main.c`
  99. - WiFi Access Point: :example_file:`wifi/getting_started/softAP/main/softap_example_main.c`
  100. - Ethernet: :example_file:`ethernet/basic/main/ethernet_example_main.c`
  101. For more specific cases please consult this guide: :doc:`/api-reference/network/esp_netif_driver`.
  102. WiFi default initialization
  103. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  104. The initialization code as well as registering event handlers for default interfaces,
  105. such as softAP and station, are provided in two separate APIs to facilitate simple startup code for most applications:
  106. * :cpp:func:`esp_netif_create_default_wifi_ap()`
  107. * :cpp:func:`esp_netif_create_default_wifi_sta()`
  108. Please note that these functions return the ``esp_netif`` handle, i.e. a pointer to a network interface object allocated and
  109. configured with default settings, which as a consequence, means that:
  110. * The created object has to be destroyed if a network de-initialization is provided by an application using :cpp:func:`esp_netif_destroy_default_wifi()`.
  111. * These *default* interfaces must not be created multiple times, unless the created handle is deleted using :cpp:func:`esp_netif_destroy()`.
  112. * When using Wifi in ``AP+STA`` mode, both these interfaces has to be created.
  113. API Reference
  114. -------------
  115. .. include-build-file:: inc/esp_netif.inc
  116. WiFi default API reference
  117. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  118. .. include-build-file:: inc/esp_wifi_default.inc