// ***************************************************************************
// @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-17 11:39:57 +0200 (Fri, 17 Sep 2010) $
// * $Rev: 3955 $
// ***************************************************************************

module NAS_Detach {

import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
import from EUTRA_Component all;
import from EUTRA_CommonDefs all;
import from EUTRA_ASP_SrbDefs all;
import from EUTRA_CommonProcedures all;
import from EUTRA_NASSteps all;
import from EUTRA_NASCommonFunctions all;
import from EPS_NAS_Templates all;
import from EUTRA_NASCommonTemplates all;
import from EUTRA_AspCommon_Templates all;
import from EUTRA_SRB_Templates all;
import from EUTRA_SecurityFunctions all;
import from NAS_CommonTypeDefs all;
import from NAS_CommonTemplates all;
import from EPS_NAS_TypeDefs all;
import from EPS_NAS_MsgContainers all;
import from EPS_NAS_Constants all;
import from EUTRA_AuxiliaryFunctions all;
import from EUTRA_RRCSteps all;
import from EUTRA_ConfigurationSteps all;
import from UpperTesterFunctions all;
import from EUTRA_SecuritySteps all;
import from EUTRA_RRC_Templates all;
import from Parameters all;
import from EUTRA_Paging all;
import from EUTRA_Timing all;
import from NAS_AuxiliaryDefsAndFunctions all;
import from EUTRA_CellInfo all;

group LocalTemplates
{

template NAS_UL_Message_Type cr_DETACH_REQUEST_NormalDetachandEpsOnly (template (present) NAS_KeySetIdentifier p_NasKeySetId,
                                                                       template (present) MobileIdentity p_MobileId) :=
  /* @status    APPROVED */
  cr_508_DETACH_REQUEST_MO(p_NasKeySetId,
                           p_MobileId,
                           cr_DetachType('0'B, tsc_DetachMO_EpsOnly));

template NAS_UL_Message_Type cr_DETACH_REQUEST_CombineEPSIMSI_Detach (template (present) NAS_KeySetIdentifier p_NasKeySetId,
                                                                      template (present) MobileIdentity p_MobileId) :=
  cr_508_DETACH_REQUEST_MO(p_NasKeySetId,
                           p_MobileId,
                           cr_DetachType('0'B, tsc_DetachMO_Combined));

template NAS_UL_Message_Type cdr_TAU_Request_combinedupdating_IMSIattach(EPS_UpdateTypeValue p_EpsUpdate_TypeValue,
                                                                         NAS_KsiValue p_KsiValue,
                                                                         template AdditionalUpdateType p_AdditionalUpdateType,
                                                                         template (value) MobileIdentity p_OldGuti,
                                                                         template (omit) LocAreaId p_LAI,
                                                                         NAS_PlmnId p_PlmnId,
                                                                         NAS_Tac    p_Tac)
  modifies cr_508_TAU_Request :=
{
   tRACKING_AREA_UPDATE_REQUEST := {
     oldGuti       := p_OldGuti,
     lastVisitedRegisteredTai   := cr_TAI(p_PlmnId, p_Tac),
     oldLai                     := p_LAI,
     tmsiStatus                 := cr_TMSI_Status('1'B)
   }
};

}// end of local_templates

group TestcaseImplementations
{

///////////////////////////////////////////////////////////////////////////////
// TC 9.2.2.1.1
///////////////////////////////////////////////////////////////////////////////
/*
 * @desc      REFERENCE TS 36.523-1 clause 9.2.2.1.1
 * @status    APPROVED
 */
function f_TC_9_2_2_1_1_EUTRA() runs on EUTRA_PTC
{
  // @desc UE initiated detach procedure / UE switched off.
  /* Test Purpose (TP)
   * (1)
   * with { UE in EMM-REGISTERED state }
   *   ensure that {
   *     when { the UE is switched off }
   *       then { the UE establishes the RRC connection with the RRC establishmentCause set to 'mo-Signalling' and sends DETACH REQUEST message, keeps the native security context, and deactivates the EPS bearer context(s) locally }
   *   }
   */
  
  // System Simulator:
  //   Cell A (HPLMN)
  
  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);
  
  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);

  // Preamble: UE is in state Registered, Idle (state 2) on Cell
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_1_1_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);  //@sic R5-103849, R5-105005 sic@
}

/*
 * @desc      Test body of test case 9.2.2.1.1
 * @status    APPROVED
 */
function fl_TC_9_2_2_1_1_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_Guti := f_EUTRA_CellInfo_GetGuti ( eutra_CellA );
  // var ReleaseCause v_RRC_ReleaseCause := other;
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  //v_AttachType and AdditionalUpdateType are introduced here to follow TTCN3 v411 standard
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType(  ); // R5-103673
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
  // temporary variable which is used when receiving NAS ASPs
  var NAS_MSG_Indication_Type v_NasInd;

  // ==========================================================================

  //+   Cause the UE to switch or power off
  //@siclog "Step 1" siclog@
  f_UT_SwitchOffUE (UT, false);

  //+  Check: does the UE transmit an RRCConnectionRequest message with establishmentCause set
  //         to mo-Signalling followed by a DETACH REQUEST with the Detach Type IE indicating "switch off"? [1,P]
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoSignalling,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected, // @sic R5s090312 sic@
                                                               cr_508_DETACH_REQUEST_MO(cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                        f_GutiParameters2MobileIdentity(omit, v_Guti),
                                                                                        cr_DetachType( '1'B, v_AttachType)))); // R5-103675
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
  
  //+ Void  @sic R5-102247 sic@
  //@siclog "Step 3 - 7 Void" siclog@
  f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);
  
  //+  The UE is switched on.
  //@siclog "Step 8" siclog@
  f_UT_SwitchOnUE (UT, false);
  
  //+  The UE transmits an ATTACH REQUEST message.
  //@siclog "Step 9" siclog@
  v_NasInd:= f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                            tsc_RRC_TI_Def,
                                            cr_EstablishmentCause_Any,
                                            cr_NAS_IndicationWithPiggybacking(tsc_SHT_IntegrityProtected,
                                                                              cdr_ATTACH_REQUEST_WithEPSnativeSecurityCtx(v_AttachType,
                                                                                                                          v_AdditionalUpdateType,
                                                                                                                          v_SecurityParams.KSIasme),
                                                                              cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach,
                                                                                                              f_GetPdnType())));
  f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Step 9");
  
  // Bring UE into a stable state
  //@siclog "StepS 10 - 21" siclog@
  f_EUTRA_IdleUpdated_Step5_17 (eutra_CellA, STATE2_IDLEUPDATE, NORMAL, v_NasInd, false); // @sic R5-105005 sic@

} // function tc_9_2_2_1_1_EUTRA



/**
  * @desc UE initiated detach / USIM removed from the UE.
*/
function f_TC_9_2_2_1_2_EUTRA() runs on EUTRA_PTC
{

  /* Test Purpose (TP)
   (1)
   with { UE in EMM-REGISTERED }
   ensure that {
    when { the USIM is removed from the UE }
      then { the UE sends DETACH REQUEST message and indicates that the detach is for both EPS services and
        non-EPS services or for EPS services depending on the EPS attach type used }
              }
 */


  /* Pre-test conditions
  System Simulator:
      - Cell A (HPLMN).
      UE:
      None.
      Preamble:
      - UE is in state Registered, Idle mode (State 2) according to [18]
  */

  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);

  // Preamble: UE is in state Registered, Idle (state 2) on Cell
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_1_2_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E4_DEREGISTERED);
}

