/******************************************************************************/
// @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.
//   (c) 2023, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC).
//   All rights reserved.
// @version: IWD_23wk37
// $Date: 2022-12-07 16:03:11 +0100 (Wed, 07 Dec 2022) $
// $Rev: 35058 $
/******************************************************************************/
module Common4G5G_LoopBack {
  import from CommonDefs all;
  import from NAS_CommonTypeDefs all;
  import from EPS_NAS_LoopBack_TypeDefs all;
  import from NAS_CommonTemplates all;

  //----------------------------------------------------------------------------
  // Constant definitions Elements (36.509)
  //----------------------------------------------------------------------------

  // Message Type definitions from 36.509 clause 6.X
  const MessageType tsc_MsgType_CloseUE_TestLoop        := oct2bit ('80'O);     /* @status    APPROVED (ENDC, IMS, LTE, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const MessageType tsc_MsgType_CloseUE_TestLoopCmpl    := oct2bit ('81'O);     /* @status    APPROVED (ENDC, IMS, LTE, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const MessageType tsc_MsgType_OpenUE_TestLoop         := oct2bit ('82'O);     /* @status    APPROVED (ENDC, LTE, NBIOT, NR5GC) */
  const MessageType tsc_MsgType_OpenUE_TestLoopCmpl     := oct2bit ('83'O);     /* @status    APPROVED (ENDC, LTE, NBIOT, NR5GC) */
  const MessageType tsc_MsgType_Activate_TestMode       := oct2bit ('84'O);     /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const MessageType tsc_MsgType_Activate_TestModeCmpl   := oct2bit ('85'O);     /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const MessageType tsc_MsgType_Deactivate_TestMode     := oct2bit ('86'O);     /* @status    APPROVED (ENDC, LTE, LTE_A_PRO, NBIOT, NR5GC) */
  const MessageType tsc_MsgType_Deactivate_TestModeCmpl := oct2bit ('87'O);     /* @status    APPROVED (ENDC, LTE, LTE_A_PRO, NBIOT, NR5GC) */
  const MessageType tsc_MsgType_SetULMsgReq         := '10101100'B;             /* @status    APPROVED (NR5GC) */
  const MessageType tsc_MsgType_SetULMsgResp        := '10101101'B;             /* @status    APPROVED (NR5GC) */


