// ***************************************************************************
// @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_ServiceRequest {

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

group LocalConstants {
} // group LocalConstants

group LocalTemplates {

template NAS_UL_Message_Type cdr_ATTACH_REQUEST_WithNativeSecCtxGutiTAI(template (present) NAS_AttDetValue_Type p_AttachTypeValue,
                                                                        template AdditionalUpdateType p_AdditionalUpdateType,
                                                                        template (present) NAS_KsiValue   p_KsiValue,
                                                                        template (present) MobileIdentity p_MobileId,
                                                                        NAS_PlmnId                        p_PlmnId,
                                                                        NAS_Tac                           p_Tac,
                                                                        template    TMSI_Status           p_TmsiStatus)
  modifies cr_508_ATTACH_REQUEST :=
{ /* @status    APPROVED */
  aTTACH_REQUEST := {
    nasKeySetId              := cr_NAS_KeySetIdentifier(p_KsiValue, tsc_NasKsi_NativeSecurityContext),
    oldGutiOrImsi            := p_MobileId,
    lastVisitedRegisteredTai := cr_TAI (p_PlmnId, p_Tac),
    oldLai                   := omit,
    tmsiStatus               := p_TmsiStatus
  }
};
} // group LocalTemplates
  
group TestcaseImplementations {

///////////////////////////////////////////////////////////////////////////////
// TC 9.3.1.1
///////////////////////////////////////////////////////////////////////////////
/*
 * @desc      REFERENCE TS 36.523-1 clause 9.3.1.1
 * @status    APPROVED
 */
function f_TC_9_3_1_1_EUTRA() runs on EUTRA_PTC
{
  /* @desc Service Request initiated by UE for user data
   *
   * (1)
   * with { UE in EMM-REGISTERED state and EMM-IDLE mode }
   *    ensure that {
   *     when { UE has user data pending }
   *        then { UE establishes the RRC connection with the RRC establishmentCause set to "mo-Data" and sends a SERVICE REQUEST message }
   *
   * Pre-test conditions
   * -------------------
   * System Simulator:
   *   - 1 cell
   * User Equipment:
   *   - None
   */
  
  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);
  
  // create cell and send out system information
  f_EUTRA_CellConfig_Def (eutra_CellA);
  
  // Preamble: The UE is in state Loopback Activated (State 4) according to [18]
  // using the specific message content of CLOSE UE TEST LOOP message in table
  // 9.3.1.1.3.3-1.
  // ==========================================================================
  f_EUTRA_Preamble(eutra_CellA, STATE2A_TESTLOOP_ModeB);
  f_EUTRA_RbEst_Def(eutra_CellA);
  f_EUTRA_CloseUE_TestLoopModeB(eutra_CellA, '05'O);
  
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_1_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);//@sic R5-103875 sic@
}; // function f_TC_9_3_1_1_EUTRA

/*
 * @desc      testbody of TC_9_3_1_1
 * @status    APPROVED
 */
function fl_TC_9_3_1_1_Body() runs on EUTRA_PTC
{
  var NAS_MSG_Indication_Type v_NAS_Ind;
  var template (value) octetstring v_IPData := crs_IPv4_AnyIcmpEchoReply_1 (px_IPv4_Address, px_IPv4_Address);

  // The SS transmits one IP Packet to the UE (over default DRB 0)
  //@siclog "Step 1" siclog@
  DRB.send (cas_DRB_COMMON_REQ_PDCP_SDU(eutra_CellA, tsc_RbId_DRB1, cs_TimingInfo_Now, {v_IPData})); // @sic R5s100135, R5s100189 sic@
  
  // The SS waits 1 second after the IP packet has been transmitted in step 1
  //+ and then transmits an RRCConnectionRelease message
  //@siclog "Step 2" siclog@
  f_Delay (1.0);
  f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);
  
  
  //+   Check: Check: Does the UE transmit an RRCConnectionRequest message with establishmentCause set
  //             to mo-Data followed by a SERVICE REQUEST message? [1,P]
  //+ Note: Triggered when timer T_delay_modeB (IP PDU delay time) expires and
  //+ pending uplink data exist in buffered PDCP SDUs according to [25] clause
  //+ 5.4.4.3.
  //@siclog "Step 3" siclog@
  v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoData,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(f_EUTRA_SecurityKSIasme_Get())));
  
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3");
  
  // + Step 4-7: @sic R5-102180 sic@ @sic R5s100300 sic@
  // Steps 6 to 9 of the generic radio bearer establishment procedure (TS 36.508 5.4.3.3-1) are executed
  // to successfully complete the service request procedure
  //  f_EUTRA_ServiceReqPostamble_508Steps6_9 (eutra_CellA, v_NasInd);
  //@siclog "Step 4 - 7" siclog@
  f_EUTRA_Activate_SRB2_DRB_SendRrcMsg ( eutra_CellA,
                                         tsc_RRC_TI_Def,
                                         v_NAS_Ind.SecurityProtection.NasCount);
  interleave
  {
    [] SRB.receive ( car_SRB1_RrcPdu_IND ( eutra_CellA,
                                           cr_508_RRCConnectionReconfigurationComplete(tsc_RRC_TI_Def) ) )
       {}
    [] DRB.receive (car_DRB_COMMON_IND_PDCP_SDUList(eutra_CellA,
                                                    tsc_RbId_DRB1,
                                                    ?,
                                                    {v_IPData}))
       {}
   }
  
} // function fl_TC_9_3_1_1_Body

///////////////////////////////////////////////////////////////////////////////
// TC 9.3.1.7
///////////////////////////////////////////////////////////////////////////////
/*
 * @desc      REFERENCE TS 36.523-1 clause 9.3.1.7
 * @status    APPROVED
 */
function f_TC_9_3_1_7_EUTRA() runs on EUTRA_PTC
{
  /// @desc Service Request / Rejected / UE identity cannot be derived by the network
  
  // purpose
  // -------
  // (1)
  // with { UE having sent a SERVICE REQUEST message }
  // ensure that {
  //   when { UE receives a SERVICE REJECT message with the EMM cause set to
  // value = 9 (UE identity cannot be derived by the network) }
  //   then { UE sets the EPS update status to EU2 NOT UPDATED and deletes any GUTI,
  //          last visited registered TAI, TAI list and KSI and automatically initiate the attach procedure}
  //         }
  
  // Pre-test conditions
  // -------------------
  // System Simulator: cellA
  // UE:
  // The USIM contains IMSI-1, GUTI-1 and TAI-1 (MCC1/MNC1/TAC1), and EPS 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);
  
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_7_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);//@sic R5-103875 sic@
  
}// function f_TC_9_3_1_7_EUTRA

