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
3947static 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-
7365static 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
93123static 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
153252bool 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 ();
0 commit comments