rdbusb.c
5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* 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;
}