/******************************************************************************/
// @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 23:11:35 +0200 (Thu, 16 Sep 2010) $
// $Rev: 3943 $
/******************************************************************************/

module EUTRA_AspCommon_Templates {
  import from EUTRA_CommonDefs all;
  import from EUTRA_ASP_TypeDefs all;
  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;

  //****************************************************************************
  // timing info
  //----------------------------------------------------------------------------

  template (value) SubFrameTiming_Type cs_SubFrameTiming(SubFrameNumber_Type p_SubFrameNumber,
                                                         SystemFrameNumber_Type p_SystemFrameNumber) :=
  { /* @status    APPROVED */
    SFN := { Number := p_SubFrameNumber },
    Subframe := { Number := p_SystemFrameNumber }
  };

  template (value) TimingInfo_Type cs_TimingInfo_Now := {Now := true};  /* @status    APPROVED */

  template (value) TimingInfo_Type cs_TimingInfo_SFN(SystemFrameNumber_Type p_SFN) :=
  { /* @status    APPROVED */
    SubFrame := {
      SFN := { Number := p_SFN },
      Subframe := { Any := true }
    }
  };

  template (value) TimingInfo_Type cs_TimingInfo_SubFrame(SubFrameNumber_Type p_Subframe) :=
  { /* @status    APPROVED */
    SubFrame := {
      SFN := { Any := true },
      Subframe := { Number := p_Subframe }
    }
  };

  template (value) TimingInfo_Type cs_TimingInfo(SystemFrameNumber_Type p_SFN,
                                                 integer p_SubframeOffset) :=
  { /* R5s100172 */
    /* @status    APPROVED */
    SubFrame := {
      SFN := { Number := (p_SFN + (p_SubframeOffset / 10)) mod 1024},
      Subframe := { Number := p_SubframeOffset mod 10 }
    }
  };

  template TimingInfo_Type cr_TimingInfo_Any :=
  { /* @status    APPROVED */
    SubFrame := {
      SFN := ? ,
      Subframe := ?
    }
  };

  template TimingInfo_Type cr_TimingInfo(template (present) SystemFrameNumber_Type p_SFN,
                                         template (present) SubFrameNumber_Type p_Subframe) :=
  { /* @status    APPROVED */
    SubFrame := {
      SFN := { Number := p_SFN },
      Subframe := { Number := p_Subframe }
    }
  };

  /*
   * @desc      auxiliary function to check whether timing info is "now"
   * @param     p_TimingInfo
   * @return    boolean
   * @status    APPROVED
   */
  function f_TimingInfo_IsNow(template (value) TimingInfo_Type p_TimingInfo) return boolean
  {
    return match(valueof(p_TimingInfo), cs_TimingInfo_Now);
  }

  //****************************************************************************
  // common part of ASPs
  //----------------------------------------------------------------------------

  template CnfAspCommonPart_Type cr_CnfAspCommonPart_CellCfg(CellId_Type p_CellId) :=
  { /* @status    APPROVED */
    CellId := p_CellId,
    RoutingInfo := { None := true },
    TimingInfo := ?,
    Result := { Success := true }
  };

  template (value) ReqAspCommonPart_Type cs_ReqAspCommonPart_CellCfg(CellId_Type p_CellId,
                                                                     template (value) TimingInfo_Type p_TimingInfo := cs_TimingInfo_Now,
                                                                     boolean p_CnfFlag := tsc_CnfReq) :=
  { /* @status    APPROVED */
    CellId := p_CellId,
    RoutingInfo := { None := true },
    TimingInfo := p_TimingInfo,
    ControlInfo := {
      CnfFlag      := p_CnfFlag,
      FollowOnFlag := false
    }
  };

  template (value) ReqAspCommonPart_Type cs_ReqAspCommonPart_RB (CellId_Type p_CellId,
                                                                 RadioBearerId_Type p_RB_Id,
                                                                 template (value) TimingInfo_Type p_TimingInfo := cs_TimingInfo_Now,
                                                                 boolean p_CnfFlag := tsc_NoCnfReq) :=
  { /* @status    APPROVED */
    CellId := p_CellId,
    RoutingInfo := {
      RadioBearerId := p_RB_Id
    },
    TimingInfo := p_TimingInfo,
    ControlInfo := {
      CnfFlag := p_CnfFlag,
      FollowOnFlag := false
    }
  };


  template RoutingInfo_Type cr_RoutingInfo_None :=
  { /* @status    APPROVED */
    None := true
  };

  template IndAspCommonPart_Type cr_IndAspCommonPart_Common (template (present) CellId_Type p_CellId,
                                                             template (present) RoutingInfo_Type p_RoutingInfo,
                                                             template (present) TimingInfo_Type p_TimingInfo := cr_TimingInfo_Any) :=
  { /* @status    APPROVED */
    CellId                 :=  p_CellId,
    RoutingInfo            := p_RoutingInfo,
    TimingInfo             := p_TimingInfo,
    Status                 := {
      Ok := true
    }
  };

  template IndAspCommonPart_Type cr_IndAspCommonPart_SysInd (template (present) CellId_Type p_CellId,
                                                             template (present) TimingInfo_Type p_TimingInfo := cr_TimingInfo_Any) :=
    /* @status    APPROVED */
    cr_IndAspCommonPart_Common (p_CellId, cr_RoutingInfo_None, p_TimingInfo);

  template IndAspCommonPart_Type cr_IndAspCommonPart_RB(template (present) CellId_Type p_CellId,
                                                        template (present) RadioBearerId_Type p_RbId,
                                                        template (present) TimingInfo_Type p_TimingInfo := cr_TimingInfo_Any) :=
    /* @status    APPROVED */
    cr_IndAspCommonPart_Common (p_CellId, {RadioBearerId := p_RbId}, p_TimingInfo);

  //****************************************************************************
  // common CNF ASPs
  //----------------------------------------------------------------------------

  template SYSTEM_CTRL_CNF car_CellConfig_Power_CNF :=
  { /* @status    APPROVED */
    Common := cr_CnfAspCommonPart_CellCfg(eutra_Cell_NonSpecific),
    Confirm := {
      CellAttenuationList := true
    }
  };

  template SYSTEM_CTRL_CNF car_CellConfig_CNF(CellId_Type p_CellId) :=
  { /* @status    APPROVED */
    Common := cr_CnfAspCommonPart_CellCfg(p_CellId),
    Confirm := {
      Cell := true
    }
  };

  template SYSTEM_CTRL_CNF car_EnquireTiming ( CellId_Type p_CellId ) :=
  { /* @status    APPROVED */
    Common := cr_CnfAspCommonPart_CellCfg( p_CellId ),
    Confirm := {
      EnquireTiming := true
    }
  };

  template SYSTEM_CTRL_CNF car_SYSTEM_CTRL_L1MACInd_CNF(CellId_Type p_CellId) :=
  { /* @status    APPROVED */
    Common := cr_CnfAspCommonPart_CellCfg(p_CellId),
    Confirm := {
      L1MacIndCtrl := true
    }
  };

  //****************************************************************************
  // common REQ ASPs
  //----------------------------------------------------------------------------

  template (value) SYSTEM_CTRL_REQ cas_EnquireTiming ( CellId_Type p_CellId ) :=
  { /* @status    APPROVED */
    Common := cs_ReqAspCommonPart_CellCfg ( p_CellId ),
    Request := {
      EnquireTiming := true
    }
  };

  template (value) SYSTEM_CTRL_REQ cas_ReleaseCell_REQ ( CellId_Type p_CellId,
                                                         template (value) TimingInfo_Type p_TimingInfo ) :=
  { /* @status    APPROVED */
    Common := cs_ReqAspCommonPart_CellCfg( p_CellId, p_TimingInfo, tsc_NoCnfReq), // Don't request a confirmation
    Request := {
      Cell := {
        Release := true
      }
    }
  };

  template (value) SYSTEM_CTRL_REQ cas_RachPreambleIndConfig_REQ(CellId_Type p_CellId,
                                                                 L1Mac_IndicationMode_Type p_RachPreambleMode) :=
  { /* @status    APPROVED */
    Common := cs_ReqAspCommonPart_CellCfg(p_CellId, cs_TimingInfo_Now, tsc_NoCnfReq),
    Request := {
      L1MacIndCtrl := cs_EnableRachPreamble_Req ( p_RachPreambleMode)
    }
  };

  template (value) SYSTEM_CTRL_REQ cas_HarqErrorIndication_REQ(CellId_Type p_CellId,
                                                               L1Mac_IndicationMode_Type p_Mode := enable) :=
  { /* @status    APPROVED */
    Common := cs_ReqAspCommonPart_CellCfg(p_CellId, cs_TimingInfo_Now),
    Request := {
      L1MacIndCtrl := cs_HarqErrorInd_Req(p_Mode)
    }
  };

  template (value) SYSTEM_CTRL_REQ cas_PdcpHandoverInit_REQ(CellId_Type p_SourceCellId,
                                                            CellId_Type p_TargetCellId) :=
  { /* @status    APPROVED */
    Common := cs_ReqAspCommonPart_CellCfg(p_TargetCellId, cs_TimingInfo_Now),
    Request := {
      PdcpHandoverControl := {
        HandoverInit := {
          SourceCellId := p_SourceCellId
        }
      }
    }
  };

  template (value) SYSTEM_CTRL_REQ cas_PdcpHandoverComplete_REQ(CellId_Type p_CellId) :=
  { /* @status    APPROVED */
    Common := cs_ReqAspCommonPart_CellCfg(p_CellId, cs_TimingInfo_Now, tsc_NoCnfReq),   // note: there shall be no CNF to avoid race conditions in case of UL data pending
    Request := {
      PdcpHandoverControl := {
        HandoverComplete := true
      }
    }
  };

  //****************************************************************************
  // common IND ASPs
  //----------------------------------------------------------------------------

  template SYSTEM_IND car_SYSTEM_IND_Any :=
  { /* @status    APPROVED */
    Common        := cr_IndAspCommonPart_SysInd(?, cr_TimingInfo_Any),
    Indication    := ?
  };

  template RachPreamble_Type cr_RachPreamble(template (present) PrachPreambleIndex_Type p_PrachPreambleIndex) :=
  { /* @status    APPROVED */
    RAPID        := p_PrachPreambleIndex,
    PRTPower     := ?
  };

  // WORKAROUND
  template RachPreamble_Type cr_RachPreamble_Def := cr_RachPreamble(tsc_EUTRA_PrachPreambleIndex_Dedicated_Def); /* @status    APPROVED */
  // /WORKAROUND

/*   template SYSTEM_IND car_PRACH_Preamble_IND(CellId_Type p_CellId, */
/*                                              template (present) TimingInfo_Type p_TimingInfo, */
/*                                              template (present) RachPreamble_Type p_RachPreamble := cr_RachPreamble(tsc_EUTRA_PrachPreambleIndex_Dedicated_Def)) :=   */
  template SYSTEM_IND car_PRACH_Preamble_IND(CellId_Type p_CellId,
                                             template (present) TimingInfo_Type p_TimingInfo,
                                             template (present) RachPreamble_Type p_RachPreamble := cr_RachPreamble_Def) :=
  { /* @status    APPROVED */
    Common := cr_IndAspCommonPart_SysInd (p_CellId, p_TimingInfo),
    Indication := {
      RachPreamble := p_RachPreamble
    }
  };

  template SYSTEM_IND car_PRACH_Preamble_IND_Any ( CellId_Type p_CellId,
                                                   template (present) TimingInfo_Type p_TimingInfo) :=
    /* @status    APPROVED */
    car_PRACH_Preamble_IND(p_CellId, p_TimingInfo, cr_RachPreamble(?));

  //************************************************************************************
  // IE templates used in cas_RachPreambleIndConfig_REQ and car_SYSTEM_CTRL_L1MACInd_CNF
  //------------------------------------------------------------------------------------

  const L1Mac_IndicationMode_Type tsc_L1Mac_IndicationMode_Enable :=  enable;      /* @status    APPROVED */
  const L1Mac_IndicationMode_Type tsc_L1Mac_IndicationMode_Disable :=  disable;    /* @status    APPROVED */

  template (value) L1Mac_IndicationControl_Type cs_EnableRachPreamble_Req(L1Mac_IndicationMode_Type p_RachPreambleMode) :=
  { /* @status    APPROVED */
    RachPreamble  := p_RachPreambleMode, /* To enable/disable reporting of PRACH preamble received. */
    SchedReq  := omit,
    BSR       := omit,
    UL_HARQ   := omit,
    C_RNTI    := omit,
    PHR       := omit,
    HarqError := omit
  };

  template (value) L1Mac_IndicationControl_Type cs_HarqErrorInd_Req(L1Mac_IndicationMode_Type p_Mode) :=
  { /* @status    APPROVED */
    RachPreamble  := omit,
    SchedReq  := omit,
    BSR       := omit,
    UL_HARQ   := omit,
    C_RNTI    := omit,
    PHR       := omit,
    HarqError := p_Mode
  };

  //************************************************************************************
  // R8 wrapper templates
  //------------------------------------------------------------------------------------

  template (value) TDD_Config_Type cs_TDD_Config_r8(template (value) TDD_Config p_TDD_Config) :=
  { /* @status    APPROVED */
    R8 := p_TDD_Config
  };

  template (value) PHICH_Config_Type cs_PHICH_Config_r8(template (value) PHICH_Config p_PHICH_Config) :=
  { /* @status    APPROVED */
    R8 := p_PHICH_Config
  };

  template (value) PRACH_Config_Type cs_PRACH_Config_r8(template (value) PRACH_Config p_PRACH_Config) :=
  { /* @status    APPROVED */
    R8 := p_PRACH_Config
  };

  template (value) PUCCH_ConfigCommon_Type cs_PUCCH_ConfigCommon_r8(template (value) PUCCH_ConfigCommon p_PUCCH_ConfigCommon) :=
  { /* @status    APPROVED */
    R8 := p_PUCCH_ConfigCommon
  };

  template (value) PUCCH_ConfigDedicated_Type cs_PUCCH_ConfigDedicated_r8(template (value) PUCCH_ConfigDedicated p_PUCCH_ConfigDedicated) :=
  { /* @status    APPROVED */
    R8 := p_PUCCH_ConfigDedicated
  };

  template (value) PUSCH_ConfigCommon_Type cs_PUSCH_ConfigCommon_r8(template (value) PUSCH_ConfigCommon p_PUSCH_ConfigCommon) :=
  { /* @status    APPROVED */
    R8 := p_PUSCH_ConfigCommon
  };

  template (value) PUSCH_ConfigDedicated_Type cs_PUSCH_ConfigDedicated_r8(template (value) PUSCH_ConfigDedicated p_PUSCH_ConfigDedicated) :=
  { /* @status    APPROVED */
    R8 := p_PUSCH_ConfigDedicated
  };

  template (value) SoundingRS_UL_ConfigCommon_Type cs_SoundingRS_UL_ConfigCommon_r8(template (value) SoundingRS_UL_ConfigCommon p_SoundingRS_UL_ConfigCommon) :=
  { /* @status    APPROVED */
    R8 := p_SoundingRS_UL_ConfigCommon
  };

  template (value) SoundingRS_UL_ConfigDedicated_Type cs_SoundingRS_UL_ConfigDedicated_r8(template (value) SoundingRS_UL_ConfigDedicated p_SoundingRS_UL_ConfigDedicated) :=
  { /* @status    APPROVED */
    R8 := p_SoundingRS_UL_ConfigDedicated
  };

  template (value) SchedulingRequestConfig_Type cs_SchedulingRequestConfig_r8(template (value) SchedulingRequestConfig p_SchedulingRequestConfig) :=
  { /* @status    APPROVED */
    R8 := p_SchedulingRequestConfig
  };

