// ***************************************************************************
// @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-08-19 01:48:46 +0200 (Thu, 19 Aug 2010) $
// * $Rev: 3729 $
// ***************************************************************************

module SMS_Testcases {

import from CommonDefs all;
import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
import from EUTRA_Component all;
import from EUTRA_CommonDefs all;
import from EUTRA_ConfigurationSteps all;
import from EUTRA_RRC_Templates all;
import from EUTRA_AspCommon_Templates all;
import from EUTRA_SecurityFunctions 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 EPS_NAS_MsgContainers all;
import from EUTRA_NASSteps all;
import from UpperTesterFunctions all;
import from EUTRA_Paging all;
import from Parameters all;
import from SMS_Templates all;
import from SMS_TypeDefs all;
import from MTC_UpperTester all;
import from EUTRA_ASP_SrbDefs all;
import from EUTRA_RRCSteps all;
import from EUTRA_SecuritySteps all;
import from EUTRA_CellInfo all;
import from EUTRA_SRB_Templates all;

group LocalConstants {
} // group LocalConstants

group LocalFunctions {

function f_SMS_PDU_Encvalue(template (value) CP_PDU_Type p_CP_PDU) return octetstring {
  var bitstring v_Bitstring := encvalue(p_CP_PDU);
  var octetstring v_Octetstring := bit2oct(f_OctetAlignedBitString(v_Bitstring));
  return v_Octetstring;
};

function f_SMS_PDU_Decvalue(inout bitstring p_EncodedSMS_Message,
                            out CP_PDU_Type p_CP_PDU) return integer {
  // wrapper function for decvalue
  return decvalue(p_EncodedSMS_Message, p_CP_PDU);
};

// AT command functions
function f_SMS_AT_CSMS(charstring p_MsgService) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_SelectMessageService(UT, p_MsgService);
  if(f_CheckStringStartWith(v_ResultStr, "<CR><LF>+CSMS:") == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CPMS(charstring p_Mem1, charstring p_Mem2, charstring p_Mem3) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_SetPreferredMessageStorage(UT, p_Mem1, p_Mem2, p_Mem3);
  if(f_CheckStringStartWith(v_ResultStr, "<CR><LF>+CPMS:") == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CMGF(charstring p_Mode) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_SetMessageFormat(UT, p_Mode);
  if(f_CheckStringStartWith(v_ResultStr, tsc_AT_ResultOK) == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CSCS(charstring p_Chset) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_SelectCharacterSet(UT, p_Chset);
  if(f_CheckStringStartWith(v_ResultStr, tsc_AT_ResultOK) == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CMGD_ALL() runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_DeleteMessageFromStorage_DELFLAG(UT, 1, 4);
  if(f_CheckStringStartWith(v_ResultStr, tsc_AT_ResultOK) == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CSCA(charstring p_Sca, integer p_Tosca) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_ServiceCentreAddress(UT, p_Sca, p_Tosca);
  if(f_CheckStringStartWith(v_ResultStr, tsc_AT_ResultOK) == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CMGW(charstring p_Da,
                       integer p_Toda,
                       charstring p_Text) runs on EUTRA_PTC return integer
{
  var charstring v_ResultStr := "";
  var charstring v_SubStr:= "";

  v_ResultStr := f_UT_WriteMessageToMemoryText(UT, p_Da, int2str(p_Toda), p_Text);
  if(f_CheckStringStartWith(v_ResultStr, "<CR><LF>+CMGW:") == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
  
  if(lengthof(v_ResultStr) == 24) // Length of "<CR><LF>+CMGW: ?<CR><LF>" is 24
  {
    v_SubStr := substr(v_ResultStr, 15, 1);
  }
  else if(lengthof(v_ResultStr) == 25) // Length of "<CR><LF>+CMGW: ??<CR><LF>" is 25
  {
    v_SubStr := substr(v_ResultStr, 15, 2);
  }
  else
  {
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The index length is larger than expected.");
  }

  return str2int(v_SubStr);
};

function f_SMS_AT_CMSS(integer p_Index) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_SendStoredOneMessage(UT, p_Index);
  if(f_CheckStringStartWith(v_ResultStr, "<CR><LF>+CMSS:") == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_AT_CSMP(integer p_Fo) runs on EUTRA_PTC {
  var charstring v_ResultStr := "";

  v_ResultStr := f_UT_SetTextModeParameters(UT, p_Fo);
  if(f_CheckStringStartWith(v_ResultStr, tsc_AT_ResultOK) == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The AT CMD results in an error.");
  }
};

function f_SMS_MMI_ChkMsgDisplayed(integer p_Length, charstring p_Msg) runs on EUTRA_PTC {
  var boolean v_Result;

  v_Result := f_UT_ChkMsgDisplayed(UT, p_Length, p_Msg);
  if(v_Result == false)
  {
     f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "The MMI CMD results in an error.");
  }
};

} // group LocalFunctions

group TestcaseImplementations {

function f_TC_11_1_1_EUTRA() runs on EUTRA_PTC
{
  // @desc MT-SMS over SGs / idle mode

  // Purpose
  // -------
  // (1)
  // with { UE is IMSI attached for non-EPS services and UE has received a paging request with CN domain indicator set to "PS" in EMM-IDLE mode and UE has sent a SERVICE REQUEST message}
  // ensure that {
  // when { UE receives a CP-DATA containing an RP-DATA RPDU (SMS DELIVER TPDU) encapsulated in a Downlink NAS transport message }
  // then { UE sends a CP-ACK encapsulated in an Uplink NAS transport message followed by a CP-DATA containing an RP-ACK RPDU encapsulated in an Uplink NAS transport message}
  // }

  // Pre-test conditions
  // -------------------
  // System Simulator:
  // -  Cell A is configured according to Table 6.3.2.2-1 in [18] // TBD replace [18].
  // -  Cell A belongs TAI-1.
  // -  Cell A is set to the ''Serving cell''.
  // UE:
  // -  The UE does not have any stored SMS message.
  // Preamble:
  // -  UE is in state Registered, Idle Mode (state 2) on Cell A

  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN,LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def (eutra_CellA);
  // -  UE is in state Registered, Idle Mode (state 2) on Cell A
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  // Set message service as indicated in the PIXIT.
  f_SMS_AT_CSMS(px_SMS_Service);
  // Set Preferrred memory topreferred memories as indicated in the PIXIT.
  f_SMS_AT_CPMS(px_SMS_PrefMem1, px_SMS_PrefMem2, px_SMS_PrefMem3);
  // Set message format to text mode
  f_SMS_AT_CMGF("1");
  // Set Character Set "GSM"
  f_SMS_AT_CSCS("GSM");
  // Set the UE to delete all messages from Preferred message store
  f_SMS_AT_CMGD_ALL();

  f_EUTRA_TestBody_Set(true);

  fl_TC_11_1_1_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);

} // f_TC_11_1_1_EUTRA

function fl_TC_11_1_1_Body() runs on EUTRA_PTC
{
  // get current security parameters
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  //
  var SRB_COMMON_IND v_ReceivedAsp;
  var NAS_MSG_Indication_Type v_NasInd;
  var NAS_MessageContainer v_NAS_RcvMsgContainer_SMS;
  var CP_PDU_Type v_RcvSMS_CP_PDU;
  var template CP_PDU_Type v_ExpSMS_CP_PDU;
  var integer v_DecodeResult := 0;
  var bitstring v_NAS_RcvBitsMsgContainer_SMS;

  var O4_Type v_NasCountUL;
  var template (value) EUTRA_FDD_TDD_CellInfo_Type  v_EUTRA_FDD_TDD_Info;
  var template (value) PhysicalConfigDedicated_AntennaInfo_Type  v_AntennaInfo;

  //@siclog "Step 1" siclog@
  //The SS pages the UE using S-TMSI with CN domain indicator set to 'PS'.
  f_EUTRA_UE_Page(eutra_CellA,
                  cs_NasPaging_OneRecord(cs_PagingUE_Identity_S_TMSI(v_GutiParams.MME_Code,
                                                                     v_GutiParams.M_TMSI)));

  //@siclog "Step 2" siclog@
  //The UE transmits a SERVICE REQUEST message.
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_mt_Access,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));

  v_NasCountUL := v_NasInd.SecurityProtection.NasCount;
  v_SecurityParams := f_EUTRA_RRC_ActivateSecurity(eutra_CellA, v_SecurityParams, v_NasCountUL); // RRC SECURITY COMMAND & COMPLETE
  f_EUTRA_Security_Set ( v_SecurityParams );//Saving security parameters

  // RRC Connection Reconfiguration to configure DRB
  v_EUTRA_FDD_TDD_Info:=f_EUTRA_CellInfo_GetFDD_TDD_Info (eutra_CellA);
  v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (eutra_CellA);
  f_EUTRA_RRCConnectionReconfiguration_RadioResConfig(eutra_CellA,
                                                      tsc_RRC_TI_Def,
                                                      cs_SRB2_1AM_DRB_Config_Def(v_EUTRA_FDD_TDD_Info.cqi_ReportConfig,
                                                                                 v_EUTRA_FDD_TDD_Info.soundingRS_UL_Config,
                                                                                 v_AntennaInfo));

  //@siclog "Step 3" siclog@
  // The SS transmits a CP-DATA containing an RP-DATA RPDU (SMS DELIVER TPDU)
  // encapsulated in a Downlink NAS transport message to the UE.
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_DATA_PDU (
                                        cs_MT_TI0_FromSS,
                                        cs_CP_UserData_DELIVER('7788996655'O))))))));

