/******************************************************************************/
// @copyright   Copyright Notification
//   No part may be reproduced except as authorized by written permission.
//   The copyright and the foregoing restriction extend to reproduction in all media.
//   Trademark 2010, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC).
//   All rights reserved.
// @version: IWD_10wk37
// $Date: 2010-09-08 10:25:40 +0200 (Wed, 08 Sep 2010) $
// $Rev: 3833 $
/******************************************************************************/
module InterRat_PLMNSelection_UTRAN
{
//=========================================================================
// Imports
//=========================================================================

  import from UTRAN_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from UTRAN_ASP_Definitions language "ASN.1:1997" all;
  import from UTRAN_CommonFunctions all;
  import from UTRANGERAN_CommonFunctions all;
  import from UTRAN_Component all;
  import from UTRAN_ASN1_Templates all;
  import from UTRAN_CellInfo all;
  import from UTRAN_Templates all;
  import from UTRAN_TypeDefs all;
  import from UTRAN_ConfigurationSteps all;
  
  import from UpperTesterFunctions all;
  import from CommonIratDefs all;
  
  import from CommonDefs all;
  import from NAS_24008TypeDefs all;
  
//==============================================================================
// Types
//==============================================================================

  type MultiplePLMN_List_r6.multiplePLMNs Multiple_PLMN_List_Type;

//==============================================================================
// Templates
//==============================================================================

  template (value) MasterInformationBlock cds_MIB_MultiplePlmnList_FDD ( PLMN_Identity                             p_PLMN,
                                                                        Multiple_PLMN_List_Type p_Multiple_PLMN_List )
  modifies cs_MIB_Def :=
  {
    v690NonCriticalExtensions := {
      masterInformationBlock_v690ext := {
        multiplePLMN_List := {
          mibPLMN_Identity := true,
          //FFS - value of mibPLMN_Identity is not specified in prose
          //It should however be set to true otherwise
          // => UE will not consider PLMN14
          // => UE will not select cell 5 in step 20
          // => conformant UE will fail the test case
          // Reference: 25.331 cl. 8.1.1.5
          /*
            3> if the IE "MIB PLMN Identity" is set to TRUE:
              4> read the "PLMN identity" IE in the MIB and consider it as a part of the "Multiple PLMN List".
            3> if the IE "MIB PLMN Identity" is set to FALSE:
              4> not consider the "PLMN identity" IE in the MIB as a part of the "Multiple PLMN List";
              4> not consider the IE "PLMN identity" in the MIB as a PLMN identity of the cell;
              4> not forward the PLMN in the IE "PLMN identity" of the MIB to upper layers.
          */
          multiplePLMNs := p_Multiple_PLMN_List
        }
      },
      v6b0NonCriticalExtensions := omit
    }
  }
  
  template (value) MasterInformationBlock cds_MIB_MultiplePlmnList_TDD ( PLMN_Identity                            p_PLMN,
                                                                        Multiple_PLMN_List_Type p_Multiple_PLMN_List )
  modifies cs_MIB_Def_TDD :=
  {
    v690NonCriticalExtensions := {
      masterInformationBlock_v690ext := {
        multiplePLMN_List := {
          mibPLMN_Identity := true,
          //FFS - value of mibPLMN_Identity is not specified in prose
          //It should however be set to true otherwise
          // => UE will not consider PLMN14
          // => UE will not select cell 5 in step 20
          // => conformant UE will fail the test case
          // Reference: 25.331 cl. 8.1.1.5
          /*
            3> if the IE "MIB PLMN Identity" is set to TRUE:
              4> read the "PLMN identity" IE in the MIB and consider it as a part of the "Multiple PLMN List".
            3> if the IE "MIB PLMN Identity" is set to FALSE:
              4> not consider the "PLMN identity" IE in the MIB as a part of the "Multiple PLMN List";
              4> not consider the IE "PLMN identity" in the MIB as a PLMN identity of the cell;
              4> not forward the PLMN in the IE "PLMN identity" of the MIB to upper layers.
          */
          multiplePLMNs := p_Multiple_PLMN_List
        }
      },
      v6b0NonCriticalExtensions := omit
    }
  }

//==============================================================================
// Functions
//==============================================================================

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.1
   * @status
   */
  function f_TC_6_2_1_1_UTRAN ( ) runs on UTRAN_PTC {
  
    var InitialUE_Identity v_InitialUE_Id;
    var RLC_TR_DATA_IND v_RLC_TR_DATA_IND;
    var integer v_T0andT3_CPICH_Cell5, v_T0andT3_PCCPCH_Cell5, v_T1_CPICH_Cell5, v_T1_PCCPCH_Cell5;
    var PLMN_Identity v_PLMN2;
    var boolean v_TriggerRxd := false;
    var template (present) EstablishmentCause v_EstCause := registration;
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer_UTRAN;

    v_T0andT3_CPICH_Cell5 := tsc_AttenuationServingCell;//No attnuation => serving cell
    v_T0andT3_PCCPCH_Cell5 := tsc_AttenuationServingCell;//No attnuation => serving cell
    v_T1_CPICH_Cell5 := tsc_AttenuationCellOFF;//Full attenuation => cell is off/non-suitable
    v_T1_PCCPCH_Cell5 := tsc_AttenuationCellOFF;//Full attenuation => cell is off/non-suitable
    v_PLMN2 := {mcc := {0,0,2}, mnc := {1,1}};//PLMN2 according to 36.523-1 Table 6.0.1-1
    
    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN_GERAN);//RAC is set to '00'O