function fl_TC_9_2_2_1_2_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_Guti := f_EUTRA_CellInfo_GetGuti ( eutra_CellA );

  // var ReleaseCause v_RRC_ReleaseCause := other;
  var float v_NoReactionTime := f_EUTRA_SetTimerToleranceMax( eutra_CellA, 3.0 );
  // temporary variable which is used when receiving NAS ASPs
  var NAS_MSG_Indication_Type v_NasInd;

  // ==========================================================================

  //+   Cause removal of USIM from the UE without powering down
  //@siclog "Step 1" siclog@
  f_UT_USIM_Remove (UT);

  //+  Check: does the UE transmit a DETACH REQUEST with the Detach Type IE indicating "normal detach" and
  //          "combined EPS/IMSI detach" or "EPS detach" depending on the UE configuration?[1,P]
  //@siclog "Step 2" siclog@
 if (pc_CS) {
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                           tsc_RRC_TI_Def,
                           cr_EstablishmentCause_MoSignalling,
                           cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                             cr_DETACH_REQUEST_CombineEPSIMSI_Detach(cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                     f_GutiParameters2MobileIdentity(omit, v_Guti) )));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
  } else {
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                             tsc_RRC_TI_Def,
                             cr_EstablishmentCause_MoSignalling,
                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                               cr_DETACH_REQUEST_NormalDetachandEpsOnly(cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                               f_GutiParameters2MobileIdentity(omit, v_Guti) )));
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
}

  //+   SS responds with DETACH ACCEPT message
  //@siclog "Step 3" siclog@
    SRB.send (cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                    cs_TimingInfo_Now,
                                    cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_DETACH_ACCEPT)));


  //@siclog "Step 4" siclog@
  //+   Check: Does the test result of generic procedure in TS 36.508 subclause 6.4.2.5 indicate that the UE does not respond to paging
  //           when paged with GUTI-1 and with CN domain indicator set to "PS"?
  
 f_EUTRA_508Check_NASNoResponse ( eutra_CellA,
                       cs_NasPaging_OneRecord ( cs_PagingUE_Identity_S_TMSI ( v_Guti.MME_Code, v_Guti.M_TMSI )),
                       v_NoReactionTime );
} // function tc_9_2_2_1_2_EUTRA


///////////////////////////////////////////////////////////////////////////////
// TC 9.2.2.1.3
///////////////////////////////////////////////////////////////////////////////
function f_TC_9_2_2_1_3_EUTRA() runs on EUTRA_PTC
{
  // @desc UE initiated detach / EPS capability of the UE is disabled
  //
  //Test Purpose (TP)
  //(1)
  //with { UE in EMM-REGISTERED state }
  //ensure that {
  //  when { EPS capability of the UE is disabled  }
  //   then { UE sends the DETACH REQUEST message on the Cell registered and indicates that the detach
  //          is for EPS services }
  //       }

  // Pre-test conditions
  //      System Simulator:
  //         - Cell A is set to ''Serving cell''.
  //      UE:
  //          -     The test USIM contains GUTI-1(belonging to TAI-1) and the update status is "EU1: UPDATED".
  //      Preamble:
  //          - The UE is in state Registered, Idle mode (state 2) on Cell A according to [18].


  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);

 // Preamble: UE is in state Registered, Idle (state 2) on Cell
  f_EUTRA_Preamble (eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_1_3_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E4_DEREGISTERED);
}// function f_TC_9_2_2_1_3_EUTRA

function fl_TC_9_2_2_1_3_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_Guti := f_EUTRA_CellInfo_GetGuti ( eutra_CellA );
  var NAS_MSG_Indication_Type v_NAS_Ind;
  var float v_NoReactionTime := f_EUTRA_SetTimerToleranceMax( eutra_CellA, 3.0 );

  //+  Cause EPS capability of the UE to be disabled.
  //@siclog "Step 1" siclog@
  f_UT_DisableEPS(UT);


  // + The UE transmits a DETACH REQUEST message with the Detach type IE indicating
  //       'EPS detach' and 'normal detach'?
  // @siclog "Step 2" siclog@
  v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoSignalling,
                                             cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                                cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                          f_GutiParameters2MobileIdentity(omit, v_Guti)
                                             )));

  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2: DETACH REQUEST message received from UE");

  //+ The SS responds the DETACH ACCEPT message.
  //@siclog "Step 3" siclog@
  SRB.receive(car_SRB2_NasPdu_IND(
              eutra_CellA,
              cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                cr_508_DETACH_ACCEPT)));


  //+ Check: Does the test result of generic procedure in TS 36.508 subclause 6.4.2.5
  //         indicate that the UE does not respond to paging when paged with GUTI-1 and with CN domain
  //         indicator set to "PS"?
  //@siclog "Step 4" siclog@
  f_EUTRA_508Check_NASNoResponse ( eutra_CellA,
                                 cs_NasPaging_OneRecord ( cs_PagingUE_Identity_S_TMSI ( v_Guti.MME_Code, v_Guti.M_TMSI )),
                                 v_NoReactionTime );

} // function tc_9_2_2_1_3_EUTRA


///////////////////////////////////////////////////////////////////////////////
// TC 9.2.2.1.6
///////////////////////////////////////////////////////////////////////////////
/*
 * @desc      REFERENCE TS 36.523-1 clause 9.2.2.1.6
 * @status    APPROVED
 */
function f_TC_9_2_2_1_6_EUTRA() runs on EUTRA_PTC
{
  // @desc UE initiated detach / Abnormal case / local detach after 5 attempts due to no network response.
  //
  //Test Purpose (TP)
  //(1)
  //with { UE in EMM-REGISTERED state }
  //ensure that {
  //   when { the UE receives no response to the UE initiated DETACH REQUEST }
  //   then { the UE re-transmits the DETACH REQUEST up to 4 times on the expiry of timer T3421 }
  //}

  //(2)
  //with { UE in EMM-REGISTERED state }
  //ensure that {
  //  when { the UE receives no response to the UE initiated DETACH REQUEST }
  //   then { the UE aborts the detach procedure and perform local detach on the 5th expiry of timer T3421 }
  //}


  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);

 // Preamble: UE is in state Registered, Idle (state 2) on Cell
  f_EUTRA_Preamble (eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_1_6_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E4_DEREGISTERED); // @sic R5-103875 sic@
}

/*
 * @desc      Test body of test case 9.2.2.1.6
 * @status    APPROVED
 */
function fl_TC_9_2_2_1_6_Body() runs on EUTRA_PTC
{
  var HalfOctet_Type v_EPS_Bearer_Id := tsc_EpsDefaultBearerId;
  var ProcedureTransactionIdentifier v_EPS_TI := tsc_PTI_1;
  var GutiParameters_Type v_Guti := f_EUTRA_CellInfo_GetGuti ( eutra_CellA );
  var integer v_Expiry_Number;

  // temporary variable which is used when receiving NAS ASPs
  var NAS_MSG_Indication_Type v_NasInd;

  timer t_T3421_SS := f_EUTRA_SetTimerToleranceMin(eutra_CellA, 15.0); // T3421 (TS 24.301 clause 10.2)- 10%
  timer t_Wait;

  //@siclog "Step 1" siclog@
  //+  Cause UE to initiate detach from the EPS services only.
  f_UT_InitiateDetach_EPSOnly(UT);

  //@siclog "Step 2" siclog@
  //+  The UE transmits a DETACH REQUEST message with the Detach type IE indicating
  //   'EPS detach' and 'normal detach'? The UE starts timer T3421.
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoSignalling,
                                             cr_NAS_Indication (tsc_SHT_IntegrityProtected, // @sic R5-103368 sic@
                                                                cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                          f_GutiParameters2MobileIdentity(omit, v_Guti) )));
 
  //+  The SS does not respond to the DETACH REQUEST message.
  
  //+  Check: When the timer T3421 expires does the UE re-transmit DETACH REQUEST message.
  //+  Timer T3421 is re-started (1st expiry).
  
  //+  The SS does not respond to the DETACH REQUEST message.
  
  //+  Checks: When the timer T3421 expires does the UE re-tranmit DETACH REQUEST message.
  //+  Timer T3421 is re-started (2nd expiry).
 
  //+  The SS does not respond to the DETACH REQUEST message.
  
  //+  Check: When the timer T3421 expires does the UE re-transmit DETACH REQUEST message.
  //+  Timer T3421 is re-started (3th expiry).
 
  //+  The SS does not respond to the DETACH REQUEST message.
 
  //+  Check: When the timer T3421 expires does the UE re-transmit DETACH REQUEST message.
  //+  Timer T3421 is re-started (4th expiry).
  //@siclog "Step 3 - 10" siclog@
  for (v_Expiry_Number := 1; v_Expiry_Number <= 4; v_Expiry_Number := v_Expiry_Number + 1)
    {
      t_T3421_SS.start;  //SS starts a timer to check that UE sends the Detach message after timeout.
      alt {
        
        [] SRB.receive ( car_SRB1_NasPdu_IND( eutra_CellA, ?)) { // receive anything before the timer expires and it's a fail R5s100189
          f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step " & int2str((v_Expiry_Number*2)+2));
          t_T3421_SS.stop;
        }
        [] t_T3421_SS.timeout {
          SRB.receive ( car_SRB1_NasPdu_IND( eutra_CellA,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected, // @sic R5-103368, R5s100189 sic@
                                                               cr_DETACH_REQUEST_NormalDetachandEpsOnly( cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                         f_GutiParameters2MobileIdentity(omit, v_Guti) ))));
          f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Step " & int2str((v_Expiry_Number*2)+2));
        }
     } // alt
  } // for

    //+  The SS does not respond to the DETACH REQUEST message.
 
  //+   When the timer T3421 expires the UE aborts the detach procedure and performs a local detach (5th expiry).
  //@siclog "Step 11 - 12" siclog@
  t_T3421_SS.start;
  alt {
    // if SS receives anything, it fails.
    []  any port.receive
      {
        f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 12");
        t_T3421_SS.stop;
      }
    [] t_T3421_SS.timeout
      {
        f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Step 12");
      }
  }

  //@siclog "Step 13" siclog@
  //+  The SS starts the EPS bearer context modification procedure using the previously allocated EPS bearer identity.
  SRB.send(cas_SRB1_NasPdu_REQ(eutra_CellA,                                        // @sic R5s100189 sic@
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_IntegrityProtected,          // @sic R5s100189 sic@
                                              cs_508_ModifyEPSBearerCxtReq(v_EPS_Bearer_Id,
                                                                           v_EPS_TI,
                                                                           cs_508_EPS_QoS_Dedicated_1, // @sic R5-103625 sic@
                                                                           cds_Tft_Dedicated_2_Modify)))); // @sic R5-103625 sic@

    //@siclog "Step 14" siclog@
  //    Check: does the UE transmit a MODIFY EPS BEARER CONTEXT ACCEPT
  //    message within the next 10 s?
  t_Wait.start(f_EUTRA_SetTimerToleranceMin(eutra_CellA, 10.0));
  alt {
    [] SRB.receive(car_SRB1_NasPdu_IND(eutra_CellA,                                   // @sic R5s100189 sic@
                                       cr_NAS_Indication(tsc_SHT_IntegrityProtected, // @sic R5s100189 sic@
                                                         cr_508_ModifyEPSBearerCxtAccept(v_EPS_Bearer_Id))))
      {
        f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 14");
        t_Wait.stop;
      }
    [] t_Wait.timeout {}
  }

} // function tc_9_2_2_1_6_EUTRA