/*
 * @desc      test body of TC_9_3_1_7
 * @status    APPROVED
 */
function fl_TC_9_3_1_7_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_Guti_Params := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  
  // temporary variable which is used when receiving NAS ASPs
  var NAS_MSG_Indication_Type v_NasInd;
  var SRB_COMMON_IND v_ReceivedAsp;
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType (); //used to follow TTCN3 v411 standard
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType(); //used to follow TTCN3 v411 standard

  
  //+   The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page(eutra_CellA,
                  cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_Guti_Params.MME_Code,
                                                                     v_Guti_Params.M_TMSI)));
  
  
  //+   The UE transmits a SERVICE REQUEST message.
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(f_EUTRA_SecurityKSIasme_Get())));

  //+   The SS transmits a SERVICE REJECT message with the EMM cause set to #9
  //+   (UE identity cannot be derived by the network).
  //@siclog "Step 3" siclog@
  SRB.send(cas_SRB1_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_NoSecurityProtection,
                                              cs_508_SERVICE_REJECT(tsc_EmmCause_UEIdentityNotDerived))));

  //+   Check: Does the UE transmit an ATTACH REQUEST message? (1,P)
  //@siclog "Step 4" siclog@
  SRB.receive(car_SRB1_NasPdu_IND(eutra_CellA,
                                  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()))))
    -> value v_ReceivedAsp;
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 4");

  //@siclog "Step 5 - 16" siclog@
  f_EUTRA_IdleUpdated_Step5_17(eutra_CellA, STATE2_IDLEUPDATE, NORMAL, v_ReceivedAsp.Signalling.Nas[0], false); // @sic R5-105005

}

/*
 * @desc      REFERENCE TS 36.523-1 clause 9.3.1.7a
 * @status    APPROVED
 */
function f_TC_9_3_1_7a_EUTRA() runs on EUTRA_PTC
{
  /// @desc Service Request / Rejected / UE implicitly detached
  
  // purpose
  // -------
  // (1)
  // with { UE having sent a SERVICE REQUEST message }
  // ensure that {
  //   when { UE receives a SERVICE REJECT message with the EMM cause set to 'Implicitly detached' }
  //   then { UE enters the state EMM-DEREGISTERED.NORMAL-SERVICE,
  //          delete the EPS mapped EPS security context if any and performs a new attach procedure}
  //         }
  
  // Pre-test conditions
  // -------------------
  // System Simulator: cellA
  // UE:
  // None.
  // 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);
  
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_7a_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);//@sic R5-103875, R5-105005 sic@
  
}// function f_TC_9_3_1_7a_EUTRA

/*
 * @desc      test body of TC_9_3_1_7a
 * @status    APPROVED
 */
function fl_TC_9_3_1_7a_Body() runs on EUTRA_PTC
{
  // get all data which are required for a GUTI
  var GutiParameters_Type v_Guti_Params := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var template TMSI_Status v_TMSI_Status := omit;
  var NAS_MSG_Indication_Type v_NasInd;
  var SRB_COMMON_IND v_ReceivedAsp;
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType (); //used to follow TTCN3 v411 standard
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType(); //used to follow TTCN3 v411 standard
  
  //+   The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page(eutra_CellA,
                  cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_Guti_Params.MME_Code,
                                                                     v_Guti_Params.M_TMSI)));
  
  //+   The UE transmits a SERVICE REQUEST message.
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(f_EUTRA_SecurityKSIasme_Get())));
  
  //+   The SS transmits a SERVICE REJECT message with the EMM cause set to
  //+   'Implicitly detached'.
  //@siclog "Step 3" siclog@
  SRB.send(cas_SRB1_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_NoSecurityProtection,
                                              cs_508_SERVICE_REJECT(tsc_EmmCause_ImplicitlyDetached))));
  
  if (pc_CS_Fallback or pc_SMS_SGs_MO or pc_SMS_SGs_MT) {
    v_TMSI_Status := cr_TMSI_Status('0'B);
  }
  
  //+   Check: Does the UE transmit an ATTACH REQUEST message including a PDN CONNECTIVITY REQUEST message
  //          and integrity protected using the native security context resulting from authentication during the test preamble?  1   P
  //@siclog "Step 4" siclog@
  SRB.receive(car_SRB1_NasPdu_IND(eutra_CellA,
                                  cr_NAS_IndicationWithPiggybacking(tsc_SHT_IntegrityProtected,
                                                                    cdr_ATTACH_REQUEST_WithNativeSecCtxGutiTAI(v_AttachType,
                                                                                                               v_AdditionalUpdateType,
                                                                                                               v_SecurityParams.KSIasme,
                                                                                                               f_GutiParameters2MobileIdentity(omit, v_Guti_Params),
                                                                                                               f_Asn2Nas_PlmnId(v_Guti_Params.PLMN_Identity),
                                                                                                               bit2oct(f_EUTRA_CellInfo_GetTAC(eutra_CellA)),
                                                                                                               v_TMSI_Status),                         // @sic R5-101191 sic@
                                                                    cr_508_PDN_CONNECTIVITY_REQUEST( tsc_PdnRequest_InitialAttach,
                                                                                                     f_GetPdnType()))))
    -> value v_ReceivedAsp;
  
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 4");
  
  //@siclog "Step 5 - 16" siclog@
  f_EUTRA_IdleUpdated_Step5_17(eutra_CellA, STATE2_IDLEUPDATE, NORMAL, v_ReceivedAsp.Signalling.Nas[0], false); // @sic R5-105005 sic@

 }// function fl_TC_9_3_1_7a_Body


/**
 * @desc Service Request / Mobile originating CS fallback
 */
function f_TC_9_3_1_3_EUTRA() runs on EUTRA_PTC
{
  // Purpose
  // -------
  // (1)
  // with { UE in state EMM-REGISTERED and EMM-CONNECTED mode }
  // ensure that {
  //   when { UE initiates mobile originating CS fallback }
  //   then { UE sends EXTENDED SERVICE REQUEST message }
  // }
  
  // (2)
  // with { UE in state EMM-REGISTERED and EMM-IDLE mode }
  // ensure that {
  //   when { UE initiates mobile originating CS fallback }
  //   then { UE sends EXTENDED SERVICE REQUEST message }
  // }
  
  // Pre-test conditions
  // -------------------
  // System Simulator:
  //   - Cell A is configured according to Table 6.3.2.2-1 in [18].
  //   - Cell A belongs TAI-1.
  //   - Cell A is set to the "Serving cell".
  // UE:
  //   - The UE has a valid GUTI (GUTI-1).
  //   - The UE is registered on TAI-1.
  //   - The UE is IMSI attached for non-EPS services.
  
  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 A
  // ==========================================================================
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_RbEst_Def(eutra_CellA);
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_3_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);

} // function f_TC_9_3_1_3_EUTRA

