⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ether.c

📁 代码在ti的c67系列单片机上实现了完整的TCPIP协议栈
💻 C
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// ETHER.C
//
// Object member functions for the Ether device object.
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>

static UINT8   bEtherBCast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

//--------------------------------------------------------------------
// EtherNew()
//
// Creates a new Ether object
//--------------------------------------------------------------------
HANDLE EtherNew( uint llIndex )
{
    ETHDRV  *pe;
    uint   w;
    UINT8   bTmp[6];

    // Allocate space for the ETHER object
    if( !(pe = mmAlloc(sizeof(ETHDRV))) )
    {
        DbgPrintf(DBG_WARN,"EtherNew: OOM");
        ExecLowResource();
        return( 0 );
    }

    // Initialize type
    pe->Type           = HTYPE_ETH;
    pe->llIndex        = llIndex;

    // Allocate our index
    if( IFIndexNew( (HANDLE)pe, llIndex ) != llIndex )
    {
        DbgPrintf(DBG_WARN,"EtherNew: Can't Map Index");
abortnew:
        mmFree( pe );
        return( 0 );
    }

    // Set MTU to NULL for now
    pe->PhysMTU        = 0;

    // Open the low-level packet driver
    if( !llPacketOpen( pe->llIndex, (HANDLE) pe ) )
    {
        DbgPrintf(DBG_WARN,"EtherNew: Can't Open Driver");
        goto abortnew;
    }

    // Init Multicast
    pe->MCastMax       = llPacketGetMCastMax( pe->llIndex );
    if( pe->MCastMax > ETH_MAX_MCAST )
        pe->MCastMax   = ETH_MAX_MCAST;
    pe->MCastUsed      = 0;

    // Init Packet filter
    pe->PktFilter      = ETH_PKTFLT_ALLMULTICAST;
    llPacketSetRxFilter( pe->llIndex, pe->PktFilter );

    // Initialize NULL for our MCast addresses
    for(w=0; w<pe->MCastMax; w++)
        pe->hLLAMCast[w] = 0;

    // Initialize our direct MAC address
    pe->hLLADirect = LLANew();
    llPacketGetMacAddr( pe->llIndex, bTmp );
    LLASet( pe->hLLADirect, TYPE_LLA_UNICAST, 6, bTmp );

    // Initialize our broadcast MAC address
    pe->hLLABCast = LLANew();
    LLASet( pe->hLLABCast, TYPE_LLA_BROADCAST, 6, bEtherBCast );

    return( (HANDLE)pe );
}

//--------------------------------------------------------------------
// EtherFree()
//
// Destroys an Ether device
//--------------------------------------------------------------------
void EtherFree(HANDLE h)
{
    ETHDRV *pe = (ETHDRV *)h;

#ifdef _STRONG_CHECKING
    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherFree: HTYPE %04x",pe->Type);
        return;
    }
#endif

    // Close the low-level packet driver
    llPacketClose( pe->llIndex );

    // Free what we've allocated
    LLADeRef( pe->hLLABCast );
    LLADeRef( pe->hLLADirect );

    // Free our index
    IFIndexFree( pe->llIndex );

    // Kill type for debug
    pe->Type = 0;

    mmFree( h );
}

//--------------------------------------------------------------------
// EtherConfig()
//
// Set Ethernet Header Properties
//--------------------------------------------------------------------
void EtherConfig
(
    HANDLE h,
    uint   PhysMTU,
    uint   EthHdrSize,
    uint   OffDstMac,
    uint   OffSrcMac,
    uint   OffEtherType,
    uint   PacketPad
)
{
    ETHDRV *pe = (ETHDRV *)h;

#ifdef _STRONG_CHECKING
    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherHeaderProp: HTYPE %04x",pe->Type);
        return;
    }
#endif

    pe->EthHdrSize   = EthHdrSize;
    pe->OffDstMac    = OffDstMac;
    pe->OffSrcMac    = OffSrcMac;
    pe->OffEtherType = OffEtherType;
    pe->PacketPad    = PacketPad;
    pe->PhysMTU      = PhysMTU;
    pe->ProtMTU      = PhysMTU - (EthHdrSize + PacketPad);

    // Record the "worst case" condition
    IFSetPad( EthHdrSize, PacketPad );
}

//--------------------------------------------------------------------
// EtherSetPktFilter()
//
// SEt Ether packet filter
//--------------------------------------------------------------------
void EtherSetPktFilter( HANDLE hEther, uint  PktFilter )
{
    ETHDRV *pe = (ETHDRV *)hEther;

#ifdef _STRONG_CHECKING
    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherSetPktFilter: HTYPE %04x",pe->Type);
        return;
    }