//   **************************************************************
//                              TC_9_2_2_1_7_EUTRA
//   **************************************************************
function f_TC_9_2_2_1_7_EUTRA() runs on EUTRA_PTC
{
   // @desc UE initiated detach / Abnormal case / Detach procedure collision
   /* Test Purpose (TP)
    (1)
    with { UE in EMM-DEREGISTERED-INITIATED state }
    ensure that {
      when { the UE receives the DETACH REQUEST message from the network }
       then { the UE aborts the UE initiated detach procedure and completes the network initiated detach procedure }
    }

    (2)
    with { UE in EMM-DEREGISTERED state }
    ensure that {
      when { the UE has completed the network initiated detach procedure with re-attach required }
       then { the UE initiates the attach procedure }
    }

    System Simulator:
        -   Cell A configured according to Table 6.3.2.2-1 in [18].
    UE:
        None.
    Preamble:
        -   The UE is in state Registered, Idle mode (State 2) according to [18].
        -   UE has a valid GUTI-1 and is registered in TAI-1
    */

    f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

    // create cell and send out system information
    f_EUTRA_CellConfig_Def(eutra_CellA);

    //Go to state 2
    f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
    
    f_EUTRA_TestBody_Set(true);

    fl_TC_9_2_2_1_7_Body();

    // Postamble
    f_EUTRA_TestBody_Set(false);
    f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);
    
} // end f_TC_9_2_2_1_7_EUTRA


function fl_TC_9_2_2_1_7_Body() runs on EUTRA_PTC
{
    var NAS_MSG_Indication_Type v_NAS_Ind;
    var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
            
    var GutiParameters_Type v_GutiParams1 := f_EUTRA_CellInfo_GetGuti (eutra_CellA);
    var NAS_MSG_Indication_Type v_NasInd;

   
    timer t_T3421 := f_EUTRA_SetTimerToleranceMin(eutra_CellA, 15.0);

    // Cause UE to initiate detach from the EPS services.
    //@siclog "Step 1" siclog@
    f_UT_InitiateDetach_EPSOnly(UT);

    //@siclog "Step 2" siclog@
    //       The UE transmits a DETACH REQUEST message with the Detach type IE indicating
    //       'EPS detach' and 'normal detach'? The UE starts timer T3421.
    v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                               tsc_RRC_TI_Def,
                                               cr_EstablishmentCause_MoSignalling,
                                               cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                                  cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                            f_GutiParameters2MobileIdentity(omit, v_GutiParams1)
                                               )));

    //  The SS does not respond to the DETACH REQUEST message.
    //  With T3421 still running the SS shall send DETACH REQUEST message with the
    //         Detach type IE indicating "re-attach required".
    //@siclog "Step 3 - 4" siclog@
    t_T3421.start;  //SS starts a timer to check that UE sends the Detach message while T3421 is running
    SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                                 cs_TimingInfo_Now,
                                 cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                cs_508_DETACH_REQUEST_MT(cs_DetachType(tsc_DetachMT_ReAttach),
                                                                                       omit)
                                                )));

    if (t_T3421.running) {
      t_T3421.stop;
    }
    else {
      f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 3: Detach Request not sent within T3421");
    }

    //  Check: Does the UE transmit a DETACH ACCEPT message?
    //@siclog "Step 5" siclog@
    SRB.receive(car_SRB2_NasPdu_IND(
                eutra_CellA,
                cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                  cr_508_DETACH_ACCEPT)));

    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step5: Detach accept message sent");


    //  The SS releases the RRC connection
    //@siclog "Step 6" siclog@
    f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);

    //@siclog "Step 7" siclog@
    // Check: Does the UE ATTACH REQUEST message with a PDN CONNECTIVITY REQUEST message
    v_NasInd:= f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                  tsc_RRC_TI_Def,
                                  cr_EstablishmentCause_Any,
                                  cr_NAS_IndicationWithPiggybacking(tsc_SHT_IntegrityProtected,
                                        cr_508_ATTACH_REQUEST(tsc_EAttachType_Combined, v_AdditionalUpdateType),
                                        cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach,
                                                                        f_GetPdnType())));

    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 7");

    //@siclog "Step 8 - 19" siclog@
    f_EUTRA_IdleUpdated_Step5_17(eutra_CellA, STATE2_IDLEUPDATE, NORMAL, v_NAS_Ind, false);

}// end function fl_TC_9_2_2_1_7_Body


//   **************************************************************
//                              TC_9_2_2_1_8_EUTRA
//   **************************************************************
function f_TC_9_2_2_1_8_EUTRA() runs on EUTRA_PTC
{
   // @desc UE initiated detach / Abnormal case / Detach and EMM common procedure collision
   /* Test Purpose (TP)
    (1)
    with { UE in EMM-DEREGISTERED-INITIATED state due to switch off }
    ensure that {
      when { the UE receives an EMM common procedure message from the network }
       then { the UE ignores the message and continues the detach procedure }
    }

    (2)
    with { UE in EMM-DEREGISTERED-INITIATED state due to normal detach }
    ensure that {
      when { the UE receives GUTI REALLOCATION COMMAND from the network }
       then { the UE ignores the message and continues the detach procedure }
    }

    (3)
    with { UE in EMM-DEREGISTERED-INITIATED state due to normal detach }
    ensure that {
      when { the UE receives AUTHENTICATION REQUEST, SECURITY MODE COMMAND or IDENTITY REQUEST message from the network }
       then { the UE responds to the message and then continues the detach procedure }
    }


    System Simulator:
        -   Cell A configured according to Table 6.3.2.2-1 in [18].
    UE:
        None.
    Preamble:
        -   The UE is in state Registered, Idle mode (State 2) according to [18].
        -   UE has a valid GUTI-1 and is registered in TAI-1
    */

    f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

    // create cell and send out system information
    f_EUTRA_CellConfig_Def(eutra_CellA);

    //Go to state 2
    f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
    
    f_EUTRA_TestBody_Set(true);

    fl_TC_9_2_2_1_8_Body();

    // Postamble
    f_EUTRA_TestBody_Set(false);
    f_EUTRA_Postamble(eutra_CellA, E4_DEREGISTERED);
    
} // end f_TC_9_2_2_1_8_EUTRA


