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

📄 frag.c

📁 代码在ti的c67系列单片机上实现了完整的TCPIP协议栈
💻 C
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// FRAG.C
//
// Memory Fragment Object
//
// (Pure Data Object - No functional properties)
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>

//--------------------------------------------------------------------
// FragNewAlloc()
//
// Creates a fragment which allocates a new buffer, and attaches
// it to a packet.
//
//--------------------------------------------------------------------
HANDLE FragNewAlloc
(
    uint PoolID,
    uint BufferLen,
    uint PadLen,
    uint ValidLen,
    uint DataOffset
)
{
    HANDLE hFrag;
    UINT8  *pb;

    // Right now, we only support two allocation pools
    if( PoolID == FRAG_POOL_PACKET )
    {
        // If buffer alloc fails, return NULL.
        if( !(pb = llPacketGetBuffer( BufferLen+PadLen )) )
        {
            ExecLowResource();
            return(0);
        }
    }
    else if( PoolID == FRAG_POOL_MEMORY )
    {
        // If buffer alloc fails, return NULL.
        if( !(pb = mmAlloc( BufferLen+PadLen )) )
        {
            ExecLowResource();
            return(0);
        }
    }
    else
        return(0);

    // Wrap and attach
    if( !(hFrag = FragNewWrap( PoolID, BufferLen,
                               ValidLen, DataOffset, pb )) )
    {
        // Frag not created - free buffer memory
        if( PoolID == FRAG_POOL_PACKET )
            llPacketReturnBuffer( pb );
        else
            mmFree( pb );
    }

    return( hFrag );
}

//--------------------------------------------------------------------
// FragNewWrap()
//
// Creates a fragment which wraps a known buffer, and attaches
// it to a packet.
//
//--------------------------------------------------------------------
HANDLE FragNewWrap
(
    uint  PoolID,
    uint  BufferLen,
    uint  ValidLen,
    uint  DataOffset,
    UINT8*  pbData
)
{
    FRAG   *pf;

    // If Frag alloc fails, return NULL.
    if( !(pf = mmAlloc(sizeof(FRAG))) )
    {
        DbgPrintf(DBG_WARN,"FragNewWrap: OOM");
        ExecLowResource();
        return( 0 );
    }

    // Initialize type
    pf->Type = HTYPE_FRAG;

    // Initialize the object
    pf->PoolID      = PoolID;
    pf->pNext       = 0;
    pf->pPrev       = 0;
    pf->dwAux1      = 0;
    pf->dwAux2      = 0;
    pf->BufferLen   = BufferLen;
    pf->ValidLen    = ValidLen;
    pf->DataOffset  = DataOffset;
    pf->pbData      = pbData;

    return( (HANDLE)pf );
}

//--------------------------------------------------------------------
// FragFree()
//
// Frees a chain of Frags
//--------------------------------------------------------------------
void FragFree( HANDLE h )
{
    FRAG   *pf  = (FRAG *)h;
    FRAG   *pfNext;

    // Free entire chain
    while( pf )
    {
#ifdef _STRONG_CHECKING
        if( pf->Type != HTYPE_FRAG )
        {
            DbgPrintf(DBG_ERROR,"FragFree: HTYPE %04x",pf->Type);
            return;
        }
#endif

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

        // Free the frag buffer
        if( pf->PoolID == FRAG_POOL_PACKET )
            llPacketReturnBuffer( pf->pbData );
        else if( pf->PoolID == FRAG_POOL_MEMORY )
            mmFree( pf->pbData );

        // Get next entry in chain
        pfNext = pf->pNext;

        // Free the Frag
        mmFree( pf );

        // Setup for next free
        pf = pfNext;
    }
}

//--------------------------------------------------------------------
// FragGetBufParams()
//
//--------------------------------------------------------------------
UINT8 *FragGetBufParams
(
    HANDLE h,
    uint   *pwBufferLen,
    uint   *pValidLen,
    uint   *pwDataOffset
)
{
    FRAG *pf = (FRAG *)h;

#ifdef _STRONG_CHECKING
    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragGetBufParams: HTYPE %04x",pf->Type);
        return( 0 );
    }
#endif

    if( pwBufferLen )
        *pwBufferLen  = pf->BufferLen;
    if( pValidLen )
        *pValidLen   = pf->ValidLen;
    if( pwDataOffset )
        *pwDataOffset = pf->DataOffset;

    return( pf->pbData );
}

