esp_netif.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 architecture
  10. ======================
  11. .. code-block:: text
  12. | (A) USER CODE |
  13. | |
  14. .............| init settings events |
  15. . +----------------------------------------+
  16. . . | *
  17. . . | *
  18. --------+ +===========================+ * +-----------------------+
  19. | | new/config get/set | * | |
  20. | | |...*.....| init |
  21. | |---------------------------| * | |
  22. init | | |**** | |
  23. start |********| event handler |*********| DHCP |
  24. stop | | | | |
  25. | |---------------------------| | |
  26. | | | | NETIF |
  27. +-----| | | +-----------------+ |
  28. | glue|----<---| esp_netif_transmit |--<------| netif_output | |
  29. | | | | | | |
  30. | |---->---| esp_netif_receive |-->------| netif_input | |
  31. | | | | + ----------------+ |
  32. | |....<...| esp_netif_free_rx_buffer |...<.....| packet buffer |
  33. +-----| | | | |
  34. | | | | (D) |
  35. (B) | | (C) | +-----------------------+
  36. --------+ +===========================+
  37. communication NETWORK STACK
  38. DRIVER ESP-NETIF
  39. Data and event flow in the diagram
  40. ----------------------------------
  41. * ``........`` Initialization line from user code to ESP-NETIF and communication driver
  42. * ``--<--->--`` Data packets going from communication media to TCP/IP stack and back
  43. * ``********`` Events aggregated in ESP-NETIF propagates to driver, user code and network stack
  44. * ``|`` User settings and runtime configuration
  45. ESP-NETIF interaction
  46. ---------------------
  47. A) User code, boiler plate
  48. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  49. Overall application interaction with a specific IO driver for communication media and configured TCP/IP network stack
  50. is abstracted using ESP-NETIF APIs and outlined as below:
  51. A) Initialization code
  52. 1) Initializes IO driver
  53. 2) Creates a new instance of ESP-NETIF and configure with
  54. * ESP-NETIF specific options (flags, behaviour, name)
  55. * Network stack options (netif init and input functions, not publicly available)
  56. * IO driver specific options (transmit, free rx buffer functions, IO driver handle)
  57. 3) Attaches the IO driver handle to the ESP-NETIF instance created in the above steps
  58. 4) Configures event handlers
  59. * use default handlers for common interfaces defined in IO drivers; or define a specific handlers for customised behaviour/new interfaces
  60. * register handlers for app related events (such as IP lost/acquired)
  61. B) Interaction with network interfaces using ESP-NETIF API
  62. * Getting and setting TCP/IP related parameters (DHCP, IP, etc)
  63. * Receiving IP events (connect/disconnect)
  64. * Controlling application lifecycle (set interface up/down)
  65. Please note that the initialization code as well as registering event handlers for default interfaces,
  66. such as WiFi softAP and WiFi station, are provided as a separate APIs (for example ``esp_netif_create_default_wifi_ap()`` and
  67. ``esp_netif_create_default_wifi_sta()``) to facilitate simple startup code for most applications.
  68. B) Communication driver, IO driver, media driver
  69. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  70. Communication driver plays these two important roles in relation with ESP-NETIF:
  71. 1) Event handlers: Define behaviour patterns of interaction with ESP-NETIF (for example: ethernet link-up -> turn netif on)
  72. 2) Glue IO layer: Adapts the input/output functions to use ESP-NETIF transmit, receive and free receive buffer
  73. * Installs driver_transmit to appropriate ESP-NETIF object, so that outgoing packets from network stack are passed to the IO driver
  74. * Calls ``esp_netif_receive()`` to pass incoming data to network stack
  75. C) ESP-NETIF, former tcpip_adapter
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. ESP-NETIF is an intermediary between an IO driver and a network stack, connecting packet data path between these two.
  78. As that it provides a set of interfaces for attaching a driver to ESP-NETIF object (runtime) and
  79. configuring a network stack (compile time). In addition to that a set of API is provided to control network interface lifecycle
  80. and its TCP/IP properties. As an overview, the ESP-NETIF public interface could be divided into these 6 groups:
  81. 1) Initialization APIs (to create and configure ESP-NETIF instance)
  82. 2) Input/Output API (for passing data between IO driver and network stack)
  83. 3) Event or Action API
  84. * Used for network interface lifecycle management
  85. * ESP-NETIF provides building blocks for designing event handlers
  86. 4) Setters and Getters for basic network interface properties
  87. 5) Network stack abstraction: enabling user interaction with TCP/IP stack
  88. * Set interface up or down
  89. * DHCP server and client API
  90. * DNS API
  91. 6) Driver conversion utilities
  92. D) Network stack
  93. ^^^^^^^^^^^^^^^^
  94. Network stack has no public interaction with application code with regard to public interfaces and shall be fully abstracted by
  95. ESP-NETIF API.
  96. API Reference
  97. -------------
  98. .. include:: /_build/inc/esp_netif.inc