function fl_TC_9_2_2_1_8_Body() runs on EUTRA_PTC
{
    var NAS_MSG_Indication_Type v_NasInd;
    var SRB_COMMON_IND v_ReceivedAsp;
    var NasCount_Type v_NasCountUL;
    var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
    var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
    
    var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti (eutra_CellA);
    // create new GUTI with different S-TMSI
    var GutiParameters_Type v_Guti_2_Params := f_CreateGutiParamsWithUpdatedSTmsi(v_GutiParams);
    var template (value) MobileIdentity v_Guti;

    var float v_NoReactionTime := f_EUTRA_SetTimerToleranceMax( eutra_CellA, 3.0 );
    timer t_T3450 := f_EUTRA_SetTimerToleranceMin(eutra_CellA, 6.0);
    timer t_T3421 := f_EUTRA_SetTimerToleranceMin(eutra_CellA, 15.0);

    //  Cause switch off
    //@siclog "Step 1" siclog@
    f_UT_SwitchOffUE (UT, false);

    //The UE transmits a DETACH REQUEST message with the Detach type IE indicating "switch off"
    //@siclog "Step 2" siclog@
    SRB.receive(car_SRB1_NasPdu_IND(eutra_CellA,
                                   cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                     cr_508_DETACH_REQUEST_MO(cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                              f_GutiParameters2MobileIdentity(omit, v_GutiParams),
                                                                              cr_DetachType( '1'B, v_AttachType)
                                                                              ))));

    //  The SS transmits a GUTI REALLOCATION COMMAND message.
    //@siclog "Step 3" siclog@
    SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                     cs_TimingInfo_Now,
                                     cs_NAS_Request ( tsc_SHT_IntegrityProtected,
                                                      cs_508_GUTI_REALLOCATION_COMMAND(f_GutiParameters2MobileIdentity(tsc_IEI_Guti, v_Guti_2_Params),
                                                                                       omit)
                                                     )));


    //  Check: Does the UE transmit an GUTI REALLOCATION COMPLETE message?
    //@siclog "Step 4" siclog@
    t_T3450.start;
    alt {
      [] SRB.receive(car_SRB1_NasPdu_IND(eutra_CellA,
                                         cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                           cr_508_GUTI_REALLOCATION_COMPLETE)))
      {
         t_T3450.stop;
         f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 4: GUTI REALLOCATION COMPLETE");
       }
    [] t_T3450.timeout {}
    }


    //  Check: Does the test result of generic procedure in TS 36.508 subclause 6.4.2.5
    // indicate that the UE does not respond to paging when paged with GUTI-1 and with CN domain
    // indicator set to "PS"?
    //@siclog "Step 5" siclog@
    f_EUTRA_508Check_NASNoResponse ( eutra_CellA,
                                     cs_NasPaging_OneRecord ( cs_PagingUE_Identity_S_TMSI ( v_GutiParams.MME_Code, v_GutiParams.M_TMSI )),
                                     v_NoReactionTime );

    

    //  The UE is switched on and performs an ATTACH procedure.
    //@siclog "Step 6" siclog@
    f_UT_SwitchOnUE (UT, false );
    f_EUTRA_AttachProcedure (eutra_CellA, "No Verdict");


    // Cause UE to initiate detach from the EPS services only
    //@siclog "Step 7" siclog@
    f_UT_InitiateDetach_EPSOnly(UT);


    //  The UE transmits a DETACH REQUEST message with the Detach type IE indicating "EPS detach"
    //         and "normal detach"?
    //         The UE starts timer T3421.
    //@siclog "Step 8" siclog@
    v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                         tsc_RRC_TI_Def,
                                         cr_EstablishmentCause_MoSignalling,
                                         cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                            cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                      f_GutiParameters2MobileIdentity(omit, v_GutiParams) )));

    // Use GUTI-3 different from GUTI-2
    v_GutiParams.M_TMSI := oct2bit('33345678'O); //default is oct2bit('12345678'O)
    v_Guti := f_GutiParameters2MobileIdentity( tsc_IEI_Guti, v_Guti_2_Params);

    // The SS does not respond to the DETACH REQUEST message
    //  With T3421 still running the SS shall send GUTI REALLOCATION COMMAND.
    //@siclog "Step 9 - 10" siclog@
    t_T3421.start;  // SS starts a timer to check that UE sends the GUTI REALLOCATION COMMAND message
                    // while T3421 is running
    SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                     cs_TimingInfo_Now,
                                     cs_NAS_Request ( tsc_SHT_IntegrityProtected,
                                                      cs_508_GUTI_REALLOCATION_COMMAND(v_Guti,
                                                                                       omit))));

    //  Check: Does the UE transmit a GUTI REALLOCATION COMPLETE message?
    //@siclog "Step 11" siclog@
    if (t_T3421.running) {
      t_T3421.stop;
      t_T3450.start;
      alt {
        [] SRB.receive(car_SRB1_NasPdu_IND(eutra_CellA,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_GUTI_REALLOCATION_COMPLETE)))
        {
           t_T3450.stop;
           f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 11: GUTI REALLOCATION COMPLETE received");
        }
        [] t_T3450.timeout
        {
           f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 11");
        }
      }
    }
    else {
      f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 9: GUTI REALLOCATION COMMAND not sent within T3421");
    }


    // SS responds with DETACH ACCEPT message
    //@siclog "Step 12" siclog@
    SRB.send (cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                    cs_TimingInfo_Now,
                                    cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_DETACH_ACCEPT)));

    //  The SS releases the RRC connection
    //@siclog "Step 13" siclog@
    f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);


    //  Cause UE to initiate attach for the EPS services.
    //@siclog "Step 14" siclog@
    // The generic procedure in 36.508 clause 4.5.2.3-1 is executed so as to complete the attach procedure
    f_EUTRA_AttachProcedure (eutra_CellA, "No Verdict", EPS_Only);


    //  Cause UE to initiate detach from the EPS services only
    //@siclog "Step 15" siclog@
    f_UT_InitiateDetach_EPSOnly(UT);

    //  The UE transmits a DETACH REQUEST message with the Detach type IE indicating
    // "EPS detach" and "normal detach"? The UE starts timer T3421.
    //@siclog "Step 16" siclog@
    v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                         tsc_RRC_TI_Def,
                                         cr_EstablishmentCause_MoSignalling,
                                         cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                            cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                      f_GutiParameters2MobileIdentity(omit, v_GutiParams) )));


    t_T3421.start;
    //  The SS does not respond to the DETACH REQUEST message
    //  With T3421 still running the SS shall send AUTHENTICATION REQUEST
    //  Check: Does the UE transmit an AUTHENTICATION RESPONSE message?
    //@siclog "Step 17 - 19" siclog@
    v_SecurityParams := f_EUTRA_NAS_Authentication(eutra_CellA, tsc_SRB2,
                                                   v_SecurityParams,
                                                   tsc_SHT_IntegrityProtected_Ciphered,
                                                   tsc_SHT_IntegrityProtected_Ciphered,
                                                   f_Asn2Nas_PlmnId(v_GutiParams.PLMN_Identity));

    if (t_T3421.running) {
        t_T3421.stop;
        f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 19: AUTHENTICATION RESPONSE message received");
        }
      else {
        f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 19");
    }

    //  SS responds with DETACH ACCEPT message
    //@siclog "Step 20" siclog@
    SRB.send (cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                    cs_TimingInfo_Now,
                                    cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_DETACH_ACCEPT)));

    //  The SS releases the RRC connection
    //@siclog "Step 21" siclog@
    f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);

    //  Cause UE to initiate attach for the EPS services.
    //@siclog "Step 22" siclog@
    // The generic procedure in 36.508 clause 4.5.2.3-1 is executed so as to complete the attach procedure
    f_EUTRA_AttachProcedure (eutra_CellA, "No Verdict", EPS_Only);

    //  Cause UE to initiate detach from the EPS services only
    //@siclog "Step 23" siclog@
    f_UT_InitiateDetach_EPSOnly(UT);

    //  The UE transmits a DETACH REQUEST message with the Detach type IE indicating "EPS detach"
    //          and "normal detach"? The UE starts timer T3421
    //@siclog "Step 24" siclog@
    v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                         tsc_RRC_TI_Def,
                                         cr_EstablishmentCause_MoSignalling,
                                         cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                            cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                      f_GutiParameters2MobileIdentity(omit, v_GutiParams) )));



    f_EUTRA_SS_NAS_ConfigureSecurity(v_SecurityParams.NAS_Integrity, v_SecurityParams.NAS_Ciphering); /* download security keys and reset NAS COUNT */

    t_T3421.start;

    //  The SS does not respond to the DETACH REQUEST message.
    //  With T3421 still running the SS shall send SECURITY MODE COMMAND
    //@siclog "Step 25 - 26" siclog@

    // send NAS SECURITY MODE COMMAND (integrity protected acc. 24.301 cl. 5.4.3.2):
    SRB.send(cas_SRB_NasPdu_REQ(eutra_CellA,
                                tsc_SRB1,
                                cs_TimingInfo_Now,
                                cs_NAS_Request(tsc_SHT_IntegrityProtected_NewSecurityContext,
                                               cs_508_SECURITY_MODE_COMMAND(v_SecurityParams.NAS_Ciphering.Algorithm,
                                                                            v_SecurityParams.NAS_Integrity.Algorithm,
                                                                            v_SecurityParams.KSIasme,
                                                                            v_SecurityParams.NAS_SecurityCap))));

    if (t_T3421.running) {
        t_T3421.stop;
        }
      else {
        f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 25");
    }

    //  Check: Does the UE transmit a SECURITY MODE COMPLETE message?
    //@siclog "Step 27" siclog@
    // receive NAS SECURITY MODE COMPLETE (integrity protected and ciphered acc. 24.301 cl. 5.4.3.3):
    SRB.receive(car_SRB_NasPdu_IND(eutra_CellA,
                                   tsc_SRB1,
                                   cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered_NewSecurityContext,
                                                     cr_508_SECURITY_MODE_COMPLETE
                                   ))) -> value v_ReceivedAsp;

    v_NasCountUL := v_ReceivedAsp.Signalling.Nas[0].SecurityProtection.NasCount;
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Step 27");

    // SS responds with DETACH ACCEPT message.
    //@siclog "Step 28" siclog@
    SRB.send (cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                    cs_TimingInfo_Now,
                                    cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_DETACH_ACCEPT)));

    // The SS releases the RRC connection.
    //@siclog "Step 29" siclog@
    f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);

    // Cause UE to initiate attach for the EPS services.
    //@siclog "Step 30" siclog@
    // The generic procedure in 36.508 clause 4.5.2.3-1 is executed so as to complete the attach procedure.
    f_EUTRA_AttachProcedure (eutra_CellA, "No Verdict", EPS_Only);

    //  Cause UE to initiate detach from the EPS services only.
    //@siclog "Step 31" siclog@
    f_UT_InitiateDetach_EPSOnly(UT);

    // The UE transmits a DETACH REQUEST message with the Detach type IE indicating "EPS detach"
    //@siclog "Step 32" siclog@
    //          and "normal detach"? The UE starts timer T3421.
    v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                         tsc_RRC_TI_Def,
                                         cr_EstablishmentCause_MoSignalling,
                                         cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                            cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                      f_GutiParameters2MobileIdentity(omit, v_GutiParams) )));


    t_T3421.start;
    //  The SS does not respond to the DETACH REQUEST message.
    //  With T3421 still running the SS shall send IDENTITY REQUEST.
    //@siclog "Step 33 - 34" siclog@
    SRB.send(cas_SRB1_NasPdu_REQ(eutra_CellA,
                                 cs_TimingInfo_Now,
                                 cs_NAS_Request(tsc_SHT_NoSecurityProtection,
                                                cs_IDENTITY_REQUEST(tsc_IdType_IMSI))));

    if (t_T3421.running) {
        t_T3421.stop;
        }
      else {
        f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 34");
    }

    // Check: Does the UE transmit an IDENTITY RESPONSE message?
    //@siclog "Step 35" siclog@
    SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                    cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                      cr_IDENTITY_RESPONSE(f_Imei2MobileIdentity(px_IMEI_Def)))));

    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 35");

    //  SS responds with DETACH ACCEPT message.
    //@siclog "Step 36" siclog@
    SRB.send (cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                    cs_TimingInfo_Now,
                                    cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_DETACH_ACCEPT)));


}// end function fl_TC_9_2_2_1_8_Body