#endif

    // Check for legal range
#if ETH_PKTFLT_NOTHING
    if( PktFilter < ETH_PKTFLT_NOTHING || PktFilter > ETH_PKTFLT_ALL )
        return;
#else
    if( PktFilter > ETH_PKTFLT_ALL )
        return;
#endif

    pe->PktFilter = PktFilter;

    llPacketSetRxFilter( pe->llIndex, pe->PktFilter );
}

//--------------------------------------------------------------------
// EtherAddMCast()
//
// Add a multicast address
//--------------------------------------------------------------------
void EtherAddMCast( HANDLE hEther, HANDLE hLLAMCast )
{
#ifdef _STRONG_CHECKING
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherAddMCast: HTYPE %04x",pe->Type);
        return;
    }
#else
    (void)hEther;
#endif

    (void)hLLAMCast;

    // NO MCAST SUPPORT
    DbgPrintf(DBG_WARN,"EtherAddMCast: Not Supported");
}

//--------------------------------------------------------------------
// EtherDelMCast()
//
// Del a multicast address
//--------------------------------------------------------------------
void EtherDelMCast( HANDLE hEther, HANDLE hLLAMCast )
{
#ifdef _STRONG_CHECKING
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherDelMCast: HTYPE %04x",pe->Type);
        return;
    }
#else
    (void)hEther;
#endif

    (void)hLLAMCast;

    // NO MCAST SUPPORT
    DbgPrintf(DBG_WARN,"EtherDelMCast: Not Supported");
}

//--------------------------------------------------------------------
// EtherClearMCast()
//
// Clear all multicast addresses
//--------------------------------------------------------------------
void EtherClearMCast( HANDLE hEther )
{
#ifdef _STRONG_CHECKING
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherClearMCast: HTYPE %04x",pe->Type);
        return;
    }
#else
    (void)hEther;
#endif

    // NO MCAST SUPPORT
    DbgPrintf(DBG_WARN,"EtherClearMCast: Not Supported");
}

//--------------------------------------------------------------------
// EtherGetMCastList()
//
// Get the current MCast list for this device
//--------------------------------------------------------------------
uint  EtherGetMCastList( HANDLE hEther, uint  wMax, HANDLE *hArray )
{
    ETHDRV *pe = (ETHDRV *)hEther;

#ifdef _STRONG_CHECKING
    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherGetMCastList: HTYPE %04x",pe->Type);
        return(0);
    }
#else
    (void)hEther;
#endif

    (void)wMax;
    (void)hArray;

    // NO MCAST SUPPORT
    // (Right now MCastUsed will always be NULL)

    return( pe->MCastUsed );
}


#ifdef _STRONG_CHECKING

//--------------------------------------------------------------------
// EtherGetLLADirect()
//
// Get the Direct LLA MAC address for this device
//--------------------------------------------------------------------
HANDLE EtherGetLLADirect( HANDLE hEther )
{
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherGetLLADirect: HTYPE %04x",pe->Type);
        return(0);
    }
    return( pe->hLLADirect );
}

//--------------------------------------------------------------------
// EtherGetLLABCast()
//
// Get the BCast LLA MAC address for this device
//--------------------------------------------------------------------
HANDLE EtherGetLLABCast( HANDLE hEther )
{
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherGetLLABCast: HTYPE %04x",pe->Type);
        return(0);
    }
    return( pe->hLLABCast );
}

//--------------------------------------------------------------------
// EtherGetMCastMax()
//
// Get the max MCast list size for this device
//--------------------------------------------------------------------
uint  EtherGetMCastMax( HANDLE hEther )
{
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherGetMCastMax: HTYPE %04x",pe->Type);
        return(0);
    }
    return( pe->MCastMax );
}

//--------------------------------------------------------------------
// EtherGetMCastUsed()
//
// Get the max MCast list size for this device
//--------------------------------------------------------------------
uint  EtherGetMCastUsed( HANDLE hEther )
{
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherGetMCastMax: HTYPE %04x",pe->Type);
        return(0);
    }
    return( pe->MCastUsed );
}

//--------------------------------------------------------------------
// EtherGetPktFilter()
//
// Get the current packet filter setting
//--------------------------------------------------------------------
uint  EtherGetPktFilter( HANDLE hEther )
{
    ETHDRV *pe = (ETHDRV *)hEther;

    if( pe->Type != HTYPE_ETH )
    {
        DbgPrintf(DBG_ERROR,"EtherGetPktFilter: HTYPE %04x",pe->Type);
        return(0);
    }
    return( pe->PktFilter );
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -