/******************************************************************************/
// @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-08 10:25:40 +0200 (Wed, 08 Sep 2010) $
// $Rev: 3833 $
/******************************************************************************/
module RRC_ConnRel_UTRAN
{
//=========================================================================
// Import Part
//=========================================================================

  import from UTRAN_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from UTRAN_ASP_Definitions language "ASN.1:1997" all;
  import from UTRAN_Component all;
  import from UTRAN_CommonFunctions all;
  import from UTRAN_CellInfo all;
  import from UTRAN_TypeDefs all;
  import from UTRAN_ASN1_Templates all;
  import from UTRAN_Templates all;
  import from UTRANGERAN_CommonFunctions all;
  import from UTRAN_CapabilityFunctions all;
  import from UTRAN_RAB_Functions all;
  import from UTRAN_ConfigurationSteps all;
  
  import from NAS_CommonTypeDefs all;
  import from NAS_CommonTemplates all;
  import from NAS_24008TypeDefs all;
  import from NAS_24008Templates all;
  
  import from UpperTesterFunctions all;
  import from CommonIratDefs all;
  import from Parameters all;
  
  import from EUTRA_CapabilityFunctions all;
  
  import from InterRat_Handover_to_EUTRA_UTRAN all; //FFS - for cr_MeasurementReport
  import from CSG_Testcases_UTRAN all; //FFS - for function f_UTRAN_SendAuthParameters

//==============================================================================
// altsteps
//==============================================================================

  /*
   * @desc      additional default behaviour to be activated on top of the standard default behaviour
   *            when periodical MeasurmentReports shall be ignored
   * @param     p_U_RLC_AM_IND
   * @param     p_U_RLC_AM_IND .. shall be a periodical MeasurmentReport
   * @status
   */
  altstep a_UTRA_AddDefault_PeriodicalMeasurmentReport( UTRAN_CellId_Type                  p_UTRAN_CellId,
                                                        template (present) UL_DCCH_Message p_UL_DCCH_Message ) runs on UTRAN_PTC
  {
    [] U_AM.receive ( car_RLC_Data_Ind ( p_UTRAN_CellId,
                                         tsc_RB2,
                                         p_UL_DCCH_Message ) )
      {
        repeat;
      };
  };

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

  //Default values according to 36.508 Table 4.7B.1-3: MEASUREMENT CONTROL for Inter-RAT
  template (value) DL_DCCH_Message cs_MeasurementControl_InterRATMeasurement (IntegrityCheckInfo         p_Integrityinfo,
                                                                              RRC_TransactionIdentifier  p_RRC_TI,
                                                                              integer                    p_EutraCell_ARFCN,
                                                                              EUTRA_MeasurementBandwidth p_EutraCell_MeasBandwidth,
                                                                              TGPS_Reconfiguration_CFN   p_Tgps_Reconfiguration_CFN,
                                                                              TGCFN                      p_TGCFN) :=
    cs_MeasurementControl_InterRATMeasurement_InterRATEventList ( p_Integrityinfo,
                                                                  p_EutraCell_ARFCN,
                                                                  p_EutraCell_MeasBandwidth,
                                                                  {cs_InterRATEvent3a_Def},
                                                                  p_Tgps_Reconfiguration_CFN,
                                                                  p_TGCFN,
                                                                  p_RRC_TI );

  //Default values according to 36.508 Table 4.7B.1-3: MEASUREMENT CONTROL for Inter-RAT
  template (value) InterRATEvent cs_InterRATEvent3a_Def := {
    event3a := {
      thresholdOwnSystem    :=  -66,
      w                     :=    0,
      thresholdOtherSystem  :=  -80,
      hysteresis            :=    0,
      timeToTrigger         :=   ttt10,
      reportingCellStatus   := {
        withinActSetOrVirtualActSet_InterRATcells := e2
      }
    }
  }
  
  template (value) InterRATEvent cs_InterRATEvent3c := {
    event3c := {
      thresholdOtherSystem  :=  -80,
      hysteresis            :=    0,
      timeToTrigger         :=   ttt10,
      reportingCellStatus   := {
        withinActSetOrVirtualActSet_InterRATcells := e2
      }
    }
  }
  
