/*lint -e961*/
VOS_VOID  NAS_LMM_RemoveMsgFromFsmQue(
    NAS_LMM_FSM_MSG_BUF_STRU*            pstFsmMsgBuffAddr,
    VOS_UINT32                          ulMsgType,
    VOS_UINT32                          ulPriLevel)

{
    VOS_UINT8                           ucCnt;
    NAS_LMM_BUFF_MSG_STRU*				pstMsgQue         = NAS_LMM_NULL_PTR;
    VOS_BOOL                            bRsltRemoveMsgFromQue = VOS_FALSE;


    /* 参数检查  */

    if(NAS_LMM_NULL_PTR                  == pstFsmMsgBuffAddr)
    {
        NAS_LMM_PUBM_LOG_WARN(           "NAS_LMM_RemoveMsgFromFsmQue, Get pstFsmMsgBuffAddr Fail!");
        return;

    }
    if( (NAS_LMM_STORE_HIGH_PRIO_MSG != ulPriLevel) &&
        (NAS_LMM_STORE_LOW_PRIO_MSG  != ulPriLevel))
    {
        NAS_LMM_PUBM_LOG1_WARN(           "NAS_LMM_RemoveMsgFromFsmQue, PRIO Fail!",ulPriLevel);
        return;

    }
    /* 在高优先级和低优先级中查找ulMsgType,如果已有,需要释放  */

    pstMsgQue                           = pstFsmMsgBuffAddr->astHighPrioBufQue;
    ucCnt                               = pstFsmMsgBuffAddr->ucHighPrioCnt;
    bRsltRemoveMsgFromQue               = NAS_LMM_RemoveMsgFromQue( pstMsgQue,
                                                                ucCnt,
                                                                ulMsgType);
    /*消息成功释放,更新MSG COUNT */
    if( bRsltRemoveMsgFromQue )
    {
        pstFsmMsgBuffAddr->ucHighPrioCnt-=1;
    }

    /*低优先级 */
    pstMsgQue                           = pstFsmMsgBuffAddr->astLowPrioBufQue;
    ucCnt                               = pstFsmMsgBuffAddr->ucLowPrioCnt;

    bRsltRemoveMsgFromQue               = NAS_LMM_RemoveMsgFromQue( pstMsgQue,
                                                                ucCnt,
                                                                ulMsgType);

    /*消息成功释放,更新MSG COUNT */
    if( bRsltRemoveMsgFromQue )
    {
        pstFsmMsgBuffAddr->ucLowPrioCnt-=1;
    }

    return;
}
VOS_UINT32  NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf( MsgBlock  * pMsg)
{
    PS_USIM_GET_FILE_CNF_STRU          *pstUsimCnf = NAS_LMM_NULL_PTR;
    VOS_UINT32                          ulRst;

    NAS_LMM_PUBM_LOG2_INFO("NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf: g_ulSendMsgCounter = ,NAS_EMM_GET_USIM_FILE_TYPE=",
                                        NAS_EMM_GET_SEND_COUNTER(),
                                        NAS_EMM_GET_USIM_FILE_TYPE());

    /* 状态匹配检查,若不匹配,退出 */
    if (NAS_EMM_PLMN_CHK_STAT_INVALID(EMM_MS_NULL, EMM_SS_NULL_WAIT_READING_USIM))
    {
        NAS_LMM_EMM_PLMN_LOG_WARN("NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf: STATE ERR!!!");
        return NAS_LMM_MSG_DISCARD;
    }

    /*收到后计数*//*对全局变量进行保护,以防出现负数*/
    if (NAS_NVIM_SEND_COUNTER_NONE == NAS_EMM_GET_SEND_COUNTER())
    {
        NAS_LMM_EMM_PLMN_LOG_WARN("NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf: Receive USIM cnf when Counter is 0!!!");
        return NAS_LMM_MSG_DISCARD;
    }
    else
    {
        NAS_EMM_GET_SEND_COUNTER() --;
    }

    pstUsimCnf = (PS_USIM_GET_FILE_CNF_STRU*)pMsg;


    NAS_LMM_PUBM_LOG1_WARN("NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf:===>",pstUsimCnf->ulResult);

    /*读卡失败*/
    if (VOS_OK != pstUsimCnf->ulResult)
    {
        NAS_LMM_PUBM_LOG_WARN("NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf: Read file err !");


        if( NAS_NVIM_SEND_COUNTER_NONE == NAS_EMM_GET_SEND_COUNTER() )
        {/*可选最后一个文件读取失败,进行读NV、转状态的动作*/

            NAS_LMM_DoAfterReadOptFileOver();
        }

        return NAS_LMM_MSG_HANDLED;
    }

    /* 对文件进行处理 */
    ulRst = NAS_LMM_ProcessHardUsimCnf(pstUsimCnf);
    if(NAS_LMM_NVIM_OK != ulRst)
    {
        NAS_LMM_EMM_PLMN_LOG1_ERR("NAS_LMM_MsNullSsNullReadingUsimMsgUsimCnf : Decode USIM MSG err: ",
                                ulRst);
    }

    if( NAS_NVIM_SEND_COUNTER_NONE == NAS_EMM_GET_SEND_COUNTER())
    {   /* 读完必读或可选文件 */

        NAS_LMM_DoAfterReadOptFileOver();
    }

    /* 没有读完,则继续等待读卡响应消息 */

    return NAS_LMM_MSG_HANDLED;
}
VOS_VOID  NAS_LMM_PrintFsmTimer(NAS_LMM_PARALLEL_FSM_ENUM_UINT16         enParallelFsmId)
{
    NAS_LMM_FSM_STATE_STRU              *pstCurFsm;          /* 状态机地址 */
    NAS_LMM_TIMER_CTRL_STRU             *pstTimerSta;        /* 状态定时器 */
    NAS_LMM_TIMER_CTRL_STRU             *pstTimerPtl;        /* 协议定时器 */
    VOS_UINT16                          usPtlTid;


    /* 入口检查 */
    if ( NAS_LMM_PARA_FSM_ID_INVALID(enParallelFsmId))
    {
        NAS_LMM_PUBM_LOG1_ERR("NAS_LMM_FSM_PushTimer, The input is illegal! enParalFsmId",
                             enParallelFsmId);

        return;
    }

    pstCurFsm      = NAS_LMM_GetCurFsmAddr(NAS_LMM_PARALLEL_FSM_EMM);
    pstTimerSta    = NAS_LMM_GetMmTimerStaCtrlAddrByIdx(pstCurFsm->enStaTId);

    /*判断当前状态定时器状态为停止和挂起*/
    if(VOS_NULL_PTR == pstTimerSta->psthTimer)
    {
        /*状态定时器处于挂起状态*/
        if((NAS_LMM_TIMER_SS_SUSPENDING == pstTimerSta->ucTimerSs)
           && (NAS_LMM_TIMER_RS_RUNNING == pstTimerSta->ucTimerRs))
        {
            NAS_LMM_PUBM_LOG1_WARN("NAS_LMM_PrintFsmTimer: State Timer is suspended! enStaTId:",
                                  pstCurFsm->enStaTId);
        }
        /*状态定时器处于停止状态*/
        else
        {
            NAS_LMM_PUBM_LOG1_WARN("NAS_LMM_PrintFsmTimer: State Timer is stoped! enStaTId:",
                                  pstCurFsm->enStaTId);
        }

    }
    /*状态定时器处于运行状态*/

    /*协议定时器*/
    for(usPtlTid = TI_NAS_LMM_PTL_TI_PUB_BOUNDARY_START+1; usPtlTid < NAS_LMM_PTL_TI_BUTT; usPtlTid++)
    {
        pstTimerPtl    = NAS_LMM_GetMmTimerPtlCtrlAddrByIdx(usPtlTid);

        /*协议定时器处于运行状态*/
        if(VOS_NULL_PTR != pstTimerPtl->psthTimer)
        {
            NAS_LMM_PUBM_LOG1_NORM("NAS_LMM_PrintFsmTimer: Ptl Timer is running! PtlTid:",
                                  usPtlTid);
        }
        /*协议定时器处于停止状态*/
        else
        {
            NAS_LMM_PUBM_LOG1_NORM("NAS_LMM_PrintFsmTimer: Ptl Timer is stoped! PtlTid:",
                                  usPtlTid);
        }
    }

    return;
}
VOS_UINT32 NAS_LMM_OutMsgFromQue( NAS_LMM_PARALLEL_FSM_ENUM_UINT16 enParalFsmId,
                                        VOS_UINT32         ulBufPriLevel,
                                        NAS_LMM_BUFF_MSG_STRU   *pstGetMsg)
{
    VOS_UINT32                          ulLoop;
    VOS_UINT8                           *pucMsgCnt          = NAS_LMM_NULL_PTR;
    NAS_LMM_BUFF_MSG_STRU				*pstBuffer          = NAS_LMM_NULL_PTR;
    NAS_LMM_FSM_MSG_BUF_STRU             *pstFsmMsgBuffAddr  = NAS_LMM_NULL_PTR;
    VOS_UINT32                          ulRslt              = NAS_LMM_NOT_TRAVEL_BUFFER_MSG;


    /* 参数检查*/
    if (enParalFsmId != NAS_LMM_PARALLEL_FSM_EMM )
    {
        NAS_LMM_PUBM_LOG1_WARN("NAS_LMM_OutMsgFromQue: FsmId error, FsmId:", enParalFsmId);
        return ulRslt;
    }

    if (NAS_LMM_NULL_PTR == pstGetMsg)
    {
        NAS_LMM_PUBM_LOG_WARN("NAS_LMM_OutMsgFromQue: pstGetMsg is null");
        return ulRslt;
    }

    /* 获取缓存FSM Buffer的地址 */

    pstFsmMsgBuffAddr                   = NAS_LMM_GetFsmBufAddr( enParalFsmId );
    if(NAS_LMM_NULL_PTR                  == pstFsmMsgBuffAddr)
    {
        NAS_LMM_PUBM_LOG_WARN(           "NAS_LMM_OutMsgFromQue, Get pstFsmMsgBuffAddr Fail!");
        return  ulRslt;

    }

    /* 依据优先级取相关参数*/

    if (NAS_LMM_STORE_HIGH_PRIO_MSG == ulBufPriLevel)
    {
        NAS_LMM_PUBM_LOG_NORM("NAS_LMM_OutMsgFromQue: high prio buff");
        pstBuffer = pstFsmMsgBuffAddr->astHighPrioBufQue;
        pucMsgCnt = &(pstFsmMsgBuffAddr->ucHighPrioCnt);

    }
    else if (NAS_LMM_STORE_LOW_PRIO_MSG == ulBufPriLevel)
    {

        NAS_LMM_PUBM_LOG_NORM("NAS_LMM_OutMsgFromQue: low prio buff");
        pstBuffer = pstFsmMsgBuffAddr->astLowPrioBufQue;
        pucMsgCnt = &(pstFsmMsgBuffAddr->ucLowPrioCnt);
    }
    else
    {
        NAS_LMM_PUBM_LOG_WARN("NAS_LMM_OutMsgFromQue is entered");
        return ulRslt;
    }


    /* 队列中有消息存在 */

    if ((*pucMsgCnt > 0) && (*pucMsgCnt <= NAS_EMM_BUF_MSG_MAX_NUM))
    {
        NAS_LMM_PUBM_LOG1_NORM("NAS_LMM_OutMsgFromQue: ucMsgCnt valid, *pucMsgCnt = ",*pucMsgCnt);

        /* 将第一个发送出来 */
        pstGetMsg->ulEvtType = pstBuffer[0].ulEvtType;
        pstGetMsg->pBuffMsg = pstBuffer[0].pBuffMsg;

        /* 后面的依次前移 */
        for (ulLoop = 1; ulLoop < *pucMsgCnt; ulLoop++)
        {
            pstBuffer[ulLoop - 1].pBuffMsg  = pstBuffer[ulLoop].pBuffMsg;
            pstBuffer[ulLoop - 1].ulEvtType = pstBuffer[ulLoop].ulEvtType;
        }

        /* 将消息缓存指针置空 */
        pstBuffer[*pucMsgCnt - 1].pBuffMsg = VOS_NULL_PTR;
        pstBuffer[*pucMsgCnt - 1].ulEvtType = 0;

        /* 数目-1 */
        (*pucMsgCnt) --;

        ulRslt = NAS_LMM_TRAVEL_BUFFER_MSG;

    }
    else
    {
        NAS_LMM_PUBM_LOG_NORM("NAS_LMM_OutMsgFromQue: *pucMsgCnt is invalid");
    }

    return ulRslt;
}
VOS_UINT32  NAS_LMM_StoreMsgToQue(
                NAS_LMM_PARALLEL_FSM_ENUM_UINT16   enParalFsmId,
                VOS_UINT32                        ulBufPriLevel,
                VOS_UINT32                        ulMsgType,
                const VOS_VOID                    *pMsg )
{
    VOS_UINT32                          ulLength;
    VOS_UINT8                          *pucMsgBuff          = NAS_LMM_NULL_PTR;
    NAS_LMM_BUFF_MSG_STRU			   *pstBuffer           = NAS_LMM_NULL_PTR;
    NAS_LMM_FSM_MSG_BUF_STRU            *pstFsmMsgBuffAddr   = NAS_LMM_NULL_PTR;
    VOS_UINT32                          ulRslt;

    /* 入口检查 ,为了避免内存泄漏,当前只支持EMM,如果需要支持MMC,可以修改以下条件即可,
    ** 并且在对应的模块需要添加相应的处理;另外对于中优先级的缓存,也不支持;*/
    if ( enParalFsmId != NAS_LMM_PARALLEL_FSM_EMM )
    {
        NAS_LMM_PUBM_LOG1_ERR("NAS_LMM_StoreMsgToQue, FsmId error, FsmId:", enParalFsmId);
        return  NAS_LMM_ERR_CODE_ARRAY_OVERFLOW;
    }

    if ( NAS_LMM_NULL_PTR == pMsg )
    {
        NAS_LMM_PUBM_LOG_WARN("NAS_LMM_StoreMsgToQue, Msg is NULL!");
        return  NAS_LMM_ERR_CODE_PTR_NULL;
    }
    if( (NAS_LMM_STORE_HIGH_PRIO_MSG != ulBufPriLevel) &&
        (NAS_LMM_STORE_LOW_PRIO_MSG  != ulBufPriLevel))
    {
        NAS_LMM_PUBM_LOG1_WARN("NAS_LMM_StoreMsgToQue, Prio err!",ulBufPriLevel);
        return  NAS_LMM_ERR_CODE_PARA_INVALID;
    }

    /* 获取缓存Buffer的地址 */
    pstFsmMsgBuffAddr                   = NAS_LMM_GetFsmBufAddr( enParalFsmId );
    if(NAS_LMM_NULL_PTR                  == pstFsmMsgBuffAddr)
    {
        NAS_LMM_PUBM_LOG_WARN(           "NAS_LMM_StoreMsgToQue, Get pstFsmMsgBuffAddr Fail!");
        return  NAS_LMM_ERR_CODE_PTR_NULL;

    }

    /*如果消息存在,移去该消息 */
    NAS_LMM_RemoveMsgFromFsmQue( pstFsmMsgBuffAddr, ulMsgType, ulBufPriLevel );


    pstBuffer = NAS_LMM_GetStoreBufMsgAddr( pstFsmMsgBuffAddr, ulBufPriLevel );
    if(NAS_LMM_NULL_PTR                  == pstBuffer)
    {
        NAS_LMM_PUBM_LOG_WARN(           "NAS_LMM_StoreMsgToQue, GetStoreBufMsgAddr Fail!");
        return  NAS_LMM_ERR_CODE_PTR_NULL;

    }

    /* 获取消息长度  */
    ulLength  = ((NAS_LMM_MSG_HEADER_STRU *)pMsg)->ulLength
                            + NAS_EMM_PUB_VOS_MSG_HEADER_LEN;

    /* 动态分配缓存空间 */
    pucMsgBuff                          = NAS_COMM_AllocBuffItem(NAS_COMM_BUFF_TYPE_EMM, ulLength);
    if(NAS_LMM_NULL_PTR                  == pucMsgBuff )
    {
        NAS_LMM_PUBM_LOG_WARN(           "NAS_LMM_StoreMsgToQue, Msg Allocate Fail!");
        return  NAS_LMM_ERR_CODE_PTR_NULL;
    }

    /* 把需要缓存的消息拷贝到申请到的地址中 */
    NAS_LMM_MEM_CPY( pucMsgBuff, pMsg, ulLength );

    /* 消息入队 */
    pstBuffer->pBuffMsg                 = (VOS_VOID*)(pucMsgBuff);
    pstBuffer->ulEvtType                = ulMsgType;
    ulRslt                              = NAS_LMM_BufMsgInQue( pstFsmMsgBuffAddr,ulBufPriLevel );

    if( NAS_LMM_FAIL                     == ulRslt)
    {
        NAS_LMM_PUBM_LOG_WARN(           "NAS_LMM_StoreMsgToQue, In Queue Fail!");
        ulRslt = NAS_COMM_FreeBuffItem(NAS_COMM_BUFF_TYPE_EMM, pucMsgBuff);

        if (NAS_COMM_BUFF_SUCCESS != ulRslt)
        {
            NAS_LMM_PUBM_LOG_WARN("NAS_LMM_StoreMsgToQue, Memory Free is not succ");
        }

        pstBuffer->pBuffMsg             = NAS_LMM_NULL_PTR;
        pstBuffer->ulEvtType            = 0;
        return  NAS_LMM_ERR_CODE_PTR_NULL;
    }

    return  NAS_LMM_SUCC;
}