/* 
+----------------------------------------------------------------------------- 
|  Project :  
|  Modul   :  
+----------------------------------------------------------------------------- 
|  Copyright 2002 Texas Instruments Berlin, AG 
|                 All rights reserved. 
| 
|                 This file is confidential and a trade secret of Texas 
|                 Instruments Berlin, AG 
|                 The receipt of or possession of this file does not convey 
|                 any rights to reproduce or disclose its contents or to 
|                 manufacture, use, or sell anything it may describe, in 
|                 whole, or in part, without the specific written consent of 
|                 Texas Instruments Berlin, AG. 
+----------------------------------------------------------------------------- 
|  Purpose :  This module defines the functions which are responsible
|             for the responses of the protocol stack adapter for SNDCP.
+----------------------------------------------------------------------------- 
*/ 

#if defined (GPRS) && defined (DTI)

#ifndef CMH_SNDR_C
#define CMH_SNDR_C
#endif

#include "aci_all.h"
/*==== INCLUDES ===================================================*/

#include "aci_cmh.h"

#include "psa.h"

#include "dti.h"
#include "dti_conn_mng.h"
#include "dti_cntrl_mng.h"

#include "gaci_cmh.h"
#include "gaci.h"

#include "cmh.h"
#include "cmh_snd.h"
#include "cmh_sm.h"

#ifdef FF_GPF_TCPIP
#include "dcm_utils.h"
#include "dcm_state.h"
#include "dcm_env.h"
#endif

/*==== CONSTANTS ==================================================*/


/*==== TYPES ======================================================*/


/*==== EXPORT =====================================================*/


/*==== VARIABLES ==================================================*/
EXTERN SHORT nsapi_to_cid[SMH_NSAPI_MAX];


/*==== FUNCTIONS ==================================================*/

/*
+-------------------------------------------------------------------+
| PROJECT : GPRS (8441)      MODULE  : CMH_SNDR                      |
| STATE   : finished         ROUTINE : cmhSNDCP_Counted             |
+-------------------------------------------------------------------+

  PURPOSE : 

*/
GLOBAL void cmhSNDCP_Counted( UBYTE nsapi,
                              ULONG octets_uplink,
                              ULONG octets_downlink,
                              ULONG packets_uplink,
                              ULONG packets_downlink )
{
  UBYTE i;
  SHORT cid = nsapi_to_cid[nsapi];

  TRACE_FUNCTION ("cmhSNDCP_Counted()");

  for (i = 0; i < MAX_CID ; i++)
  {
    if (pdp_context[i].nsapi EQ nsapi AND 
       (pdp_context[i].state NEQ CS_ACTIVATED AND pdp_context[i].state NEQ CS_DATA_LINK))
    {
      cid = i+1;                                      /* Gets the deactivated CID */
      pdp_context[i].nsapi = UNUSED_NSAPI;
      break;
    } 
  }
  
  R_AT( RAT_SNCNT, sndcpShrdPrm.srcId ) ( cid,           /* issue 10417 */
                                          octets_uplink, 
                                          octets_downlink,
                                          packets_uplink,
                                          packets_downlink );
}


/*
+---------------------------------------------------------------------------+
| PROJECT : GSM-F&D (8411)              MODULE  : CMH_SNDR                  |
| STATE   : code                        ROUTINE : NULL_SNDCP_connect_dti_cb |
+---------------------------------------------------------------------------+

  PURPOSE : Callback for connection between PPP Server and SNDCP.

*/
GLOBAL BOOL NULL_SNDCP_connect_dti_cb(UBYTE dti_id, T_DTI_CONN_STATE result_type)
{
  TRACE_FUNCTION("NULL_SNDCP_connect_dti_cb");

  switch( result_type)
  {
    case DTI_CONN_STATE_DISCONNECTED:
      cmhSM_connection_down(dti_id);
      break;
    case DTI_CONN_STATE_DISCONNECTING:
    case DTI_CONN_STATE_CONNECTING:
    case DTI_CONN_STATE_CONNECTED:
      break;
    case DTI_CONN_STATE_ERROR:
      /* connection not possible: disconnect SNDCP */
      dti_cntrl_close_dpath_from_dti_id( dti_id );
      break;
    case DTI_CONN_STATE_UNKNOWN:
    default:
      TRACE_EVENT("PPP_SNDCP_connect_dti_cb call with not awaited value");
      break;
  }
  return TRUE;
}


