/*++
*******************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved				*
* 																										*
* This software contains proprietary and confidential 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.																						*
* 																										*
* Restricted Rights Legend																		*
*																										*
* Use, duplication, or disclosure by the Government is subject to					*
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in			*
* Technical Data and Computer Software clause at DFARS 252.227-7031 or			*
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -				*
* Restricted Rights at 48 CFR 52.227-19, as applicable.								*
*																										*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343				*
*																										*
*******************************************************************************

Module Name:

   dxbapi.c

Abstract:

   Wrapper routines for dstreams api requests.

Author:

   Michael Zarns

--*/
#include "windows.h"
#include "winioctl.h"
#include "stdio.h"
#include "stdlib.h"

#include "dsioctl.h"
#include "dxbapi.h"

static CHAR DebugBuffer[4096];

BOOL 
dsvSyncIoRequest( IN HANDLE hDevice, 
                  IN LONG   Ioctl, 
                  IN PVOID  pInData, 
                  IN ULONG  InDataLength, 
                  IN PVOID  pOutData, 
                  IN ULONG  OutDataLength,
                  OUT PDWORD pError )
{
	BOOL bSuccess;
	DWORD dwBytes;
	OVERLAPPED Ol;
	DWORD Error;

	ZeroMemory( &Ol, sizeof( Ol ) );
	Ol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
	dwBytes = 0;

	bSuccess = DeviceIoControl( hDevice, Ioctl, 
	                            pInData, InDataLength,
	                            pOutData, OutDataLength,
	                            &dwBytes, &Ol );
	if ( !bSuccess )
	{
		Error = GetLastError();
		if ( Error == ERROR_IO_PENDING )
			bSuccess = GetOverlappedResult( hDevice, &Ol, &dwBytes, TRUE );
		else
		{
		#if DBG
	      sprintf( DebugBuffer, "DeviceIoControl failed, Error %d\n", Error );
	      OutputDebugString( DebugBuffer );
		#endif
		}
	}

	CloseHandle( Ol.hEvent );

	*pError = bSuccess ? 0 : GetLastError();
	return( bSuccess );
}


BOOL 
dsvAsyncIoRequest( HANDLE hDevice, 
                   LONG   Ioctl, 
                   PVOID  pInData, 
                   ULONG  InDataLength, 
                   PVOID  pOutData, 
                   ULONG  OutDataLength,
                   PDWORD pdwBytes,
                   LPOVERLAPPED pOl )
{
	BOOL bSuccess;

	bSuccess = DeviceIoControl( hDevice, Ioctl, 
	                            pInData, InDataLength,
	                            pOutData, OutDataLength,
	                            pdwBytes, pOl );

#if DBG
	if ( !bSuccess && GetLastError() != ERROR_IO_PENDING )
	{
		#if DBG
	      sprintf( DebugBuffer, "AsyncIoRequest failed, GetLastError = 0x%08x\n", GetLastError() );
	      OutputDebugString( DebugBuffer );
		#endif
	}
#endif

	return( bSuccess );
}


BOOL
DsOpen(
	IN HANDLE hDevice,
	IN PCHAR pDeviceName,
	OUT PHANDLE phStream )
/*++

Description:

	This routine performs a synchronous open request to the named device.

Parameters:

	hDevice
		Device handle on which to send this request.

	pDeviceName
		Null-terminated string containing the devicename to open, i.e. "X250".

	phStream
		Address to which the stream handle will be written on successful completion
		of the open request..

Return Value:

	TRUE if successful, otherwise FALSE.

--*/
{
	DS_OPEN Open = {0};
	BOOL bSuccess;
	DWORD Error;

	strncpy( (PVOID)&Open.DeviceName, pDeviceName, DS_DEVICENAME_SIZE );
	bSuccess = dsvSyncIoRequest( hDevice, IOCTL_DS_OPEN,
	                             &Open, sizeof( Open ),
	                             &Open, sizeof( Open ), &Error );
	*phStream = Open.hStream;
	return( bSuccess && !Open.ErrorCode );
}


BOOL
DsClose(
	IN HANDLE hDevice,
	IN HANDLE hStream )
/*++

Description:

	Performs a synchronous close request on the stream.

Parameters:

	hDevice
		Device handle on which to send this request.

	hStream
		Handle of stream to close.

Return Value:

	TRUE if successful, otherwise FALSE.

--*/
{
	DS_CLOSE Close = {0};
	BOOL bSuccess;
	DWORD Error;
	Close.hStream = hStream;
	bSuccess = dsvSyncIoRequest( hDevice, IOCTL_DS_CLOSE,
	                             &Close, sizeof Close,
	                             &Close, sizeof Close,
	                             &Error );
	return( bSuccess );
}


BOOL
DsIoctl(
	IN HANDLE hDevice,
	IN HANDLE hStream,
	IN ULONG  Command,
	IN PVOID  pData,
	IN ULONG  cbData )

/*++

Description:

	Performs a synchronous ioctl request on the stream.

Return Value:

	TRUE if successful, otherwise FALSE.

--*/
{
	DS_IOCTL Ioctl = {0};
	BOOL bSuccess;
	DWORD Error;

	Ioctl.hStream = hStream;
	Ioctl.Command = Command;
	Ioctl.pDataIn = Ioctl.pDataOut = pData;
	Ioctl.cbDataIn = Ioctl.cbMaxDataOut = cbData;


	bSuccess = dsvSyncIoRequest( hDevice, IOCTL_DS_IOCTL, 
	                             &Ioctl, sizeof Ioctl, 
	                             &Ioctl, sizeof Ioctl, &Error );
	if ( !bSuccess || Error || Ioctl.ReturnValue || Ioctl.ErrorCode )
	{
		#if DBG
	      sprintf( DebugBuffer, "Ioctl failed, bSuccess 0x%x, ReturnVal 0x%x, Error 0x%x\n", bSuccess, Ioctl.ReturnValue, Ioctl.ErrorCode );
	      OutputDebugString( DebugBuffer );
		#endif
		return FALSE;
	}
	return TRUE;
}