  template (value) CQI_ReportConfig_Type cs_CQI_ReportConfig_r8(template (value) CQI_ReportConfig p_CQI_ReportConfig) :=
  { /* @status    APPROVED */
    R8 := p_CQI_ReportConfig
  };

  template (value) RACH_ConfigCommon_Type cs_RACH_ConfigCommon_r8(template (value) RACH_ConfigCommon p_RACH_ConfigCommon) :=
  { /* @status    APPROVED */
    R8 := p_RACH_ConfigCommon
  };

  template (value) RACH_ConfigDedicated_Type cs_RACH_ConfigDedicated_r8(template (value) RACH_ConfigDedicated p_RACH_ConfigDedicated) :=
  { /* @status    APPROVED */
    R8 := p_RACH_ConfigDedicated
  };

  template (value) MeasGapConfig_Type cs_MeasGapConfig_r8(template (value) MeasGapConfig p_MeasGapConfig) :=
  { /* @status    (APPROVED) */
    R8 := p_MeasGapConfig
  };

  template (value) AntennaInfoCommon_Type cs_AntennaInfoCommon_r8(template (value) AntennaInfoCommon p_AntennaInfoCommon) :=
  { /* @status    APPROVED */
    R8 := p_AntennaInfoCommon
  };

  template (value) AntennaInfoDedicated_Type cs_AntennaInfoDedicated_r8(template (value) AntennaInfoDedicated p_AntennaInfoDedicated) :=
  { /* @status    APPROVED */
    R8 := p_AntennaInfoDedicated
  };

  template (value) PDCP_Config_Type cs_PDCP_Config_r8(template (value) PDCP_Config p_PDCP_Config) :=
  { /* @status    APPROVED */
    R8 := p_PDCP_Config
  };

  template (value) UL_AM_RLC_Type cs_UL_AM_RLC_r8(template (value) UL_AM_RLC p_UL_AM_RLC) :=
  { /* @status    APPROVED */
    R8 := p_UL_AM_RLC
  };

  template (value) DL_AM_RLC_Type cs_DL_AM_RLC_r8(template (value) DL_AM_RLC p_DL_AM_RLC) :=
  { /* @status    APPROVED */
    R8 := p_DL_AM_RLC
  };

  template (value) UL_UM_RLC_Type cs_UL_UM_RLC_r8(template (value) UL_UM_RLC p_UL_UM_RLC) :=
  { /* @status    APPROVED */
    R8 := p_UL_UM_RLC
  };

  template (value) DL_UM_RLC_Type cs_DL_UM_RLC_r8(template (value) DL_UM_RLC p_DL_UM_RLC) :=
  { /* @status    APPROVED */
    R8 := p_DL_UM_RLC
  };

  template (value) TTI_BundlingConfig_Type cs_TTI_BundlingConfig_r8(boolean p_TtiBundling) :=
  { /* @status    APPROVED */
    R8 := p_TtiBundling
  };

  template (value) SpsConfigurationDL_Type cs_SpsConfigurationDL_r8(template (value) SPS_ConfigDL.setup p_SpsConfigDL) :=
  {
    R8 := p_SpsConfigDL
  };

  template (value) SpsConfigurationUL_Type cs_SpsConfigurationUL_r8(template (value) SPS_ConfigUL.setup p_SpsConfigUL) :=
  {
    R8 := p_SpsConfigUL
  };

  template (value) UplinkPowerControlCommon_Type cs_UplinkPowerControlCommon_r8(template (value) UplinkPowerControlCommon p_UplinkPowerControlCommon) :=
  { /* @status    APPROVED */
    R8 := p_UplinkPowerControlCommon
  };

  template (value) UplinkPowerControlDedicated_Type cs_UplinkPowerControlDedicated_r8(template (value) UplinkPowerControlDedicated p_UplinkPowerControlDedicated) :=
  { /* @status    APPROVED */
    R8 := p_UplinkPowerControlDedicated
  };
}
