企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
该函数是直接拿来用即可,如果没有特别的需求,基本不需要怎么修改它,它是LwIP中默认的网卡初始化函数,内部封装了low\_level\_init()函数,具体见代码清单 4‑7。 ``` 1 2 err_t ethernetif_init(struct netif *netif) 3 { 4 struct ethernetif *ethernetif; 5 6 // LWIP_ASSERT("netif != NULL", (netif != NULL)); 7 8 ethernetif = mem_malloc(sizeof(struct ethernetif)); 9 10 if (ethernetif == NULL) 11 { 12 PRINT_ERR("ethernetif_init: out of memory\n"); 13 return ERR_MEM; 14 } 15 16 LWIP_ASSERT("netif != NULL", (netif != NULL)); 17 // 18 #if LWIP_NETIF_HOSTNAME 19 /* Initialize interface hostname */ 20 netif->hostname = "lwip"; 21 #endif /* LWIP_NETIF_HOSTNAME */ 22 netif->state = ethernetif; (1) 23 netif->name[0] = IFNAME0; 24 netif->name[1] = IFNAME1; 25 26 #if LWIP_IPV4 27 #if LWIP_ARP || LWIP_ETHERNET 28 #if LWIP_ARP 29 netif->output = etharp_output; 30 #else 31 32 netif->output = low_level_output_arp_off; 33 #endif /* LWIP_ARP */ 34 #endif /* LWIP_ARP || LWIP_ETHERNET */ 35 #endif /* LWIP_IPV4 */ 36 37 #if LWIP_IPV6 38 netif->output_ip6 = ethip6_output; 39 #endif /* LWIP_IPV6 */ 40 41 netif->linkoutput = low_level_output; 42 43 /* initialize the hardware */ 44 low_level_init(netif); (2) 45 ethernetif->ethaddr = (struct eth_addr *) &(netif->hwaddr[0]); 46 47 return ERR_OK; 48 } ``` * (1):通过netif的state成员变量将ethernetif结构传递给上层。 * (2):调用low\_level\_init()函数对网卡进行初始化,而该函数需要我们根据网卡的实际情况进行编写