@@ -17,17 +17,26 @@ static char* TAG = "mac.c";
17
17
18
18
typedef struct openmac_netif_driver {
19
19
esp_netif_driver_base_t base ;
20
- mac_interface_type_t interface_type ;
20
+ rs_mac_interface_type_t interface_type ;
21
21
} openmac_netif_driver_t ;
22
22
23
- static esp_netif_t * netif_openmac_sta = NULL ;
23
+ static openmac_netif_driver_t * active_interfaces [ NUM_VIRTUAL_INTERFACES ] = { 0 } ;
24
24
25
25
static esp_err_t openmac_netif_transmit (void * h , void * buffer , size_t len )
26
26
{
27
- uint8_t * eth_data = (uint8_t * ) buffer ;
28
- ESP_LOGI ("netif-tx" , "Going to transmit a data packet: to " MACSTR " from " MACSTR " type=%02x%02x" , MAC2STR (& eth_data [0 ]), MAC2STR (& eth_data [6 ]), eth_data [12 ], eth_data [13 ]);
29
- c_transmit_data_frame (buffer , len );
30
- return ESP_OK ;
27
+ openmac_netif_driver_t * driver = (openmac_netif_driver_t * )h ;
28
+
29
+ for (int i = 0 ; i < NUM_VIRTUAL_INTERFACES ; i ++ ) {
30
+ if (active_interfaces [i ] != NULL && active_interfaces [i ] == driver ) {
31
+ uint8_t * eth_data = (uint8_t * ) buffer ;
32
+ ESP_LOGI ("netif-tx" , "Going to transmit a data packet: to " MACSTR " from " MACSTR " type=%02x%02x" , MAC2STR (& eth_data [0 ]), MAC2STR (& eth_data [6 ]), eth_data [12 ], eth_data [13 ]);
33
+ c_transmit_data_frame (active_interfaces [i ]-> interface_type , buffer , len );
34
+ return ESP_OK ;
35
+ }
36
+ }
37
+ ESP_LOGE (TAG , "netif_tx: failed to find vif for handle %p" , h );
38
+
39
+ return ESP_FAIL ;
31
40
}
32
41
33
42
static esp_err_t openmac_netif_transmit_wrap (void * h , void * buffer , size_t len , void * netstack_buf )
@@ -36,18 +45,38 @@ static esp_err_t openmac_netif_transmit_wrap(void *h, void *buffer, size_t len,
36
45
}
37
46
38
47
39
- void openmac_netif_up () {
40
- esp_netif_action_connected (netif_openmac_sta , NULL , 0 , NULL );
48
+ void openmac_netif_up (rs_mac_interface_type_t interface ) {
49
+ for (int i = 0 ; i < NUM_VIRTUAL_INTERFACES ; i ++ ) {
50
+ if (active_interfaces [i ] != NULL && active_interfaces [i ]-> interface_type == interface ) {
51
+ esp_netif_action_connected (active_interfaces [i ]-> base .netif , NULL , 0 , NULL );
52
+ return ;
53
+ }
54
+ }
55
+ ESP_LOGE (TAG , "trying to up vif %d but not active" , interface );
41
56
}
42
57
43
- void openmac_netif_down () {
44
- esp_netif_action_disconnected (netif_openmac_sta , NULL , 0 , NULL );
58
+ void openmac_netif_down (rs_mac_interface_type_t interface ) {
59
+ for (int i = 0 ; i < NUM_VIRTUAL_INTERFACES ; i ++ ) {
60
+ if (active_interfaces [i ] != NULL && active_interfaces [i ]-> interface_type == interface ) {
61
+ esp_netif_action_disconnected (active_interfaces [i ]-> base .netif , NULL , 0 , NULL );
62
+ return ;
63
+ }
64
+ }
65
+ ESP_LOGE (TAG , "trying to down vif %d but not active" , interface );
45
66
}
46
67
47
68
// Put Ethernet-formatted frame in MAC stack; does not take ownership of the buffer: after the function returns, you can delete/reuse it.
48
- void openmac_netif_receive (void * buffer , size_t len ) {
69
+ void openmac_netif_receive (rs_mac_interface_type_t interface , void * buffer , size_t len ) {
49
70
assert (buffer != NULL );
50
- esp_netif_receive (netif_openmac_sta , buffer , len , buffer );
71
+
72
+ for (int i = 0 ; i < NUM_VIRTUAL_INTERFACES ; i ++ ) {
73
+ if (active_interfaces [i ] != NULL && active_interfaces [i ]-> interface_type == interface ) {
74
+ esp_netif_receive (active_interfaces [i ]-> base .netif , buffer , len , buffer );
75
+ return ;
76
+ }
77
+ }
78
+ // If we get here, the MAC stack passed us a frame that does not have a currently active interface
79
+ ESP_LOGE (TAG , "received frame for vif %d but not active" , interface );
51
80
}
52
81
53
82
// Free RX buffer
@@ -92,14 +121,19 @@ esp_err_t openmac_netif_start()
92
121
.base = & base_cfg_sta ,
93
122
.driver = NULL ,
94
123
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
95
- netif_openmac_sta = esp_netif_new (& cfg_sta );
124
+
125
+ esp_netif_t * netif_openmac_sta = esp_netif_new (& cfg_sta );
96
126
assert (netif_openmac_sta );
97
127
98
128
openmac_netif_driver_t * driver = openmac_create_if_driver ();
99
129
if (driver == NULL ) {
100
130
ESP_LOGE (TAG , "Failed to create wifi interface handle" );
101
131
return ESP_FAIL ;
102
132
}
133
+
134
+ driver -> interface_type = STA_1_MAC_INTERFACE_TYPE ;
135
+ active_interfaces [0 ] = driver ;
136
+
103
137
esp_netif_attach (netif_openmac_sta , driver );
104
138
esp_netif_set_hostname (netif_openmac_sta , "esp32-open-mac" );
105
139
esp_netif_set_mac (netif_openmac_sta , module_mac_addr );
0 commit comments