/******************************************************************************/
// @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 13:09:07 +0200 (Thu, 16 Sep 2010) $
// $Rev: 3930 $
/******************************************************************************/

module RRC_Others
{

//=========================================================================
// Import Part
//=========================================================================

    import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
    import from CommonIratDefs all;
    import from EUTRA_CommonDefs all;
    import from EUTRA_Component all;
    import from EUTRA_ASP_SrbDefs all;
    import from EUTRA_CommonProcedures all;
    import from EUTRA_SecurityFunctions all;
    import from EUTRA_RRCSteps all;
    import from EUTRA_NASSteps all;
    import from EUTRA_SecuritySteps all;
    import from EUTRA_ConfigurationSteps all;
    import from EUTRA_AuxiliaryFunctions all;
    import from EUTRA_AspCommon_Templates all;
    import from EUTRA_SRB_Templates all;
    import from EUTRA_RRC_Templates all;
    import from EUTRA_CellInfo all;
    import from EUTRA_IRATFunctions all;
    import from EUTRA_Parameters all;

    import from UpperTesterFunctions all;
    import from CommonDefs all;
    import from Parameters all;

    import from EPS_NAS_TypeDefs all;
    import from NAS_CommonTypeDefs all;
    import from EPS_NAS_MsgContainers all;
    import from EPS_NAS_Constants all;
    import from EPS_NAS_Templates all;

    import from UTRAN_CapabilityFunctions all;
    import from GERAN_CapabilityFunctions all;

//=========================================================================
// Templates
//=========================================================================

  template SupportedBandEUTRA cr_SupportedBandEUTRA :=
  {
    bandEUTRA := (1..64),
    halfDuplex := ?
  };
  
  template UE_EUTRA_Capability cr_UeCapabilitiesRAT_Container :=
  { /* @status    APPROVED */
    accessStratumRelease := rel8,
    ue_Category := px_eUE_Category_Type,
    pdcp_Parameters := ?,
    phyLayerParameters := {
      ue_TxAntennaSelectionSupported := ?,
      ue_SpecificRefSigsSupported := ?
    },
    rf_Parameters := {
      supportedBandListEUTRA := ?
    },
    measParameters := {
      bandListEUTRA := ?
    },
    featureGroupIndicators := fl_DeriveFeatrGrpCapFromPics(),
    interRAT_Parameters := {
      utraFDD := *,
      utraTDD128 := *,
      utraTDD384 := *,
      utraTDD768 := *,
      geran := *,
      cdma2000_HRPD := *,
      cdma2000_1xRTT := *
    },
    nonCriticalExtension := *
  };
  
  template (value) UE_CapabilityRequest cs_AllOtherRatCapReq := {utra, geran_cs, geran_ps, cdma2000_1XRTT};    /* @status    APPROVED */

  template UE_CapabilityRAT_Container cr_CDMA2K_UE_CapabilityRAT_Container :=
  { /* @status    APPROVED */
    rat_Type := cdma2000_1XRTT,
    ueCapabilityRAT_Container := ?
  };

  template UE_CapabilityRAT_Container cr_UE_CapabilityRAT_Container (RAT_Type p_RAT_Type) :=
  { /* @status    APPROVED */
    rat_Type := p_RAT_Type,
    ueCapabilityRAT_Container := ?
  };

  template UECapabilityInformation_r8_IEs cr_UECapabilityInformation (template (present) UE_CapabilityRAT_ContainerList p_UE_CapabilityRAT_ContainerList) :=
  { /* @status    APPROVED */
    ue_CapabilityRAT_ContainerList := p_UE_CapabilityRAT_ContainerList,
    nonCriticalExtension := *
  };

//=========================================================================
// Functions
//=========================================================================

  /*
   * @desc      convert the PICS parameters pc_FeatrGrp_X from single booleans to a bitstring of size 32.
   *            Size 32 is chose because the signaled element featureGroupIndicators in UE-EUTRA-Capability is of size 32.
   * @return    B32_Type
   * @status    APPROVED
   */
  function fl_DeriveFeatrGrpCapFromPics () return B32_Type
  {
    var B32_Type v_FeatureGroupIndicators := f_ConvertBoolToBit(pc_FeatrGrp_1) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_2) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_3) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_4) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_5) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_6) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_7) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_8) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_9) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_10) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_11) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_12) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_13) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_14) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_15) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_16) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_17) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_18) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_19) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_20) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_21) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_22) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_23) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_24) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_25) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_26) &
                                             f_ConvertBoolToBit(pc_FeatrGrp_27) & // @sic R5-103621 sic@
                                             '00000'B;
    return v_FeatureGroupIndicators;
  }

  /*
   * @desc      Set the interRAT-Parameters entries of the expected template.
   *            Expected template is to be used when matching with the received template.
   * @param     p_UE_EUTRA_Capability
   * @return    template UE_EUTRA_Capability
   * @status    APPROVED
   */
  function fl_EUTRA_SetExpectedUeEutraCap ( template UE_EUTRA_Capability p_UE_EUTRA_Capability )
    return template UE_EUTRA_Capability
  {
    var template UE_EUTRA_Capability v_UeCapabilityTemplate := p_UE_EUTRA_Capability;

    if ( pc_FDD ) {
      v_UeCapabilityTemplate.interRAT_Parameters.utraFDD := ?;
    }
    if ( pc_TDD_LCR ){
      v_UeCapabilityTemplate.interRAT_Parameters.utraTDD128 := ?;
    }
    if ( pc_TDD_HCR ){
      v_UeCapabilityTemplate.interRAT_Parameters.utraTDD384 := ?;
    }
    if ( pc_TDD_VHCR ){
      v_UeCapabilityTemplate.interRAT_Parameters.utraTDD768 := ?;
    }
    if ( pc_GERAN ){
      v_UeCapabilityTemplate.interRAT_Parameters.geran := ?;
    }
    if ( pc_HRPD ){
      v_UeCapabilityTemplate.interRAT_Parameters.cdma2000_HRPD := ?;
    }
    if ( pc_1xRTT ){
      v_UeCapabilityTemplate.interRAT_Parameters.cdma2000_1xRTT := ?; // @sic R5s100302 Change 9 sic@
    }
    
    return v_UeCapabilityTemplate;
  }

  /*
   * @desc      Fill the ue-CapabilityRAT-ContainerList in the expected template based on set PICS parameters
   *            Filled template will be used in the receiving statement in function fl_AllRatCapabilityCheck.
   * @return    template UECapabilityInformation_r8_IEs
   * @status    APPROVED
   */
  function fl_SetUeOtherSupRatCap ( ) return template UECapabilityInformation_r8_IEs
  {
    var template (present) UE_CapabilityRAT_ContainerList v_UE_CapabilityRAT_ContainerList;
    var integer i := 0;

    if ( pc_GERAN ){
      v_UE_CapabilityRAT_ContainerList[i] := cr_UE_CapabilityRAT_Container(geran_cs);
      i := i + 1;
      v_UE_CapabilityRAT_ContainerList[i] := cr_UE_CapabilityRAT_Container(geran_ps);
      i := i + 1;
    }
    if ( pc_FDD or pc_TDD_LCR or pc_TDD_HCR or pc_TDD_VHCR ) {
      v_UE_CapabilityRAT_ContainerList[i] := cr_UE_CapabilityRAT_Container(utra);
      i := i + 1;
    }
    if ( pc_HRPD or pc_1xRTT ){
      v_UE_CapabilityRAT_ContainerList[i] := cr_UE_CapabilityRAT_Container(cdma2000_1XRTT);
      i := i + 1;
    }

    select (i) {
      case (1) {
        // do nothing
      }
      case (2) {
        v_UE_CapabilityRAT_ContainerList := { permutation ( v_UE_CapabilityRAT_ContainerList[0],
                                                            v_UE_CapabilityRAT_ContainerList[1] ) };
      }
      case (3) {
        v_UE_CapabilityRAT_ContainerList := { permutation ( v_UE_CapabilityRAT_ContainerList[0],
                                                            v_UE_CapabilityRAT_ContainerList[1],
                                                            v_UE_CapabilityRAT_ContainerList[2] ) };
      }
      case (4) {
        v_UE_CapabilityRAT_ContainerList := { permutation ( v_UE_CapabilityRAT_ContainerList[0],
                                                            v_UE_CapabilityRAT_ContainerList[1],
                                                            v_UE_CapabilityRAT_ContainerList[2],
                                                            v_UE_CapabilityRAT_ContainerList[3] ) };
      }
      case else {
        FatalError(__FILE__, __LINE__, "invalid PICS configuration");
      }
    }
    return cr_UECapabilityInformation(v_UE_CapabilityRAT_ContainerList);
  }

  /*
   * @desc      Function to convert the PICS parameters pc_eBandX_Supp from single booleans to a bitstring.
   *            The leftmost bit represent eBand1 support ('1'B if supported according to PICS pc_eBand1_Supp
   *            and '0'B if not supported according to PICS pc_eBand1_Supp).
   * @return    B40_Type
   * @status    APPROVED
   */
  function fl_GetNrOfSuppEutraBandsBitstring (  ) return B40_Type
  {
    var B40_Type v_Bitstring := f_ConvertBoolToBit(pc_eBand1_Supp) &
                                f_ConvertBoolToBit(pc_eBand2_Supp) &
                                f_ConvertBoolToBit(pc_eBand3_Supp) &
                                f_ConvertBoolToBit(pc_eBand4_Supp) &
                                f_ConvertBoolToBit(pc_eBand5_Supp) &
                                f_ConvertBoolToBit(pc_eBand6_Supp) &
                                f_ConvertBoolToBit(pc_eBand7_Supp) &
                                f_ConvertBoolToBit(pc_eBand8_Supp) &
                                f_ConvertBoolToBit(pc_eBand9_Supp) &
                                f_ConvertBoolToBit(pc_eBand10_Supp) &
                                f_ConvertBoolToBit(pc_eBand11_Supp) &
                                f_ConvertBoolToBit(pc_eBand12_Supp) &
                                f_ConvertBoolToBit(pc_eBand13_Supp) &
                                f_ConvertBoolToBit(pc_eBand14_Supp) &
                                '0'B &//Band 15 is reserved
                                '0'B &//Band 16 is reserved
                                f_ConvertBoolToBit(pc_eBand17_Supp) &
                                f_ConvertBoolToBit(pc_eBand18_Supp) &
                                f_ConvertBoolToBit(pc_eBand19_Supp) &
                                f_ConvertBoolToBit(pc_eBand20_Supp) & // @sic R5-103122 sic@
                                f_ConvertBoolToBit(pc_eBand21_Supp) & // @sic R5-103122 sic@
                                '0'B &//Band 22
                                '0'B &//Band 23
                                '0'B &//Band 24
                                '0'B &//Band 25
                                '0'B &//Band 26
                                '0'B &//Band 27
                                '0'B &//Band 28
                                '0'B &//Band 29
                                '0'B &//Band 30
                                '0'B &//Band 31
                                '0'B &//Band 32
                                f_ConvertBoolToBit(pc_eBand33_Supp) &
                                f_ConvertBoolToBit(pc_eBand34_Supp) &
                                f_ConvertBoolToBit(pc_eBand35_Supp) &
                                f_ConvertBoolToBit(pc_eBand36_Supp) & /* @sic R5s100020 sic@ */
                                f_ConvertBoolToBit(pc_eBand37_Supp) &
                                f_ConvertBoolToBit(pc_eBand38_Supp) &
                                f_ConvertBoolToBit(pc_eBand39_Supp) &
                                f_ConvertBoolToBit(pc_eBand40_Supp);
    return v_Bitstring;
  }

  /*
   * @desc      Function which returns the total number of supported bands according to the set PICS parameters pc_eBandX_Supp.
   * @return    integer
   * @status    APPROVED
   */
  function fl_GetNrOfSuppEutraBands (  ) return integer
  {
    var integer v_NrOfSuppEutraBands := 0;
    var integer i:= 0;
    var B40_Type v_Bitstring := fl_GetNrOfSuppEutraBandsBitstring();

    for ( i := 0; i < lengthof ( v_Bitstring ); i := i + 1 ) {
      if ( v_Bitstring[i] == '1'B ) {
        v_NrOfSuppEutraBands := v_NrOfSuppEutraBands + 1;
      }
    }
    return v_NrOfSuppEutraBands;
  }
  
