/*****************************************************************************
 Function Name   : NAS_EMM_GetDetachType
 Description     : 编码DETACH REQ消息时,获取DETACH TYPE

         用户DetachType                   UE MODE
                                PS                      CS+PS
         EPS detach             EPS DETACH              EPS DETACH
         IMSI detach            在预处理里已处理        IMSI DETACH
         EPS/IMSI detach        EPS DETACH              EPS DETACH(当注册域为PS);
                                                        EPS/IMSI DETACH(当注册域为其他)

 Input           : 无
 Output          : 无
 Return          : MMC_LMM_MO_DETACH_TYPE_ENUM_UINT32

 History         :
    1.lihong00150010      2011-09-29  Draft Enact

*****************************************************************************/
MMC_LMM_MO_DETACH_TYPE_ENUM_UINT32 NAS_EMM_GetDetachType( VOS_VOID )
{
    MMC_LMM_MO_DETACH_TYPE_ENUM_UINT32  enDetachType = MMC_LMM_MO_DETACH_TYPE_BUTT;

    /* 获取用户请求的DETACH类型 */
    enDetachType = NAS_EMM_GLO_AD_GetDetTypeMo();

    /* 如果UE模式为PS,则返回类型为EPS ONLY */
    if (NAS_EMM_NO == NAS_EMM_IsCsPsUeMode())
    {
        return MMC_LMM_MO_DET_PS_ONLY;
    }

    /* 如果用户请求的DETACH类型不为EPS/IMSI,则返回用户请求的DETACH类型 */
    if (MMC_LMM_MO_DET_CS_PS != enDetachType)
    {
        return enDetachType;
    }

    /*如果用户请求的是EPS/IMSI DETACH,且当前注册域为PS,则返回EPS ONLY;如果当前
      注册域为CS+PS,则返回EPS/IMSI;如果当前注册域为NULL,则暂时暂时先返回EPS/IMSI,
      不再考虑UE是否被原因值#2拒绝过,也不考虑在收到DETACH消息之前用户是否允许在CS域注册 */
    if (NAS_LMM_REG_DOMAIN_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        return MMC_LMM_MO_DET_PS_ONLY;
    }
    else
    {
        return MMC_LMM_MO_DET_CS_PS;
    }
}
/*****************************************************************************
 Function Name   : NAS_EMM_GetSwitchOffDetachType
 Description     : 当为关机类型的DETACH时,获取DETACH TYPE

 Input           : 无
 Output          : 无
 Return          : MMC_LMM_MO_DETACH_TYPE_ENUM_UINT32

 History         :
    1.lihong00150010      2012-02-02  Draft Enact

*****************************************************************************/
MMC_LMM_MO_DETACH_TYPE_ENUM_UINT32 NAS_EMM_GetSwitchOffDetachType( VOS_VOID )
{
    VOS_UINT32                          ulCurEmmStat;

    /*如果用户请求的是EPS/IMSI DETACH,且当前注册域为PS,则返回EPS ONLY;如果当前
      注册域为CS+PS,则返回EPS/IMSI;如果当前注册域为NULL时,目前只有注册过程中的
      三种状态会发送关机DETACH消息,因此根据ATTACH类型来填写DETACH类型*/
    if (NAS_LMM_REG_DOMAIN_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        return MMC_LMM_MO_DET_PS_ONLY;
    }

    if (NAS_LMM_REG_DOMAIN_CS_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        return MMC_LMM_MO_DET_CS_PS;
    }

    ulCurEmmStat = NAS_LMM_PUB_COMP_EMMSTATE(   NAS_EMM_CUR_MAIN_STAT,
                                                NAS_EMM_CUR_SUB_STAT);

    if ((ulCurEmmStat != NAS_LMM_PUB_COMP_EMMSTATE(    EMM_MS_REG_INIT,
                                                        EMM_SS_ATTACH_WAIT_CN_ATTACH_CNF))
        && (ulCurEmmStat != NAS_LMM_PUB_COMP_EMMSTATE(  EMM_MS_REG_INIT,
                                                        EMM_SS_ATTACH_WAIT_ESM_BEARER_CNF))
        && (ulCurEmmStat != NAS_LMM_PUB_COMP_EMMSTATE(  EMM_MS_REG_INIT,
                                                        EMM_SS_ATTACH_WAIT_RRC_DATA_CNF)))
    {
        NAS_EMM_DETACH_LOG_WARN("NAS_EMM_GetSwitchOffDetachType:Illegal state!");

        return MMC_LMM_MO_DET_PS_ONLY;
    }

    /* 如果是ATTACH过程中的关机,则根据ATTACH类型填写DETACH类型 */
    if (MMC_LMM_ATT_TYPE_COMBINED_EPS_IMSI == NAS_EMM_GLO_GetCnAttReqType())
    {
        return MMC_LMM_MO_DET_CS_PS;
    }
    else
    {
        return MMC_LMM_MO_DET_PS_ONLY;
    }
}
VOS_UINT32    NAS_EMM_MsRegSsNoCellAvailMsgAppAttachReq( VOS_UINT32  ulMsgId,
                                                    VOS_VOID   *pMsgStru  )
{
    MMC_LMM_ATTACH_REQ_STRU             *pRcvEmmMsg;

    (VOS_VOID)ulMsgId;

    pRcvEmmMsg                          = (MMC_LMM_ATTACH_REQ_STRU *)pMsgStru;


    /*打印进入该函数*/
    NAS_EMM_ATTACH_LOG_NORM("NAS_EMM_MsRegSsNoCellAvailMsgAppAttachReq is entered");

    /*检查状态是否匹配,若不匹配,退出*/
    if (NAS_EMM_AD_CHK_STAT_INVALID(EMM_MS_REG,EMM_SS_REG_NO_CELL_AVAILABLE))
    {
        /*打印出错信息*/
         NAS_EMM_ATTACH_LOG_WARN("NAS_EMM_MsDrgSsPlmnSrcMsgAppAttachReq: STATE ERR!");
        return  NAS_LMM_MSG_DISCARD;
    }

    /*消息内容检查,若有错,打印并退出*/
    if (NAS_EMM_PARA_INVALID == NAS_EMM_AppAttachReqMsgChk(pRcvEmmMsg))
    {
        NAS_EMM_ATTACH_LOG_ERR("NAS_EMM_MsRegSsNoCellAvailMsgAppAttachReq: APP_MM_ATTACH_REQ_STRU PARA ERR!");
        return  NAS_LMM_ERR_CODE_PARA_INVALID;

    }


    /*保存APP参数*/
    NAS_EMM_SaveAppMsgPara(pRcvEmmMsg->ulMsgId,pRcvEmmMsg->ulOpId);

    if(MMC_LMM_ATT_REQ_TYPE_PS_ONLY == pRcvEmmMsg->ulAttachType)
    {

        /*向MMC发送ATTACH成功*/
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return  NAS_LMM_MSG_HANDLED;
    }
    if ( (NAS_EMM_YES == NAS_EMM_IsCsPsUeMode())
        &&(NAS_LMM_REG_DOMAIN_CS_PS == NAS_LMM_GetEmmInfoRegDomain()))
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }
    else
    {

        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_FAILURE);
        return NAS_LMM_MSG_HANDLED;
    }

}
VOS_UINT32    NAS_EMM_MsRegSsWaitAccessGrantIndMsgAppAttachReq( VOS_UINT32  ulMsgId,
                                                    VOS_VOID   *pMsgStru  )
{
    MMC_LMM_ATTACH_REQ_STRU              *pRcvEmmMsg;

    (VOS_VOID)ulMsgId;

    pRcvEmmMsg                          = (MMC_LMM_ATTACH_REQ_STRU *)pMsgStru;


    /*打印进入该函数*/
    NAS_EMM_ATTACH_LOG_NORM("NAS_EMM_MsRegSsWaitAccessGrantIndMsgAppAttachReq is entered");

    /*检查状态是否匹配,若不匹配,退出*/
    if (NAS_EMM_AD_CHK_STAT_INVALID(EMM_MS_REG,EMM_SS_REG_WAIT_ACCESS_GRANT_IND))
    {
        /*打印出错信息*/
         NAS_EMM_ATTACH_LOG_WARN("NAS_EMM_MsRegSsWaitAccessGrantIndMsgAppAttachReq: STATE ERR!");
        return  NAS_LMM_MSG_DISCARD;
    }

    /*消息内容检查,若有错,打印并退出*/
    if (NAS_EMM_PARA_INVALID == NAS_EMM_AppAttachReqMsgChk(pRcvEmmMsg))
    {
        NAS_EMM_ATTACH_LOG_ERR("NAS_EMM_MsRegSsWaitAccessGrantIndMsgAppAttachReq: MMC_LMM_ATTACH_REQ_STRU PARA ERR!");
        return  NAS_LMM_ERR_CODE_PARA_INVALID;

    }


    /*保存APP参数*/
    NAS_EMM_SaveAppMsgPara(pRcvEmmMsg->ulMsgId,pRcvEmmMsg->ulOpId);

    if(MMC_LMM_ATT_REQ_TYPE_PS_ONLY == pRcvEmmMsg->ulAttachType)
    {
        /*向MMC发送ATTACH成功*/
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }

    /*操作模式是PS*/
    if(NAS_EMM_NO == NAS_EMM_IsCsPsUeMode())
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_FAILURE);
        return NAS_LMM_MSG_HANDLED;
    }

    /*注册域为CS_PS*/
    if (NAS_LMM_REG_DOMAIN_CS_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }
    /*注册域为PS*/
    if (NAS_LMM_REG_DOMAIN_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        if( (NAS_EMM_REJ_YES == NAS_LMM_GetEmmInfoRejCause2Flag())
            ||(NAS_EMM_REJ_YES == NAS_EMMC_GetRejCause18Flag()))
        {
            NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_FAILURE);
            return NAS_LMM_MSG_HANDLED;
        }
        /*联合TAU*/
        /*信令被BAR不能发起,其它可以发起,内部消息处理中会判断,此处不再判断*/
        NAS_EMM_SendIntraTauReq(ID_APP_MM_MSG_TYPE_BUTT,NAS_LMM_INTRA_TAU_TYPE_IMSI_ATTACH);
        return NAS_LMM_MSG_HANDLED;
    }

    return  NAS_LMM_MSG_HANDLED;
}
VOS_UINT32    NAS_EMM_MsSerInitSsWtCnSerCnfMsgAppAttachReq( VOS_UINT32  ulMsgId,
                                                         VOS_VOID   *pMsgStru )
{
    MMC_LMM_ATTACH_REQ_STRU              *pRcvEmmMsg;

    (VOS_VOID)ulMsgId;

    pRcvEmmMsg                          = (MMC_LMM_ATTACH_REQ_STRU *)pMsgStru;


    /*打印进入该函数*/
    NAS_EMM_ATTACH_LOG_NORM("NAS_EMM_MsSerInitSsWtCnSerCnfMsgAppAttachReq is entered");

    /*检查状态是否匹配,若不匹配,退出*/
    if (NAS_EMM_AD_CHK_STAT_INVALID(EMM_MS_SER_INIT,EMM_SS_SER_WAIT_CN_SER_CNF))
    {
        /*打印出错信息*/
         NAS_EMM_ATTACH_LOG_WARN("NAS_EMM_MsSerInitSsWtCnSerCnfMsgAppAttachReq: STATE ERR!");
        return  NAS_LMM_MSG_DISCARD;
    }

    /*消息内容检查,若有错,打印并退出*/
    if (NAS_EMM_PARA_INVALID == NAS_EMM_AppAttachReqMsgChk(pRcvEmmMsg))
    {
        NAS_EMM_ATTACH_LOG_ERR("NAS_EMM_MsSerInitSsWtCnSerCnfMsgAppAttachReq: MMC_LMM_ATTACH_REQ_STRU PARA ERR!");
        return  NAS_LMM_ERR_CODE_PARA_INVALID;

    }

    /*保存APP参数*/
    NAS_EMM_SaveAppMsgPara(pRcvEmmMsg->ulMsgId,pRcvEmmMsg->ulOpId);

    if(MMC_LMM_ATT_REQ_TYPE_PS_ONLY == pRcvEmmMsg->ulAttachType)
    {
        /*向MMC发送ATTACH成功*/
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }

    /*请求类型是IMSI或者CS_ps,操作模式是PS*/
    if(NAS_EMM_NO == NAS_EMM_IsCsPsUeMode())
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_FAILURE);
        return NAS_LMM_MSG_HANDLED;
    }

    /*请求类型是IMSI或者CS_ps操作模式是CS_PS,注册域为CS_PS*/
    if (NAS_LMM_REG_DOMAIN_CS_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }
    /*请求类型是IMSI或者CS_ps操作模式是CS_PS,注册域为PS*/
    if(NAS_LMM_REG_DOMAIN_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        /* 发起TAU,打断SERVICE */
        /*停止T3417定时器*/
        NAS_LMM_StopStateTimer(TI_NAS_EMM_STATE_SERVICE_T3417);

        /*Inform RABM that SER fail*/
        NAS_EMM_SER_SendRabmReestInd(EMM_ERABM_REEST_STATE_FAIL);

        /*结束SER流程*/
        /* NAS_EMM_SER_AbnormalOver();*/

        /*设置流程冲突标志位*/
        NAS_EMM_TAU_SaveEmmCollisionCtrl(NAS_EMM_COLLISION_SERVICE);

        /*NAS_EMM_TAU_SaveEmmTAUStartCause(NAS_EMM_TAU_START_CAUSE_IMSI_ATTACH);*/
        NAS_EMM_TAU_SaveEmmTAUStartCause(NAS_EMM_TAU_START_CAUSE_OTHERS);

        /*启动TAU Procedure*/
        NAS_EMM_TAU_StartTAUREQ();

        return NAS_LMM_MSG_HANDLED;
    }

    return  NAS_LMM_MSG_HANDLED;
}
VOS_UINT32    NAS_EMM_MsTauInitSsWtCnTauCnfMsgAppAttachReq(VOS_UINT32  ulMsgId,
                                                    VOS_VOID   *pMsgStru )
{
    MMC_LMM_ATTACH_REQ_STRU              *pRcvEmmMsg;

    (VOS_VOID)ulMsgId;

    pRcvEmmMsg                          = (MMC_LMM_ATTACH_REQ_STRU *)pMsgStru;


    /*打印进入该函数*/
    NAS_EMM_ATTACH_LOG_NORM("NAS_EMM_MsTauInitSsWtCnTauCnfMsgAppAttachReq is entered");

    /*检查状态是否匹配,若不匹配,退出*/
    if (NAS_EMM_AD_CHK_STAT_INVALID(EMM_MS_TAU_INIT,EMM_SS_TAU_WAIT_CN_TAU_CNF))
    {
        /*打印出错信息*/
         NAS_EMM_ATTACH_LOG_WARN("NAS_EMM_MsDrgSsPlmnSrcMsgAppAttachReq: STATE ERR!");
        return  NAS_LMM_MSG_DISCARD;
    }

    /*消息内容检查,若有错,打印并退出*/
    if (NAS_EMM_PARA_INVALID == NAS_EMM_AppAttachReqMsgChk(pRcvEmmMsg))
    {
        NAS_EMM_ATTACH_LOG_ERR("NAS_EMM_MsTauInitSsWtCnTauCnfMsgAppAttachReq: MMC_LMM_ATTACH_REQ_STRU PARA ERR!");
        return  NAS_LMM_ERR_CODE_PARA_INVALID;

    }


    /*保存APP参数*/
    NAS_EMM_SaveAppMsgPara(pRcvEmmMsg->ulMsgId,pRcvEmmMsg->ulOpId);

    if(MMC_LMM_ATT_REQ_TYPE_PS_ONLY == pRcvEmmMsg->ulAttachType)
    {
        /*向MMC发送ATTACH成功*/
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }

    /*操作模式是PS*/
    if(NAS_EMM_NO == NAS_EMM_IsCsPsUeMode())
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_FAILURE);
        return NAS_LMM_MSG_HANDLED;
    }
    /*操作模式是CS_PS*/
    /*注册域为CS_PS*/
    if (NAS_LMM_REG_DOMAIN_CS_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        NAS_EMM_MmcSendAttCnf(MMC_LMM_ATT_RSLT_SUCCESS);
        return NAS_LMM_MSG_HANDLED;
    }
    /*注册域为PS*/
    if(NAS_LMM_REG_DOMAIN_PS == NAS_LMM_GetEmmInfoRegDomain())
    {
        return NAS_LMM_STORE_LOW_PRIO_MSG;
    }

    return  NAS_LMM_MSG_HANDLED;
}
VOS_UINT32  NAS_EMM_MsRegSsRegNmlSrvMsgIntraDetReq(VOS_UINT32  ulMsgId,
                                                   VOS_VOID   *pMsgStru)
{
    MMC_LMM_TAU_RSLT_ENUM_UINT32        ulTauRslt = MMC_LMM_TAU_RSLT_BUTT;
    NAS_LMM_INTRA_DETACH_REQ_STRU              *pRcvEmmMsg;
    pRcvEmmMsg                          = (NAS_LMM_INTRA_DETACH_REQ_STRU *) pMsgStru;

    /*打印进入该函数*/
    NAS_EMM_DETACH_LOG1_INFO("NAS_EMM_MsRegSsRegNmlSrvMsgTauIntraMsgDetReq is entered", ulMsgId);


     /*检查状态是否匹配,若不匹配,退出*/
    if (NAS_EMM_AD_CHK_STAT_INVALID(EMM_MS_REG,EMM_SS_REG_NORMAL_SERVICE))
    {
        /*打印出错信息*/
        NAS_EMM_DETACH_LOG_WARN("NAS_EMM_MsRegSsRegNmlSrvMsgTauIntraMsgDetReq: STATE ERR!");
        return NAS_LMM_MSG_DISCARD;
    }

    /*消息内容检查,若有错,打印并退出*/
    if (NAS_EMM_PARA_INVALID == NAS_EMM_IntraDetReqChk(pRcvEmmMsg))
    {
        NAS_EMM_DETACH_LOG_ERR("NAS_EMM_MsRegSsRegNmlSrvMsgTauIntraMsgDetReq: MMC_MM_SYS_INFO_IND_STRU para err!");
        return  NAS_LMM_ERR_CODE_PARA_INVALID;
    }

    /* 如果是IMSI DETACH类型,则发起IMSI DETACH流程 */
    if (MMC_LMM_MO_DET_CS_ONLY == NAS_EMM_GLO_AD_GetDetTypeMo())
    {
        NAS_EMM_DETACH_LOG_INFO("NAS_EMM_MsRegSsRegNmlSrvMsgTauIntraMsgDetReq:Proceed Imsi Detach");

        /* IMSI detach被TAU打断,如果TAU ACCEPT是Ta Updated Only,原因值为#2或者#18
           也会进入NORMAL_SERVICE,发送内部DETACH请求,这时不应该再发起IMSI DETACH流程*/
        if (NAS_LMM_REG_DOMAIN_PS == NAS_LMM_GetEmmInfoRegDomain())
        {
            /*向APP发送DETACH成功*/
            NAS_EMM_AppSendDetCnf(MMC_LMM_DETACH_RSLT_SUCCESS);

            /* IMSI DETACH后释放资源 */
            NAS_LMM_ImsiDetachReleaseResource();

            return  NAS_LMM_MSG_HANDLED;
        }

        /* 发起IMSI DETACH流程 */
        NAS_EMM_SendImsiDetachReqMo();
        return  NAS_LMM_MSG_HANDLED;
    }

    NAS_EMM_DETACH_LOG_INFO("NAS_EMM_MsRegSsRegNmlSrvMsgTauIntraMsgDetReq:Proceed EPS Detach or EPS/IMSI Detach");

    /* 如果3411在运行,需要给MMC报TAU结果,以让MMC退出搜网状态机;3402定时器在
       运行无需通知,MMC收到TAU结果尝试次数为5时,已退出搜网状态机 */
    if (NAS_LMM_TIMER_RUNNING == NAS_LMM_IsPtlTimerRunning(TI_NAS_EMM_PTL_T3411))
    {
        NAS_EMM_SetTauTypeNoProcedure();

        ulTauRslt = MMC_LMM_TAU_RSLT_MO_DETACH_FAILURE;
        NAS_EMM_MmcSendTauActionResultIndOthertype((VOS_VOID*)&ulTauRslt);

    }

    /*停止定时器*/
    NAS_LMM_StopPtlTimer(                TI_NAS_EMM_PTL_T3411);
    NAS_LMM_StopPtlTimer(                TI_NAS_EMM_PTL_T3402);

    /*发送DETACH REQUEST消息*/
    NAS_EMM_SendDetachReqMo();

    return   NAS_LMM_MSG_HANDLED;
}