//*****************************************************************************
/// \brief
///      RRLP PDU delivery in.
/// \details
///      Delivers a RRLP PDU from an external entity.
///      <p> This function is called in response to data being sent over an
///      RRLP Control Plane connection.
///      <p> For the RRLP PDU delivery the Host software provides a
///      Handle which is used in subsequent exchanges to tie up the RRLP
///      sequence.
///      <ul>
///         <li><var>#GN_SUCCESS \copydoc GN_SUCCESS</var>\n
///            p_Status should be set to this value when the return value
///            indicated success.</li>
///         <li><var>#GN_ERR_NO_RESOURCE \copydoc GN_ERR_NO_RESOURCE</var>\n
///            p_Status should be set to this value when there are not
///            enough resources available to the library to perform the
///            requested function.</li>
///      </ul>
/// \attention
///      The calling party is responsible for managing the memory referenced by
///      p_Status and p_PDU.
/// \returns
///      Flag to indicate success or failure of the posting of the request.
/// \retval #TRUE Flag indicating success.
/// \retval #FALSE Flag indicating failure and that the status should be checked.
BL GN_RRLP_PDU_Delivery_In(
   void        *Handle,       ///< [in] Opaque Handle used to coordinate requests.
   U2          PDU_Size,      ///< [in] Size of data at p_PDU in bytes.
   U1          *p_PDU,        ///< [in] Pointer to data.
   U4          PDU_TimeStamp  ///< [in] Timestamp of reception of PDU.
)
{
   s_RRLP_Instance   *p_RRLP_Instance;

   GN_RRLP_Log( "+GN_RRLP_PDU_Delivery_In:" );
   GN_RRLP_Log( "GN_RRLP_api_call:    RRLP<=SUPL [label=\"GN_RRLP_PDU_Delivery_In: Handle=%p\"];", Handle );
   p_RRLP_Instance = RRLP_Instance_From_Handle( Handle );
   if(p_RRLP_Instance != NULL)
   {
        s_PDU_Encoded     *p_POS_Payload    = GN_Calloc( 1, sizeof( s_PDU_Encoded ) );

        p_POS_Payload->Length = PDU_Size;
        p_POS_Payload->p_PDU_Data = GN_Calloc( 1, PDU_Size );

        memcpy( p_POS_Payload->p_PDU_Data, p_PDU, PDU_Size );

        RRLP_Send_PDU_Delivery( p_RRLP_Instance,
                               NULL,
                               p_RRLP_Instance->Handle,
                               p_POS_Payload,
                               PDU_TimeStamp,
                               NULL );
           GN_RRLP_Log( "GN_RRLP_api_call: PDU Delivery Successful" );

   }

   GN_RRLP_Log( "GN_RRLP_PDU_Delivery_In-:" );
   return TRUE;
}
//*****************************************************************************
/// \brief
///      RRLP Sequence Start in.
/// \details
///      Delivers the first RRLP PDU in the sequence from an external entity.
///      <p> This function is called in response to data being sent over an
///      RRLP Control Plane connection.
///      <p> For the RRLP Sequence Start the RRLP subsystem provides a
///      Handle which is used in subsequent exchanges to tie up the RRLP
///      sequence.
///      <ul>
///         <li><var>#GN_SUCCESS \copydoc GN_SUCCESS</var>\n
///            p_Status should be set to this value when the return value
///            indicated success.</li>
///         <li><var>#GN_ERR_NO_RESOURCE \copydoc GN_ERR_NO_RESOURCE</var>\n
///            p_Status should be set to this value when there are not
///            enough resources available to the library to perform the
///            requested function.</li>
///      </ul>
/// \attention
///      The calling party is responsible for managing the memory referenced by
///      p_Status and p_PDU.
/// \returns
///      Flag to indicate success or failure of the posting of the request.
/// \retval #TRUE Flag indicating success.
/// \retval #FALSE Flag indicating failure and that the status should be checked.
BL GN_RRLP_Sequence_Start_In(
   void           **p_Handle,    ///< [out] Opaque Handle used to coordinate requests.
   U2             PDU_Size,      ///< [in] Size of data at p_PDU in bytes.
   U1             *p_PDU,        ///< [in] Pointer to data.
   U4             PDU_TimeStamp, ///< [in] Timestamp of reception of PDU.
   s_GN_RRLP_QoP  *p_GN_RRLP_QoP, ///< [in] QoP if available.
   BL              LoggingDisabled ///< [in] This is used to enable/disable logging from the RRLP module
)
{
   s_RRLP_Instance   *p_RRLP_Instance;
   s_PDU_Encoded     *p_POS_Payload    = GN_Calloc( 1, sizeof( s_PDU_Encoded ) );
   s_GN_RRLP_QoP     *p_Temp_Qop;

   GN_RRLP_Log( "+GN_RRLP_Sequence_Start_In:" );
   GN_RRLP_Log( "GN_RRLP_api_call:    RRLP<=SUPL [label=\"GN_RRLP_Sequence_Start_In:\"];" );
   p_RRLP_Instance                  = RRLP_Instance_Request_New();
   *p_Handle                        = p_RRLP_Instance->Handle;
   p_RRLP_Instance->p_RRLP->State   = state_RRLP_Transaction_In_Progress;
   p_RRLP_Instance->p_RRLP->LoggingDisabled = LoggingDisabled;

   // POS_Handle not set, this is the first in the sequence.
   if ( p_GN_RRLP_QoP != NULL )
   {
      p_Temp_Qop = GN_Calloc( 1, sizeof( s_GN_RRLP_QoP ) );
      memcpy( p_Temp_Qop, p_GN_RRLP_QoP, sizeof( s_GN_RRLP_QoP ) );
   }
   else
   {
      p_Temp_Qop = NULL;
   }

   p_POS_Payload->Length      = PDU_Size;
   p_POS_Payload->p_PDU_Data  = GN_Calloc( 1, PDU_Size );

   memcpy( p_POS_Payload->p_PDU_Data, p_PDU, PDU_Size );

   RRLP_Send_PDU_Delivery( p_RRLP_Instance,
                           NULL,
                           p_RRLP_Instance->Handle,
                           p_POS_Payload,
                           PDU_TimeStamp,
                           p_Temp_Qop );
   if ( p_Temp_Qop != NULL )
   {
      GN_Free( p_Temp_Qop );
   }
   GN_RRLP_Log( "GN_RRLP_api_call:    RRLP>>SUPL [label=\"GN_RRLP_Sequence_Start_In: Handle=%p\"];", p_RRLP_Instance->Handle );
   GN_RRLP_Log( "-GN_RRLP_Sequence_Start_In:" );
   return TRUE;
}
//*****************************************************************************
/// \brief
///      Function to log a message in a form for parsing by mscgen.
//*****************************************************************************
void RRLP_MSC_Log_Message
(
   s_RRLP_Message  *p_Message  ///< [in] Pointer to Message to be logged.
)
{
   char  *MsgSrcName = "",
         *MsgDestName = "",
         *MsgEventName = "";
   I4 MsgSrcId, MsgDestId;
   s_RRLP_Instance *p_srcInst  = (void *) -1;
   s_RRLP_Instance *p_destInst = (void *) -1;

   MsgDestName = "RRLP_HandlerPeer";
   p_destInst = RRLP_Instance_Get_Data( p_Message->MsgHdr.p_InstanceOfDest );
   if ( p_destInst != NULL )
   {
       MsgDestId=p_destInst->ThisInstanceId;
   }
   else
   {
       // Destination Id no longer in use.
       MsgDestId = -1;
   }

   MsgSrcName = "RRLP_HandlerPeer";
   if ( p_Message->MsgHdr.p_InstanceOfSrc == NULL )
   {
      MsgSrcId = 0;
   }
   else
   {
      p_srcInst = RRLP_Instance_Get_Data( p_Message->MsgHdr.p_InstanceOfSrc );
      if ( p_srcInst != NULL )
      {
          MsgSrcId = p_srcInst->ThisInstanceId;
      }
      else
      {
          // Destination Id no longer in use.
          MsgSrcId = -1;
      }
   }

   MsgEventName = RRLP_Event_Text[p_Message->Payload.MsgEvent];
   GN_RRLP_Log( "Event:    %s->%s [label=\"%s SrcId=%d, DestId=%d, Handle=%p\"];",
      MsgSrcName, MsgDestName, MsgEventName, MsgSrcId, MsgDestId, p_Message->MsgHdr.Handle );
}