  template (value) DL_DCCH_Message cs_MeasurementControl_InterRATMeasurement_InterRATEventList ( IntegrityCheckInfo                 p_Integrityinfo,
                                                                                                 integer                            p_EutraCell_ARFCN,
                                                                                                 EUTRA_MeasurementBandwidth         p_EutraCell_MeasBandwidth,
                                                                                                 template (value) InterRATEventList p_InterRATEventList,
                                                                                                 TGPS_Reconfiguration_CFN           p_Tgps_Reconfiguration_CFN,
                                                                                                 TGCFN                              p_TGCFN,
                                                                                                 RRC_TransactionIdentifier          p_RRC_TI := tsc_RRC_TI) :=
  {
    integrityCheckInfo := p_Integrityinfo,
    message_ := {
      measurementControl := {
        later_than_r3 := {
          rrc_TransactionIdentifier := p_RRC_TI,
          criticalExtensions := {
            later_than_r4 := {
              rrc_TransactionIdentifier_MSP := p_RRC_TI,
              criticalExtensions := {
                criticalExtensions := {
                  criticalExtensions := {
                    r8  := {
                      measurementControl_r8  := {
                        measurementIdentity  := 3,
                        measurementCommand := {
                          setup := {
                            interRATMeasurement   := {
                              interRATMeasurementObjects := {
                                eutra_FrequencyList := {
                                  eutraFrequencyRemoval  := {
                                    removeNoFrequencies  := NULL
                                  },
                                  eutraNewFrequencies := {
                                    {
                                      earfcn  := p_EutraCell_ARFCN,
                                      measurementBandwidth := p_EutraCell_MeasBandwidth,
                                      eutra_blackListedCellList   := omit
                                    }
                                  }
                                }
                              },
                              interRATMeasQuantity :=  {
                                measQuantityUTRAN_QualityEstimate := {
                                  filterCoefficient := fc0,
                                  modeSpecificInfo  := {
                                    fdd   := {
                                      intraFreqMeasQuantity_FDD  := cpich_Ec_N0
                                    }
                                  }
                                },
                                ratSpecificInfo  := {
                                  e_UTRA  := {
                                    measurementQuantity := rsrq,
                                    filterCoefficient   := fc0
                                  }
                                }
                              },
                              interRATReportingQuantity := omit,
                              reportCriteria  :={
                                interRATReportingCriteria := {
                                  interRATEventList  := p_InterRATEventList
                                }
                              },
                              idleIntervalInfo := omit
                            }
                          }
                        },
                        measurementReportingMode  := {
                          measurementReportTransferMode := acknowledgedModeRLC,
                          periodicalOrEventTrigger      := eventTrigger
                        },
                        additionalMeasurementList  := omit,
                        dpch_CompressedModeStatusInfo := {
                          tgps_Reconfiguration_CFN  := p_Tgps_Reconfiguration_CFN,
                          tgp_SequenceShortList     := {
                            {
                              tgpsi := 1,
                              tgps_Status := {
                                activate_ := {
                                  tgcfn := p_TGCFN
                                }
                              }
                            }
                          }
                        }
                      },
                      measurementControl_r8_add_ext   := omit,
                      nonCriticalExtensions           := omit
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  };
  
  template (value) EUTRA_TargetFreqInfo cs_EUTRA_TargetFreqInfo ( EARFCN p_DlEUTRACarrierFreq ) := {
    dlEUTRACarrierFreq := p_DlEUTRACarrierFreq,
    eutraBlacklistedCellPerFreqList := omit
  };
  
  template (value) DL_DCCH_Message cs_108_RRC_ConnRelDCCH_WithEutraReDirection ( IntegrityCheckInfo                        p_IntegrityCheckInfo,
                                                                                 RRC_TransactionIdentifier                 p_RRC_Ti,
                                                                                 template (omit) integer                   p_N308,
                                                                                 template (value) EUTRA_TargetFreqInfoList p_EUTRA_TargetFreqInfoList) :=
  {
    integrityCheckInfo := p_IntegrityCheckInfo,
    message_ := {
      rrcConnectionRelease := {
        later_than_r3 := {
          rrc_TransactionIdentifier := p_RRC_Ti,
          criticalExtensions := {
            r4 := {
              rrcConnectionRelease_r4 := {
                n_308 := p_N308,
                releaseCause := normalEvent,
                rplmn_information := omit
              },
              v4d0NonCriticalExtensions := {
                rrcConnectionRelease_r4_add_ext := omit,
                v690NonCriticalExtensions := {
                  rrcConnectionRelease_v690ext := {
                    redirectionInfo_v690ext := omit
                  },
                  v770NonCriticalExtensions := {
                    rrcConnectionRelease_v770ext := {
                      ueMobilityStateIndicator := omit
                    },
                    v860NonCriticalExtensions := {
                      rrcConnectionRelease_v860ext := {
                        redirectionInfo := {
                          interRATInfo := {
                            eutra := {
                              eutra_TargetFreqInfoList := p_EUTRA_TargetFreqInfoList
                            }
                          }
                        }
                      },
                      nonCriticalExtensions := omit
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  };

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

  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.1.3.6
   * @status
   */
  function f_TC_8_1_3_6_UTRAN ( ) runs on UTRAN_PTC {

    var boolean v_TriggerRxd := false;
    var integer v_T0_CPICH_Cell5 := -70;
    var integer v_T0_PCCPCH_Cell5 := -72;

    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);

    //Create and configure Cell 5.
    //Cell started switched on with default cell power level and max attenuation
    f_SS_CreateCellFACH ( utran_Cell5 );

    //Start sending System Information
    f_UTRAN_SendDefSysInfo ( utran_Cell5 );

    //Send System Information of UTRAN cell to EUTRA component
    f_UTRAN_Send_IR_SysInfoToEUTRA ( utran_Cell5 );
    
    //Since UE starts in EUTRA wait for EUTRA PTC to send the Auth Params
    f_UTRAN_InterRAT_InitialiseAuthParams();

    //Set cell power level according to T0
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);
    f_UTRAN_SetCellPower(utran_Cell5, v_T0_CPICH_Cell5, v_T0_PCCPCH_Cell5);

    f_UTRAN_TestBody_Set ( true );

    //@siclog "Step 2" siclog@
    //Step 2: Check if UE camps on Cell 5
    f_UTRAN_508CheckCampOnNewUtranCell ( utran_Cell5 );

    //Tell the EUTRA component that test case is over
    f_SendIRATCoOrd ( EUTRA, cms_IRAT_Trigger );

    f_UTRAN_TestBody_Set ( false );

    //Switch/Power off UE
    f_UTRA_Postamble ( utran_Cell5, U1_IDLE );
  }
  
  /*
   * @desc      REFERENCE TS 36.523-1 clause 8.1.3.7
   * @status
   */
  function f_TC_8_1_3_7_UTRAN ( ) runs on UTRAN_PTC {
  
    var UTRAN_FDD_TDD v_FDD_TDD_Mode;
    var TGCFN v_TGCFN;
    var integer v_FrameNumber;
    var RLC_TR_DATA_IND v_RLC_TR_DATA_IND;
    var template (value) QualityOfService v_QualityOfService;
    var InitialUE_Identity v_InitialUE_Id;
    var IRAT_CoOrd_SysInfo_Type v_EUTRASysInfo;
    var UTRAN_SecurityInfo_Type v_SecurityInfo;
    var KeySeq v_KeySeqPs;
    var RRC_DATA_IND v_RRC_DataInd;
    var default v_DefaultRef;
    var TGPS_Reconfiguration_CFN v_Tgps_Reconfiguration_CFN;
    var integer v_TtiValue := 4; //cell_DCH_64kPS_RAB_SRB is configured
    var boolean v_TriggerRxd := false;
    var template (present) EstablishmentCause v_EstCause := ?;
    var START_Value v_START_Value := '00000000000000000000'B;
    var integer v_DedicatedId := 1;
    var template (value) PktDataProtoAddr v_ExpectedPDN_Address := f_UG_GetPktDataProtoAddr (true);
    var EUTRA_MeasurementBandwidth v_EUTRA_MeasurementBandwidth :=
      f_UTRAN_ConvertIntegerToEUTRA_MeasurementBandwidth(f_ConvertDL_BandwidthPIXITToInteger());

    //Initialise all cells
    f_UTRAN_Init (EUTRA_UTRAN);

    v_FDD_TDD_Mode := f_UTRAN_CellInfo_GetFDD_TDD (utran_Cell5);
    
    //Get Cell Parameters for EUTRA cell from EUTRA PTC
    v_EUTRASysInfo := f_WaitForIRATCoOrd_SysInfo (EUTRA);
    
    //Derive Quality of service for AT command
    v_QualityOfService := f_UTRAN_InitialiseQoS (utran_Cell5);
    
    //Update SIB19 of cell 5 according to Table 8.1.3.7.3.3-1
    f_UTRAN_SysInfo_SetSIB19 (utran_Cell5, cs_SIB19_UtraAndOneEutraEntry( cs_Utra_PriorityInfoList_Common(4,0,0),
                                                                          cs_Eutra_FreqAndPriList_OneEntry_Def(v_EUTRASysInfo.Eutra[0].Arfcn,
                                                                                                               v_EUTRA_MeasurementBandwidth,
                                                                                                               3)) );

    //Create and configure Cell 5.
    //Cell started switched on with default cell power level and max attenuation
    f_SS_CreateCellDCH (utran_Cell5);

    //Start sending System Information
    f_UTRAN_SendDefSysInfo (utran_Cell5);
    
   //Bring UE to initial state
    f_UTRAN_Preamble (utran_Cell5);

    //Set cell power level according to T0
    f_SendIRATCoOrd (EUTRA, cms_IRAT_Trigger);
    f_UTRAN_SetCellPower (utran_Cell5, tsc_AttenuationServingCell);

    f_UTRAN_TestBody_Set (true);

    //@siclog "Step 1" siclog@
    //Step 1: Originate PS Call
    f_UT_ActivatePDPContextUTRAN (UT,
                                  v_DedicatedId,
                                  v_ExpectedPDN_Address.addrInfo,
                                  v_QualityOfService,
                                  "" );

    //@siclog "Step 2" siclog@
    //Step 2: Receive RRC Connection Request
    if ( v_FDD_TDD_Mode == UTRAN_FDD ) {
      U_TM.receive ( car_RRC_ConnReq (utran_Cell5,
                                      tsc_RB0,
                                      cdr_RRC_ConnReq_r8OrLater ( v_EstCause,
                                                                  cr_RRCConnReq_v860ext(true, -) ) ) ) -> value v_RLC_TR_DATA_IND;
    }
    else {
      U_TM.receive ( car_RRC_ConnReq (utran_Cell5,
                                      tsc_RB0,
                                      cdr_RRC_ConnReq_r8OrLater ( v_EstCause,
                                                                  cr_RRCConnReq_v860ext(-, true) ) ) ) -> value v_RLC_TR_DATA_IND;
    }
    v_InitialUE_Id := v_RLC_TR_DATA_IND.tM_message.uL_CCCH_Message.message_.rrcConnectionRequest.initialUE_Identity;
    //* @verdict pass UE sends the RRC CONNECTION REQUEST including the IE
    //* Pre-redirection info with Support of E-UTRA set to TRUE?
    f_UTRAN_PreliminaryPass (__FILE__, __LINE__, "Test Case 8.1.3.7 Step 2");

    //@siclog "Step 3-4" siclog@
    //Step 3-4: Send RRC Connection Setup and Receive RRC Connection Setup Complete
    f_UTRAN_RRC_ConnEst_WithoutRRCConnectionReq (utran_Cell5, v_InitialUE_Id);
    
    v_SecurityInfo := f_UTRAN_Security_Get( );
    v_KeySeqPs := v_SecurityInfo.authKeys.AuthPS.KeySeq;

    //@siclog "Step 5" siclog@
    //Step 5: Receive Service Request
    U_Dc.receive ( car_PS_InitDirectTransfer (utran_Cell5,
                                              tsc_RB3,
                                              cr_U_ServiceRequest ( cr_ServiceType ( ? ),
                                                                    cs_MobileIdTMSIorPTMSI ('18'O, px_PTMSI_Def),
                                                                    v_KeySeqPs ) ) )-> value v_RRC_DataInd;

    v_START_Value := v_RRC_DataInd.ttcn_start;
    f_SS_SecurityDownloadStart (ps_domain, v_START_Value);

    //@siclog "Step 6-7" siclog@
    //Step 6-7: Transmit Physical Channel Reconfiguration and receive Complete message
    f_UTRAN_PhyChReconf_InterRatCompresseModeActivate (utran_Cell5);

    //@siclog "Step 8" siclog@
    //Step 8: Configure InterRat periodic measurements
    v_FrameNumber := f_CPHY_GetFrameNumber (utran_Cell5, tsc_DL_DPCH1 );
    //Physical Channel identity is hardcoded to the value set in cell_DCH_64kPS_RAB_SRB is configured, tsc_DL_DPCH1
    v_Tgps_Reconfiguration_CFN := (v_FrameNumber+(250-v_TtiValue )) mod 256;
    v_TGCFN := (v_FrameNumber+(252-v_TtiValue )) mod 256;
    U_AM.send ( cas_RLC_Data_Req_NoCnf (utran_Cell5,
                                        tsc_RB2,
                                        cs_MeasurementControl_InterRATMeasurement_InterRATEventList ( v_SecurityInfo.dl_IntegrityCheckInfo,
                                                                                                      v_EUTRASysInfo.Eutra[0].Arfcn,
                                                                                                      v_EUTRA_MeasurementBandwidth,
                                                                                                      {cs_InterRATEvent3c},
                                                                                                      v_Tgps_Reconfiguration_CFN,
                                                                                                      v_TGCFN ) ) );

    //@siclog "Step 9" siclog@
    //Step 9: Start receiving Measurement reports
    //Activate the default to receive measurement reports
    v_DefaultRef := activate (a_UTRA_AddDefault_PeriodicalMeasurmentReport (utran_Cell5, cr_MeasurementReport ));

    //@siclog "Step 10-11" siclog@
    //Step 10-11: Send Authentication Request and receive response
    f_GMM_Authentication ();

    //@siclog "Step 12-13" siclog@
    //Step 12-13: Transmit Security Mode Command and receive complete
    f_RRC_Security (utran_Cell5, true, ps_domain);

    //@siclog "Step 14" siclog@
    //Step 14: Wait for Measurement report if not yet received
    //FFS - Is there any way the default can indicate that a Measuremet report has been received?
    
    v_SecurityInfo := f_UTRAN_Security_Get( );

    //@siclog "Step 15" siclog@
    //Step 15: Transmit RRC Connection Release
    U_UM.send (cas_RRC_ConnRelDCCH ( utran_Cell5,
                                     tsc_RB1,
                                     cs_108_RRC_ConnRelDCCH_WithEutraReDirection ( v_SecurityInfo.dl_IntegrityCheckInfo,
                                                                                   tsc_RRC_TI,
                                                                                   1,
                                                                                   {cs_EUTRA_TargetFreqInfo(v_EUTRASysInfo.Eutra[0].Arfcn)})));

    //@siclog "Step 16" siclog@
    //Step 16: Receive RRC Connection Release Complete
    U_UM.receive(car_RRC_ConnRelCmplUM(utran_Cell5,
                                       tsc_RB1,
                                       cr_108_RRC_ConnRelCmpl(tsc_RRC_TI)));

    //@siclog "Step 17" siclog@
    //Step 17: Receive RRC Connection Release Complete
    U_UM.receive(car_RRC_ConnRelCmplUM(utran_Cell5,
                                       tsc_RB1,
                                       cr_108_RRC_ConnRelCmpl(tsc_RRC_TI)));

    //Might not be needed since no more connections will happen on UTRAN cell
    f_RestartCRLC_ForNextConnection (utran_Cell5);
    f_SS_ResetSecurityKey();
    
    //Tell EUTRA PTC to take over from here by sending the updated authentication parameters
    f_UTRAN_SendAuthParameters();

    f_UTRAN_TestBody_Set (false);
    
    //Wait till end of test case
    v_TriggerRxd := f_WaitForIRATCoOrd_Trigger (EUTRA);

    //Release UTRA cell
    f_UTRAN_ReleaseCell (utran_Cell5);
  }
}
