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

📄 if.c

📁 代码在ti的c67系列单片机上实现了完整的TCPIP协议栈
💻 C
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// IF.C
//
// Generic Interface Functions
//
// Author: Michael A. Denio
// Copyright 2000 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>
#include <netmain.h>

static uint    wcHeader;          // Worst case HdrSize
static uint    wcPad;             // Worst case PacketPad

#define IFMAX   16
static  uint    IndexHWM;
static  HANDLE  hIFMapping[IFMAX];

//--------------------------------------------------------------------
// IFCreatePacket()
//
// Create an emptry packet suitable for transmission on an generic
// device. If the size of the packet is larger than the MTU of the
// egress device, there are no memory restrictions as this function
// will be called later to create smaller packets for transmission.
//--------------------------------------------------------------------
HANDLE IFCreatePacket
(
    uint   SizePayload,
    uint   SizeNet,
    uint   SizeTP
)
{
    HANDLE  hPkt,hFrag;

    // We'll put everything into "Payload"
    SizePayload += SizeNet + SizeTP;

    // Verify legal Ethernet size range
    if( SizePayload < ETH_MIN_PAYLOAD )
        SizePayload = ETH_MIN_PAYLOAD;

    // Get the total length (less "invisible" pad)
    SizePayload += wcHeader;

    // Create a packet
    if( !(hPkt = PktNew()) )
        return(0);

    // Create the frag
    if( (SizePayload+wcPad) > 1536 )
        hFrag=FragNewAlloc( FRAG_POOL_MEMORY, SizePayload, wcPad, 0, 0 );
    else
        hFrag=FragNewAlloc( FRAG_POOL_PACKET, SizePayload, wcPad, 0, 0 );

    if( !hFrag )
    {
        PktFree( hPkt );
        return(0);
    }

    // Attach the frag to the packet
    PktSetFrag( hPkt, hFrag );

    // Set the packet offsets
    PktSetSizeLLC( hPkt, wcHeader );
    if( SizeNet )
        PktSetSizeNet( hPkt, SizeNet );
    if( SizeTP )
        PktSetSizeTP( hPkt, SizeTP );

    // Return the packet
    return( hPkt );
}

//--------------------------------------------------------------------
// IFSetPad()
//
// Called to set the header and trailer padding for CreatePacket
//--------------------------------------------------------------------
void IFSetPad( uint Header, uint Trailer )
{
    // Record the "worst case" condition
    if( Header > wcHeader )
        wcHeader = Header;
    if( Trailer > wcPad )
        wcPad  = Trailer;
}

//--------------------------------------------------------------------
// IFInit()
//
// Clear our Index mappings
//--------------------------------------------------------------------
void IFInit()
{
    wcHeader = 0;
    wcPad    = 0;
    IndexHWM = 0;
    mmZeroInit( hIFMapping, sizeof(hIFMapping) );
}

//--------------------------------------------------------------------
// IFMaxIndex()
//
// Returns the max Index currently in use
//--------------------------------------------------------------------
uint IFMaxIndex()
{
    return( IndexHWM );
}

//--------------------------------------------------------------------
// IFIndexNew()
//
// Allocates and returns a physical index for this device
//--------------------------------------------------------------------
uint IFIndexNew( HANDLE hIF, uint Index )
{
    IFDEV *p = (IFDEV *)hIF;
    uint  tmp;

    if( p->Type != HTYPE_ETH && p->Type != HTYPE_PPP )
        DbgPrintf(DBG_ERROR,"IFAllocateIndex: HTYPE %d",p->Type);

    if( Index > IFMAX )
        return(0);

    if( Index && hIFMapping[Index-1] && hIFMapping[Index-1] != hIF )
        return(0);

    if( !Index )
    {
        for( tmp=0; tmp<IFMAX; tmp++ )
            if( !hIFMapping[tmp] )
                break;
        if( tmp == IFMAX )
            return(0);
        Index = tmp+1;
    }

    if( Index > IndexHWM )
        IndexHWM = Index;

    hIFMapping[Index-1] = hIF;
    return( Index );
}

//--------------------------------------------------------------------
// IFIndexFree()
//
// Frees the physical indentifier for this device
//--------------------------------------------------------------------
void IFIndexFree( uint Index )
{
    uint tmp;

    if( !Index || Index > IFMAX || !hIFMapping[Index-1] )
        return;

    hIFMapping[Index-1] = 0;

    if( Index == IndexHWM )
    {
        for( tmp=IFMAX; tmp>0; tmp-- )
            if( hIFMapping[tmp-1] )
                break;
        if( (uint)tmp < IndexHWM )
            IndexHWM = (uint)tmp;
    }
}

//--------------------------------------------------------------------
// IFIndexGetHandle()
//
// Returns a handle what matches the supplied physical index
//--------------------------------------------------------------------
HANDLE IFIndexGetHandle( uint Index )
{
    if( !Index || Index > IFMAX )
        return(0);
    return( hIFMapping[Index-1] );
}

#ifdef _STRONG_CHECKING

//--------------------------------------------------------------------
// IFGetIndex()
//
// Returns a physical indentifier for this device
//--------------------------------------------------------------------
uint IFGetIndex( HANDLE hIF )
{
    IFDEV *p = (IFDEV *)hIF;

    if( p->Type == HTYPE_ETH || p->Type == HTYPE_PPP )
        return( p->llIndex );

    DbgPrintf(DBG_ERROR,"IFGetIndex: HTYPE %d",p->Type);
    return(0);
}

//--------------------------------------------------------------------
// IFGetMTU()
//
// Returns the MTU of the Device
//--------------------------------------------------------------------
uint IFGetMTU( HANDLE hIF )
{
    IFDEV *p = (IFDEV *)hIF;

    if( p->Type == HTYPE_ETH || p->Type == HTYPE_PPP )
        return( p->ProtMTU );

    DbgPrintf(DBG_ERROR,"IFGetMTU: HTYPE %d",p->Type);
    return(0);
}

#endif

⌨️ 快捷键说明

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