  //@siclog "Step 4" siclog@
  //Check: Does the UE transmit a CP-ACK encapsulated in an Uplink NAS transport message?  // Store procedure transaction id to be used in SS responses.
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?) )))) -> value v_ReceivedAsp;
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "SMS_CP_PDU cannot be decoded in Step 4");
  }
  v_ExpSMS_CP_PDU := cr_CP_ACK_PDU (cr_MT_TI0_FromUE);
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched in Step 4");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test Case 11.1.1 Step 4");

  //@siclog "Step 5" siclog@
  //Check: Does the UE transmit a CP-DATA containing an RP-ACK RPDU encapsulated in an Uplink NAS transport message?
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  // Store procedure transaction id to be used in SS responses.
  // needs to be decoded with Decvalue !!
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "SMS_CP_PDU cannot be decoded in Step 5");
  }
  v_ExpSMS_CP_PDU := cr_CP_DATA_PDU (cr_MT_TI0_FromUE, cr_CP_UserData_DELIVER_REPORT);
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched Step 5");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test Case 11.1.1 Step 5");

  //@siclog "Step 6" siclog@
  //The SS transmits a CP-ACK encapsulated in a Downlink NAS transport message to the UE.
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_ACK_PDU (
                                        cs_MT_TI0_FromSS)))))));

  //@siclog "Step 7" siclog@
  //Make the operator check an MT Short Message received.
  if(px_SMS_ChkMsgReceived == true)
  {
    f_SMS_MMI_ChkMsgDisplayed(160, tsc_Fox);
  }

} // function fl_TC_11_1_1_Body

