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

  import from UTRAN_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from UTRAN_ASP_Definitions language "ASN.1:1997" all;
  import from UTRAN_CommonFunctions all;
  import from UTRAN_Component all;
  import from UTRAN_Templates all;
  import from UTRAN_ASN1_Templates all;
  import from UTRAN_CellInfo all;
  import from UTRAN_TypeDefs all;
  import from UTRAN_RAB_Functions all;
  import from UTRAN_RAB_Templates all;
  import from UTRAN_Parameters all;
  import from UTRAN_ConfigurationSteps all;

  import from UpperTesterFunctions all;
  import from NAS_CommonTypeDefs all;
  import from NAS_24008TypeDefs all;
  import from NAS_24008Templates all;
  import from CommonIratDefs all;
  import from CommonDefs all;

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

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.2.1
   * @status
   */
  function f_TC_6_2_2_1_UTRAN ( ) runs on UTRAN_PTC {

    var boolean v_TriggerRxd := false;
    var integer v_T1_CPICH_Cell5 := -70;
    var integer v_T1_PCCPCH_Cell5 := -72;

    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);

    //Create and configure Cell 5.
    //Cell started switched on with default cell power level and max attenuation
    f_SS_CreateCellFACH (utran_Cell5);

    //Start sending System Information
    f_UTRAN_SendDefSysInfo (utran_Cell5);
    
    //Since UE starts in EUTRA wait for EUTRA PTC to send the Auth Params
    f_UTRAN_InterRAT_InitialiseAuthParams();

    f_UTRAN_TestBody_Set(true);

    //@siclog "Step 1" siclog@
    //Set cell power level according to T1
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);
    f_UTRAN_SetCellPower(utran_Cell5, v_T1_CPICH_Cell5, v_T1_PCCPCH_Cell5);

    //@siclog "Step 2" siclog@
    //Check if UE camps on Cell 5
    f_UTRAN_IdleMode_RAU ( utran_Cell5 );

    //@siclog "Step 3" siclog@
    //Set power level of Cell 1 according to row "T2" in table 6.1.2.3.3.2-1 & cell 5 according to row "T0" in table 6.1.2.3.3.2-2.
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );
    f_UTRAN_SetCellPower ( utran_Cell5, tsc_AttenuationCellOFF );

    //@siclog "Step 6" siclog@
    //Set Cell 5 power level according to T1
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( EUTRA );
    f_UTRAN_SetCellPower(utran_Cell5, v_T1_CPICH_Cell5, v_T1_PCCPCH_Cell5);

    //@siclog "Step 7" siclog@
    //Check if UE camps on Cell 5
    f_UTRAN_IdleMode_RAU ( utran_Cell5 );

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

    f_UTRAN_TestBody_Set ( false );

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

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.2.5
   * @status
   */
  function f_TC_6_2_2_5_UTRAN ( ) runs on UTRAN_PTC {

    var UTRAN_FDD_TDD v_FDD_TDD_Mode;
    var UTRAN_CellInfo_Type_FDD v_CellInfoFDD;
    var UTRAN_CellInfo_Type_TDD v_CellInfoTDD;
    var RLC_TR_DATA_IND v_RLC_TR_DATA_IND;
    var InitialUE_Identity v_InitialUE_Id;
    var U_RNTI v_U_RNTI;
    var template (value) TI v_TI_R, v_TI_S;
    var RRC_DATA_IND v_RRC_DataInd;
    var integer v_T0_CPICH_Cell5 := -90;
    var integer v_T0_PCCPCH_Cell5 := -92;
    var template (present) EstablishmentCause v_EstCause := emergencyCall;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI;
    var START_Value v_START_Value := '00000000000000000000'B;
    var KeySeq v_KeySeq := '111'B; //cipher key sequence number IE: no key is available

    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);

    //Get cell specific parameters
    v_FDD_TDD_Mode := f_UTRAN_CellInfo_GetFDD_TDD ( utran_Cell5 );
    v_U_RNTI := f_UTRAN_CellInfo_GetU_RNTI ( utran_Cell5 );

    //Get FDD/TDD specific information
    if( v_FDD_TDD_Mode == UTRAN_FDD ) {
      v_CellInfoFDD := f_UTRAN_CellInfo_GetFDDSpecific (utran_Cell5);
    }
    else {
      v_CellInfoTDD := f_UTRAN_CellInfo_GetTDDSpecific (utran_Cell5);
    }

    //Create and configure Cell 5
    //Cell started switched on with default power level, -60 and max attenuation
    f_SS_CreateCellDCH ( utran_Cell5 );

    //Start sending System Information
    f_UTRAN_SendDefSysInfo ( utran_Cell5 );

    //Set cell power level for test
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );
    f_UTRAN_SetCellPower(utran_Cell5, v_T0_CPICH_Cell5, v_T0_PCCPCH_Cell5);

    //Bring UE to initial state
    f_UT_USIM_Remove ( UT );

    f_UTRAN_TestBody_Set( true );

    //@siclog "Step 1" siclog@
    //Switch on the UE
    f_UT_SwitchOnUE ( UT, false );

    //@siclog "Step 2" siclog@
    //Wait 60s for UE to complete cell selection
    f_Delay ( 60.0 );

    //@siclog "Step 3" siclog@
    //Make the initate an Emergency call
    f_UT_RequestCSCall ( UT, px_EmergencyCallNumber );

    //@siclog "Step 4" siclog@
    //Receive RRC Connection Request on Cell 5
    U_TM.receive ( car_RRC_ConnReq ( utran_Cell5,
                                     tsc_RB0,
                                     cdr_RRC_ConnReq_r8OrLater ( v_EstCause ) ) ) -> value v_RLC_TR_DATA_IND;
    //* @verdict pass UE transmits RRC Connection Request
    f_UTRAN_PreliminaryPass (__FILE__, __LINE__, "Test case 6.2.2.5 Step 4");
    v_InitialUE_Id := v_RLC_TR_DATA_IND.tM_message.uL_CCCH_Message.message_.rrcConnectionRequest.initialUE_Identity;

    //@siclog "Step 5" siclog@
    //Transmit RRC Connection Setup
    if ( v_FDD_TDD_Mode == UTRAN_FDD ) {
        U_UM.send ( cas_RRC_ConnSetup ( utran_Cell5,
                                    tsc_RB0,
                                    cs_108_RRC_ConnSetupDCH_r8_FDD ( v_InitialUE_Id,
                                                                 v_RRC_TI,
                                                                 v_CellInfoFDD.priScrmCode,
                                                                 v_U_RNTI,
                                                                 v_CellInfoFDD.uL_ScramblingCode ) ) )
    }
    else if ( v_FDD_TDD_Mode == UTRAN_TDD ){
        U_UM.send ( cas_RRC_ConnSetup ( utran_Cell5,
                                    tsc_RB0,
                                    cs_108_RRC_ConnSetupDCH_r8_TDD ( v_InitialUE_Id,
                                                                 v_RRC_TI,
                                                                 v_U_RNTI,
                                                                 v_CellInfoTDD.cellParametersID ) ) )
    }
    else {
      FatalError(__FILE__, __LINE__, "Cell is neither FDD nor TDD");
    }

    //Set Cell configuration
    f_UTRAN_CellInfo_SetConfigType ( utran_Cell5, cell_DCH_StandAloneSRB );

    //@siclog "Step 6" siclog@
    //Receive RRC Connection Setup Complete
    f_UTRAN_RRC_ReceiveConnSetupCmpl ( utran_Cell5 );

    //@siclog "Step 7" siclog@
    //Receive an INITIAL DIRECT TRANSFER message on Cell 5. This message includes a CM SERVICE REQUEST.
    U_Dc.receive ( car_InitDirectTransfer ( utran_Cell5,
                                            tsc_RB3,
                                            cr_U_ServReqAny_Common ( v_KeySeq ) ) ) -> value v_RRC_DataInd;

    v_START_Value := v_RRC_DataInd.ttcn_start;
    f_SS_SecurityDownloadStart ( cs_domain, v_START_Value );

    //@siclog "Step 8" siclog@
    //Transmit CM Service Accept
    U_Dc.send ( cs_DataReq ( utran_Cell5,
                             tsc_RB3,
                             cs_U_CM_ServAcc ) );

    //@siclog "Step 9" siclog@
    //Receive Emergency Setup message
    U_Dc.receive ( car_UplinkDirectTransfer ( utran_Cell5,
                                              tsc_RB3,
                                              cr_U_ESetup ( cr_ESetup ( cr_BcapSpeechMO ))))
    -> value v_RRC_DataInd;
    v_TI_R := v_RRC_DataInd.msg.esetup.ti;
    v_TI_S := v_TI_R;
    v_TI_S.tiFlag := '1'B;

    //@siclog "Step 10" siclog@
    //Transmit Call Proceeding message
    U_Dc.send ( cs_DataReq ( utran_Cell5,
                             tsc_RB3,
                             cs_U_CallProc ( v_TI_S )));

    //@siclog "Step 11 - 12" siclog@
    //Transmit Radio Bearer Setup message and Receive Radio Bearer Setup Complete message
    f_UTRAN_Setup_RAB_Speech ( utran_Cell5, false );

    //@siclog "Step 13" siclog@
    //Transmit Alerting message
    U_Dc.send ( cs_DataReq ( utran_Cell5,
                             tsc_RB3,
                             cs_U_Alert ( v_TI_S )));

    //@siclog "Step 14" siclog@
    //Transmit Connect message
    U_Dc.send ( cs_DataReq ( utran_Cell5,
                             tsc_RB3,
                             cs_U_Connect ( v_TI_S )));

    //@siclog "Step 15" siclog@
    //Receive Connect Acknowledge message
    U_Dc.receive ( car_UplinkDirectTransfer ( utran_Cell5,
                                              tsc_RB3,
                                              cr_U_ConnAck ( v_TI_R ) ) );

    //Tell EUTRA component test case is over
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );

    f_UTRAN_TestBody_Set ( false );

    //Release RAB, switch/power off UE and release cell
    f_UTRA_Postamble ( utran_Cell5, U5_CSCALL );
  }
}
