1 /*
2  * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdlib.h>
7 #include "esp_netif_br_glue.h"
8 #include "esp_eth_driver.h"
9 #include "esp_event.h"
10 #include "esp_log.h"
11 #include "esp_check.h"
12 
13 #if CONFIG_ESP_NETIF_BRIDGE_EN
14 
15 const static char *TAG = "esp_netif_br_glue";
16 
17 typedef struct esp_netif_br_glue_t esp_netif_br_glue_t;
18 
19 struct esp_netif_br_glue_t {
20     esp_netif_driver_base_t base;
21     bool br_started;
22     esp_netif_t **ports_esp_netifs;
23     uint8_t port_cnt;
24     esp_event_handler_instance_t eth_start_ctx_handler;
25     esp_event_handler_instance_t eth_stop_ctx_handler;
26     esp_event_handler_instance_t eth_connect_ctx_handler;
27     esp_event_handler_instance_t eth_disconnect_ctx_handler;
28     esp_event_handler_instance_t get_ip_ctx_handler;
29 };
30 
esp_eth_post_attach_br(esp_netif_t * esp_netif,void * args)31 static esp_err_t esp_eth_post_attach_br(esp_netif_t *esp_netif, void *args)
32 {
33     uint8_t eth_mac[6];
34     esp_netif_br_glue_t *netif_glue = (esp_netif_br_glue_t *)args;
35     netif_glue->base.netif = esp_netif;
36 
37     esp_netif_get_mac(esp_netif, eth_mac);
38     ESP_LOGI(TAG, "%02x:%02x:%02x:%02x:%02x:%02x", eth_mac[0], eth_mac[1],
39              eth_mac[2], eth_mac[3], eth_mac[4], eth_mac[5]);
40 
41     ESP_LOGI(TAG, "bridge netif glue attached");
42 
43     return ESP_OK;
44 }
45 
eth_action_start(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)46 static void eth_action_start(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
47 {
48     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
49     esp_netif_br_glue_t *netif_glue = handler_args;
50     ESP_LOGD(TAG, "eth_action_start: %p, %p, %d, %p, %p", netif_glue, base, event_id, event_data, *(esp_eth_handle_t *)event_data);
51 
52     for (int i = 0; i < netif_glue->port_cnt; i++) {
53         if (eth_handle == esp_netif_get_io_driver(netif_glue->ports_esp_netifs[i])) {
54             if (netif_glue->br_started == false) {
55                 esp_netif_action_start(netif_glue->base.netif, base, event_id, event_data); // basically creates lwip_netif br instance
56                 netif_glue->br_started = true;
57                 ESP_LOGD(TAG, "bridge netif %p is started", netif_glue->base.netif);
58             }
59             esp_netif_bridge_add_port(netif_glue->base.netif, netif_glue->ports_esp_netifs[i]);
60         }
61     }
62 }
63 
eth_action_stop(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)64 static void eth_action_stop(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
65 {
66     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
67     esp_netif_br_glue_t *netif_glue = handler_args;
68     ESP_LOGD(TAG, "eth_action_stop: %p, %p, %d, %p, %p", netif_glue, base, event_id, event_data, *(esp_eth_handle_t *)event_data);
69 
70     for (int i = 0; i < netif_glue->port_cnt; i++) {
71         // if one of the bridge's ports is stopped, we need to stop the bridge too, since port's lwip_netif is removed and so it would become
72         // an invalid reference in the bridge's internal structure (there is no way how to remove single port from bridge in current LwIP)
73         if (eth_handle == esp_netif_get_io_driver(netif_glue->ports_esp_netifs[i])) {
74             if (netif_glue->br_started == true) {
75                 esp_netif_action_stop(netif_glue->base.netif, base, event_id, event_data); // basically removes lwip_netif br
76                 netif_glue->br_started = false;
77                 ESP_LOGD(TAG, "bridge netif %p is stopped", netif_glue->base.netif);
78             }
79         }
80     }
81 }
82 
eth_action_connected(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)83 static void eth_action_connected(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
84 {
85     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
86     esp_netif_br_glue_t *netif_glue = handler_args;
87     ESP_LOGD(TAG, "eth_action_connected: %p, %p, %d, %p, %p", netif_glue, base, event_id, event_data, *(esp_eth_handle_t *)event_data);
88 
89     // if bridge interface is already up, do nothing
90     if (esp_netif_is_netif_up(netif_glue->base.netif) == true) {
91         return;
92     }
93 
94     for (int i = 0; i < netif_glue->port_cnt; i++) {
95         if (eth_handle == esp_netif_get_io_driver(netif_glue->ports_esp_netifs[i])) {
96             esp_netif_action_connected(netif_glue->base.netif, base, event_id, event_data);
97             ESP_LOGD(TAG, "bridge netif %p is connected", netif_glue->base.netif);
98             break;
99         }
100     }
101 }
102 
eth_action_disconnected(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)103 static void eth_action_disconnected(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
104 {
105     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
106     esp_netif_br_glue_t *netif_glue = handler_args;
107     ESP_LOGD(TAG, "eth_action_disconnected: %p, %p, %d, %p, %p", netif_glue, base, event_id, event_data, *(esp_eth_handle_t *)event_data);
108 
109     for (int i = 0; i < netif_glue->port_cnt; i++) {
110         // if this is a Ethernet interface associated with bridge, check if other ports are disconnected
111         if (eth_handle == esp_netif_get_io_driver(netif_glue->ports_esp_netifs[i])) {
112             int disc_cnt;
113             for (disc_cnt = 0; disc_cnt < netif_glue->port_cnt; disc_cnt++) {
114                 if (esp_netif_is_netif_up(netif_glue->ports_esp_netifs[disc_cnt]) == true) {
115                     break;
116                 }
117             }
118             // if all ports are disconnected, set bridge as disconnected too
119             if (disc_cnt >= netif_glue->port_cnt) {
120                 esp_netif_action_disconnected(netif_glue->base.netif, base, event_id, event_data);
121                 ESP_LOGD(TAG, "bridge netif %p is disconnected", netif_glue->base.netif);
122             }
123         }
124     }
125 }
126 
br_action_got_ip(void * handler_args,esp_event_base_t base,int32_t event_id,void * event_data)127 static void br_action_got_ip(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
128 {
129     ip_event_got_ip_t *ip_event = (ip_event_got_ip_t *)event_data;
130     esp_netif_br_glue_t *netif_glue = handler_args;
131     ESP_LOGD(TAG, "br_action_got_ip: %p, %p, %d, %p, %p", netif_glue, base, event_id, event_data, *(esp_eth_handle_t *)event_data);
132     if (netif_glue->base.netif == ip_event->esp_netif) {
133         esp_netif_action_got_ip(ip_event->esp_netif, base, event_id, event_data);
134     }
135 }
136 
esp_netif_br_glue_clear_instance_handlers(esp_netif_br_glue_handle_t esp_netif_br_glue)137 static esp_err_t esp_netif_br_glue_clear_instance_handlers(esp_netif_br_glue_handle_t esp_netif_br_glue)
138 {
139     ESP_RETURN_ON_FALSE(esp_netif_br_glue, ESP_ERR_INVALID_ARG, TAG, "esp_netif_br_glue handle can't be null");
140 
141     if (esp_netif_br_glue->eth_start_ctx_handler) {
142         esp_event_handler_instance_unregister(ETH_EVENT, ETHERNET_EVENT_START, esp_netif_br_glue->eth_start_ctx_handler);
143         esp_netif_br_glue->eth_start_ctx_handler = NULL;
144     }
145 
146     if (esp_netif_br_glue->eth_stop_ctx_handler) {
147         esp_event_handler_instance_unregister(ETH_EVENT, ETHERNET_EVENT_STOP, esp_netif_br_glue->eth_stop_ctx_handler);
148         esp_netif_br_glue->eth_stop_ctx_handler = NULL;
149     }
150 
151     if (esp_netif_br_glue->eth_connect_ctx_handler) {
152         esp_event_handler_instance_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, esp_netif_br_glue->eth_connect_ctx_handler);
153         esp_netif_br_glue->eth_connect_ctx_handler = NULL;
154     }
155 
156     if (esp_netif_br_glue->eth_disconnect_ctx_handler) {
157         esp_event_handler_instance_unregister(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, esp_netif_br_glue->eth_disconnect_ctx_handler);
158         esp_netif_br_glue->eth_disconnect_ctx_handler = NULL;
159     }
160 
161     if (esp_netif_br_glue->get_ip_ctx_handler) {
162         esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, esp_netif_br_glue->get_ip_ctx_handler);
163         esp_netif_br_glue->get_ip_ctx_handler = NULL;
164     }
165 
166     return ESP_OK;
167 }
168 
esp_netif_br_glue_set_instance_handlers(esp_netif_br_glue_handle_t esp_netif_br_glue)169 static esp_err_t esp_netif_br_glue_set_instance_handlers(esp_netif_br_glue_handle_t esp_netif_br_glue)
170 {
171     ESP_RETURN_ON_FALSE(esp_netif_br_glue, ESP_ERR_INVALID_ARG, TAG, "esp_netif_br_glue handle can't be null");
172 
173     esp_err_t ret = esp_event_handler_instance_register(ETH_EVENT, ETHERNET_EVENT_START, eth_action_start, esp_netif_br_glue, &esp_netif_br_glue->eth_start_ctx_handler);
174     if (ret != ESP_OK) {
175         goto fail;
176     }
177 
178     ret = esp_event_handler_instance_register(ETH_EVENT, ETHERNET_EVENT_STOP, eth_action_stop, esp_netif_br_glue, &esp_netif_br_glue->eth_stop_ctx_handler);
179     if (ret != ESP_OK) {
180         goto fail;
181     }
182 
183     ret = esp_event_handler_instance_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, eth_action_connected, esp_netif_br_glue, &esp_netif_br_glue->eth_connect_ctx_handler);
184     if (ret != ESP_OK) {
185         goto fail;
186     }
187 
188     ret = esp_event_handler_instance_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, eth_action_disconnected, esp_netif_br_glue, &esp_netif_br_glue->eth_disconnect_ctx_handler);
189     if (ret != ESP_OK) {
190         goto fail;
191     }
192 
193     ret = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, br_action_got_ip, esp_netif_br_glue, &esp_netif_br_glue->get_ip_ctx_handler);
194     if (ret != ESP_OK) {
195         goto fail;
196     }
197 
198     return ESP_OK;
199 
200 fail:
201     esp_netif_br_glue_clear_instance_handlers(esp_netif_br_glue);
202     return ret;
203 }
204 
esp_netif_br_glue_new(void)205 esp_netif_br_glue_handle_t esp_netif_br_glue_new(void)
206 {
207     esp_netif_br_glue_t *netif_glue = calloc(1, sizeof(esp_netif_br_glue_t));
208     if (!netif_glue) {
209         ESP_LOGE(TAG, "create netif glue failed");
210         return NULL;
211     }
212 
213     netif_glue->base.post_attach = esp_eth_post_attach_br;
214 
215     if (esp_netif_br_glue_set_instance_handlers(netif_glue) != ESP_OK) {
216         esp_netif_br_glue_del(netif_glue);
217         return NULL;
218     }
219 
220     return netif_glue;
221 }
222 
esp_netif_br_glue_add_port(esp_netif_br_glue_handle_t netif_br_glue,esp_netif_t * esp_netif_port)223 esp_err_t esp_netif_br_glue_add_port(esp_netif_br_glue_handle_t netif_br_glue, esp_netif_t *esp_netif_port)
224 {
225     if (netif_br_glue->ports_esp_netifs == NULL) {
226         netif_br_glue->ports_esp_netifs = malloc(sizeof(esp_netif_t *));
227     } else {
228         netif_br_glue->ports_esp_netifs = realloc(netif_br_glue->ports_esp_netifs, (netif_br_glue->port_cnt + 1) * sizeof(esp_netif_t *));
229     }
230     if (!netif_br_glue->ports_esp_netifs) {
231         ESP_LOGE(TAG, "no memory to add br port");
232         return ESP_ERR_NO_MEM;
233     }
234 
235     netif_br_glue->ports_esp_netifs[netif_br_glue->port_cnt] = esp_netif_port;
236     netif_br_glue->port_cnt++;
237 
238     return ESP_OK;
239 }
240 
esp_netif_br_glue_del(esp_netif_br_glue_handle_t netif_br_glue)241 esp_err_t esp_netif_br_glue_del(esp_netif_br_glue_handle_t netif_br_glue)
242 {
243     esp_netif_br_glue_clear_instance_handlers(netif_br_glue);
244     free(netif_br_glue->ports_esp_netifs);
245     free(netif_br_glue);
246     netif_br_glue = NULL;
247     return ESP_OK;
248 }
249 
250 #endif // CONFIG_ESP_NETIF_BRIDGE_EN
251