// ***************************************************************************
// @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-17 11:39:57 +0200 (Fri, 17 Sep 2010) $
// * $Rev: 3955 $
// ***************************************************************************

module NAS_EMMInformationProcedure {

  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_Component all;
  import from EUTRA_CommonDefs all;
  import from CommonDefs all;
  import from Parameters all;
  // import from EUTRA_Parameters all;
  import from NAS_CommonTypeDefs all;
  import from EUTRA_SRB_Templates all;
  import from EUTRA_AspCommon_Templates all;
  import from EUTRA_Timing all;
  import from EUTRA_CommonProcedures all;
  import from EPS_NAS_TypeDefs all;
  import from EPS_NAS_Constants all;
  import from EPS_NAS_Templates all;
  import from NAS_CommonTemplates all;
  import from EUTRA_ConfigurationSteps all;
  import from UpperTesterFunctions all;
  import from NAS_AuxiliaryDefsAndFunctions all;

  group LocalTemplates {
    template (value) TimeZoneAndTime cs_TimeZoneAndTime (
                                                         O1_Type p_Year,
                                                         O1_Type p_Month,
                                                         O1_Type p_Day,
                                                         O1_Type p_Hour,
                                                         O1_Type p_Minute,
                                                         O1_Type p_Second,
                                                         O1_Type p_TimeZone):=
    {
       iei      := '49'O,
       year     := p_Year,
       month    := p_Month,
       day      := p_Day,
       hour     := p_Hour,
       minute   := p_Minute,
       second   := p_Second,
       timeZone := p_TimeZone
    };

    template (value) TimeZone cs_TimeZone (O1_Type p_TimeZone):=
    {
       iei      := '46'O,
       timeZone := p_TimeZone
    };

    template (value) NetworkName cs_NetworkName (IEI8_Type p_Iei,
                                                 octetstring p_Name,
                                                 Type4Length_Type p_Length) :=
    {
        iei          := p_Iei,
        iel          := p_Length,
        ext          := '1'B,
        codingScheme := tsc_Spare3, // GSM default 7
        addCI        := tsc_Spare1, // don't include the CI
        spareBitCnt  := tsc_Spare3, // no info about spare bit count
        text         := p_Name
    };
 }
  group TestcaseImplementations {

    function f_TC_9_1_5_1_EUTRA() runs on EUTRA_PTC
    {
    // @desc EMM information procedure
     /* Test Purpose (TP)
        (1)
        with { UE in EMM-REGISTERED state and UE supporting the EMM information message }
        ensure that {
          when { UE receives an EMM Information message }
            then { UE accepts the message and use the contents to update appropriate information stored within the UE }
        }

       System Simulator:
           -    Cell A

       UE:
           -    None

       Preamble:
           -    UE is in state Generic RB established (state 3) on Cell A according to [18].
     */

        f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

        // create cell and send out system information
        f_EUTRA_CellConfig_Def (eutra_CellA);

        // Preamble:
        //   - UE is in state Generic RB established (state 3) on Cell A
        // ==========================================================================
        f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
        f_EUTRA_RbEst_Def(eutra_CellA);
        f_EUTRA_TestBody_Set(true);

        fl_TC_9_1_5_1_Body ();

        // Postamble
        f_EUTRA_TestBody_Set(false);
        f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);   // @sic R5-103875 sic@
    } // end f_TC_9_1_5_1_EUTRA


    function fl_TC_9_1_5_1_Body() runs on EUTRA_PTC
    {
        // ==========================================================================
        // initialize constant test case data structures
        // ==========================================================================
        var template  EMM_Cause v_EMMCause:=cs_EMM_Cause_v(tsc_EmmCause_MessageTypeNonExistentOrNotImplemented);
        var charstring v_FullNameString := "FullName1234567890";
        var charstring v_ShortNameString := "ShortName123";
        var octetstring v_FullNameOctets := f_ConvertIA5toDefaultGsm7Bit(v_FullNameString);
        var octetstring v_ShortNameOctets := f_ConvertIA5toDefaultGsm7Bit(v_ShortNameString);
        var charstring v_ReturnedString := "";

        timer t_Wait := f_EUTRA_SetTimerToleranceMin( eutra_CellA, 5.0 );

        // The SS transmits an EMM INFORMATION message
        //@siclog "Step 1" siclog@
        SRB.send(cas_SRB2_NasPdu_REQ(
                                 eutra_CellA,
                                 cs_TimingInfo_Now,
                                 cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                cs_508_EMMInformation( cs_NetworkName ('43'O, v_FullNameOctets, int2oct(lengthof (v_FullNameOctets)+1,1)),
                                                                       cs_NetworkName ('43'O, v_ShortNameOctets, int2oct(lengthof (v_ShortNameOctets)+1,1)),
                                                                       cs_TimeZone('01'O),
                                                                       cs_TimeZoneAndTime('10'O, '04'O, '12'O, '13'O, '38'O, '52'O, '01'O),
                                                                       cs_DaylightSavingTime_tlv('01'B)
                                                                      ))));


