/******************************************************************************/
// @copyright   Copyright Notification
//   No part may be reproduced except as authorized by written permission.
//   The copyright and the foregoing restriction extend to reproduction in all media.
//   Trademark 2010, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC).
//   All rights reserved.
// @version: IWD_10wk37
// $Date: 2010-09-16 17:37:41 +0200 (Thu, 16 Sep 2010) $
// $Rev: 3942 $
/******************************************************************************/

module EUTRA_CommonProcedures {
  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from CommonDefs all;
  import from EUTRA_CommonDefs all;
  import from NAS_CommonTypeDefs all;
  import from EPS_NAS_TypeDefs all;
  import from EPS_NAS_MsgContainers all;
  import from EPS_NAS_Constants all;
  import from EUTRA_Component all;
  import from EUTRA_CellInfo all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_SecurityFunctions all;
  import from EUTRA_SecuritySteps all;
  import from UpperTesterFunctions all;
  import from IP_PTC_CtrlMsgs all;
  import from EUTRA_RRC_Templates all;
  import from EPS_NAS_Templates all;
  import from Parameters all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_DRB_Templates all;
  import from EUTRA_LoopBack all;
  import from EUTRA_LoopBack_TypeDefs all;
  import from EUTRA_NASSteps all;
  import from EUTRA_RRCSteps all;
  import from EUTRA_Paging all;
  import from EUTRA_AuxiliaryFunctions all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_Timing all;

