/******************************************************************************/
// @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-17 00:56:42 +0200 (Fri, 17 Sep 2010) $
// $Rev: 3946 $
/******************************************************************************/

module EUTRA_RRCSteps {
  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from CommonDefs all;
  import from EUTRA_CommonDefs all;
  import from EUTRA_ASP_SrbDefs all;
  import from EUTRA_ASP_TypeDefs all;
  import from EPS_NAS_MsgContainers all;
  import from EUTRA_Component all;
  import from EUTRA_CellInfo all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_SecuritySteps all;
  import from EUTRA_RRC_Templates all;
  import from EUTRA_CellCfg_Templates all;
  import from Parameters all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_Paging all;
  import from EUTRA_Timing all;
  import from EUTRA_DRB_Templates all;
  import from EUTRA_SecurityFunctions all;

  //----------------------------------------------------------------------------
  /*
   * @desc      send RRC connection setup
   * @param     p_CellId
   * @param     p_TimingInfo
   * @param     p_RRC_TI
   * @status    APPROVED
   */
  function f_EUTRA_RRC_ConnectionSetup_Def (CellId_Type p_CellId,
                                            template (value) TimingInfo_Type p_TimingInfo,
                                            RRC_TransactionIdentifier p_RRC_TI) runs on EUTRA_PTC
  {
    // Get FDD or TDD mode from Cell-configuration:
    var template (value) EUTRA_FDD_TDD_CellInfo_Type  v_EUTRA_FDD_TDD_Info := f_EUTRA_CellInfo_GetFDD_TDD_Info(p_CellId);
    var SR_PUCCH_ResourceIndex_Type v_Sr_PUCCH_ResourceIndex := f_EUTRA_CellInfo_GetSR_PUCCH_ResourceIndex ( p_CellId );
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (p_CellId);
    var SR_ConfigIndex_Type v_SR_ConfigIndex := f_EUTRA_CellInfo_GetSR_ConfigIndex (p_CellId ); //@sic R5-102177 sic@

    // switch on UL grant assignments
    f_EUTRA_StartDefULGrantTransmission ( p_CellId, cs_TimingInfo_Now );

    SRB.send ( cas_SRB0_RrcPdu_REQ ( p_CellId,
                                     p_TimingInfo,
                                     cs_508_RRCConnectionSetup ( p_RRC_TI,
                                                                 v_EUTRA_FDD_TDD_Info.pucch_Config,
                                                                 v_Sr_PUCCH_ResourceIndex,
                                                                 v_AntennaInfo,
                                                                 v_SR_ConfigIndex  //@sic R5-102177 sic@
                                                                 ) ) );
  };

  //----------------------------------------------------------------------------
  /*
   * @desc      send RRC connection reconfiguration and receive RRC connection reconfiguration complete
   * @param     p_CellId
   * @param     p_RRC_TI
   * @param     p_NAS_MSG_Request
   * @status    APPROVED
   */
  function f_EUTRA_RRCConnectionReconfigurationWithPiggyNAS(CellId_Type p_CellId,
                                                            RRC_TransactionIdentifier p_RRC_TI,
                                                            template (value) NAS_MSG_Request_Type p_NAS_MSG_Request)
    runs on EUTRA_PTC
  {
    var template (value) EUTRA_FDD_TDD_CellInfo_Type v_EUTRA_FDD_TDD_CellInfo := f_EUTRA_CellInfo_GetFDD_TDD_Info ( p_CellId );
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (p_CellId);


    SRB.send(cas_SRB1_RrcNasPdu_REQ ( p_CellId,
                                      cs_TimingInfo_Now,
                                      cs_RRCConnectionReconfiguration_SRB2_1AM_DRB_Est(p_RRC_TI,
                                                                                       v_EUTRA_FDD_TDD_CellInfo.cqi_ReportConfig,
                                                                                       v_EUTRA_FDD_TDD_CellInfo.soundingRS_UL_Config,
                                                                                       v_AntennaInfo
                                                                                       ),
                                      p_NAS_MSG_Request));

    //Receive RRCConnectionReconfigurationComplete
    SRB.receive(car_SRB1_RrcPdu_IND(p_CellId, cr_508_RRCConnectionReconfigurationComplete(p_RRC_TI)));
  };