function f_TC_11_1_2_EUTRA() runs on EUTRA_PTC
{
  // @desc MT-SMS over SGs / active mode

  // Purpose
  // -------
  // (1)
  // with { UE is IMSI attached for non-EPS services and in EMM-CONNECTED mode }
  // ensure that {
  // when { UE receives a CP-DATA containing an RP-DATA RPDU (SMS DELIVER TPDU) encapsulated in a Downlink NAS transport message }
  // then { UE sends a CP-ACK encapsulated in an Uplink NAS transport message followed by a CP-DATA containing an RP-ACK RPDU encapsulated in an Uplink NAS transport message}
  // }

  // Pre-test conditions
  // -------------------
  // System Simulator:
  // -  Cell A is configured according to Table 6.3.2.2-1 in [18] // TBD replace [18].
  // -  Cell A belongs TAI-1.
  // -  Cell A is set to the ''Serving cell''.
  // UE:
  // -  The UE does not have any stored SMS message.
  // Preamble:
  // -  UE is in state Generic RB Established (state 3) on Cell A

  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN,LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def (eutra_CellA);
  // -  UE is in state Registered, Idle Mode (state 2) on Cell A
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  //entering UE test state 3
  f_EUTRA_RbEst_Def ( eutra_CellA );

  // Set message service as indicated in the PIXIT.
  f_SMS_AT_CSMS(px_SMS_Service);
  // Set Preferrred memory topreferred memories as indicated in the PIXIT.
  f_SMS_AT_CPMS(px_SMS_PrefMem1, px_SMS_PrefMem2, px_SMS_PrefMem3);
  // Set message format to text mode
  f_SMS_AT_CMGF("1");
  // Set Character Set "GSM"
  f_SMS_AT_CSCS("GSM");
  // Set the UE to delete all messages from Preferred message store
  f_SMS_AT_CMGD_ALL();

  f_EUTRA_TestBody_Set(true);

  fl_TC_11_1_2_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);

} // f_TC_11_1_2_EUTRA

