4646#include "shared/netutils/netutils.h"
4747#include "shared/netutils/dhcpserver.h"
4848
49+ #if LWIP_IPV6
50+ #define IP (x ) ((x).u_addr.ip4)
51+ #else
52+ #define IP (x ) (x)
53+ #endif
4954
5055typedef struct _usbnet_obj_t {
5156 mp_obj_base_t base ;
@@ -62,6 +67,7 @@ typedef struct _usbnet_obj_t {
6267 ip_addr_t ipaddr ;
6368 ip_addr_t netmask ;
6469 ip_addr_t gateway ;
70+ ip_addr_t dns ;
6571
6672} usbnet_obj_t ;
6773
@@ -115,7 +121,7 @@ static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t
115121static err_t netif_init_cb (struct netif * netif ) {
116122 LWIP_ASSERT ("netif != NULL" , (netif != NULL ));
117123 netif -> mtu = CFG_TUD_NET_MTU ;
118- netif -> flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP ;
124+ netif -> flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP ;
119125 netif -> state = NULL ;
120126 netif -> name [0 ] = 'E' ;
121127 netif -> name [1 ] = 'X' ;
@@ -124,29 +130,78 @@ static err_t netif_init_cb(struct netif *netif) {
124130 #if LWIP_IPV6
125131 netif -> output_ip6 = ip6_output_fn ;
126132 #endif
133+ #if LWIP_IGMP
134+ // netif_set_igmp_mac_filter(netif, cyw43_netif_update_igmp_mac_filter);
135+ #endif
127136 return ERR_OK ;
128137}
129138
139+ #if MICROPY_HW_NETWORK_USBNET_DHCP_SERVER
140+ void dhcp_client_connected (uint8_t ciaddr [4 ], uint8_t chaddr [16 ]) {
141+ // When a host pc connects and is assigned an ip address, use that host
142+ // as the default gateway. This means the host could optionally be configured
143+ // to forward internet to the usb network adaptor and this device will be able to use it.
144+ ip4_addr_t gw4 ;
145+ IP4_ADDR (& gw4 , ciaddr [0 ], ciaddr [1 ], ciaddr [2 ], ciaddr [3 ]);
146+ netif_set_gw (& (usbnet_obj .netif ), & gw4 );
147+ }
148+ #endif
149+
150+ // #if LWIP_IGMP
151+ // static err_t cyw43_netif_update_igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action) {
152+ // cyw43_t *self = netif->state;
153+ // uint8_t mac[] = { 0x01, 0x00, 0x5e, ip4_addr2(group) & 0x7F, ip4_addr3(group), ip4_addr4(group) };
154+
155+ // if (action != IGMP_ADD_MAC_FILTER && action != IGMP_DEL_MAC_FILTER) {
156+ // return ERR_VAL;
157+ // }
158+
159+ // // if (cyw43_wifi_update_multicast_filter(self, mac, action == IGMP_ADD_MAC_FILTER)) {
160+ // // return ERR_IF;
161+ // // }
162+
163+ // return ERR_OK;
164+ // }
165+ // #endif
166+
167+ // #if LWIP_IPV6
168+ // static err_t cyw43_macfilter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action) {
169+ // uint8_t address[6] = { 0x33, 0x33 };
170+ // memcpy(address + 2, group->addr + 3, 4);
171+ // if (action != NETIF_ADD_MAC_FILTER && action != NETIF_DEL_MAC_FILTER) {
172+ // return ERR_VAL;
173+ // }
174+ // if (cyw43_wifi_update_multicast_filter(netif->state, address, action == NETIF_ADD_MAC_FILTER)) {
175+ // return ERR_IF;
176+ // }
177+ // return ERR_OK;
178+ // }
179+ // #endif
180+
130181void usbnet_init (void ) {
131182 struct netif * netif = & (usbnet_obj .netif );
132183
133184 MICROPY_PY_LWIP_ENTER
134185
135186 mp_hal_get_mac (MP_HAL_MAC_ETH0 , tud_network_mac_address );
187+ tud_network_mac_address [0 ] = 0x02 ;
188+ // tud_network_mac_address[1] = 0x00;
189+ // tud_network_mac_address[2] = 0x5E;
190+ // tud_network_mac_address[5] ^= 0x1;
136191
137192 netif -> hwaddr_len = sizeof (tud_network_mac_address );
138193 memcpy (netif -> hwaddr , tud_network_mac_address , sizeof (tud_network_mac_address ));
139- netif -> hwaddr [5 ] ^= 0x01 ;
140194
141- #if LWIP_IPV6
142- #define IP ( x ) ((x).u_addr.ip4)
143- #else
144- #define IP ( x ) (x)
145- #endif
195+ // This USB Network essentially creates a link with two interfaces, one for the
196+ // host and another for the micropython/lwip end of the connection.
197+ // The MAC configured above is use for one end of the link, flip a bit for the
198+ // MAC on the other end of the link.
199+ netif -> hwaddr [ 5 ] ^= 0x01 ;
146200
147201 IP (usbnet_obj .ipaddr ).addr = PP_HTONL (MICROPY_HW_NETWORK_USBNET_IPADDRESS );
148202 IP (usbnet_obj .netmask ).addr = PP_HTONL (MICROPY_HW_NETWORK_USBNET_SUBNET );
149203 IP (usbnet_obj .gateway ).addr = PP_HTONL (MICROPY_HW_NETWORK_USBNET_GATEWAY );
204+ IP (usbnet_obj .dns ).addr = PP_HTONL (MICROPY_HW_NETWORK_USBNET_DNS );
150205
151206 netif = netif_add (
152207 netif ,
@@ -160,15 +215,28 @@ void usbnet_init(void) {
160215 #if LWIP_IPV6
161216 netif_create_ip6_linklocal_address (netif , 1 );
162217 #endif
218+ #if LWIP_NETIF_HOSTNAME
219+ netif_set_hostname (netif , mod_network_hostname_data );
220+ #endif
163221 netif_set_default (netif );
222+ netif_set_up (netif );
223+ netif_set_link_up (netif );
224+
225+ #if LWIP_MDNS_RESPONDER
226+ mdns_resp_add_netif (netif , mod_network_hostname_data );
227+ #endif
228+
229+ #if LWIP_DNS
230+ dns_setserver (0 , & usbnet_obj .dns );
231+ #endif
164232
165233 #if MICROPY_HW_NETWORK_USBNET_DHCP_SERVER
166234 dhcp_server_init (& usbnet_obj .dhcp_server , & usbnet_obj .ipaddr , & usbnet_obj .netmask );
235+ dhcp_server_register_connect_cb (& usbnet_obj .dhcp_server , & dhcp_client_connected );
167236 #endif
168237
169- #if LWIP_MDNS_RESPONDER
170- mdns_resp_add_netif (netif , mod_network_hostname_data );
171- #endif
238+ // register with network module
239+ mod_network_register_nic (& usbnet_obj );
172240
173241 MICROPY_PY_LWIP_EXIT
174242}
0 commit comments