/*
   * @desc      Function returns the number of supported interRat bands listed in all IEs in interRAT_Parameters.
   *            @sic R5s100302 Change 7 : Creation of new function for counting the number of supported interRat bands sic@
   * @param     p_UECaps
   * @return    integer
   * @status    APPROVED
   */
  function fl_Count_Supported_interRAT_Bands ( UE_EUTRA_Capability p_UECaps ) return integer
  {
    var integer v_NrOfSuppIratBands := 0;
 
    if (ispresent (p_UECaps.interRAT_Parameters.utraFDD)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.utraFDD.supportedBandListUTRA_FDD);
    };
    if (ispresent (p_UECaps.interRAT_Parameters.utraTDD128)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.utraTDD128.supportedBandListUTRA_TDD128);
    };
    if (ispresent (p_UECaps.interRAT_Parameters.utraTDD384)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.utraTDD384.supportedBandListUTRA_TDD384);
    };
    if (ispresent (p_UECaps.interRAT_Parameters.utraTDD768)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.utraTDD768.supportedBandListUTRA_TDD768);
    };
    if (ispresent (p_UECaps.interRAT_Parameters.geran)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.geran.supportedBandListGERAN);
    }
    if (ispresent (p_UECaps.interRAT_Parameters.cdma2000_HRPD)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.cdma2000_HRPD.supportedBandListHRPD);
    }
    if (ispresent (p_UECaps.interRAT_Parameters.cdma2000_1xRTT)){
      v_NrOfSuppIratBands := v_NrOfSuppIratBands + lengthof(p_UECaps.interRAT_Parameters.cdma2000_1xRTT.supportedBandList1XRTT);
    }
    
    return v_NrOfSuppIratBands;
  }

  /*
   * @desc      Function to match the received UECapabilityInformation with the expected UECapabilityInformation.
   *            Reference 36.523-1 Table 8.5.4.1.3.3-2
   * @param     p_Octetstring                    ..  received ueCapabilitiesRAT-Container
   * @param     p_Exptected_Ue_Eutra_Capability  ..  expected ueCapabilitiesRAT-Container (set by function fl_EUTRA_SetUeEutraCapability )
   * @status    APPROVED
   */
  function fl_EUTRA_MatchEutraCapMsgs ( octetstring p_Octetstring,
                                        template UE_EUTRA_Capability p_Exptected_Ue_Eutra_Capability ) runs on EUTRA_PTC
  {
    var UE_EUTRA_Capability v_ReceivedCapabilityMsg;
    var integer i, k, l;
    var B40_Type v_NrOfSuppEutraBandsBitstring := fl_GetNrOfSuppEutraBandsBitstring();
    var charstring v_FailedEutraBand;
    var boolean v_BandSuppMatch := false;

    v_ReceivedCapabilityMsg := f_EUTRA_DecodeEutraCapMsg ( p_Octetstring );

    //* @desc Check that number of supported EUTRA bands match the set PICS parameters (pc_eBanda_Supp)
    if ( lengthof ( v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA ) != fl_GetNrOfSuppEutraBands() ) {
      //* @verdict fail Number of Supported EUTRA Bands according to PICS parameters
      // does not match number of entries in SupportedBandListEUTRA
      f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 10/18 - Missmatch between pc_eBanda_Supp and number of reported supported EUTRA bands");
    }

    //* @desc Check that any value B such that pc_eBandB_Supp is TRUE and different from all eutra-Band[k] where k = 1 to a - 1
    //* Go through the received supportedBandListEUTRA and match against the supported bands according to the PICS (received from fl_GetNrOfSuppEutraBandsBitstring).
    for ( k := 0; k < lengthof ( v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA ); k := k + 1 ){
      v_BandSuppMatch := false;
      for ( l := 0; l < lengthof ( v_NrOfSuppEutraBandsBitstring ); l := l + 1 ){
        //* @desc For each supported band (according to the set PICS) match the band with what is reported according to k.
        if ( (l+1) == v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA[k].bandEUTRA ){
          if ( v_NrOfSuppEutraBandsBitstring[l] == '0'B ){
            v_FailedEutraBand := int2char ( l + 1 );
            //* @verdict fail PICS setting does not match reported capability for band nr. l+1
            // or band-support har been reported more than once
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, v_FailedEutraBand);
          }
          else {
            //* UE reports support of band v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA[k].bandEUTRA
            // and the correspponing PICS parameter is also set.
            v_NrOfSuppEutraBandsBitstring[l] := '0'B;
            v_BandSuppMatch := true;
          }
        }
      }
      if ( v_BandSuppMatch == false ) {
        //* @verdict fail UE reports support of band v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA[k].bandEUTRA
        // but corresponding PICS is not set to true.
        v_FailedEutraBand := int2char ( v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA[k].bandEUTRA );
        f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, v_FailedEutraBand);
      }
    }

    //* @desc interFreqEUTRA-BandList SEQUENCE (SIZE (1..maxBands)) OF SEQUENCE {
    //* same number of entries like in supportedEUTRA-BandList
    //* Match number of entries in interFreqEUTRA-BandList with number of entries in supportedEUTRA-BandList
    for ( i := 0; i < lengthof ( v_ReceivedCapabilityMsg.measParameters.bandListEUTRA ); i := i + 1 ) {
      if ( lengthof ( v_ReceivedCapabilityMsg.rf_Parameters.supportedBandListEUTRA ) != lengthof ( v_ReceivedCapabilityMsg.measParameters.bandListEUTRA[i].interFreqBandList ) )
      {
        //* @verdict fail Number of interFreqEUTRA-BandList does not match nr. of supportedEUTRA-BandList
        f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 10/18 - Number of interFreqEUTRA-BandList does not match nr. of supportedEUTRA-BandList");
      }
    }

    //@sic R5s100302 Change 6 : Correction of check of supported interRat bands sic@
    // Check that number of bands in interRAT-Parameters matches number of entries in interRAT-BandList
    // According to 36.331:
    // interRAT-BandList - One entry corresponding to each supported band of another RAT
    //                     listed in the same order as in the interRAT-Parameters.
    for ( i := 0; i < lengthof ( v_ReceivedCapabilityMsg.measParameters.bandListEUTRA ); i := i + 1 ) {
      if ( ispresent ( v_ReceivedCapabilityMsg.measParameters.bandListEUTRA[i].interRAT_BandList ) )
      {
        if ( lengthof ( v_ReceivedCapabilityMsg.measParameters.bandListEUTRA[i].interRAT_BandList ) != fl_Count_Supported_interRAT_Bands(v_ReceivedCapabilityMsg))
        {
          //* @verdict fail Number of elements in interRAT-Parameters does not match number of entires in interRAT-BandList
          f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 10/18 - Number of listed bands in interRAT-Parameters does not match number of entires in interRAT-BandList");
        }
      }
    }

    //* @desc Match the received decoded template with the expected template
    if ( not match ( v_ReceivedCapabilityMsg, p_Exptected_Ue_Eutra_Capability )){
      //* @verdict fail Received decoded template does not match with the expected template
      f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 10/18 - Received decoded template does not match with the expected template");
    }
  }

  /*
   * @desc      decodes and checks the ueCapabilitiesRAT-Containers for every RAT except EUTRA
   * @param     p_ReceivedUeCapInfo
   * @status    APPROVED
   */
  function fl_EUTRA_CheckOtherRatCap ( UECapabilityInformation p_ReceivedUeCapInfo ) runs on EUTRA_PTC
  {
    var UE_CapabilityRAT_ContainerList v_UeCapContainerForOtherRats := p_ReceivedUeCapInfo.criticalExtensions.c1.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList;
    var integer v_SrcLength := lengthof ( v_UeCapContainerForOtherRats );
    var integer i := 0;
    var boolean v_GeranPsFound := false;
    var boolean v_GeranCsFound := false;
    var boolean v_UtraFound := false;
    var boolean v_Cdma2kFound := false;
    var integer v_GeranCsResult := 0;
    
    if ( v_SrcLength == 0 ){
      //* @verdict fail UE does incorrectly not report any other supported Rat
      f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - No other supported Rats reported");
    }
    //* @desc When UE according to PICS supports GERAN_PS check for presence of rat type GERAN_PS
    //* and match capabilities with template based on PICS/PIXITS.
    if ( pc_GERAN and pc_PS ){
      for ( i := 0; i < v_SrcLength; i := i + 1 ) {
        if ( v_UeCapContainerForOtherRats[i].rat_Type == geran_ps ){
          v_GeranPsFound := true;
          if ( not f_DecodeAndCheckMsRadioAccessCap ( v_UeCapContainerForOtherRats[i].ueCapabilityRAT_Container  ) ) {
            //* @verdict fail UE does not report correct GERAN PS capabilities
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - Geran PS capabilities are not correct");
          }
        }
      }
      if ( not v_GeranPsFound ) {
        //* @verdict fail UE does not report GERAN PS capability
        f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - Geran PS support and PICS mismatch");
      }
    }
    
    //* @desc When UE according to PICS supports GERAN_CS check for presence of rat type GERAN_CS
    //* and match capabilities with template based on PICS/PIXITS.
    if ( pc_GERAN and pc_CS ){
      for ( i := 0; i < v_SrcLength; i := i + 1 ) {
        if ( v_UeCapContainerForOtherRats[i].rat_Type == geran_cs ){
          v_GeranCsFound := true;
          v_GeranCsResult := bit2int ( f_DecodeAndCheckMsClassmark ( v_UeCapContainerForOtherRats[i].ueCapabilityRAT_Container ) );
          if ( v_GeranCsResult == 1 ) {
            //* @verdict fail UE does not report correct Classmark 3 Capabilities
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - UE does not report correct Classmark 3 Capabilities");
          }
          else if ( v_GeranCsResult == 2 ) {
            //* @verdict fail UE does not report correct Classmark 2 Capabilities
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - UE does not report correct Classmark 2 Capabilities");
          }
          else if ( v_GeranCsResult == 3 ) {
            //* @verdict fail UE does not report correct Classmark 2 & 3 Capabilities
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - UE does not report correct Classmark 2 & 3 Capabilities");
          }
        }
      }
      if ( not v_GeranCsFound ) {
        //* @verdict fail UE does not report GERAN CS capability
        f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - Geran CS support and PICS mismatch");
      }
    }
    
    //* @desc When UE according to PICS supports UTRAN, check for presence of rat type UTRAN
    //* and match capabilities with template based on PICS/PIXITS
    if ( pc_FDD or pc_TDD_LCR or pc_TDD_HCR or pc_TDD_VHCR ){
      for ( i := 0; i < v_SrcLength; i := i + 1 ) {
        if ( v_UeCapContainerForOtherRats[i].rat_Type == utra ){
          v_UtraFound := true;
          if ( not f_DecodeAndCheckInterRatHandoverInfo ( v_UeCapContainerForOtherRats[i].ueCapabilityRAT_Container ) ) {
            //* @verdict fail UE does not report correct UTRAN capabilities
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - Utran capabilities mismatch");
          }
        }
      }
      if ( not v_UtraFound ) {
        //* @verdict fail UE does not report UTRAN capability
        f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - Utran support and PICS mismatch");
      }
    }
    
    //* @desc When UE according to PICS supports CDMA2K, check for presence of rat typeCDMA2K container
    if ( pc_HRPD or pc_1xRTT ){
      for ( i := 0; i < v_SrcLength; i := i + 1 ) {
        if ( v_UeCapContainerForOtherRats[i].rat_Type == cdma2000_1XRTT ){
          v_Cdma2kFound := true;
          if ( not match ( v_UeCapContainerForOtherRats[i], cr_CDMA2K_UE_CapabilityRAT_Container ) ) {
            //* @verdict fail UE does not include CDMA2K container
            f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - CDMA200-1XRTT capabilities mismatch");
          }
        }
      }
      if ( not v_Cdma2kFound ) {
        //* @verdict fail UE does not report UTRAN capability
        f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19a2 - Utran support and PICS mismatch");
      }
    }
  }

  /*
   * @desc      Send UECapabilityEnquiry to request UE radio access capability information for EUTRA
   *            and check the received UECapabilityInformation.
   * @param     p_CellId
   * @param     p_RRC_TI
   * @status    APPROVED
   */
  function fl_EUTRA_FullCapabilityCheck ( CellId_Type p_CellId,
                                          RRC_TransactionIdentifier p_RRC_TI ) runs on EUTRA_PTC
  {
    var SRB_COMMON_IND v_ReceivedAspForUeCapabilityInfo;

    //* @desc Fill the IE interRAT-Parameters with expected contents based on set PICS parameters
    var template UE_EUTRA_Capability v_EutraCapability := fl_EUTRA_SetExpectedUeEutraCap ( cr_UeCapabilitiesRAT_Container );

    //* @desc Transmit UE Capability Enquiry message to ask for EUTRA Capability information
    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId, cs_TimingInfo_Now, cs_508_UeCapabilityEnquiry ( p_RRC_TI ) ) );

    //* @desc Receive UE Capability Information message for EUTRA capabilities
    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId, cr_508_UeCapabilityInformation ( p_RRC_TI, cr_EutraCapInfo ) ) )
    -> value v_ReceivedAspForUeCapabilityInfo;

    //* @desc Decode and check the ueCapabilitiesRAT-Container
    fl_EUTRA_MatchEutraCapMsgs ( v_ReceivedAspForUeCapabilityInfo.Signalling.Rrc.Dcch.message_.c1.ueCapabilityInformation.criticalExtensions.c1.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList[0].ueCapabilityRAT_Container, v_EutraCapability );
  }

  /*
   * @desc      Send UECapabilityEnquiry to request UE radio access capability information for other supported RATs
   *            and check the received UECapabilityInformation.
   * @param     p_CellId
   * @param     p_RRC_TI
   * @status    APPROVED
   */
  function fl_AllRatCapabilityCheck ( CellId_Type p_CellId,
                                      RRC_TransactionIdentifier p_RRC_TI ) runs on EUTRA_PTC
  {
    var SRB_COMMON_IND v_ReceivedAspForUeCapabilityInfo;

    //* @desc Fill the UECapabilityInformation_r8_IEs with expected RAT-containers based on set PICS parameters
    var template UECapabilityInformation_r8_IEs v_OtherSupRatCapability := fl_SetUeOtherSupRatCap ( );
    
    //* @desc Transmit UE Capability Enquiry message for every other RAT
    SRB.send ( cas_SRB1_RrcPdu_REQ ( p_CellId,
                                     cs_TimingInfo_Now,
                                     cs_508_UeCapabilityEnquiry ( p_RRC_TI,
                                                                  cs_AllOtherRatCapReq ) ) );
    
    //* @desc Receive UE Capability Information message for every other supported RAT
    SRB.receive ( car_SRB1_RrcPdu_IND ( p_CellId, cr_508_UeCapabilityInformation ( p_RRC_TI, v_OtherSupRatCapability ) ) )
      -> value v_ReceivedAspForUeCapabilityInfo;
    
    //* @desc Decode and check the ueCapabilitiesRAT-Containers for every other supported RAT
    fl_EUTRA_CheckOtherRatCap ( v_ReceivedAspForUeCapabilityInfo.Signalling.Rrc.Dcch.message_.c1.ueCapabilityInformation );
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.5.2.1
   * @status
   */
  function f_TC_8_5_2_1_EUTRA ( ) runs on EUTRA_PTC
  {

    var boolean v_TriggerRxd := false;

    f_EUTRA_Init ( c1 );

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Send Cell information to UTRA PTC
    f_IRAT_Send_EUTRA_SysInfoToOtherPort(UTRAN, f_EUTRA_CellInfo_GetEARFCN_DL(eutra_Cell1));

    //Receive new athentication parameters and set inital cell power level according to T0
    f_EUTRA_InterRAT_InitialiseAuthParams(UTRAN);
    f_EUTRA_SetCellPower ( eutra_Cell1, tsc_SuitableCellRS_EPRE, cs_TimingInfo_Now );

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 4" siclog@
    //Does the test result of generic test procedure in TS 36.508 subclause 6.4.2.7A indicate that the UE is camped on E-UTRAN Cell 1?
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger ( UTRAN );
    f_EUTRA_508CheckCampOnNewEutraCellUponMobilityFromAnotherRAT ( eutra_Cell1 );

    f_EUTRA_TestBody_Set ( false );

    //Tell the UTRAN PTC that test case is over
    f_SendIRATCoOrd (UTRAN, cms_IRAT_Trigger);

    //Switch/power off UE
    f_EUTRA_Postamble ( eutra_Cell1, E1_IDLE );
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.5.4.1
   * @status    APPROVED
   */
  function f_TC_8_5_4_1_EUTRA ( ) runs on EUTRA_PTC
  {
    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 NAS_AttDetValue_Type v_AttachType;
    var B3_Type v_PDNType;
    var NAS_MSG_Indication_Type v_NasInd;
    var NAS_UL_Message_Type v_PdnConnectivityRequest;
    var template (present) SecurityHeaderType v_AttReqSecurityHeader := tsc_SHT_IntegrityProtected;  //Integrity protected since UE has gone through preamble
    var GutiParameters_Type v_GutiParams;
    var NAS_Lac v_LAC;
    var NAS_PlmnId v_PLMN;
    var template (omit) LocAreaId v_LAI;
    var template (omit) MobileIdentity v_MSId;
    var NasCount_Type v_NasCountUL;
    var EUTRA_SecurityParams_Type v_AuthParams;
    var template (value) MobileIdentity v_Guti;
    var boolean v_IPallocationViaNasFlag;
    var template (value) PDN_Address v_PDN_Address;
    var TrackingAreaCode v_TAC;
    var template AdditionalUpdateType v_AdditionalUpdateType := f_GetAdditionalUpdateType ();  //used to follow TTCN3 v411 standard
    var template (omit) AccessPointName v_APN := omit;
    var template (omit) ProtocolConfigOptions v_Pco := omit;
    var template (omit) ESM_Cause v_ESM_Cause := omit; // @sic R5-104703 sic@

    f_EUTRA_Init ( c1 );

    //Get cell dependant parameters
    v_GutiParams := f_EUTRA_CellInfo_GetGuti ( eutra_Cell1 );
    v_PLMN := f_Asn2Nas_PlmnId (v_GutiParams.PLMN_Identity);
    v_LAC := f_EUTRA_CellInfo_GetLocationAreaCode ( eutra_Cell1 );
    v_TAC := f_EUTRA_CellInfo_GetTAC ( eutra_Cell1 );

    //Set Attachtype, PDN Type, LAI, MsId & EMM cause
    v_AttachType := f_GetEAttachType ();//@sic R5-103673 : Parameter removed sic@
    v_PDNType := f_GetPdnType ( );
    
    // @sic R5-103673 : Implemented in functions f_GetLAI, f_GetMSId & f_GetEMMCause sic@
    v_LAI := f_GetLAI ( v_PLMN, v_LAC );
    v_MSId := f_GetMSId ( );

    //Create and configure all cells
    f_EUTRA_CellConfig_Def ( eutra_Cell1 );

    //Preamble to ensure USIM is updated
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );
    f_EUTRA_UE_Detach_SwitchOffUe ( eutra_Cell1, RRC_IDLE );

    //Get security parameters
    v_AuthParams := f_EUTRA_Security_Get ( );

    f_EUTRA_TestBody_Set ( true );

    //@siclog "Step 1" siclog@
    //Switch on UE
    f_UT_SwitchOnUE ( UT, false );

    //@siclog "Step 2 - 4" siclog@
    //RRC connection establishment
    v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas ( eutra_Cell1,
                                                  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 )));

    //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(v_NasInd.Pdu.Msg.aTTACH_REQUEST);  // @sic R5s100135 sic@

    f_EUTRA_Security_Set(v_AuthParams);
    v_PdnConnectivityRequest := v_NasInd.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;
    }

    //@siclog "Step 5 - 6" siclog@
    //NAS Authentication procedure
    v_AuthParams := f_EUTRA_NAS_Authentication ( eutra_Cell1,
                                                 tsc_SRB1,
                                                 v_AuthParams,
                                                 tsc_SHT_IntegrityProtected_Ciphered,//Security Header to use in Authentication Request   // @sic R5s100049 sic@
                                                 tsc_SHT_IntegrityProtected_Ciphered,//Security Header to use in Authentication Response  // @sic R5s100049 sic@
                                                 v_PLMN );

    //@siclog "Step 7 - 8" siclog@
    //NAS Security mode control procedure
    v_NasCountUL := f_EUTRA_NAS_ActivateSecurity ( eutra_Cell1, v_AuthParams );
    f_EUTRA_Security_Set ( v_AuthParams );

    //@siclog "Step 9 - 10" siclog@
    //UE Capability check for EUTRA capabilities only
    fl_EUTRA_FullCapabilityCheck ( eutra_Cell1, v_RRC_TI );
    //* @verdict pass EUTRA capability check passed
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 10");

    //@siclog "Step 11 - 12" siclog@
    //AS security activation
    v_AuthParams := f_EUTRA_Authentication_InitAS ( v_AuthParams, v_NasCountUL );
    v_AuthParams := f_EUTRA_RRC_ActivateSecurity ( eutra_Cell1, v_AuthParams, v_NasCountUL );
    f_EUTRA_Security_Set ( v_AuthParams );

    //@siclog "Step 13" siclog@
    //Optional ESM Information transfer
    if (f_CheckEsmInfoTransferFlag(v_PdnConnectivityRequest.pDN_CONNECTIVITY_REQUEST)) {
      v_APN := f_ESM_InformationTransfer_GetAPN ( eutra_Cell1, v_EPS_TI);
    }

    v_Guti := f_GutiParameters2MobileIdentity ( tsc_IEI_Guti,
                                                f_EUTRA_CellInfo_GetGuti ( eutra_Cell1 ) );
    v_IPallocationViaNasFlag :=  f_CheckPCOforIPallocationViaNas ( v_Pco ); // See TS 36.508 v.8.2.2 Table 4.7.3-6
    v_PDN_Address := f_GetPDNAddress (v_IPallocationViaNasFlag);

    //@siclog "Step 14 - 15" siclog@
    //RRC connection reconfiguration
    f_EUTRA_RRCConnectionReconfigurationWithAttachAcceptAllParams( eutra_Cell1,
                                                                   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,
                                                                   v_APN,
                                                                   f_GetDefPCOforPCSCFAddress (v_Pco), // @sic R5-104703 sic@
                                                                   v_NasInd.Pdu.Msg.aTTACH_REQUEST.addUpdateType);  // @sic R5s100084, R5-103681 sic@);

    //@siclog "Step 16" siclog@
    //Receive Attach complete
    f_EUTRA_NAS_AttachComplete ( eutra_Cell1, v_EpsBearerId );

    //@siclog "Step 17 - 18" siclog@
    //UE Capability check for EUTRA capabilities only
    fl_EUTRA_FullCapabilityCheck ( eutra_Cell1, v_RRC_TI );
    //* @verdict pass EUTRA capability check passed
    f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 18");

    //EU Capability check for every other supported RAT
    //Only perform if UE supports any other RAT
    if ( pc_FDD or pc_TDD_LCR or pc_TDD_HCR or pc_TDD_VHCR or pc_GERAN or pc_HRPD or pc_1xRTT )
    {
      //@siclog "Step 19a1 - 19a2" siclog@
      //UE Capability check for every other supported RAT
      fl_AllRatCapabilityCheck ( eutra_Cell1, v_RRC_TI );
      //* @verdict pass Capability check for other supported RATs passed
      f_EUTRA_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.5.4.1 Step 19");
    }

    f_EUTRA_TestBody_Set ( false );

    f_EUTRA_Postamble ( eutra_Cell1, E2_CONNECTED ); // @sic R5-103847 sic@
  }
}
