/******************************************************************************/
// @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 RRC_Paging
{

//=========================================================================
// Import Part
//=========================================================================

  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_CellInfo all;
  import from EUTRA_Component all;
  import from EUTRA_CommonDefs all;
  import from EUTRA_Paging all;
  import from EUTRA_RRCSteps all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_CommonProcedures all;
  import from EUTRA_AuxiliaryFunctions all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_RRC_Templates all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_SecurityFunctions all;
  import from EUTRA_SecuritySteps all;
  import from EUTRA_ASP_TypeDefs all;
  import from EUTRA_ASP_SrbDefs all;
  import from EUTRA_Timing all;
  import from EUTRA_CellCfg_Templates all;
  import from EUTRA_IdleMode all;
  import from EUTRA_NASCommonTemplates all;
  
  import from CommonDefs all;
  import from Parameters all;

  import from EPS_NAS_Templates all;
  import from EPS_NAS_Constants all;
  import from EPS_NAS_MsgContainers all;

  import from L2_CommonFunctions all;
  
//=================================================================================================================
// Templates
//=================================================================================================================
  
  template (value) PagingRecordList cs_NasPaging_ThreeRecords ( template (value) PagingUE_Identity p_PagingUE_Identity1,
                                                                template (value) PagingUE_Identity p_PagingUE_Identity2,
                                                                template (value) PagingUE_Identity p_PagingUE_Identity3 ) :=
  {
    {
      ue_Identity := p_PagingUE_Identity1,
      cn_Domain := ps
    },
    {
      ue_Identity := p_PagingUE_Identity2,
      cn_Domain := ps
    },
    {
      ue_Identity := p_PagingUE_Identity3,
      cn_Domain := ps
    }
  };

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

  /*
   * @desc      Creates a new IMSI based on the default IMSI.
   *            The IMSI is a hexstring given as sequence of digits of type Integer with a valid digit range from 0 to 9.
   *            The function increases the digit at index p_Index with 1. If this digit is 9 then it is set to 0.
   * @param     p_Imsi        Current IMSI value expressed as hexstring
   * @param     p_Index       (Default value: 0)
   * @return    hexstring     New IMSI expressed as hexstring
   * @status
   */
  function f_CreateNewImsi( integer p_Index := 0 ) return hexstring
  {
    //Get length of IMSI hexstring
    var hexstring v_NewImsi := px_IMSI_Def;
    var integer v_Length := lengthof(v_NewImsi);

    //If digit to be changed is out of range...
    if (v_Length > p_Index){
      p_Index := v_Length + 1;//...then change last digit of string
    }

    //Produce the new IMSI by increases the digit at index p_Index with 1.
    v_NewImsi[p_Index] := int2hex((hex2int(v_NewImsi[p_Index])+1) mod 10, 1);

    return v_NewImsi;
  }

  /*
   * @desc      Test case 8.1.1.1 RRC / Paging for Connection in idle mode. UE identified with S-TMSI
   * @status    APPROVED
   */
  function f_TC_8_1_1_1_EUTRA ( ) runs on EUTRA_PTC
  {
    //Time in which UE should not respond to paging
    var float v_NoReactionTime := 5.0;
    var GutiParameters_Type v_GutiParameters;
    var S_TMSI v_S_Tmsi2;
    var SRB_COMMON_IND v_ReceivedAsp; //@sic R5-102180 sic@
    var NAS_MSG_Indication_Type v_NAS_Ind; //@sic R5-102180 sic@
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;

    f_EUTRA_Init ( c1 );

    //Get cell specific parameters
    v_GutiParameters := f_EUTRA_CellInfo_GetGuti ( eutra_Cell1 );
    v_S_Tmsi2 := f_CreateNewS_Tmsi({v_GutiParameters.MME_Code, v_GutiParameters.M_TMSI});

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Bring UE to initial state
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1-2" siclog@
    //Page UE with unmatched identity and expect no response
    f_EUTRA_UE_Page_NoReaction ( eutra_Cell1,
                                 cs_NasPaging_OneRecord ( cs_PagingUE_Identity_S_TMSI ( v_S_Tmsi2.mmec, v_S_Tmsi2.m_TMSI ) ),
                                 v_NoReactionTime );

    //@siclog "Step 3" siclog@
    //Page UE with matched identity
    f_EUTRA_UE_Page_Def ( eutra_Cell1 );

    //@siclog "Step 4" siclog@
    //Receive RRCConnectionRequest
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1,
                                       cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) );
    //* @verdict pass RRCConnectionRequest received
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.1.1.1 Step 4");

    //@siclog "Step 5" siclog@
    //Transmit RRCConnectionSetup
    f_EUTRA_RRC_ConnectionSetup_Def ( eutra_Cell1, cs_TimingInfo_Now, v_RRC_TI );

    //@siclog "Step 6" siclog@
    //Receive RRCConnectionSetupComplete
    SRB.receive(car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                         cr_508_RRCConnectionSetupComplete ( v_RRC_TI, ? ),
                                         cr_NAS_Indication ( tsc_SHT_IntegrityProtected,
                                                             cr_508_SERVICE_REQUEST ( f_EUTRA_SecurityKSIasme_Get (  ) ) ) ) )
    -> value v_ReceivedAsp; //@sic R5-102180 sic@
    
    //* @verdict pass RRCConnectionSetupComplete received
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.1.1.1 Step 6");

    //@siclog "Step 6A-6D" siclog@
    //Steps 6 to 9 of the generic radio bearer establishment procedure (TS 36.508 4.5.3.3-1) are executed to successfully complete the service request procedure.
    v_NAS_Ind := v_ReceivedAsp.Signalling.Nas[0]; //@sic R5-102180 sic@
    f_EUTRA_ServiceReqPostamble_508Steps6_9( eutra_Cell1, v_NAS_Ind ); //@sic R5-102180 sic@
    
    //@siclog "Step 7" siclog@
    //Check RRC_Connected state
    f_EUTRA_508Check_ConnectedState ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( false );

    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell1, E2_CONNECTED ); // @sic R5-103847 sic@
  }
  
  /*
   * @desc      Test case 8.1.1.2 RRC / Paging for notification of BCCH modification in idle mode
   * @status
   */
  function f_TC_8_1_1_2_EUTRA ( ) runs on EUTRA_PTC
  {
    var SRB_COMMON_IND v_ReceivedAsp;
    var EUTRA_SecurityParams_Type v_AuthParams;
    var O4_Type v_NasCountUL;
    var template (value) EUTRA_FDD_TDD_CellInfo_Type v_EUTRA_FDD_TDD_Info;
    var EUTRA_FDD_TDD_Mode_Type v_EUTRA_FDD_TDD_Mode ;
    var EUTRA_CellSysInfo_Type v_SysInfo_Cell1;
    var PRACH_ConfigSIB v_PRACH_ConfigSIB;
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var template (present) integer v_SubFrameNum := 0;
    var ReleaseCause v_RRC_ReleaseCause := other;
    var template (omit) SRB_COMMON_IND v_SrbIndByRef := omit;    // not used (needed when other information of the RRC message needs to be access)
    var template (omit) SYSTEM_IND v_SysIndByRef := omit;        // to store SYSTEM_IND if it takes over the RRC message

    f_EUTRA_Init ( c1 );
    
    v_AntennaInfo :=  f_EUTRA_CellInfo_GetAntennaInfoDedicated ( eutra_Cell1 );

    //Get FDD or TDD mode from Cell-configuration
    v_EUTRA_FDD_TDD_Info := f_EUTRA_CellInfo_GetFDD_TDD_Info ( eutra_Cell1 );
    v_EUTRA_FDD_TDD_Mode := f_EUTRA_CellInfo_GetFDD_TDD ( eutra_Cell1 );

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Bring UE to initial state
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );
    v_AuthParams := f_EUTRA_Security_Get();

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Page UE with matched identity.
    f_EUTRA_UE_Page_Def ( eutra_Cell1 );

    //@siclog "Step 2" siclog@
    //Receive RRCConnectionRequest.
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1,
                                       cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) );
    //* @verdict pass RRCConnectionRequest received
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.1.1.2 Step 2");

    //@siclog "Step 3" siclog@
    //The SS transmits an RRCConnectionSetup message on Cell 1.
    f_EUTRA_RRC_ConnectionSetup_Def ( eutra_Cell1,
                                      cs_TimingInfo_Now,
                                      v_RRC_TI );

    //@siclog "Step 3A" siclog@
    //The UE transmits an RRCConnectionSetupComplete message on Cell 1. This message includes a SERVICE REQUEST message.
    SRB.receive ( car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                           cr_508_RRCConnectionSetupComplete ( v_RRC_TI, ? ),
                                           cr_NAS_Indication ( tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST ( f_EUTRA_SecurityKSIasme_Get ( ) ) ) ) ) -> value v_ReceivedAsp;

    v_NasCountUL := v_ReceivedAsp.Signalling.Nas[0].SecurityProtection.NasCount;

    //@siclog "Step 3B-3C" siclog@
    //The SS transmits a SecurityModeCommand message.
    //The UE transmits a SecurityModeComplete message.
    v_AuthParams := f_EUTRA_RRC_ActivateSecurity ( eutra_Cell1, v_AuthParams, v_NasCountUL );
    f_EUTRA_Security_Set ( v_AuthParams );

    //@siclog "Step 3D" siclog@
    //The SS transmits an RRCConnectionReconfiguration message on Cell 1.
    SRB.send ( cas_SRB1_RrcPdu_REQ ( eutra_Cell1,
                                     cs_TimingInfo_Now,
                                     cs_RRCConnectionReconfiguration_SRB2_1AM_DRB_Est ( v_RRC_TI,
                                                                                        v_EUTRA_FDD_TDD_Info.cqi_ReportConfig,
                                                                                        v_EUTRA_FDD_TDD_Info.soundingRS_UL_Config,
                                                                                        v_AntennaInfo
                                                                                        ) ) );

    //@siclog "Step 3E" siclog@
    //The UE transmits an RRCConnectionReconfigurationComplete message on Cell 1.
    SRB.receive ( car_SRB1_RrcPdu_IND ( eutra_Cell1, cr_508_RRCConnectionReconfigurationComplete ( v_RRC_TI ) ) );

    //@siclog "Step 3F" siclog@
    //The SS transmits an RRCConnectionRelease message on Cell 1.
    f_EUTRA_RRC_ConnectionRelease ( eutra_Cell1, v_RRC_TI, v_RRC_ReleaseCause );

    //@siclog "Step 3G" siclog@
    //Wait for 5 s for the UE to enter E-UTRA RRC_IDLE state.
    f_Delay(5.0);

    //Configure SS to enable report of PRACH preamble reception
    f_EUTRA_SS_ConfigRachPreambleIndMode ( eutra_Cell1, tsc_L1Mac_IndicationMode_Enable );

    //@siclog "Step 4" siclog@
    //Change the prach config index in system information.
    if ( v_EUTRA_FDD_TDD_Mode == FDD )
      {
        f_EUTRA_CellInfo_SetPRACH_ConfigurationIndex ( eutra_Cell1, 5 );
        v_SubFrameNum := 7; //According to 36.211
      }
    else if ( v_EUTRA_FDD_TDD_Mode == TDD )
      {
        f_EUTRA_CellInfo_SetPRACH_ConfigurationIndex ( eutra_Cell1, 51 ); // TBD: FFS according to 36.523-1
        v_SubFrameNum := 7; // TBD
      }

    //@siclog "Step 5" siclog@
    //SS transmits a Paging message including a systemInfoModification.
    f_EUTRA_ModifySysinfo ( eutra_Cell1 );

    //Configure PRACH at SS
    v_SysInfo_Cell1 := f_EUTRA_CellInfo_GetSYSINFO ( eutra_Cell1 );
    v_PRACH_ConfigSIB := v_SysInfo_Cell1.BCCH_Info.SIs[0].message_.c1.systemInformation.criticalExtensions.systemInformation_r8.sib_TypeAndInfo[0].sib2.radioResourceConfigCommon.prach_Config;
    f_EUTRA_SS_CommonCellConfig ( eutra_Cell1, cas_CellConfig_UpdatePRACH_Info_REQ( eutra_Cell1,
                                                                                    cs_TimingInfo_Now,
                                                                                    v_PRACH_ConfigSIB ) );

    //@siclog "Step 6" siclog@
    //Wait for 15s for the UE to receive system information.
    f_Delay( 15.0 );

    //@siclog "Step 7" siclog@
    //Page UE with matched identity.
    f_EUTRA_UE_Page_Def ( eutra_Cell1 );
    
    //@siclog "Step 7A" siclog@
    //Receive Receive dedicated preamble
    f_EUTRA_RacingCond_AwaitSysIndBeforeSrbMsg ( car_PRACH_Preamble_IND_Any ( eutra_Cell1, cr_TimingInfo ( ?, v_SubFrameNum )),
                                                 v_SrbIndByRef,
                                                 v_SysIndByRef);
    //Random Access Response automatically taken care of due to prach config
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test Case 8.1.1.2 Step 7A");
    
    //@siclog "Step 8" siclog@
    //Receive RRCConnectionRequest.
    f_EUTRA_RacingCond_MatchRrcMsg ( v_SrbIndByRef,
                                     car_SRB0_RrcPdu_IND (eutra_Cell1,
                                                          cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access )));
                                                          
    //Configure SS to disable report of PRACH preamble reception
    f_EUTRA_SS_ConfigRachPreambleIndMode ( eutra_Cell1, tsc_L1Mac_IndicationMode_Disable );

    //@siclog "Step 9" siclog@
    //The SS transmits an RRCConnectionSetup message on Cell 1.
    f_EUTRA_RRC_ConnectionSetup_Def ( eutra_Cell1,
                                      cs_TimingInfo_Now,
                                      v_RRC_TI );

    //@siclog "Step 9A" siclog@
    //The UE transmits an RRCConnectionSetupComplete message on Cell 1. This message includes a SERVICE REQUEST message.
    SRB.receive ( car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                           cr_508_RRCConnectionSetupComplete ( v_RRC_TI, ? ),
                                           cr_NAS_Indication ( tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST ( f_EUTRA_SecurityKSIasme_Get ( ) ) ) ) ) -> value v_ReceivedAsp;

    v_NasCountUL := v_ReceivedAsp.Signalling.Nas[0].SecurityProtection.NasCount;

    //@siclog "Step 9B-9C" siclog@
    //The SS transmits a SecurityModeCommand message on Cell 1.
    //The UE transmits a SecurityModeComplete message on Cell 1.
    v_AuthParams := f_EUTRA_RRC_ActivateSecurity ( eutra_Cell1, v_AuthParams, v_NasCountUL );
    f_EUTRA_Security_Set ( v_AuthParams );

    //@siclog "Step 9D" siclog@
    //The SS transmits an RRCConnectionReconfiguration message on Cell 1.
    SRB.send ( cas_SRB1_RrcPdu_REQ ( eutra_Cell1,
                                     cs_TimingInfo_Now,
                                     cs_RRCConnectionReconfiguration_SRB2_1AM_DRB_Est ( v_RRC_TI,
                                                                                        v_EUTRA_FDD_TDD_Info.cqi_ReportConfig,
                                                                                        v_EUTRA_FDD_TDD_Info.soundingRS_UL_Config,
                                                                                        v_AntennaInfo ) ) );

    //@siclog "Step 9E" siclog@
    //The UE transmits an RRCConnectionReconfigurationComplete message on Cell 1.
    SRB.receive ( car_SRB1_RrcPdu_IND ( eutra_Cell1, cr_508_RRCConnectionReconfigurationComplete ( v_RRC_TI ) ) );

    //@siclog "Step 9F" siclog@
    //The SS transmits an RRCConnectionRelease message on Cell 1.
    f_EUTRA_RRC_ConnectionRelease ( eutra_Cell1, v_RRC_TI, v_RRC_ReleaseCause );

    //@siclog "Step 9G" siclog@
    //Wait for 5 s for the UE to enter E-UTRA RRC_IDLE state.
    f_Delay ( 5.0 );

    //@siclog "Step 10" siclog@
    //Check: Does the test result of generic test procedure in TS 36.508 subclause 6.4.2.2 indicate that the UE is in E-UTRA RRC_IDLE state on Cell 1?
    f_EUTRA_508Check_IdleState ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( false );

    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell1, E1_IDLE );
  }
  
  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.1.1.3
   * @status
   */
  function f_TC_8_1_1_3_EUTRA ( ) runs on EUTRA_PTC
  {
    var NAS_MSG_Indication_Type v_NAS_Ind;
    var SRB_COMMON_IND v_ReceivedAsp;
    var float v_NoReactionTime := 10.0;
    var hexstring v_ImsiDiff1 := f_CreateNewImsi();
    var hexstring v_ImsiDiff2 := f_CreateNewImsi(1);
    var hexstring v_ImsiDiff3 := f_CreateNewImsi(5);

    f_EUTRA_Init ( c1 );
    
    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Bring UE to initial state
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1-2" siclog@
    //Page UE with unmatched identities and expect no response
    //Check: Does the UE transmit an RRCConnectionRequest message within 10s?
    f_EUTRA_UE_Page_NoReaction ( eutra_Cell1,
                                 cs_NasPaging_ThreeRecords(cs_PagingUE_Identity_IMSI(f_ConvertImsi2ASN(v_ImsiDiff1)),
                                                           cs_PagingUE_Identity_IMSI(f_ConvertImsi2ASN(v_ImsiDiff2)),
                                                           cs_PagingUE_Identity_IMSI(f_ConvertImsi2ASN(v_ImsiDiff3))),
                                 v_NoReactionTime );

    //@siclog "Step 3" siclog@
    //Page UE with matched identity
    f_EUTRA_UE_Page ( eutra_Cell1,
                      cs_NasPaging_ThreeRecords(cs_PagingUE_Identity_IMSI(f_ConvertImsi2ASN(v_ImsiDiff1)),
                                                cs_PagingUE_Identity_IMSI(f_ConvertImsi2ASN(v_ImsiDiff2)),
                                                cs_PagingUE_Identity_IMSI(f_ConvertImsi2ASN(px_IMSI_Def))));

    //@siclog "Step 4" siclog@
    //Receive RRCConnectionRequest
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1,
                                       cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) );
    //* @verdict pass RRCConnectionRequest received
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.1.1.3 Step 4");

    //@siclog "Step 5" siclog@
    //Transmit RRCConnectionSetup
    f_EUTRA_RRC_ConnectionSetup_Def ( eutra_Cell1, cs_TimingInfo_Now, tsc_RRC_TI_Def );

    //@siclog "Step 6" siclog@
    //Receive RRCConnectionSetupComplete
    SRB.receive(car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                         cr_508_RRCConnectionSetupComplete ( tsc_RRC_TI_Def, ? ),
                                         cr_NAS_Indication ( tsc_SHT_IntegrityProtected,
                                                             cr_508_SERVICE_REQUEST ( f_EUTRA_SecurityKSIasme_Get (  ) ) ) ) )
    -> value v_ReceivedAsp;

    //@siclog "Step 6A-6D" siclog@
    //Steps 6 to 9 of the generic radio bearer establishment procedure (TS 36.508 4.5.3.3-1) are executed to successfully complete the service request procedure
    v_NAS_Ind := v_ReceivedAsp.Signalling.Nas[0];
    f_EUTRA_ServiceReqPostamble_508Steps6_9( eutra_Cell1, v_NAS_Ind );
    
    //@siclog "Step 7" siclog@
    //Check RRC_Connected state
    f_EUTRA_508Check_ConnectedState ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( false );

    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell1, E2_CONNECTED );
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.1.1.4
   * @status
   */
  function f_TC_8_1_1_4_EUTRA ( ) runs on EUTRA_PTC
  {
  
    /* OPEN ISSUE
     * No PLMN identity is specified in specific message contents
    */
  
    var SRB_COMMON_IND v_ReceivedAsp;
    var NAS_MSG_Indication_Type v_NAS_Ind;
    f_EUTRA_Init ( c1 );
    
    //Update SIB1 according to specific message contents
    f_EUTRA_CellInfo_SetPLMN_2Entries ( eutra_Cell1, cs_HPLMN_001_02, cs_HPLMN_Def );
    //FFS - PLMN randomly picked since prose is still FFS
    
    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Bring UE to initial state
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Page UE with matched identity
    f_EUTRA_UE_Page_Def ( eutra_Cell1 );

    //@siclog "Step 2" siclog@
    //Receive RRCConnectionRequest
    SRB.receive( car_SRB0_RrcPdu_IND ( eutra_Cell1,
                                       cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) );
    //* @verdict pass RRCConnectionRequest received
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.1.1.4 Step 2");

    //@siclog "Step 3" siclog@
    //Transmit RRCConnectionSetup
    f_EUTRA_RRC_ConnectionSetup_Def ( eutra_Cell1, cs_TimingInfo_Now, tsc_RRC_TI_Def );

    //@siclog "Step 4" siclog@
    //Receive RRCConnectionSetupComplete
    SRB.receive(car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                         cdr_RRCConnectionSetupComplete_2ndSelPlmnId ( tsc_RRC_TI_Def, ? ),
                                         cr_NAS_Indication ( tsc_SHT_IntegrityProtected,
                                                             cr_508_SERVICE_REQUEST ( f_EUTRA_SecurityKSIasme_Get ( )))))
    -> value v_ReceivedAsp;

    //@siclog "Step 4A-4D" siclog@
    //Steps 6 to 9 of the generic radio bearer establishment procedure (TS 36.508 4.5.3.3-1) are executed to successfully complete the service request procedure
    v_NAS_Ind := v_ReceivedAsp.Signalling.Nas[0];
    f_EUTRA_ServiceReqPostamble_508Steps6_9( eutra_Cell1, v_NAS_Ind );
    
    //@siclog "Step 5" siclog@
    //Check RRC_Connected state
    f_EUTRA_508Check_ConnectedState ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( false );

    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell1, E2_CONNECTED );
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.1.1.6
   * @status
   */
  function f_TC_8_1_1_6_EUTRA ( ) runs on EUTRA_PTC {

    var EUTRA_FDD_TDD_Mode_Type v_EUTRA_FDD_TDD_Mode ;
    var PRACH_ConfigSIB v_PRACH_ConfigSIB;
    var template (present) integer v_SubFrameNum :=0;
    var SubFrameTiming_Type v_Timing;
    var SystemFrameNumber_Type v_StartNextModPeriod;
    var template (value) AllSiSchedul_Type v_SiSchedul;
    var EUTRA_CellSysInfo_Type v_Sysinfo;
    var integer v_ModificationPeriodFrames;

    f_EUTRA_Init ( c1 );

    //Get FDD/TDD mode from Cell-configuration
    v_EUTRA_FDD_TDD_Mode := f_EUTRA_CellInfo_GetFDD_TDD ( eutra_Cell1 );

    //Calculate length of System Information Modification Period
    v_ModificationPeriodFrames := f_CalculateModificationPeriod ( eutra_Cell1 );

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Bring UE to initial state
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );
    f_EUTRA_RbEst_Def_DisableBSR_PHR ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( true );

    //Change the prach config index in system information according to Table 8.1.1.6.3.3-3
    if ( v_EUTRA_FDD_TDD_Mode == FDD )
    {
      f_EUTRA_CellInfo_SetPRACH_ConfigurationIndex(eutra_Cell1, 5);
      v_SubFrameNum := 7; // According to 36.211 Table 5.7.1-2
    }
    else
    {
      f_EUTRA_CellInfo_SetPRACH_ConfigurationIndex(eutra_Cell1, 53);
      v_SubFrameNum := (1,6);
      /* FFS - Clarification in prose is needed for TDD:
         36.523-1 cl. 8.1.1.6.3.2 step 4 says: i.e. in subframe number 7 and not 1
         but according to 36.211 Table Table 5.7.1-4 UE will/can use subframe 1 or 6 (UpPTS  apply)
         1. PRACH configuration Index 53, preamble format is 4 & UL/DL configuration is 1 (default UL/DL configuration).
         2. The quadruple equals (according to 36.211 Table 5.7.1-4.)
            (0,0,0,*)
            (0,0,1,*)
         3. For the first quadruple
            t0ra is 0 which indicates that the resource is reoccuring in all radio frames, i.e. 2, 3, 7, 8
            t1ra is 0 which indicates that the resource is in the first half of the frame, i.e. 2, 3 remain
            t2ra is * which indicates that UpPTS apply.
            subframe 1 is selected since UpPTS applies!
         4. For the second quadruple
            t0ra is 0 which indicates that the resource is reoccuring in all radio frames, i.e. 2, 3, 7, 8
            t1ra is 1 which indicates that the resource is located in the second half of the frame, i.e. 7,8 remain.
            t2ra is * which indicates that UpPTS apply.
            subframe 6 is selected since UpPTS applies!
      */
    }

    //Get the newly changed Prach Config in SIB2 to later be able to send it to SS
    v_Sysinfo := f_EUTRA_CellInfo_GetSYSINFO ( eutra_Cell1 );
    v_PRACH_ConfigSIB := v_Sysinfo.BCCH_Info.SIs[0].message_.c1.systemInformation.criticalExtensions.systemInformation_r8.sib_TypeAndInfo[0].sib2.radioResourceConfigCommon.prach_Config;

    f_EUTRA_SetSB1_ValueTag ( eutra_Cell1, true );
    v_Sysinfo.BCCH_Info.SIB1.message_.c1.systemInformationBlockType1.systemInfoValueTag :=
        (v_Sysinfo.BCCH_Info.SIB1.message_.c1.systemInformationBlockType1.systemInfoValueTag + 1) mod 32;

    f_EUTRA_SetSysinfo (eutra_Cell1,v_Sysinfo);    // Save SYSINFO with the new valueTag in the record
    f_EUTRA_SetSB1_ValueTag (eutra_Cell1, false);  // Set the flag to FALSE

    //Get the current SFN
    v_Timing := f_EUTRA_GetCurrentTiming ( eutra_Cell1 );

    //Calculate the next Modification Period
    v_StartNextModPeriod := ((v_Timing.SFN.Number + tsc_SfnDelay) + (v_ModificationPeriodFrames -((v_Timing.SFN.Number + tsc_SfnDelay) mod v_ModificationPeriodFrames)))mod 1024;

    //@siclog "Step 1" siclog@
    //The SS transmits a Paging message including a systemInfoModification
    //Page UE from the start of the next modification period
    f_EUTRA_UE_PageSysinfoMod ( eutra_Cell1, v_StartNextModPeriod );

    //Calculate the second next modification period
    v_StartNextModPeriod := ( v_StartNextModPeriod + v_ModificationPeriodFrames ) mod 1024;

    //Extract the SI scheduling from BCCH info
    v_SiSchedul := f_EUTRA_SS_SetupSchedulingInfo(v_Sysinfo.BCCH_Info.SIB1.message_.c1.systemInformationBlockType1, v_EUTRA_FDD_TDD_Mode);

    //@siclog "Step 2" siclog@
    //From the begining of the next modification period, the SS transmits a modified SystemInformationBlockType2 as specified
    SYS.send (cas_ModifySysinfo_Def_REQ( eutra_Cell1,
                                         cs_TimingInfo_SFN(v_StartNextModPeriod),
                                         v_SiSchedul,         /* scheduling of SIs */
                                         v_Sysinfo.BCCH_Info, /* system information to be scheduled */
                                         f_EUTRA_CellInfo_GetDL_ChBandwidth ( eutra_Cell1 ) ) );

    //Configure SS to enable report of PRACH preamble reception
    f_EUTRA_SS_ConfigRachPreambleIndMode ( eutra_Cell1, tsc_L1Mac_IndicationMode_Enable );

    //Configure SS with the changed PRACHConfigIndex
    f_EUTRA_SS_CommonCellConfig  ( eutra_Cell1,
                                  cas_CellConfig_UpdatePRACH_Info_REQ ( eutra_Cell1,
                                                                        cs_TimingInfo_Now,
                                                                        v_PRACH_ConfigSIB ) );

    //Configure SS to send the appropriate RA response
    f_EUTRA_SS_ConfigRachProcedure ( eutra_Cell1,
                                     omit,
                                     cs_RachProcedureConfig_CRNTI_Dedicated ( f_EUTRA_CellInfo_GetRAR_TA ( eutra_Cell1 ),
                                                                              f_EUTRA_CellInfo_GetDL_ChBandwidth ( eutra_Cell1 )));

    // Calculate the third next modification period used to send the PDCCH order
    v_StartNextModPeriod := (v_StartNextModPeriod + v_ModificationPeriodFrames) mod 1024;

    //@siclog "Step 3" siclog@
    //During the first on duration period after the end of the modification period where SystemInformationBlockType2 was modified (see step 2), the SS transmits a PDCCH order including a dedicated preamble.
    //* cs_RachProcedureConfig_CRNTI_Dedicated and cs_RA_PDCCH_Order are using the same parameters (52, 0).
    f_Send_PDCCH_Order ( eutra_Cell1,
                         cs_TimingInfo_SFN ( v_StartNextModPeriod ),
                         cs_RA_PDCCH_Order );

    //@siclog "Step 4" siclog@
    //Check: Does the UE initiate a random access procedure using prach-ConfigIndex given in step 2?
    SYSIND.receive ( car_PRACH_Preamble_IND_Any (eutra_Cell1, cr_TimingInfo (?, v_SubFrameNum)));
    //* @verdict pass UE initiated a ranom access procedure using prach-configuration in step 2
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test Case 8.1.1.6 Step 4");

    //Configure SS to disable report of PRACH preamble reception
    f_EUTRA_SS_ConfigRachPreambleIndMode (eutra_Cell1, tsc_L1Mac_IndicationMode_Disable);

    //@siclog "Step 5" siclog@
    //The SS transmits Random Access Response with RAPID corresponding to preamble in step4.
    //--- This step is implicitly done by SS according the configuration done previuosly

    f_EUTRA_TestBody_Set ( false );

    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell1, E1_IDLE );
  }
}
