BST_IP_ERR_T BST_IP_RawTcpConnect( BST_FD_T fd, BST_ARG_T Arg, BST_IP_SOCKET_ADD_T *pAddr )
{
    struct ip_addr                      stLwipAddr;
    struct tcp_pcb                     *pstPcb;


    if( !BST_IP_IsRawFdValid( fd ) )
    {
        return BST_IP_ERR_MEM;
    }

    if( BST_NULL_PTR == pAddr )
    {
        return BST_IP_ERR_MEM;
    }

    BST_IP_GetU32Addr( stLwipAddr.addr,
                       pAddr->aucRemoteIp[3],
                       pAddr->aucRemoteIp[2],
                       pAddr->aucRemoteIp[1],
                       pAddr->aucRemoteIp[0] );
    pstPcb                              = ( struct tcp_pcb * )fd.pFd;

    BST_DBG_LOG2("ip = %d,remote port = %d", stLwipAddr.addr, pAddr->usRemotePort);
    return tcp_connect( pstPcb, &stLwipAddr, pAddr->usRemotePort, LWIP_NULL_PTR );
}
BST_VOID    BST_CTaskSchdler::TrigScheduleTask( BST_CORE_CPTask *pC_PTask)
{
    BST_SRV_CTaskMng           *pcTaskManager;
    BST_TASK_STATE_ENUM_UINT8   enTaskState;
    BST_UINT32                  ulRemainTickNum;

    pcTaskManager               = BST_SRV_CTaskMng::GetInstance ();
    BST_ASSERT_NULL( pcTaskManager );

    /*
     * 入口检查
     */
    if ( BST_NULL_PTR == pC_PTask )
    {
        BST_RLS_LOG( "BST_CTaskSchdler::TrigScheduleTask pC_PTask=NULL" );
        return;
    }
    /*
     * 如果上次执行的事件尚未完成,本次不运行
     */
    if ( BST_TRUE == pC_PTask->IsBusy () )
    {
        BST_RLS_LOG( "BST_CTaskSchdler::TrigScheduleTask pC_PTask Is Busy" );
        return;
    }
    /*
     * 如果系统TICK可以被任务周期整除,说明到了整数倍对齐时间
     */

    enTaskState                 = pC_PTask->GetState ();

    BST_DBG_LOG2( "BST_CTaskSchdler::TrigScheduleTask enTaskState:%d,m_ulTaskBlockState:%d",
                       enTaskState,pC_PTask->m_ulTaskBlockState );

    switch ( enTaskState )
    {
        case BST_TASK_STATE_START:
            if( BST_TASK_BLOCK_STATE_EXPIRED ==  pC_PTask->m_ulTaskBlockState )
            {
                Execute( pC_PTask );
                ulRemainTickNum = pC_PTask->m_ulCycle - (m_ulSystemTick % pC_PTask->m_ulCycle);
                if( ulRemainTickNum > BST_TASK_SYS_TICKS )
                {
                    pC_PTask->m_ulTaskBlockState    = BST_TASK_BLOCK_STATE_NORMAL;
                }
                else
                {
                    pC_PTask->m_ulTaskBlockState    = BST_TASK_BLOCK_STATE_IGNORE;
                }
                m_bTaskMissExec                     = BST_FALSE;
            }
            return;
        default:
            break;
    }
}
BST_ERR_ENUM_UINT8 BST_APP_CEmailIMAP::HandleQuitResp( BST_VOID )
{
    if ( 0 == BST_OS_MEMCMP( m_pcServerResponse1, "BYE", BST_LIB_StrLen("BYE") ) )
    {
        BST_DBG_LOG2( "CEmailIMAP ProcId=%d,TaskId=%d run successfully.",
                      m_pcHostTask->m_usProcId, m_pcHostTask->m_usTaskId );
        m_lServerState      = BST_EMAIL_QUITED;
        return BST_NO_ERROR_MSG;
    }
    else
    {
        return BST_ERR_UNREACHABLE;
    }
}
BST_UINT32 BST_Pal_DrvAcomRecv( BST_UINT32 ulLength, BST_UINT8 *pucData )
{
    BST_ACOM_MSG_STRU              *pstAcoreMsg;

    if ( BST_NULL_PTR == pucData )
    {
        return 0;
    }

    pstAcoreMsg                     = ( BST_ACOM_MSG_STRU * )pucData;
    if ( 0 == pstAcoreMsg->ulLen )
    {
        return 0;
    }
    BST_DBG_LOG2( "BST_Pal_DrvAcomRecv, Length=%u, data[0]=u%",
                  pstAcoreMsg->ulLen, pstAcoreMsg->aucValue[0] );
    BST_PalAcomRcvCb( pstAcoreMsg->ulLen, pstAcoreMsg->aucValue );
    return ulLength;
}
BST_VOID BST_CTaskSchdler::UtranBlockInfoChgProc( BST_UINT32 *pulBlockInfo )
{
    BST_INT32                   lTaskCnt;
    BST_UINT32                  ulNextTickRemainTime;
    BST_UINT32                  ulBlockInfo;
    BST_CORE_PTASK_NODE_STRU   *pstPtaskNode;
    BST_CORE_PTASK_NODE_STRU   *pstPtaskNodeNext;
    BST_BOOL                    bTimerStop;

    if( BST_NULL_PTR == pulBlockInfo)
    {
        return;
    }
    ulBlockInfo     = (BST_UINT32 )*pulBlockInfo;
    BST_DBG_LOG2( "BST_CTaskSchdler::UtranBlockInfoChgProc blockold=u%, blockNew=u%",
                  m_ulBlockInfo,ulBlockInfo);

    if ( m_ulBlockInfo == ulBlockInfo )
    {
        return;
    }
    m_ulBlockInfo           = ulBlockInfo;

    if ( BST_AS_UNBLOCKED == m_ulBlockInfo )
    {

        BST_DBG_LOG1( "BST_CTaskSchdler::UtranBlockInfoChgProc :%d", m_bTaskMissExec );
        if(BST_FALSE == m_bTaskMissExec)
        {
            return;
        }
        lTaskCnt            = lstCount( &g_stPTaskList );

        BST_DBG_LOG1( "BST_CTaskSchdler::UtranBlockInfoChgProc TaskNum=u%", lTaskCnt );

        if ( 0 == lTaskCnt )
        {
            return;
        }
        ulNextTickRemainTime = 0;
        bTimerStop      = BST_OS_TimerIsStop( m_ulTimerId );
        if ( BST_FALSE == bTimerStop )
        {
            ulNextTickRemainTime      = BST_OS_TimeGetRemain( m_ulTimerId );
            BST_OS_TimerStop ( m_ulTimerId );
        }

        BST_DBG_LOG2( "BST_CTaskSchdler::UtranBlockInfoChgProc stop:%d,timerremain=u%", bTimerStop,ulNextTickRemainTime );
        /*
         * 遍历任务列表,获取任务并进行相应调度
         */
        for ( pstPtaskNode = ( BST_CORE_PTASK_NODE_STRU *)lstFirst( &g_stPTaskList );
              pstPtaskNode!= BST_NULL_PTR;
              pstPtaskNode = pstPtaskNodeNext )
        {
            pstPtaskNodeNext    = ( BST_CORE_PTASK_NODE_STRU *)
                                  lstNext((NODE *)pstPtaskNode);
            if ( BST_NULL_PTR == pstPtaskNode->pcTask )
            {
                continue;
            }
            /*
             * 如果任务不为空,则根据状态进行调度
             */
            TrigScheduleTask ( pstPtaskNode->pcTask);
        }

        if((0 == ulNextTickRemainTime) || (ulNextTickRemainTime > BST_TASK_SYS_BASE_TIMELEN))
        {
            ulNextTickRemainTime = BST_TASK_SYS_BASE_TIMELEN;
        }

        BST_OS_TimerStart ( m_ulTimerId, ulNextTickRemainTime );

    }

}
BST_UINT32 BST_OS_StaticMemInit( BST_VOID )
{
    BST_INT32                           i;
    BST_UINT32                          lTotalSize = 0;
    BST_UINT32                          lTotalCtrlSize = 0;
    BST_VOID                           *pCtrlStart;
    BST_UINT_PTR                        ulSpaceStart;
    BST_UINT_PTR                        ulSpaceEnd;
    BST_UINT_PTR                        ulCtrlStart;
    BST_INT32                           ulIndex;
    /* calculate mem's size */
    for ( i=0; i<BST_MEM_CTRL_BLOCK_NUMBER; i++ )
    {
        if ( 0x00000003 & g_BstMemBlockInfo[i].ulSize )/* 4 byte Aligned */
        {
            BST_DBG_LOG2("BST_OS_StaticMemInit MemBlockInfo[%d].size(%d) not aligned.\r\n",
                i, g_BstMemBlockInfo[i].ulSize );

            return BST_MEM_ERROR;
        }

        lTotalSize += ( g_BstMemBlockInfo[i].ulSize + BST_MEMORY_RESERVED_BYTES
            + BST_MEM_HEAD_BLOCK_SIZE )
            * g_BstMemBlockInfo[i].ulNumber;
        lTotalCtrlSize += BST_MEM_HEAD_BLOCK_SIZE * g_BstMemBlockInfo[i].ulNumber;
    }
    
    /* calculate buf's size */
    for ( i=0; i<BST_BUF_CTRL_BLOCK_NUMBER; i++ )
    {
        if ( 0x00000003 & g_BstBufBlockInfo[i].ulSize )/* 4 byte Aligned */
        {
            BST_DBG_LOG2("BST_OS_StaticMemInit bufMemBlockInfo[%d].size(%d) not aligned.\r\n",
                    i, g_BstBufBlockInfo[i].ulSize );

            return BST_MEM_ERROR;
        }

        lTotalSize += ( g_BstBufBlockInfo[i].ulSize + BST_MEMORY_RESERVED_BYTES
            + BST_MEM_HEAD_BLOCK_SIZE )
            * g_BstBufBlockInfo[i].ulNumber;
        lTotalCtrlSize += BST_MEM_HEAD_BLOCK_SIZE * g_BstBufBlockInfo[i].ulNumber;
    }

    /* add protected space */
    lTotalSize += 2 * BST_MEMORY_PROTECT_SIZE;
    lTotalCtrlSize += BST_MEMORY_PROTECT_SIZE;

    if ( lTotalSize > sizeof(g_acBstMemBuf) )
    {
        BST_DBG_LOG2("mem init failed, lTotalSize:%d > available:%d",lTotalSize,sizeof(g_acBstMemBuf));
        return BST_MEM_ERROR;
    }

    pCtrlStart = (BST_VOID *)g_acBstMemBuf;

    ulCtrlStart = (BST_UINT_PTR)pCtrlStart;

    ulSpaceStart = ulCtrlStart + lTotalCtrlSize;
    g_ulBstSpaceStart = ulSpaceStart;

    ulSpaceEnd = ulCtrlStart + lTotalSize - BST_MEMORY_PROTECT_SIZE ;
    g_ulBstSpaceEnd = ulSpaceEnd;

    g_ulBstSpaceAndProtectionStart = g_ulBstSpaceStart - BST_MEMORY_PROTECT_SIZE;

    g_ulBstSpaceAndProtectionEnd = g_ulBstSpaceEnd + BST_MEMORY_PROTECT_SIZE;

    /* the start address of msg pool */
    g_ulBstMemPoolIntervalAddress[0] = g_ulBstSpaceStart;

    for ( i=0; i<BST_MEM_CTRL_BLOCK_NUMBER; i++ )
    {
       BST_OS_StaticMemCtrlBlkInit( &g_BstMemCtrlBlk[i],  ( BST_INT32 )g_BstMemBlockInfo[i].ulSize,
            ( BST_INT32 )g_BstMemBlockInfo[i].ulNumber, &ulCtrlStart, &ulSpaceStart);
       g_BstMemCtrlBlk[i].enType        = BST_MEM_TYPE_DYNAMIC;
    }

    /* the start address of mem pool */
    g_ulBstMemPoolIntervalAddress[1] = ulSpaceStart;

    for ( i=0; i<BST_BUF_CTRL_BLOCK_NUMBER; i++ )
    {
        BST_OS_StaticMemCtrlBlkInit( &g_BstBufCtrlBlk[i], (BST_INT32)g_BstBufBlockInfo[i].ulSize,
            (BST_INT32)g_BstBufBlockInfo[i].ulNumber, &ulCtrlStart, &ulSpaceStart);
        g_BstBufCtrlBlk[i].enType        = BST_MEM_TYPE_PBUF;
    }

    /* the start address of app mem pool */
    g_ulBstMemPoolIntervalAddress[2] = ulSpaceStart;

    /* 初始化内存状态状态注册回调函数*/
    for ( i=0; i<BST_MEM_TYPE_BUTT; i++ )
    {
        for(ulIndex=0; ulIndex < BST_MEM_STATUS_CALLBACK_MAX_NUMBER; ulIndex++)
        {
            g_acBstMemCallBackInfo[i][ulIndex].pfCallBack = BST_NULL_PTR;
            g_acBstMemCallBackInfo[i][ulIndex].usLastReportValue = BST_MEM_HIGH_LEVEL;
        }
    }
    BST_OS_InitSpinLock(&g_stBstMemSpinLock);

    return BST_MEM_OK;
}
BST_ERR_ENUM_UINT8 BST_SRV_CHNL_Acked (
    BST_SRV_CHNL_LINK_STRU     *pstLinkItem,
    const BST_UINT32            ulMinNumber,
    const BST_UINT32            ulMaxNumber )
{
    BST_SRV_CHNL_SENT_STRU             *pSentItem;
    BST_SRV_CHNL_SENT_STRU             *pSentItem2;
    BST_SRV_CHNL_HEAD_STRU             *pstPktItem;
    BST_SRV_CHNL_HEAD_STRU             *pstPktItem2;
    BST_UINT32                          ulSpinlockCnt;
    BST_UINT32                          ulOrigCounter;
    BST_DRV_STRU                       *pNetDrvHandle;
    BST_ERR_ENUM_UINT8                  enRtnVal;

    BST_ASSERT_NULL_RTN( pstLinkItem, BST_ERR_INVALID_PTR );
    BST_ASSERT_NORM_RTN( ulMaxNumber < ulMinNumber, BST_ERR_ILLEGAL_PARAM );

    BST_DBG_LOG2( "BST_SRV_CHNL_Acked ulMinNumber, ulMaxNumber",
                  ulMinNumber, ulMaxNumber );
    /*查找已发送的数据包的*/
    pSentItem                           = BST_SRV_Q_PeekHead(pstLinkItem->pSentList);
    ulOrigCounter                       = g_ulHighPriCnt;
    pSentItem2                          = BST_NULL_PTR;
    pstPktItem2                         = BST_NULL_PTR;
    while( pSentItem != BST_NULL_PTR )
    {
        if ( ( pSentItem->ulAckNo > ulMinNumber )
           &&( pSentItem->ulAckNo <=ulMaxNumber ) )
        {
            if( BST_DSPP_FLG_LVLTYPE_HS == pSentItem->usProcLevel )
            {
                ulSpinlockCnt           = BST_OS_SpinLock( &g_HighPriCntSpinLock );
                if ( g_ulHighPriCnt > 0 )
                {
                    g_ulHighPriCnt--;
                }
                BST_OS_SpinUnLock( &g_HighPriCntSpinLock , ulSpinlockCnt );
            }
            BST_DBG_LOG1("BST_SRV_CHNL_Acked,g_ulHighPriCnt--",g_ulHighPriCnt);
            BST_SRV_Q_RmvItem( (BST_SRV_CHNL_Q **)&pstLinkItem->pSentList,
                               (BST_SRV_CHNL_Q *)pSentItem,
                               (BST_SRV_CHNL_Q **)&pSentItem2 );
            BST_OS_FREE( pSentItem );
        }
        else
        {
            pSentItem2                  = pSentItem->pNext;
        }
        pSentItem                       = pSentItem2;
    }

    pstPktItem                          = BST_SRV_Q_PeekHead(pstLinkItem->pUnsendList);
    while ( pstPktItem != BST_NULL_PTR )
    {
        if( BST_IP_SKT_PROP_NONE != pstPktItem->ucPropType )
        {
            if( ( ( pstPktItem->stProp.ulSeqNum + pstPktItem->ulUsed  ) > ulMinNumber )
              &&( ( pstPktItem->stProp.ulSeqNum + pstPktItem->ulUsed  ) <=ulMaxNumber ) )
            {
                BST_SRV_Q_RmvItem( (BST_SRV_CHNL_Q **)&pstLinkItem->pUnsendList,
                                   (BST_SRV_CHNL_Q *)pstPktItem,
                                   (BST_SRV_CHNL_Q **)&pstPktItem2 );
                BST_SRV_FreeIpBlock( pstPktItem );
            }
            else
            {
                pstPktItem2             = pstPktItem->pNext;
            }
        }
        else
        {
            pstPktItem2                 = pstPktItem->pNext;
        }
        pstPktItem                      = pstPktItem2;
    }
    enRtnVal                            = BST_NO_ERROR_MSG;
    if( BST_NULL_PTR != pstLinkItem->pUnsendList )
    {
        BST_OS_TimerStop( pstLinkItem->ulTimerId );
        BST_DBG_LOG( "BST_SRV_CHNL_Acked, Proc Unsent List" );
        enRtnVal                        = BST_SRV_CHNL_ProcInCB( pstLinkItem );
    }

    /**
     * 刚刚有值,现在无值,那么说明全部高优先数据发送完成
     */
    if( ( 0 != ulOrigCounter ) && ( 0 == g_ulHighPriCnt ) )
    {
        pNetDrvHandle   = BST_DRV_NetGetDvcHandle();
        enRtnVal        = 0;
        pNetDrvHandle->pfIoCtrl( BST_DRV_CMD_HPR_DATA_FINISH, (BST_VOID *)&enRtnVal );
        BST_DBG_LOG( "BST_SRV_CHNL_Acked, BST_DRV_CMD_HPR_DATA_FINISH" );
    }

    return enRtnVal;
}
BST_ERR_ENUM_UINT8 BST_SRV_CHNL_Send_InCB( 
    BST_SRV_CHNL_LINK_STRU     *pstChnlLink,
    BST_SRV_CHNL_HEAD_STRU     *pstPktItem )
{
    BST_SRV_CHNL_SENT_STRU     *pSentItem;
    BST_IP_ERR_T                enIpErr;
    BST_UINT16                  usFlags;
    BST_UINT32                  ulSpinlockCnt;

    BST_ASSERT_NULL_RTN( pstChnlLink, BST_ERR_INVALID_PTR );
    BST_ASSERT_NULL_RTN( pstPktItem, BST_ERR_INVALID_PTR );
    enIpErr                     = BST_IP_ERR_OK;
    if( BST_SRV_ChnlIsTail(pstPktItem) )
    {
        usFlags                 = BST_IP_SEND_FLAG_VOID;
    }
    else
    {
        usFlags                 = BST_IP_SEND_FLAG_MORE;
    }

    if( BST_IP_SKT_PROP_NONE != pstPktItem->ucPropType )
    {
        BST_SRV_CHNL_Set_LinkProp( pstChnlLink, pstPktItem );
        pstChnlLink->ulCurrentSeq= pstPktItem->stProp.ulSeqNum;
    }
    BST_DBG_LOG4("BST_SRV_CHNL_Send_InCB Length, ProcLevel, PackAddr, Flag",
                    pstPktItem->ulUsed,
                    pstPktItem->usProcLevel,
                    pstPktItem->ucPktAddr,
                    usFlags );

    switch( pstPktItem->usProcLevel )
    {
        case BST_DSPP_FLG_LVLTYPE_NORMAL:
            enIpErr             = pstChnlLink->pcSocket->Write
                                  ( (const BST_UINT8 *)&pstPktItem->aucData[0],
                                    (BST_UINT16)pstPktItem->ulUsed, BST_IP_BSTT_DATA, usFlags );
        break;

        case BST_DSPP_FLG_LVLTYPE_HS:
            enIpErr             = pstChnlLink->pcSocket->Write
                                  ( (const BST_UINT8 *)&pstPktItem->aucData[0],
                                    (BST_UINT16)pstPktItem->ulUsed,
                                    BST_IP_HPRI_DATA,
                                    usFlags );
        break;

        case BST_DSPP_FLG_LVLTYPE_LP:
            enIpErr             = pstChnlLink->pcSocket->Write
                                  ( (const BST_UINT8 *)&pstPktItem->aucData[0],
                                    (BST_UINT16)pstPktItem->ulUsed,
                                    BST_IP_LPWR_DATA,
                                    usFlags );
        break;

        default:
            return BST_ERR_PAR_UNKNOW;
    }
    if( BST_IP_ERR_OK == enIpErr )
    {
        pSentItem               = ( BST_SRV_CHNL_SENT_STRU *)BST_OS_MALLOC(
                                  BST_OS_SIZEOF( BST_SRV_CHNL_SENT_STRU ) );
        if( BST_NULL_PTR == pSentItem )
        {
            return BST_ERR_INVALID_PTR;
        }
        pstChnlLink->ulCurrentSeq += pstPktItem->ulUsed;
        pstChnlLink->ulRetryTimes  = 0;
        pSentItem->usProcLevel  = pstPktItem->usProcLevel;
        pSentItem->pNext        = BST_NULL_PTR;
        pSentItem->ulAckNo      = pstChnlLink->ulCurrentSeq;
        BST_SRV_Q_PushToTail( (BST_SRV_CHNL_Q **)&pstChnlLink->pSentList,
                              (BST_SRV_CHNL_Q *)pSentItem );
        if ( BST_DSPP_FLG_LVLTYPE_HS == pstPktItem->usProcLevel )
        {
            ulSpinlockCnt       = BST_OS_SpinLock( &g_HighPriCntSpinLock );
            g_ulHighPriCnt++;
            BST_OS_SpinUnLock( &g_HighPriCntSpinLock , ulSpinlockCnt );
            BST_DBG_LOG2(" g_ulHighPriCnt++, sent", g_ulHighPriCnt, pstPktItem->ulUsed);
        }
    }
    else if( BST_IP_ERR_MEM == enIpErr )
    {
        pstChnlLink->ulRetryTimes++;
        BST_OS_TimerStart( pstChnlLink->ulTimerId, BST_SRV_CHNL_RTX_INTERVAL );
        return BST_ERR_NO_MEMORY;
    }
    else
    {
        return BST_ERR_SOCKET_CLSD;
    }
    return BST_NO_ERROR_MSG;
}