#if defined(FF_PKTIO) OR defined(FF_TCP_IP) OR defined (FF_PSI)
/*
+---------------------------------------------------------------------------+
| PROJECT : GSM-F&D (8411)             MODULE  : CMH_SNDR                   |
| STATE   : code                       ROUTINE : PKTIO_SNDCP_connect_dti_cb |
+---------------------------------------------------------------------------+

  PURPOSE : Callback for connection between PKTIO and SNDCP.

*/
GLOBAL BOOL PKTIO_SNDCP_connect_dti_cb(UBYTE dti_id, T_DTI_CONN_STATE result_type)
{
  SHORT cid;

  TRACE_FUNCTION("PKTIO_SNDCP_connect_dti_cb");

  switch( result_type)
  {
    case DTI_CONN_STATE_DISCONNECTING:
      cid = gaci_get_cid_over_dti_id(dti_id);
      if( INVALID_CID   NEQ cid                                AND
          CS_ACTIVATING  EQ get_state_over_cid(cid)            AND
          ( DTI_ENTITY_PKTIO EQ pdp_context[cid - 1].entity_id OR
            DTI_ENTITY_AAA   EQ pdp_context[cid - 1].entity_id OR
            DTI_ENTITY_PSI   EQ pdp_context[cid - 1].entity_id    ) )
      {
        cmhSM_deactivateAContext(CMD_SRC_NONE, cid);
        set_state_over_cid( cid, CS_DEACTIVATE_NORMAL);
      }
      break;
    case DTI_CONN_STATE_DISCONNECTED:
      cmhSM_connection_down(dti_id);
      break;
    case DTI_CONN_STATE_CONNECTING:
    case DTI_CONN_STATE_CONNECTED:
      break ;
    case DTI_CONN_STATE_ERROR:
      /* connection not possible: disconnect SNDCP */
      dti_cntrl_close_dpath_from_dti_id( dti_id );
      break;
    case DTI_CONN_STATE_UNKNOWN:
    default:
      TRACE_EVENT("PKTIO_SNDCP_connect_dti_cb call with not awaited value");
      break;
  }
  return TRUE;
}
#endif /* FF_PKTIO OR FF_TCP_IP OR FF_PSI */


#ifdef CO_UDP_IP
/*
+--------------------------------------------------------------------------+
| PROJECT : GSM-F&D (8411)              MODULE  : CMH_SNDR                 |
| STATE   : code                        ROUTINE : IP_SNDCP_connect_dti_cb  |
+--------------------------------------------------------------------------+

  PURPOSE : Callback for connection between IP Server and SNDCP.

*/

GLOBAL BOOL IP_SNDCP_connect_dti_cb(UBYTE dti_id, T_DTI_CONN_STATE result_type)
{
  TRACE_FUNCTION("IP_SNDCP_connect_dti_cb");

  switch( result_type)
  {
    case DTI_CONN_STATE_DISCONNECTING:
    case DTI_CONN_STATE_DISCONNECTED:
    case DTI_CONN_STATE_CONNECTING:
    case DTI_CONN_STATE_CONNECTED:
      break;
    case DTI_CONN_STATE_ERROR:
      /* connection not possible: disconnect SNDCP */
      TRACE_EVENT("IP_SNDCP_connect_dti_cb connection not possible: disconnect SNDCP");
      dti_cntrl_close_dpath_from_dti_id( dti_id);
      break;
    case DTI_CONN_STATE_UNKNOWN:
    default:
      TRACE_EVENT("IP_SNDCP_connect_dti_cb call with not awaited value");
      break;
  }
  return TRUE;
}
#endif /* CO_UDP_IP */


#ifdef FF_GPF_TCPIP
GLOBAL BOOL TCPIP_SNDCP_connect_dti_cb(UBYTE dti_id, T_DTI_CONN_STATE result_type)
{
  TRACE_FUNCTION("TCPIP_SNDCP_connect_dti_cb");
  TRACE_EVENT_P1("TCPIP_SNDCP_connect_dti_cb() result = %d",result_type);

  switch( result_type)
  {
    case DTI_CONN_STATE_DISCONNECTING:
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb in DTI_CONN_STATE_DISCONNECTING") ;
      break ;

    case DTI_CONN_STATE_CONNECTING:
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb in DTI_CONN_STATE_CONNECTING") ;
      break;
         
    case DTI_CONN_STATE_DISCONNECTED:
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb in DTI_CONN_STATE_DISCONNECTED") ;
      dti_cntrl_erase_entry(dti_id);
      {
        T_DCM_STATUS_IND_MSG msg;
        msg.hdr.msg_id = DCM_NEXT_CMD_READY_MSG;
        dcm_send_message(msg, DCM_SUB_WAIT_CGACT_CNF);
      }
      break;
         
    case DTI_CONN_STATE_CONNECTED:
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb in DTI_CONN_STATE_CONNECTED");
      {
        T_DCM_STATUS_IND_MSG msg;
        msg.hdr.msg_id = DCM_NEXT_CMD_READY_MSG;
        dcm_send_message(msg, DCM_SUB_WAIT_CGACT_CNF);
      }
      break;
         
    case DTI_CONN_STATE_ERROR:
      /* connection not possible: disconnect SNDCP */
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb connection not possible: disconnect SNDCP");
      dti_cntrl_close_dpath_from_dti_id( dti_id);
      break;

    case DTI_CONN_STATE_UNKNOWN:
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb in DTI_CONN_STATE_UNKNOWN") ;
      break;
         
    default:
      TRACE_EVENT("TCPIP_SNDCP_connect_dti_cb call with not awaited value");
      break;
  }
  return TRUE;
}
#endif /* FF_GPF_TCPIP */

#endif /* GPRS && DTI*/
