Skip to content

Commit f7ae059

Browse files
authored
Ethernet support (#56)
* Split event handlers * Ethernet support * Document ethernet usage
1 parent ee6c1cd commit f7ae059

File tree

8 files changed

+200
-62
lines changed

8 files changed

+200
-62
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
set(component_requires esp_netif nvs_flash esp_wifi)
2+
3+
if("${IDF_TARGET}" STREQUAL "esp32p4")
4+
list(APPEND component_requires ethernet_init esp_eth)
5+
endif()
6+
17
idf_component_register(
28
SRC_DIRS ./src
39
INCLUDE_DIRS ./include
4-
PRIV_REQUIRES esp_netif esp_wifi nvs_flash
10+
PRIV_REQUIRES ${component_requires}
511
)

components/example_utils/Kconfig.projbuild

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ menu "LiveKit Example Utilities"
1111
bool "WiFi"
1212
help
1313
Connect to the internet using WiFi.
14+
config LK_EXAMPLE_USE_ETHERNET
15+
bool "Ethernet"
16+
help
17+
Connect to the internet using Ethernet (supported boards only).
1418
endchoice
1519
config LK_EXAMPLE_WIFI_SSID
1620
depends on LK_EXAMPLE_USE_WIFI

components/example_utils/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Establish a network connection with a single function call, configured via _Kcon
1111
### Supported Interfaces
1212

1313
- [x] WiFi
14-
- [ ] Ethernet (*TODO*)
14+
- [x] Ethernet (ESP32-P4 only)
1515

1616
### Usage
1717

components/example_utils/idf_component.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ discussion: https://livekit.io/join-slack/
88
license: Apache-2.0
99
dependencies:
1010
idf: ">=5.4"
11+
espressif/ethernet_init:
12+
version: "1.2.0"
13+
rules:
14+
- if: "idf_version >=5.4.3,!=5.5.0,!=5.5.1 && target in [esp32p4]"
15+
# IDF version restriction comes from the ethernet_init component
1116
files:
12-
use_gitignore: true
17+
use_gitignore: true

components/example_utils/src/livekit_example_net.c

Lines changed: 164 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <string.h>
18-
#include <nvs_flash.h>
17+
#include "esp_event.h"
1918
#include "esp_log.h"
2019
#include "esp_netif.h"
20+
#include "freertos/FreeRTOS.h"
21+
#include <nvs_flash.h>
22+
#include <string.h>
23+
24+
#if CONFIG_LK_EXAMPLE_USE_WIFI
2125
#include "esp_wifi.h"
26+
#elif CONFIG_LK_EXAMPLE_USE_ETHERNET
27+
#include "esp_eth.h"
28+
#include "ethernet_init.h"
29+
#endif
2230

2331
#include "livekit_example_net.h"
2432

@@ -38,56 +46,78 @@ typedef struct {
3846

3947
static network_connect_t state = {};
4048

41-
// MARK: - Event handler
42-
43-
static void event_handler(void* arg,
44-
esp_event_base_t event_base,
45-
int32_t event_id,
46-
void* event_data)
47-
{
48-
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
49-
esp_wifi_connect();
50-
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
51-
if (CONFIG_LK_EXAMPLE_NETWORK_MAX_RETRIES < 0 || state.retry_attempt < CONFIG_LK_EXAMPLE_NETWORK_MAX_RETRIES) {
52-
ESP_LOGI(TAG, "Retry: attempt=%d", state.retry_attempt + 1);
53-
esp_wifi_connect();
54-
state.retry_attempt++;
55-
return;
56-
}
57-
ESP_LOGE(TAG, "Unable to establish connection");
58-
xEventGroupSetBits(state.event_group, NETWORK_EVENT_FAILED);
59-
} else if (event_base == IP_EVENT &&
60-
event_id == IP_EVENT_STA_GOT_IP) {
49+
// MARK: - Common
6150

62-
ip_event_got_ip_t* event = (ip_event_got_ip_t *)event_data;
63-
ESP_LOGI(TAG, "Connected: ip=" IPSTR ", gateway=" IPSTR,
64-
IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw));
51+
static void ip_event_handler(
52+
void* arg,
53+
esp_event_base_t event_base,
54+
int32_t event_id,
55+
void* event_data
56+
) {
57+
ip_event_got_ip_t* event = (ip_event_got_ip_t *)event_data;
58+
ESP_LOGI(TAG, "Connected: ip=" IPSTR ", gateway=" IPSTR,
59+
IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw));
6560

66-
state.retry_attempt = 0;
67-
xEventGroupSetBits(state.event_group, NETWORK_EVENT_CONNECTED);
68-
}
61+
state.retry_attempt = 0;
62+
xEventGroupSetBits(state.event_group, NETWORK_EVENT_CONNECTED);
6963
}
7064

71-
// MARK: - Initialization & connection
72-
7365
static inline void init_common(void)
7466
{
7567
if (!state.event_group) {
7668
state.event_group = xEventGroupCreate();
7769
}
78-
7970
ESP_ERROR_CHECK(nvs_flash_init());
8071
ESP_ERROR_CHECK(esp_netif_init());
8172
ESP_ERROR_CHECK(esp_event_loop_create_default());
73+
}
8274