function fl_TC_11_1_2_Body() runs on EUTRA_PTC
{
  // get current security parameters
  // var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  // var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  
  var SRB_COMMON_IND v_ReceivedAsp;
  var NAS_MessageContainer v_NAS_RcvMsgContainer_SMS;
  var CP_PDU_Type v_RcvSMS_CP_PDU;
  var template CP_PDU_Type v_ExpSMS_CP_PDU;
  var integer v_DecodeResult := 0;
  var bitstring v_NAS_RcvBitsMsgContainer_SMS;

  //@siclog "Step 1" siclog@
  //The SS transmits a CP-DATA containing an RP-DATA RPDU (SMS DELIVER TPDU)
  // encapsulated in a Downlink NAS transport message to the UE.
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_DATA_PDU (
                                        cs_MT_TI0_FromSS,
                                        cs_CP_UserData_DELIVER('4433226655'O))))))));

  //@siclog "Step 2" siclog@
  //Check: Does the UE transmit a CP-ACK encapsulated in an Uplink NAS transport message?  // Store procedure transaction id to be used in SS responses.
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "Test Case 11.1.2 SMS_CP_PDU cannot be decoded in Step 2");
  }
  v_ExpSMS_CP_PDU := cr_CP_ACK_PDU (cr_MT_TI0_FromUE);
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "Test Case 11.1.2 SMS_CP_PDU cannot be matched in Step 2");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Step 2");

  //@siclog "Step 3" siclog@
  //Check: Does the UE transmit a CP-DATA containing an RP-ACK RPDU encapsulated in an Uplink NAS transport message?
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  // Store procedure transaction id to be used in SS responses.
  // needs to be decoded with Decvalue !!
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "Test Case 11.1.2 SMS_CP_PDU cannot be decoded in Step 3");
  }
  v_ExpSMS_CP_PDU := cr_CP_DATA_PDU (cr_MT_TI0_FromUE, cr_CP_UserData_DELIVER_REPORT);
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched Step 3");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test Case 11.1.2 Step 3");

  //@siclog "Step 4" siclog@
  //The SS transmits a CP-ACK encapsulated in a Downlink NAS transport message to the UE.
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_ACK_PDU (
                                        cs_MT_TI0_FromSS)))))));

  //Make the operator check an MT Short Message received.
  if(px_SMS_ChkMsgReceived == true)
  {
    f_SMS_MMI_ChkMsgDisplayed(160, tsc_Fox);
  }

} // function fl_TC_11_1_2_Body