//   **************************************************************
//                              TC_9_2_2_1_9
//   **************************************************************
function f_TC_9_2_2_1_9_EUTRA() runs on EUTRA_PTC
{
   // @desc UE initiated detach / Abnormal case / Change of cell into a new tracking area
   /* Test Purpose (TP)
    (1)
    with { UE in EMM-DEREGISTERED-INITIATED state }
    ensure that {
      when { the UE changes into a new tracking area that is not in the stored TAI list }
       then { the UE aborts the detach procedure and initiates a Tracking Area Updating procedure }
    }

    (2)
    with { UE in EMM-TRACKING-AREA-UPDATING-INITIATED state }
    ensure that {
      when { the UE receives TRACKING AREA UPDATE ACCEPT message }
       then { the UE re-initiates the detach procedure after completing the Tracking Area Updating procedure }
    }


    System Simulator:
        -   Cell A and Cell B are configured according to Table 6.3.2.2-1 in [18].
        -   Cell A belongs to TAI-1 (home PLMN) and is set to ''Serving cell''
        -   Cell B belongs to TAI-2 (home PLMN, another TAC) and is set to ''Non-Suitable cell''
    UE:
        None.
    Preamble:
        -   The UE is in state Registered, Idle mode (State 2) according to [18].
        -   UE has a valid GUTI-1 and is registered in TAI-1


    */
    var template (value) CellPowerList_Type v_CellPowerList;

    f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN ,LTEmulticell);

    // create cell and send out system information
    f_EUTRA_CellConfig_Def(eutra_CellA);
    f_EUTRA_CellConfig_Def(eutra_CellB);

    //Go to state 2
    f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);

    // The SS pre-configures Cell A as the "Serving cell" and Cell B as "Non-Suitable cell"
    v_CellPowerList := {
      cs_CellPower ( eutra_CellA, tsc_ServingCellRS_EPRE ),
      cs_CellPower ( eutra_CellB, tsc_NonSuitableCellRS_EPRE )
    };
    f_EUTRA_SetCellPowerList ( v_CellPowerList, cs_TimingInfo_Now );
    
    f_EUTRA_TestBody_Set(true);

    fl_TC_9_2_2_1_9_Body();

    // Postamble
    f_EUTRA_TestBody_Set(false);
    f_EUTRA_Postamble(eutra_CellB, E4_DEREGISTERED);
    
} // end f_TC_9_2_2_1_9_EUTRA


