/******************************************************************************/
// @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-16 23:11:35 +0200 (Thu, 16 Sep 2010) $
// $Rev: 3943 $
/******************************************************************************/
module InterRat_PLMNSelection_GERAN
{
//=========================================================================
// Imports
//=========================================================================

  import from GERAN_Component all;
  import from GERAN_CommonFunctions all;
  import from CommonIratDefs all;
  import from GERAN_Templates all;
  import from GERAN_TypeDefs all;
  import from UpperTesterFunctions all;
  import from CommonDefs all;
  import from NAS_24008Templates all;
  import from NAS_CommonTemplates all;
  import from GERAN_Parameters all;
  import from UTRAN_Parameters all;
  import from Parameters all;
  
//==============================================================================
// Functions
//==============================================================================

  /*
   * @desc
   * @status
   */
  function fl_GERAN_RAU_WithoutChannelReq ( GERAN_CellId_Type p_CellId,
                                            B1_Type p_FOR,
                                            B3_Type p_Type ) runs on GERAN_PTC
  {
    var G_LLC_DATAMESSAGE_IND v_LLCDataInd;
    var B3_Type v_UpdateType;
    var octetstring v_PLMN := f_GERAN_PLMN_Get(p_CellId);
    var O1_Type v_RAC := f_GERAN_RAC_Get(p_CellId);
    var O2_Type v_LAC := f_GERAN_LAC_Get(p_CellId);
    
    G_LLC.receive( car_G_LLC_UnitData_IND ( cr_G_RA_UpdReqAny ( cr_GMM_UpdateType ( p_FOR, p_Type),
                                                                cr_RAI_Any (omit),
                                                                cr_PTMSI_SignatureAny ifpresent, ?)))
      -> value v_LLCDataInd;

    v_UpdateType := v_LLCDataInd.unit.msg.rauReq.updateType.updateValue;

    if (v_UpdateType != '000'B){
      v_UpdateType := '001'B;
    }

    //Do Authentication
    f_GERAN_GPRS_Authentication (p_CellId);
    
    //Send RAU Accept
    f_DownlinkTBFEstablishment(p_CellId, 1);
    G_LLC.send(cas_G_LLC_UnitData_Req (f_GERAN_TLLI_Get(p_CellId),
                                       tsc_LLCSapi_GMM,
                                       tsc_LLC_PM,
                                       px_GSM_CipheringOnOff,
                                       cs_G_RA_UpdAcc3 (cs_GMM_UpdateResult( v_UpdateType ),
                                                        cs_RAI(omit,
                                                               cs_LAI(omit, v_PLMN, v_LAC),
                                                               v_RAC),
                                                        cs_PTMSI_Signature (px_PTMSI_SigDef),
                                                        cs_MobileIdTMSIorPTMSI ('18'O, px_PTMSI_Def),
                                                        omit )));
    //Receive RAU Complete Message
    f_UplinkTBFOnePhase (p_CellId);
    G_LLC.receive(car_G_LLC_UnitData_IND (cr_G_RA_UpdComplete));
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.1
   * @status
   */
  function f_TC_6_2_1_1_GERAN ( ) runs on GERAN_PTC {

    var boolean v_TriggerRxd := false;
    var integer v_T0_RF_SignalLevel_Cell24 := tsc_GERAN_ServingCell;
    var integer v_T3_RF_SignalLevel_Cell24 := tsc_GERAN_ChPwrLvl_Off;
    var octetstring v_PLMN13 := '002810'O; //PLMN13 according to Table 6.2.1.1.3.2-1: Cell PLMN identities
    timer t_IdleMode_GenericTimer_GERAN := 360.0;   //Idle mode generic timer according to 36.523-3 cl. 7.8

    //Initialise test case
    f_GERAN_Init ( EUTRA_UTRAN_GERAN );

    //Set PLMN according to Table 6.2.1.2.3.2-1: Cell PLMN identities
    f_GERAN_PLMN_Set ( geran_Cell24, v_PLMN13 );
    
    //Set RAC/LAC
    //LAC in geran_Cell24 is per default set to '0001'O according to cs_G_CommonCellConfigInfoDef
    //RAC in geran_Cell24 is per default set to 05'O according to cs_G_CommonCellConfigInfoDef

    //Create GERAN cell and broadcast System Infomation
    f_GERAN_CreateCell ( geran_Cell24 );
    f_GERAN_SetCellPowerLevel( geran_Cell24, tsc_GERAN_ChPwrLvl_Off );

    //Set cell power level according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (UTRAN);
    f_GERAN_SetCellPowerLevel( geran_Cell24, v_T0_RF_SignalLevel_Cell24 );
    
    //Tell EUTRA PTC to start test case
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );

    f_GERAN_TestBody_Set( true );
    
    //Receive updated Authentication parameters from EUTRA PTC
    f_GERAN_InterRAT_InitialiseAuthParams();

    //@siclog "Step 21" siclog@
    //Wait for CHANNEL REQUEST on cell 24
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);
    t_IdleMode_GenericTimer_GERAN.start;
    f_UplinkTBFOnePhase ( geran_Cell24 );
    t_IdleMode_GenericTimer_GERAN.stop;
    // @verdict UE sends a CHANNEL REQUEST on Cell 24
    f_GERAN_PreliminaryPass(__FILE__, __LINE__, "Test case 6.2.1.1 step 21");
    