function f_TC_11_1_3_EUTRA() runs on EUTRA_PTC
{
  // @desc MO-SMS over SGs / idle modeMO-SMS over SGs in Idle Mode

  // Purpose
  // -------
  // (1)
  // with { UE is IMSI attached for non-EPS services and in EMM-IDLE mode }
  // ensure that {
  // when { a MO SMS is initiated at the UE }
  // then { UE sends an SERVICE REQUEST message followed by a CP-DATA containing an RP-DATA
  //        (SMS DELIVER TPDU) encapsulated in an Uplink NAS transport message }
  // }
  // (2)
  // with { UE is IMSI attached for non-EPS services and UE has sent an SERVICE REQUEST message triggered by MO SMS
  //        followed by a CP-DATA containing an RP-DATA (SMS DELIVER TPDU) encapsulated in an Uplink NAS transport message }
  // ensure that {
  // when { UE receives a CP-DATA containing an RP-ACK RPDU encapsulated in a Downlink NAS transport message }
  // then { UE sends a CP-ACK encapsulated in an Uplink NAS Transport message }
  // }

  // Pre-test conditions
  // -------------------
  // System Simulator:
  // -  Cell A is configured according to Table 6.3.2.2-1 in [18] // TBD replace [18].
  // -  Cell A belongs TAI-1.
  // -  Cell A is set to the ''Serving cell''.
  // UE:
  // -  The UE does not have any stored SMS message.
  // Preamble:
  // -  UE is in state Registered, Idle Mode (state 2) on Cell A

  f_EUTRA_NAS_Init(LTE_AllCellsOnSamePLMN,LTEsinglecell);

  // create cell and send out system information
  f_EUTRA_CellConfig_Def (eutra_CellA);
  // -  UE is in state Registered, Idle Mode (state 2) on Cell A
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  // Set message service as indicated in the PIXIT.
  f_SMS_AT_CSMS(px_SMS_Service);
  // Set Preferrred memory topreferred memories as indicated in the PIXIT.
  f_SMS_AT_CPMS(px_SMS_PrefMem1, px_SMS_PrefMem2, px_SMS_PrefMem3);
  // Set message format to text mode
  f_SMS_AT_CMGF("1");
  // Set Character Set "GSM"
  f_SMS_AT_CSCS("GSM");
  // Set the UE to delete all messages from Preferred message store
  f_SMS_AT_CMGD_ALL();
  // Set service center address
  f_SMS_AT_CSCA("""2222222222""", 129);

  f_EUTRA_TestBody_Set(true);

  fl_TC_11_1_3_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);

} // f_TC_11_1_3_EUTRA