function fl_TC_9_3_1_3_Body() runs on EUTRA_PTC
{
  // ==========================================================================
  // initialize constant test case data structures
  // ==========================================================================
  // none required
  // ==========================================================================
  
  // ==========================================================================
  // initialize component specific data structures
  // ==========================================================================
  // get current security parameters
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  // ==========================================================================
  
  // temporary variable which is used when receiving NAS ASPs
  var NAS_MSG_Indication_Type v_NasInd;
  
  //+   Force the UE to initiate CS Voice call.
  //+ Note 1: This could be done by e.g. MMI or by AT command.
  //@siclog "Step 1" siclog@
  f_UT_RequestCSCall(UT);
  
  //+   Check: Does the UE transmit EXTENDED SERVICE REQUEST message?  -->
  //+   EXTENDED SERVICE REQUEST [1,P]
  //@siclog "Step 2" siclog@
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                    cr_508_EXT_SERVICE_REQUEST(v_SecurityParams.KSIasme,
                                                                               tsc_EST_CsFallback_MO,
                                                                               bit2oct(v_GutiParams.M_TMSI)))));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
  

  //+   The SS sends SERVICE REJECT message in order that the UE enters
  //+   EMM-REGISTERED.NORMAL-SERVICE.
  //@siclog "Step 3" siclog@
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_NoSecurityProtection,
                                              cs_508_SERVICE_REJECT(tsc_EmmCause_CsFbCallEstNotAllowed))));

  //+   The SS releases the RRC connection
  //@siclog "Step 4" siclog@
  f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);
  
  //+   Force the UE to initiate CS Voice call.
  //+ Note 1: This could be done by e.g. MMI or by AT command.
  //@siclog "Step 5" siclog@
  f_UT_RequestCSCall(UT);
  
  //+   Check: Does the UE transmit an RRCConnectionRequest message with establishmentCause
  //           set to mo-Data followed by EXTENDED SERVICE REQUEST message?  -->
  //+   EXTENDED SERVICE REQUEST [2,P]
  //@siclog "Step 6" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoData,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_EXT_SERVICE_REQUEST(v_SecurityParams.KSIasme,
                                                                                          tsc_EST_CsFallback_MO,
                                                                                          bit2oct(v_GutiParams.M_TMSI))));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 6");
  
} // function fl_TC_9_3_1_3_Body

/**
 * @desc Service Request / Rejected / IMSI invalid (multi RAT test case)
 */
function f_TC_9_3_1_4_EUTRA() runs on EUTRA_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].
  */
  var template (value) CellPowerList_Type v_CellPowerList;
  var boolean v_TriggerRxd := false;
  
  if (pc_UTRA) {
      f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN, LTEmulticell_UTRANCell);
  } else if (pc_GERAN) {
      f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN, LTEmulticell_GERANcell);
  } else { // Neither UMTS, nor GSM, so just do LTE
      f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN, LTEmulticell);
  }

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

  v_CellPowerList := {
    cs_CellPower ( eutra_CellA, tsc_ServingCellRS_EPRE ),
    cs_CellPower ( eutra_CellB, tsc_NonSuitableCellRS_EPRE )
  };
  f_EUTRA_SetCellPowerList ( v_CellPowerList, cs_TimingInfo_Now );
  
    if (pc_UTRA) {
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN); // wait for pre-preamble to finish
        f_EUTRA_Preamble_InterRAT(eutra_CellA, STATE2_IDLEUPDATE, UTRAN);
    } else if (pc_GERAN) {
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(GERAN); // wait for pre-preamble to finish
        f_EUTRA_Preamble_InterRAT(eutra_CellA, STATE2_IDLEUPDATE, GERAN);
    } else { // supports neither UTRAN or GERAN
        f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
    }
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_4and5_Body(tsc_EmmCause_IllegalUe);
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_ReleaseAllCells();
  
} // function f_TC_9_3_1_4_EUTRA


function f_TC_9_3_1_5_EUTRA() runs on EUTRA_PTC
{
// @desc Service request / Rejected / Illegal ME
 /* 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
 */

  var template (value) CellPowerList_Type v_CellPowerList;
  var boolean v_TriggerRxd := false;

  if (pc_UTRA) {
      f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN, LTEmulticell_UTRANCell);
  } else if (pc_GERAN) {
      f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN, LTEmulticell_GERANcell);
  } else { // Neither UMTS, nor GSM, so just do LTE
      f_EUTRA_NAS_Init(LTE_CellsOnDifferentPLMN, LTEmulticell);
  }

  // create cell and send out system information
  f_EUTRA_CellConfig_Def(eutra_CellA);
  f_EUTRA_CellConfig_SRBs_Only(eutra_CellB);
  v_CellPowerList := {
    cs_CellPower ( eutra_CellA, tsc_ServingCellRS_EPRE ),
    cs_CellPower ( eutra_CellB, tsc_NonSuitableCellRS_EPRE )
  };
  f_EUTRA_SetCellPowerList ( v_CellPowerList, cs_TimingInfo_Now );

    if (pc_UTRA) {
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN); // wait for pre-preamble to finish
        f_EUTRA_Preamble_InterRAT(eutra_CellA, STATE2_IDLEUPDATE, UTRAN);
    } else if (pc_GERAN) {
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(GERAN); // wait for pre-preamble to finish
        f_EUTRA_Preamble_InterRAT(eutra_CellA, STATE2_IDLEUPDATE, GERAN);
    } else { // supports neither UTRAN or GERAN
        f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
    }
  f_EUTRA_TestBody_Set(true);

  fl_TC_9_3_1_4and5_Body(tsc_EmmCause_IllegalMe);

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_ReleaseAllCells();
} // end f_TC_9_3_1_5_EUTRA

