📄 net_if.c
字号:
/*
*********************************************************************************************************
* uC/TCP-IP
* The Embedded TCP/IP Suite
*
* (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may not be used to write a similar
* product. This file may only be used in accordance with a license
* and should not be redistributed in any way.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* NETWORK INTERFACE LAYER
*
* ETHERNET
*
* Filename : net_if.c
* Version : V1.86
* Programmer(s) : ITJ
*********************************************************************************************************
* Note(s) : (1) Supports Ethernet as described in RFC #894; supports IEEE 802 as described in RFC #1042.
*
* (2) Ethernet implementation conforms to RFC #1122, Section 2.3.3, bullets (a) & (b), but
* does NOT implement bullet (c) :
*
* RFC #1122 LINK LAYER October 1989
*
* 2.3.3 ETHERNET (RFC-894) and IEEE 802 (RFC-1042) ENCAPSULATION
*
* Every Internet host connected to a 10Mbps Ethernet cable :
*
* (a) MUST be able to send and receive packets using RFC-894 encapsulation;
*
* (b) SHOULD be able to receive RFC-1042 packets, intermixed with RFC-894 packets; and
*
* (c) MAY be able to send packets using RFC-1042 encapsulation.
*
* (3) REQUIREs the following network protocol files in network directories :
*
* where
* <Network Protocol Suite> directory path for network protocol suite
*
* (a) Packet-based Network Interface Layer located in the following network directory :
*
* \<Network Protocol Suite>\IF\
*
* (b) Address Resolution Protocol Layer located in the following network directory :
*
* \<Network Protocol Suite>\
*
* See also 'net_arp.h Note #1'.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define NET_IF_MODULE
#include <net.h>
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/* --------------- ETHER BROADCAST ADDR --------------- */
static const CPU_INT08U NetIF_AddrBroadcast[NET_IF_ADDR_SIZE] = {
NET_IF_ADDR_BROADCAST_00,
NET_IF_ADDR_BROADCAST_01,
NET_IF_ADDR_BROADCAST_02,
NET_IF_ADDR_BROADCAST_03,
NET_IF_ADDR_BROADCAST_04,
NET_IF_ADDR_BROADCAST_05
};
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/* ------------------- RX FCNTS ------------------- */
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
static void NetIF_RxPktValidateBuf (NET_BUF_HDR *pbuf_hdr,
NET_ERR *perr);
#endif
static void NetIF_RxPktFrameDemux (NET_BUF *pbuf,
NET_BUF_HDR *pbuf_hdr,
NET_IF_HDR *pif_hdr,
NET_ERR *perr);
static void NetIF_RxPktFrameDemuxEther (NET_BUF_HDR *pbuf_hdr,
NET_IF_HDR *pif_hdr,
NET_ERR *perr);
static void NetIF_RxPktFrameDemuxIEEE802(NET_BUF_HDR *pbuf_hdr,
NET_IF_HDR *pif_hdr,
NET_ERR *perr);
static void NetIF_RxPktDiscard (NET_BUF *pbuf,
NET_ERR *perr);
/* ------------------- TX FNCTS ------------------- */
static void NetIF_TxPktHandler (NET_BUF *pbuf_q);
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
static void NetIF_TxPktValidate (NET_BUF *pbuf,
NET_BUF_HDR *pbuf_hdr,
NET_ERR *perr);
#endif
static void NetIF_TxPktPrepareFrame (NET_BUF *pbuf,
NET_BUF_HDR *pbuf_hdr,
NET_ERR *perr);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* NetIF_Init()
*
* Description : (1) Initialize Network Interface Layer :
*
* (a) Initialize network interface statistics & error counters
* (b) Initialize Packet-based Network Interface Layer
* (see this 'net_if_.c Note #3a')
* (c) Initialize Address Resolution Protocol Layer
* (see this 'net_if_.c Note #3b')
*
*
* Argument(s) : perr Pointer to variable that will receive the return error code from this function :
*
* NET_IF_ERR_NONE Network interface layer successfully
* initialized.
*
* --- RETURNED BY NetIF_Pkt_Init() : ---
* NET_OS_ERR_INIT_IF_RX_Q Network interface receive queue signal
* NOT successfully initialized.
* NET_OS_ERR_INIT_IF_RX_Q_NAME Network interface receive queue name
* NOT successfully configured.
* NET_OS_ERR_INIT_IF_RX_TASK Network interface receive task
* NOT successfully initialized.
* NET_OS_ERR_INIT_IF_RX_TASK_NAME Network interface receive task name
* NOT successfully configured.
*
* Return(s) : none.
*
* Caller(s) : Net_Init().
*
* Note(s) : (2) MAC address MUST be initialized & validated in NIC driver initialization.
*********************************************************************************************************
*/
void NetIF_Init (NET_ERR *perr)
{
/* ----------- INIT NET IF STAT & ERR CTRS ------------ */
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
NetIF_StatRxPktCtr = 0;
NetIF_StatRxPktProcessedCtr = 0;
NetIF_StatRxPktBroadcastCtr = 0;
NetIF_StatTxPktCtr = 0;
NetIF_StatTxPktBroadcastCtr = 0;
#endif
#if (NET_CTR_CFG_ERR_EN == DEF_ENABLED)
NetIF_ErrNullPtrCtr = 0;
NetIF_ErrInvalidProtocolCtr = 0;
NetIF_ErrRxInvalidFrameCtr = 0;
NetIF_ErrRxInvalidAddrDestCtr = 0;
NetIF_ErrRxInvalidAddrSrcCtr = 0;
NetIF_ErrRxPktDiscardedCtr = 0;
NetIF_ErrTxInvalidBufLenCtr = 0;
NetIF_ErrTxPktDiscardedCtr = 0;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
NetIF_ErrRxInvalidBufIxCtr = 0;
NetIF_ErrTxProtocolCtr = 0;
NetIF_ErrTxInvalidBufIxCtr = 0;
NetIF_ErrTxHdrDataLenCtr = 0;
#endif
#endif
/* ---------------- INIT PKT IF MODULE ---------------- */
NetIF_Pkt_Init(perr);
if (*perr != NET_IF_ERR_NONE) {
return;
}
/* ----------------- INIT ARP MODULE ------------------ */
NetARP_Init();
*perr = NET_IF_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetIF_MAC_AddrGet()
*
* Description : Get NIC's hardware (MAC) address.
*
* Argument(s) : paddr Pointer to memory buffer to receive NIC's hardware address (see Note #2).
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_IF_ERR_NONE NIC's hardware address successfully returned.
* NET_IF_ERR_INVALID_MAC Invalid or uninitialized hardware address.
*
* Return(s) : none.
*
* Caller(s) : NetIF_InitDrv().
*
* Note(s) : (1) NIC's hardware address also known as MAC (Media Access Control) or physical address.
*
* (2) (a) The size of the memory buffer that will receive the return NIC hardware address
* MUST be greater than or equal to NET_IF_ADDR_SIZE.
*
* (b) NIC's hardware address accessed by octets in memory buffer array.
*
* (c) NIC's hardware address memory array cleared in case of any error(s).
*********************************************************************************************************
*/
void NetIF_MAC_AddrGet (CPU_INT08U *paddr,
NET_ERR *perr)
{
if (NetIF_MAC_AddrValid != DEF_YES) {
Mem_Clr((void *)paddr,
(CPU_SIZE_T)NET_IF_ADDR_SIZE);
*perr = NET_IF_ERR_INVALID_MAC;
return;
}
Mem_Copy((void *) paddr,
(void *)&NetIF_MAC_Addr[0],
(CPU_SIZE_T) NET_IF_ADDR_SIZE);
*perr = NET_IF_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetIF_MAC_AddrSet()
*
* Description : Set NIC's hardware (MAC) address.
*
* Argument(s) : paddr Pointer to a memory buffer that contains the NIC's hardware address (see Note #2).
*
* perr Pointer to variable that will receive the return error code from this function :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -