/******************************************************************************/
// @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-03 20:04:51 +0200 (Fri, 03 Sep 2010) $
// $Rev: 3810 $
/******************************************************************************/

module EUTRA_NASCommonTemplates {
  
  import from CommonDefs all;
  import from NAS_CommonTypeDefs all;
  import from EPS_NAS_TypeDefs all;
  import from EPS_NAS_Constants all;
  import from EPS_NAS_MsgContainers all;
  import from NAS_CommonTemplates all;
  import from EPS_NAS_Templates all;
  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  
  
  template (value) PagingUE_Identity cs_PagingUE_Identity_IMSI ( IMSI p_IMSI ) :=
  { /* @status    APPROVED */
    imsi := p_IMSI
  };
  
  template (value) PDN_Address cs_IPv4_PDNAddressDHCP :=
  {
    iei := omit,       // Mandatory
    iel := '05'O,
    spare := tsc_Spare5,
    typeValue := '001'B,  // IPv4
    adressInfo := '00000000'O  // DHCP is to be used
  };
  
  template (value) PDN_Address cs_IPv6_PDNAddressDHCP :=
  {
    iei := omit,       // Mandatory
    iel := '09'O,
    spare := tsc_Spare5,
    typeValue := '010'B,  // IPv4v6
    adressInfo := int2oct(0, 8) & '00000000'O  // what is the IP6 identifier?
  };
  
  template (value) PDN_Address cs_IPv4v6_PDNAddressDHCP :=
  {
    iei := omit,       // Mandatory
    iel := '0C'O,
    spare := tsc_Spare5,
    typeValue := '011'B,  // IPv4v6
    adressInfo := int2oct(0, 8) & '00000000'O  // DHCP is to be used
  };
  
  template NAS_UL_Message_Type cdr_ATTACH_REQUEST_WithGuti(template (present) NAS_AttDetValue_Type p_AttachTypeValue,
                                                           template AdditionalUpdateType p_AdditionalUpdateType,
                                                           template (present) NAS_KsiValue p_KsiValue,
                                                           template (present) MobileIdentity p_MobileId,
                                                           template TrackingAreaId p_Tai)
    modifies cr_508_ATTACH_REQUEST :=
  { /* @status    APPROVED */
    aTTACH_REQUEST := {
      nasKeySetId              := cr_NAS_KeySetIdentifier(p_KsiValue, tsc_Spare1),
      epsAttachType            := cr_EPS_AttachType(p_AttachTypeValue),
      oldGutiOrImsi            := p_MobileId,
      lastVisitedRegisteredTai := p_Tai
    }
  };
  
  template NAS_UL_Message_Type cdr_ATTACH_REQUEST_WithEPSnativeSecurityCtx(template (present) NAS_AttDetValue_Type p_AttachTypeValue,
                                                                           template AdditionalUpdateType p_AdditionalUpdateType,
                                                                           template (present) NAS_KsiValue p_KsiValue)
    modifies cr_508_ATTACH_REQUEST :=
  { /* @status    APPROVED */
    aTTACH_REQUEST := {
      nasKeySetId              := cr_NAS_KeySetIdentifier(p_KsiValue, tsc_NasKsi_NativeSecurityContext),
      epsAttachType            := cr_EPS_AttachType(p_AttachTypeValue)
    }
  };
  