function fl_TC_9_3_1_4and5_Body(NAS_CauseValue_Type p_RejectCause) runs on EUTRA_PTC
{
  var GutiParameters_Type v_GutiParams_A := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var NAS_MSG_Indication_Type v_NasInd;
  var ProcedureTransactionIdentifier v_EPS_TI := tsc_PTI_1;
  var template (omit) AccessPointName v_APN := omit;
  var boolean v_EIT_Flag := false;
  var template TMSI_Status v_TMSI_Status := omit;
  var template (omit) ProtocolConfigOptions v_Pco := omit;
  var NAS_UL_Message_Type v_PdnConnectivityRequest;
  var boolean v_IPallocationViaNasFlag :=  false;
  var boolean v_TriggerRxd := false;
  var charstring v_OtherPTC := "";
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType(); //used to follow TTCN3 v411 standard
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();  //used to follow TTCN3 v411 standard
  var template (value) CellPowerList_Type v_CellPowerList;
  timer t_Wait;
  
    if (pc_UTRA) {
       v_OtherPTC := "UTRAN";
     } else if (pc_GERAN) {
       v_OtherPTC := "GERAN";
     } else { // Neither UMTS, nor GSM, so just do LTE
       v_OtherPTC := "NONE";
    }

  //+   The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page ( eutra_CellA,
                    cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI)));
  
  //+   The UE transmits a SERVICE REQUEST message. --> SERVICE REQUEST
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));
  
  
  //+   The SS transmits a SERVICE REJECT message with the EMM cause set to
  //+   9.3.1.4 : 'Illegal UE'
  //+   9.3.1.5 : 'Illegal ME'
  //                            <-- SERVICE REJECT
  //@siclog "Step 3" siclog@
  SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                   cs_TimingInfo_Now,
                                   cs_NAS_Request ( tsc_SHT_NoSecurityProtection,
                                                    cs_508_SERVICE_REJECT ( p_RejectCause ) ) ) );
  
  //+   The SS releases the RRC connection.
  //@siclog "Step 4" siclog@
  f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);

  //+   Set the cell type of Cell A to the "non-Suitable cell". Set the cell
  //+   type of Cell B to the "Serving cell".
  //@siclog "Step 5" siclog@
  f_EUTRA_SetCellPower(eutra_CellA, tsc_NonSuitableCellRS_EPRE, cs_TimingInfo_Now);
  f_EUTRA_SetCellPower(eutra_CellB, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now);
  
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // The following messages are sent and shall be received on Cell B.
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
  //+   Check: Does the UE transmit an ATTACH REQUEST message in the next 30
  //+   seconds? --> ATTACH REQUEST [1,F]
  //@siclog "Step 6" siclog@
  f_EUTRA_CheckNoAttachOn1Cell(eutra_CellB, 30.0);
  
  // CellB as a "Non-Suitable cell" and trigger UTRAN/GERAN side
  //@siclog "Step 6Aa1 - 6Aa3" siclog@
  select (v_OtherPTC) { // send trigger to other RAT to change its power level too
    case ("UTRAN") {
      f_EUTRA_SetCellPower(eutra_CellB, tsc_NonSuitableCellRS_EPRE, cs_TimingInfo_Now);
      f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN); // wait for 1st trigger from UTRAN side
    } case ("GERAN") {
      f_EUTRA_SetCellPower(eutra_CellB, tsc_NonSuitableCellRS_EPRE, cs_TimingInfo_Now);
      f_SendIRATCoOrd (GERAN, cms_IRAT_Trigger);
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(GERAN); // wait for 1st trigger from GERAN side
    } case else { //UE doesn't support other RATs and is already Unattached and Idle, so just switch off
      f_UT_SwitchOffUE (UT, false);
    }
  }
  
  //+   Set the cell type of the Cell B to the "non-Suitable cell". Set the cell
  //+   type of Cell A to the "Serving cell".
  //@siclog "Step 7" siclog@
  v_CellPowerList := {
      cs_CellPower ( eutra_CellA, tsc_ServingCellRS_EPRE ),
      cs_CellPower ( eutra_CellB, tsc_NonSuitableCellRS_EPRE )
  };
  f_EUTRA_SetCellPowerList ( v_CellPowerList, cs_TimingInfo_Now );
  
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // The following messages are sent and shall be received on Cell A.
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
  //+   Check: Does the UE transmit an ATTACH REQUEST message in the next 30
  //+   seconds? --> ATTACH REQUEST [1,F]
  //@siclog "Step 8" siclog@
  f_EUTRA_CheckNoAttachOn1Cell(eutra_CellA, 30.0);
  
  //+   The SS pages the UE using same S-TMSI in the step 1 with CN domain
  //+   indicator set to "PS".
    //@siclog "Step 9" siclog@
  f_EUTRA_UE_Page ( eutra_CellA,
                    cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI)));
  
  //+   Check: Does the UE transmit a SERVICE REQUEST message in the next 30
  //+   seconds? --> SERVICE REQUEST [1,F]
  //@siclog "Step 10" siclog@
  t_Wait.start(30.0*0.9);
  alt {
    [] SRB.receive(car_SRB0_RrcPdu_IND(eutra_CellA, cr_RRCConnectionRequest(?)))
      { // even though prose says SERVICE REQUEST, as soon as UE sends RRC ConnReq, we know
        // something's wrong
        t_Wait.stop;
        f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 10");
      }
    [] t_Wait.timeout {}
  }
  
  //+ Step 11
  //+   The UE is powered off or switched off.
  //@siclog "Step 11" siclog@
  f_UT_SwitchOffUE(UT, false);
  

  //+   The UE is powered on or switched on.
  //@siclog "Step 12" siclog@
  f_UT_SwitchOnUE(UT, false);
  
  if (pc_CS_Fallback or pc_SMS_SGs_MO or pc_SMS_SGs_MT) {
    v_TMSI_Status := cr_TMSI_Status('0'B);
  }
  
  //+   Check: Does the UE transmit an ATTACH REQUEST message?  --> ATTACH
  //+   REQUEST [1,P]
  //@siclog "Step 13" siclog@
  v_NasInd:= f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                            tsc_RRC_TI_Def,
                                            cr_EstablishmentCause_Any,
                                            cr_NAS_IndicationWithPiggybacking(tsc_SHT_NoSecurityProtection,
                                                                              cdr_ATTACH_REQUEST_WithIMSIAndnoTAI(v_AttachType,
                                                                                                                  v_AdditionalUpdateType,
                                                                                                                  f_Imsi2MobileIdentity(px_IMSI_Def),
                                                                                                                  v_TMSI_Status),
                                                                              cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach,
                                                                                                              f_GetPdnType())));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 13");
  v_PdnConnectivityRequest := v_NasInd.Pdu.PiggybackedPduList[0].Msg;
  v_EPS_TI := v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.procedureTransactionIdentifier; // Use this value in SS responses
  if (ispresent(v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.esmInfoTransferFlag)) {
    if ( v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.esmInfoTransferFlag.eitValue == '1'B ) {
      v_EIT_Flag := true;
    }
  }
  if (ispresent( v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions)) {
    v_Pco := v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions;
  }
  //+ Step 14
  //+   The SS transmits an AUTHENTICATION REQUEST message to initiate the EPS
  //+   authentication and AKA procedure.  <-- AUTHENTICATION REQUEST
  //+ Step 15
  //+   The UE transmits an AUTHENTICATION RESPONSE message and establishes
  //+   mutual authentication.  --> AUTHENTICATION RESPONSE
  //+ Step 16
  //+   The SS transmits a NAS SECURITY MODE COMMAND message to activate NAS
  //+   security.  <-- SECURITY MODE COMMAND
  //+ Step 17
  //+   The UE transmits a NAS SECURITY MODE COMPLETE message and establishes
  //+   the initial security configuration. --> SECURITY MODE COMPLETE
  //@siclog "Step 14 - 17" siclog@
  v_APN := f_EUTRA_Authentication_NAS_AS_Security (eutra_CellA,v_EIT_Flag, v_EPS_TI, false);
  
  //+   The UE transmits an ATTACH ACCEPT message.  <-- ATTACH ACCEPT
  //@siclog "Step 18" siclog@
  v_IPallocationViaNasFlag :=  f_CheckPCOforIPallocationViaNas (v_Pco); // See TS 36.508 v.8.2.2 Table 4.7.3-6
  f_EUTRA_RRCConnectionReconfigurationWithAttachAcceptAllParams(eutra_CellA,
                                                                tsc_EpsDefaultBearerId,
                                                                tsc_RRC_TI_Def,
                                                                tsc_PTI_1,
                                                                tsc_EAttachType_EpsOnly,
                                                                f_GetPDNAddress(v_IPallocationViaNasFlag),
                                                                f_GutiParameters2MobileIdentity(tsc_IEI_Guti,
                                                                                                v_GutiParams_A),
                                                                cs_TrackingAreaIdListDef_lv,
                                                                omit,
                                                                omit,
                                                                omit,
                                                                omit,
                                                                f_GetDefPCOforPCSCFAddress (v_Pco), // @sic R5-104703 sic@
                                                                v_NasInd.Pdu.Msg.aTTACH_REQUEST.addUpdateType);  // @sic R5-103681 sic@);
  

  //+   The UE transmits an ATTACH COMPLETE message.  --> ATTACH COMPLETE
  //@siclog "Step 19" siclog@
  f_EUTRA_NAS_AttachComplete(eutra_CellA, tsc_EpsDefaultBearerId);
  
  //+   The SS releases the RRC connection.
  //@siclog "Step 20" siclog@
  f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);
  

  //@siclog "Step 21 - 26 Void" siclog@

  //+   The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 27" siclog@
  f_EUTRA_UE_Page ( eutra_CellA,
                    cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI)));
  
  //+   The UE transmits a SERVICE REQUEST message. --> SERVICE REQUEST
  //@siclog "Step 28" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));
  
  //+   The SS transmits a SERVICE REJECT message with the EMM cause set to
  //+   9.3.1.4 : 'Illegal UE'
  //+   9.3.1.5 : 'Illegal ME'
  //                            <-- SERVICE REJECT
  //@siclog "Step 29" siclog@
  SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                   cs_TimingInfo_Now,
                                   cs_NAS_Request ( tsc_SHT_NoSecurityProtection,
                                                    cs_508_SERVICE_REJECT ( p_RejectCause ) ) ) );

  //+   The SS releases the RRC connection.
  //@siclog "Step 30" siclog@
  f_EUTRA_RRC_ConnectionRelease(eutra_CellA, tsc_RRC_TI_Def, other);
  
  //@siclog "Step 31a" siclog@
  f_EUTRA_SetCellPower(eutra_CellA, tsc_NonSuitableCellRS_EPRE, cs_TimingInfo_Now);
  select (v_OtherPTC) { // send trigger to other RAT to change its power level too
   case ("UTRAN") {
      f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN); // wait for 1st trigger from UTRAN side
   } case ("GERAN") {
      f_SendIRATCoOrd (GERAN, cms_IRAT_Trigger);
      v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(GERAN); // wait for 1st trigger from GERAN side
   } case else { //UE doesn't support other RATs and is already Unattached and Idle, so just switch off
      f_UT_SwitchOffUE (UT, false);
   }
  }

  // Wait for 2nd trigger from other PTC to finish TC
  v_TriggerRxd := f_WaitForIRATCoOrd_Trigger(UTRAN);
  
} // function fl_TC_9_3_1_4and5_Body