  //============================================================================
  // Pre-/Postambles
  //----------------------------------------------------------------------------
  /*
   * @desc      to be used in the test case preamble
   *            N.B. The power level used in this function is tsc_ServingCellRS_EPRE, if you need this cell to be
   *            set to a different level for your test, please adjust the level after this function.
   * @param     p_CellId
   * @param     p_State
   * @param     p_ForcedAttach (optional)
   * @status    APPROVED
   */
  function f_EUTRA_Preamble (CellId_Type p_CellId,
                             IDLEUPDATED_STATE_Type p_State,
                             EUTRA_ATTACH_Type p_ForcedAttach := NORMAL) runs on EUTRA_PTC
  {
    // First switch cell back on
    f_EUTRA_SetCellPower ( p_CellId, tsc_ServingCellRS_EPRE, cs_TimingInfo_Now );

    // Switch on UE
    f_UT_SwitchOnUE (UT, false);

    // Activate DHCP and ICMPv6
    f_DHCPv4_Start(IP, px_IPv4_RemoteAddress, cs_DrbInfo_EUTRA(p_CellId, tsc_DRB1));   /* @sic R5s100135, R5s100189 sic@ */
    // f_ICMPv6_Start(IP, px_IPv6RemoteAddress, cs_DrbInfo_EUTRA(p_CellId, tsc_DRB1));

    // Now continue with the procedure
    f_EUTRA_IdleUpdated(p_CellId, PREAMBLE, p_State, p_ForcedAttach);
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      step 5 .. 17 of UE Registration acc. to 36.508 cl. 4.5.2
   * @param     p_CellId
   * @param     p_State
   * @param     p_ForcedAttach
   * @param     p_NAS_Ind
   * @param     p_ReleaseConnection - default value is to release the connection at the end (i.e. complete step 17) R5-105005
   * @status    APPROVED
   */
  function f_EUTRA_IdleUpdated_Step5_17 (CellId_Type p_CellId,
                                         IDLEUPDATED_STATE_Type p_State,
                                         EUTRA_ATTACH_Type p_ForcedAttach,
                                         NAS_MSG_Indication_Type p_NAS_Ind,
                                         boolean p_ReleaseConnection := true) runs on EUTRA_PTC
  {
    // var GutiParameters_Type v_GutiParameters := f_EUTRA_CellInfo_GetGuti ( p_CellId );
    var HalfOctet_Type v_EpsBearerId := tsc_EpsDefaultBearerId;
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var ProcedureTransactionIdentifier v_EPS_TI := '00'O;
    var ReleaseCause v_ReleaseCause := other;
    var NAS_AttDetValue_Type v_AttachType :=f_GetEAttachType(); // R5-103673
    var NAS_UL_Message_Type v_PdnConnectivityRequest;
    var template (present) SecurityHeaderType v_ExpectedSecurityHeader; // @sic R5s090373 sic@
    var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(p_CellId);
    var NAS_Lac v_LAC := f_EUTRA_CellInfo_GetLocationAreaCode ( p_CellId );
    var NAS_PlmnId v_PLMN := f_Asn2Nas_PlmnId (v_GutiParams.PLMN_Identity);
    var template (omit) LocAreaId v_LAI := f_GetLAI(v_PLMN,v_LAC);
    var template (omit) MobileIdentity v_MSId := f_GetMSId (p_ForcedAttach); // @sic R5-103673 sic@
    var template (omit) ESM_Cause v_ESM_Cause := omit; // @sic R5-104703 sic@
    var NasCount_Type v_NasCountUL;
    var EUTRA_SecurityParams_Type v_AuthParams := f_EUTRA_Security_Get();
    // var ESM_INFORMATION_RESPONSE v_ESMResponse;
    var template (omit) AccessPointName v_APN := omit;
    var template (omit) ProtocolConfigOptions v_Pco := omit;
    var template (value) MobileIdentity v_Guti;
    var boolean v_IPallocationViaNasFlag;
    var template (value) PDN_Address v_PDN_Address;
    var TrackingAreaCode v_TAC := f_EUTRA_CellInfo_GetTAC(p_CellId);

    // Set UE network capabilities as sent by the UE to be used later (see TS 24.301 clause 5.4.3.3)
    v_AuthParams.NAS_SecurityCap := f_EUTRA_FillNAS_SecurityCapability(p_NAS_Ind.Pdu.Msg.aTTACH_REQUEST);  // @sic R5s100135 sic@
    f_EUTRA_Security_Set(v_AuthParams);
    
    if (p_NAS_Ind.SecurityProtection.Status == tsc_SHT_NoSecurityProtection) // @sic R5s090210, R5s090373 sic@
    {
      v_ExpectedSecurityHeader := tsc_SHT_NoSecurityProtection;
    } else {
      v_ExpectedSecurityHeader := (tsc_SHT_NoSecurityProtection, tsc_SHT_IntegrityProtected);
    }

    v_PdnConnectivityRequest := p_NAS_Ind.Pdu.PiggybackedPduList[0].Msg;
    v_EPS_TI := v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.procedureTransactionIdentifier; // Use this value in SS responses
    // Get the PCO to be used later (TS 36.508 v.8.2.2 Table 4.7.3-6)
    if (ispresent( v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions)) {
      v_Pco := v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST.protocolConfigurationOptions;
    }
    // Step 5-6: Authentication
    v_AuthParams:=f_EUTRA_NAS_Authentication(p_CellId,
                                             tsc_SRB1,
                                             v_AuthParams,
                                             tsc_SHT_NoSecurityProtection,
                                             v_ExpectedSecurityHeader,       // @sic R5s090210, R5s090373 sic@
                                             v_PLMN);                        // AUTHENTICATION REQUEST & RESPONSE
    // Step 7-8: NAS Security
    v_NasCountUL := f_EUTRA_NAS_ActivateSecurity(p_CellId, v_AuthParams);   // NAS SECURITY COMMAND & COMPLETE

    // Step 9a1 & 2 - ESM information optionally transferred
    if (f_CheckEsmInfoTransferFlag(v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST)) {
      v_APN := f_ESM_InformationTransfer_GetAPN (p_CellId, v_EPS_TI);
    }
    // optional
    if (p_State == STATE2A_TESTLOOP_ModeA) {
      f_EUTRA_Activate_TestMode (p_CellId,tsc_UE_TestLoopMode_TypeA);
    }
    else if (p_State == STATE2A_TESTLOOP_ModeB) {
      f_EUTRA_Activate_TestMode (p_CellId,tsc_UE_TestLoopMode_TypeB);
    }

    // Step 10-11: RRC Security Mode procedures
    v_AuthParams := f_EUTRA_RRC_ActivateSecurity(p_CellId, v_AuthParams, v_NasCountUL); // RRC SECURITY COMMAND & COMPLETE
    f_EUTRA_Security_Set ( v_AuthParams ); //Saving security parameters

    // Step 12-13 UE Capability check
    f_EUTRA_Capability (p_CellId, v_RRC_TI); // This enquires for the eutra capability but does not check the contents of the message received

    v_Guti := f_GutiParameters2MobileIdentity(tsc_IEI_Guti, f_EUTRA_CellInfo_GetGuti(p_CellId));
    v_IPallocationViaNasFlag :=  f_CheckPCOforIPallocationViaNas (v_Pco); // See TS 36.508 v.8.2.2 Table 4.7.3-6
    // @sic R5-101045 sic@
    if ((p_State == STATE2A_TESTLOOP_ModeB) or (p_State == STATE2A_TESTLOOP_ModeA))
    {
       v_PDN_Address := f_GetPDNAddress (v_IPallocationViaNasFlag, true); // 36.508 Table 4.5.2A.4-3
    }
    else { // @sic R5-103858 sic@
       v_PDN_Address := f_GetPDNAddress (v_IPallocationViaNasFlag);
    }

    // @sic R5-103858 sic@
    if ( pc_IPv6 and pc_IPv4){
     v_ESM_Cause := cs_ESM_Cause_tv('00110010'B); // PDN type IPv4 only allowed @sic R5-104703 sic@
    }
    
    f_EUTRA_RRCConnectionReconfigurationWithAttachAcceptAllParams(p_CellId,
                                                                  v_EpsBearerId,
                                                                  v_RRC_TI,
                                                                  v_EPS_TI,
                                                                  v_AttachType,
                                                                  v_PDN_Address,
                                                                  v_Guti,
                                                                  cs_TAIListNonConsecutive_lv (v_PLMN, {bit2oct(v_TAC)}),
                                                                  v_LAI,
                                                                  v_MSId,
                                                                  v_ESM_Cause, // @sic R5-104703 sic@
                                                                  v_APN,
                                                                  f_GetDefPCOforPCSCFAddress (v_Pco), // @sic R5-104703 sic@
                                                                  p_NAS_Ind.Pdu.Msg.aTTACH_REQUEST.addUpdateType);

    f_EUTRA_NAS_AttachComplete(p_CellId, v_EpsBearerId);                        // step 16
    if (not v_IPallocationViaNasFlag) {                                         /* @sic R5s100135: integration of the IP PTC sic@ */
      f_Delay(f_EUTRA_SetTimerToleranceMax(p_CellId, 1.2));                     /* acc. to 36.508 cl. 4.5.2.3 and cl. 4.5A.1 when the UE does not request the IP address via NAS
                                                                                   it has 1.2s to request it via DHCP */
    }
    if (p_ReleaseConnection) { // @sic R5-105005 Some tests only want to do up to step 16
      f_EUTRA_RRC_ConnectionRelease(p_CellId, v_RRC_TI, v_ReleaseCause);          // step 17
    }
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      UE Registration acc. to 36.508 cl. 4.5.2
   * @param     p_CellId
   * @param     p_Type
   * @param     p_State
   * @param     p_ForcedAttach (optional)
   * @status    APPROVED
   */
  function f_EUTRA_IdleUpdated (CellId_Type p_CellId,
                                IDLEUPDATED_Type p_Type,
                                IDLEUPDATED_STATE_Type p_State,
                                EUTRA_ATTACH_Type p_ForcedAttach := NORMAL) runs on EUTRA_PTC
  {
    // var GutiParameters_Type v_GutiParameters := f_EUTRA_CellInfo_GetGuti ( p_CellId );
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var NAS_AttDetValue_Type v_AttachType :=f_GetEAttachType();
    var B3_Type v_PDNType := f_GetPdnType();
    var NAS_MSG_Indication_Type v_NAS_Ind;
    var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();  //used to follow TTCN3 v411 standard
    var template (present) SecurityHeaderType v_AttReqSecurityHeader;

    if (p_Type == PREAMBLE)
      {
        v_AttReqSecurityHeader := (tsc_SHT_NoSecurityProtection, tsc_SHT_IntegrityProtected);
      }
    else
      {
        v_AttReqSecurityHeader := tsc_SHT_IntegrityProtected;
      }

    // Receive RRC Connection Setup Complete with
    v_NAS_Ind := f_EUTRA_RRC_ConnEst_DefWithNas(p_CellId,
                                                v_RRC_TI,
                                                cr_EstablishmentCause_Any,
                                                cr_NAS_IndicationWithPiggybacking(v_AttReqSecurityHeader,
                                                                                  cr_508_ATTACH_REQUEST(v_AttachType, v_AdditionalUpdateType), // @sic R5-103681 sic@
                                                                                  cr_508_PDN_CONNECTIVITY_REQUEST(tsc_PdnRequest_InitialAttach, v_PDNType)));

    f_EUTRA_IdleUpdated_Step5_17 (p_CellId, p_State, p_ForcedAttach, v_NAS_Ind);

  }

  //----------------------------------------------------------------------------
  /*
   * @desc      This function should be used at the end of every test case to ensure the UE
     *          detaches and release the RRC connection as it is turned off and only then will it
     *          take down the cell the UE is currently camped on  This function has been completely changed in R5-103847
   * @param     p_CellId
   * @param     p_E_State values: E1_IDLE, E2_CONNECTED, E3_TESTMODE, E4_DEREGISTERED
   * @param     p_RRC_TI            (default value: tsc_RRC_TI_Def)
   * @status    APPROVED
   */
  function f_EUTRA_Postamble (CellId_Type p_CellId,
                              EUTRA_POSTAMBLE_STATE_Type p_E_State,
                              RRC_TransactionIdentifier p_RRC_TI := tsc_RRC_TI_Def ) runs on EUTRA_PTC
  {
    if (pc_SwitchOnOff) // The UE can actually be switched off
      {
        select (p_E_State) {
          case (E1_IDLE)
          {
            //Expect the UE to detach and release RRC connection
            f_EUTRA_UE_Detach_SwitchOffUe ( p_CellId, RRC_IDLE, p_RRC_TI, omit );
          }
          case (E2_CONNECTED, E3_TESTMODE)
          {
            //Expect the UE to detach and release RRC connection
            f_EUTRA_UE_Detach_SwitchOffUe ( p_CellId, RRC_CONNECTED, p_RRC_TI, omit );
          }
          case (E4_DEREGISTERED)
          {
            f_UT_SwitchOffUE (UT, false);
          }
        }
      }
    else
      {// The power must be removed from the UE
        f_UT_PowerOffUE (UT);
      }
    //And now remove the cell(s)
    f_EUTRA_ReleaseAllCells();

  } // end of f_EUTRA_Postamble

  //----------------------------------------------------------------------------
  /*
   * @desc      Generic Radio Bearer Establishment (State 3)
   *            If no Dedicated bearers are required, set p_NoOfAddDRBs_AM and p_NoOfAddDRBs_UM to 0 and p_QoS and p_Tft to omit
   *            Otherwise, at least 1 AM DRB will be configured
   * @param     p_CellId
   * @param     p_NoOfAddDRBs_AM
   * @param     p_NoOfAddDRBs_UM
   * @param     p_DRB_ToAddModList              p_NoOfAddDRBs_AM+p_NoOfAddDRBs_UM+1 elements or omit;
   *                                            omit => default setting acc. 36.508 cl. 4.8.2.1.7
   * @param     p_MAC_MainConfig                omit => default setting acc. 36.508 cl. 4.8.2.1.5
   * @param     p_PhysicalConfigDedicated       omit => default setting acc. 36.508 4.8.2.1.6
   * @param     p_QoS
   * @param     p_Tft
   * @param     p_PDCP_StatusReportRequired     (default value: true)
   * @status    APPROVED
   */
  function f_EUTRA_GenericRbEst_Common ( CellId_Type p_CellId,
                                         integer p_NoOfAddDRBs_AM,
                                         integer p_NoOfAddDRBs_UM,
                                         template (omit) DRB_ToAddModList p_DRB_ToAddModList,
                                         template (omit) MAC_MainConfig_Type p_MAC_MainConfig,
                                         template (omit) PhysicalConfigDedicated p_PhysicalConfigDedicated,
                                         template (omit) EPS_QoSList_Type p_QoS,
                                         template (omit) Tft_List_Type p_Tft,
                                         boolean p_PDCP_StatusReportRequired := true )
    runs on EUTRA_PTC
  {
    var GutiParameters_Type v_GutiParameters := f_EUTRA_CellInfo_GetGuti ( p_CellId );

    /* Step 2 */
    f_EUTRA_UE_Page ( p_CellId,
                      cs_NasPaging_OneRecord ( cs_PagingUE_Identity_S_TMSI ( v_GutiParameters.MME_Code, v_GutiParameters.M_TMSI )) ); //@sic R5s100398 sic@
    
    // Steps 3 - 10
    f_EUTRA_ServiceRequestAndActivate_SRB2_DRB ( p_CellId,
                                                 p_NoOfAddDRBs_AM,
                                                 p_NoOfAddDRBs_UM,
                                                 p_DRB_ToAddModList,
                                                 p_MAC_MainConfig,
                                                 p_PhysicalConfigDedicated,
                                                 p_QoS,
                                                 p_Tft,
                                                 p_PDCP_StatusReportRequired);
  }

  //------------------------------------
  /*
   * @desc      common generic function
   * @param     p_CellId
   * @param     p_NoOfAddDRBs_AM
   * @param     p_NoOfAddDRBs_UM
   * @param     p_QoS
   * @param     p_Tft
   * @status    APPROVED
   */
  function fl_EUTRA_GenericRbEst ( CellId_Type p_CellId,
                                   integer p_NoOfAddDRBs_AM,
                                   integer p_NoOfAddDRBs_UM,
                                   template (omit) EPS_QoSList_Type p_QoS,
                                   template (omit) Tft_List_Type p_Tft ) runs on EUTRA_PTC
  {
    var template (omit) DRB_ToAddModList v_DRB_ToAddModList := omit;                // => apply default setiing acc. 36.508 cl. 4.8.2.1.7 */
    var template (omit) MAC_MainConfig_Type v_MAC_MainConfig := omit;               // => apply default setting acc. 36.508 cl. 4.8.2.1.5 */
    var template (omit) PhysicalConfigDedicated v_PhysicalConfigDedicated := omit;  // => apply default setting acc. 36.508 cl. 4.8.2.1.6 */
    f_EUTRA_GenericRbEst_Common(p_CellId,
                                p_NoOfAddDRBs_AM,
                                p_NoOfAddDRBs_UM,
                                v_DRB_ToAddModList,
                                v_MAC_MainConfig,
                                v_PhysicalConfigDedicated,
                                p_QoS,
                                p_Tft);
  }

  //------------------------------------
  // wrapper functions
  /*
   * @desc      no additional bearer will be configured
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_RbEst_Def(CellId_Type p_CellId) runs on EUTRA_PTC
  {
    fl_EUTRA_GenericRbEst(p_CellId, 0, 0, omit, omit);
  }
  //------------------------------------
  /*
   * @desc      Steps 6-9 of 4.5.3.3 will be performed as a postamble, and no dedicated bearers will be activated
   * @param     p_CellId
   * @param     p_NAS_Ind
   * @status    APPROVED
   */
  function f_EUTRA_ServiceReqPostamble_508Steps6_9(CellId_Type p_CellId,
                                                   NAS_MSG_Indication_Type p_NAS_Ind) runs on EUTRA_PTC
  {
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;

    f_EUTRA_Activate_SRB2_DRB_SendRrcMsg ( p_CellId, v_RRC_TI, p_NAS_Ind.SecurityProtection.NasCount); /* @sic R5s100315 sic@ */
    f_EUTRA_Activate_SRB2_DRB_ReceiveRrcAndNasMsg ( p_CellId, v_RRC_TI);                               /* @sic R5s100315 sic@ */
  }
  //------------------------------------
  /*
   * @desc      additional UM bearer will be established
   * @param     p_CellId
   * @status    (APPROVED)
   */
  function f_EUTRA_RbEst_508_UM(CellId_Type p_CellId) runs on EUTRA_PTC
  {
    var template (value) EPS_QoSList_Type v_Qos_List := {cs_508_EPS_QoS_Dedicated_1};
    var template (value) Tft_List_Type v_Tft_List := {cs_508_Tft_Dedicated_1(tsc_EpsDedicatedBearerId)};
    fl_EUTRA_GenericRbEst(p_CellId, 0, 1, v_Qos_List, v_Tft_List);
  }
  //------------------------------------
  /*
   * @desc      additional AM bearer will be established
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_RbEst_508_AM(CellId_Type p_CellId) runs on EUTRA_PTC
  {
    var template (value) EPS_QoSList_Type v_Qos_List := {cs_508_EPS_QoS_Dedicated_1};
    var template (value) Tft_List_Type v_Tft_List := {cs_508_Tft_Dedicated_1(tsc_EpsDedicatedBearerId)};
    fl_EUTRA_GenericRbEst(p_CellId, 1, 0, v_Qos_List, v_Tft_List);
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      Brings UE to state 4, with default DRB (AM) and an additional AM bearer being established;
   *            SRBs and the default bearer are expected to be established at the SS beforehand;
   *            Put UE in Loop Back Mode A.
   *            Configures SS in normal mode
   * @param     p_CellId
   * @param     p_UE_TestLoopModeA_LB_Setup
   * @status    APPROVED
   */
  function f_EUTRA_LoopBackActivation_State4_Def(CellId_Type p_CellId,
                                                 template (value) UE_TestLoopModeA_LB_Setup_Type p_UE_TestLoopModeA_LB_Setup) runs on EUTRA_PTC
  {
    f_EUTRA_RbEst_Def(p_CellId);                                                      // bring UE to State 3
    f_EUTRA_CloseUE_TestLoopModeA(p_CellId, p_UE_TestLoopModeA_LB_Setup);             // Step 1-2
  }
  //------------------------------------
  function f_EUTRA_LoopBackActivation_State4_508_UM(CellId_Type p_CellId,
                                                    template (value) UE_TestLoopModeA_LB_Setup_Type p_UE_TestLoopModeA_LB_Setup) runs on EUTRA_PTC
  { /* Brings UE to state 4, with default DRB (AM) and an additional UM bearer being established;
       SRBs and DRBs are expected to be established at the SS beforehand;
       Put UE in Loop Back Mode A.
       Configures SS in normal mode */
    f_EUTRA_RbEst_508_UM(p_CellId);                                                   // bring UE to State 3
    f_EUTRA_CloseUE_TestLoopModeA(p_CellId, p_UE_TestLoopModeA_LB_Setup);             // Step 1-2
  }
  //------------------------------------
  function f_EUTRA_LoopBackActivation_State4_508_AM(CellId_Type p_CellId,
                                                    template (value) UE_TestLoopModeA_LB_Setup_Type p_UE_TestLoopModeA_LB_Setup) runs on EUTRA_PTC
  { /* Brings UE to state 4, with default DRB (AM) and an additional AM bearer being established;
       SRBs and DRBs are expected to be established at the SS beforehand;
       Put UE in Loop Back Mode A.
       Configures SS in normal mode */
    f_EUTRA_RbEst_508_AM(p_CellId);                                                   // bring UE to State 3
    f_EUTRA_CloseUE_TestLoopModeA(p_CellId, p_UE_TestLoopModeA_LB_Setup);             // Step 1-2
  }

  //****************************************************************************
  // Check functions acc. to TS 36.508
  //----------------------------------------------------------------------------

  group Checkfunctions {

  /*
   * @desc      Test procedure to check Idle state, see TS 36.508 clause 6.4.2.2
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_508Check_IdleState(CellId_Type p_CellId) runs on EUTRA_PTC
  {
    // Step 1- 10  this is the same as the generic bearer establishment procedure, but without configuring any additional bearers @sic R5-102082 sic@
    f_EUTRA_RbEst_Def(p_CellId);
    
    f_EUTRA_RRC_ConnectionRelease(p_CellId, tsc_RRC_TI_Def, other);
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "508Check IdleState");
  } // f_EUTRA_508Check_IdleState

  /*
   * @desc      Test procedure to check Idle state, see TS 36.508 clause 6.4.2.3
   * @param     p_CellId
   * @status    APPROVED
   */
  function f_EUTRA_508Check_ConnectedState ( CellId_Type  p_CellId ) runs on EUTRA_PTC
  {
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;

    SRB.send ( cas_SRB1_RrcPdu_REQ( p_CellId,
                                    cs_TimingInfo_Now,
                                    cs_508_UeCapabilityEnquiry ( v_RRC_TI ) ) );

    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId, cr_508_UeCapabilityInformation ( v_RRC_TI, cr_EutraCapInfo ) ) );

    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "508Check ConnectedState");
  }