  template NAS_UL_Message_Type cdr_ATTACH_REQUEST_WithOldMobileIdNoTAI(template (present) NAS_AttDetValue_Type p_AttachTypeValue,
                                                                       template AdditionalUpdateType p_AdditionalUpdateType,
                                                                       template MobileIdentity p_MobileId)
    modifies cr_508_ATTACH_REQUEST :=
  { /* 24.301 cl. 8.2.4 */
    /* @status    APPROVED */
    aTTACH_REQUEST := {
      nasKeySetId              := cr_NAS_KeySetIdentifier(tsc_NasKsi_NoKey, tsc_Spare1),
      oldGutiOrImsi            := p_MobileId,
      lastVisitedRegisteredTai := omit
      
    }
  };
    
template NAS_UL_Message_Type cdr_ATTACH_REQUEST_WithGutiAndTAI(template (present) NAS_AttDetValue_Type p_AttachTypeValue,
                                                               template AdditionalUpdateType p_AdditionalUpdateType,
                                                               template (present) MobileIdentity       p_MobileId,
                                                               NAS_PlmnId                              p_PlmnId,
                                                               NAS_Tac                                 p_Tac,
                                                               template    TMSI_Status          p_TmsiStatus)
  modifies cr_508_ATTACH_REQUEST :=
{ /* 24.301 cl. 8.2.4 */
  aTTACH_REQUEST := {
    
    nasKeySetId              := cr_NAS_KeySetIdentifier(tsc_NasKsi_NoKey, ?),
    oldGutiOrImsi            := p_MobileId,
    lastVisitedRegisteredTai := cr_TAI (p_PlmnId, p_Tac),
    oldLai                   := omit,
    tmsiStatus               := p_TmsiStatus
  }
};

  template (value) NAS_DL_Message_Type cs_508_AttachReject (template (value) EMM_Cause p_Cause,
                                                            template (omit) ESM_MessageContainer p_ESM):=
  { /* @status    APPROVED */
    aTTACH_REJECT := {
      securityHeaderType := tsc_SHT_NoSecurityProtection,
      protocolDiscriminator := tsc_PD_EMM,
      messageType := tsc_MT_AttachReject,
      emmCause := p_Cause,
      esmMessage := p_ESM
    }
  };
  
  template (value) NAS_DL_Message_Type cds_ATTACH_ACCEPT_t3402(NAS_AttDetValue_Type p_EpsAttachResultValue,
                                                                          template (value) TrackingAreaIdList p_TaiList,
                                                                          template (omit)  MobileIdentity p_Guti,
                                                                          template (omit)  LocAreaId p_Lai,
                                                                          template (omit)  MobileIdentity p_MSId,
                                                                          template (omit)  EMM_Cause p_Cause,
                                                                          template (omit) AdditionalUpdateResult p_AdditionalUpdateResult,
                                                                          template (value) GPRS_Timer p_T3402)
  modifies cs_508_ATTACH_ACCEPT :=
{
  aTTACH_ACCEPT := {
    t3402      := p_T3402
  }
};

template (value) NAS_DL_Message_Type cs_508_AUTHENTICATION_REJECT :=
  { /* @status    APPROVED */
    aUTHENTICATION_REJECT := {
      securityHeaderType          := tsc_SHT_NoSecurityProtection,
      protocolDiscriminator       := tsc_PD_EMM,
      messageType                 := tsc_MT_AuthenticationReject
    }
  };
  
  template (value) NAS_DL_Message_Type cs_508_TRACKING_AREA_UPDATE_REJECT (B8_Type p_EmmCause) :=
  {
    tRACKING_AREA_UPDATE_REJECT := {
      securityHeaderType        := tsc_SHT_NoSecurityProtection,
      protocolDiscriminator     := tsc_PD_EMM,
      messageType               := tsc_MT_TauReject,
      emmCause                  := cs_EMM_Cause_v(p_EmmCause)
    }
  };
  
  template NAS_UL_Message_Type cdr_AUTHENTICATION_FAIL_synch modifies cr_AUTHENTICATION_FAIL_Any :=
  { /* @status    APPROVED */
    aUTHENTICATION_FAILURE := {
      emmCause                 := cr_EMM_Cause_AuthFail_SQN,          /* cl. 9.9.3.7  M V 1   */
      authenticationFailureParameter :=  cr_AuthFailParam_Any
    }
  };
  
  template NAS_UL_Message_Type cdr_AUTHENTICATION_FAIL_mac modifies cr_AUTHENTICATION_FAIL_Any :=
  { /* @status    APPROVED */
    aUTHENTICATION_FAILURE := {
      emmCause                 := cr_EMM_Cause_AuthFail_Mac           /* cl. 9.9.3.7  M V 1   */
    }
  };
  
  template NAS_UL_Message_Type cr_508_EXT_SERVICE_REQUEST(NAS_KsiValue p_KsiValue,
                                                          EPS_ServiceTypeValue p_ServiceType,
                                                          EPS_M_Tmsi p_MTmsi) :=
  { /* @status    APPROVED */
    eXT_SERVICE_REQUEST := {
      securityHeaderType    := tsc_SHT_NoSecurityProtection,
      protocolDiscriminator := tsc_PD_EMM,
      messageType           := tsc_MT_ExtServiceRequest,
      nasKeySetId           := cr_NAS_KeySetIdentifier(p_KsiValue,
                                                       tsc_NasKsi_NativeSecurityContext),
      serviceType           := { omit, p_ServiceType },
      mTMSI                 := cr_MobileIdTMSI_lv(p_MTmsi),
      csfbResponse          := cr_CSFBResponse('001'B) // CS Fallback accepted by ue
    }
  };
  
  template CSFBResponse cr_CSFBResponse (B3_Type p_Csfb) :=                                     // 24.301 cl. 9.9.3.5
  { /* @status    APPROVED */
    iei       := 'B'H,                       // currently only used as TV
    spare     := '0'B,
    csfbValue := p_Csfb                      // 0 = CS fallback rejected, 1 = accepted by UE
  };
  
  template EMM_Cause cr_EMM_Cause_AuthFail_Mac := cs_EMM_Cause_v('00010100'B); /* @status    APPROVED */
  
  template EMM_Cause cr_EMM_Cause_AuthFail_SQN := cs_EMM_Cause_v('00010101'B); /* @status    APPROVED */
  
  template (value) PLMN_List cs_PLMNList_2(NAS_PlmnId p_PLMN1,
                                           NAS_PlmnId p_PLMN2) :=
  {
    iei    := '4A'O,
    iel    := int2oct(6, 1),        // two PLMN is included
    plmnList         :=  {
      [0] :=  p_PLMN1 ,
      [1] :=  p_PLMN2
    }                        /* list of PLMNs */
  };
  
  template (value) PLMN_List cs_PLMNList_3(NAS_PlmnId p_PLMN1,
                                           NAS_PlmnId p_PLMN2,
                                           NAS_PlmnId p_PLMN3
                                           ) :=
  {
    iei    := '4A'O,
    iel    := int2oct(9, 1),        // three PLMN is included
    plmnList         :=  {
      [0] :=  p_PLMN1,
      [1] :=  p_PLMN2,
      [2] :=  p_PLMN3
    }                        /* list of PLMNs */
  };
  
  template NAS_UL_Message_Type cdr_TAU_Request_OldGuti_BearerEst(EPS_UpdateTypeValue p_EpsUpdate_TypeValue,
                                                                 NAS_KsiValue p_KsiValue,
                                                                 template AdditionalUpdateType p_AdditionalUpdateType,
                                                                 template (present) MobileIdentity p_OldGuti)
    modifies cr_508_TAU_Request :=
  { /* @status    APPROVED */
    tRACKING_AREA_UPDATE_REQUEST := {
      epsUpdateType := cr_EPS_UpdateType(tsc_EpsUpdate_Active, p_EpsUpdate_TypeValue),
      oldGuti       := p_OldGuti
    }
  };

  template NAS_UL_Message_Type cdr_TAU_Request_specificGutiTaiLai(EPS_UpdateTypeValue p_EpsUpdate_TypeValue,
                                                                  NAS_KsiValue p_KsiValue,
                                                                  template AdditionalUpdateType p_AdditionalUpdateType,
                                                                  template (value) MobileIdentity p_OldGuti,
                                                                  template (omit) LocAreaId p_LAI,
                                                                  template (omit) TrackingAreaId p_TAI,
                                                                  template (omit) TMSI_Status   p_TmsiStatus)
    modifies cr_508_TAU_Request :=
  {
   tRACKING_AREA_UPDATE_REQUEST := {
     epsUpdateType := cr_EPS_UpdateType('0'B, p_EpsUpdate_TypeValue),
     oldGuti       := p_OldGuti,
     lastVisitedRegisteredTai   := p_TAI,
     oldLai                     := p_LAI,
     tmsiStatus                 := p_TmsiStatus
   }
  };
  
