-
Notifications
You must be signed in to change notification settings - Fork 49
/
bridge_usb.c
312 lines (271 loc) · 8.91 KB
/
bridge_usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/fcntl.h>
#include <sys/errno.h>
#include <sys/unistd.h>
#include <sys/select.h>
#include "lwip/opt.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/ethip6.h"
#include "netif/etharp.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_netif_net_stack.h"
#include "esp_bridge_internal.h"
#include "tinyusb.h"
#include "tusb_net.h"
/* Define those to better describe your network interface. */
#define IFNAME0 'u'
#define IFNAME1 's'
static const char* TAG = "bridge_usb";
esp_err_t pkt_netif2usb(void *buffer, uint16_t len);
esp_netif_t* usb_netif;
static esp_err_t usb_netif_dhcp_status_change_cb(esp_ip_addr_t *ip_info)
{
#ifdef CONFIG_TINYUSB_NET_ECM
if (tud_connected()) {
ecm_close();
ecm_open();
}
#else
ESP_LOGE(TAG, "You need to reset the USB Nic to get the new IP");
ESP_LOGE(TAG, "If you want automatic reset, please use USB CDC-ECM");
#endif
return ESP_OK;
}
static void esp_bridge_usb_init(void)
{
tusb_net_init();
ESP_LOGI(TAG, "USB initialization");
tinyusb_config_t tusb_cfg = {
.external_phy = false // In the most cases you need to use a `false` value
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_LOGI(TAG, "USB initialization DONE");
}
struct esp_netif_lwip_vanilla_config {
err_t (*init_fn)(struct netif*);
void (*input_fn)(void *netif, void *buffer, size_t len, void *eb);
};
/**
* In this function, the hardware should be initialized.
* Invoked by ethernetif_init().
*
* @param netif lwip network interface which has already been initialized
*/
static void usb_low_level_init(struct netif *netif)
{
/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
/* maximum transfer unit */
netif->mtu = 1500;
/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
#if ESP_LWIP
#if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
#endif
#endif
#if ESP_IPV6
#if LWIP_IPV6 && LWIP_IPV6_MLD
netif->flags |= NETIF_FLAG_MLD6;
#endif
#endif
}
/**
* @brief This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf might be chained.
*
* @param netif lwip network interface structure for this ethernetif
* @param p MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return err_t ERR_OK if the packet has been sent to buffer successfully
* ERR_MEM if private data couldn't be allocated
* ERR_IF if netif is not supported
* ERR_ABORT if there's some wrong when send pbuf payload to DMA buffer
*/
static err_t usb_low_level_output(struct netif *netif, struct pbuf *p)
{
struct pbuf *q = p;
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
esp_err_t ret = ESP_FAIL;
if (!esp_netif) {
LWIP_DEBUGF(NETIF_DEBUG, ("corresponding esp-netif is NULL: netif=%p pbuf=%p len=%d\n", netif, p, p->len));
return ERR_IF;
}
if(q->next == NULL) {
ret = esp_netif_transmit(esp_netif, q->payload, q->len);
} else {
LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
if (q != NULL) {
pbuf_copy(q, p);
} else {
return ERR_MEM;
}
ret = esp_netif_transmit(esp_netif, q->payload, q->len);
pbuf_free(q);
}
if (ret == ESP_OK) {
return ERR_OK;
} else if (ret == ESP_ERR_NO_MEM) {
return ERR_MEM;
} else if (ret == ESP_ERR_INVALID_ARG) {
return ERR_ARG;
} else {
return ERR_IF;
}
}
/**
* @brief This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that
* should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
*
* @param netif lwip network interface structure for this ethernetif
* @param buffer ethernet buffer
* @param len length of buffer
*/
void usb_input(void *h, void *buffer, size_t len, void *l2_buff)
{
struct netif * netif = h;
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
struct pbuf *p;
if(unlikely(!buffer || !netif_is_up(netif))) {
if (l2_buff) {
esp_netif_free_rx_buffer(esp_netif, l2_buff);
}
return;
}
#ifdef CONFIG_LWIP_L2_TO_L3_COPY
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
if (p == NULL) {
esp_netif_free_rx_buffer(esp_netif, l2_buff);
return;
}
memcpy(p->payload, buffer, len);
esp_netif_free_rx_buffer(esp_netif, l2_buff);
#else
p = esp_pbuf_allocate(esp_netif, buffer, len, l2_buff);
if (p == NULL) {
esp_netif_free_rx_buffer(esp_netif, l2_buff);
return;
}
#endif
/* full packet send to tcpip_thread to process */
if (unlikely(netif->input(p, netif) != ERR_OK)) {
LWIP_DEBUGF(NETIF_DEBUG, ("wlanif_input: IP input error\n"));
pbuf_free(p);
}
}
/**
* Set up the network interface. It calls the function low_level_init() to do the
* actual init work of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the ethernetif is initialized
*/
err_t usb_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
#if ESP_LWIP
if (esp_netif_get_hostname(esp_netif_get_handle_from_netif_impl(netif), &netif->hostname) != ESP_OK) {
netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME;
}
#else
netif->hostname = "lwip";
#endif
#endif /* LWIP_NETIF_HOSTNAME */
/*
* Initialize the snmp variables and counters inside the struct netif.
* The last argument should be replaced with your link speed, in units
* of bits per second.
*/
NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100);
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */
netif->output = etharp_output;
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
netif->linkoutput = usb_low_level_output;
usb_low_level_init(netif);
return ERR_OK;
}
static void usb_driver_free_rx_buffer(void *h, void* buffer)
{
if (buffer) {
free(buffer);
}
}
static esp_err_t usb_io_transmit(void *h, void *buffer, size_t len)
{
// send data to usb driver
pkt_netif2usb(buffer, len);
return ESP_OK;
}
static esp_err_t usb_io_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
{
return usb_io_transmit(h, buffer, len);
}
static esp_netif_driver_ifconfig_t usb_driver_ifconfig = {
.driver_free_rx_buffer = usb_driver_free_rx_buffer,
.transmit = usb_io_transmit,
.transmit_wrap = usb_io_transmit_wrap,
.handle = "USB" // this IO object is a singleton, its handle uses as a name
};
static const struct esp_netif_lwip_vanilla_config usb_netstack_config = {
.init_fn = usb_init,
.input_fn = usb_input
};
esp_netif_t* esp_bridge_create_usb_netif(esp_netif_ip_info_t* ip_info, uint8_t mac[6], bool data_forwarding, bool enable_dhcps)
{
esp_netif_ip_info_t netif_ip_info = { 0 };
const esp_netif_inherent_config_t esp_netif_common_config = {
.flags = (ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED),
.get_ip_event = IP_EVENT_STA_GOT_IP,
.lost_ip_event = IP_EVENT_STA_LOST_IP,
.if_key = "USB_key",
.if_desc = "USB_Netif"
};
esp_netif_config_t usb_config = {
.base = &esp_netif_common_config,
.driver = &usb_driver_ifconfig,
.stack = (const esp_netif_netstack_config_t*)&usb_netstack_config
};
if (!data_forwarding) {
return NULL;
}
esp_netif_t* netif = esp_bridge_create_netif(&usb_config, ip_info, mac, enable_dhcps);
if (netif) {
esp_bridge_usb_init();
esp_bridge_netif_list_add(netif, usb_netif_dhcp_status_change_cb);
if (data_forwarding) {
esp_netif_get_ip_info(netif, &netif_ip_info);
ESP_LOGI(TAG, "USB IP Address:" IPSTR, IP2STR(&netif_ip_info.ip));
ip_napt_enable(netif_ip_info.ip.addr, 1);
}
}
usb_netif = netif;
return netif;
}