rdbusb.c 5.39 KB

/*  USB functions of rdb driver */
#include <windows.h>
#include "bbrdb_guid.h"
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <setupapi.h>
#include "bbrdb.h"
#include <basetyps.h>
#include "usbdi.h"
#include <stdio.h>
#include <io.h>
#include <fcntl.h>

HANDLE  open_usb_pipe(int which)
{
	int success = 1;
	HANDLE h;
	char pipename[8], usbDevName[512];

	if ( !GetUsbDeviceFileName((LPGUID) &GUID_CLASS_BBRDB, usbDevName)) {
		printf("Failed to GetUsbDeviceFileName\n", GetLastError());
		return  INVALID_HANDLE_VALUE;
	}

	strcat(usbDevName, "\\");
	sprintf(pipename, "PIPE%02d", which);
    strcat (usbDevName, pipename);

	printf("completeDeviceName = (%s)\n", usbDevName);

    {
		int fd;

		fd = _open(usbDevName, _O_RDWR);

        /* if (fd == -1) 
			return 0; */
	}


	h = CreateFile(usbDevName,
		GENERIC_WRITE | GENERIC_READ,
		FILE_SHARE_WRITE | FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		0, 
		NULL);

	if (h == INVALID_HANDLE_VALUE) {
		printf("Failed to open (%s) = %d", usbDevName, GetLastError());
		success = 0;
	} else {
	    printf("Opened successfully.\n");
    }		

	return h;
}

HANDLE OpenOneDevice (HDEVINFO HardwareDeviceInfo, 
		    PSP_INTERFACE_DEVICE_DATA  DeviceInfoData, char *devName)
{
	PSP_INTERFACE_DEVICE_DETAIL_DATA     functionClassDeviceData = NULL;
    ULONG                                predictedLength = 0;
    ULONG                                requiredLength = 0;
	HANDLE								 hOut = INVALID_HANDLE_VALUE;

	SetupDiGetInterfaceDeviceDetail (
            HardwareDeviceInfo,
            DeviceInfoData,
            NULL, // probing so no output buffer yet
            0, // probing so output buffer length of zero
            &requiredLength,
            NULL); // not interested in the specific dev-node


    predictedLength = requiredLength;

    functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc (predictedLength);
    functionClassDeviceData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);

    if (! SetupDiGetInterfaceDeviceDetail (
               HardwareDeviceInfo,
               DeviceInfoData,
               functionClassDeviceData,
               predictedLength,
               &requiredLength,
               NULL)) {
		free( functionClassDeviceData );
        return INVALID_HANDLE_VALUE;
    }

	strcpy( devName,functionClassDeviceData->DevicePath) ;
	printf( "Attempting to open %s\n", devName );
    hOut = CreateFile (
                  functionClassDeviceData->DevicePath,
                  GENERIC_READ | GENERIC_WRITE,
                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                  NULL, // no SECURITY_ATTRIBUTES structure
                  OPEN_EXISTING, // No special create flags
                  0, // No special attributes
                  NULL); // No template file

    if (INVALID_HANDLE_VALUE == hOut) {
		printf( "FAILED to open %s\n", devName );
    }
	free( functionClassDeviceData );

	return hOut;
}

HANDLE OpenUsbDevice( LPGUID  pGuid, char *outNameBuf)
{
   ULONG NumberDevices;
   HANDLE hOut = INVALID_HANDLE_VALUE;
   HDEVINFO                 hardwareDeviceInfo;
   SP_INTERFACE_DEVICE_DATA deviceInfoData;
   ULONG                    i;
   BOOLEAN                  done;
   PUSB_DEVICE_DESCRIPTOR   usbDeviceInst;
   PUSB_DEVICE_DESCRIPTOR	*UsbDevices = &usbDeviceInst;

   *UsbDevices = NULL;
   NumberDevices = 0;

   hardwareDeviceInfo = SetupDiGetClassDevs (
                           pGuid,
                           NULL, // Define no enumerator (global)
                           NULL, // Define no
                           (DIGCF_PRESENT | // Only Devices present
                            DIGCF_INTERFACEDEVICE));

   NumberDevices = 4;
   done = FALSE;
   deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);

   i=0;
   while (!done) {
      NumberDevices *= 2;

      if (*UsbDevices) {
         *UsbDevices = (PUSB_DEVICE_DESCRIPTOR)
               realloc(*UsbDevices, (NumberDevices * sizeof (USB_DEVICE_DESCRIPTOR)));
      } else {
         *UsbDevices = (PUSB_DEVICE_DESCRIPTOR) calloc (NumberDevices, sizeof (USB_DEVICE_DESCRIPTOR));
      }

      if (NULL == *UsbDevices) {  
		 SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
         return INVALID_HANDLE_VALUE;
      }

      usbDeviceInst = *UsbDevices + i;
      for (; i < NumberDevices; i++) {
		 if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,
                                         0, // We don't care about specific PDOs
										 pGuid,
                                         i,
                                         &deviceInfoData)) {

            hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, outNameBuf);
			if ( hOut != INVALID_HANDLE_VALUE ) {
               done = TRUE;
               break;
			}
         } else {
            if (ERROR_NO_MORE_ITEMS == GetLastError()) {
               done = TRUE;
               break;
            }
         }	
	  }
   }

   NumberDevices = i;
   SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
   free ( *UsbDevices );

   return hOut;
}

BOOL GetUsbDeviceFileName( LPGUID  pGuid, char *outNameBuf)
{
	HANDLE hDev = OpenUsbDevice( pGuid, outNameBuf );
	if ( hDev != INVALID_HANDLE_VALUE )
	{
		CloseHandle( hDev );
		return TRUE;
	}
	return FALSE;

}