    //Set PLMN2 in cell 5
    f_UTRAN_CellInfo_SetPLMNIdentity ( utran_Cell5, v_PLMN2 );
    
    //Create and configure Cell 5
    //Cell started switched on with default power level, -60 and max attenuation
    f_SS_CreateCellFACH ( utran_Cell5 );

    //Start sending System Information
    f_UTRAN_SendDefSysInfo ( utran_Cell5 );
    
    //Wait for synch with EUTRA PTC, then inform GERAN PTC and set cell power level according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    f_SendIRATCoOrd ( GERAN, cms_IRAT_Trigger );
    f_UTRAN_SetCellPower(utran_Cell5, v_T0andT3_CPICH_Cell5, v_T0andT3_PCCPCH_Cell5);
    
    f_UTRAN_TestBody_Set ( true );
    
    //@siclog "Step 19" siclog@
    //Set cell power level according to T1
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    f_UTRAN_SetCellPower(utran_Cell5, v_T1_CPICH_Cell5, v_T1_PCCPCH_Cell5);
    
    //@siclog "Step 20" siclog@
    //Set cell power level according to T2
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );
    
    //Receive updated Authentication parameters from GERAN PTC
    f_UTRAN_InterRAT_InitialiseAuthParams();
    //FFS - this function only expects message from EUTRA PTC?
    //Hence the workaround in the EUTRA PTC
    
    //@siclog "Step 34" siclog@
    //Set cell power level according to T3
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (GERAN);
    f_UTRAN_SetCellPower(utran_Cell5, v_T0andT3_CPICH_Cell5, v_T0andT3_PCCPCH_Cell5);

    //@siclog "Step 36" siclog@
    //Wait for RRC CONNECTION REQUEST on cell 5
    t_IdleMode_GenericTimer.start;
    U_TM.receive ( car_RRC_ConnReq ( utran_Cell5,
                                     tsc_RB0,
                                     cdr_RRC_ConnReq_r8OrLater ( v_EstCause ) ) ) -> value v_RLC_TR_DATA_IND;
    v_InitialUE_Id := v_RLC_TR_DATA_IND.tM_message.uL_CCCH_Message.message_.rrcConnectionRequest.initialUE_Identity;
    
    //@siclog "Step 37 - 38" siclog@
    //Step 37-38: Send RRC CONNECTION SETUP and receive RRC CONNECTION SETUP COMPLETE
    f_UTRAN_RRC_ConnEst_WithoutRRCConnectionReq ( utran_Cell5, v_InitialUE_Id );
 
    //@siclog "Step 39 - 47" siclog@
    //Routing Area Update and RRC Security
    f_UTRAN_GMM_RAU_MappedContext ( utran_Cell5 );
    
    //Release RRC Connection
    f_UTRAN_RRC_ConnRel ( utran_Cell5, cell_Fach_Dcch );
    
    //@siclog "Step 48" siclog@
    //Check if PLMN2 is indicated by UE
    f_UT_PLMN_Check ( UT, "00211" );
    //* @verdict pass PLMN2 is indicated by UE
    f_UTRAN_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.1 Step 48");

    f_UTRAN_TestBody_Set ( false );
    
    //Tell the EUTRA and the GERAN component that the test case is over
    f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger);
    f_SendIRATCoOrd (GERAN, cms_IRAT_Trigger);

    //Switch/Power off, detach UE and release cell
    f_UTRA_Postamble ( utran_Cell5, U1_IDLE );
  }
  
  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.2
   * @status
   */
  function f_TC_6_2_1_2_UTRAN ( ) runs on UTRAN_PTC {
  
    var InitialUE_Identity v_InitialUE_Id;
    var RLC_TR_DATA_IND v_RLC_TR_DATA_IND;
    var integer v_T0_CPICH_Cell5, v_T0_PCCPCH_Cell5;
    var PLMN_Identity v_PLMN14;
    var boolean v_TriggerRxd := false;
    var template (present) EstablishmentCause v_EstCause := registration;
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer_UTRAN;
    
    v_PLMN14 := {mcc := {0,0,2}, mnc := {9,1}};//PLMN14 according to 36.523-1 Table 6.0.1-1
    v_T0_CPICH_Cell5 := tsc_AttenuationServingCell;//No attnuation => serving cell
    v_T0_PCCPCH_Cell5 := tsc_AttenuationServingCell;//No attnuation => serving cell
    
    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);
    
    //Set PLMN14 in cell 5
    f_UTRAN_CellInfo_SetPLMNIdentity ( utran_Cell5, v_PLMN14 );
    
    //Create and configure Cell 5
    //Cell started switched on with default power level, -60 and max attenuation
    f_SS_CreateCellFACH ( utran_Cell5 );

    //Start sending System Information
    f_UTRAN_SendDefSysInfo ( utran_Cell5 );
    
    //Wait for synch with EUTRA PTC and then set cell power level according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    f_UTRAN_SetCellPower(utran_Cell5, v_T0_CPICH_Cell5, v_T0_PCCPCH_Cell5);
    
    //Tell EUTRA PTC to start the test case
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );
    
    //Receive updated Authentication parameters
    f_UTRAN_InterRAT_InitialiseAuthParams();
    
    //@siclog "Step 19" siclog@
    //Set cell power level according to T1
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    
    //@siclog "Step 20" siclog@
    //Wait for RRC CONNECTION REQUEST on cell 5
    t_IdleMode_GenericTimer.start;
    U_TM.receive ( car_RRC_ConnReq ( utran_Cell5,
                                     tsc_RB0,
                                     cdr_RRC_ConnReq_r8OrLater ( v_EstCause ) ) ) -> value v_RLC_TR_DATA_IND;
    v_InitialUE_Id := v_RLC_TR_DATA_IND.tM_message.uL_CCCH_Message.message_.rrcConnectionRequest.initialUE_Identity;
    
    //@siclog "Step 21 - 22" siclog@
    //Send RRC CONNECTION SETUP and receive RRC CONNECTION SETUP COMPLETE
    f_UTRAN_RRC_ConnEst_WithoutRRCConnectionReq ( utran_Cell5, v_InitialUE_Id );
 
    //@siclog "Step 23 - 29" siclog@
    //Routing Area Update and RRC Security
    f_UTRAN_GMM_RAU_MappedContext ( utran_Cell5 );
    
    //@siclog "Step 30 - 31" siclog@
    //Release RRC Connection
    f_UTRAN_RRC_ConnRel ( utran_Cell5, cell_Fach_Dcch );
    
    //@siclog "Step 32" siclog@
    //Check if PLMN14 is indicated by UE
    f_UT_PLMN_Check ( UT, "00291" );
    //* @verdict pass PLMN14 is indicated by UE
    f_UTRAN_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.2 Step 32");

    f_UTRAN_TestBody_Set ( false );
    
    //Tell the EUTRA component that the test case is over
    f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger);

    //Switch/Power off UE and release cell
    f_UTRA_Postamble ( utran_Cell5, U1_IDLE );
  }
  
  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.3
   * @status
   */
  function f_TC_6_2_1_3_UTRAN ( ) runs on UTRAN_PTC {
  
    var UTRAN_FDD_TDD v_FDD_TDD_Mode;
    var template (value) MasterInformationBlock v_MIB_MultiplePlmnList;
    var InitialUE_Identity v_InitialUE_Id;
    var RLC_TR_DATA_IND v_RLC_TR_DATA_IND;
    var U_RLC_AM_IND v_RLC_AM_DATA_IND;
    var integer v_T0_CPICH_Cell5, v_T0_PCCPCH_Cell5;
    var PLMN_Identity v_PLMN14;
    var PLMN_IdentityWithOptionalMCC_r6 v_PLMN13;
    var octetstring v_RauReq_Msg;
    var ROUTINGAREAUPDATEREQUEST v_ReceivedRauReq_Msg;
    var B3_Type v_UpdateValue;
    var START_Value v_START_Value;
    var boolean v_TriggerRxd := false;
    var template (present) EstablishmentCause v_EstCause := registration;
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer_UTRAN;

    v_PLMN14 := {mcc := {0,0,2}, mnc := {9,1}};//PLMN14 according to 36.523-1 Table 6.0.1-1
    v_PLMN13 := {mcc := {0,0,2}, mnc := {8,1}};//PLMN13 according to 36.523-1 Table 6.0.1-1
    v_T0_CPICH_Cell5 := tsc_AttenuationServingCell;//No attnuation => serving cell
    v_T0_PCCPCH_Cell5 := tsc_AttenuationServingCell;//No attnuation => serving cell
    
    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);//RAC is set to '00'O
    
    //Get FDD or TDD mode
    v_FDD_TDD_Mode := f_UTRAN_CellInfo_GetFDD_TDD ( utran_Cell5 );
    
    //Set PLMN14 in cell 5
    f_UTRAN_CellInfo_SetPLMNIdentity ( utran_Cell5, v_PLMN14 );

    //Set MIB to include support of shared network
    if ( v_FDD_TDD_Mode == UTRAN_FDD ) {
      v_MIB_MultiplePlmnList := cds_MIB_MultiplePlmnList_FDD ( v_PLMN14, {v_PLMN13} )
    }
    else {
      v_MIB_MultiplePlmnList := cds_MIB_MultiplePlmnList_FDD ( v_PLMN14, {v_PLMN13} )
    }
    f_UTRAN_SysInfo_SetMIB ( utran_Cell5, v_MIB_MultiplePlmnList );
    
    //Create and configure Cell 5
    //Cell started switched on with default power level, -60 and max attenuation
    f_SS_CreateCellFACH ( utran_Cell5 );

    //Start sending System Information
    f_UTRAN_SendDefSysInfo ( utran_Cell5 );
    
    //Wait for synch with EUTRA PTC and then set cell power level according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    f_UTRAN_SetCellPower(utran_Cell5, v_T0_CPICH_Cell5, v_T0_PCCPCH_Cell5);
    
    //Tell EUTRA PTC to start the test case
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );
    
    f_UTRAN_TestBody_Set ( true );
    
    //Receive updated Authentication parameters
    f_UTRAN_InterRAT_InitialiseAuthParams();
    
    //@siclog "Step 19" siclog@
    //Set cell power level according to T1
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    
    //@siclog "Step 20" siclog@
    //Wait for RRC CONNECTION REQUEST on cell 5
    t_IdleMode_GenericTimer.start;
    U_TM.receive ( car_RRC_ConnReq ( utran_Cell5,
                                     tsc_RB0,
                                     cdr_RRC_ConnReq_r8OrLater ( v_EstCause ) ) ) -> value v_RLC_TR_DATA_IND;
    v_InitialUE_Id := v_RLC_TR_DATA_IND.tM_message.uL_CCCH_Message.message_.rrcConnectionRequest.initialUE_Identity;
    
    //@siclog "Step 21 - 22" siclog@
    //Send RRC CONNECTION SETUP and receive RRC CONNECTION SETUP COMPLETE
    f_UTRAN_RRC_ConnEst_WithoutRRCConnectionReq ( utran_Cell5, v_InitialUE_Id );
    
    f_UTRA_SS_SetConfigRRC_RB3 ( utran_Cell5 );
    
    //@siclog "Step 23" siclog@
    //Receive Routing Area Request message with PLMN identity set to PLMN14
    U_AM.receive ( car_RLC_Data_Ind ( utran_Cell5,
                                      tsc_RB3,
                                      cr_InitDirectTransfer ( ?,
                                                              v_PLMN14 ) ) ) -> value v_RLC_AM_DATA_IND;
                                                              
    //Decode the RAU Req message
    v_RauReq_Msg := v_RLC_AM_DATA_IND.data.aM_message.uL_DCCH_Message.message_.initialDirectTransfer.nas_Message;
    v_ReceivedRauReq_Msg := f_DecodeRauReq_Msg ( v_RauReq_Msg );
    
    v_UpdateValue := v_ReceivedRauReq_Msg.updateType.updateValue;
    v_START_Value := v_RLC_AM_DATA_IND.data.aM_message.uL_DCCH_Message.message_.initialDirectTransfer.v3a0NonCriticalExtensions.initialDirectTransfer_v3a0ext.start_Value;
    
    f_UTRA_SS_RemoveConfigRRC_RB3 ( utran_Cell5 );

    //@siclog "Step 24 - 29" siclog@
    //Perform 5-10 of the Routing Area Update procedure (36.508 cl. 6.4.2.8)
    f_UTRAN_GMM_RAU_Without_RAUReq ( utran_Cell5, v_START_Value, v_UpdateValue, false );
    
    //@siclog "Step 30 - 31" siclog@
    //Release RRC Connection
    f_UTRAN_RRC_ConnRel ( utran_Cell5, cell_Fach_Dcch );
    
    //@siclog "Step 32" siclog@
    //Check if PLMN14 is indicated by UE
    f_UT_PLMN_Check ( UT, "00291" );
    //* @verdict pass PLMN14 is indicated by UE
    f_UTRAN_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.3 Step 32");

    f_UTRAN_TestBody_Set ( false );
    
    //Tell the EUTRA component that the test case is over
    f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger);

    //Switch/Power off UE and release cell
    f_UTRA_Postamble ( utran_Cell5, U1_IDLE );
  }
}