  /*
   * @desc      Send RRC PDU containing RadioResourceConfigDedicated to reconfigure the radio bearers
   *            and wait for confirmation by the UE
   * @param     p_CellId
   * @param     p_RRC_TI
   * @param     p_RadioResourceConfigDedicated
   * @status    APPROVED
   */
  function f_EUTRA_RRCConnectionReconfiguration_RadioResConfig(CellId_Type p_CellId,
                                                               RRC_TransactionIdentifier p_RRC_TI,
                                                               template (value) RadioResourceConfigDedicated  p_RadioResourceConfigDedicated)
    runs on EUTRA_PTC
  {

    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId,
                                     cs_TimingInfo_Now,
                                     cs_RRCConnectionReconfiguration_RadioResConfig ( p_RRC_TI,
                                                                                      p_RadioResourceConfigDedicated ) ) );

    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId,
                                        cr_508_RRCConnectionReconfigurationComplete ( p_RRC_TI ) ) );
  };

  function f_EUTRA_RRCConnectionReconfiguration_DRB_Release(CellId_Type p_CellId,
                                                            RRC_TransactionIdentifier p_RRC_TI,
                                                            DRB_Identity p_DRB_Id)
    runs on EUTRA_PTC
  { /* @desc
       Send RRC PDU containing RadioResourceConfigDedicated to release DRB and
       wait for confirmation by the UE */

    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId,
                                     cs_TimingInfo_Now,
                                     cs_RRCConnectionReconfiguration_DRB_Release ( tsc_RRC_TI_Def,
                                                                                   p_DRB_Id )));
    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId,
                                        cr_508_RRCConnectionReconfigurationComplete ( p_RRC_TI ) ) );
  };

  // ---------------------------------------------------------------------------------------------
  // ----------------------------FUNCTIONs for Handover during a test execution ------------------
  /*
   * @desc      configuration of ConfigRachProcedure in case of HO
   * @param     p_TargetCellId
   * @param     p_TimingInfo
   * @param     p_C_RNTI
   * @status    APPROVED
   */
  function f_EUTRA_SS_ConfigRachProcedure_HO(CellId_Type p_TargetCellId,
                                             template (value) TimingInfo_Type p_TimingInfo,
                                             C_RNTI p_C_RNTI) runs on EUTRA_PTC
  {
    var integer v_RA_GrantRIV := 0;   /* RB start = 0, Nprb = 1 */
    var integer v_RA_GrantIMCS := 7;  /* MCS as used in the Initial Grant: acc. with Nprb=1 this results in a TBS of 104
                                         NOTE: this maybe changed to 4 (=> 56 bits) */
    var ImcsValue_Type v_InitalGrantIMCS := 0;
    var integer        v_InitalGrantNprb := 4;       /* IMCS := 0, Nprb := 4 => grant of 88 bits (see 36.213 Table 7.1.7.2.1-1 */
    
    f_EUTRA_SS_ConfigRachProcedure ( p_TargetCellId,
                                     p_C_RNTI,
                                     cs_RachProcedureConfig_CRNTI(f_EUTRA_CellInfo_GetRAR_TA ( p_TargetCellId ),
                                                                  f_EUTRA_CellInfo_GetDL_ChBandwidth ( p_TargetCellId ), // @sic R5-103846 sic@
                                                                  v_RA_GrantRIV,                                         // @sic R5s100430  Change 2 sic@
                                                                  v_RA_GrantIMCS,                                        // @sic R5s100430  Change 2 sic@
                                                                  v_InitalGrantIMCS,                                     // @sic R5s100516  Change 4 sic@
                                                                  v_InitalGrantNprb),                                    // @sic R5s100516  Change 4 sic@
                                     p_TimingInfo);
  }

  /* @desc      Function for performing SS and UE configuration at IntraLTE IntraCell handover. The function will:
   *             1. configure C-RNTI based contention resolution in SS with minimal grant
   *                i.e. the UE will have to raise SR to send additional/user data.
   *             2. activate security at target cell according to 36.508 cl. 4.6.4 Table 4.6.4-1
   *                with keyChangeIndicator set to FALSE and nextHopChainingCount set to 0
   *             3. send the RRCConnectionReconfiguration to UE. This message will include
   *               3a. Measurement configuration if specified.
   *               3b. MobilityControlInfo according to 36.508 cl. 4.6.5 Table 4.6.5-1
   *                   with newUE-Identity set to tsc_C_RNTI_Def2 or p_C_R_NTI
   *               3c. RadioResourceConfigDedicated according to 36.508 cl. 4.6.3 Table 4.6.3-19
   *               3d. SecurityConfigHO according to 36.508 cl. 4.6.4 Table 4.6.4-1
   *                   with keyChangeIndicator set to FALSE and nextHopChainingCount set to 0
   *             4. Receive the RRCConnectionReconfigurationComplete message
   *             5. Reset and re-establish SRBs and DRB(s) at source cell.
   *             NOTE: Steps above may not happen in the listed order as it depends on whether target and source cell are the same or not.
   * @param     p_CellId
   * @param     p_TimingInfo      (default value: cs_TimingInfo_Now)
   * @param     p_DrbConfigList   (default value: cs_DrbConfigList_Def)
   * @param     p_C_RNTI          (default value: tsc_C_RNTI_Def2)
   * @param     p_MeasConfig      (default value: p_MeasConfig)
   * @status    APPROVED
   */
  function f_EUTRA_508RRC_IntraLTE_HO_IntraCell ( CellId_Type                           p_CellId,
                                                  template (value) TimingInfo_Type      p_TimingInfo    := cs_TimingInfo_Now,
                                                  template (value) RadioBearerList_Type p_DrbConfigList := cs_DrbConfigList_Def,
                                                  C_RNTI                                p_C_RNTI        := tsc_C_RNTI_Def2,
                                                  template (omit)  MeasConfig           p_MeasConfig    := omit)  runs on EUTRA_PTC
  { /* @sic R5s100491: enhancement to support MeasConfig sic@ */
    /* @sic R5s100524: common change of HO functions sic@ */
    var DRB_IdentityList_Type v_DRB_IdentityList;
    var integer k;
    var SubFrameTiming_Type v_Timing;
    var EUTRA_SecurityParams_Type v_Auth_Params := f_EUTRA_Security_Get();
    var NextHopChainingCount v_NextHopChainingCount := 0; //According to 36.508 cl. 4.6.1 RRCConnectionReestablishment
    var Frequency_IE_Type v_Frequency_IE := f_EUTRA_CellInfo_GetFrequencyIEs (p_CellId);
    var CarrierFreqEUTRA v_CarrierFreqEutra := v_Frequency_IE.UL_DL_Earfcn;
    var template (value) MobilityControlInfo v_MobilityControlInfo := f_Generate_cs_MobilityControlInfo_HO_CRNTI(p_CellId, v_CarrierFreqEutra, p_C_RNTI);
    var template (value) RadioResourceConfigDedicated v_RadioResourceConfigDedicated := f_Generate_cs_508_RadioResourceConfigDedicated_HO (p_CellId);

    //Build DRB identity list from inputed radiobearer config list
    for (k:=0; k < lengthof(p_DrbConfigList); k:= k+1) {
      v_DRB_IdentityList[k] := valueof ( p_DrbConfigList[k].Id.Drb );
    }
    
    /* for intra cell HO the HO procedure and the SS configuration must be executed with timing info.
     * This is to make sure SS reconfiguration is done properly and in time before UE access the new cell.
     * The timing info used is based on the information provided in TTCN CR R5s100178 */
    if ( ischosen ( p_TimingInfo.SubFrame ) )
      {
        //Use the provided timing iformation
        v_Timing := valueof(p_TimingInfo.SubFrame);
      }
    else //i.e. p_TimingInfo is "now" (either not provided or provided as "now")
      {
        //Get next sending occassion
        v_Timing := f_EUTRA_GetNextSendOccasion ( p_CellId );
      }
    
    //Transmit RRCConnectionReconfiguration/HO command to UE
    f_EUTRA_RRC_ConnReconfigHO_Common(p_CellId,
                                      p_MeasConfig,
                                      v_MobilityControlInfo,
                                      v_RadioResourceConfigDedicated,
                                      cs_508SecurityConfigHO_IntraLTE,
                                      cs_TimingInfo (v_Timing.SFN.Number, v_Timing.Subframe.Number));
    
    /* Note: current implementation releases SRBs and reconfigures C-RNTI 5ms after sending the RRCConnectionReconfiguration
       => RLC ACK is not taken into consideration */
    
    v_Timing := f_EUTRA_TimingInfoAdd(v_Timing, 5);   // add 5ms
    
    //Release SRBs and DRBs
    f_EUTRA_SS_SRBs_DRBs_Release ( p_CellId,
                                   cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number),
                                   v_DRB_IdentityList );
    
    //Configure C-RNTI based contention resolution in target cell 1 and assign new C-RNTI to the cell
    f_EUTRA_SS_ConfigRachProcedure_HO(p_CellId, cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number), p_C_RNTI);
      
    //Save new C-RNTI in cell configuration for target cell (which is also source cell)
    f_EUTRA_CellInfo_SetC_RNTI ( p_CellId, p_C_RNTI );
    
    //Setup SRBs and DRBs again
    v_Timing := f_EUTRA_TimingInfoAdd(v_Timing, 5);   // add 5ms
    f_EUTRA_SS_SRBs_DRBs_Config ( p_CellId,
                                  cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number),
                                  p_DrbConfigList );
    
    //Enable RRC Integrity and Ciphering at SS target cell
    v_Auth_Params := f_EUTRA_SS_AS_ActivateSecurity_HO_Reest ( p_CellId,
                                                               v_Auth_Params,
                                                               v_NextHopChainingCount,
                                                               cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number),
                                                               v_DRB_IdentityList );
    
    f_EUTRA_Security_Set ( v_Auth_Params );
    
    SRB.receive(car_SRB1_RrcPdu_IND(p_CellId, cr_508_RRCConnectionReconfigurationComplete(tsc_RRC_TI_Def)));       // @sic R5s100430  Change 2 sic@

    // configure RACH procedure for target cell back to default    @sic R5s100520 change 11 sic@
    f_EUTRA_SS_ConfigRachProcedure (p_CellId,
                                    omit,
                                    cs_RachProcedureConfig_Def(omit, f_EUTRA_CellInfo_GetRAR_TA(p_CellId), f_EUTRA_CellInfo_GetDL_ChBandwidth(p_CellId)));
  }


  /* @desc      Function for performing SS and UE configuration at IntraLTE InterCell handover. The function will:
   *             1. configure C-RNTI based contention resolution in SS with minimal grant
   *                i.e. the UE will have to raise SR to send additional/user data.
   *             2. activate security at target cell according to 36.508 cl. 4.6.4 Table 4.6.4-1
   *                with keyChangeIndicator set to FALSE and nextHopChainingCount set to 0
   *             3. send the RRCConnectionReconfiguration to UE. This message will include
   *               3a. Measurement configuration if specified.
   *               3b. MobilityControlInfo according to 36.508 cl. 4.6.5 Table 4.6.5-1
   *                   with newUE-Identity set to tsc_C_RNTI_Def2 or p_C_R_NTI
   *               3c. RadioResourceConfigDedicated according to 36.508 cl. 4.6.3 Table 4.6.3-19
   *               3d. SecurityConfigHO according to 36.508 cl. 4.6.4 Table 4.6.4-1
   *                   with keyChangeIndicator set to FALSE and nextHopChainingCount set to 0
   *             4. Receive the RRCConnectionReconfigurationComplete message
   *             5. Reset and re-establish SRBs and DRB(s) at source cell.
   *             NOTE: Steps above may not happen in the listed order as it depends on whether target and source cell are the same or not.
   * @param     p_SourceCellId
   * @param     p_TargetCellId
   * @param     p_DrbConfigList     (default value: cs_DrbConfigList_Def)
   * @param     p_C_RNTI            (default value: tsc_C_RNTI_Def2)
   * @param     p_MeasConfig        (default value: omit)
   * @param     p_DciUlInfo_TargetCell (default value: cs_DciInfo_CcchDcchDtchUL_Def)
   * @param     p_UL_GrantConfig_TargetCell (default value: cs_UL_GrantConfig_Def)
   * @status    APPROVED
   */
  function f_EUTRA_508RRC_IntraLTE_HO_InterCell ( CellId_Type                           p_SourceCellId,
                                                  CellId_Type                           p_TargetCellId,
                                                  template (value) RadioBearerList_Type p_DrbConfigList := cs_DrbConfigList_Def,
                                                  C_RNTI                                p_C_RNTI        := tsc_C_RNTI_Def2,
                                                  template (omit)  MeasConfig           p_MeasConfig    := omit,
                                                  template (omit)  DciUlInfo_Type       p_DciUlInfo_TargetCell      := cs_DciInfo_CcchDcchDtchUL_Def,
                                                  template (value) UL_GrantConfig_Type  p_UL_GrantConfig_TargetCell := cs_UL_GrantConfig_Def)  runs on EUTRA_PTC
  { /* @sic R5s100491: enhancement to support MeasConfig sic@ */
    /* @sic R5s100524: common change of HO functions sic@ */
    var DRB_IdentityList_Type v_DRB_IdentityList;
    var integer k;
    var EUTRA_SecurityParams_Type v_Auth_Params := f_EUTRA_Security_Get();
    var NextHopChainingCount v_NextHopChainingCount := 0; //According to 36.508 cl. 4.6.1 RRCConnectionReestablishment
    var Frequency_IE_Type v_Frequency_IE := f_EUTRA_CellInfo_GetFrequencyIEs (p_TargetCellId);
    var CarrierFreqEUTRA v_CarrierFreqEutra := v_Frequency_IE.UL_DL_Earfcn;
    var template (value) MobilityControlInfo v_MobilityControlInfo := f_Generate_cs_MobilityControlInfo_HO_CRNTI(p_TargetCellId, v_CarrierFreqEutra, p_C_RNTI);
    var template (value) RadioResourceConfigDedicated v_RadioResourceConfigDedicated := f_Generate_cs_508_RadioResourceConfigDedicated_HO (p_TargetCellId);

    // Build DRB identity list from inputed radiobearer config list
    for (k:=0; k < lengthof(p_DrbConfigList); k:= k+1) {
      v_DRB_IdentityList[k] := valueof ( p_DrbConfigList[k].Id.Drb );
    }
    
    // Configure C-RNTI based contention resolution in target cell
    f_EUTRA_SS_ConfigRachProcedure_HO(p_TargetCellId, cs_TimingInfo_Now, p_C_RNTI);
    
    //Save new C-RNTI in cell configuration for p_TargetCellId
    f_EUTRA_CellInfo_SetC_RNTI ( p_TargetCellId, p_C_RNTI );
      
    // activate RRC security at SS target cell for SRB 1, 2 and 1 AM DRB
    v_Auth_Params := f_EUTRA_SS_AS_ActivateSecurity_HO_Reest ( p_TargetCellId,
                                                               v_Auth_Params,
                                                               v_NextHopChainingCount,
                                                               -,
                                                               v_DRB_IdentityList);
    f_EUTRA_Security_Set ( v_Auth_Params );
    
    // stop periodic TA on source cell   @sic R5s100520 change 10 sic@
    f_EUTRA_SS_CommonCellConfig(p_SourceCellId, cas_ULGrantAllocation_Def_REQ(p_SourceCellId, cs_TimingInfo_Now, cs_PUCCH_Synch_None));

    // configure UL grant configuration on target cell    @sic R5s100515 sic@
    // f_EUTRA_SS_CommonCellConfig(p_TargetCellId, cas_ULGrantAllocation_REQ(p_TargetCellId, cs_TimingInfo_Now, p_DciUlInfo_TargetCell, cs_PUCCH_Synch_None, p_UL_GrantConfig_TargetCell));
    // Note: UL grant configuration is done directly after the HO as temporary solution for wk37 delivery

    // Tell the SS about the HO    @sic R5w100212 sic@
    f_EUTRA_SS_CommonCellConfig(p_TargetCellId, cas_PdcpHandoverInit_REQ(p_SourceCellId, p_TargetCellId));

    // Transmit RRCConnectionReconfiguration/HO command to UE
    f_EUTRA_RRC_ConnReconfigHO_Common(p_SourceCellId,
                                      p_MeasConfig,
                                      v_MobilityControlInfo,
                                      v_RadioResourceConfigDedicated,
                                      cs_508SecurityConfigHO_IntraLTE);
    
    SRB.receive(car_SRB1_RrcPdu_IND(p_TargetCellId, cr_508_RRCConnectionReconfigurationComplete(tsc_RRC_TI_Def)));       // @sic R5s100430  Change 2 sic@

    // temporary solution for wk37 delivery: further discussions ongoing
    f_EUTRA_SS_CommonCellConfig(p_TargetCellId,
                                cas_ULGrantAllocation_REQ(p_TargetCellId, cs_TimingInfo_Now, p_DciUlInfo_TargetCell, cs_PUCCH_Synch_Auto_Def, p_UL_GrantConfig_TargetCell, tsc_NoCnfReq));


    // Tell the SS about the HO    @sic R5w100212 sic @
    f_EUTRA_SS_CommonCellConfig(p_TargetCellId, cas_PdcpHandoverComplete_REQ(p_TargetCellId));

    // configure periodic TA on target cell    @sic R5s100525 change 1.3 sic@
    // f_EUTRA_SS_CommonCellConfig(p_TargetCellId, cas_PUCCH_Synch_Config_REQ(p_TargetCellId, cs_TimingInfo_Now, cs_PUCCH_Synch_Auto_Def, tsc_NoCnfReq)));
    // Note: UL grant configuration is done directly after the HO as temporary solution for wk37 delivery; therefore no reconfiguration of PUCCH_Synch is necessary
   
    // configure RACH procedure for target cell back to default    @sic R5s100520 change 11 sic@
    f_EUTRA_SS_ConfigRachProcedure (p_TargetCellId,
                                    omit,
                                    cs_RachProcedureConfig_Def(omit, f_EUTRA_CellInfo_GetRAR_TA(p_TargetCellId), f_EUTRA_CellInfo_GetDL_ChBandwidth(p_TargetCellId)));

    //Re-establish SRBs and DRB(s) in source cell
    f_EUTRA_SS_SRBs_DRBs_Release ( p_SourceCellId, -, v_DRB_IdentityList );
    f_EUTRA_SS_SRBs_DRBs_Config ( p_SourceCellId, -, p_DrbConfigList );
  }

  /* @desc      Function for sending IntraLTE HO command (as per 36.508) to UE
   *            i.e MobilityControlInfo is according to 36.508 cl. 4.6.5 Table 4.6.5-1 with newUE-Identity set to tsc_C_RNTI_Def2
   *            i.e RadioResourceConfigDedicated is according to 36.508 cl. 4.6.3 Table 4.6.3-19
   *            i.e SecurityConfigHO is according to 36.508 cl. 4.6.4 Table 4.6.4-1 with keyChangeIndicator set to FALSE and nextHopChainingCount set to 0
   * @param     p_SourceCellId
   * @param     p_TargetCellId
   * @param     p_TimingInfo        (default value: cs_TimingInfo_Now)
   * @status    (APPROVED)
   */
  function f_EUTRA_508RRC_ConnReconfigHO_IntraLTE ( CellId_Type                      p_SourceCellId,
                                                    CellId_Type                      p_TargetCellId,
                                                    template (value) TimingInfo_Type p_TimingInfo := cs_TimingInfo_Now  )  runs on EUTRA_PTC
  {
    f_EUTRA_RRC_ConnReconfigHO_Common ( p_SourceCellId,
                                        omit,
                                        f_Generate_cs_508_MobilityControlInfo_HO (p_TargetCellId),
                                        f_Generate_cs_508_RadioResourceConfigDedicated_HO (p_TargetCellId),
                                        cs_508SecurityConfigHO_IntraLTE,
                                        p_TimingInfo );
  }

  /* @desc      Function for sending IntraLTE HO command to UE with non-default parameters for SecurityConfigHO
   *            i.e MobilityControlInfo is according to 36.508 cl. 4.6.5 Table 4.6.5-1 with newUE-Identity set to tsc_C_RNTI_Def2
   *            i.e RadioResourceConfigDedicated is according to 36.508 cl. 4.6.3 Table 4.6.3-19
   *            i.e SecurityConfigHO is according to p_SecurityConfigHO
   * @param     p_SourceCellId
   * @param     p_TargetCellId
   * @param     p_SecurityConfigHO
   * @param     p_TimingInfo
   * @status    APPROVED
   *
  */
  function f_EUTRA_RRC_ConnReconfigHO_IntraLTE_Security ( CellId_Type                       p_SourceCellId,
                                                          CellId_Type                       p_TargetCellId,
                                                          template (value) SecurityConfigHO p_SecurityConfigHO,
                                                          template (value) TimingInfo_Type  p_TimingInfo := cs_TimingInfo_Now )  runs on EUTRA_PTC
  {
    f_EUTRA_RRC_ConnReconfigHO_Common ( p_SourceCellId,
                                        omit,
                                        f_Generate_cs_508_MobilityControlInfo_HO (p_TargetCellId),
                                        f_Generate_cs_508_RadioResourceConfigDedicated_HO (p_TargetCellId),
                                        p_SecurityConfigHO,
                                        p_TimingInfo );
  }

  /*
   * @desc      Generates the default RadioresourceConfigDedicated template (according to 36.508) and returns the template
   * @param     p_TargetCellId
   * @return    template (value) RadioResourceConfigDedicated
   * @status    APPROVED
   */
  function f_Generate_cs_508_RadioResourceConfigDedicated_HO (CellId_Type p_TargetCellId) runs on EUTRA_PTC return template (value) RadioResourceConfigDedicated
  {

    var template (value) EUTRA_FDD_TDD_CellInfo_Type v_EUTRA_FDD_TDD_CellInfo := f_EUTRA_CellInfo_GetFDD_TDD_Info (p_TargetCellId);
    var SR_PUCCH_ResourceIndex_Type v_Sr_PUCCH_ResourceIndex := f_EUTRA_CellInfo_GetSR_PUCCH_ResourceIndex (p_TargetCellId);
    var SR_ConfigIndex_Type v_SR_ConfigIndex := f_EUTRA_CellInfo_GetSR_ConfigIndex (p_TargetCellId );
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (p_TargetCellId);

    return cs_508_RadioResourceConfigDedicated_HO ( v_EUTRA_FDD_TDD_CellInfo.cqi_ReportConfig,
                                                    v_EUTRA_FDD_TDD_CellInfo.soundingRS_UL_Config,
                                                    v_AntennaInfo,
                                                    v_Sr_PUCCH_ResourceIndex,
                                                    v_SR_ConfigIndex )
  }

  /* @desc      Generates a MobilityControlInfo template based on the input parameters
   *            If parameters are not input, default values (according to 36.508) will be used.
   * @param     p_TargetCellId
   * @param     p_CarrierFreqEutra
   * @param     p_RadioResourceConfigCommon
   * @param     p_EUTRA_CarrierBandwidth (default value: omit)
   * @param     p_AdditionalSpectrumEmission (default value: omit)
   * @param     p_T304              (default value: ms1000)
   * @param     p_C_RNTI            (default value: tsc_C_RNTI_Def2)
   * @param     p_RACH_ConfigDedicated (default value: omit)
   * @return    template (value) MobilityControlInfo
   * @status    APPROVED
  */
  function f_Generate_cs_MobilityControlInfo_HO ( CellId_Type                                p_TargetCellId,
                                                  template (omit) CarrierFreqEUTRA           p_CarrierFreqEutra,
                                                  template (omit) RadioResourceConfigCommon  p_RadioResourceConfigCommon,
                                                  template (omit) CarrierBandwidthEUTRA      p_EUTRA_CarrierBandwidth := omit,
                                                  template (omit) integer                    p_AdditionalSpectrumEmission := omit,
                                                  MobilityControlInfo.t304                   p_T304 := ms1000,
                                                  C_RNTI                                     p_C_RNTI := tsc_C_RNTI_Def2,
                                                  template (omit) RACH_ConfigDedicated       p_RACH_ConfigDedicated := omit ) runs on EUTRA_PTC return template (value) MobilityControlInfo
  {

    var template (omit) AntennaInfoCommon v_AntennaInfoCommon := f_EUTRA_CellInfo_GetAntennaInfoCommon (p_TargetCellId);
    var EUTRA_CellSysInfo_Type v_EUTRA_CellSysInfo_Type := f_EUTRA_CellInfo_GetSYSINFO(p_TargetCellId);
    var RadioResourceConfigCommonSIB v_RadioResourceConfigCommonSIB := v_EUTRA_CellSysInfo_Type.BCCH_Info.SIs[0].message_.c1.systemInformation.criticalExtensions.systemInformation_r8.sib_TypeAndInfo[0].sib2.radioResourceConfigCommon;
    var PhysCellId v_PhysicalCellIdentity := f_EUTRA_CellInfo_GetPhyCellId(p_TargetCellId);
    var template (value) RadioResourceConfigCommon v_RadioResourceConfigCommon;
    var template (omit) TDD_Config v_Tdd_Configuration := v_EUTRA_CellSysInfo_Type.BCCH_Info.SIB1.message_.c1.systemInformationBlockType1.tdd_Config;

    if ( isvalue ( p_RadioResourceConfigCommon ) ) {
      //If non default RadioResourceConfigCommon has been specified, use those values
      v_RadioResourceConfigCommon := p_RadioResourceConfigCommon;
    }
    else {
      //If no RadioResourceConfigCommon-template has been passed in to function generate default one
      v_RadioResourceConfigCommon := cs_RadioResourceConfigCommon_Def ( v_RadioResourceConfigCommonSIB.rach_ConfigCommon,
                                                                        cs_PRACH_Config_Def(v_RadioResourceConfigCommonSIB.prach_Config),
                                                                        v_RadioResourceConfigCommonSIB.pusch_ConfigCommon,
                                                                        v_RadioResourceConfigCommonSIB.soundingRS_UL_ConfigCommon,
                                                                        v_Tdd_Configuration,
                                                                        v_AntennaInfoCommon );
    }

    return cs_MobilityControlInfo ( v_PhysicalCellIdentity,
                                    p_CarrierFreqEutra,
                                    p_EUTRA_CarrierBandwidth,
                                    p_AdditionalSpectrumEmission,
                                    p_T304,
                                    p_C_RNTI,
                                    v_RadioResourceConfigCommon,
                                    p_RACH_ConfigDedicated )
  }

  /* @desc      Wrapper function to generate a MobilityControlInfo template with a specific C-RNTI.
   * @param     p_TargetCellId
   * @param     p_CarrierFreqEutra
   * @param     p_C_RNTI
   * @return    template (value) MobilityControlInfo
   * @status    APPROVED
   */
  function f_Generate_cs_MobilityControlInfo_HO_CRNTI ( CellId_Type                      p_TargetCellId,
                                                        template (omit) CarrierFreqEUTRA p_CarrierFreqEutra,
                                                        C_RNTI                           p_C_RNTI ) runs on EUTRA_PTC return template (value) MobilityControlInfo
  {

    return f_Generate_cs_MobilityControlInfo_HO ( p_TargetCellId, p_CarrierFreqEutra, omit, -, -, -, p_C_RNTI )
  }

  /* @desc      Wrapper function to generate a MobilityControlInfo template with dedicated resources for RACH procedure.
   * @param     p_TargetCellId
   * @param     p_CarrierFreqEutra
   * @param     p_RACH_ConfigDedicated
   * @return    template (value) MobilityControlInfo
   * @status    APPROVED
  */
  function f_Generate_cs_MobilityControlInfo_HO_RACH ( CellId_Type                           p_TargetCellId,
                                                       template (omit) CarrierFreqEUTRA      p_CarrierFreqEutra,
                                                       template (value) RACH_ConfigDedicated p_RACH_ConfigDedicated  ) runs on EUTRA_PTC return template (value) MobilityControlInfo
  {

    return f_Generate_cs_MobilityControlInfo_HO ( p_TargetCellId, p_CarrierFreqEutra, omit, -, -, -, -, p_RACH_ConfigDedicated )
  }

  /* @desc      Wrapper function to generate a MobilityControlInfo template with dedicated resources for RACH procedure and a specific C-RNTI.
   * @param     p_TargetCellId
   * @param     p_CarrierFreqEutra
   * @param     p_C_RNTI
   * @param     p_RACH_ConfigDedicated
   * @return    template (value) MobilityControlInfo
   * @status    APPROVED
   *
  */
  function f_Generate_cs_MobilityControlInfo_HO_CRNTI_RACH ( CellId_Type                           p_TargetCellId,
                                                             template (omit) CarrierFreqEUTRA      p_CarrierFreqEutra,
                                                             C_RNTI                                p_C_RNTI,
                                                             template (value) RACH_ConfigDedicated p_RACH_ConfigDedicated  ) runs on EUTRA_PTC return template (value) MobilityControlInfo
  {
    return f_Generate_cs_MobilityControlInfo_HO ( p_TargetCellId, p_CarrierFreqEutra, omit, -, -, -, p_C_RNTI, p_RACH_ConfigDedicated )
  }

  /* @desc      Generates the default MobilityControlInfo template (acordin to 36.508) and returns the template
   * @param     p_TargetCellId
   * @return    template (value) MobilityControlInfo
   * @status    APPROVED
   */
  function f_Generate_cs_508_MobilityControlInfo_HO ( CellId_Type p_TargetCellId ) runs on EUTRA_PTC return template (value) MobilityControlInfo
  {
    var Frequency_IE_Type v_Frequency_IE := f_EUTRA_CellInfo_GetFrequencyIEs (p_TargetCellId);
    var CarrierFreqEUTRA v_CarrierFreqEutra := v_Frequency_IE.UL_DL_Earfcn;
    return f_Generate_cs_MobilityControlInfo_HO ( p_TargetCellId, v_CarrierFreqEutra, omit )
  }

  /* @desc      Function for sending IntraLTE HO command to UE with non-default parameters for SecurityConfigHO
   * @param     p_SourceCellId
   * @param     p_TargetCellId
   * @param     p_MobilityControlInfo
   * @param     p_TimingInfo        (default value: cs_TimingInfo_Now)
   * @status    APPROVED
  */
  function f_EUTRA_RRC_ConnReconfigHO_IntraLTE_Mobility ( CellId_Type                          p_SourceCellId,
                                                          CellId_Type                          p_TargetCellId,
                                                          template (value) MobilityControlInfo p_MobilityControlInfo,
                                                          template (value) TimingInfo_Type     p_TimingInfo := cs_TimingInfo_Now )  runs on EUTRA_PTC
  {
    f_EUTRA_RRC_ConnReconfigHO_Common ( p_SourceCellId,
                                        omit,
                                        p_MobilityControlInfo,
                                        f_Generate_cs_508_RadioResourceConfigDedicated_HO (p_TargetCellId),
                                        cs_508SecurityConfigHO_IntraLTE,
                                        p_TimingInfo );
  }

  /* @desc      Function for sending IntraLTE HO command to UE with non-default parameters for SecurityConfigHO MobilityControlInfo
   * @param     p_SourceCellId
   * @param     p_TargetCellId
   * @param     p_MobilityControlInfo
   * @param     p_SecurityConfigHO
   * @param     p_TimingInfo        (default value: cs_TimingInfo_Now)
   * @status    APPROVED
  */
  function f_EUTRA_RRC_ConnReconfigHO_IntraLTE_MobilitySecurity ( CellId_Type                          p_SourceCellId,
                                                                  CellId_Type                          p_TargetCellId,
                                                                  template (value) MobilityControlInfo p_MobilityControlInfo,
                                                                  template (value) SecurityConfigHO    p_SecurityConfigHO,
                                                                  template (value) TimingInfo_Type     p_TimingInfo := cs_TimingInfo_Now )  runs on EUTRA_PTC {

    f_EUTRA_RRC_ConnReconfigHO_Common ( p_SourceCellId,
                                        omit,
                                        p_MobilityControlInfo,
                                        f_Generate_cs_508_RadioResourceConfigDedicated_HO (p_TargetCellId),
                                        p_SecurityConfigHO,
                                        p_TimingInfo );

  }


  /* @desc      Generic function for IntraLTE HO
   * @param     p_SourceCellId
   * @param     p_MeasurementConfiguration
   * @param     p_MobilityControlInfo
   * @param     p_RadioResourceConfigDedicated
   * @param     p_SecurityConfigHO
   * @param     p_TimingInfo        (default value: cs_TimingInfo_Now)
   * @status    APPROVED
   */
  function f_EUTRA_RRC_ConnReconfigHO_Common ( CellId_Type                                  p_SourceCellId,
                                               template (omit) MeasConfig                   p_MeasurementConfiguration,
                                               template (omit) MobilityControlInfo          p_MobilityControlInfo,
                                               template (omit) RadioResourceConfigDedicated p_RadioResourceConfigDedicated,
                                               template (omit) SecurityConfigHO             p_SecurityConfigHO,
                                               template (value) TimingInfo_Type             p_TimingInfo := cs_TimingInfo_Now )  runs on EUTRA_PTC {

    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_SourceCellId,
                                     p_TimingInfo,
                                     cs_RRCConnectionReconfiguration_Common ( tsc_RRC_TI_Def,
                                                                              p_MeasurementConfiguration,
                                                                              p_MobilityControlInfo,
                                                                              p_RadioResourceConfigDedicated,
                                                                              p_SecurityConfigHO ) ) );

  }

  //----------------------------------------------------------------------------
  /*
   * @desc      RRC connection release acc. to 36.331 cl. 5.3.8
   * @param     p_CellId
   * @param     p_RRCConnectionRelease
   * @status    APPROVED
   */
  function f_EUTRA_RRC_ConnectionRelease_Common(CellId_Type p_CellId,
                                                template (value) DL_DCCH_Message p_RRCConnectionRelease) runs on EUTRA_PTC
  { /* RRC connection release acc. to 36.331 cl. 5.3.8 */

    SRB.send(cas_SRB1_RrcPdu_REQ(p_CellId,
                                 cs_TimingInfo_Now,
                                 p_RRCConnectionRelease));

    //Reset security entity in SS
    f_EUTRA_SS_RRC_ReleaseSecurity(p_CellId);

    //Wait 60ms + tolerance to allow maximum UE delay according to 36.331 cl. 5.3.8.3
    f_Delay ( f_EUTRA_SetTimerToleranceMax (p_CellId, 60E-3) );

    f_EUTRA_StopULGrantTransmission ( p_CellId, cs_TimingInfo_Now );

    f_EUTRA_SS_SRBs_DRBs_Release ( p_CellId );            /* R5s100090 and R5s090312 change 7; additional changes */
    f_EUTRA_SS_SRBs_DRBs_Config ( p_CellId );             /* R5s100090 and R5s090312 change 7; additional changes */
  };

  /*
   * @desc      RRC connection release acc. to 36.331 cl. 5.3.8
   * @param     p_CellId
   * @param     p_RRC_TI
   * @param     p_ReleaseCause
   * @status    APPROVED
   */
  function f_EUTRA_RRC_ConnectionRelease(CellId_Type p_CellId,
                                         RRC_TransactionIdentifier p_RRC_TI,
                                         ReleaseCause p_ReleaseCause) runs on EUTRA_PTC
  {
    f_EUTRA_RRC_ConnectionRelease_Common(p_CellId, cs_RRCConnectionRelease(p_RRC_TI, p_ReleaseCause));
  };

  //----------------------------------------------------------------------------
  /*
   * @desc      RRC connection release acc. to 36.331 cl. 5.3.8 but for scheduled release
   * @param     p_CellId
   * @param     p_RRCConnectionRelease
   * @status    APPROVED
   */
  function f_EUTRA_RRC_ConnectionRelease_ImmediateReset(CellId_Type p_CellId,
                                                        template (value) DL_DCCH_Message p_RRCConnectionRelease) runs on EUTRA_PTC
  { /* RRC connection release acc. to 36.331 cl. 5.3.8 */
    var SubFrameTiming_Type v_Timing;

    /* disable the UL grant (=> it is not possible for the UE to send any ACK for the RRC Connecion Release): */
    f_EUTRA_StopULGrantTransmission(p_CellId, cs_TimingInfo_Now);  // schedule it now => it shall be done in less than 80ms

    v_Timing := f_EUTRA_GetNextSendOccasion(p_CellId);

    /* send RRCConnectionRelease (UE cannot send an ACK =>
       "lower layers" cannot "indicate that the receipt of the RRCConnectionRelease message has been successfully acknowledged" and the UE will wait for 60ms;
       see 36.331 cl. 5.3.8.3) */
    SRB.send(cas_SRB1_RrcPdu_REQ(p_CellId,
                                 cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number),
                                 p_RRCConnectionRelease));

    // Release SRDs and default DRB
    v_Timing := f_EUTRA_TimingInfoAdd(v_Timing, 5);   // add 5ms
    f_EUTRA_SS_SRBs_DRBs_Release(p_CellId, cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number));

    // Reset security entity in SS
    v_Timing := f_EUTRA_TimingInfoAdd(v_Timing, 5);   // add 5ms
    f_EUTRA_SS_RRC_ReleaseSecurity(p_CellId, cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number));

    // Configure SRBs and default DRB
    v_Timing := f_EUTRA_TimingInfoAdd(v_Timing, 5);   // add 5ms
    f_EUTRA_SS_SRBs_DRBs_Config(p_CellId, cs_TimingInfo(v_Timing.SFN.Number, v_Timing.Subframe.Number));
  };

  //----------------------------------------------------------------------------
  /*
   * @desc      Function that acquires the FDD/TDD specific templates and sends RRCConnectionReestablishment
   *            according to 36.508 cl. 4.6.1 Table 4.6.1-10
   * @param     p_CellId
   * @param     p_RRC_TI
   * @param     p_NextHopChainingCount
   * @status    APPROVED
   */
  function f_EUTRA_RRC_RRCConnectionReestablishment_Def ( CellId_Type               p_CellId,
                                                          RRC_TransactionIdentifier p_RRC_TI,
                                                          NextHopChainingCount      p_NextHopChainingCount ) runs on EUTRA_PTC {

    var template (value) EUTRA_FDD_TDD_CellInfo_Type v_EUTRA_FDD_TDD_Info := f_EUTRA_CellInfo_GetFDD_TDD_Info( p_CellId );
    var SR_PUCCH_ResourceIndex_Type v_Sr_PUCCH_ResourceIndex := f_EUTRA_CellInfo_GetSR_PUCCH_ResourceIndex ( p_CellId );
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (p_CellId);
    var SR_ConfigIndex_Type v_SR_ConfigIndex := f_EUTRA_CellInfo_GetSR_ConfigIndex (p_CellId );
    var template (value) DL_CCCH_Message v_RRCConnectionReestablishment;

    if ( p_NextHopChainingCount == 0 ) {
      v_RRCConnectionReestablishment := cs_508_RRCConnectionReestablishment(p_RRC_TI,
                                                                            v_EUTRA_FDD_TDD_Info.pucch_Config,
                                                                            v_Sr_PUCCH_ResourceIndex,
                                                                            v_AntennaInfo,
                                                                            v_SR_ConfigIndex);
    } else {
      v_RRCConnectionReestablishment := cs_RRCConnectionReestablishment_Common(p_RRC_TI,
                                                                               cs_508_RadioResourceConfigDedicated_SRB1 ( cs_MAC_MainConfig_SRB1,
                                                                                                                          cs_508_PhysicalConfigDedicated_Default_SRB1(v_EUTRA_FDD_TDD_Info.pucch_Config,
                                                                                                                                                                      v_Sr_PUCCH_ResourceIndex,
                                                                                                                                                                      v_AntennaInfo,
                                                                                                                                                                      v_SR_ConfigIndex )),
                                                                               p_NextHopChainingCount);
    }
    SRB.send (cas_SRB0_RrcPdu_REQ(p_CellId, cs_TimingInfo_Now, v_RRCConnectionReestablishment));
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      Function for resuming SRB2 and existing DRBs after successful completion of the RRC Connection Re-establishment procedure.
   *            Procedure is according to 36.330 cl. 5.3.5.3 with default values according to
   *            - for RRCConnectionReconfiguration : 36.508 Table 4.6.1-8 using value RadioResourceConfigDedicated-HO
   *            - for RRCConnectionReconfigurationComplete : 36.508 Table 4.6.1-9
   * @param     p_CellId
   * @param     p_RRC_TI
   * @status    APPROVED
   */
  function f_EUTRA_RRC_RRCConnectionReconfiguration_Resume_SRB2_DRBs ( CellId_Type               p_CellId,
                                                                       RRC_TransactionIdentifier p_RRC_TI) runs on EUTRA_PTC {

    //Get FDD or TDD mode from Cell-configuration
    var template (value) EUTRA_FDD_TDD_CellInfo_Type  v_EUTRA_FDD_TDD_Info := f_EUTRA_CellInfo_GetFDD_TDD_Info( p_CellId );
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (p_CellId);
    var SR_PUCCH_ResourceIndex_Type v_Sr_PUCCH_ResourceIndex := f_EUTRA_CellInfo_GetSR_PUCCH_ResourceIndex ( p_CellId );
    var SR_ConfigIndex_Type v_SR_ConfigIndex := f_EUTRA_CellInfo_GetSR_ConfigIndex ( p_CellId );

    //Send RRCConnectionReconfiguration
    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId,
                                     cs_TimingInfo_Now,
                                     cs_RRCConnReconfig_Resume_SRB2_DRBs ( p_RRC_TI,
                                                                           v_EUTRA_FDD_TDD_Info.cqi_ReportConfig,
                                                                           v_EUTRA_FDD_TDD_Info.soundingRS_UL_Config,
                                                                           v_AntennaInfo,
                                                                           v_Sr_PUCCH_ResourceIndex,
                                                                           v_SR_ConfigIndex) ) );
    //Receive RRCConnectionReconfigurationComplete
    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId, cr_508_RRCConnectionReconfigurationComplete ( p_RRC_TI ) ) );
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      To decode the EUTRA UE-CapabilityRAT-Container
   * @param     p_Octetstring
   * @return    UE_EUTRA_Capability
   * @status    APPROVED
   */
  function f_EUTRA_DecodeEutraCapMsg ( octetstring p_Octetstring ) return UE_EUTRA_Capability {
    var UE_EUTRA_Capability v_Ue_Eutra_Capability;
    var bitstring v_Bitstring := oct2bit(p_Octetstring);
    var integer v_Result := decvalue(v_Bitstring, v_Ue_Eutra_Capability);
    if (v_Result != 0) {
      FatalError(__FILE__, __LINE__, "UE EUTRA Capability cannot be decoded");
    }
    return v_Ue_Eutra_Capability;
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      Send UECapabilityEnquiry message and receives UECapabilityInformation message.
   *            The received UE Capabilities are decoded and stored in the structure for the Mobile Info.
   * @param     p_CellId
   * @param     p_RRC_TI
   * @status    APPROVED
   */
  function f_EUTRA_Capability ( CellId_Type p_CellId,
                                RRC_TransactionIdentifier p_RRC_TI ) runs on EUTRA_PTC
  {
    var SRB_COMMON_IND v_ReceivedAspForUeCapabilityInfo;
    var UE_EUTRA_Capability v_EutraCapability;

    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId, cs_TimingInfo_Now, cs_508_UeCapabilityEnquiry ( p_RRC_TI ) ) );
    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId, cr_UeCapabilityInformation_Any ( p_RRC_TI ) ) )
         -> value v_ReceivedAspForUeCapabilityInfo;

    // Decode and store the ueCapabilitiesRAT-Container
    v_EutraCapability := f_EUTRA_DecodeEutraCapMsg ( v_ReceivedAspForUeCapabilityInfo.Signalling.Rrc.Dcch.message_.c1.ueCapabilityInformation.criticalExtensions.c1.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList[0].ueCapabilityRAT_Container );
    f_EUTRA_MobileInfo_SetUECapability (v_EutraCapability);
  }

  //============================================================================

  /*
   * @desc      Calculate the BCCH modification period length in radio frames as per 36.331 cl 6.3.2
   * @param     p_CellId
   * @return    integer
   * @status    APPROVED
   */
  function f_CalculateModificationPeriod (CellId_Type p_CellId) runs on EUTRA_PTC return integer
  {
    var integer v_PagingCycle;
    var integer v_ModPeriodcoef;
    var integer v_ModificationPeriodFrames;
    var EUTRA_CellSysInfo_Type  v_Sysinfo := f_EUTRA_CellInfo_GetSYSINFO (p_CellId);
    var PCCH_Config v_PcchConfig := f_EUTRA_CellInfo_GetPCCH_ConfigInSYSINFO(p_CellId);

    // Get the modification period coefficient from sysinfo
    select (v_Sysinfo.BCCH_Info.SIs[0].message_.c1.systemInformation.criticalExtensions.systemInformation_r8.sib_TypeAndInfo[0].sib2.radioResourceConfigCommon.bcch_Config.modificationPeriodCoeff){
      case (n2) {v_ModPeriodcoef:=2}
      case (n4) {v_ModPeriodcoef:=4}
      case (n8) {v_ModPeriodcoef:=8}
      case else{
        FatalError (__FILE__, __LINE__, "invalid modification period coefficient");
      }
    };

    // Get paging cycle from sysinfo
    v_PagingCycle := f_EUTRA_GetPagingCycleValue (v_PcchConfig.defaultPagingCycle);

    // As per 36.331 cl 6.3.2: modification period in radio frames= modificationPeriodCoeff * defaultPagingCycle.
    v_ModificationPeriodFrames := v_PagingCycle*v_ModPeriodcoef;  /* @sic R5s100020 sic@ */

    return v_ModificationPeriodFrames;
  };

  //----------------------------------------------------------------------------
  /*
   * @desc      Trigger SS to send paging request to indicate sysinfo modification to UE from a given SFN
   * @param     p_CellId
   * @param     p_StartSFN
   * @status    APPROVED
   */
  function f_EUTRA_UE_PageSysinfoMod ( CellId_Type p_CellId,
                                       SystemFrameNumber_Type  p_StartSFN ) runs on EUTRA_PTC
  {
    var PCCH_Config v_SI_Pcch_Configuration := f_EUTRA_CellInfo_GetPCCH_ConfigInSYSINFO ( p_CellId ); //Derive DRX params from SYS Info
    var EUTRA_FDD_TDD_Mode_Type v_EUTRA_FDD_TDD_Mode := f_EUTRA_CellInfo_GetFDD_TDD ( p_CellId ); //Derive FDD or TDD mode from Cell-configuration
    var SubFrameTimingList_Type v_PoList;

    // @sic R5s100240 : Removed derivation of UE specicif drx parameters. Calculation of smallest paging cycle is modified. sic@

    //Calculate next Paging Occassion
    v_PoList := f_EUTRA_Calculate_PF_PO ( p_StartSFN, v_SI_Pcch_Configuration.defaultPagingCycle, v_SI_Pcch_Configuration.nB, px_IMSI_Def, v_EUTRA_FDD_TDD_Mode );

    // Send Paging indicating SysinfoModification
    SYS.send ( cas_CellConfig_PagingSysinfoMod_REQ ( p_CellId, v_PoList[0], omit));

    /* @sic R5s100135 CNF removed sic@ */
  }

  // ---------------------------------------------------------------------------------------------
  //----------------------------FUNCTION to modify SYSINFO during a test execution ---------------

  /*
   * @desc      - Change SB1_ValueTagChanged == true
   *            - Page UE and Send the modified SYSINFO
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_ModifySysinfo ( CellId_Type p_CellId ) runs on EUTRA_PTC
  {
    f_EUTRA_SetSB1_ValueTag (p_CellId, true);
    f_EUTRA_ModifySysinfo_ValueTag (p_CellId);
  }

  /*
   * @desc      - Increment the valueTag in SIB1(if SB1_ValueTagChanged == true)
   *            - Page UE (depending on p_PagingFlag)
   *            - Send the modified SYSINFO
   * @param     p_CellId
   * @param     p_PagingFlag      (default value: true)
   * @return    SystemFrameNumber_Type   (time at which the paging will happen)
   * @status    APPROVED
   */
  function f_EUTRA_ModifySysinfo_ValueTag ( CellId_Type p_CellId,
                                            boolean p_PagingFlag := true ) runs on EUTRA_PTC return SystemFrameNumber_Type
  { //@sic R5-104157 return SystemFrameNumber_Type sic@
    var template (value) AllSiSchedul_Type v_SiSchedul;
    var SubFrameTiming_Type     v_Timing;
    var integer                 v_TimeToPaging; //@sic R5s100475 cl. 4.3 Additional changes sic@
    var SystemFrameNumber_Type  v_StartNextModPeriod, v_Timing_SFN, v_TimeOfPaging; //@sic R5s100475 cl. 4.3 Additional changes sic@
    var EUTRA_CellSysInfo_Type  v_Sysinfo:= f_EUTRA_CellInfo_GetSYSINFO (p_CellId);  // Get SYSINFO to be sent
    var integer v_ModificationPeriodFrames := f_CalculateModificationPeriod (p_CellId);
    var EUTRA_FDD_TDD_Mode_Type  v_EUTRA_FDD_TDD_Mode := f_EUTRA_CellInfo_GetFDD_TDD ( p_CellId );
    var PCCH_Config v_PcchConfig := f_EUTRA_CellInfo_GetPCCH_ConfigInSYSINFO(p_CellId); //@sic R5s100475 cl. 4.3 Additional changes sic@
    var integer v_PagingCycle := f_EUTRA_GetPagingCycleValue (v_PcchConfig.defaultPagingCycle); //@sic R5s100475 cl. 4.3 Additional changes sic@

    // Increment the VALUE TAG if neeeded
    if (v_Sysinfo.SB1_ValueTagChanged == 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 (p_CellId,v_Sysinfo);     // Save SYSINFO with the new valueTag in the record
      f_EUTRA_SetSB1_ValueTag (p_CellId, false);  // Set the flag to FALSE
    };

    //Get the current SFN and subframe
    v_Timing := f_EUTRA_GetCurrentTiming ( p_CellId );
    //Add processing delay to current SFN
    v_Timing_SFN := (v_Timing.SFN.Number + tsc_SfnDelay) mod 1024; //@sic R5s100475 cl. 4.3 Additional changes sic@

    //Calculate the next Modification Period
    //@sic R5s100475 cl. 4.3 Additional changes sic@
    v_StartNextModPeriod := (v_Timing_SFN + (v_ModificationPeriodFrames -(v_Timing_SFN mod v_ModificationPeriodFrames)))mod 1024;
    
    //@sic R5s100475 cl. 4.3 Additional changes sic@
    //Calculate time to paging in SFNs
    v_TimeToPaging := (1024 - v_Timing_SFN + v_StartNextModPeriod) mod 1024;
    
    //@sic R5s100475 cl. 4.3 Additional changes sic@
    if (p_PagingFlag) {
      //Check if there is time to page within this modification period
      if (v_TimeToPaging > 2*v_PagingCycle) {//At least two paging frames are available within this modification period
        //Page UE within this modification period
        v_TimeOfPaging := v_Timing_SFN;
      }
      else {//if two paging frames are not available within this modification period
        //Page UE from the start of the next modification period
        v_TimeOfPaging := v_StartNextModPeriod;

        //And send Sys info at the second next modification period
        v_StartNextModPeriod := (v_StartNextModPeriod + v_ModificationPeriodFrames) mod 1024;
      }

      //Page UE within this modification period or from the start of the next modification period
      //based oon above calculation
      f_EUTRA_UE_PageSysinfoMod ( p_CellId, v_TimeOfPaging );
    }
    // 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);      /* derive SS's scheduling information from SIB1 */

    // Send sysinfo at the start of the next appropriate modification period
    SYS.send (cas_ModifySysinfo_Def_REQ( p_CellId,
                                         cs_TimingInfo_SFN(v_StartNextModPeriod),
                                         v_SiSchedul,              /* scheduling of SIs */
                                         v_Sysinfo.BCCH_Info,       /* system information to be scheduled */
                                         f_EUTRA_CellInfo_GetDL_ChBandwidth (p_CellId)));
    return v_StartNextModPeriod;
  };

  /*
   * @desc      - Increment the valueTag in SIB1(if SB1_ValueTagChanged == true)
   *            - No Paging message is sent (e.g. when UE is not in the cell)
   *            - Send the modified SYSINFO
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_ModifySysinfo_ValueTag_NoPaging ( CellId_Type p_CellId ) runs on EUTRA_PTC
  {
    var boolean v_PagingFlag := false;
    f_EUTRA_ModifySysinfo_ValueTag ( p_CellId, v_PagingFlag );
  }

   /*
   * @desc      - Increment the valueTag in SIB1(if SB1_ValueTagChanged == true)
   *            - No Paging message is sent (e.g. when UE is OFF)
   *            - Send the modified SYSINFO
   *            - Wait the duration of one modification period to ensure SYSINFO is broadcasted
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_ModifySysinfoUE_Off ( CellId_Type p_CellId ) runs on EUTRA_PTC
  { // @sic R5s100008 sic@
    var boolean v_PagingFlag := false;
    var float v_ModificationPeriod_s := int2float(f_CalculateModificationPeriod (p_CellId))/100.0; // Modification period in second

    f_EUTRA_ModifySysinfo_ValueTag ( p_CellId, v_PagingFlag );
    
    //Wait the duration of 1.1 modification periods to ensure new SYSINFO takes effect
    f_Delay (1.1*v_ModificationPeriod_s); //@sic R5s100466 Change 13 & R5s100475 cl. 4.4 Additional changes sic@
  }

  /*
   * @desc      To set SYSINFO back to the default value (as per 36.508), the ValueTag value is kept (not reinitialised to 0)
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_ChangeSystemInformation_ToDefault ( CellId_Type p_CellId ) runs on EUTRA_PTC
  {
    var EUTRA_CellInfo_Type v_CellInfo := f_EUTRA_CellInfo_Get ( p_CellId );
    var template (value) EUTRA_CellInfo_Type v_NewCellInfo := v_CellInfo;
    var template (value) BcchInfo_Type v_BCCH_Info;
    // Save the current ValueTag
    var integer v_ValueTag := v_CellInfo.Sysinfo.BCCH_Info.SIB1.message_.c1.systemInformationBlockType1.systemInfoValueTag;
    var RootSequenceIndex_Type v_RootSequenceIndex := f_EUTRA_CellInfo_GetRootSequenceIndex ( p_CellId );

    // Initialise SYSINFO to default value
    // ValueTag is reinitialise to 0
    v_BCCH_Info := f_EUTRA_InitSystemInformation ( p_CellId,
                                                   v_CellInfo.CellIds.CellIdentity,
                                                   v_CellInfo.Sysinfo.Combination,
                                                   v_CellInfo.Frequency.FrequencyBandIndicator,
                                                   v_CellInfo.Frequency.DL_ChBandwidth,
                                                   v_CellInfo.Frequency.UL_ChBandwidth,
                                                   cs_PlmnIdList_1Entry(v_CellInfo.NAS_Parameters.Guti_Parameters.PLMN_Identity),
                                                   v_CellInfo.NAS_Parameters.TrackingAreaCode,
                                                   v_CellInfo.FDD_TDD_Info,
                                                   v_RootSequenceIndex );
    // Restore ValueTag
    v_BCCH_Info.SIB1.message_.c1.systemInformationBlockType1.systemInfoValueTag:= v_ValueTag;

    v_NewCellInfo.Sysinfo.BCCH_Info := v_BCCH_Info;
    f_EUTRA_CellInfo_Set (p_CellId, v_NewCellInfo);
  };
}
