/******************************************************************************/
// @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 12:03:23 +0200 (Wed, 15 Sep 2010) $
// $Rev: 3890 $
/******************************************************************************/

module EUTRA_IRATFunctions {
  
//  import from EUTRA_RRC_ASN1_Definitions language "ASN.1:1997" all;
  import from EUTRA_CommonDefs all;
  import from CommonDefs all;
//  import from Parameters all;
  import from EUTRA_Component all;
  import from NAS_AuxiliaryDefsAndFunctions all;
  import from CommonIratDefs all;
  import from EUTRA_SecuritySteps all;
  import from EUTRA_SecurityFunctions all;
  import from EUTRA_NASSteps all;
  import from EUTRA_CommonProcedures all;

  
  //----------------------------------------------------------------------------
  function f_EUTRA_SendAuthCoOrdParameters (IRAT_CO_ORD_PORT p_Port, CellId_Type p_CellId, Common_AuthenticationParams_Type p_AuthParams) runs on EUTRA_PTC
  {
    var GutiParameters_Type v_Guti_Params := f_EUTRA_CellInfo_GetGuti(p_CellId);

    p_Port.send (cms_IRAT_AuthInfo(p_AuthParams,
                                   p_AuthParams,
                                   cs_IRAT_GUTIStuff (v_Guti_Params.MME_GroupId, v_Guti_Params.MME_Code, v_Guti_Params.M_TMSI)));
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      Function to send the authentication parameters to UTRAN or GERAN in case they are updated in the test case.
   * @param     p_CellId
   * @param     p_Port
   * @status
   */
  function f_EUTRA_SendAuthParameters (CellId_Type p_CellId,
                                       IRAT_CO_ORD_PORT p_Port ) runs on EUTRA_PTC
  {
    var EUTRA_SecurityParams_Type v_SecurityParams;
    var Common_AuthenticationParams_Type v_CommonAuthParams;
    var B256_Type v_CkIk;
    var B128_Type v_CK, v_IK;
    var O4_Type v_DL_NAS_Count := f_EUTRA_SS_NAS_CountGetDL();
    
    //Get latest security params...
    v_SecurityParams := f_EUTRA_Security_Get();
    //...and use them to update CK and IK
    v_CkIk := f_EUTRA_Authentication_S16(v_SecurityParams, v_DL_NAS_Count);
                                       
    v_CK := substr(v_CkIk, 0, 128);
    v_IK := substr(v_CkIk, 128, 128);

    v_CommonAuthParams := v_SecurityParams.AuthParams;
    v_CommonAuthParams.CK := v_CK;
    v_CommonAuthParams.IK := v_IK;
    
    //Now send these over to the other RAT, but DON'T store them
    f_EUTRA_SendAuthCoOrdParameters (p_Port, p_CellId, v_CommonAuthParams);
  }

  //----------------------------------------------------------------------------
  /*
   * @desc      to be used in the test case preamble for ALL Inter-RAT tests that start in EUTRA
   * @param     p_CellId
   * @param     p_State
   * @param     p_ForcedAttach (optional)
   * @status
   */
  function f_EUTRA_Preamble_InterRAT (CellId_Type p_CellId,
                             IDLEUPDATED_STATE_Type p_State,
                             IRAT_CO_ORD_PORT p_Port,
                             EUTRA_ATTACH_Type p_ForcedAttach := NORMAL) runs on EUTRA_PTC
  {
    // Do normal preamble
    f_EUTRA_Preamble (p_CellId, p_State, p_ForcedAttach);
    
    // then get latest security params and send them over
    f_EUTRA_SendAuthParameters (p_CellId, p_Port);
  }
  //----------------------------------------------------------------------------
   /* @desc      To be called after EUTRA_Init, before the test case body for every test that doesn't start in EUTRA.
   *             This will wait until Preamble is finished on the other RAT and it has received the AuthParams
   *             These are then stored in SecurityParams
   */
  function f_EUTRA_InterRAT_InitialiseAuthParams (IRAT_CO_ORD_PORT p_Port) runs on EUTRA_PTC
{
    var IRAT_Coordination_MSG v_RxdMsg;
    var EUTRA_SecurityParams_Type v_SecurityParams := f_EUTRA_Security_Get();

    p_Port.receive (cmr_IRAT_AuthInfo) -> value v_RxdMsg;
    
    v_SecurityParams.AuthParams := v_RxdMsg.Authentication.AuthPS;
    f_EUTRA_Security_Set (v_SecurityParams);
}

}
