// ***************************************************************************
// @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-15 09:01:11 +0200 (Wed, 15 Sep 2010) $
// * $Rev: 3883 $
// ***************************************************************************

module NAS_ServiceRequest_UTRAN {

  import from UTRAN_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from UTRAN_ASP_Definitions language "ASN.1:1997" all;  // get UTRAN_CellId_Type
  import from Parameters all;
  import from UTRAN_Component all;
  import from CommonIratDefs all;
  import from UTRAN_ConfigurationSteps all;
  import from UTRAN_CommonFunctions all;
  import from UTRANGERAN_CommonFunctions all;
  import from NAS_CommonTemplates all;
  import from NAS_24008Templates all;
  import from UTRAN_TypeDefs all;
  import from UTRAN_Templates all;
  import from UpperTesterFunctions all;
  import from NAS_AuxiliaryDefsAndFunctions all;


group LocalTempates {
  
}

//---------------------------------------------------------------------

/**
  * @desc Service Request / Rejected / IMSI invalid (multi RAT test case)
  */
function f_TC_9_3_1_4_UTRAN () runs on UTRAN_PTC
{

  /* Test Purpose (TP)
   * (1)
   * with { UE having sent a SERVICE REQUEST message }
   * ensure that {
   *   when { UE receives a SERVICE REJECT message with the EMM cause set to 'Illegal UE' }
   *   then { UE sets the EPS update status to EU3 ROAMING NOT ALLOWED, deletes any GUTI, last visited
   *   registered TAI, TAI list and KSI, considers the USIM as invalid for EPS services until switching off
   *   or the UICC containing the USIM is removed, enters the state EMM-DEREGISTERED }
   * }
   * (2)
   * with { UE having sent a SERVICE REQUEST message }
   * ensure that {
   *   when { UE receives a SERVICE REJECT message with the EMM cause set to 'Illegal UE' }
   *   then { UE handles the GMM parameters GMM state, GPRS update status, P-TMSI, P-TMSI signature,
   *   RAI and GPRS ciphering key sequence number and the MM parameters update status, TMSI, LAI and
   *   ciphering key sequence number }}
   */
  /* Pre-test conditions
   * System Simulator:
   * - Cell A and Cell B.
   * - If pc_UTRAN supported by UE, Cell 5.
   *    - Cell 5 belongs to LAI-1 and RAI-1(home PLMN)
   * - If pc_GERAN and NOT pc_UTRAN supported by UE, Cell 24.
   *    - Cell 24 belongs to LAI-1 and RAI-1(home PLMN)
   * - Cell A is "Serving cell" and Cell B, Cell 5 and Cell 24 are "non-Suitable cell".
   * UE:
   * Preamble:
   * - The UE is in state Registered, Idle mode (state 2) on Cell A according to [18].
  */
  
  fl_TC_9_3_1_4and5Body_UTRAN ();
  
 } // f_TC_9_3_1_4_UTRAN
 
//---------------------------------------------------------------------
/**
  * @desc Service Request / Rejected / Illegal ME
  */
function f_TC_9_3_1_5_UTRAN () runs on UTRAN_PTC
{

 /* Test Purpose (TP)
    (1)
    with { UE having sent a SERVICE REQUEST message }
    ensure that {
      when { UE receives a SERVICE REJECT message with the EMM cause set to 'Illegal ME' }
        then { UE sets the EPS update status to EU3 ROAMING NOT ALLOWED, deletes any GUTI, last visited registered TAI, TAI list and KSI, considers the USIM as invalid for EPS services until switching off or the UICC containing the USIM is removed, enters the state EMM-DEREGISTERED }
                }

    (2)
    with { UE supporting A/Gb mode or Iu mode and having sent a SERVICE REQUEST message }
    ensure that {
      when { UE receives a SERVICE REJECT message with the EMM cause set to 'Illegal ME' }
        then { UE sets the GPRS update status to GU3 ROAMING NOT ALLOWED, deletes any P-TMSI, P-TMSI signature, TMSI, LAI, RAI and GPRS ciphering key sequence number and considers the SIM/USIM as invalid for GPRS services until switching off or the SIM/USIM is removed }
    }


    System Simulator:
    -   Cell A and Cell B.
    -   If pc_UTRAN supported by UE, Cell 5.
    -   Cell 5 belongs to LAI-1 and RAI-1(home PLMN)
    -   If pc_GERAN and NOT pc_UTRAN supported by UE, Cell 24.
    -   Cell 24 belongs to LAI-1 and RAI-1(home PLMN)
    -   Cell A is "Serving cell" and Cell B, Cell 5 and Cell 24 are "non-Suitable cell".
    UE:
    -   The test USIM contains IMSI-1, GUTI-1 and TAI-1, and EPS update status is "EU1: UPDATED".
    -   The test USIM contains P-TMSI-1 (belonging to RAI-1) and the GPRS update status is "GU1: UPDATED"
    -   The test USIM contains TMSI-1 (belonging to LAI-1) and the update status is "U1: UPDATED"
    Preamble:
    -   The UE is in state Registered, Idle mode (state 2) on Cell A according to [18].

    Note: all pre-test descriptions are the same as in TC9.3.1.4
 */

    fl_TC_9_3_1_4and5Body_UTRAN ();
    
  } // f_TC_9_3_1_5_UTRAN

//---------------------------------------------------------------------
function fl_TC_9_3_1_4and5Body_UTRAN () runs on UTRAN_PTC
 {
  var boolean v_TriggerRxd := false;
  var RRC_DATA_IND v_RRC_DataInd;
  var octetstring v_PLMN := f_U_Asn2Nas_PlmnId(f_UTRAN_CellInfo_GetPLMNIdentity(utran_Cell5));
  //var O1_Type v_RAC := f_UTRAN_CellInfo_GetRAC(utran_Cell5);
  //var O2_Type v_LAC := f_UTRAN_CellInfo_GetLAC(utran_Cell5);
  var START_Value v_START_Value := '00000000000000000000'B;
  var boolean v_NewKeyFlag := true;

  f_UTRAN_Init(EUTRA_UTRAN);

  f_SS_CreateCellDCH(utran_Cell5);
  f_UTRAN_SendDefSysInfo (utran_Cell5);

  // Pre-Preamble on UTRAN cell first to set PTSMI, PTSMI Sig and TMSI in USIM
  f_UTRAN_Pre_Preamble (utran_Cell5);
  f_UTRAN_InterRAT_InitialiseAuthParams();  // The UE starts in EUTRA, so wait for it to send the Auth Params

  f_UTRAN_SetCellPower (utran_Cell5, tsc_AttenuationNonSuitableCell);
  f_UTRAN_TestBody_Set( true );

  v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(EUTRA); // Wait for 1st trigger from EUTRA side
  //@siclog "Step 6Aa1" siclog@
  f_UTRAN_SetCellPower (utran_Cell5, tsc_AttenuationServingCell);
  
  //@siclog "Step 6Aa2 - 6Aa3" siclog@
  f_UTRAN_CheckNoAttachOnCell( utran_Cell5,  30.0, "Step 6Aa3");
  
  //@siclog "Step 7" siclog@
  f_UTRAN_SetCellPower (utran_Cell5, tsc_AttenuationNonSuitableCell);
  f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger); // Send 1st trigger to EUTRA side

