/*++

*****************************************************************************
*                                                                           *
*  This software contains proprietary and confiential information of        *
*                                                                           *
*                    Digi International Inc.                                *
*                                                                           *
*  By accepting transfer of this copy, Recipient agrees to retain this      *
*  software in confidence, to prevent disclosure to others, and to make     *
*  no use of this software other than that for which it was delivered.      *
*  This is an unpublished copyrighted work of Digi International Inc.       *
*  Except as permitted by federal law, 17 USC 117, copying is strictly      *
*  prohibited.                                                              *
*                                                                           *
*****************************************************************************

Module Name:

   wanindic.c

Abstract:

   This module provides needed functionality for indicating line
   up and line down notifications to the WAN wrapper.

Revision History:

   $Log: /Components/Windows/NT/Sync/Wan/WANINDIC.C $
 * 
 * 5     4/28/97 1:55p Mikez
 * Updated to use DsApi library.  Uses IoBuildDeviceIoControlRequest to
 * alloc Irps causing two problems.  Because ioctls are METHOD_BUFFERED, a
 * systemBuffer is alloc'd, but, because the completionroutines return
 * STATUS_MORE_PROCESSING_REQUIRED, never gets freed.  Also, SystemBuffer
 * is not copied to OutputBuffer until after completionRoutine returns
 * STATUS_SUCCESS.  Need to move from METHOD_BUFFER or from using
 * IoBuildDeviceIoControlRequest.
 * 
 * 4     3/03/97 9:23a Mikez
 * Updated to use async completions on MakeCall, Drop and Answer TAPI OID
 * requests.
 * 
 * 3     12/06/96 2:28p Mikez
 * Modified to support new SXB paridigm of "one NT device per adapter"
 * rather than the older "one NT device per port".
 * 
 * 2     10/28/96 1:25p Mikez
 * Updated to support latest firmware release, v1.1.2b.
 * 
 * 1     3/11/96 5:07p Mikez
 * Revision 1.5  1995/10/06  07:00:18  Craig_Harrison
 * Version 1.0.1.2 
 * 
 * Revision 1.4  1995/09/15  00:00:00  Craig_Harrison
 * First beta release
 * 
 * Revision 1.2  1995/06/01  10:09:02  rik
 * Updated for various problems and enhancements
 * 
 * Revision 1.1  1995/01/23  09:25:18  rik
 * Initial revision
 * 

--*/
#include <ntddk.h>
#include <ntddndis.h>
#include <ndis.h>
#include <ndiswan.h>

#include "DsApi.h"
#include "digix25.h"
#include "digix25p.h"



VOID WanLineup( PDIGIWAN_CALL_OBJECT CallObject,
                NDIS_HANDLE EndPoint )
/*++

Routine Description:

   Generates a LINE_UP indication to the Wan wrapper.

Arguments:

   CallObject - Pointer to the call object which will be associated
                with this line up indication.

   EndPoint - Context to give the Connectection Wrapper.  If NULL, then
              we use the CallObjects TapiCallHandle.

Return Value:

    None.

--*/
{
   PDIGIWAN_ADAPTER_OBJECT AdapterObject;
   NDIS_MAC_LINE_UP WanLineUp;

   DigiDump( (DIGIFLOW|DIGIWANINFO),
             ("WanLineup: CallObject=0x%x, EndPoint=0x%x, htCall=0x%x\n",
               CallObject,
               EndPoint,
               CallObject->htCall) );

   ASSERT( CallObject->Type == DIGIWAN_TYPE_CALL_OBJECT );

   AdapterObject = CallObject->pAdapter;

   ASSERT( AdapterObject->Type == DIGIWAN_TYPE_ADAPTER_OBJECT );

   WanLineUp.LinkSpeed = CallObject->LocalLinkSpeed / 100;
   WanLineUp.Quality = NdisWanReliable;
   WanLineUp.SendWindow = AdapterObject->MaxSendWindow;

   if( EndPoint )
      WanLineUp.ConnectionWrapperID = EndPoint;
   else
      WanLineUp.ConnectionWrapperID = (NDIS_HANDLE)CallObject->htCall;

   //
   // Fill the context we would like to be called back with in
   // later calls from the Wan Wrapper.
   //
   WanLineUp.NdisLinkHandle = (NDIS_HANDLE)CallObject;

   //
   // Clear the Link Context.  The Wan wrapper will initialize.
   //
   WanLineUp.NdisLinkContext = NULL;

   NdisMIndicateStatus( AdapterObject->MiniportAdapterHandle,
                        NDIS_STATUS_WAN_LINE_UP,
                        &WanLineUp,
                        sizeof(WanLineUp) );

   NdisMIndicateStatusComplete( AdapterObject->MiniportAdapterHandle );

   CallObject->WrapperLinkHandle = WanLineUp.NdisLinkContext;

   DigiDump( (DIGIFLOW|DIGIWANINFO),
             ("WAN_LINE_UP: LinkHandle=0x%x LinkContext=0x%x WrapperID=0x%x\n",
               WanLineUp.NdisLinkHandle,
               WanLineUp.NdisLinkContext,
               WanLineUp.ConnectionWrapperID) );

	DigiDump( (DIGIWANINFO),
				 ("             LinkSpeed = %u Quality = %u SendWindow = %u\n",
				  WanLineUp.LinkSpeed,
				  WanLineUp.Quality,
				  WanLineUp.SendWindow) );

}  // end WanLineup



