/******************************************************************************/
// @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-16 12:38:24 +0200 (Thu, 16 Sep 2010) $
// $Rev: 3923 $
/******************************************************************************/
module InterRat_PLMNSelection
{
//=========================================================================
// Imports
//=========================================================================

  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_Component all;
  import from EUTRA_IdleMode all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_CellInfo all;
  import from EUTRA_RRC_Templates all;
  import from EUTRA_ASP_TypeDefs all;
  import from EUTRA_CommonDefs all;
  import from EUTRA_IRATFunctions all;
  
  import from CommonIratDefs all;
  import from UpperTesterFunctions all;
  
  import from EPS_NAS_Templates all;
  import from EPS_NAS_MsgContainers all;
  import from NAS_CommonTypeDefs all;
  import from EUTRA_NASSteps all;
  import from EPS_NAS_TypeDefs all;
  import from EPS_NAS_Constants all;
  import from EUTRA_RRCSteps all;
  import from CommonDefs all;
  import from EUTRA_CommonProcedures all;
  import from EUTRA_ASP_SrbDefs all;

//==============================================================================
// Templates
//==============================================================================
  
  template (value) PLMN_Identity cs_HPLMN_002_81 := { mcc := {0,0,2},  mnc :={8,1} };//PLMN13 according to 36.523-1 Table 6.0.1-1
  template (value) PLMN_Identity cs_HPLMN_002_91 := { mcc := {0,0,2},  mnc :={9,1} };//PLMN14 according to 36.523-1 Table 6.0.1-1

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

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.1
   * @status
   */
  function f_TC_6_2_1_1_EUTRA ( ) runs on EUTRA_PTC {
  
    var boolean v_TriggerRxd := false;
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer;

    //Initialise all cell, security and mobile parameters
    f_EUTRA_Init (c10);
    
    //Set TAC according to 36.523-1 Table 6.0.1-2
    //TAC for Cell 1 is set to 1 in f_EUTRA_Init ();
    f_EUTRA_CellInfo_SetTAC ( eutra_Cell13, tsc_IdleMode_TAC_Cell13 );
    
    //Set PLMN according to Table 6.2.1.2.3.2-1: Cell PLMN identities
    f_EUTRA_CellInfo_SetPLMN_1Entry ( eutra_Cell1, cs_HPLMN_002_11 );//PLMN2 according to 36.523-1 Table 6.0.1-1
    f_EUTRA_CellInfo_SetPLMN_1Entry ( eutra_Cell13, cs_HPLMN_002_91 );//PLMN14 according to 36.523-1 Table 6.0.1-1
    
    //Set Minimum UL flag for Cell 1
    f_EUTRA_SetCellConfigCapability ( eutra_Cell13, minimumUplinkCell );
    
    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );
    f_EUTRA_CellConfig_Def ( eutra_Cell13 );
    
    //Equip UE with a USIM containing default values except for those shown in Table 6.2.1.1.3.1-1
    f_UT_USIM_Insert ( UT, "36.523-1 Table 6.2.1.1.3.1-1" );