function fl_TC_9_2_2_1_9_Body() runs on EUTRA_PTC
{
    var NAS_MSG_Indication_Type v_NAS_Ind;
    var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
    var template (value) CellPowerList_Type v_CellPowerList;
    
    var GutiParameters_Type v_GutiParams1 := f_EUTRA_CellInfo_GetGuti (eutra_CellA);
    var GutiParameters_Type v_GutiParams_B := f_EUTRA_CellInfo_GetGuti (eutra_CellB);
    var NAS_PlmnId v_PLMN := f_Asn2Nas_PlmnId (v_GutiParams_B.PLMN_Identity);
    var EPS_BearerContextStatus v_EpsBearerCtxtStatus;
    var float v_NoReactionTime := f_EUTRA_SetTimerToleranceMax( eutra_CellB, 3.0 );
    var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
  
    var TrackingAreaCode v_Tac := f_EUTRA_CellInfo_GetTAC ( eutra_CellB );
    var NAS_Lac v_LAC := f_EUTRA_CellInfo_GetLocationAreaCode ( eutra_CellB );
    var template (omit) LocAreaId v_LAI := f_GetLAI ( v_PLMN, v_LAC);
    var template (omit) MobileIdentity v_MSId := f_GetMSId ( );
    var template (value) TrackingAreaIdList v_TaiList := cds_TAIListNonConsecutive_tlv ( v_PLMN, { bit2oct(v_Tac) } );
   
    //  Cause UE to initiate detach from the EPS services.
    //@siclog "Step 1" siclog@
    f_UT_InitiateDetach_EPSOnly(UT);

    //       The UE transmits a DETACH REQUEST message with the Detach type IE indicating
    //       'EPS detach' and 'normal detach'? The UE starts timer T3421.
    //@siclog "Step 2" siclog@
    v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                               tsc_RRC_TI_Def,
                                               cr_EstablishmentCause_MoSignalling,
                                               cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                                  cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                            f_GutiParameters2MobileIdentity(omit, v_GutiParams1)
                                               )));


    // The SS does not respond to the DETACH REQUEST message.
    // Set the cell type of cell A to the "Non-Suitable cell".
    //         Set the cell type of cell B to the "Serving cell".
    // Note: T3421 value is specified as 15s in TS 24.301 and it is assumed that
    //       SS can configure cells within this time.
    //@siclog "Step 3 - 4" siclog@
    v_CellPowerList := {
      cs_CellPower ( eutra_CellA, tsc_NonSuitableCellRS_EPRE ),
      cs_CellPower ( eutra_CellB, tsc_ServingCellRS_EPRE )
    };
    f_EUTRA_SetCellPowerList ( v_CellPowerList, cs_TimingInfo_Now );


    //  Check: Does the UE transmit a TRACKING AREA UPDATE REQUEST message?
    //@siclog "Step 5" siclog@
    v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellB,
                                               tsc_RRC_TI_Def,
                                               cr_EstablishmentCause_Any,
                                               cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                                 cr_508_TAU_Request(tsc_EpsUpdate_TaUpdate,
                                                                                    f_EUTRA_SecurityKSIasme_Get(),
                                                                                    v_AdditionalUpdateType
                                                                                    )));

    v_EpsBearerCtxtStatus := v_NAS_Ind.Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.epsBearerContextStatus;
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 5: TAU Request sent by UE");


    // The SS transmits a TRACKING AREA UPDATE ACCEPT message.
    //@siclog "Step 6" siclog@
    SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellB,
                                     cs_TimingInfo_Now,
                                     cs_NAS_Request ( tsc_SHT_IntegrityProtected_Ciphered,
                                                      cs_508_TAU_Accept ( v_AttachType,
                                                                          f_GutiParameters2MobileIdentity (tsc_IEI_Guti, v_GutiParams_B ), //GUTI-2
                                                                          v_TaiList,
                                                                          v_EpsBearerCtxtStatus,
                                                                          v_LAI,
                                                                          v_MSId,
                                                                          f_GetAdditionalUpdateResult(v_NAS_Ind.Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.addUpdateType)))));

    // Check: Does the UE transmit a TRACKING AREA UPDATE COMPLETE message?
    //@siclog "Step 7" siclog@
    SRB.receive(car_SRB1_NasPdu_IND(eutra_CellB,
                                    cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                      cr_508_TAU_Complete)
                                   ));

    //  The UE transmits a DETACH REQUEST message with the Detach type IE indicating
    //         'EPS detach' and 'normal detach'? The UE starts timer T3421.
    //@siclog "Step 8" siclog@
    v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellB,
                                               tsc_RRC_TI_Def,
                                               cr_EstablishmentCause_MoSignalling,
                                               cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                                  cr_DETACH_REQUEST_NormalDetachandEpsOnly (cr_NAS_KeySetIdentifier (?, tsc_NasKsi_NativeSecurityContext),
                                                                                                            f_GutiParameters2MobileIdentity(omit, v_GutiParams_B)
                                               )));
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 8: Detach Request sent by UE");

    // SS responds with DETACH ACCEPT message
    //@siclog "Step 9" siclog@
    SRB.send (cas_SRB1_NasPdu_REQ ( eutra_CellB,
                                    cs_TimingInfo_Now,
                                    cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_DETACH_ACCEPT)
                                  ));

    f_EUTRA_RRC_ConnectionRelease(eutra_CellL, tsc_RRC_TI_Def, other);

     //  Check: Does the test result of generic procedure in TS 36.508 subclause 6.4.2.5 indicate that
     // the UE does not respond to paging when paged with GUTI-2 and with CN domain indicator set to "PS"?
     //@siclog "Step 10" siclog@
     f_EUTRA_508Check_NASNoResponse ( eutra_CellB,
                                      cs_NasPaging_OneRecord ( cs_PagingUE_Identity_S_TMSI ( v_GutiParams_B.MME_Code, v_GutiParams_B.M_TMSI )),
                                      v_NoReactionTime );

}// end function fl_TC_9_2_2_1_9_Body


///////////////////////////////////////////////////////////////////////////////
// TC 9.2.2.2.1
///////////////////////////////////////////////////////////////////////////////
/*
 * @desc      Network initiated detach procedure / re-attach required.
 *
 *            (1)
 *            with { UE in EMM-REGISTERED state }
 *            ensure that {
 *              when { SS sends DETACH REQUEST message with the Detach type IE 're-attach
 *                     required' }
 *              then { UE sends DETACH ACCEPT message and UE intiates an attach procedure }
 *            }
 *
 *            System Simulator:
 *              - Cell 1 (FDD or TDD).
 *              - Cell 1 belongs to TAI-1(MCC1/MNC1/TAC1).
 *            UE:
 *              - The UE has a valid GUTI (GUTI-1) and is registered in TAI-1
 *
 * @status    APPROVED
 */
function f_TC_9_2_2_2_1_EUTRA() runs on EUTRA_PTC
{
  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN,LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);

  // Preamble: UE is in state Generic RB established (state 3) on Cell 1
  f_EUTRA_Preamble (eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_RbEst_Def(eutra_CellA);
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_2_1_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED); //@sic R5-103849 sic@
}

/*
 * @desc      Test body of test case 9.2.2.2.1
 * @status    APPROVED
 */