83-
esp_event_handler_instance_t instance_got_ip;
84-
ESP_ERROR_CHECK(esp_event_handler_instance_register(
85-
IP_EVENT,
86-
IP_EVENT_STA_GOT_IP,
87-
&event_handler,
88-
NULL,
89-
&instance_got_ip
90-
));
75+
static inline bool wait_for_connection_or_failure(void)
76+
{
77+
EventBits_t bits;
78+
do {
79+
bits = xEventGroupWaitBits(
80+
state.event_group,
81+
NETWORK_EVENT_CONNECTED | NETWORK_EVENT_FAILED,
82+
pdFALSE,
83+
pdFALSE,
84+
portMAX_DELAY
85+
);
86+
if (bits & NETWORK_EVENT_CONNECTED) {
87+
return true;
88+
}
89+
} while (!(bits & NETWORK_EVENT_FAILED));
90+
return false;
91+
}
92+
93+
94+
// MARK: - WiFi
95+
#if CONFIG_LK_EXAMPLE_USE_WIFI
96+
97+
static void wifi_event_handler(
98+
void* arg,
99+
esp_event_base_t event_base,
100+
int32_t event_id,
101+
void* event_data
102+
) {
103+
switch (event_id) {
104+
case WIFI_EVENT_STA_START:
105+
esp_wifi_connect();
106+
break;
107+
case WIFI_EVENT_STA_DISCONNECTED:
108+
if (CONFIG_LK_EXAMPLE_NETWORK_MAX_RETRIES < 0 ||
109+
state.retry_attempt < CONFIG_LK_EXAMPLE_NETWORK_MAX_RETRIES) {
110+
ESP_LOGI(TAG, "Retry: attempt=%d", state.retry_attempt + 1);
111+
esp_wifi_connect();
112+
state.retry_attempt++;
113+
return;
114+
}
115+
ESP_LOGE(TAG, "Unable to establish connection");
116+
xEventGroupSetBits(state.event_group, NETWORK_EVENT_FAILED);
117+
break;
118+
default:
119+
break;
120+
}
91121
}
92122

93123
static inline bool connect_wifi(void)
@@ -107,13 +137,17 @@ static inline bool connect_wifi(void)
107137
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
108138
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
109139

110-
esp_event_handler_instance_t instance_any_id;
111-
ESP_ERROR_CHECK(esp_event_handler_instance_register(
140+
ESP_ERROR_CHECK(esp_event_handler_register(
112141
WIFI_EVENT,
113142
ESP_EVENT_ANY_ID,
114-
&event_handler,
115-
NULL,
116-
&instance_any_id
143+
&wifi_event_handler,
144+
NULL
145+
));
146+
ESP_ERROR_CHECK(esp_event_handler_register(
147+
IP_EVENT,
148+
IP_EVENT_STA_GOT_IP,
149+
&ip_event_handler,
150+
NULL
117151
));
118152

119153
wifi_config_t wifi_config = {};
@@ -130,30 +164,101 @@ static inline bool connect_wifi(void)
130164
return true;
131165
}
132166

133-
static inline bool wait_for_connection_or_failure(void)
167+
// MARK: - Ethernet
168+
#elif CONFIG_LK_EXAMPLE_USE_ETHERNET
169+
170+
static void eth_event_handler(
171+
void* arg,
172+
esp_event_base_t event_base,
173+
int32_t event_id,
174+
void* event_data
175+
) {
176+
uint8_t mac_addr[6] = { 0 };
177+
esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
178+
179+
switch (event_id) {
180+
case ETHERNET_EVENT_CONNECTED:
181+
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
182+
ESP_LOGD(TAG, "Ethernet Link Up");
183+
break;
184+
case ETHERNET_EVENT_DISCONNECTED:
185+
if (CONFIG_LK_EXAMPLE_NETWORK_MAX_RETRIES < 0 ||
186+
state.retry_attempt < CONFIG_LK_EXAMPLE_NETWORK_MAX_RETRIES) {
187+
ESP_LOGI(TAG, "Retry: attempt=%d", state.retry_attempt + 1);
188+
state.retry_attempt++;
189+
return;
190+
}
191+
ESP_LOGE(TAG, "Unable to establish connection");
192+
xEventGroupSetBits(state.event_group, NETWORK_EVENT_FAILED);
193+
break;
194+
default:
195+
break;
196+
}
197+
}
198+
199+
static inline bool connect_ethernet(void)
134200
{
135-
EventBits_t bits;
136-
do {
137-
bits = xEventGroupWaitBits(
138-
state.event_group,
139-
NETWORK_EVENT_CONNECTED | NETWORK_EVENT_FAILED,
140-
pdFALSE,
141-
pdFALSE,
142-
portMAX_DELAY
143-
);
144-
if (bits & NETWORK_EVENT_CONNECTED) {
145-
return true;
201+
static esp_eth_handle_t *handles = NULL;
202+
203+
uint8_t port_count = 0;
204+
ESP_ERROR_CHECK(ethernet_init_all(&handles, &port_count));
205+
206+
if (port_count == 1) {
207+
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
208+
esp_netif_t *eth_netif = esp_netif_new(&cfg);
209+
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(handles[0])));
210+
} else {
211+
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
212+
esp_netif_config_t cfg_spi = {
213+
.base = &esp_netif_config,
214+
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
215+
};
216+
char if_key_str[10];
217+
char if_desc_str[10];
218+
char num_str[3];
219+
for (int i = 0; i < port_count; i++) {
220+
itoa(i, num_str, 10);
221+
strcat(strcpy(if_key_str, "ETH_"), num_str);
222+
strcat(strcpy(if_desc_str, "eth"), num_str);
223+
esp_netif_config.if_key = if_key_str;
224+
esp_netif_config.if_desc = if_desc_str;
225+
esp_netif_config.route_prio -= i * 5;
226+
esp_netif_t *eth_netif = esp_netif_new(&cfg_spi);
227+
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(handles[i])));
146228
}
147-
} while (!(bits & NETWORK_EVENT_FAILED));
148-
return false;
229+
}
230+
ESP_ERROR_CHECK(esp_event_handler_register(
231+
ETH_EVENT,
232+
ESP_EVENT_ANY_ID,
233+
&eth_event_handler,
234+
NULL
235+
));
236+
ESP_ERROR_CHECK(esp_event_handler_register(
237+
IP_EVENT,
238+
IP_EVENT_ETH_GOT_IP,
239+
&ip_event_handler,
240+
NULL
241+
));
242+
for (int i = 0; i < port_count; i++) {
243+
ESP_ERROR_CHECK(esp_eth_start(handles[i]));
244+
}
245+
ESP_LOGI(TAG, "Connecting Ethernet");
246+
return true;
149247
}
248+
#endif
150249