  template NAS_UL_Message_Type cdr_TAU_RequestOldGutiLaiTmsiStatus(EPS_UpdateTypeValue p_EpsUpdate_TypeValue,
                                                                   NAS_KsiValue p_KsiValue,
                                                                   template AdditionalUpdateType p_AdditionalUpdateType,
                                                                   template (present) MobileIdentity p_OldGuti,
                                                                   template LocAreaId p_LAI,
                                                                   template TrackingAreaId p_TAI,
                                                                   template TMSI_Status p_TmsiStatus)
    modifies cr_508_TAU_Request :=
  {
    tRACKING_AREA_UPDATE_REQUEST := {
      epsUpdateType := cr_EPS_UpdateType('0'B, p_EpsUpdate_TypeValue),
      oldGuti       := p_OldGuti,
      lastVisitedRegisteredTai   := p_TAI,
      oldLai                     := p_LAI,
      tmsiStatus                 := p_TmsiStatus
    }
  };
  
  //@sic R5-102227
  template Nonce cr_NonceUE_Any := {                                           /* 24.301 cl. 9.9.3.25 */
    iei       := '55'O            , /* present in case of TV; omit in case of V */
    nonceValue := ?
  };
  //@sic R5-102227
  template (value) Nonce cs_Nonce ( template (omit) IEI8_Type p_IEI,
                                    O4_Type p_NonceValue) := {                                           /* 24.301 cl. 9.9.3.25 */
    iei       := p_IEI, /* present in case of TV; omit in case of V */
    nonceValue := p_NonceValue
  };
  
  template NAS_UL_Message_Type cdr_TAU_RequestIdleMobilityDiffRAT (EPS_UpdateTypeValue p_EpsUpdate_TypeValue,
                                                                   NAS_KsiValue p_KsiValue,
                                                                   template AdditionalUpdateType p_AdditionalUpdateType)
    modifies cr_508_TAU_Request :=
  {
    tRACKING_AREA_UPDATE_REQUEST := {
      oldPtmsiSignature          := *, //The UE shall include this IE if the TIN indicates "P-TMSI" and the UE holds a valid P-TMSI signature, P-TMSI and RAI.
      additionalGuti             := *, // The UE shall include this IE if the TIN indicates "P-TMSI" and the UE holds a valid GUTI, P-TMSI and RAI
      nonce                      := cr_NonceUE_Any //This IE is included if the UE performs an A/Gb mode or Iu mode to S1 mode inter-system change in idle mode.
    }
  };
  //@sic R5-102227
  template (value) NAS_DL_Message_Type cds_SECURITY_MODE_COMMAND_MappedContext(B3_Type p_CipheringType,
                                                                               B3_Type p_IntegrityType,
                                                                               NAS_KsiValue p_AsmeKeySetId,
                                                                               UE_SecurityCapability p_SecurityCap,
                                                                               B32_Type p_NonceUE,
                                                                               B32_Type p_NonceMME ) // @sic R5s100135 sic@
    modifies cs_508_SECURITY_MODE_COMMAND :=
  {
    sECURITY_MODE_COMMAND := {
      asmeNasKeySetId            := cs_NAS_KeySetIdentifier_lv(p_AsmeKeySetId, tsc_NasKsi_MappedSecurityContext),
      replayedNonceUe            := cs_Nonce ( '55'O, bit2oct (p_NonceUE)),
      nonceMme                   := cs_Nonce ( '56'O, bit2oct (p_NonceMME))
    }
  };
}
