esp_netif_driver.rst 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ESP-NETIF Custom I/O Driver
  2. ===========================
  3. :link_to_translation:`zh_CN:[中文]`
  4. This section outlines implementing a new I/O driver with ESP-NETIF connection capabilities.
  5. By convention, the I/O driver has to register itself as an ESP-NETIF driver, and thus holds a dependency on ESP-NETIF component and is responsible for providing data path functions, post-attach callback and in most cases, also default event handlers to define network interface actions based on driver's lifecycle transitions.
  6. Packet Input/Output
  7. ^^^^^^^^^^^^^^^^^^^
  8. According to the diagram shown in the :ref:`esp-netif structure` part, the following three API functions for the packet data path must be defined for connecting with ESP-NETIF:
  9. * :cpp:func:`esp_netif_transmit()`
  10. * :cpp:func:`esp_netif_free_rx_buffer()`
  11. * :cpp:func:`esp_netif_receive()`
  12. The first two functions for transmitting and freeing the rx buffer are provided as callbacks, i.e., they get called from ESP-NETIF (and its underlying TCP/IP stack) and I/O driver provides their implementation.
  13. The receiving function on the other hand gets called from the I/O driver, so that the driver's code simply calls :cpp:func:`esp_netif_receive()` on a new data received event.
  14. Post Attach Callback
  15. ^^^^^^^^^^^^^^^^^^^^
  16. A final part of the network interface initialization consists of attaching the ESP-NETIF instance to the I/O driver, by means of calling the following API:
  17. .. code:: c
  18. esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle);
  19. It is assumed that the ``esp_netif_iodriver_handle`` is a pointer to driver's object, a struct derived from ``struct esp_netif_driver_base_s``, so that the first member of I/O driver structure must be this base structure with pointers to:
  20. * post-attach function callback
  21. * related ESP-NETIF instance
  22. As a result, the I/O driver has to create an instance of the struct per below:
  23. .. code:: c
  24. typedef struct my_netif_driver_s {
  25. esp_netif_driver_base_t base; /*!< base structure reserved as esp-netif driver */
  26. driver_impl *h; /*!< handle of driver implementation */
  27. } my_netif_driver_t;
  28. with actual values of ``my_netif_driver_t::base.post_attach`` and the actual drivers handle ``my_netif_driver_t::h``.
  29. So when the :cpp:func:`esp_netif_attach()` gets called from the initialization code, the post-attach callback from I/O driver's code gets executed to mutually register callbacks between ESP-NETIF and I/O driver instances. Typically the driver is started as well in the post-attach callback. An example of a simple post-attach callback is outlined below:
  30. .. code:: c
  31. static esp_err_t my_post_attach_start(esp_netif_t * esp_netif, void * args)
  32. {
  33. my_netif_driver_t *driver = args;
  34. const esp_netif_driver_ifconfig_t driver_ifconfig = {
  35. .driver_free_rx_buffer = my_free_rx_buf,
  36. .transmit = my_transmit,
  37. .handle = driver->driver_impl
  38. };
  39. driver->base.netif = esp_netif;
  40. ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig));
  41. my_driver_start(driver->driver_impl);
  42. return ESP_OK;
  43. }
  44. Default Handlers
  45. ^^^^^^^^^^^^^^^^
  46. I/O drivers also typically provide default definitions of lifecycle behavior of related network interfaces based on state transitions of I/O drivers. For example *driver start* ``->`` *network start*, etc.
  47. An example of such a default handler is provided below:
  48. .. code:: c
  49. esp_err_t my_driver_netif_set_default_handlers(my_netif_driver_t *driver, esp_netif_t * esp_netif)
  50. {
  51. driver_set_event_handler(driver->driver_impl, esp_netif_action_start, MY_DRV_EVENT_START, esp_netif);
  52. driver_set_event_handler(driver->driver_impl, esp_netif_action_stop, MY_DRV_EVENT_STOP, esp_netif);
  53. return ESP_OK;
  54. }
  55. Network Stack Connection
  56. ------------------------
  57. The packet data path functions for transmitting and freeing the rx buffer (defined in the I/O driver) are called from the ESP-NETIF, specifically from its TCP/IP stack connecting layer.
  58. Note that ESP-IDF provides several network stack configurations for the most common network interfaces, such as for the Wi-Fi station or Ethernet. These configurations are defined in :component_file:`esp_netif/include/esp_netif_defaults.h` and should be sufficient for most network drivers. In rare cases, expert users might want to define custom lwIP based interface layers; it is possible, but an explicit dependency to lwIP needs to be set.
  59. The following API reference outlines these network stack interaction with the ESP-NETIF:
  60. .. include-build-file:: inc/esp_netif_net_stack.inc