    //@siclog "Step 22 - 31" siclog@
    //Perform RAU procedure on cell 24
    fl_GERAN_RAU_WithoutChannelReq ( geran_Cell24, '0'B, '001'B );
    
    //Since Authentication parameters have been updated. Send them to EUTRA PTC, who will send them to UTRAN PTC
    //FFS - no functionality for sending the authentication parameters from GERAN PTC to UTRAN PTC exists
    f_GERAN_SendAuthParameters();

    //@siclog "Step 32" siclog@
    //Check if PLMN13 is indicated by UE
    f_UT_PLMN_Check ( UT, "00281" );
    //* @verdict pass PLMN13 is indicated by UE
    f_GERAN_PreliminaryPass (__FILE__, __LINE__, "Test Case 6.2.1.1 Step 32");
    
    //@siclog "Step 34" siclog@
    //Adjust cell power levels according to T3
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );
    f_SendIRATCoOrd ( UTRAN, cms_IRAT_Trigger );
    f_GERAN_SetCellPowerLevel( geran_Cell24, v_T3_RF_SignalLevel_Cell24 );
    
    f_GERAN_TestBody_Set ( false );
    
    //Wait for end of test case and then release cell 24
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (UTRAN);
    f_GERAN_SS_CellRelease ( geran_Cell24 );
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.2.1.4
   * @status
   */
  function f_TC_6_2_1_4_GERAN ( ) runs on GERAN_PTC {

    var boolean v_TriggerRxd := false;
    var octetstring v_PLMN2 := '002110'O;

    //Initialise test case
    f_GERAN_Init ( EUTRA_UTRAN_GERAN );

    //Set PLMN according to Table 6.2.1.2.3.2-1: Cell PLMN identities
    f_GERAN_PLMN_Set ( geran_Cell24, v_PLMN2 );
    
    //Set RAC/LAC
    //LAC in geran_Cell24 is per default set to '0001'O according to cs_G_CommonCellConfigInfoDef
    //RAC in geran_Cell24 is per default set to 05'O according to cs_G_CommonCellConfigInfoDef

    //Create GERAN cell and broadcast System Infomation
    f_GERAN_CreateCell ( geran_Cell24 );
    f_GERAN_SetCellPowerLevel( geran_Cell24, tsc_GERAN_ChPwrLvl_Off );

    //Set cell power level according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);
    f_GERAN_SetCellPowerLevel( geran_Cell24, tsc_GERAN_ServingCell );
    
    //Tell EUTRA PTC to start test case
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );

    f_GERAN_TestBody_Set( true );

    //Wait for end of test case and then release cell 24
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);

    f_GERAN_TestBody_Set ( false );
    
    f_GERAN_SS_CellRelease ( geran_Cell24 );
  }
}