  const UE_TestLoopMode_Type tsc_UE_TestLoopMode_TypeA   := '00'O;              /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const UE_TestLoopMode_Type tsc_UE_TestLoopMode_TypeB   := '01'O;              /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const UE_TestLoopMode_Type tsc_UE_TestLoopMode_TypeE   := '04'O;              /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NR5GC, NR5GC_IRAT, POS) */
  const SkipIndicator   tsc_Gen_SkipIndicator := '0000'B;                       /* Skip Indicator field consists of latter half byte
                                                                                   after Protocol discriminator field.
                                                                                   Its value is '0000'B in all the messages.
                                                                                   @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const ProtocolDiscriminator   tsc_LB_ProtocolDiscriminator := '1111'B;        /* Protocol descriminator for test procedures
                                                                                   24.007 Table 11.2
                                                                                   @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, NBIOT, NR5GC, NR5GC_IRAT, POS) */
  const B1_Type tsc_LoopModeE_Transmission := '1'B;
  const B1_Type tsc_LoopModeE_Reception := '0'B;
  const B1_Type tsc_LoopModeE_V2x := '1'B;
  
  // =============================================================================
  // Close UE Test Loop
  // 3G TS 36.509 cl. 6.1
  // Direction:     SS to UE
  // -----------------------------------------------------------------------------

  type record NR_CLOSE_UE_TEST_LOOP { /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, MCX, NBIOT, NR5GC, NR5GC_IRAT, POS, UTRAN) */
    SkipIndicator                       skipIndicator,                          /*    M V 1/2    */
    ProtocolDiscriminator               protocolDiscriminator,                  /*    M V 1/2    */
    MessageType                         messageType,                            /*    M V 1      */
    UE_TestLoopMode_Type                ueTestLoopMode,                         /*    M V 1      */
    UE_TestLoopModeA_NR_LB_Setup_Type   ueTestLoopModeA_LB_Setup optional,      /*    C LV 1-25
                                                                                      present if ueTestLoopMode ='00'F */
    UE_TestLoopModeB_LB_Setup_Type      ueTestLoopModeB_LB_Setup optional,      /*    C V 1
                                                                                      present if ueTestLoopMode ='01'F/
                                                                                      represents IP PDU delay time 0..255 seconds */
    UE_TestLoopModeE_LB_Setup_Type      ueTestLoopModeE_LB_Setup optional      /*    C LV 1-25
                                                                                      present if ueTestLoopMode ='04'F */
    };
   
  // =============================================================================
  // Structured Type Definition
  // *** Comments:
  // UE Test Loop Mode A LB Setup  38.509 cl. 6.3.1
  // *** additional Notes:
  // The maximum number of LB entities in the LB setup list is less than or equal to 5.
  // -----------------------------------------------------------------------------
  type record UE_TestLoopModeA_NR_LB_Setup_Type { /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, MCX, NBIOT, NR5GC, NR5GC_IRAT, POS, UTRAN) */
    O1_Type                             iel,
    NR_LB_SetupDRB_IE_List_Type         lbSetupDrbList optional
  };

  // =============================================================================
  // LB Setup DRB IE
  // UE Test Loop Mode A LB Setup 38.509 cl. 6.3.1
  // -----------------------------------------------------------------------------
  type record NR_LB_SetupDRB_IE_Type { /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, MCX, NBIOT, NR5GC, NR5GC_IRAT, POS, UTRAN) */
    B16_Type                            uplinkPdcpSduSize,
    B2_Type                             reserved,
    B1_Type                             nrDRB,
    B5_Type                             drbIdentity
  };

  type record length (1..tsc_MAX_ModeA_LB_Entities)of NR_LB_SetupDRB_IE_Type NR_LB_SetupDRB_IE_List_Type; /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, MCX, NBIOT, NR5GC, NR5GC_IRAT, POS, UTRAN) */

  // =============================================================================
  // Structured Type Definition
  // Set UL Message
  // *** Comments:
  // 36.509 cl. 6.19
  // -----------------------------------------------------------------------------
  type record SET_UL_MESSAGE_REQUEST { /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, MCX, NBIOT, NR5GC, NR5GC_IRAT, POS, UTRAN) */
    SkipIndicator                       skipIndicator,                  /*    M V 1/2    */
    ProtocolDiscriminator               protocolDiscriminator,          /*    M V 1/2    */
    MessageType                         messageType,                    /*    M V 1      */
    B7_Type                             spare,
    B1_Type                             usePreconfiguredULMsg
  };
  
  type record SET_UL_MESSAGE_RESPONSE { /* @status    APPROVED (ENDC, IMS, IMS_IRAT, LTE, LTE_A_IRAT, LTE_A_PRO, LTE_A_R10_R11, LTE_A_R12, LTE_IRAT, MCX, NBIOT, NR5GC, NR5GC_IRAT, POS, UTRAN) */
    SkipIndicator                       skipIndicator,                  /*    M V 1/2    */
    ProtocolDiscriminator               protocolDiscriminator,          /*    M V 1/2    */
    MessageType                         messageType                     /*    M V 1      */
  };
  
  //----------------------------------------------------------------------------
  // Templates for Loop Back PDU's
  //----------------------------------------------------------------------------
  
  template (value) NR_LB_SetupDRB_IE_Type  cs_NR_LB_SetupDRB_IE(integer p_PDCP_SDU_Size,
                                                          integer p_DRB_Id,
                                                          B1_Type p_NRDRB := '1'B) :=
  { /* @status    APPROVED (ENDC, NR5GC) */
    uplinkPdcpSduSize := int2bit (p_PDCP_SDU_Size,16),  /* Uplink PDCP SDU size */
    reserved          := '00'B,
    nrDRB             := p_NRDRB,
    drbIdentity       := int2bit (p_DRB_Id-1, 5)       /* Note: acc. to ASN.1 DRB_Identity is 1..32 but the DRB-Identity of LB setup DRB IE has just 5 bits
                                                           => assumption: binary coded DRB identity means ASN.1 coded (i.e. "p_DRB_Id-1") */
  };

  template (value) UE_TestLoopModeA_NR_LB_Setup_Type cs_UE_TestLoopModeA_NR_LB_Setup_1DRB(integer p_PDCP_SDU_Size,
                                                                                          integer p_DRB_Id,
                                                                                          B1_Type p_NRDRB := '1'B) :=
  { /* @status    APPROVED (ENDC, NR5GC) */
    iel := '03'O, // 3 octets, i.e. one element in LB_SetupDRB_IE_List
    lbSetupDrbList := {
      cs_NR_LB_SetupDRB_IE(p_PDCP_SDU_Size, p_DRB_Id, p_NRDRB)
    }
  };

  template (value) UE_TestLoopModeA_NR_LB_Setup_Type cs_UE_TestLoopModeA_NR_LB_Setup_NoScaling :=
  { /* @status    APPROVED (ENDC, IMS, NR5GC, NR5GC_IRAT, POS) */
    iel := '00'O, // 0 octets, i.e. no element in LB_SetupDRB_IE_List
    lbSetupDrbList := omit
  };

  template (value) UE_TestLoopModeE_LB_Setup_Type cs_UE_TestLoopModeE_LB_Setup(B1_Type   p_CommTransmitOrReceive := tsc_LoopModeE_Reception,
                                                                               integer   p_CommMonitorListLength,
                                                                               template (omit) CommMonitorList_Type p_CommMonitorList := omit,
                                                                               B1_Type p_D2dOrV2x := '0'B // @sic R5-175219 sic@ D2D by default
                                                                               ) :=
  { /* @status     */
    commMonitorListLength  := int2bit(p_CommMonitorListLength,8),  // Length of UE test loop mode E Monitor setup contents in bytes
    reserved               := '000000'B,
    commTransmitOrReceive  := p_CommTransmitOrReceive,  // Communication Transmit or Receive
    commMonitorList        := p_CommMonitorList,
    d2dOrV2x               := p_D2dOrV2x // @sic R5-175219 sic@
  };
  
  template (value) SET_UL_MESSAGE_REQUEST  cs_SET_UL_MESSAGE_REQUEST (B1_Type p_UsePreconfigured) :=
  {/* @status    APPROVED (NR5GC) */
    skipIndicator            := tsc_Gen_SkipIndicator,                            /*    M V 1/2    */
    protocolDiscriminator    := tsc_LB_ProtocolDiscriminator,                     /*    M V 1/2    */
    messageType              := tsc_MsgType_SetULMsgReq,                          /*    M V 1      */
    spare                    := tsc_Spare7,
    usePreconfiguredULMsg    := p_UsePreconfigured
  };
  
  template (present) SET_UL_MESSAGE_RESPONSE  cr_SET_UL_MESSAGE_RESPONSE :=
  { /* @status    APPROVED (NR5GC) */
    skipIndicator            := tsc_Gen_SkipIndicator,                            /*    M V 1/2    */
    protocolDiscriminator    := tsc_LB_ProtocolDiscriminator,                     /*    M V 1/2    */
    messageType              := tsc_MsgType_SetULMsgResp                          /*    M V 1      */
  };
  
  
} with { encode "NAS Types"} // @sic R5s190445 sic@
