You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/api-reference/network/esp_netif.rst
+34-43Lines changed: 34 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,7 @@ The purpose of the ESP-NETIF library is twofold:
10
10
11
11
ESP-IDF currently implements ESP-NETIF for the lwIP TCP/IP stack only. However, the adapter itself is TCP/IP implementation-agnostic and allows different implementations.
12
12
13
-
It is also possible to use a custom TCP/IP stack with ESP-IDF, provided it implements BSD API. For more information on building ESP-IDF without lwIP, please refer to :idf_file:`components/esp_netif_stack/README.md`.
14
-
15
-
Some ESP-NETIF API functions are intended to be called by application code, for example, to get or set interface IP addresses, and configure DHCP. Other functions are intended for internal ESP-IDF use by the network driver layer.
16
-
17
-
In many cases, applications do not need to call ESP-NETIF APIs directly as they are called by the default network event handlers.
13
+
Some ESP-NETIF API functions are intended to be called by application code, for example, to get or set interface IP addresses, and configure DHCP. Other functions are intended for internal ESP-IDF use by the network driver layer. In many cases, applications do not need to call ESP-NETIF APIs directly as they are called by the default network event handlers.
18
14
19
15
If you are only interested in using the most common network interfaces with default setting, please read :ref:`esp_netif_user` to see how you can initialize default interfaces and register event handlers.
20
16
@@ -29,15 +25,15 @@ If you would like to develop your own network driver, implement support for a ne
29
25
ESP-NETIF User's Manual
30
26
=======================
31
27
32
-
It is usually just enough to create a default network interface after startup and destroy it upon closing (see :ref:`esp_netif_init`). It is also necessary to receive a notification upon assigning a new IP address or losing it (see :ref:`esp-netif-ip-events`).
28
+
It is usually just enough to create a default network interface after startup and destroy it upon closing (see :ref:`esp_netif_init`). It is also useful to receive a notification upon assigning a new IP address or losing it (see :ref:`esp-netif-ip-events`).
33
29
34
30
35
31
.. _esp_netif_init:
36
32
37
33
Initialization
38
34
--------------
39
35
40
-
Since the ESP-NETIF component uses system events, the typical network startup code looks like this (note, that error handling is omitted for clarity):
36
+
Since the ESP-NETIF component uses system events, the typical network startup code looks like this (note, that error handling is omitted for clarity, see :example_file:`ethernet/basic/main/ethernet_example_main.c` for complete startup code):
41
37
42
38
.. code-block:: c
43
39
@@ -48,7 +44,7 @@ Since the ESP-NETIF component uses system events, the typical network startup co
48
44
// 2) Create the network interface handle
49
45
esp_netif = esp_netif_new(&config);
50
46
51
-
// 3) Create the communication driver (e.g. Ethernet) and it's network layer glue
47
+
// 3) Create the network interface driver (e.g. Ethernet) and it's network layer glue
52
48
// and register the ESP-NETIF event (e.g. to bring the interface up upon link-up event)
53
49
esp_netif_glue_t glue = driver_glue(driver);
54
50
@@ -57,15 +53,15 @@ Since the ESP-NETIF component uses system events, the typical network startup co
57
53
58
54
// 5) Register user-side event handlers
59
55
esp_event_handler_register(DRIVER_EVENT, ...); // to observe driver states, e.g. link-up
60
-
esp_event_handler_register(IP_EVENT, ...); // to observer ESP-NETIF states, e.g. get an IP
56
+
esp_event_handler_register(IP_EVENT, ...); // to observe ESP-NETIF states, e.g. get an IP
61
57
62
58
63
59
.. note::
64
60
65
-
These 5 steps need to be performed in the exact order as shown above, mainly due to the default event loop, which is used by network interface drivers to register system events.
61
+
These steps must be performed in the exact order shown above, primarily due to the default event loop used by network interface drivers to register system events.
66
62
67
-
- Default event loop needs to be created **before** initializing an interface driver (as it typically needs register system even handlers)
68
-
- Registering application event handlers need to happen **after** calling :cpp:func:`esp_netif_attach`, because the event handlers are called in the order they were registered and we need to have the system handlers called first.
63
+
- The default event loop needs to be created **before** initializing an interface driver, as the driver typically needs to register system event handlers.
64
+
- Registering application event handlers must occur **after** calling :cpp:func:`esp_netif_attach`, because event handlers are called in the order they were registered. To ensure that system handlers are called first, you should register application handlers afterward.
69
65
70
66
Steps ``2)``, ``3)`` and ``4)`` are quite complex for most common use-cases, so ESP-NETIF provides some pre-configured interfaces and convenience functions that create the most common network interfaces in their most common configurations.
71
67
@@ -74,16 +70,16 @@ Since the ESP-NETIF component uses system events, the typical network startup co
74
70
Each network interface needs to be initialized separately, so if you would like to use multiple interfaces, you would have to run steps ``2)`` to ``5)`` for every interface. Set ``1)`` should be performed only once.
75
71
76
72
77
-
Creating and configuring the interface and attaching the communication driver to it (steps ``2)``, ``3)`` and ``4)``) is described in :ref:`create_esp_netif`.
73
+
Creating and configuring the interface and attaching the network interface driver to it (steps ``2)``, ``3)`` and ``4)``) is described in :ref:`create_esp_netif`.
78
74
Using the ESP-NETIF event handlers (step ``5)``) is described in :ref:`esp-netif-ip-events`.
79
75
80
76
81
77
.. _create_esp_netif:
82
78
83
-
Common network interfaces
79
+
Common Network Interfaces
84
80
^^^^^^^^^^^^^^^^^^^^^^^^^
85
81
86
-
As the initialization of network interfaces could be quite complex, ESP-NETIF provides some convenient methods of creating the most common onc, such as Wi-Fi and Ethernet.
82
+
As the initialization of network interfaces could be quite complex, ESP-NETIF provides some convenient methods of creating the most common ones, such as Wi-Fi and Ethernet.
87
83
88
84
Please refer to the following example to understand the initialization process of the default interface:
89
85
@@ -115,34 +111,34 @@ The initialization code as well as registering event handlers for default interf
115
111
116
112
.. only:: SOC_WIFI_SUPPORTED
117
113
118
-
Please note that these functions return the ``esp_netif`` handle, i.e., a pointer to a network interface object allocated and configured with default settings, as a consequence, which means that:
114
+
Please note that these functions return the ``esp_netif`` handle, i.e., a pointer to a network interface object allocated and configured with default settings, which means that:
119
115
120
116
* 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()`.
121
117
122
-
* These *default* interfaces must not be created multiple times unless the created handle is deleted using :cpp:func:`esp_netif_destroy()`.
118
+
* These *default* interfaces must not be created multiple times unless the created handle is deleted using :cpp:func:`esp_netif_destroy_default_wifi()`.
123
119
124
120
125
121
.. only:: CONFIG_ESP_WIFI_SOFTAP_SUPPORT
126
122
127
-
* When using Wi-Fi in ``AP+STA`` mode, both these interfaces have to be created.
123
+
* When using Wi-Fi in ``AP+STA`` mode, both these interfaces have to be created. Please refer to the example :example_file:`wifi/softap_sta/main/softap_sta.c`
128
124
129
125
.. _esp-netif-ip-events:
130
126
131
127
IP Events
132
128
---------
133
129
134
-
In the last section of :ref:`esp_netif_init` code (step ``5)``), we register two sets of event handlers:
130
+
In the final section of :ref:`esp_netif_init` code (step ``5)``), you register two sets of event handlers:
135
131
136
-
* Communication driver events: To be notified about driver's lifecycle states, for example when Wi-Fi station joined an AP, or when it gets disconnected. Handling these events is out of the scope of ESP-NETIF component, it is only worth mentioning that the same events are also used by ESP-NETIF to set the network interface to a desired state of, so if an application uses the driver's events to assume specific states of the network interface, it need to register their handlers **after** registering system handlers (which typically happen when attaching the driver to the interface). That is why the handler registration is performed in the last step of the :ref:`esp_netif_init` code.
132
+
* **Network Interface Driver Events**: These events notify you about the driver's lifecycle states, such as when a Wi-Fi station joins an AP or gets disconnected. Handling these events is outside the scope of the ESP-NETIF component. It is worth noting that the same events are also used by ESP-NETIF to set the network interface to a desired state. Therefore, if your application uses the driver's events to determine specific states of the network interface, you should register these handlers **after** registering the system handlers (which typically happens when attaching the driver to the interface). This is why handler registration occurs in the final step of the :ref:`esp_netif_init` code.
137
133
138
-
* IP events: To be notified about IP address changes, that is when we get assigned a new address or when the valid address is lost. Specific types of these events are listed in :cpp:type:`ip_event_t`, each common interface has the related pair of ``GOT_IP`` and ``LOST_IP`` events.
134
+
* **IP Events**: These events notify you about IP address changes, such as when a new address is assigned or when a valid address is lost. Specific types of these events are listed in :cpp:type:`ip_event_t`. Each common interface has a related pair of ``GOT_IP`` and ``LOST_IP`` events.
135
+
136
+
Registering event handlers is crucial due to the asynchronous nature of networking, where changes in network state can occur unpredictably. By registering event handlers, applications can respond to these changes promptly, ensuring appropriate actions are taken in response to network events.
139
137
140
138
.. note::
141
139
142
140
Lost IP events are triggered by a timer configurable by :ref:`CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL`. The timer is started upon losing the IP address and the event will be raised after the configured interval, which is 120s by default. The event could be disabled when setting the interval to 0.
* ``........`` Initialization line from user code to ESP-NETIF and communication driver
192
+
* ``........`` Initialization line from user code to ESP-NETIF and network interface driver
198
193
199
194
* ``--<--->--`` Data packets going from communication media to TCP/IP stack and back
200
195
@@ -208,7 +203,7 @@ ESP-NETIF Interaction
208
203
A) User Code, Boilerplate
209
204
'''''''''''''''''''''''''
210
205
211
-
Overall application interaction with a specific IO driver for communication media and configured TCP/IP network stack is abstracted using ESP-NETIF APIs and is outlined as below:
206
+
Overall application interaction with a specific IO driver for the communication media (network interface driver) and configured TCP/IP network stack is abstracted using ESP-NETIF APIs and is outlined as below:
212
207
213
208
A) Initialization code
214
209
@@ -232,10 +227,10 @@ B) Interaction with network interfaces using ESP-NETIF API
232
227
3) Controls application lifecycle (set interface up or down)
233
228
234
229
235
-
B) Communication Driver, IO Driver, and Media Driver
Communication driver plays these two important roles in relation to ESP-NETIF:
233
+
Network interface driver (also called I/O Driver, or Media Driver) plays these two important roles in relation to ESP-NETIF:
239
234
240
235
1) Event handlers: Defines behavior patterns of interaction with ESP-NETIF (e.g., ethernet link-up -> turn netif on)
241
236
@@ -276,18 +271,14 @@ The network stack has no public interaction with application code with regard to
276
271
277
272
E) ESP-NETIF L2 TAP Interface
278
273
'''''''''''''''''''''''''''''
279
-
The ESP-NETIF L2 TAP interface is a mechanism in ESP-IDF used to access Data Link Layer (L2 per OSI/ISO) for frame reception and transmission from the user application. Its typical usage in the embedded world might be the implementation of non-IP-related protocols, e.g., PTP, Wake on LAN. Note that only Ethernet (IEEE 802.3) is currently supported.
280
-
281
-
From a user perspective, the ESP-NETIF L2 TAP interface is accessed using file descriptors of VFS, which provides file-like interfacing (using functions like ``open()``, ``read()``, ``write()``, etc). To learn more, refer to :doc:`/api-reference/storage/vfs`.
282
-
283
-
There is only one ESP-NETIF L2 TAP interface device (path name) available. However multiple file descriptors with different configurations can be opened at a time since the ESP-NETIF L2 TAP interface can be understood as a generic entry point to the Layer 2 infrastructure. What is important is then the specific configuration of the particular file descriptor. It can be configured to give access to a specific Network Interface identified by ``if_key`` (e.g., `ETH_DEF`) and to filter only specific frames based on their type (e.g., Ethernet type in the case of IEEE 802.3). Filtering only specific frames is crucial since the ESP-NETIF L2 TAP needs to exist along with the IP stack and so the IP-related traffic (IP, ARP, etc.) should not be passed directly to the user application. Even though this option is still configurable, it is not recommended in standard use cases. Filtering is also advantageous from the perspective of the user's application, as it only gets access to the frame types it is interested in, and the remaining traffic is either passed to other L2 TAP file descriptors or to the IP stack.
274
+
The ESP-NETIF L2 TAP interface is a mechanism in ESP-IDF used to access Data Link Layer (L2 per OSI/ISO) for frame reception and transmission from the user application. Its typical usage in the embedded world might be the implementation of non-IP-related protocols, e.g., PTP, Wake on LAN. Note that only Ethernet (IEEE 802.3) is currently supported. Please read more about L2 TAP in :ref:`esp_netif_l2tap`.
284
275
285
276
.. _esp_netif_programmer:
286
277
287
278
ESP-NETIF Programmer's Manual
288
279
=============================
289
280
290
-
In same cases, it is not enough to simply initialize a network interface by default, start using it and connect to the local network. If so, please consult the programming guide: :doc:`/api-reference/network/esp_netif_programming`.
281
+
In some cases, it is not enough to simply initialize a network interface by default, start using it and connect to the local network. If so, please consult the programming guide: :doc:`/api-reference/network/esp_netif_programming`.
291
282
292
283
You would typically need to use specific sets of ESP-NETIF APIs in the following use-cases:
293
284
@@ -303,4 +294,4 @@ You would typically need to use specific sets of ESP-NETIF APIs in the following
303
294
ESP-NETIF Developer's Manual
304
295
============================
305
296
306
-
In some cases, user applications might need to customize ESP-NETIF, register custom drivers or even custom TCP/IP stacks. If so, please consult the programming guide::doc:`/api-reference/network/esp_netif_driver`.
297
+
In some cases, user applications might need to customize ESP-NETIF, register custom drivers or even use a custom TCP/IP stack. If so, please consult the :doc:`/api-reference/network/esp_netif_driver`.
0 commit comments