  //@siclog "Step 31a2" siclog@
  v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(EUTRA); // Wait for 2nd trigger from EUTRA side
  f_UTRAN_SetCellPower (utran_Cell5, tsc_AttenuationServingCell);

  //+   The UE is powered on or switched on.
  f_UT_SwitchOnUE(UT, false);

  // The TMSI Status is dependant on CS FallBack
  //@siclog "Step31a3 - 31a9 " siclog@
  // f_UTRAN_GMM_RAU( utran_Cell5 ); Former stuff
  // f_UTRAN_GMMOnly_Attach ( utran_Cell5 );
  //@siclog "Step 31a5" siclog@
  // Check: Does the UE send an ATTACH REQUEST?
  f_UTRAN_RRC_ConnEst(utran_CellDedicated, est_Reg, registration); // Establish RRC connection
  alt {
    [] U_Dc.receive(car_PS_InitDirectTransfer(utran_CellDedicated,
                                              tsc_RB3,
                                              cr_U_AttachReqIMSInoTMSI(cr_AttachTypeAny,
                                                                       f_Imsi2MobileIdentity(px_IMSI_Def),
                                                                       cr_RAI (omit, cr_LAI(omit, v_PLMN, 'FFFE'O), ?))))
      -> value v_RRC_DataInd
      {
        f_UTRAN_PreliminaryPass (__FILE__, __LINE__, "Step 15a5");
        v_START_Value := v_RRC_DataInd.ttcn_start;
        f_SS_SecurityDownloadStart(ps_domain, v_START_Value);
      }
  }
  
  //@siclog "Step 31a6 - 31a7" siclog@
  f_GMM_Authentication();
  f_RRC_Security(utran_CellDedicated,                        // SECURITY MODE COMMAND, SECURITY MODE COMPLETE
                  v_NewKeyFlag,
                  ps_domain);
  
  //  Attach Accept, Attach Complete
  //@siclog "Step 31a8 - 31a9" siclog@
  f_UTRAN_AttachAccept(utran_CellDedicated);
  
  f_UTRAN_RRC_ConnRel(utran_CellDedicated, cell_Dch);

  // Test case now finished on UTRAN side
  f_UTRAN_TestBody_Set( false );
  f_UTRA_Postamble ( utran_Cell5, U1_IDLE );
  //  f_UTRAN_GMM_DetachOnSwitchOff ( utran_Cell5 );
  //  f_UTRAN_ReleaseCell ( utran_Cell5 );
  f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger); // Send 2nd trigger to EUTRA side

 } // fl_TC_9_3_1_4and5Body_UTRAN

} // module NAS_ServiceRequest_UTRAN