/**
 * @desc Service request / Abnormal case / Switch off
 */
function f_TC_9_3_1_16_EUTRA() runs on EUTRA_PTC
{
  /* Purpose
    (1)
    with { UE having sent a SERVICE REQUEST message }
    ensure that {
      when { UE is switched off }
        then { UE performs the detach procedure }
                }

    System Simulator:
    -   Cell A.
    UE:
    -   The test USIM contains IMSI-1, GUTI-1 and TAI-1, and EPS update status is "EU1: UPDATED".
    Preamble:
    -   The 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);
  

  // ==========================================================================
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);

  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_16_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E4_DEREGISTERED);

} // function f_TC_9_3_1_16_EUTRA

function fl_TC_9_3_1_16_Body() runs on EUTRA_PTC
{
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
  var float v_NoReactionTime := f_EUTRA_SetTimerToleranceMax( eutra_CellA, 3.0 );
  var NAS_MSG_Indication_Type v_NasInd;
  
  //+ The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page(eutra_CellA,
                  cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams.MME_Code,
                                                                     v_GutiParams.M_TMSI)));
  
  //+  The UE transmits a SERVICE REQUEST message.
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));
  
  //+ The SS does not respond to the SERVICE REQUEST message.
  //        NOTE: The SS does not transmit both SecurityModeCommand message and RRCConnectionReconfiguration message.
  //@siclog "Step 3" siclog@

  //+ The UE is switched off.
  //@siclog "Step 4" siclog@
  f_UT_SwitchOffUE (UT, false);
  
  //+ Check: Does the UE transmit a DETACH REQUEST message? [1,P]
  //@siclog "Step 5" 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)))));
  
  //+  Check: Does the test result of generic test procedure in TS 36.508 subclause 6.4.2.5 indicates that the
  //         UE does not respond to paging when paged with GUTI-1 and with CN domain indicator set to "PS"?
  //@siclog "Step 6" 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 );
} // function fl_TC_9_3_1_16_Body


/**
 * @desc Paging for CS fallback / Idle mode
 */
function f_TC_9_3_2_2_EUTRA() runs on EUTRA_PTC
{
  /* Test Purpose (TP)
   * (1)
   * with { UE in state EMM-REGISTERED and EMM-IDLE mode}
   * ensure that {
   * when { UE received Paging for mobile termination CS fallback from NW }
   * then { UE establishes the RRC connection with the RRC establishmentCause set to 'mt-Access' and
   * sends EXTENDED SERVICE REQUEST message}
   * }
   */
  /* Pre-test conditions
   * - Cell A is configured according to Table 6.3.2.2-1 in [18].
   * - Cell A belongs TAI-1.
   * - Cell A is set to the ''Serving cell''.
   * UE:
   * - The UE has a valid GUTI (GUTI-1)
   * - The UE is registered on TAI-1.
   * - The UE is IMSI attached for non-EPS services.
   * Preamble:
   * - 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);
  
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_2_2_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);
}// function f_TC_9_3_2_2_EUTRA

function fl_TC_9_3_2_2_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var NAS_MSG_Indication_Type v_NasInd;
  
  // The SS sends a paging message which CN domain indicates 'CS' domain to the UE.
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page(eutra_CellA,
                  cs_DomainNasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams.MME_Code,
                                                                           v_GutiParams.M_TMSI),
                                               cs));

  // Verify that the UE sends EXTENDED SERVICE REQUEST
  //@siclog "Step 2 - 3" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_EXT_SERVICE_REQUEST(v_SecurityParams.KSIasme,
                                                                                          tsc_EST_CsFallback_MO,
                                                                                          bit2oct(v_GutiParams.M_TMSI))));

  // SERVICE REJECT including EMM cause CS fallback call establishment not allowed.
  //@siclog "Step 4" siclog@
  SRB.send(cas_SRB1_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_NoSecurityProtection,
                                              cs_508_SERVICE_REJECT(tsc_EmmCause_CsFbCallEstNotAllowed)))); // See TS 24.008 clause 10.5.7.4
  
}// function fl_tc_9_3_2_2_Body


/**
 * @desc Service request / Abnormal case / Tracking area update procedure is triggered
 */
function f_TC_9_3_1_15_EUTRA() runs on EUTRA_PTC
{
  /* Test Purpose (TP)
   * (1)
    with { UE having sent a SERVICE REQUEST message }
    ensure that {
      when { Tracking area updating procedure is triggered }
        then { The UE abort the service request procedure, stop timer T3417 if running and perform the tracking area updating procedure, in the TRACKING AREA UPDATE REQUEST message, the "active" flag shall be set to "1"}
                }

    (2)
    with { UE having sent a EXTENDED SERVICE REQUEST message }
    ensure that {
      when { Tracking area updating procedure is triggered }
        then { The UE shall abort the service request procedure, stop timer T3417ext if running and perform the tracking area updating procedure. after the completion of tracking area update procedure, UE restart the CSFB procedure }
                }

   */
  /* Pre-test conditions
    System Simulator:
    -   Cell A, Cell B are configured according to Table 6.3.2.2-1 in [18].
    UE:
    -   The test USIM contains GUTI-1 and TAI-1, and EPS update status is "EU1: UPDATED".
    Preamble:
    -   UE is in state Registered, Idle Mode (state 2) on Cell A according to [18].

  */
  var template (value) CellPowerList_Type v_CellPowerList;
  
  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEmulticell);


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

  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_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_15_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble ( eutra_CellA, E1_IDLE );
  
} // function f_TC_9_3_1_15_EUTRA

function fl_TC_9_3_1_15_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_GutiParams_A := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var GutiParameters_Type v_GutiParams_B := f_EUTRA_CellInfo_GetGuti(eutra_CellB);
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var NAS_PlmnId v_PLMN := f_Asn2Nas_PlmnId (v_GutiParams_A.PLMN_Identity);
  var SRB_COMMON_IND v_ReceivedAsp;
  var NAS_MSG_Indication_Type v_NasInd;
  var EPS_BearerContextStatus v_EPS_ContextStatus;
  var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();
  var NAS_AttDetValue_Type v_AttachType := f_GetEAttachType();
  timer t_T3417 := f_EUTRA_SetTimerToleranceMin( eutra_CellA, 5.0 );
  timer t_T3417ext := f_EUTRA_SetTimerToleranceMin( eutra_CellA, 10.0 );
  timer t_WaitTimer := f_EUTRA_SetTimerToleranceMax( eutra_CellB, 5.0 );

  //+   The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page ( eutra_CellA,
                    cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI)));
  

  //+   The UE transmits a SERVICE REQUEST message. --> SERVICE REQUEST
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));
  t_T3417.start;
  
  //+ The SS does not perform a radio bearer establishment procedure.Note: e.g. SS does not send any AS or NAS messages to UE.
  //@siclog "Step 3" siclog@

  //+ Set the cell type of cell A to the ''Non-Suitable cell''.Set the cell type of cell B to the ''Serving cell''.
  // Note: cell type should be set before timer T3417 expired.
  //@siclog "Step 4" siclog@
  f_EUTRA_SetCellPower ( eutra_CellB, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now );
  f_EUTRA_SetCellPower ( eutra_CellA, tsc_NonSuitableCellRS_EPRE, cs_TimingInfo_Now );
  if( t_T3417.running)
  {
    t_T3417.stop
    }
  else
  {
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "")
      }

  //+  UE initiates RRC connection establishment procedure on cell B.
  //@siclog "Step 5" siclog@
   SRB.receive(car_SRB0_RrcPdu_IND(eutra_CellB,
                                    cr_RRCConnectionRequest(cr_EstablishmentCause_Any)));
   f_EUTRA_RRC_ConnectionSetup_Def (eutra_CellB, cs_TimingInfo_Now, tsc_RRC_TI_Def);

  //+   Check: Does the UE transmit TRACKING AREA UPDATE REQUEST message on cell B in the next 5 seconds?
  //@siclog "Step 6" siclog@
      t_WaitTimer.start;
      alt {
          [] SRB.receive(car_SRB1_RrcNasPdu_IND(eutra_CellB,
                                                cr_508_RRCConnectionSetupComplete(tsc_RRC_TI_Def, ?),
                                                cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                                  cdr_TAU_Request_OldGuti_BearerEst(v_AttachType,
                                                                                                 f_EUTRA_SecurityKSIasme_Get(),
                                                                                                 v_AdditionalUpdateType,
                                                                                                 f_GutiParameters2MobileIdentity(omit, v_GutiParams_A)))))
                         -> value v_ReceivedAsp
           {
                t_WaitTimer.stop;
                f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 6: TAU Request on cell B")
           }
           [] t_WaitTimer.timeout
           {
           }
      }
     
  //+    The SS transmits a TRACKING AREA UPDATE ACCEPT message.
  //@siclog "Step 7" siclog@
  v_EPS_ContextStatus := v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.epsBearerContextStatus;
  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),
                                                                            cds_TAIListNonConsecutive_tlv( v_PLMN, {'0002'O} ), //TAC-2
                                                                            v_EPS_ContextStatus,
                                                                            f_EUTRA_GetCellLAI(eutra_CellB),
                                                                            f_GetMSId ( ),
                                                                            f_GetAdditionalUpdateResult(v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.addUpdateType)) ) ) );

  //+  The UE transmits a TRACKING AREA UPDATE COMPLETE message.
  //@siclog "Step 8" siclog@
  SRB.receive ( car_SRB1_NasPdu_IND ( eutra_CellB,
                                          cr_NAS_Indication ( tsc_SHT_IntegrityProtected_Ciphered,
                                                              cr_508_TAU_Complete ) ) );
      

  //-   EXCEPTION: Steps 9 to 27 describe behaviour that depends on the UE capability. IF pc_CSfallback is true, from Step 9 to step 27should be executed, or else test case is terminated in step 8
  if(pc_CS_Fallback){

  //+   If possible (see ICS) switch off is performed. Otherwise the power is removed.
  //@siclog "Step 9" siclog@

  //+  the UE transmit a DETACH REQUEST with the Detach Type IE indicating "switch off" .( optional)
  //  Note: whether send this message depends on switch-off or power-off.
  //@siclog "Step 10" siclog@
  f_EUTRA_UE_Detach_SwitchOffUe ( eutra_CellB, RRC_CONNECTED, tsc_RRC_TI_Def );

  //+  The UE is switched on.
  //@siclog "Step 11" siclog@
  f_UT_SwitchOnUE(UT, false);

  //+    The UE transmits an ATTACH REQUEST including a PDN CONNECTIVITY REQUEST message on cell B.
  //-   EXCEPTION: Steps 13a1 to 13a2 describe behaviour that depends on UE configuration; the "lower case letter" identifies a step sequence that take place if the UE has ESM information which needs to be transferred.
  //+  IF the UE sets the ESM information transfer flag in the last PDN CONNECTIVITY REQUEST message THEN the SS transmits an ESM INFORMATION REQUEST message to initiate exchange of protocol configuration options and/or APN.
  //+  The UE transmits an ESM INFORMATION RESPONSE message to transfer protocol configuration options and/or APN.
  //+   SS responds with ATTACH ACCEPT message. The ACTIVATE DEFAULT EPS BEARER CONTEXT REQUEST message is piggybacked in ATTACH ACCEPT message
  //+   The UE transmits an ATTACH COMPLETE message including an ACTIVATE DEFAULT EPS BEARER CONTEXT ACCEPT message
  //+   The SS releases the RRC connection.
  //@siclog "Step 12 - 16" siclog@
  f_EUTRA_AttachProcedure (eutra_CellB, "");

  //+  Force the UE to initiate CS Voice call. (Note 1)
  //@siclog "Step 17" siclog@
  f_UT_RequestCSCall(UT);

  //+ Step 18   The UE transmits an EXTENDED SERVICE REQUEST message.
  //@siclog "Step 18" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellB,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoData,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_EXT_SERVICE_REQUEST(v_SecurityParams.KSIasme,
                                                                                          tsc_EST_CsFallback_MO,
                                                                                          bit2oct(v_GutiParams_B.M_TMSI))));
  t_T3417ext.start;

  //+    The SS does not respond to this request. Note: e.g. SS does not send any AS or NAS messages to UE.
  //@siclog "Step 19" siclog@
  //+   Set the cell type of cell A to the '' Serving cell ''.
  //+ Step Set the cell type of cell B to the '' Non-Suitable cell ''.Note: cell type should be set before timer T3417ext expired.
  //@siclog "Step 20" siclog@
  f_EUTRA_SetCellPower ( eutra_CellA, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now );
  f_EUTRA_SetCellPower ( eutra_CellB, tsc_NonSuitableCellRS_EPRE, cs_TimingInfo_Now );
  if( t_T3417ext.running)
  {
    t_T3417ext.stop
    }
  else
  {
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "")
      }

  //+   UE initiates RRC connection establishment procedure on cell A.
  //@siclog "Step 21" siclog@
   SRB.receive(car_SRB0_RrcPdu_IND(eutra_CellA,
                                    cr_RRCConnectionRequest(cr_EstablishmentCause_Any)));
   f_EUTRA_RRC_ConnectionSetup_Def (eutra_CellA, cs_TimingInfo_Now, tsc_RRC_TI_Def);
   
   //+   Check: Does the UE transmit TRACKING AREA UPDATE REQUEST message on cell A in the next 5 seconds?
   //@siclog "Step 22" siclog@
      t_WaitTimer.start;
      alt {
          [] SRB.receive(car_SRB1_RrcNasPdu_IND(eutra_CellA,
                                                cr_508_RRCConnectionSetupComplete(tsc_RRC_TI_Def, ?),
                                                cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                                   cdr_TAU_Request_OldGuti_BearerEst(v_AttachType,
                                                                                                 f_EUTRA_SecurityKSIasme_Get(),
                                                                                                 v_AdditionalUpdateType,
                                                                                                 f_GutiParameters2MobileIdentity(omit, v_GutiParams_B)))))
                         -> value v_ReceivedAsp
           {
                t_WaitTimer.stop;
                f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 22: TAU Request on cell A")
           }
           [] t_WaitTimer.timeout
           {
           }
      }
  //+    The SS transmits a TRACKING AREA UPDATE ACCEPT message.
  //@siclog "Step 23" siclog@
  v_EPS_ContextStatus := v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.epsBearerContextStatus;
  SRB.send ( cas_SRB1_NasPdu_REQ ( eutra_CellA,
                                       cs_TimingInfo_Now,
                                       cs_NAS_Request ( tsc_SHT_IntegrityProtected_Ciphered,
                                                        cs_508_TAU_Accept ( v_AttachType,
                                                                            f_GutiParameters2MobileIdentity(tsc_IEI_Guti, v_GutiParams_A),
                                                                            cds_TAIListNonConsecutive_tlv( v_PLMN, {'0001'O} ), //TAC-1
                                                                            v_EPS_ContextStatus,
                                                                            f_EUTRA_GetCellLAI(eutra_CellA),
                                                                            f_GetMSId ( ),
                                                                            f_GetAdditionalUpdateResult(v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.tRACKING_AREA_UPDATE_REQUEST.addUpdateType)) ) ) );

  //+   The UE transmits a TRACKING AREA UPDATE COMPLETE message.
  //@siclog "Step 24" siclog@
  SRB.receive ( car_SRB1_NasPdu_IND ( eutra_CellA,
                                          cr_NAS_Indication ( tsc_SHT_IntegrityProtected_Ciphered,
                                                              cr_508_TAU_Complete ) ) );
 
  //+  Check: Does the UE transmit EXTENDED SERVICE REQUEST message on cell A? [2,P]
  //@siclog "Step 25" siclog@
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                    cr_508_EXT_SERVICE_REQUEST(v_SecurityParams.KSIasme,
                                                                               tsc_EST_CsFallback_MO,
                                                                               bit2oct(v_GutiParams_A.M_TMSI)))));
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 25");

  //+    The SS sends SERVICE REJECT message in order that the UE enters EMM-REGISTERED.NORMAL-SERVICE.
  //@siclog "Step 26" siclog@
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request(tsc_SHT_NoSecurityProtection,
                                              cs_508_SERVICE_REJECT(tsc_Cause_CSDomainNotAvailable))));

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

  
} // function fl_TC_9_3_1_15_Body