151250
// MARK: - Public API
152251

153252
bool lk_example_network_connect()
154253
{
155254
init_common();
156-
if (!connect_wifi()) {
255+
bool success = false;
256+
#if CONFIG_LK_EXAMPLE_USE_WIFI
257+
success = connect_wifi();
258+
#elif CONFIG_LK_EXAMPLE_USE_ETHERNET
259+
success = connect_ethernet();
260+
#endif
261+
if (!success) {
157262
return false;
158263
}
159264
return wait_for_connection_or_failure();

components/livekit/examples/minimal/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ CONFIG_LK_EXAMPLE_WIFI_SSID="<your SSID>"
3434
CONFIG_LK_EXAMPLE_WIFI_PASSWORD="<your password>"
3535
```
3636

37+
Or using Ethernet (ESP32-P4 only):
38+
39+
```ini
40+
CONFIG_LK_EXAMPLE_USE_ETHERNET=y
41+
```
42+
3743
### Development Board
3844

3945
This example uses the Espressif [*codec_board*](https://components.espressif.com/components/tempotian/codec_board/) component to access board-specific peripherals for media capture and rendering. Supported boards are [defined here](https://github.com/espressif/esp-webrtc-solution/blob/65d13427dd83c37264b6cff966d60af0f84f649c/components/codec_board/board_cfg.txt). Locate the name of your board, and set it as follows:

components/livekit/examples/minimal_video/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ CONFIG_LK_EXAMPLE_WIFI_SSID="<your SSID>"
3434
CONFIG_LK_EXAMPLE_WIFI_PASSWORD="<your password>"
3535
```
3636

37+
Or using Ethernet (ESP32-P4 only):
38+
39+
```ini
40+
CONFIG_LK_EXAMPLE_USE_ETHERNET=y
41+
```
42+
3743
### Development Board
3844

3945
This example uses the Espressif [*codec_board*](https://components.espressif.com/components/tempotian/codec_board/) component to access board-specific peripherals for media capture and rendering. Supported boards are [defined here](https://github.com/espressif/esp-webrtc-solution/blob/65d13427dd83c37264b6cff966d60af0f84f649c/components/codec_board/board_cfg.txt). Locate the name of your board, and set it as follows:

components/livekit/examples/voice_agent/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ CONFIG_LK_EXAMPLE_WIFI_SSID="<your SSID>"
4848
CONFIG_LK_EXAMPLE_WIFI_PASSWORD="<your password>"
4949
```
5050

51+
Or using Ethernet (ESP32-P4 only):
52+
53+
```ini
54+
CONFIG_LK_EXAMPLE_USE_ETHERNET=y
55+
```
56+
5157
### Development Board
5258

5359
By default, this example targets the [ESP32-S3-Korvo-2](https://docs.espressif.com/projects/esp-adf/en/latest/design-guide/dev-boards/user-guide-esp32-s3-korvo-2.html) development board, using its corresponding [board support package](https://components.espressif.com/components/espressif/esp32_s3_korvo_2/) (BSP) to access the LED peripherals for the agent to control. If you wish to target a different board, this dependency can be easily removed or replaced.

0 commit comments

Comments
 (0)