        //@siclog "Step 2" siclog@
        // Check: Does the UE transmit an EMM STATUS message with cause #97 "message type non-existent
        // or not implemented" in the next 5 seconds
        t_Wait.start;
        alt {
            [] SRB.receive (car_SRB2_NasPdu_IND(eutra_CellA,
                                       cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                         cr_508_EMMStatus(v_EMMCause))))
              {
                t_Wait.stop;
                f_EUTRA_SetVerdictFailOrInconc(__FILE__, __LINE__, "Step 2");
              }
            [] t_Wait.timeout
              {
                f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
              }
        }

       // EXCEPTION: Steps 3a1 to 3a4 describe behaviour that depends on the UE capability.

       //@siclog "Step 3a1" siclog@
       // IF pc_EMM_Information AND pc_FullNameNetwork
       // Check: Does the UE associate the "full length name of the network" with the MCC and MNC contained in
       // the last visited tracking area identification and is presented to the MS user at the earliest opportunity?
       if (pc_EMM_Information and pc_FullNameNetwork)
       {
          v_ReturnedString := f_UT_ChkOperatorName (UT, LONG_NAME);
          if (v_ReturnedString == v_FullNameString) {
            f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3a1");
          }
       }

       //@siclog "Step 3a2" siclog@
       // IF pc_EMM_Information AND pc_ShortNameNetwork
       // Check: Does the UE associate the "abbreviated name of the network" with the MCC and MNC contained
       // in the last visited tracking area identification and is presented to the MS user at the earliest opportunity?
       if (pc_EMM_Information and pc_ShortNameNetwork)
       {
          v_ReturnedString := f_UT_ChkOperatorName (UT, SHORT_NAME);
          if (v_ReturnedString == v_ShortNameString) {
            f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3a2");
          }
       }

       //@siclog "Step 3a3" aiclog@
       // IF pc_EMM_Information AND pc_LocalTimeZone
       // Check: Does the UE assume that this time zone applies to the tracking area of the current cell and
       // is presented to the MS user at the earliest opportunity?
       if (pc_EMM_Information and pc_LocalTimeZone)
       {
          v_ReturnedString := f_UT_ChkTimeZone (UT);
          if (v_ReturnedString == "10/04/12 13:38:52+01") {
            f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3a3");
          }
       }

       //@siclog "Step 3a4" siclog@
       // IF pc_EMM_Information AND pc_UniversalAndLocalTimeZone
       // Check: Does the UE assume that this time zones applies to the tracking area of the current cell and
       // is presented to the MS user at the earliest opportunity?
       if (pc_EMM_Information and pc_UniversalAndLocalTimeZone)
       {
          v_ReturnedString := f_UT_ChkTimeZone (UT);
          if (v_ReturnedString == "10/04/12 13:38:52+01") {
            f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 3a3");
          }
       }

    }// end function fl_TC_9_1_5_1_Body

  function f_TC_9_1_5_2_EUTRA() runs on EUTRA_PTC
  { /* EMM information procedure not supported by the UE
     *
     * (1)
     *  with { UE in EMM-REGISTERED state }
     *  ensure that {
     *    when { UE receives an EMM Information message }
     *      then { UE ignore the contents of the message and return an EMM STATUS message with cause #97 "message type non-existent or not implemented" }
     *       }

     * Pre-test conditions
     * -------------------
     * System Simulator:
     *   - Cell A
     * Preamble:
     *  -   UE is in state Generic RB established (state 3) on Cell A according to [18].

     */

    f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN, LTEsinglecell);

    // create cell and send out system information
    f_EUTRA_CellConfig_Def (eutra_CellA);

    // Preamble:
    //   - UE is in state Generic RB established (state 3) on Cell A
    // ==========================================================================
    f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
    f_EUTRA_RbEst_Def(eutra_CellA);
    f_EUTRA_TestBody_Set(true);

    fl_TC_9_1_5_2_Body();

    // Postamble
    f_EUTRA_TestBody_Set(false);
    f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);
  } // function f_TC_9_1_5_2_EUTRA

  function fl_TC_9_1_5_2_Body() runs on EUTRA_PTC
  {
    // ==========================================================================
    // initialize constant test case data structures
    // ==========================================================================
    var template  EMM_Cause v_EMMCause:=cs_EMM_Cause_v('01100001'B);

    //+   The SS transmits an EMM INFORMATION message.
    //@siclog "Step 1" siclog@
     SRB.send(cas_SRB2_NasPdu_REQ(
                                 eutra_CellA,
                                 cs_TimingInfo_Now,
                                 cs_NAS_Request(tsc_SHT_IntegrityProtected_Ciphered,
                                                cs_508_EMMInformation(omit ,
                                                                  omit,
                                                                  omit,
                                                                  omit,
                                                                  cs_DaylightSavingTime_tlv('00'B)))));

    //+   Check: Does the UE transmit an EMM STATUS message with cause #97 "message type non-existent or not implemented".  (1,P)
    //@siclog "Step 2" siclog@
    SRB.receive (car_SRB2_NasPdu_IND(eutra_CellA,
                                   cr_NAS_Indication(tsc_SHT_IntegrityProtected_Ciphered,
                                                     cr_508_EMMStatus(v_EMMCause))));
    f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");
  } // function fl_TC_9_1_5_2_Body

  } // group TestcaseImplementations

} // module NAS_SecurityMode