function fl_TC_9_2_2_2_1_Body() runs on EUTRA_PTC
{
  // ==========================================================================
  // initialize constant test case data structures
  // ==========================================================================
  var ProcedureTransactionIdentifier v_EpsPti := tsc_PTI_1;
  // ==========================================================================

  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var GutiParameters_Type v_Guti1_Params := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var TrackingAreaCode v_TAC := f_EUTRA_CellInfo_GetTAC(eutra_CellA);
  var NAS_Lac v_LAC := f_EUTRA_CellInfo_GetLocationAreaCode(eutra_CellA);
  var NAS_PlmnId v_PLMN := f_Asn2Nas_PlmnId(v_Guti1_Params.PLMN_Identity);
  var template (omit) MobileIdentity v_MSId := f_GetMSId ( );
  var template (value) TrackingAreaIdList v_Tai1_List := cs_TAIListNonConsecutive_lv(f_Asn2Nas_PlmnId( v_Guti1_Params.PLMN_Identity),
                                                                                      { bit2oct(v_TAC) });
  var template (omit) LocAreaId v_LAI := f_GetLAI(v_PLMN,v_LAC);
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
  var NAS_MSG_Indication_Type v_NasInd;
  // create new GUTI with different S-TMSI (gets sent to UE in Attach Accept)
  var GutiParameters_Type v_Guti2_Params := f_CreateGutiParamsWithUpdatedSTmsi(v_Guti1_Params);
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();  //used to follow TTCN3 v411 standard
  var NasCount_Type v_NasCountUL;
  var template (omit) ProtocolConfigOptions v_Pco := omit;
  var template (omit) AccessPointName v_APN := omit;

  // ==========================================================================

  //+   The SS initiates Detach procedure with the Detach Type IE
  //+   "re-attach required"
  //@siclog "Step 1" siclog@
  SRB.send(cas_SRB2_NasPdu_REQ(
             eutra_CellA,
             cs_TimingInfo_Now,
             cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                            cs_508_DETACH_REQUEST_MT(cs_DetachType(tsc_DetachMT_ReAttach), omit))));

  //+   Check: Does the UE send DETACH ACCEPT message? [1,P]
  //@siclog "Step 2" siclog@
  SRB.receive(car_SRB2_NasPdu_IND(
                eutra_CellA,
                cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                  cr_508_DETACH_ACCEPT)));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");

  //+   The SS releases the RRC connection
  //@siclog "Step 3" siclog@
  f_EUTRA_RRC_ConnectionRelease_ImmediateReset(eutra_CellA, cs_RRCConnectionRelease(tsc_RRC_TI_Def, other)); // @sic R5s100135 sic@

  //+ Check: Does the UE send ATTACH REQUEST message? [1,P]
  // Note: Even if the UE is detached, according to TS24.301 clause 5.5.2.3.2 the UE
  //       still has a valid EPS security context, so the message is expected to be protected.
  //@siclog "Step 4" siclog@
  v_NasInd:= f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                  tsc_RRC_TI_Def,
                  cr_EstablishmentCause_Any,
                  cr_NAS_IndicationWithPiggybacking(tsc_SHT_IntegrityProtected,
                            cdr_ATTACH_REQUEST_WithGuti(
                                                        v_AttachType,
                                                        v_AdditionalUpdateType,
                                                        v_SecurityParams.KSIasme,
                                                        f_GutiParameters2MobileIdentity(omit, v_Guti1_Params),
                                                        cr_TAI_Any),
                            cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach,
                                                            f_GetPdnType())));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 4");
  v_EpsPti := v_NasInd.Pdu.PiggybackedPduList[0].Msg.pDN_CONNECTIVITY_REQUEST.procedureTransactionIdentifier; // Use this value in SS responses
  if (ispresent( v_NasInd.Pdu.PiggybackedPduList[0].Msg.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions)) { // @sic R5s100084 sic@
    v_Pco := v_NasInd.Pdu.PiggybackedPduList[0].Msg.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions;
  }

  // @sic R5s090312 sic@
  v_NasCountUL := v_NasInd.SecurityProtection.NasCount;

  // Steps 4A1&2 Optional ESM Information Transfer @sic R5s100172 sic@
  if (f_CheckEsmInfoTransferFlag(v_NasInd.Pdu.PiggybackedPduList[0].Msg.pDN_CONNECTIVITY_REQUEST)) {
    v_APN := f_ESM_InformationTransfer_GetAPN (eutra_CellA, v_EpsPti);
  }
  v_SecurityParams := f_EUTRA_RRC_ActivateSecurity(eutra_CellA, v_SecurityParams, v_NasCountUL); // RRC SECURITY COMMAND & COMPLETE
  f_EUTRA_Security_Set ( v_SecurityParams );//Saving security parameters

  //@siclog "Step 5" siclog@
  //+   The SS sends ATTACH ACCEPT to assign the new GUTI (GUTI-2).
  f_EUTRA_RRCConnectionReconfigurationWithAttachAcceptAllParams(eutra_CellA,
                                                tsc_EpsDefaultBearerId,
                                                tsc_RRC_TI_Def,
                                                v_EpsPti,
                                                v_AttachType,
                                                f_GetPDNAddress(f_CheckPCOforIPallocationViaNas (v_Pco)),
                                                f_GutiParameters2MobileIdentity(tsc_IEI_Guti, v_Guti2_Params),
                                                v_Tai1_List,
                                                v_LAI,
                                                v_MSId,
                                                omit,
                                                v_APN,
                                                f_GetDefPCOforPCSCFAddress (v_Pco), // @sic R5-104703 sic@
                                                v_NasInd.Pdu.Msg.aTTACH_REQUEST.addUpdateType);
  // change GUTI to GUTI-2 in cell info
  f_EUTRA_CellInfo_SetGuti(eutra_CellA, v_Guti2_Params);

  //@siclog "Step 6" siclog@
  //+   Check: Does the UE send ATTACH COMPLETE message? [1,P]
  f_EUTRA_NAS_AttachComplete(eutra_CellA, tsc_EpsDefaultBearerId);
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 6");

} // function tc_9_2_2_2_1_EUTRA

function f_TC_9_2_2_2_2_EUTRA() runs on EUTRA_PTC
{
   // @desc Network initiated detach / IMSI detach.
   //System Simulator:
   //-    Cell A is configured according to Table 6.3.2.2-1 in [18].
   //-    Cell A belongs to TAI-1.
   //UE:  UE configured to initiate combined EPS/IMSI attach

  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN,LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);

  f_UT_ConfigureCombinedAttach (UT); // R5-103814

  // Preamble: UE is in state Generic RB established (state 3) on Cell A according to [18]
  f_EUTRA_Preamble (eutra_CellA, STATE2_IDLEUPDATE);// mohongbo: TMSI-1 is not handled in the preamble, BUT it is needed in this TC. TO DO
  f_EUTRA_RbEst_Def(eutra_CellA);
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_2_2_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E1_IDLE);
}

function fl_TC_9_2_2_2_2_Body() runs on EUTRA_PTC
{

  var GutiParameters_Type v_Guti_1_Params := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var NAS_Tac v_Tac := bit2oct(f_EUTRA_CellInfo_GetTAC(eutra_CellA));
  var NAS_Lac v_LAC := f_EUTRA_CellInfo_GetLocationAreaCode(eutra_CellA);
  var GutiParameters_Type v_Guti_2_Params := f_CreateGutiParamsWithUpdatedSTmsi(v_Guti_1_Params);
  var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
  var ReleaseCause v_ReleaseCause := other;

  var NAS_PlmnId v_PLMN := f_Asn2Nas_PlmnId (v_Guti_1_Params.PLMN_Identity);
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
  var template (omit) LocAreaId v_LAI := f_GetLAI(v_PLMN,v_LAC);
  var template (omit) MobileIdentity v_MSId := f_GetMSId ( );
  var EPS_BearerContextStatus v_EPS_ContextStatus;
  var SRB_COMMON_IND v_ReceivedAsp;

// ==========================================================================

  //+  Force the SS to initiates Detach procedure with the Detach Type IE "IMSI detach"
  //@siclog "Step 6" siclog@
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                              cs_508_DETACH_REQUEST_MT(cs_DetachType(tsc_DetachMT_Imsi), omit))));

  //+ The UE may send DETACH ACCEPT message.(Optional)
  //+ Check: Does the UE send TRACKING AREA UPDATE REQUEST message?
  //@siclog "Step 2 - 3" siclog@
 alt{
  [] SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                    cr_508_DETACH_ACCEPT)))
    {
      SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                      cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                        cdr_TAU_Request_combinedupdating_IMSIattach(tsc_EpsUpdate_Combined_IMSIAttach,
                                                                                                    f_EUTRA_SecurityKSIasme_Get(),
                                                                                                    v_AdditionalUpdateType,
                                                                                                    f_GutiParameters2MobileIdentity(omit, v_Guti_1_Params),
                                                                                                    v_LAI,
                                                                                                    v_PLMN,
                                                                                                    v_Tac))))
        -> value v_ReceivedAsp;
      f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3");
    }
  [] SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                     cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                       cdr_TAU_Request_combinedupdating_IMSIattach(tsc_EpsUpdate_Combined_IMSIAttach,
                                                                                                   f_EUTRA_SecurityKSIasme_Get(),
                                                                                                   v_AdditionalUpdateType,
                                                                                                   f_GutiParameters2MobileIdentity(omit, v_Guti_1_Params),
                                                                                                   v_LAI,
                                                                                                   v_PLMN,
                                                                                                   v_Tac))))
    -> value v_ReceivedAsp
    {
      f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3");
    }
 };
 v_EPS_ContextStatus := v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.epsBearerContextStatus; // Send this back in the Accept


 //  The SS starts a NAS security mode command procedure to perform NAS integrity protection.
 //  The UE responds properly to the NAS security mode command procedure.
 //@siclog "Step 4 - 5" siclog@
 f_EUTRA_NAS_AS_Security(eutra_CellA, false, tsc_PTI_1, tsc_SRB2);

 //@siclog "Step 6" siclog@
 //+  The SS sends TRACKING AREA UPDATE ACCEPT message.
 SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                  cs_TimingInfo_Now,
                                  cs_NAS_Request ( tsc_SHT_IntegrityProtected_Ciphered,
                                                   cs_508_TAU_Accept ( tsc_EpsUpdate_Combined_TaLaUpdate,
                                                                       f_GutiParameters2MobileIdentity(tsc_IEI_Guti, v_Guti_2_Params),
                                                                       cds_TAIListNonConsecutive_tlv( v_PLMN, {v_Tac } ),
                                                                       v_EPS_ContextStatus,
                                                                        v_LAI,
                                                                       v_MSId,
                                                                       f_GetAdditionalUpdateResult(v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.addUpdateType)))));

 //@siclog "Step 7" siclog@
 //+   Check: Does the UE send TRACKING AREA UPDATE COMPLETE message.
 SRB.receive ( car_SRB2_NasPdu_IND ( eutra_CellA,
                                     cr_NAS_Indication ( tsc_SHT_IntegrityProtected_Ciphered,
                                                         cr_508_TAU_Complete )));
  f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "");

  //@siclog "Step 8" siclog@
  //+   The SS releases the RRC connection.
  f_EUTRA_RRC_ConnectionRelease ( eutra_CellA, v_RRC_TI, v_ReleaseCause );

  //@siclog "Step 9" siclog@
  //+   Check: Does the UE respond to paging on cell A with S-TMSI2 for CS domain? Generic Procedure (TS36.508 subclause 6.4.2.4)
  f_EPS_508Check_NAS_Paging(eutra_CellA, cs);

  //@siclog "Step 10" siclog@
  //+   Check: Does the UE respond to paging on cell A with S-TMSI2 for PS domain? Generic Procedure (TS36.508 subclause 6.4.2.4)
  f_EPS_508Check_NAS_Paging(eutra_CellA, ps);

}// function tc_9_2_2_2_2_EUTRA

