LwIP 之二 网络接口 netif(netif.c、ethernetif( 三 )


struct ethernetif{struct eth_addr *ethaddr;/* Add whatever per-interface state that is needed here. */};
该结构用来描述底层硬件设备 , 该结构体唯一不可或缺的是 MAC 地址 , 它是 LWIP 用于相应 ARP 查询的核心数据 。其他如果没有特殊需要 , 可以不添加其他成员数据 。主要用于将底层硬件设备的一些私有数据通过 netif ->state 传递给上层(在函数中赋值netif->state = ;) 。一般用不到该结构 。因为在 netif 结构中 , 基本包含了所有需要的信息 。
接口
接下来 , 我们再来看看 ,  都需要提供那些接口 。其中 , 大部分接口都是的内部函数 , 对外的即可其实没有几个 。注意 , 有些接口并没有固定的原型 , 可以根据自己的需要来修改 。下面挨个来介绍一下每个接口 。
默认接口
以下接口是模板默认的一些接口:
void ( netif *netif); 对网卡硬件进行一系列的初始化工作 。
err_t ( netif *netif,pbuf *p); 该接口实现将协议栈的数据发送到网卡硬件(通常是 MAC)
pbuf * ( netif *netif); 该接口实现将网卡硬件(通常是 MAC)收到的数据转移到一个 pbuf 内存中 。
void ( netif *netif); 该接口实现将带有数据的 pbuf 传递到 LwIP 协议栈中 。
err_t ( netif *netif); 该接口是网络硬件的初始化接口
额外接口
以下接口是我们在实际使用中额外添加的一些接口:
连接状态变更检测接口连接状态变更回调接口 。该接口用来动态改变底层硬件参数示例
注意 , .h/c 的主要作用就是把网络硬件底层的信息进行屏蔽 , 例如 , 只有在这一层我们才会访问 STM32 eth 驱动库 。
带系统的示例
在使用操作系统时 , 该层使用的操作系统接口也应该使用 .c 中封装的接口 , 从而实现操作系统屏蔽 。
.h
/********************************************************************************* @fileethernetif.h* @authorZCShou* @version 1.0.0* @date2020.10.6* @briefThis file provides initialization code for LWIP middleWare.******************************************************************************* @attention* * © COPYRIGHT 2022 ZCShou **(1) **(2) **(3) ********************************************************************************/#ifndef __ETHERNETIF_H__#define __ETHERNETIF_H__#ifdef __cplusplusextern "C" {#endif/* Includes ------------------------------------------------------------------*/#include "lwip/err.h"#include "lwip/netif.h"/* Exported defines ----------------------------------------------------------*//* Exported types ------------------------------------------------------------*//* Exported variables ------------------------------------------------------- *//* Exported macro ----------------------------------------------------------- *//* Exported functions ------------------------------------------------------- */err_t ethernetif_init(struct netif *netif);void ethernetif_link_check(void *argument);#if LWIP_NETIF_LINK_CALLBACKvoid ethernetif_link_config(struct netif *netif);#endif#ifdef __cplusplus}#endif#endif/************************ (C) COPYRIGHT ZCShou ***********END OF FILE**********/
.c
注意 , 原则上 , 这里的与系统相关的操作(例如创建一个接收任务) , 我们应该使用 .c 中封装的操作系统的接口 。然而 ,  .c 中的相关接口并没有处理中断问题 , 如果要启用网卡的硬件中断 , 切忌不可直接使用的相关接口 。例如 , 下面的 ck 接口 。
/********************************************************************************* @fileethernetif.c* @authorZCShou* @version 1.0.0* @date2020.10.6* @briefThis file provides initialization code for LWIP middleWare.******************************************************************************* @attention* * © COPYRIGHT 2022 ZCShou **(1) **(2) **(3) ********************************************************************************//* Includes ------------------------------------------------------------------*/#include "ethernetif.h"#include #include "lwip/opt.h"#include "lwip/timeouts.h"#include "lwip/ethip6.h"#include "lwip/tcpip.h"#include "netif/ethernet.h"#include "netif/etharp.h"#include "dp83848.h"/* Private define ------------------------------------------------------------*//* Network interface name */#define IFNAME0's'#define IFNAME1't'#define NETIF_INPUT_STACK_SIZE(256)#define NETIF_INPUT_PRIO(1)// #define NETIF_INPUT_MBOX_SIZE(20)#define NETIF_INPUT_TIMEOUT(portMAX_DELAY)#define NETIF_OUTPUT_TIMEOUT(portMAX_DELAY)/* Private functions ---------------------------------------------------------*/int32_t ETH_PHY_IO_Init(void);int32_t ETH_PHY_IO_DeInit (void);int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal);int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal);int32_t ETH_PHY_IO_GetTick(void);/* Private variables ---------------------------------------------------------*/#if defined ( __ICCARM__ ) /*!