 /*++
*******************************************************************************
* Copyright (c) 1990-97 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:

   PciTest.c

Abstract:

   This program is a win32 console application which exercises the DigiPci driver.

	It performs the following tasks: 
		- create the driver service
		- start the driver
		- send the ioctl request to get pci data
		- stop the driver
		- remove the driver service

Author:

   Michael Zarns

--*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include "digipci.h"


#define PrintUsage() \

SC_HANDLE
StartDriver( PCSTR pDriverPath )
{
	BOOL bSuccess;
	SC_HANDLE hService = NULL;
	SC_HANDLE hScManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
	if ( hScManager == NULL )
	{
		printf( "OpenSCManager failed, GetLastError 0x%x\n", GetLastError() );
		goto Exit;
	}
	
	hService = CreateService( hScManager, WANLINKS_PCI_NAME_TEXT, DIGIPCI_DISPLAYNAME, 
	                          SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, 
	                          SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, 
	                          pDriverPath, NULL, NULL, NULL, NULL, NULL );
	if ( hService == NULL )
	{
		printf( "CreateService failed, GetLastError 0x%x\n", GetLastError() );
		goto Exit;
	}

	bSuccess = StartService( hService, 0, NULL );
	if ( !bSuccess )
	{
		printf( "StartService failed, GetLastError 0x%x\n", GetLastError() );
		DeleteService( hService );
		CloseServiceHandle( hService );
		hService = NULL;
	}

Exit:
	if ( hScManager != NULL )
		CloseServiceHandle( hScManager );

	return hService;
}




VOID
StopDriver( HANDLE hService )
{
	SERVICE_STATUS Status;

	if ( !ControlService( hService, SERVICE_CONTROL_STOP, &Status ) )
		printf( "ControlService failed, GetLastError 0x%x\n", GetLastError() );

	if ( !DeleteService( hService ) )
		printf( "DeleteService failed, GetLastError 0x%x\n", GetLastError() );

	CloseServiceHandle( hService );
}


VOID
GetData( DWORD BusNumber, DWORD SlotNumber )
{
	HANDLE hFile;
	BOOL bSuccess;
	DIGI_PCI_DATA PciData = {0};
	DWORD cb;

	hFile = CreateFile( TEXT("\\\\.\\")WANLINKS_PCI_NAME_TEXT, GENERIC_READ | GENERIC_WRITE,  
	                    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 
	                    FILE_ATTRIBUTE_NORMAL, NULL );
	if ( hFile == NULL )
	{
		printf( "CreateFile failed, GetLastError 0x%x\n", GetLastError() );
		return;
	}
	
	PciData.BusNumber  = BusNumber;
	PciData.SlotNumber = SlotNumber;

	bSuccess = DeviceIoControl( hFile, IOCTL_DIGIPCI_GET_DATA, &PciData, sizeof PciData, 
	                            &PciData, sizeof PciData, &cb, NULL );

	if ( bSuccess )
		printf( "PciData:\n"
		        "   BusNumber   0x%x\n"
		        "   SlotNumber  0x%x\n"
		        "   VendorId    0x%x\n"
		        "   DeviceId    0x%x\n"
		        "   SubVendorId 0x%x\n"
		        "   SubSystemId 0x%x\n"
		        "   RevisionId  0x%x\n",
		        PciData.BusNumber, PciData.SlotNumber, 
		        PciData.VendorId, PciData.DeviceId, PciData.SubVendorId, 
		        PciData.SubSystemId, PciData.RevisionId );
	else
		printf( "DeviceIoControl failed, GetLastError 0x%x\n", GetLastError() );

	CloseHandle( hFile );
}


void _cdecl
main( int argc, char *argv[] )
{
	DWORD BusNumber;
	DWORD SlotNumber;
	HANDLE hService;

	// This program requires 3 arguments - busnumber, slotnumber, and path to driver.
	if ( argc != 4 )
	{
	   printf( "usage:\n\n"
	           "   PciTest BusNumber SlotNumber DriverPathAndName\n\n"
	           "   Example: pcitest 0 4 c:\\trash\\digipci.sys\n");
		return;
	}

	BusNumber = atoi( argv[1] );
	SlotNumber = atoi( argv[2] );
	printf( "Using BusNumber %d, SlotNumber %d, path %s\n", BusNumber, SlotNumber, argv[3] );

	hService = StartDriver( argv[3] );
	if ( hService )
	{
		GetData( BusNumber, SlotNumber );
		StopDriver( hService );
	}
}