function f_TC_9_2_2_2_14_EUTRA() runs on EUTRA_PTC
{
   // @desc NW initiated detach / Abnormal case / EMM cause not included
    /* Test Purpose (TP)
     (1)
    with { UE in EMM-REGISTERED state }
    ensure that {
      when { UE receives a DETACH REQUEST message with the Detach type IE "re-attach not required" and with no EMM cause IE included }
        then { UE delete any GUTI, TAI list, last visited registered TAI, list of equivalent PLMNs, KSI, set the update status to EU2 NOT UPDATED and start timer T3402. when T3402 expired, the UE attempts to attach again }
     }

    System Simulator:
    -   Cell I are configured according to Table 6.3.2.2-3 in [18].
    -   Cell I (VPLMN and set as a Serving cell)
    UE:
    -   The test USIM contains GUTI-9 and TAI-9, and EPS update status is "EU1: UPDATED".
    Preamble:
    -   UE is in state Registered, Idle Mode (state 2) on Cell I according to [18]
    -   T3402 in the UE is set to 2 minutes @sic R5s102201 sic@

    */
  var NAS_MSG_Indication_Type v_NasInd;
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType(  ); // R5-103673
  var template (value) EUTRA_FDD_TDD_CellInfo_Type v_EUTRA_FDD_TDD_CellInfo := f_EUTRA_CellInfo_GetFDD_TDD_Info ( eutra_CellI );
  var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (eutra_CellI);
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell, LTE_NAS_Usef1_f2);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellI);
  f_EUTRA_Preamble (eutra_CellI, STATE2_IDLEUPDATE);
  // now to transmit T3402 to UE with value 2 minuts
  f_EUTRA_UE_Detach_SwitchOffUe ( eutra_CellI, RRC_IDLE, tsc_RRC_TI_Def );
  f_UT_SwitchOnUE (UT, false);

  v_NasInd:= f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellI,
                  tsc_RRC_TI_Def,
                  cr_EstablishmentCause_Any,
                  cr_NAS_IndicationWithPiggybacking(tsc_SHT_IntegrityProtected,
                                                    cr_508_ATTACH_REQUEST(v_AttachType, v_AdditionalUpdateType),
                                                    cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach,
                                                                                                              f_GetPdnType())));

  SRB.send(cas_SRB1_RrcNasPdu_REQ ( eutra_CellI,
                                      cs_TimingInfo_Now,
                                      cs_RRCConnectionReconfiguration_SRB2_1AM_DRB_Est(tsc_RRC_TI_Def,
                                                                                       v_EUTRA_FDD_TDD_CellInfo.cqi_ReportConfig,
                                                                                       v_EUTRA_FDD_TDD_CellInfo.soundingRS_UL_Config,
                                                                                       v_AntennaInfo
                                                                                       ),
                                      cs_NAS_RequestWithPiggybacking(tsc_SHT_IntegrityProtected_Ciphered,
                                                        cds_ATTACH_ACCEPT_t3402(v_AttachType,
                                                                                cs_TrackingAreaIdListDef_lv,
                                                                                f_GutiParameters2MobileIdentity(tsc_IEI_Guti, f_EUTRA_CellInfo_GetGuti(eutra_CellI)),
                                                                                omit,
                                                                                omit,
                                                                                omit,
                                                                                f_GetAdditionalUpdateResult(v_NasInd.Pdu.Msg.aTTACH_REQUEST.addUpdateType),
                                                                                cs_GprsTimer_v('001'B, '00010'B)), //2 minutes
                                                        cs_508_ActivateDefEpsBearerContextRequest(tsc_EpsDefaultBearerId,
                                                                                                  tsc_PTI_Unassigned,
                                                                                                  cs_AccessPointName(px_AccessPointName),
                                                                                                  f_GetPDNAddress (f_CheckPCOforIPallocationViaNas (omit)),
                                                                                                  omit,
                                                                                                  omit,
                                                                                                  f_GetDefPCOforPCSCFAddress (v_NasInd.Pdu.PiggybackedPduList[0].Msg.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions))))); // @sic R5-104703 sic@

    //Receive RRCConnectionReconfigurationComplete
  SRB.receive(car_SRB1_RrcPdu_IND(eutra_CellI, cr_508_RRCConnectionReconfigurationComplete(tsc_RRC_TI_Def)));

  f_EUTRA_NAS_AttachComplete(eutra_CellI, tsc_EpsDefaultBearerId);

/////////////////////////////////////////////////////////////////
  
  f_EUTRA_RbEst_Def(eutra_CellI);

  f_EUTRA_TestBody_Set(true);

  fl_TC_9_2_2_2_14_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellI, E2_CONNECTED);
}

function fl_TC_9_2_2_2_14_Body() runs on EUTRA_PTC
{
  var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
  var ReleaseCause v_ReleaseCause := other;
  var NAS_MSG_Indication_Type v_NasInd;
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();

  timer t_T3402 := f_EUTRA_SetTimerToleranceMin (eutra_CellI, 120.0 ); // @sic R5s102190 sic@
// ==========================================================================

  //@siclog "Step 1" siclog@
  //+  SS initiate Detach procedure with the Detach Type IE "re-attach not required" and no EMM cause IE included
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellI,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                              cs_508_DETACH_REQUEST_MT(cs_DetachType(tsc_DetachMT_NoReAttach), omit))));
 
  //+ The UE transmits a DETACH ACCEPT message on Cell I. Note: Now UE should start timer T3402
  //@siclog "Step 2" siclog@
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellI,
                                  cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                  cr_508_DETACH_ACCEPT)));
  t_T3402.start;

  //+The SS releases the RRC connection.
    //@siclog "Step 3" siclog@
  f_EUTRA_RRC_ConnectionRelease ( eutra_CellI, v_RRC_TI, v_ReleaseCause );

 // Check: When the timer T3402 expires does the UE transmit ATTACH REQUEST message on cell I?
   //@siclog "Step 4" siclog@
  t_T3402.timeout;
  v_NasInd:= f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellI,
                                            v_RRC_TI,
                                            cr_EstablishmentCause_Any,
                                            cr_NAS_IndicationWithPiggybacking(tsc_SHT_NoSecurityProtection,
                                                                              cdr_ATTACH_REQUEST_WithIMSIAndnoTAI(v_AttachType,
                                                                                                                  v_AdditionalUpdateType,
                                                                                                                  f_Imsi2MobileIdentity(px_IMSI_Def),
                                                                                                                  cr_TMSI_Status('0'B) ifpresent),
                                                                              cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach,
                                                                                                              f_GetPdnType())));
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 4");

    //@siclog "StepS 5 - 16" siclog@
    f_EUTRA_IdleUpdated_Step5_17(eutra_CellA, STATE2_IDLEUPDATE, NORMAL, v_NasInd, false);

    }// function tc_9_2_2_2_14_EUTRA

} // end of group TestcaseImplementations

} // end of module
