/******************************************************************************/
// @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_ConnRel
{
//=========================================================================
// Import Part
//=========================================================================

  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from CommonDefs all;
  import from CommonIratDefs all;
  import from Parameters all;
  import from EUTRA_Component all;
  import from EUTRA_CellInfo all;
  import from EUTRA_CommonDefs all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_CommonProcedures all;
  import from EUTRA_RRCSteps all;
  import from EUTRA_IdleMode all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_SysInfo_Templates all;
  import from EUTRA_Timing all;
  import from EUTRA_RRC_Templates all;
  import from EUTRA_IRATFunctions all;


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

  template (value) IdleModeMobilityControlInfo cs_IdleModeMobCntrlInfo_3Entries ( ARFCN_ValueEUTRA p_Dl_CarrierFreq_Cell1,
                                                                                  integer              p_Prio1,
                                                                                  ARFCN_ValueEUTRA p_Dl_CarrierFreq_Cell2,
                                                                                  integer              p_Prio2,
                                                                                  ARFCN_ValueEUTRA p_Dl_CarrierFreq_Cell3,
                                                                                  integer              p_Prio3 ) := {
    freqPriorityListEUTRA := {
      {
        carrierFreq := p_Dl_CarrierFreq_Cell1,
        cellReselectionPriority := p_Prio1
      },
      {
        carrierFreq := p_Dl_CarrierFreq_Cell2,
        cellReselectionPriority := p_Prio2
      },
      {
        carrierFreq := p_Dl_CarrierFreq_Cell3,
        cellReselectionPriority := p_Prio3
      }
    },
    freqPriorityListGERAN := omit,
    freqPriorityListUTRA_FDD := omit,
    freqPriorityListUTRA_TDD := omit,
    bandClassPriorityListHRPD := omit,
    bandClassPriorityList1XRTT := omit,
    t320 := omit
  }

  template (value) RedirectedCarrierInfo cs_RedirectionInfo_Eutra ( ARFCN_ValueEUTRA p_EUTRA_DL_CarrierFreq ) :=
  { /* @status    APPROVED */
    eutra := p_EUTRA_DL_CarrierFreq
  }

  //new template for tc8138:
  template (value) RedirectedCarrierInfo cs_RedirectionInfo_GERAN ( ARFCN_ValueGERAN p_StartingARFCN,
                                                                    B1_Type p_BandIndicator,
                                                                    CoOrd_GERANNeighCell_Type p_NeighCell_Type ) := {
    geran := {
      startingARFCN := p_StartingARFCN,
      bandIndicator := f_ConvertGERAN_Band( p_BandIndicator ),
      followingARFCNs := {
        explicitListOfARFCNs := fl_ConvertGERANNeighCell (p_NeighCell_Type )//explicitListOfARFCNs
      }
    }
  };

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

  /*
   * @desc      Test case 8.1.3.1 RRC / RRC Connection Release: Success
   * @status    APPROVED
   */
  function f_TC_8_1_3_1_EUTRA ( ) runs on EUTRA_PTC
  {
    var ReleaseCause v_ReleaseCause := other;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var float v_WaitTime; //Time to allow UE to move to RRC_IDLE

    f_EUTRA_Init ( c1 );

    v_WaitTime := f_EUTRA_SetTimerToleranceMax ( eutra_Cell1, 5.0 );

    //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 ( eutra_Cell1);

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Transmit RRC Connection Release message
    f_EUTRA_RRC_ConnectionRelease ( eutra_Cell1, v_RRC_TI, v_ReleaseCause );

    //@siclog "Step 2" siclog@
    //Wait for 5 seconds
    f_Delay ( v_WaitTime );

    //@siclog "Step 3" siclog@
    //Check if UE is in RRC_IDLE
    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.3.4
   * @status    APPROVED
   */
  function f_TC_8_1_3_4_EUTRA ( ) runs on EUTRA_PTC
  {
    var ReleaseCause v_ReleaseCause := other;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var CarrierFreqEUTRA v_CarrierFreq_Cell1, v_CarrierFreq_Cell23;
    var ChannelBandwidthDependency_Type v_ChannelBandwidthDependency;
    var Ul_Bandwidth_Type v_UL_Bandwidth := f_ConvertDL_BandwidthToUL(px_ePrimaryBandChannelBandwidth);
    var Dl_Bandwidth_Type v_DL_Bandwidth := px_ePrimaryBandChannelBandwidth;

    //Initialise all cell, security and mobile parameters
    //* SIB5 is init. in f_EUTRA_InitSystemInformation5 with default values according to 36.508 cl. 6.3.1.2
    f_EUTRA_Init ( c3 );

    //Get band dependent parameters
    v_ChannelBandwidthDependency := f_EUTRA_BandDependentParam ( v_DL_Bandwidth, v_UL_Bandwidth );

    //Get cell specific parameter
    v_CarrierFreq_Cell23 := f_EUTRA_CellInfo_GetEARFCN ( eutra_Cell23 );
    v_CarrierFreq_Cell1 := f_EUTRA_CellInfo_GetEARFCN ( eutra_Cell1 );

    //Set System Information for Cell 1 & 23 according to Table 8.1.3.4.3.3-3.
    //* This will overwrite the default SIB5 init. by function f_EUTRA_Init.
    f_EUTRA_CellInfo_SetSIB5 ( eutra_Cell1, cs_508_SystemInformationBlockType5_Def ( { cs_InterFreqCarrierFreq_CRPri ( v_CarrierFreq_Cell23,
                                                                                                                       v_ChannelBandwidthDependency.AllowedMeasBandwidth,
                                                                                                                       omit ) } ) );

    f_EUTRA_CellInfo_SetSIB5 ( eutra_Cell23, cs_508_SystemInformationBlockType5_Def ( { cs_InterFreqCarrierFreq_CRPri ( v_CarrierFreq_Cell1,
                                                                                                                        v_ChannelBandwidthDependency.AllowedMeasBandwidth,
                                                                                                                        omit ) } ) );

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );
    f_EUTRA_CellConfig_Def ( eutra_Cell23 );
    
    //Bring UE to initial state
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );
    f_EUTRA_RbEst_Def ( eutra_Cell1 );
    
    //Set inital cell power level for Cell 1 and Cell 23
    //* Cell power levels are selected according to [18] so that
    //* camping on Cell 1 is guaranteed according to 36.523-1 cl. 8.1.3.4.3.1
    f_EUTRA_SetCellPower ( eutra_Cell23, tsc_SuitableCellRS_EPRE, cs_TimingInfo_Now );
    
    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Transmit RRCConnectionRelease
    f_EUTRA_RRC_ConnectionRelease_Common ( eutra_Cell1,
                                           cs_RRCConnectionRelease ( v_RRC_TI,
                                                                     v_ReleaseCause,
                                                                     cs_RedirectionInfo_Eutra ( v_CarrierFreq_Cell23.dl_CarrierFreq ),
                                                                     omit ) );
    
    //@siclog "Step 2" siclog@
    //Check if UE camps on Cell 23
    f_EUTRA_508CheckCampOnNewEutraCell ( eutra_Cell23 );
    
    f_EUTRA_TestBody_Set ( false );
    
    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell23, E1_IDLE );
  }

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

    var ReleaseCause v_ReleaseCause := other;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    //Time to allow UE to move to RRC_IDLE
    var float v_WaitTime := 5.0;
    var CarrierFreqEUTRA v_CarrierFreq_Cell1, v_CarrierFreq_Cell3, v_CarrierFreq_Cell6;
    var ReselectionThreshold v_ThreshXHigh_Cell6 := 11;
    var ChannelBandwidthDependency_Type v_ChannelBandwidthDependency;
    var Ul_Bandwidth_Type v_UL_Bandwidth := f_ConvertDL_BandwidthToUL(px_ePrimaryBandChannelBandwidth);
    var Dl_Bandwidth_Type v_DL_Bandwidth := px_ePrimaryBandChannelBandwidth;
    var template (value) CellPowerList_Type v_CellPowerList_AtT1 := {
      cs_CellPower ( eutra_Cell3, -73 ),
      cs_CellPower ( eutra_Cell6, -73 )
    };

    //Initialise all cell, security and mobile parameters
    //* SIB5 is init. in f_EUTRA_InitSystemInformation5 with default values according to 36.508 cl. 6.3.1.2
    f_EUTRA_Init (c3);

    //Get band dependent parameters
    v_ChannelBandwidthDependency := f_EUTRA_BandDependentParam (v_DL_Bandwidth, v_UL_Bandwidth);

    //Get cell specific parameters
    v_CarrierFreq_Cell1 := f_EUTRA_CellInfo_GetEARFCN (eutra_Cell1);
    v_CarrierFreq_Cell3 := f_EUTRA_CellInfo_GetEARFCN (eutra_Cell3);
    v_CarrierFreq_Cell6 := f_EUTRA_CellInfo_GetEARFCN (eutra_Cell6);

     //Set SIB5 information accordig to Table 8.1.3.5.3.3-2
    f_EUTRA_CellInfo_SetSIB5 (eutra_Cell1, cs_508_SystemInformationBlockType5_Def ( { cs_InterFreqCarrierFreq (v_CarrierFreq_Cell3,
                                                                                                               v_ChannelBandwidthDependency.AllowedMeasBandwidth),
                                                                                      cs_InterFreqCarrierFreq_ThreshXHigh (v_CarrierFreq_Cell6,
                                                                                                                           v_ChannelBandwidthDependency.AllowedMeasBandwidth,
                                                                                                                           v_ThreshXHigh_Cell6)}));

    //Set maximum cell powel level for Cell 1, 3 & 6 to be used in creation
    f_EUTRA_CellInfo_InitMaxReferencePower (eutra_Cell3, -73);
    f_EUTRA_CellInfo_InitMaxReferencePower (eutra_Cell6, -73);

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

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

    //Set initial cell power level according to T0
    //Power levels are already according to T0

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Transmit RRCConnectionRelease
    f_EUTRA_RRC_ConnectionRelease_Common (eutra_Cell1,
                                          cs_RRCConnectionRelease ( v_RRC_TI,
                                                                    v_ReleaseCause,
                                                                    omit,
                                                                    cs_IdleModeMobCntrlInfo_3Entries ( v_CarrierFreq_Cell1.dl_CarrierFreq,
                                                                                                       4,
                                                                                                       v_CarrierFreq_Cell3.dl_CarrierFreq,
                                                                                                       1,
                                                                                                       v_CarrierFreq_Cell6.dl_CarrierFreq,
                                                                                                       5 ) ) );

    //@siclog "Step 2" siclog@
    //Wait 5 s. for UE to enter RRC_IDLE
    f_Delay (v_WaitTime);

    //@siclog "Step 3" siclog@
    //Set RS EPRE of Cell 3 & Cell 6 according to T1
    f_EUTRA_SetCellPowerList (v_CellPowerList_AtT1, cs_TimingInfo_Now);

    //@siclog "Step 4" siclog@
    //Void

    //@siclog "Step 5" siclog@
    //Check if UE is in E-UTRA RRC_IDLE state
    f_EUTRA_508CheckCampOnNewEutraCell (eutra_Cell6);

    f_EUTRA_TestBody_Set (false);

    //Switch/power off UE
    f_EUTRA_Postamble (eutra_Cell6, E1_IDLE);

    //Remove the non-camping cells
    f_EUTRA_ReleaseAllCells( );
  }

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

    var boolean v_TriggerRxd := false;
    var ReleaseCause v_ReleaseCause := other;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var template (value) RedirectedCarrierInfo v_RedirectedCarrierInfo;
    var IRAT_CoOrd_SysInfo_Type v_UTRANSysInfo;
    var EUTRA_FDD_TDD_Mode_Type v_FDD_TDD_Cell1;

    //Initialise all cells, security and mobile parameters
    f_EUTRA_Init(c4);

    //Get FDD or TDD mode
    v_FDD_TDD_Cell1 := f_EUTRA_CellInfo_GetFDD_TDD (eutra_Cell1);

    //Set maximum cell powel level for Cell 1
    f_EUTRA_CellInfo_InitMaxReferencePower (eutra_Cell1, -75);

    //Coordinate with UTRAN component to receive UTRAN frequencies
    v_UTRANSysInfo := f_WaitForIRATCoOrd_SysInfo (UTRAN);

    //Set Sys. info for Cell 1 according to Table 6.2.3.5.3.2-2
    //* cellReselectionPriority is already set to 3 in initialisation (cs_CarrierFreqUTRA_FDD)

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

    //Bring UE to initial state
    f_EUTRA_Preamble_InterRAT(eutra_Cell1, STATE2_IDLEUPDATE, UTRAN);
    f_EUTRA_RbEst_Def (eutra_Cell1);

    //Set cell power level according to T0
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
    f_EUTRA_SetCellPower (eutra_Cell1, -75, cs_TimingInfo_Now);

    //Set the Redirect Information in the RRCConnectionRelease message (step 1)
    if ( f_EUTRA_CellInfo_GetFDD_TDD ( eutra_Cell1 ) == FDD ) {//Since we do not have mixed FDD/TDD InterRat environment.
        v_RedirectedCarrierInfo := cs_RedirectionInfo_UtraFdd ( v_UTRANSysInfo.Utran[0].FDD.Freq );//[0] corresponds to Utran cell 5 according to the Init-function.
    }
    else {
        v_RedirectedCarrierInfo := cs_RedirectionInfo_UtraTdd ( v_UTRANSysInfo.Utran[0].TDD.Freq );
    }

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Transmit RRC Connection Release message
    f_EUTRA_RRC_ConnectionRelease_Common ( eutra_Cell1,
                                           cs_RRCConnectionRelease ( v_RRC_TI,
                                                                     v_ReleaseCause,
                                                                     v_RedirectedCarrierInfo,
                                                                     omit ) );

    //Wait for end of test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );

    f_EUTRA_TestBody_Set ( false );

    //Remove the non-camping cell
    f_EUTRA_ReleaseAllCells( );
  }
  
  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.1.3.7
   * @status
   */
  function f_TC_8_1_3_7_EUTRA ( ) runs on EUTRA_PTC {
  
    var boolean v_TriggerRxd := false;

    //Initialise all cells, security and mobile parameters
    f_EUTRA_Init(c4);//SIB2, SIB3 & SIB6 are transmitted

    //Send Cell information to UTRA PTC
    f_IRAT_Send_EUTRA_SysInfoToOtherPort(UTRAN, f_EUTRA_CellInfo_GetEARFCN_DL(eutra_Cell1));

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );
    
    //Since Authentication parameters were updated in the idle updated procedure...
    //...receive updated parameters...
    f_EUTRA_InterRAT_InitialiseAuthParams(UTRAN);
    
    //Wait for end of preamble and then set power levels according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    f_EUTRA_SetCellPower ( eutra_Cell1, tsc_SuitableCellRS_EPRE, cs_TimingInfo_Now );
    
    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 18" siclog@
    //Check that UE camps on Cell 1
    f_EUTRA_InterRAT_InitialiseAuthParams(UTRAN);
    f_EUTRA_IdleMode_TAU_UponMobility_FromOtherRAT ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( false );
    
    //Tell UTRA PTC that test case is over
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);

    //Perform postamble
    f_EUTRA_Postamble( eutra_Cell1, E1_IDLE );
  }

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

    var boolean v_TriggerRxd := false;
    var ReleaseCause v_ReleaseCause := other;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var template (value) RedirectedCarrierInfo v_RedirectedCarrierInfo;
    var IRAT_CoOrd_SysInfo_Type v_GERANSysInfo;
    var integer v_T0_Cell_Specific_RS_EPRE_Cell1 := -75;

    //Initialise all cells, security and mobile parameters
    f_EUTRA_Init( c5 );//EUTRA-GERAN configuration

    //Set maximum cell powel level for Cell 1
    f_EUTRA_CellInfo_InitMaxReferencePower ( eutra_Cell1, -75 );

    //Coordinate with GERAN component to receive GERAN frequencies
    v_GERANSysInfo := f_WaitForIRATCoOrd_SysInfo (GERAN);

    f_EUTRA_CellInfo_SetSIB7 ( eutra_Cell1,
                               cs_508_SystemInformationBlockType7_Def (
                                  { cds_CarrierFreqsInfoGERAN_CellReselectionPriority (
                                           cs_CarrierFreqsGERAN(
                                               v_GERANSysInfo.Geran[0].Arfcn,
                                               f_ConvertGERAN_Band(v_GERANSysInfo.Geran[0].BandIndicator),
                                               fl_ConvertGERANNeighCell(v_GERANSysInfo.Geran[0].NeighCells)
                                           ), 3
                                    )
                                  }
                               )
                             );

    //Initial trigger: tell GERAN side to finish preamble
    f_SendIRATCoOrd ( GERAN, cms_IRAT_Trigger );

    //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 ( eutra_Cell1 );

    //Set cell power level according to T0
    f_EUTRA_SetCellPower ( eutra_Cell1, v_T0_Cell_Specific_RS_EPRE_Cell1, cs_TimingInfo_Now );

    //Set the Redirect Information in the RRCConnectionRelease message (step 1)
    v_RedirectedCarrierInfo := cs_RedirectionInfo_GERAN ( v_GERANSysInfo.Geran[0].Arfcn,
                                                          v_GERANSysInfo.Geran[0].BandIndicator,
                                                          v_GERANSysInfo.Geran[0].NeighCells
                                                        );//[0] corresponds to Geran cell 24 according to the Init-function.

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Step 1: Transmit RRC Connection Release message
    f_EUTRA_RRC_ConnectionRelease_Common ( eutra_Cell1,
                                           cs_RRCConnectionRelease ( v_RRC_TI,
                                                                     v_ReleaseCause,
                                                                     v_RedirectedCarrierInfo,
                                                                     omit  ) );

    //First trigger: tell GERAN side to start with step2
    f_SendIRATCoOrd ( GERAN, cms_IRAT_Trigger );

    //Wait for end of test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( GERAN );

    f_EUTRA_TestBody_Set ( false );

    //Remove the non-camping cell
    f_EUTRA_ReleaseAllCells( );
  }
}
