esp_netif.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Please note that the initialization code as well as registering event handlers for default interfaces,
  68. such as WiFi softAP and WiFi station, are provided as a separate APIs (for example ``esp_netif_create_default_wifi_ap()`` and
  69. ``esp_netif_create_default_wifi_sta()``) to facilitate simple startup code for most applications.
  70. B) Communication driver, IO driver, media driver
  71. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  72. Communication driver plays these two important roles in relation with ESP-NETIF:
  73. 1) Event handlers: Define behaviour patterns of interaction with ESP-NETIF (for example: ethernet link-up -> turn netif on)
  74. 2) Glue IO layer: Adapts the input/output functions to use ESP-NETIF transmit, receive and free receive buffer
  75. * Installs driver_transmit to appropriate ESP-NETIF object, so that outgoing packets from network stack are passed to the IO driver
  76. * Calls ``esp_netif_receive()`` to pass incoming data to network stack
  77. C) ESP-NETIF, former tcpip_adapter
  78. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79. ESP-NETIF is an intermediary between an IO driver and a network stack, connecting packet data path between these two.
  80. As that it provides a set of interfaces for attaching a driver to ESP-NETIF object (runtime) and
  81. configuring a network stack (compile time). In addition to that a set of API is provided to control network interface lifecycle
  82. and its TCP/IP properties. As an overview, the ESP-NETIF public interface could be divided into these 6 groups:
  83. 1) Initialization APIs (to create and configure ESP-NETIF instance)
  84. 2) Input/Output API (for passing data between IO driver and network stack)
  85. 3) Event or Action API
  86. * Used for network interface lifecycle management
  87. * ESP-NETIF provides building blocks for designing event handlers
  88. 4) Setters and Getters for basic network interface properties
  89. 5) Network stack abstraction: enabling user interaction with TCP/IP stack
  90. * Set interface up or down
  91. * DHCP server and client API
  92. * DNS API
  93. 6) Driver conversion utilities
  94. D) Network stack
  95. ^^^^^^^^^^^^^^^^
  96. Network stack has no public interaction with application code with regard to public interfaces and shall be fully abstracted by
  97. ESP-NETIF API.
  98. API Reference
  99. -------------
  100. .. include:: /_build/inc/esp_netif.inc