  /*
   * @desc      Test procedure to check that UE is camped on a new E-UTRAN cell, see TS 36.508 clause 6.4.2.7
   * @param     p_CellId
   * @param     p_RRC_TI
   * @status    APPROVED
   */
  function f_EUTRA_508CheckCampOnNewEutraCell ( CellId_Type               p_CellId,
                                                RRC_TransactionIdentifier p_RRC_TI := tsc_RRC_TI_Def ) runs on EUTRA_PTC
  {
    /* Checks whether the UE is camping on a new E-UTRAN cell with different TAI of a test case or not.
       Procedure is defined in 36.508 cl. 6.4.2.7*/
    f_EUTRA_TrackingAreaUpdate ( p_CellId, p_RRC_TI );
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "508Check CampOnNewEutraCell");
  }

  /* // @sic CR R5-102241 @sic
   * @desc      Test procedure to check that UE is camped on a new E-UTRAN cell, see TS 36.508 clause 6.4.2.7A
   * @param     p_CellId
   * @param     p_RRC_TI
   */
  function f_EUTRA_508CheckCampOnNewEutraCellUponMobilityFromAnotherRAT ( CellId_Type               p_CellId,
                                                RRC_TransactionIdentifier p_RRC_TI := tsc_RRC_TI_Def ) runs on EUTRA_PTC
  {
    /* Checks whether the UE is camping on a new E-UTRAN cell with different TAI of a test case or not.
       Procedure is defined in 36.508 cl. 6.4.2.7A*/
    f_EUTRA_TrackingAreaUpdateFromAnotherRAT ( p_CellId, p_RRC_TI );
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "508Check CampOnNewEutraCellUponMobilityfromAnotherRAT");
  }
  

  //----------------------------------------------------------------------------
  } // end of group Checkfunctions
  /*
   * @desc      To trigger the UE to establish a Dedicated EPS bearer context
   *            see 36.508 clause 6.4.3.1
   * @param     p_CellId
   * @param     p_RRCState
   * @param     p_EpsBearerId                           .. Identity of the Dedicated Bearer that will be configured
   * @status
   *
   */
  function f_EUTRA_508_EstablishDedicatedEPSContext (CellId_Type p_CellId,
                                             EUTRA_RRC_STATE_Type p_RRCState,
                                             EPS_BearerIdentity p_EpsBearerId := tsc_EpsDedicatedBearerId) runs on EUTRA_PTC
  {
    var BEARER_RESOURCE_ALLOCATION_REQUEST v_BEARER_RESOURCE_ALLOCATION_Req;
    var  ProcedureTransactionIdentifier v_EpsTi := tsc_PTI_Unassigned;
    var NAS_MSG_Indication_Type v_NasInd;
    var NasCount_Type v_NasCountUL;
    var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
    var template (value) EUTRA_FDD_TDD_CellInfo_Type v_EUTRA_FDD_TDD_Info := f_EUTRA_CellInfo_GetFDD_TDD_Info (p_CellId); //Get FDD or TDD mode from Cell-configuration
    
    // Cause the UE to request bearer resource allocation of
    // dedicated EPS bearer associated with first PDN connectivity
    f_UT_RequestActivateDedicatedBearer (UT,
                                         hex2int(p_EpsBearerId),
                                         hex2int(tsc_EpsDefaultBearerId),
                                         cs_508_EPS_QoS_Dedicated_1,
                                         cs_508_Tft_Dedicated_1(p_EpsBearerId));
    
    if ( p_RRCState == RRC_IDLE ){
      // UE is in RRC Idle mode
      // Steps 2a1 to 2a7
      v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(p_CellId,
                                                 tsc_RRC_TI_Def,
                                                 ?, // @sic R5s100326 sic@
                                                 cr_NAS_Indication (tsc_SHT_IntegrityProtected,
                                                                    cr_508_SERVICE_REQUEST(f_EUTRA_SecurityKSIasme_Get())));
      
      // activate security in AS with parameters from AKA performed in preamble
      v_NasCountUL := v_NasInd.SecurityProtection.NasCount;
      v_SecurityParams := f_EUTRA_Authentication_InitAS(v_SecurityParams, v_NasCountUL);
      v_SecurityParams := f_EUTRA_RRC_ActivateSecurity(p_CellId,
                                                       v_SecurityParams,
                                                       v_NasCountUL);
      f_EUTRA_Security_Set(v_SecurityParams);
      
      SRB.send(cas_SRB1_RrcPdu_REQ(p_CellId,
                                   cs_TimingInfo_Now,
                                   cs_RRCConnectionReconfiguration_Common(tsc_RRC_TI_Def,
                                                                          omit,
                                                                          omit,
                                                                        cs_508_RadioResourceConfigDedicated_Srb2( {cs_508_DRB_ToAddMod_DEFAULT_AM (tsc_DRB1)},
                                                                                                                  cs_508_MAC_MainConfig_Explicit_RBC_DrxL,
                                                                                                                  cs_508_PhysicalConfigDedicated_Default_RBC(v_EUTRA_FDD_TDD_Info.cqi_ReportConfig,
                                                                                                                                                             v_EUTRA_FDD_TDD_Info.soundingRS_UL_Config,
                                                                                                                                                             f_EUTRA_CellInfo_GetAntennaInfoDedicated (eutra_CellA))),
                                                                          omit)));
      
      // receive RRCConnectionReconfigurationComplete
      SRB.receive(car_SRB1_RrcPdu_IND(p_CellId,
                                      cr_508_RRCConnectionReconfigurationComplete(tsc_RRC_TI_Def)));
      
    } // end of if RRC_Idle
    // UE is now in RRC connected mode

    // Step 3. The UE transmits a BEARER RESOURCE ALLOCATION REQUEST message
    SRB.receive ( car_SRB2_NasPdu_IND ( p_CellId,
                                        cr_NAS_Indication ( tsc_SHT_IntegrityProtected_Ciphered,
                                                            cr_508_BearerResourceAllocReq(?,
                                                                                          tsc_EpsDefaultBearerId))));

    // store PTI received
    v_BEARER_RESOURCE_ALLOCATION_Req  := v_NasInd.Pdu.Msg.bEARER_RESOURCE_ALLOCATION_REQUEST;
    v_EpsTi := v_BEARER_RESOURCE_ALLOCATION_Req.procedureTransactionIdentifier;
    // The SS transmits an ACTIVATE DEDICATED EPS BEARER CONTEXT REQUEST message.
    f_EUTRA_TxDedicatedEpsBearerCtxtReq(p_CellId,
                                        tsc_DRB2,
                                        p_EpsBearerId,
                                        v_EpsTi,
                                        cs_LinkedId(hex2bit( tsc_EpsDefaultBearerId)),
                                        cs_508_EPS_QoS_Dedicated_1,
                                        cs_508_Tft_Dedicated_1(p_EpsBearerId));
    
    
    //The UE transmits an ACTIVATE DEDICATED EPS BEARER CONTEXT ACCEPT message
    SRB.receive(car_SRB2_NasPdu_IND(p_CellId,
                                    cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                      cr_508_ActDedicatedEPSBearerCxtAccept(p_EpsBearerId))));
    }

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

}
