/******************************************************************************/
// @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 CSG_Testcases_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_Component all;
  import from UTRAN_CellInfo all;
  import from UTRAN_ConfigurationSteps all;
  import from UTRAN_CommonFunctions all;
  import from UTRAN_Templates all;
  import from UTRAN_TypeDefs all;
  import from UTRAN_CapabilityFunctions all;
  
  import from EUTRA_CapabilityFunctions all;
  import from UpperTesterFunctions all;
  import from CommonIratDefs all;
  import from Parameters all;
  
  import from RRC_Others_UTRAN all;//FFS - for template cs_Utra_PriorityInfoList_Prio
  import from NAS_AuxiliaryDefsAndFunctions all; //FFS - for type Common_AuthenticationParams_Type
  
//==============================================================================
// Functions
//==============================================================================

  function f_UTRAN_SendAuthParameters() runs on UTRAN_PTC {
  
    var UTRAN_SecurityInfo_Type v_SecurityInfo;
    var Common_AuthenticationParams_Type v_AuthCS;
    
    // Now get the security parameters that have been updated
    v_SecurityInfo := f_UTRAN_Security_Get ();
    // AuthCS not the same structure as AuthPS
    v_AuthCS := v_SecurityInfo.authKeys.AuthPS;
    v_AuthCS.CK := v_SecurityInfo.authKeys.AuthCS.CKey;
    v_AuthCS.IK := v_SecurityInfo.authKeys.AuthCS.IKey;
    v_AuthCS.KeySeq := v_SecurityInfo.authKeys.AuthCS.CKeySeq;

    
    EUTRA.send (cms_IRAT_AuthInfo(v_SecurityInfo.authKeys.AuthPS,
                                  v_AuthCS,
                                   omit));  // may need to send this eventually
  
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 6.3.3
   * @status
   */
  function f_TC_6_3_3_UTRAN ( ) runs on UTRAN_PTC {
  
    var UTRAN_FDD_TDD v_FDD_TDD_Mode;
    var IRAT_CoOrd_SysInfo_Type v_EUTRASysInfo;
    var integer v_CPICH_Minus60, v_PCCPCH_Minus62, v_CPICH_Minus70, v_PCCPCH_Minus72;
    var boolean v_TriggerRxd := false;
    var UtraServingCellPriorityValue_Type v_UtraPriorityValue := 5;
    var EUTRA_MeasurementBandwidth v_EUTRA_MeasurementBandwidth :=
      f_UTRAN_ConvertIntegerToEUTRA_MeasurementBandwidth(f_ConvertDL_BandwidthPIXITToInteger());
    v_CPICH_Minus60 := -60;
    v_PCCPCH_Minus62 := -62;
    v_CPICH_Minus70 := -70;
    v_PCCPCH_Minus72 := -72;

    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);
    /*
     FFS - is this fulfilled: "Cell 5 and Cell 7 have different Routing Area."
     From 36.523-1 cl. 6.0.1:
     "If no RAI for UTRAN cell is explicitly specified in the test case description, the RAI for UTRAN cells indicated in Table 4.4.4-2 in [18] are used in the test case."
    */
    
    //Get FDD or TDD mode
    v_FDD_TDD_Mode := f_UTRAN_CellInfo_GetFDD_TDD (utran_Cell5);
    
    //Send the cell info to the EUTRA component
    f_UTRAN_Send_IR_SysInfoToEUTRA ( utran_Cell5 );
    
    //Get Cell Parameters for EUTRA cell
     v_EUTRASysInfo := f_WaitForIRATCoOrd_SysInfo (EUTRA);
     
    //Update SIB19 of cell 5 according to Table 6.2.3.3.3.3-1
    f_UTRAN_SysInfo_SetSIB19 ( utran_Cell5, cs_SIB19_UtraAndOneEutraEntry (cs_Utra_PriorityInfoList_Prio (v_UtraPriorityValue),
                                                                           cs_Eutra_FreqAndPriList_OneEntry_Def (v_EUTRASysInfo.Eutra[0].Arfcn,
                                                                                                                 v_EUTRA_MeasurementBandwidth)));

    f_UTRAN_SysInfo_SetSIB19 ( utran_Cell7, cs_SIB19_UtraAndOneEutraEntry (cs_Utra_PriorityInfoList_Prio ( v_UtraPriorityValue ),
                                                                           cs_Eutra_FreqAndPriList_OneEntry_Def (v_EUTRASysInfo.Eutra[0].Arfcn,
                                                                                                                 v_EUTRA_MeasurementBandwidth)));
    
    //Create and configure Cell 5 & 7
    //Cell started switched on with default power level, -60 and max attenuation
    f_SS_CreateCellFACH ( utran_Cell5 );
    f_SS_CreateCellFACH ( utran_Cell5 );
    
    //Start sending System Information
    f_UTRAN_SendDefSysInfo ( utran_Cell5 );
    f_UTRAN_SendDefSysInfo ( utran_Cell7 );
    
    if ( pc_Allowed_CSG_list ) {
      //Autonumous procedure to empty the UE's Allowed CSG list is FFS - for now use USIM with service n86 available
        /* 24.301 Annex C
         * The following EMM parameters shall be stored on the USIM if the corresponding file is present:
         * ...
         * - Allowed CSG list; and
         * ...
         * If the corresponding file is not present on the USIM, these EMM parameters except allowed CSG list are stored
         * in a non-volatile memory in the ME together with the IMSI from the USIM.
         * The allowed CSG list is stored in a non-volatile memory in the ME if the UE supports CSG selection.
        */
        /* Questions:
         * 1. The UE will consider its Allowed CSGs List as valid until it receives a new list from the network
              (e.g. in the next normal tracking area update or periodic tracking area update) or it is commanded by
              the network to delete all TACs in the Allowed CSGs List.
              How can UE be commanded to delete all TACs in the Allowed CSG list?
         * 2. Is the Allowed CSG List cleared at power down?
         * 3. Is there any mapping between TAI list and Allowed CSG list => CSG Mobility list?
        */
        /* Assumptions
          24.801 cl. 10.12.2.4
           "The working assumption is that the allowed CSG list shall be stored in the UICC and,
            as an implementation option, can be stored additionally in the ME. If stored in the ME,
            when the ME detects that the UICC has been changed, the CSG list will be deleted from the ME."
          24.801 cl. 10.12.3.4
           "The UE will store the Allowed CSGs list either in the ME or in the USIM and it will consider
            its Allowed CSGs list as valid until it receives a new list in the next EMM procedure or it is commanded
            by the network to delete all entries in the Allowed CSGs list"
        */
      //Insert a USIM containing default values expect service n86 (TS 31.102) is "available", but the Allowed CSG List shall be empty
      f_UT_USIM_Insert ( UT, "USIM with serivce n86 available, but the Allowed CSG List shall be empty" );
    }
    
    //Set power levels according to T0
    f_UTRAN_SetCellPower(utran_Cell5, v_CPICH_Minus60, v_PCCPCH_Minus62);
    
    f_UTRAN_TestBody_Set ( true );
    
    //@siclog "Step 1" siclog@
    //The UE is switched on.
    f_UT_SwitchOnUE ( UT, false );
    
    //@siclog "Step 2-13" siclog@
    //Perform registration procedure
    f_UTRAN_GMMOnly_Attach (utran_Cell5);
    
    //Since Authentication parameters have been updated in the registration process, send them to EUTRA PTC
    f_UTRAN_SendAuthParameters();
    
    //@siclog "Step 17" siclog@
    //Changes Cell 1, Cell 2, Cell 5 and Cell 7 levels according to row "T2" in table 6.3.3.3.2-1 and table 6.3.3.3.2-2.
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);
    f_UTRAN_SetCellPower(utran_Cell7, v_CPICH_Minus60, v_PCCPCH_Minus62);
    f_UTRAN_SetCellPower ( utran_Cell5, tsc_AttenuationCellOFF );

    //@siclog "Step 18-29" siclog@
    //Perform RAU procedure on cell 7
    f_UTRAN_GMM_RAU_MappedContext(utran_Cell7);

    //@siclog "Step 30" siclog@
    //Change Cell 1, Cell 2, Cell 5 and Cell 7 levels according to row "T3" in table 6.3.3.3.2-1 and table 6.3.3.3.2-2.
    f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger);
    f_UTRAN_SetCellPower(utran_Cell5, v_CPICH_Minus60, v_PCCPCH_Minus62);
    f_UTRAN_SetCellPower(utran_Cell7, v_CPICH_Minus70, v_PCCPCH_Minus72);

    //@siclog "Step 31-43" siclog@
    //Perform RAU procedure on cell 5
    f_UTRAN_GMM_RAU_MappedContext(utran_Cell5);
    
    f_UTRAN_TestBody_Set ( false );
    
    //Wait for confirmation that test case has ended
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);
    
    //Release the UTRA cells
    f_UTRAN_ReleaseCell ( utran_Cell5 );
    f_UTRAN_ReleaseCell ( utran_Cell7 );
  }
}