VOID WanLinedown( PDIGIWAN_CALL_OBJECT CallObject )
/*++

Routine Description:

   Generates a LINE_UP indication to the Wan wrapper.

Arguments:

   CallObject - Pointer to the call object which will be associated
                with this line up indication.

Return Value:

    None.

--*/
{
   PDIGIWAN_ADAPTER_OBJECT AdapterObject;
   NDIS_MAC_LINE_DOWN WanLinedown;

   DigiDump( (DIGIFLOW|DIGIWANINFO), ("WanLinedown: CallObject = 0x%x\n",
                                     CallObject) );
   ASSERT( CallObject->Type == DIGIWAN_TYPE_CALL_OBJECT );
   AdapterObject = CallObject->pAdapter;
   ASSERT( AdapterObject->Type == DIGIWAN_TYPE_ADAPTER_OBJECT );

	// If our state machine is working, this assert should never fail
	ASSERT( CallObject->WrapperLinkHandle );
	if ( CallObject->WrapperLinkHandle )
	{
	   //
	   // Give the Link handle we received on LINE_UP
	   //
	   WanLinedown.NdisLinkContext = CallObject->WrapperLinkHandle;
	   CallObject->WrapperLinkHandle = NULL;
	   NdisMIndicateStatus( AdapterObject->MiniportAdapterHandle,
	                        NDIS_STATUS_WAN_LINE_DOWN,
	                        &WanLinedown,
	                        sizeof(WanLinedown) );
	   NdisMIndicateStatusComplete( AdapterObject->MiniportAdapterHandle );
	}

   DigiDump( (DIGIFLOW|DIGIWANINFO), ("WAN_LINE_DOWN: LinkContext=0x%x\n",
                                     WanLinedown.NdisLinkContext) );
}  // end WanLinedown



VOID WanFragment( PDIGIWAN_CALL_OBJECT CallObject,
                  ULONG FragmentErrors )
/*++

Routine Description:

   Generates a NDIS_STATUS_WAN_FRAGMENT indication to the Wan wrapper.

Arguments:

   CallObject - Pointer to the call object which will be associated
                with this line up indication.

   FragmentErrors - Bitmask of errors to indicate.  Should correspond to
                    NDIS_STATUS_WAN_FRAGMENT error indications.

Return Value:

    None.

--*/
{
   PDIGIWAN_ADAPTER_OBJECT AdapterObject;
   NDIS_MAC_FRAGMENT MacFragment;

   DigiDump( (DIGIFLOW|DIGIWANINFO), ("WanFragment: CallObject=0x%x, Error=0x%x\n",
                                     CallObject,
                                     FragmentErrors) );

   ASSERT( CallObject->Type == DIGIWAN_TYPE_CALL_OBJECT );
   AdapterObject = CallObject->pAdapter;
   ASSERT( AdapterObject->Type == DIGIWAN_TYPE_ADAPTER_OBJECT );

   //
   // Give the Link handle we received on LINE_UP
   //
   MacFragment.NdisLinkContext = CallObject->WrapperLinkHandle;
   MacFragment.Errors = FragmentErrors;
   NdisMIndicateStatus( AdapterObject->MiniportAdapterHandle,
                        NDIS_STATUS_WAN_FRAGMENT,
                        &MacFragment,
                        sizeof(MacFragment) );
   NdisMIndicateStatusComplete( AdapterObject->MiniportAdapterHandle );

   DigiDump( (DIGIFLOW|DIGIWANINFO), ("WAN_FRAGMENT: LinkContext=0x%x\n",
                                     MacFragment.NdisLinkContext) );
}  // end WanFragment