function fl_TC_11_1_3_Body() runs on EUTRA_PTC
{
  // get current security parameters
  var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  // var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  
  var SRB_COMMON_IND v_ReceivedAsp;
  var NAS_MSG_Indication_Type v_NasInd;
  var NAS_MessageContainer v_NAS_RcvMsgContainer_SMS;
  var CP_PDU_Type v_RcvSMS_CP_PDU;
  var template CP_PDU_Type v_ExpSMS_CP_PDU;
  var integer v_DecodeResult := 0;
  var integer v_Index := 0;
  var bitstring v_NAS_RcvBitsMsgContainer_SMS;
  // to hold MO TI values plus TIs for the duration of a transaction
  var B3_Type v_TI_Value;
  var TransactionIdentifier_Type v_MO_TI_FromSS, v_MO_TI_FromUE;

  var O4_Type v_NasCountUL;
  var EUTRA_SecurityParams_Type v_AuthParams := f_EUTRA_Security_Get();
  var template (value) EUTRA_FDD_TDD_CellInfo_Type  v_EUTRA_FDD_TDD_Info;
  var template (value) PhysicalConfigDedicated_AntennaInfo_Type  v_AntennaInfo;

  //@siclog "Step 1" siclog@
  // Sending of a 160 character MO SMS is initiated at the UE via MMI or  AT command
  // Set text mode parameters to trigger status report.
  // bit 7(RP) 6(UDHI) 5(SRR) 4 3(VPF) 2(RD) 1 0(MIT)
  //     0     0       1      1 0      0     0 1
  f_SMS_AT_CSMP(49);
  // Write message to memory storage, get the returened memory location.
  v_Index := f_SMS_AT_CMGW("""1111111111""", 129, tsc_Fox);
  // Send message from storage.
  f_SMS_AT_CMSS(v_Index);

  //@siclog "Step 2" siclog@
  // Check: Does the UE transmit a SERVICE REQUEST message?
  v_NasInd := f_EUTRA_RRC_ConnEst_DefWithNas(eutra_CellA,
                                             tsc_RRC_TI_Def,
                                             cr_EstablishmentCause_MoSignalling,
                                             cr_NAS_Indication(tsc_SHT_IntegrityProtected,
                                                               cr_508_SERVICE_REQUEST(v_SecurityParams.KSIasme)));

  v_NasCountUL := v_NasInd.SecurityProtection.NasCount;
  v_AuthParams := f_EUTRA_RRC_ActivateSecurity(eutra_CellA, v_AuthParams, v_NasCountUL); // RRC SECURITY COMMAND & COMPLETE
  f_EUTRA_Security_Set ( v_AuthParams );//Saving security parameters

  // RRC Connection Reconfiguration to configure DRB
  v_EUTRA_FDD_TDD_Info:=f_EUTRA_CellInfo_GetFDD_TDD_Info (eutra_CellA);
  v_AntennaInfo := f_EUTRA_CellInfo_GetAntennaInfoDedicated (eutra_CellA);
  f_EUTRA_RRCConnectionReconfiguration_RadioResConfig(eutra_CellA,
                                                      tsc_RRC_TI_Def,
                                                      cs_SRB2_1AM_DRB_Config_Def(v_EUTRA_FDD_TDD_Info.cqi_ReportConfig,
                                                                                 v_EUTRA_FDD_TDD_Info.soundingRS_UL_Config,
                                                                                 v_AntennaInfo));

  //@siclog "Step 3" siclog@
  // Check: Does the UE transmit a CP-DATA containing an RP-DATA RPDU (SMS SUBMIT TPDU) encapsulated in an Uplink NAS transport message?
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  // Store procedure transaction id to be used in SS responses.
  // needs to be decoded with Decvalue !!
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "Test case 11.1.3 SMS_CP_PDU cannot be decoded in Step 3");
  }

  // Note TI value used by the UE and set TI variables accordingly
  v_TI_Value := v_RcvSMS_CP_PDU.CP_DATA.transactionIdentifier.tI_Value;
  v_MO_TI_FromSS.tI_Flag := '1'B;
  v_MO_TI_FromSS.tI_Value := v_TI_Value;
  v_MO_TI_FromUE.tI_Flag := '0'B;
  v_MO_TI_FromUE.tI_Value := v_TI_Value;

  v_ExpSMS_CP_PDU := cr_CP_DATA_PDU (cr_MO_AnyTI_FromUE, cr_CP_UserData_SUBMIT );
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched Step 3");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test case 11.1.3 Step 3");

  //@siclog "Step 4" siclog@
  // The SS transmits a CP-ACK encapsulated in a Downlink NAS Transport message.
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_ACK_PDU (
                                        v_MO_TI_FromSS)))))));

  //@siclog "Step 5" siclog@
  // The SS transmits a CP-DATA containing an RP-ACK RPDU encapsulated in an Downlink NAS transport message
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_DATA_PDU (
                                        v_MO_TI_FromSS,
                                        cs_CP_UserData_SUBMIT_REPORT)))))));

  //@siclog "Step 6" siclog@
  // Check: Does the UE transmit a CP-ACK encapsulated in an Uplink NAS Transport message?
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  // Store procedure transaction id to be used in SS responses.
  // needs to be decoded with Decvalue !!
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "Test case 11.1.3 SMS_CP_PDU cannot be decoded in Step 6");
  }
  v_ExpSMS_CP_PDU := cr_CP_ACK_PDU (v_MO_TI_FromUE);
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched Step 6");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test case 11.1.3 Step 6");

} // function fl_TC_11_1_3_Body