//--------------------------------------------------------------------
// FragCopy()
//
//--------------------------------------------------------------------
HANDLE FragCopy( HANDLE h )
{
    FRAG   *pf = (FRAG *)h;
    HANDLE hFrag;
    UINT8  *pb;

#ifdef _STRONG_CHECKING
    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragCopy: HTYPE %04x",pf->Type);
        return( 0 );
    }
#endif

    // If buffer alloc fails, return NULL.
    if( !(pb = mmAlloc( pf->BufferLen )) )
        return(0);

    // Wrap and attach
    if( !(hFrag = FragNewWrap( FRAG_POOL_MEMORY, pf->BufferLen,
                               pf->ValidLen, pf->DataOffset, pb )) )
    {
        // Frag not created - free buffer memory
        mmFree( pb );
        return( 0 );
    }

    // Copy the data
    mmCopy( pb+pf->DataOffset, pf->pbData+pf->DataOffset, pf->ValidLen );

    // Copy the Aux values
    ((FRAG *)hFrag)->dwAux1 = pf->dwAux1;
    ((FRAG *)hFrag)->dwAux2 = pf->dwAux2;

    return( hFrag );
}

#ifdef _STRONG_CHECKING

//--------------------------------------------------------------------
// FragSetPrev()
//
// Set the previous frag pointer
//--------------------------------------------------------------------
void FragSetPrev( HANDLE h, HANDLE hPrev )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragSetPrev: HTYPE %04x",pf->Type);
        return;
    }

    pf->pPrev = (FRAG *)hPrev;
}

//--------------------------------------------------------------------
// FragSetNext()
//
// Set the next frag pointer
//--------------------------------------------------------------------
void FragSetNext( HANDLE h, HANDLE hNext )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragSetNext: HTYPE %04x",pf->Type);
        return;
    }

    pf->pNext = (FRAG *)hNext;
}

//--------------------------------------------------------------------
// FragGetPrev()
//
// Get the previous frag pointer
//--------------------------------------------------------------------
HANDLE FragGetPrev( HANDLE h )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragGetPrev: HTYPE %04x",pf->Type);
        return( 0 );
    }

    return( (HANDLE)pf->pPrev );
}

//--------------------------------------------------------------------
// FragGetNext()
//
// Get the next frag pointer
//--------------------------------------------------------------------
HANDLE FragGetNext( HANDLE h )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragGetNext: HTYPE %04x",pf->Type);
        return( 0 );
    }

    return( (HANDLE)pf->pNext );
}

//--------------------------------------------------------------------
// FragSetBufParams()
//
//--------------------------------------------------------------------
void FragSetBufParams( HANDLE h, uint ValidLen, uint DataOffset )
{
    FRAG *pf = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragSetBufParams: HTYPE %04x",pf->Type);
        return;
    }

    pf->ValidLen   = ValidLen;
    pf->DataOffset = DataOffset;
}

//--------------------------------------------------------------------
// FragSetAux1()
//
//--------------------------------------------------------------------
void FragSetAux1( HANDLE h, UINT32 dwAux1 )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragSetAux1: HTYPE %04x",pf->Type);
        return;
    }

    pf->dwAux1 = dwAux1;
}

//--------------------------------------------------------------------
// FragGetAux1()
//
//--------------------------------------------------------------------
UINT32 FragGetAux1( HANDLE h )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragGetAux1: HTYPE %04x",pf->Type);
        return(0);
    }

    return( pf->dwAux1 );
}

//--------------------------------------------------------------------
// FragSetAux2()
//
//--------------------------------------------------------------------
void FragSetAux2( HANDLE h, UINT32 dwAux2 )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragSetAux2: HTYPE %04x",pf->Type);
        return;
    }

    pf->dwAux2 = dwAux2;
}

//--------------------------------------------------------------------
// FragGetAux2()
//
//--------------------------------------------------------------------
UINT32 FragGetAux2( HANDLE h )
{
    FRAG   *pf  = (FRAG *)h;

    if( pf->Type != HTYPE_FRAG )
    {
        DbgPrintf(DBG_ERROR,"FragGetAux2: HTYPE %04x",pf->Type);
        return(0);
    }

    return( pf->dwAux2 );
}

#endif




⌨️ 快捷键说明

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