/******************************************************************************/
// @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-15 18:29:22 +0200 (Wed, 15 Sep 2010) $
// $Rev: 3910 $
/******************************************************************************/
module MAC_711 {
//=========================================================================
// Import Part
//=========================================================================

  import from EUTRA_CellInfo all;
  import from EUTRA_CommonProcedures all;
  import from EUTRA_LoopBack all;
  import from EPS_NAS_Templates all;
  import from EPS_NAS_Constants all;
  import from EUTRA_ASP_TypeDefs all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_CommonDefs all;
  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_RRC_Templates all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_Component all;
  import from EUTRA_Paging all;
  import from EUTRA_RRCSteps all;
  import from EUTRA_SecurityFunctions all;
  import from MAC_Templates all;
  import from L2_CommonTemplates all;
  import from L2_CommonFunctions all;
  import from MAC_Functions all;
  import from EUTRA_ConfigurationSteps all;
  import from EUTRA_SecuritySteps all;
  import from EPS_NAS_MsgContainers all;
  import from EUTRA_ASP_SrbDefs all;

  //=========================================================================
  // Test case main functions
  //=========================================================================

  group LocalFunctions {
  /*
   * @desc      local function for step 1..4 of test case 7.1.1.1
   * @param     p_CellId
   * @param     p_DiffLCHID
   * @status    APPROVED
   */
  function fl_TC_7_1_1_1_Step1To4 ( CellId_Type p_CellId,
                                    TestLogicalChannelId_Type p_DiffLCHID ) runs on EUTRA_PTC
  {
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var template (value) EUTRA_FDD_TDD_CellInfo_Type  v_EUTRA_FDD_TDD_Info := f_EUTRA_CellInfo_GetFDD_TDD_Info(p_CellId);
    var SR_PUCCH_ResourceIndex_Type v_Sr_PUCCH_ResourceIndex := f_EUTRA_CellInfo_GetSR_PUCCH_ResourceIndex ( p_CellId );
    var template (value) PhysicalConfigDedicated_AntennaInfo_Type v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (p_CellId);
    var SR_ConfigIndex_Type v_SR_ConfigIndex := f_EUTRA_CellInfo_GetSR_ConfigIndex (p_CellId );
    timer t_Watchdog := 5.0;              // Local wait Timer
    f_SS_ReconfigForDiffLCHID (p_CellId, tsc_RbId_SRB0, p_DiffLCHID ); // Configure SS to use diff LCHID for SRB0
    // this will result in Logical channel ID =p_DiffLCHID t be used in MAC header of step 3.
    //@siclog "Step 1" siclog@
    f_EUTRA_UE_Page_Def ( p_CellId );
    //@siclog "Step 2" siclog@
    t_Watchdog.start;
    alt {
      []SRB.receive ( car_SRB0_RrcPdu_IND ( p_CellId, cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) )
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
          t_Watchdog.stop;
        }
    };

    //@siclog "Step 3" siclog@
    //@sic R5s100520 sic@
    SRB.send ( cas_SRB0_RrcPdu_REQ ( p_CellId,
                                     cs_TimingInfo_Now,
                                     cs_508_RRCConnectionSetup ( v_RRC_TI,
                                                                 v_EUTRA_FDD_TDD_Info.pucch_Config,
                                                                 v_Sr_PUCCH_ResourceIndex,
                                                                 v_AntennaInfo,
                                                                 v_SR_ConfigIndex  //@sic R5-102177 sic@
                                                                 ) ) );

    //@siclog "Step 4" siclog@
    t_Watchdog.start;
    alt {
      []SRB.receive ( car_SRB1_RrcNasPdu_IND ( p_CellId,
                                               cr_508_RRCConnectionSetupComplete ( v_RRC_TI, ? ),
                                               ? ) )
        {
          t_Watchdog.stop;
          f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 2: UE Accepted a MAC PDU with reserved LCH ID");
        }
    //@sic R5s100520 parallel behaviour in 7.1.1.1.3.2-2sic@
      []SRB.receive ( car_SRB0_RrcPdu_IND ( p_CellId, cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) )
        {
          repeat;
        }
      [] t_Watchdog.timeout
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
        }
    }
  };

  /*
   * @desc      local function for step 5..8 of test case 7.1.1.1
   * @param     p_CellId
   * @status    APPROVED
   */
  function fl_TC_7_1_1_1_Step5To7 ( CellId_Type p_CellId ) runs on EUTRA_PTC
  {
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    timer t_Watchdog := 5.0;              // Local wait Timer

    //@siclog "Step 5" siclog@
    f_EUTRA_UE_Page_Def ( p_CellId );
    //@siclog "Step 6" siclog@
    t_Watchdog.start;
    alt {
      []SRB.receive ( car_SRB0_RrcPdu_IND ( p_CellId, cr_RRCConnectionRequest ( cr_EstablishmentCause_mt_Access ) ) )
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 6");
          t_Watchdog.stop;
        }
    };

    //@siclog "Step 7" siclog@
    f_EUTRA_RRC_ConnectionSetup_Def( p_CellId, cs_TimingInfo_Now,  v_RRC_TI  );
  };

  /*
   * @desc      local function for step 1..2 of test case 7.1.1.2
   * @param     p_CellId
   * @param     p_DiffLCHID
   * @status    APPROVED
   */
  function fl_TC_7_1_1_2_Step1To2 ( CellId_Type p_CellId,
                                    TestLogicalChannelId_Type p_DiffLCHID) runs on EUTRA_PTC
  {
    var octetstring v_EncodedRlcPdu;
    var octetstring v_EncodedPdcpPdu;
    timer t_Watchdog := 5.0;              // Local wait Timer

    //@siclog "Step 1" siclog@
    // 1 MAC PDU = 1 RLC PDU; 1RLC PDU =1 PDCP PDU
    // MAC Header = 1B
    // 1 MAC SDU of 14 => RLC SDU= 12, PDCP SDU = 10
    // Total MAC PDU = 1 + 14  = 15B = 120b (best fit)
    v_EncodedPdcpPdu  := f_PDCP_PDU_Encvalue (cs_PDCP_PDU_LongSN(cs_PDCP_DataPdu_LongSN(int2bit(0,12), crs_PDCP_SDU_L10)));
    v_EncodedRlcPdu   := f_RLC_PDU_Encvalue (cs_RLC_AMD_1PDU(tsc_NoPoll, int2bit (0,10), v_EncodedPdcpPdu));

    DRB.send (cas_DRB_COMMOM_REQ_MAC_PDU (p_CellId,
                                          tsc_RbId_DRB1,
                                          cs_TimingInfo_Now,
                                          cs_MAC_PDUList_1 (cs_MAC_PDU_NoPadding_RLCPDU_LCID(int2bit(p_DiffLCHID,5), v_EncodedRlcPdu))));

    //@siclog "Step 2" siclog@
    t_Watchdog.start;
    alt {
      []SYSIND.receive ( car_SchedulingReq_IND (p_CellId, ?))
        {
          t_Watchdog.stop;
          f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 2: UE is sending scheduling Request");
        }
      [] t_Watchdog.timeout
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
        }
    };
  }; // end of fl_TC_7_1_1_2_Step1To2

  /*
   * @desc      local function for step 3..6 of test case 7.1.1.2
   * @param     p_CellId
   * @status    APPROVED
   */
  function fl_TC_7_1_1_2_Step3To6 ( CellId_Type p_CellId ) runs on EUTRA_PTC
  {
    var octetstring v_EncodedRlcPdu, v_EncodedRlcPdu_Poll;
    var octetstring v_EncodedPdcpPdu;

    timer t_Watchdog := 5.0;              // Local wait Timer

    //@siclog "Step 5" siclog@
    // Preconfigure SS for UL grant transmission
    // 1 MAC PDU = 1 RLC PDU; 1RLC PDU =1 PDCP PDU
    // MAC Header = 1B
    // 1 MAC SDU of 14 => RLC SDU= 12, PDCP SDU = 10
    // Total MAC PDU = 1 + 14  = 15B = 120b (best fit)
    f_EUTRA_StartExplicitULGrantTransmissionOnSR_Reception( p_CellId,
                                                            cs_TimingInfo_Now,
                                                            4, 2); /* Nprb = 2, Itbs = 4          Table 7.1.7.2.1-1 of 36.213
                                                                      Imcs = 4 -> Itbs = 4   MCS index of table 8.6.1-1 of 36.213
                                                                      120 bits*/
    //@siclog "Step 3" siclog@
    v_EncodedPdcpPdu  := f_PDCP_PDU_Encvalue (cs_PDCP_PDU_LongSN(cs_PDCP_DataPdu_LongSN(int2bit(0,12), crs_PDCP_SDU_L10)));
    v_EncodedRlcPdu   := f_RLC_PDU_Encvalue (cs_RLC_AMD_1PDU(tsc_NoPoll, int2bit (0,10), v_EncodedPdcpPdu));
    v_EncodedRlcPdu_Poll := f_RLC_PDU_Encvalue (cs_RLC_AMD_1PDU(tsc_Poll, int2bit (0,10), v_EncodedPdcpPdu)); // In UL Poll bit can be set

    DRB.send (cas_DRB_COMMOM_REQ_MAC_PDU (p_CellId,
                                          tsc_RbId_DRB1,
                                          cs_TimingInfo_Now,
                                          cs_MAC_PDUList_1 (cs_MAC_PDU_NoPadding_RLCPDU_LCID(tsc_LchId_DRB1, v_EncodedRlcPdu))));
    //@siclog "Step 4" siclog@
    t_Watchdog.start;
    alt {
      []SYSIND.receive ( car_SchedulingReq_IND (p_CellId, ?))
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 4");
          t_Watchdog.stop;
        }
    };

    //@siclog "Step 5" siclog@
    // Grant allocated

    //@siclog "Step 6" siclog@
    t_Watchdog.start;
    alt {
      []DRB.receive (car_DRB_COMMON_IND_MAC (p_CellId, tsc_RbId_DRB1, cr_TimingInfo_Any,
                                             {cr_MAC_PDU_NoPadding_RLCPDU_LCID(tsc_LchId_DRB1, (v_EncodedRlcPdu,v_EncodedRlcPdu_Poll))}))
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 4");
          t_Watchdog.stop;
        }
    };
    f_RLC_Status_PDU_Tx (tsc_LchId_DRB1, 1);// send status PDU to avoid any retransmissions
  };
  } // end of group Local Functions.

  // Functions for Test cases
  //==========================================================
  // L2/MAC/7.1
  //==========================================================

  /*
   * @desc      L2/MAC/7.1
   *            7.1.1.1 : CCCH mapped to UL SCH/ DL-SCH / Reserved LCID (Logical Channel ID)
   * @status    APPROVED
   */
  function f_TC_7_1_1_1_EUTRA ( ) runs on EUTRA_PTC
  {
    var integer j;
    var NAS_MSG_Indication_Type v_NAS_Ind; //@sic R5-102180
    var SRB_COMMON_IND v_ReceivedAsp; //@sic R5-102180
    var RRC_TransactionIdentifier v_RRC_TI := tsc_RRC_TI_Def;
    var EUTRA_SecurityParams_Type v_Auth_Params ;
    timer t_Watchdog := 5.0;              // Local wait Timer

    ///////////////////////////////////////////////////////////////////////////////////
    //      PREAMBLE
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_Init (c1);

    f_EUTRA_CellConfig_Def (eutra_Cell1);
    f_EUTRA_Preamble ( eutra_Cell1, STATE2_IDLEUPDATE );
        v_Auth_Params := f_EUTRA_Security_Get();
    // creates cell and performs registration
    ///////////////////////////////////////////////////////////////////////////////////
    //      Test Body
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_TestBody_Set(true);
    // Test body starts
    //@siclog "Steps 1 - 2 - 3 - 4" siclog@
    for (j :=11; j<= 27; j:= j+1 )
      {
        fl_TC_7_1_1_1_Step1To4 ( eutra_Cell1, j );
      };
    f_SS_DisableMACTestMode(eutra_Cell1, tsc_RbId_SRB0);
    //@siclog "Steps 5 - 6 - 7" siclog@
    fl_TC_7_1_1_1_Step5To7(eutra_Cell1);
    //@siclog "Step 8" siclog@
    t_Watchdog.start;
    alt {
      [] SRB.receive ( car_SRB1_RrcNasPdu_IND ( eutra_Cell1,
                                                cr_508_RRCConnectionSetupComplete ( v_RRC_TI, ? ),
                                                cr_NAS_Indication ( tsc_SHT_IntegrityProtected,
                                                                    cr_508_SERVICE_REQUEST ( v_Auth_Params.KSIasme ) ) ) )-> value v_ReceivedAsp  // @sic R5-102180 sic@
        {
          f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 8");
          t_Watchdog.stop;
        }
    }
    // Step 4-7: @sic R5-102180 sic@
    //@siclog "Steps 9 - 10 - 11 - 12" siclog@
    // Steps 6 to 9 of the generic radio bearer establishment procedure (TS 36.508 5.4.3.3-1) are executed
    // to successfully complete the service request procedure
    v_NAS_Ind := v_ReceivedAsp.Signalling.Nas[0];
    f_EUTRA_ServiceReqPostamble_508Steps6_9 (eutra_Cell1, v_NAS_Ind); //@sic R5s100279
    f_EUTRA_TestBody_Set(false);
    //test body ends

    ///////////////////////////////////////////////////////////////////////////////////
    //      Postamble
    //////////////////////////////////////////////////////////////////////////////////
   
    // Release RRC Connection and Switch off
    f_EUTRA_Postamble ( eutra_Cell1, E2_CONNECTED ); //@sic R5-103875 sic@
  }

  /*
   * @desc      REFERENCE TS 36.523-1 clause 7.1.1.2
   * @status    APPROVED
   */
  function f_TC_7_1_1_2_EUTRA ( ) runs on EUTRA_PTC
  { /* 7.1.1.1 : CCCH mapped to UL SCH/ DL-SCH / Reserved LCID (Logical Channel ID) */
    var integer j;

    ///////////////////////////////////////////////////////////////////////////////////
    //      PREAMBLE
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_Init (c1);
    f_EUTRA_CellConfig_Def (eutra_Cell1);
    f_EUTRA_Preamble ( eutra_Cell1, STATE2A_TESTLOOP_ModeA );
    // creates cell and performs registration
    f_EUTRA_AS_CipheringAlgorithm_Set(eea0); // set null AS ciphering algorithm

    f_EUTRA_LoopBackActivation_State4_Def_DisableBSR_PHR(eutra_Cell1, cs_UE_TestLoopModeA_LB_Setup_NoScaling);
    // step to configure SS RLC/PDCP and MAC in No header manipulation mode on SS Side for DRB

    f_SS_Config1TM_DRB_NoMACHeader(eutra_Cell1, tsc_RbId_DRB1,cs_MAC_TestMode_NoHeaderManipulationDL_UL);

    f_SS_ConfigSchedulingRequestIndMode(eutra_Cell1, tsc_L1Mac_IndicationMode_Enable);

    ///////////////////////////////////////////////////////////////////////////////////
    //      Test Body
    //////////////////////////////////////////////////////////////////////////////////
    f_EUTRA_TestBody_Set(true,layer2); // @sic R5-101050.
    // Test body starts
    f_EUTRA_StopULGrantTransmission_PUCCH_Sync ( eutra_Cell1, cs_TimingInfo_Now );
    //@siclog "Steps 1 - 2" siclog@
    for (j :=11; j<= 27; j:= j+1 )
      {
        fl_TC_7_1_1_2_Step1To2 ( eutra_Cell1, j );
      };
    //@siclog "Steps 3 - 4 - 5 - 6" siclog@
    fl_TC_7_1_1_2_Step3To6(eutra_Cell1);


    f_EUTRA_TestBody_Set(false);
    //test body ends

    ///////////////////////////////////////////////////////////////////////////////////
    //      Postamble
    //////////////////////////////////////////////////////////////////////////////////
    f_SS_Config1TM_DRB_NoMACHeader(eutra_Cell1, tsc_RbId_DRB1,cs_MAC_TestMode_NormalMode); //@sic R5s100272
    // Configure to stop report of Scheduling Request
    f_SS_ConfigSchedulingRequestIndMode(eutra_Cell1, tsc_L1Mac_IndicationMode_Disable);
    // configure SS for default grant allocation
    f_EUTRA_StartDefULGrantTransmission(eutra_Cell1,cs_TimingInfo_Now);
    // Release RRC Connection and Switch off
    f_EUTRA_Postamble ( eutra_Cell1, E3_TESTMODE );//@sic R5-103875 sic@

  }
}