/**
 * @desc Service request / Abnormal case / Procedure collision
 */
function f_TC_9_3_1_17_EUTRA() runs on EUTRA_PTC
{
  /* Test Purpose (TP)
   * (1)
    with { UE having sent a SERVICE REQUEST message }
    ensure that {
      when { UE receives a DETACH REQUEST message with the Type of detach set to 're-attach required' }
        then { UE sends a DETACH ACCEPT message and performs the attach procedure }
                }

    (2)
    with { UE having sent a SERVICE REQUEST message or an EXTENDED SERVICE REQUEST message}
    ensure that {
      when { UE receives a DETACH REQUEST message with the Type of detach set to the value other than 're-attach required' }
        then { UE sends a DETACH ACCEPT message }
                }


   */
  /* Pre-test conditions
    System Simulator:
    -   Cell A
    UE:
    None.
    Preamble:
    -   The 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);
  
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  f_EUTRA_TestBody_Set(true);
  
  fl_TC_9_3_1_17_Body();
  
  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble ( eutra_CellA, E4_DEREGISTERED );
  
} // function f_TC_9_3_1_17_EUTRA

function fl_TC_9_3_1_17_Body() runs on EUTRA_PTC
{
  var GutiParameters_Type v_GutiParams_A := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var NAS_MSG_Indication_Type v_NasInd;


  //+   The SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 1" siclog@
  f_EUTRA_UE_Page ( eutra_CellA,
                    cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI)));
  
  //+   The UE transmits a SERVICE REQUEST message. --> SERVICE REQUEST
  //@siclog "Step 2" siclog@
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));
  
  //+  The SS transmits a DETACH REQUEST message with the Type of detach set to 're-attach required'.
  //@siclog "Step 3" 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 transmit a DETACH ACCEPT message? [1,P]
  //@siclog "Step 4" 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 4");

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

  //+    Check: Does the UE transmit an ATTACH REQUEST message including a PDN CONNECTIVITY REQUEST message?
  //+   The SS transmits an AUTHENTICATION REQUEST message to initiate the EPS authentication and AKA procedure.
  //+  The UE transmits an AUTHENTICATION RESPONSE message and establishes mutual authentication.
  //+    The SS transmits a NAS SECURITY MODE COMMAND message to activate NAS security.
  //+   The UE transmits a NAS SECURITY MODE COMPLETE message and establishes the initial security configuration.
  //   -    EXCEPTION: Steps 11a1 to 11a2 describe behaviour that depends on UE configuration; the "lower case letter" identifies a step sequence that take place if the UE has ESM information which needs to be transferred after NAS SECURITY MODE COMPLETE message.
  //+  IF the UE sets the ESM information transfer flag in the last PDN CONNECTIVITY REQUEST message THEN the SS transmits an ESM INFORMATION REQUEST message to initiate exchange of protocol configuration options and/or APN.
  //+ The UE transmits an ESM INFORMATION RESPONSE message to transfer protocol configuration options and/or APN.
  //+   The SS responds with an ATTACH ACCEPT message. The ACTIVATE DEFAULT EPS BEARER CONTEXT REQUEST message is piggybacked in ATTACH ACCEPT message.
  //   -    EXCEPTION: In parallel to the event described in step 13 below the generic procedure for IP address allocation in the U-plane specified in TS 36.508 subclause 4.5A.1 takes place performing IP address allocation in the U-plane if requested by the UE.
  //+    The UE transmits an ATTACH COMPLETE message including an ACTIVATE DEFAULT EPS BEARER CONTEXT ACCEPT message.
  //+   The SS releases the RRC connection.
  //@siclog "Step 6 - 14" siclog@
  f_EUTRA_AttachProcedure (eutra_CellA, "Step 6");

  //   -    EXCEPTION:  Steps 15a1 to 15b2 describe behaviour that depends on the UE capability; the "lower case letter" identifies a step sequence that take place if a capability is supported
  //+  IF pc_CSfallback is true THEN the SS pages the UE using S-TMSI with CN domain indicator set to "CS".
  //@siclog "Step 15a1" siclog@
  if(pc_CS_Fallback){
    f_EUTRA_UE_Page(eutra_CellA,
                    cs_DomainNasPaging_OneRecord(
                                           cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI),
                                           cs));

  //+  The UE transmits an EXTENDED SERVICE REQUEST message.
  //@siclog "Step 15a2" siclog@
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                    cr_508_EXT_SERVICE_REQUEST(v_SecurityParams.KSIasme,
                                                                               tsc_EST_CsFallback_MT,
                                                                               bit2oct(v_GutiParams_A.M_TMSI)))));
  }
  //+  ELSE the SS pages the UE using S-TMSI with CN domain indicator set to "PS".
  //@siclog "Step 15b1" siclog@
  else{
  f_EUTRA_UE_Page ( eutra_CellA,
                    cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams_A.MME_Code,
                                                                       v_GutiParams_A.M_TMSI)));

  //+  The UE transmits a SERVICE REQUEST message.
  //@siclog "Step 15b2" siclog@
    v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));
  }
  //+  The SS transmits a DETACH REQUEST message with the Type of detach set to the value other than 're-attach required'.
  //@siclog "Step 16" 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_NoReAttach),
                                                                           cs_EMM_Cause_v(tsc_EmmCause_IllegalUe)))));

  //+   Check: Does the UE transmit a DETACH ACCEPT message?
  //@siclog "Step 17" 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 17");

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

} // function fl_TC_9_3_1_17_Body
} // group TestcaseImplementations
  
} // module NAS_ServiceRequest
