hiddevice.cpp 5.28 KB
Newer Older
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
198
199
200
201
202
203
204
205
#include "OISConfig.h"
#ifdef OIS_WIN32_WIIMOTE_SUPPORT
//cWiimote 0.2 by Kevin Forbes (http://simulatedcomicproduct.com)
//This code is public domain, and comes with no warranty. The user takes full responsibility for anything that happens as a result from using this code.
//This was based in part on Alan Macek <www.alanmacek.com>'s USB interface library

//Edited for Toshiba Stack support (hopefully also all others) by 
//Sean Stellingwerff (http://sean.stellingwerff.com) using information
//gathered from http://www.lvr.com/hidpage.htm (Thanks a million! :D) 

//#include "stdafx.h"
#include "hiddevice.h"

extern "C" 
{
	#include "hidsdi.h"
	#include <Setupapi.h>
}
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hid.lib")

HIDP_CAPS							Capabilities;
PSP_DEVICE_INTERFACE_DETAIL_DATA	detailData;

cHIDDevice::cHIDDevice() : mConnected(false), mHandle(NULL), mEvent(NULL)
{
}


cHIDDevice::~cHIDDevice()
{
	if (mConnected)
	{
		Disconnect();
	}
}

bool cHIDDevice::Disconnect()
{
	bool retval = false;
	if (mConnected)
	{
		retval = (CloseHandle(mHandle) == TRUE && CloseHandle(mEvent) == TRUE);
	
		mConnected = false;
	}

	return retval;
}

bool cHIDDevice::Connect(unsigned short device_id, unsigned short vendor_id, int index)
{
	if (mConnected)
	{
		if (!Disconnect())
		{
			return false;
		}
	}

	// Find the wiimote(s)
	//for (int i = 0; i <= index; i++)
	OpenDevice( device_id, vendor_id, index );

	return mConnected;
}

bool cHIDDevice::OpenDevice(unsigned short device_id, unsigned short vendor_id, int index)
{
	//Use a series of API calls to find a HID with a specified Vendor IF and Product ID.
	HIDD_ATTRIBUTES						Attributes;
	SP_DEVICE_INTERFACE_DATA			devInfoData;
	bool								LastDevice = FALSE;
	bool								MyDeviceDetected = FALSE; 
	int									MemberIndex = 0;
	int									MembersFound = 0;
	GUID								HidGuid;
	ULONG								Length;
	LONG								Result;
	HANDLE								hDevInfo;
	ULONG								Required;

	Length = 0;
	detailData = NULL;
	mHandle=NULL;

	HidD_GetHidGuid(&HidGuid);	
	hDevInfo=SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
		
	devInfoData.cbSize = sizeof(devInfoData);

	MemberIndex = 0;
	MembersFound = 0;
	LastDevice = FALSE;

	do
	{
		Result=SetupDiEnumDeviceInterfaces(hDevInfo, 0, &HidGuid, MemberIndex, &devInfoData);
		if (Result != 0)
		{
			Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, NULL, 0, &Length, NULL);

			detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
			detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
			Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, detailData, Length, &Required, NULL);

			mHandle=CreateFile(detailData->DevicePath, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, 0, NULL);
			Attributes.Size = sizeof(Attributes);

			Result = HidD_GetAttributes(mHandle, &Attributes);
			//Is it the desired device?

			MyDeviceDetected = FALSE;

			if (Attributes.VendorID == vendor_id)
			{
				if (Attributes.ProductID == device_id)
				{
					if (MembersFound == index)
					{
						//Both the Vendor ID and Product ID match.
						//printf("Wiimote found!\n");
						mConnected = true;
						GetCapabilities();

						WriteHandle=CreateFile(detailData->DevicePath, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, 0, NULL);
						MyDeviceDetected = TRUE;

						PrepareForOverlappedTransfer();

						mEvent = CreateEvent(NULL, TRUE, TRUE, "");
						mOverlapped.Offset = 0;
						mOverlapped.OffsetHigh = 0;
						mOverlapped.hEvent = mEvent;
					
					} else { 
						//The Product ID doesn't match.
						CloseHandle(mHandle);
					}

					MembersFound++;
				}
			} else {
				CloseHandle(mHandle);
			}
			free(detailData);
		} else {
			LastDevice=TRUE;
		}
		MemberIndex = MemberIndex + 1;
	} while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE));

	SetupDiDestroyDeviceInfoList(hDevInfo);
	return MyDeviceDetected;
}

bool cHIDDevice::WriteToDevice(unsigned const char * OutputReport, int num_bytes)
{
	bool retval = false;
	if (mConnected)
	{
		DWORD bytes_written;
		retval = (WriteFile( WriteHandle, OutputReport, num_bytes, &bytes_written, &mOverlapped) == TRUE); 
		retval = retval && bytes_written == num_bytes;
	}
	return retval;
}

void cHIDDevice::PrepareForOverlappedTransfer()
{
	//Get a handle to the device for the overlapped ReadFiles.
	ReadHandle=CreateFile(detailData->DevicePath, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
}

void cHIDDevice::GetCapabilities()
{
	//Get the Capabilities structure for the device.
	PHIDP_PREPARSED_DATA PreparsedData;
	HidD_GetPreparsedData(mHandle, &PreparsedData);
	HidP_GetCaps(PreparsedData, &Capabilities);

	//No need for PreparsedData any more, so free the memory it's using.
	HidD_FreePreparsedData(PreparsedData);
}

bool cHIDDevice::ReadFromDevice(unsigned const char * buffer, int max_bytes, int & bytes_read, int timeout)
{
	bool retval = false;
	if (mConnected)
	{
		ReadFile( ReadHandle, (LPVOID)buffer,max_bytes,(LPDWORD)&bytes_read,(LPOVERLAPPED) &mOverlapped); 
		DWORD Result = WaitForSingleObject(mEvent, timeout);
		if (Result == WAIT_OBJECT_0) 
		{		
			retval = true;
		}
		else 
		{
			CancelIo(mHandle);			
		}
		ResetEvent(mEvent);
	}
	return retval;
}
#endif