    //Set initial cell power level according to T0
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);//EUTRA -> UTRA -> GERAN -> EUTRA
    f_EUTRA_SetCellPower ( eutra_Cell1, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now );
    
    //Wait for correct power levels and then start the test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( GERAN );
    
    f_EUTRA_TestBody_Set ( true );
    
    //@siclog "Step 1" siclog@
    //Switch UE on
    f_UT_SwitchOnUE ( UT, false );
    t_IdleMode_GenericTimer.start;
    
    //@siclog "Step 2" siclog@
    //Wait for RRCConnectionRequest on cell 1
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1, cr_508_RRCConnectionRequest ) );
    t_IdleMode_GenericTimer.stop;
    //@verdict pass UE sends RRCConnectionRequest on cell 1
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.1 step 2");

    //@siclog "Step 3 - 17" siclog@
    //Perform registration on cell 1
    f_EUTRA_IdleUpdated_WithoutRRCConnReq ( eutra_Cell1, STATE2_IDLEUPDATE );
    
    //Since Authentication parameters have been updated. Send them to GERAN PTC
    f_EUTRA_SendAuthParameters (eutra_Cell1, GERAN);
    
    //@siclog "Step 18" siclog@
    //Check if PLMN2 is indicated by UE
    f_UT_PLMN_Check ( UT, "00211" );
    //* @verdict pass PLMN2 is indicated by UE
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.1 Step 18");
    
    //@siclog "Step 19" siclog@
    //Adjust cell power levels according to T1
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
    
    //@siclog "Step 20" siclog@
    //Adjust cell power levels according to T2
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (UTRAN);
    f_SendIRATCoOrd (GERAN, cms_IRAT_Trigger);//Tell GERAN PTC to start with/at step 21.
    f_EUTRA_SetCellPower ( eutra_Cell1, tsc_NonSuitableOffCellRS_EPRE, cs_TimingInfo_Now );
    
    //Since Authentication parameters were updated in the RAU on GERAN side, receive updatedparameters...
    f_EUTRA_InterRAT_InitialiseAuthParams(GERAN);
    //...and send them to UTRAN PTC where they will be used in RAU procedure (steps 36-47)
    f_EUTRA_SendAuthParameters (eutra_Cell1, UTRAN);
    
    //@siclog "Step 34" siclog@
    //Adjust cell power levels according to T3
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (GERAN);
    f_EUTRA_SetCellPower ( eutra_Cell13, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now );
    
    f_EUTRA_TestBody_Set ( true );
    
    //Wait till end of test case then release cells
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (UTRAN);
    f_EUTRA_ReleaseAllCells( );
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.2
   * @status
   */
  function f_TC_6_2_1_2_EUTRA ( ) runs on EUTRA_PTC {
  
    var template (value) CellPowerList_Type v_CellPowerList_AtT0;
    var boolean v_TriggerRxd := false;
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer;
    v_CellPowerList_AtT0 := {
      cs_CellPower ( eutra_Cell1, tsc_ServingCellRS_EPRE ),
      cs_CellPower ( eutra_Cell12, tsc_ServingCellRS_EPRE )
    };
    
    //Initialise all cell, security and mobile parameters
    f_EUTRA_Init (c4);//SIB2, SIB3 & SIB6 are transmitted
    
    //Set TAC according to 36.523-1 Table 6.0.1-2
    //TAC for Cell 1 is set to 1 in f_EUTRA_Init ();
    f_EUTRA_CellInfo_SetTAC ( eutra_Cell12, tsc_IdleMode_TAC_Cell12 );
    
    //Set PLMN according to Table 6.2.1.2.3.2-1: Cell PLMN identities
    f_EUTRA_CellInfo_SetPLMN_1Entry ( eutra_Cell1, cs_HPLMN_002_11 );//PLMN1
    f_EUTRA_CellInfo_SetPLMN_1Entry ( eutra_Cell12, cs_HPLMN_001_21 );//PLMN2
    
    //Set Minimum UL flag for Cell 1
    f_EUTRA_SetCellConfigCapability ( eutra_Cell1, minimumUplinkCell );
    
    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell12 );
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );
    
    //Equip UE with a USIM containing default values except for those shown in Table 6.2.1.2.3.1-1
    f_UT_USIM_Insert ( UT, "36.523-1 Table 6.2.1.2.3.1-1" );

    //Set initial cell power level according to T0
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
    f_EUTRA_SetCellPowerList ( v_CellPowerList_AtT0, cs_TimingInfo_Now );
    
    //Wait for correct power levels and then start the test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    
    f_EUTRA_TestBody_Set ( true );
    
    //@siclog "Step 1" siclog@
    //Switch UE on
    f_UT_SwitchOnUE ( UT, false );
    t_IdleMode_GenericTimer.start;
    
    //@siclog "Step 2" siclog@
    //Wait for RRCConnectionRequest on cell 12
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell12, cr_508_RRCConnectionRequest ) );
    t_IdleMode_GenericTimer.stop;
    //@verdict pass UE sends RRCConnectionRequest on cell 12
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.2 Step 2");

    //@siclog "Step 3 - 17" siclog@
    //Perform registration on cell 12
    f_EUTRA_IdleUpdated_WithoutRRCConnReq ( eutra_Cell12, STATE2_IDLEUPDATE );
    
    //Since Authentication parameters have been updated. Send them to UTRAN PTC
    f_EUTRA_SendAuthParameters (eutra_Cell12, UTRAN);
    
    //@siclog "Step 18" siclog@
    //Check if PLMN13 is indicated by UE
    f_UT_PLMN_Check ( UT, "00281" );
    //* @verdict pass PLMN13 is indicated by UE
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.2 Step 18");
    
    //@siclog "Step 19" siclog@
    //Adjust cell power levels according to T1
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
    f_EUTRA_SetCellPower ( eutra_Cell12, tsc_NonSuitableOffCellRS_EPRE, cs_TimingInfo_Now );
    
    f_EUTRA_TestBody_Set ( true );
    
    //Wait till end of test case then release cells
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    f_EUTRA_ReleaseAllCells( );
  }
  
  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.3
   * @status
   */
  function f_TC_6_2_1_3_EUTRA ( ) runs on EUTRA_PTC {
  
    var NAS_MSG_Indication_Type v_NAS_Ind;
    var SRB_COMMON_IND v_ReceivedAsp;
    var template (value) CellPowerList_Type v_CellPowerList_AtT0;
    var boolean v_TriggerRxd := false;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
    var template (present) SecurityHeaderType v_AttReqSecurityHeader := (tsc_SHT_NoSecurityProtection, tsc_SHT_IntegrityProtected);
    var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
    var B3_Type v_PDNType := f_GetPdnType();
    
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer;
    
    v_CellPowerList_AtT0 := {
      cs_CellPower ( eutra_Cell1, tsc_ServingCellRS_EPRE ),
      cs_CellPower ( eutra_Cell12, tsc_ServingCellRS_EPRE )
    };
    
    //Initialise all cell, security and mobile parameters
    f_EUTRA_Init (c4);//SIB2, SIB3 & SIB6 are transmitted
    
    //Set TAC according to 36.523-1 Table 6.0.1-2
    //TAC for Cell 1 is set to 1 in f_EUTRA_Init ();
    f_EUTRA_CellInfo_SetTAC ( eutra_Cell12, tsc_IdleMode_TAC_Cell12 );
    
    //Set PLMN according to Table 6.2.1.2.3.2-1: Cell PLMN identities
    f_EUTRA_CellInfo_SetPLMN_2Entries ( eutra_Cell1, cs_HPLMN_002_81, cs_HPLMN_001_21 );
    f_EUTRA_CellInfo_SetPLMN_2Entries ( eutra_Cell12, cs_HPLMN_002_81, cs_HPLMN_002_91 );
    
    //Set Minimum UL flag for Cell 1
    f_EUTRA_SetCellConfigCapability ( eutra_Cell12, minimumUplinkCell );
    
    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );
    f_EUTRA_CellConfig_Def ( eutra_Cell12 );
    
    //Equip UE with a USIM containing default values except for those shown in Table 6.2.1.3.3.1-1
    f_UT_USIM_Insert ( UT, "36.523-1 Table 6.2.1.3.3.1-1" );

    //Set initial cell power level according to T0
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
    f_EUTRA_SetCellPowerList ( v_CellPowerList_AtT0, cs_TimingInfo_Now );
    
    //Wait for correct power levels and then start the test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    
    f_EUTRA_TestBody_Set ( true );
    
    //@siclog "Step 1" siclog@
    //Switch UE on
    f_UT_SwitchOnUE ( UT, false );
    t_IdleMode_GenericTimer.start;
    
    //@siclog "Step 2" siclog@
    //Wait for RRCConnectionRequest on cell 1
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1, cr_508_RRCConnectionRequest ) );
    t_IdleMode_GenericTimer.stop;
    //@verdict pass UE sends RRCConnectionRequest on cell 1
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.2 Step 2");

    //@siclog "Step 3" siclog@
    //Send RRC Connection Setup
    f_EUTRA_RRC_ConnectionSetup_Def (eutra_Cell1, cs_TimingInfo_Now, v_RRC_TI );
    
    //@siclog "Step 4" siclog@
    //Receive RRCConnectionSetupComplete with ATTACH REQUEST and piggybacked PDN CONNECTIVITY REQUEST.
    //selectedPLMN-Identity in RRCConnectionSetupComplete shall indicate 2 (PLMN2)
    SRB.receive(car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                         cdr_RRCConnectionSetupComplete_2ndSelPlmnId ( v_RRC_TI, ? ),
                                         cr_NAS_IndicationWithPiggybacking(v_AttReqSecurityHeader,
                                                                           cr_508_ATTACH_REQUEST(v_AttachType,
                                                                                                 v_AdditionalUpdateType),
                                                                           cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach, v_PDNType))))
      -> value v_ReceivedAsp;
    v_NAS_Ind := v_ReceivedAsp.Signalling.Nas[0];

    //@siclog "Step 5 - 17" siclog@
    //Perform registration on cell 1
    f_EUTRA_IdleUpdated_Step5_17 ( eutra_Cell1, STATE2_IDLEUPDATE, NORMAL, v_NAS_Ind );
    
    //Since Authentication parameters have been updated. Send them to UTRAN PTC
    f_EUTRA_SendAuthParameters (eutra_Cell1, UTRAN);
    
    //@siclog "Step 18" siclog@
    //Check if PLMN2 is indicated by UE
    f_UT_PLMN_Check ( UT, "00211" );
    //* @verdict pass PLMN2 is indicated by UE
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.3 Step 18");
    
    //@siclog "Step 19" siclog@
    //Adjust cell power levels according to T1
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
    f_EUTRA_SetCellPower ( eutra_Cell1, tsc_NonSuitableOffCellRS_EPRE, cs_TimingInfo_Now );
    
    f_EUTRA_TestBody_Set ( true );
    
    //Wait till end of test case then release cells
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    f_EUTRA_ReleaseAllCells( );
  }
  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.4
   * @status
   */
  function f_TC_6_2_1_4_EUTRA ( ) runs on EUTRA_PTC {

    var NAS_MSG_Indication_Type v_NAS_Ind;
    var SRB_COMMON_IND v_ReceivedAsp;
    var boolean v_TriggerRxd := false;
    var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
    var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
    var B3_Type v_PDNType := f_GetPdnType();
    timer t_IdleMode_GenericTimer := tsc_IdleMode_GenericTimer;
    
    //Initialise all cell, security and mobile parameters
    f_EUTRA_Init (c5);//SIB2, SIB3 & SIB7 are transmitted
    
    //Set TAC according to 36.523-1 Table 6.0.1-2
    //TAC for Cell 1 is set to 1 in f_EUTRA_Init ();
    f_EUTRA_CellInfo_SetTAC ( eutra_Cell12, tsc_IdleMode_TAC_Cell12 );
    
    //Set PLMN according to Table 6.2.1.2.3.2-1: Cell PLMN identities
    f_EUTRA_CellInfo_SetPLMN_1Entry ( eutra_Cell12, cs_HPLMN_002_81 );
    
    //Set Minimum UL flag for Cell 1
    f_EUTRA_SetCellConfigCapability ( eutra_Cell12, minimumUplinkCell );
    
    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );
    f_EUTRA_CellConfig_Def ( eutra_Cell12 );
    
    //Equip UE with a USIM containing default values except for those shown in Table 6.2.1.4.3.1-1
    f_UT_USIM_Insert ( UT, "36.523-1 Table 6.2.1.4.3.1-1" );

    //Bring UE to initial state
    //Preamble to ensure the UE is attached to PLMN1
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );
    f_UT_ManualPLMN_Set ( UT );
    f_EUTRA_UE_Detach_SwitchOffUe ( eutra_Cell1, RRC_IDLE );

    //Set initial cell parameters according to T0
    f_EUTRA_CellInfo_SetPLMN_1Entry ( eutra_Cell1, cs_HPLMN_002_11 );
    f_EUTRA_ModifySysinfo_ValueTag_NoPaging ( eutra_Cell1 );
    f_SendIRATCoOrd (GERAN, cms_IRAT_Trigger);
    f_EUTRA_SetCellPower ( eutra_Cell12, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now );
    
    //Wait for correct power levels and then start the test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    
    f_EUTRA_TestBody_Set ( true );
    
    //@siclog "Step 1" siclog@
    //Switch UE on
    f_UT_SwitchOnUE ( UT, false );
    
    //@siclog "Step 2-3" siclog@
    //Display a list of available PLMNs & Select PLMN2
    f_UT_DisplayManualPLMN_AndSelect (UT, "00211");
    t_IdleMode_GenericTimer.start;
    
    //@siclog "Step 4" siclog@
    //Receive RRC Connection Request message
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1, cr_508_RRCConnectionRequest ) );
    t_IdleMode_GenericTimer.stop;
    //@verdict pass UE sends RRCConnectionRequest on cell 1
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.4 Step 4");

    //@siclog "Step 5 - 19" siclog@
    //Perform registration on cell 1
    f_EUTRA_RRC_ConnectionSetup_Def (eutra_Cell1, cs_TimingInfo_Now, tsc_RRC_TI_Def );
    
    SRB.receive(car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                         cr_508_RRCConnectionSetupComplete ( tsc_RRC_TI_Def, ? ),
                                         cr_NAS_IndicationWithPiggybacking(tsc_SHT_IntegrityProtected,
                                                                           cr_508_ATTACH_REQUEST(v_AttachType,
                                                                                                 v_AdditionalUpdateType),
                                                                           cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach, v_PDNType))))
      -> value v_ReceivedAsp;
    v_NAS_Ind := v_ReceivedAsp.Signalling.Nas[0];
    f_EUTRA_IdleUpdated_Step5_17 ( eutra_Cell1, STATE2_IDLEUPDATE, NORMAL, v_NAS_Ind );
    
    //@siclog "Step 20" siclog@
    //Check if PLMN2 is indicated by UE
    f_UT_PLMN_Check ( UT, "00211" );
    //* @verdict pass PLMN2 is indicated by UE
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.4 Step 20");

    f_EUTRA_TestBody_Set ( true );

    //Tell GERAN PTC that test case is over and perform postamble
    f_SendIRATCoOrd (GERAN, cms_IRAT_Trigger);
    f_EUTRA_Postamble(eutra_Cell1, E1_IDLE);
  }
}