function f_TC_11_1_4_EUTRA() runs on EUTRA_PTC
{
  // @desc MO-SMS over SGs / active mode

  // Purpose
  // -------
  // (1)
  // with { UE is IMSI attached for non-EPS services and in EMM-CONNECTED mode }
  // ensure that {
  // when { a MO SMS is initiated at the UE  }
  // then { UE sends a CP-DATA containing an RP-DATA RPDU (SMS SUBMIT TPDU) encapsulated in an Uplink NAS transport message  }
  // }
  // (2)
  // with { UE is IMSI attached for non-EPS services and in EMM-CONNECTED mode and UE has sent a CP-DATA containing an RP-DATA RPDU (SMS SUBMIT TPDU) encapsulated in an Uplink NAS transport message }
  // ensure that {
  // when { UE receives a CP-DATA containing an RP-ACK RPDU (SMS SUBMIT REPORT TPDU) encapsulated in a Downlink NAS transport message }
  // then { UE sends a CP-ACK encapsulated in an Uplink NAS Transport message }
  // }

  // Pre-test conditions
  // -------------------
  // System Simulator:
  // -  Cell A is configured according to Table 6.3.2.2-1 in [18] // TBD replace [18].
  // -  Cell A belongs TAI-1.
  // -  Cell A is set to the ''Serving cell''.
  // UE:
  // -  The UE does not have any stored SMS message.
  // 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);
  // -  UE is in state Registered, Idle Mode (state 2) on Cell A
  f_EUTRA_Preamble(eutra_CellA, STATE2_IDLEUPDATE);
  //entering UE test state 3
  f_EUTRA_RbEst_Def ( eutra_CellA );

  // Set message service as indicated in the PIXIT.
  f_SMS_AT_CSMS(px_SMS_Service);
  // Set Preferrred memory topreferred memories as indicated in the PIXIT.
  f_SMS_AT_CPMS(px_SMS_PrefMem1, px_SMS_PrefMem2, px_SMS_PrefMem3);
  // Set message format to text mode
  f_SMS_AT_CMGF("1");
  // Set Character Set "GSM"
  f_SMS_AT_CSCS("GSM");
  // Set the UE to delete all messages from Preferred message store
  f_SMS_AT_CMGD_ALL();
  // Set service center address
  f_SMS_AT_CSCA("""2222222222""", 129);

  f_EUTRA_TestBody_Set(true);

  fl_TC_11_1_4_Body();

  // Postamble
  f_EUTRA_TestBody_Set(false);
  f_EUTRA_Postamble(eutra_CellA, E2_CONNECTED);

} // f_TC_11_1_4_EUTRA

