/******************************************************************************/
// @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 19:00:20 +0200 (Wed, 08 Sep 2010) $
// $Rev: 3836 $
/******************************************************************************/

module InterRat_Handover_to_UTRA {

//----------------------------------------------------------------------
// Import part
//----------------------------------------------------------------------

  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_Component all;
  import from EUTRA_CommonProcedures all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_RRC_Templates all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_CommonDefs all;
  import from UTRAN_CapabilityFunctions all;
  import from CommonIratDefs all;
  import from EUTRA_SecuritySteps all;
  import from EPS_NAS_TypeDefs all;
  import from EUTRA_CellInfo all;
  import from EUTRA_IRATFunctions all;

//----------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------

  //* @desc Test case 8.4.1.2 Inter-RAT Handover / from E-UTRA to UTRA(PS) / Data
  function f_TC_8_4_1_2_EUTRA() runs on EUTRA_PTC {

    //cell config and preamble
    var IRAT_CoOrd_SysInfo_Type v_UTRANSysInfo;
    var integer v_UTRAN_ARFCN_Cell5;
    var integer v_UTRAN_ScrCode_Cell5;
    var integer v_UTRAN_Ul_ScrCode_Cell5;
    var integer v_UTRAN_ParametersID_Cell5;
    var NasCount_Type v_NasCountDL;
    var octetstring v_HandoverToUtranCommand;
    var octetstring v_Nas_SecurityParamFromEUTRA;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var boolean v_TriggerRxd := false;

    f_EUTRA_Init ( c4 );

    //* @desc Create and configure cell
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //* @desc Bring UE to RB Established (state 3)
    f_EUTRA_Preamble_InterRAT ( eutra_Cell1, STATE2_IDLEUPDATE, UTRAN );
    f_EUTRA_RbEst_Def ( eutra_Cell1 );

    f_EUTRA_TestBody_Set(true);

    //* @desc Wait for configuration of UTRAN cell and get Cell Parameters for UTRAN cell
    v_UTRANSysInfo := f_WaitForIRATCoOrd_SysInfo (UTRAN);

    if (ischosen (v_UTRANSysInfo.Utran[0].FDD))
    {
       v_UTRAN_ARFCN_Cell5 := v_UTRANSysInfo.Utran[0].FDD.Freq;
       v_UTRAN_ScrCode_Cell5 := v_UTRANSysInfo.Utran[0].FDD.ScramblingCode;
       v_UTRAN_Ul_ScrCode_Cell5 := 0;/* Hardcoded since CoOrd_UTRANSysInfoFDD_Type is an APPROVED object
                                      instead using the coordination message => v_UTRANSysInfo.Utran[0].FDD.Ul_ScramblingCode*/
       v_HandoverToUtranCommand := f_U_EncodeHandoverToUtran_64kPS_FDD ( v_UTRAN_Ul_ScrCode_Cell5,
                                                                         v_UTRAN_ScrCode_Cell5,
                                                                         v_UTRAN_ARFCN_Cell5 );
    }
    else
    {
       v_UTRAN_ARFCN_Cell5 := v_UTRANSysInfo.Utran[0].TDD.Freq;
       v_UTRAN_ParametersID_Cell5 := v_UTRANSysInfo.Utran[0].TDD.CellParametersID;
       // Function to encode handover to UTRAN in TDD
       v_HandoverToUtranCommand := f_U_EncodeHandoverToUtran_64kPS_TDD (v_UTRAN_ParametersID_Cell5,
                                                                        v_UTRAN_ARFCN_Cell5);
    }

    
    /* @desc nas-securityParamFromEUTRA. The 4 least significant bits of the NAS downlink COUNT value
       36.508, Table 4.6.1-6 */
    v_NasCountDL := f_EUTRA_SS_NAS_CountGetDL();
    v_Nas_SecurityParamFromEUTRA := substr( v_NasCountDL, 8, 1);


    //@siclog "Step 2" siclog@
    //The SS transmits a MobilityFromEUTRACommand message
    SRB.send ( cas_SRB1_RrcPdu_REQ ( eutra_Cell1,
                                     cs_TimingInfo_Now,
                                     cs_MobilityFromEutraCommand ( v_RRC_TI,
                                                                   v_HandoverToUtranCommand,
                                                                   v_Nas_SecurityParamFromEUTRA ) ) );

    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger); // Send trigger to UTRAN side for step3

    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN);  // wait for UTRAN side to finish
    f_EUTRA_TestBody_Set(false);

    //* @desc Release non camping cell
    f_EUTRA_ReleaseAllCells ( );
  }


  //* @desc Test case 8.4.1.4 Inter-RAT Handover / from E-UTRA to UTRA (HSDPA) / Data
  function f_TC_8_4_1_4_EUTRA() runs on EUTRA_PTC {

    var IRAT_CoOrd_SysInfo_Type v_UTRANSysInfo;
    var integer v_UTRAN_ARFCN_Cell5;
    var integer v_UTRAN_ScrCode_Cell5;
    var integer v_UTRAN_Ul_ScrCode_Cell5;
    var integer v_UTRAN_ParametersID_Cell5;
    var NasCount_Type v_NasCountDL;
    var octetstring v_HandoverToUtranCommand;
    var octetstring v_Nas_SecurityParamFromEUTRA;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var boolean v_TriggerRxd := false;

    f_EUTRA_Init ( c4 );

    //* @desc Create and configure cell
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //* @desc Bring UE to RB Established (state 3)
    f_EUTRA_Preamble_InterRAT ( eutra_Cell1, STATE2_IDLEUPDATE, UTRAN );
    f_EUTRA_RbEst_Def ( eutra_Cell1 );

    f_EUTRA_TestBody_Set(true);

    //* @desc Wait for configuration of UTRAN cell and get Cell Parameters for UTRAN cell
    v_UTRANSysInfo := f_WaitForIRATCoOrd_SysInfo (UTRAN);

    if (ischosen (v_UTRANSysInfo.Utran[0].FDD))
    {
       v_UTRAN_ARFCN_Cell5 := v_UTRANSysInfo.Utran[0].FDD.Freq;
       v_UTRAN_ScrCode_Cell5 := v_UTRANSysInfo.Utran[0].FDD.ScramblingCode;
       v_UTRAN_Ul_ScrCode_Cell5 := 0;/* Hardcoded since CoOrd_UTRANSysInfoFDD_Type is an APPROVED object
                                      instead using the coordination message => v_UTRANSysInfo.Utran[0].FDD.Ul_ScramblingCode*/
       v_HandoverToUtranCommand := f_U_EncodeHandoverToUtran_HSDPA_FDD( v_UTRAN_Ul_ScrCode_Cell5,
                                                                        v_UTRAN_ScrCode_Cell5,
                                                                        v_UTRAN_ARFCN_Cell5 );
    }
    else
    {
       v_UTRAN_ARFCN_Cell5 := v_UTRANSysInfo.Utran[0].TDD.Freq;
       v_UTRAN_ParametersID_Cell5 := v_UTRANSysInfo.Utran[0].TDD.CellParametersID;
       v_HandoverToUtranCommand := f_U_EncodeHandoverToUtran_HSDPA_TDD( v_UTRAN_ParametersID_Cell5,
                                                                        v_UTRAN_ARFCN_Cell5 );
    }

    /* @desc nas-securityParamFromEUTRA. The 4 least significant bits of the NAS downlink COUNT value
       36.508, Table 4.6.1-6 */
    v_NasCountDL := f_EUTRA_SS_NAS_CountGetDL();
    v_Nas_SecurityParamFromEUTRA := substr( v_NasCountDL, 8, 1);

    //@siclog "Step 2" siclog@
    //The SS transmits a MobilityFromEUTRACommand message
    SRB.send ( cas_SRB1_RrcPdu_REQ ( eutra_Cell1,
                                     cs_TimingInfo_Now,
                                     cs_MobilityFromEutraCommand ( v_RRC_TI,
                                                                   v_HandoverToUtranCommand,
                                                                   v_Nas_SecurityParamFromEUTRA ) ) );

    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger); // Send trigger to UTRAN side for step3

    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN);  // wait for UTRAN side to finish
    f_EUTRA_TestBody_Set(false);

    //* @desc Release non camping cell
    f_EUTRA_ReleaseAllCells ( );
  }
}