function fl_TC_11_1_4_Body() runs on EUTRA_PTC
{
  // get current security parameters
  // var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();
  // var GutiParameters_Type v_GutiParams := f_EUTRA_CellInfo_GetGuti(eutra_CellA);
  
  var SRB_COMMON_IND v_ReceivedAsp;
  var NAS_MessageContainer v_NAS_RcvMsgContainer_SMS;
  var CP_PDU_Type v_RcvSMS_CP_PDU;
  var template CP_PDU_Type v_ExpSMS_CP_PDU;
  var integer v_DecodeResult := 0;
  var integer v_Index := 0;
  var bitstring v_NAS_RcvBitsMsgContainer_SMS;
  // to hold MO TI values plus TIs for the duration of a transaction
  var B3_Type v_TI_Value;
  var TransactionIdentifier_Type v_MO_TI_FromSS, v_MO_TI_FromUE;

  //@siclog "Step 1" siclog@
  // Sending of a 160 character MO SMS is initiated at the UE via MMI or  AT command
  // Set text mode parameters to trigger status report.
  // bit 7(RP) 6(UDHI) 5(SRR) 4 3(VPF) 2(RD) 1 0(MIT)
  //     0     0       1      1 0      0     0 1
  f_SMS_AT_CSMP(49);
  // Write message to memory storage, get the returened memory location.
  v_Index := f_SMS_AT_CMGW("""1111111111""", 129, tsc_Fox);
  // Send message from storage.
  f_SMS_AT_CMSS(v_Index);

  //@siclog "Step 2" siclog@
  // Check: Does the UE transmit a CP-DATA containing an RP-DATA RPDU (SMS SUBMIT TPDU) encapsulated in an Uplink NAS transport message?
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  // Store procedure transaction id to be used in SS responses.
  // needs to be decoded with Decvalue !!
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "Test case 11.1.4 SMS_CP_PDU cannot be decoded in Step 2");
  }

  v_TI_Value := v_RcvSMS_CP_PDU.CP_DATA.transactionIdentifier.tI_Value;
  v_MO_TI_FromSS.tI_Flag := '1'B;
  v_MO_TI_FromSS.tI_Value := v_TI_Value;
  v_MO_TI_FromUE.tI_Flag := '0'B;
  v_MO_TI_FromUE.tI_Value := v_TI_Value;

  v_ExpSMS_CP_PDU := cr_CP_DATA_PDU (cr_MO_AnyTI_FromUE, cr_CP_UserData_SUBMIT );
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched Step 2");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test case 11.1.4 Step 2");

  //@siclog "Step 3" siclog@
  // The SS transmits a CP-ACK encapsulated in a Downlink NAS Transport message.
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_ACK_PDU (
                                        v_MO_TI_FromSS)))))));

  //@siclog "Step 4" siclog@
  // The SS transmits a CP-DATA containing an RP-ACK RPDU(SMS SUBMIT REPORT TPDU) encapsulated in an Downlink NAS transport message
  SRB.send(cas_SRB2_NasPdu_REQ(eutra_CellA,
                               cs_TimingInfo_Now,
                               cs_NAS_Request (
                                 tsc_SHT_IntegrityProtected_Ciphered,
                                 cs_SMS_DL_NAS_Transport (
                                   tsc_SHT_IntegrityProtected_Ciphered,
                                   cs_NAS_MsgContainer_SMS (
                                     f_SMS_PDU_Encvalue(
                                       cs_CP_DATA_PDU (
                                        v_MO_TI_FromSS,
                                        cs_CP_UserData_SUBMIT_REPORT )))))));

  //@siclog "Step 5" siclog@
  // Check: Does the UE transmit a CP-ACK encapsulated in an Uplink NAS Transport message?
  SRB.receive(car_SRB2_NasPdu_IND(eutra_CellA,
                                  cr_NAS_Indication(
                                    tsc_SHT_IntegrityProtected_Ciphered,
                                    cr_SMS_UL_NAS_Transport (
                                      tsc_SHT_IntegrityProtected_Ciphered,
                                      cr_NAS_MsgContainer_SMS (?))))) -> value v_ReceivedAsp;
  // Store procedure transaction id to be used in SS responses.
  // needs to be decoded with Decvalue !!
  v_NAS_RcvMsgContainer_SMS :=  v_ReceivedAsp.Signalling.Nas[0].Pdu.Msg.ul_NAS_TRANSPORT.nasMessage;
  v_NAS_RcvBitsMsgContainer_SMS := oct2bit(v_NAS_RcvMsgContainer_SMS.smsmessage);
  v_DecodeResult := f_SMS_PDU_Decvalue(v_NAS_RcvBitsMsgContainer_SMS, v_RcvSMS_CP_PDU);
  if (v_DecodeResult != 0) {
    FatalError(__FILE__, __LINE__, "Test case 11.1.4 SMS_CP_PDU cannot be decoded in Step 5");
  }
  v_ExpSMS_CP_PDU := cr_CP_ACK_PDU (v_MO_TI_FromUE);
  if ( not match ( v_RcvSMS_CP_PDU, v_ExpSMS_CP_PDU ))
  {
    //* @verdict fail Received decoded template does not match with the expected template
    f_EUTRA_SetVerdictFailOrInconc (__FILE__, __LINE__, "SMS_CP_PDU cannot be matched Step 5");
  }
  f_EUTRA_PreliminaryPass(__FILE__, __LINE__, "Test case 11.1.4 Step 5");

} // function fl_TC_11_1_4_Body

} // group TestcaseImplementations

} // module SMS_Testcases
