1
/*******************************************************
2
HIDAPI - Multi-Platform library for
3
communication with HID devices.
10
Copyright 2009, All Rights Reserved.
12
At the discretion of the user of this library,
13
this software may be licensed under the terms of the
14
GNU Public License v3, a BSD-Style license, or the
15
original HIDAPI license as outlined in the LICENSE.txt,
16
LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
17
files located at the root of the source distribution.
18
These files may also be found in the public source
19
code repository located at:
20
http://github.com/signal11/hidapi .
21
********************************************************/
26
typedef LONG NTSTATUS;
36
#define _wcsdup wcsdup
39
//#define HIDAPI_USE_DDK
50
// Copied from inc/ddk/hidclass.h, part of the Windows DDK.
51
#define HID_OUT_CTL_CODE(id) \
52
CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
53
#define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
66
// Thanks Microsoft, but I know how to use strncpy().
67
#pragma warning(disable:4996)
74
#ifndef HIDAPI_USE_DDK
75
// Since we're not building with the DDK, and the HID header
76
// files aren't part of the SDK, we have to define all this
77
// stuff here. In lookup_functions(), the function pointers
78
// defined below are set.
79
typedef struct _HIDD_ATTRIBUTES{
84
} HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
87
typedef struct _HIDP_CAPS {
90
USHORT InputReportByteLength;
91
USHORT OutputReportByteLength;
92
USHORT FeatureReportByteLength;
94
USHORT fields_not_used_by_hidapi[10];
95
} HIDP_CAPS, *PHIDP_CAPS;
96
typedef char* HIDP_PREPARSED_DATA;
97
#define HIDP_STATUS_SUCCESS 0x0
99
typedef BOOLEAN (__stdcall *HidD_GetAttributes_)(HANDLE device, PHIDD_ATTRIBUTES attrib);
100
typedef BOOLEAN (__stdcall *HidD_GetSerialNumberString_)(HANDLE device, PVOID buffer, ULONG buffer_len);
101
typedef BOOLEAN (__stdcall *HidD_GetManufacturerString_)(HANDLE handle, PVOID buffer, ULONG buffer_len);
102
typedef BOOLEAN (__stdcall *HidD_GetProductString_)(HANDLE handle, PVOID buffer, ULONG buffer_len);
103
typedef BOOLEAN (__stdcall *HidD_SetFeature_)(HANDLE handle, PVOID data, ULONG length);
104
typedef BOOLEAN (__stdcall *HidD_GetFeature_)(HANDLE handle, PVOID data, ULONG length);
105
typedef BOOLEAN (__stdcall *HidD_GetIndexedString_)(HANDLE handle, ULONG string_index, PVOID buffer, ULONG buffer_len);
106
typedef BOOLEAN (__stdcall *HidD_GetPreparsedData_)(HANDLE handle, HIDP_PREPARSED_DATA **preparsed_data);
107
typedef BOOLEAN (__stdcall *HidD_FreePreparsedData_)(HIDP_PREPARSED_DATA *preparsed_data);
108
typedef BOOLEAN (__stdcall *HidP_GetCaps_)(HIDP_PREPARSED_DATA *preparsed_data, HIDP_CAPS *caps);
110
static HidD_GetAttributes_ HidD_GetAttributes;
111
static HidD_GetSerialNumberString_ HidD_GetSerialNumberString;
112
static HidD_GetManufacturerString_ HidD_GetManufacturerString;
113
static HidD_GetProductString_ HidD_GetProductString;
114
static HidD_SetFeature_ HidD_SetFeature;
115
static HidD_GetFeature_ HidD_GetFeature;
116
static HidD_GetIndexedString_ HidD_GetIndexedString;
117
static HidD_GetPreparsedData_ HidD_GetPreparsedData;
118
static HidD_FreePreparsedData_ HidD_FreePreparsedData;
119
static HidP_GetCaps_ HidP_GetCaps;
121
static HMODULE lib_handle = NULL;
122
static BOOLEAN initialized = FALSE;
123
#endif // HIDAPI_USE_DDK
126
HANDLE device_handle;
128
size_t input_report_length;
129
void *last_error_str;
130
DWORD last_error_num;
136
static hid_device *new_hid_device()
138
hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device));
139
dev->device_handle = INVALID_HANDLE_VALUE;
140
dev->blocking = TRUE;
141
dev->input_report_length = 0;
142
dev->last_error_str = NULL;
143
dev->last_error_num = 0;
144
dev->read_pending = FALSE;
145
dev->read_buf = NULL;
146
memset(&dev->ol, 0, sizeof(dev->ol));
147
dev->ol.hEvent = CreateEvent(NULL, FALSE, FALSE /*inital state f=nonsignaled*/, NULL);
153
static void register_error(hid_device *device, const char *op)
157
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
158
FORMAT_MESSAGE_FROM_SYSTEM |
159
FORMAT_MESSAGE_IGNORE_INSERTS,
162
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
163
(LPWSTR)&msg, 0/*sz*/,
166
// Get rid of the CR and LF that FormatMessage() sticks at the
167
// end of the message. Thanks Microsoft!
177
// Store the message off in the Device entry so that
178
// the hid_error() function can pick it up.
179
LocalFree(device->last_error_str);
180
device->last_error_str = msg;
183
#ifndef HIDAPI_USE_DDK
184
static int lookup_functions()
186
lib_handle = LoadLibraryA("hid.dll");
188
#define RESOLVE(x) x = (x##_)GetProcAddress(lib_handle, #x); if (!x) return -1;
189
RESOLVE(HidD_GetAttributes);
190
RESOLVE(HidD_GetSerialNumberString);
191
RESOLVE(HidD_GetManufacturerString);
192
RESOLVE(HidD_GetProductString);
193
RESOLVE(HidD_SetFeature);
194
RESOLVE(HidD_GetFeature);
195
RESOLVE(HidD_GetIndexedString);
196
RESOLVE(HidD_GetPreparsedData);
197
RESOLVE(HidD_FreePreparsedData);
198
RESOLVE(HidP_GetCaps);
208
static HANDLE open_device(const char *path)
212
/* First, try to open with sharing mode turned off. This will make it so
213
that a HID device can only be opened once. This is to be consistent
214
with the behavior on the other platforms. */
215
handle = CreateFileA(path,
216
GENERIC_WRITE |GENERIC_READ,
220
FILE_FLAG_OVERLAPPED,//FILE_ATTRIBUTE_NORMAL,
223
if (handle == INVALID_HANDLE_VALUE) {
224
/* Couldn't open the device. Some devices must be opened
225
with sharing enabled (even though they are only opened once),
227
handle = CreateFileA(path,
228
GENERIC_WRITE |GENERIC_READ,
229
FILE_SHARE_READ|FILE_SHARE_WRITE, /*share mode*/
232
FILE_FLAG_OVERLAPPED,//FILE_ATTRIBUTE_NORMAL,
239
int HID_API_EXPORT hid_init(void)
241
#ifndef HIDAPI_USE_DDK
243
if (lookup_functions() < 0) {
253
int HID_API_EXPORT hid_exit(void)
255
#ifndef HIDAPI_USE_DDK
257
FreeLibrary(lib_handle);
264
struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
267
struct hid_device_info *root = NULL; // return object
268
struct hid_device_info *cur_dev = NULL;
270
// Windows objects for interacting with the driver.
271
GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30} };
272
SP_DEVINFO_DATA devinfo_data;
273
SP_DEVICE_INTERFACE_DATA device_interface_data;
274
SP_DEVICE_INTERFACE_DETAIL_DATA_A *device_interface_detail_data = NULL;
275
HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
276
int device_index = 0;
281
// Initialize the Windows objects.
282
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
283
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
285
// Get information for all the devices belonging to the HID class.
286
device_info_set = SetupDiGetClassDevsA(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
288
// Iterate over each device in the HID class, looking for the right one.
291
HANDLE write_handle = INVALID_HANDLE_VALUE;
292
DWORD required_size = 0;
293
HIDD_ATTRIBUTES attrib;
295
res = SetupDiEnumDeviceInterfaces(device_info_set,
299
&device_interface_data);
302
// A return of FALSE from this function means that
303
// there are no more devices.
307
// Call with 0-sized detail size, and let the function
308
// tell us how long the detail struct needs to be. The
309
// size is put in &required_size.
310
res = SetupDiGetDeviceInterfaceDetailA(device_info_set,
311
&device_interface_data,
317
// Allocate a long enough structure for device_interface_detail_data.
318
device_interface_detail_data = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) malloc(required_size);
319
device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
321
// Get the detailed data for this device. The detail data gives us
322
// the device path for this device, which is then passed into
323
// CreateFile() to get a handle to the device.
324
res = SetupDiGetDeviceInterfaceDetailA(device_info_set,
325
&device_interface_data,
326
device_interface_detail_data,
332
//register_error(dev, "Unable to call SetupDiGetDeviceInterfaceDetail");
333
// Continue to the next device.
337
//wprintf(L"HandleName: %s\n", device_interface_detail_data->DevicePath);
339
// Open a handle to the device
340
write_handle = open_device(device_interface_detail_data->DevicePath);
342
// Check validity of write_handle.
343
if (write_handle == INVALID_HANDLE_VALUE) {
344
// Unable to open the device.
345
//register_error(dev, "CreateFile");
350
// Get the Vendor ID and Product ID for this device.
351
attrib.Size = sizeof(HIDD_ATTRIBUTES);
352
HidD_GetAttributes(write_handle, &attrib);
353
//wprintf(L"Product/Vendor: %x %x\n", attrib.ProductID, attrib.VendorID);
355
// Check the VID/PID to see if we should add this
356
// device to the enumeration list.
357
if ((vendor_id == 0x0 && product_id == 0x0) ||
358
(attrib.VendorID == vendor_id && attrib.ProductID == product_id)) {
362
struct hid_device_info *tmp;
363
HIDP_PREPARSED_DATA *pp_data = NULL;
367
wchar_t wstr[WSTR_LEN]; // TODO: Determine Size
370
/* VID/PID match. Create the record. */
371
tmp = (struct hid_device_info*) calloc(1, sizeof(struct hid_device_info));
380
// Get the Usage Page and Usage for this device.
381
res = HidD_GetPreparsedData(write_handle, &pp_data);
383
nt_res = HidP_GetCaps(pp_data, &caps);
384
if (nt_res == HIDP_STATUS_SUCCESS) {
385
cur_dev->usage_page = caps.UsagePage;
386
cur_dev->usage = caps.Usage;
389
HidD_FreePreparsedData(pp_data);
392
/* Fill out the record */
393
cur_dev->next = NULL;
394
str = device_interface_detail_data->DevicePath;
397
cur_dev->path = (char*) calloc(len+1, sizeof(char));
398
strncpy(cur_dev->path, str, len+1);
399
cur_dev->path[len] = '\0';
402
cur_dev->path = NULL;
405
res = HidD_GetSerialNumberString(write_handle, wstr, sizeof(wstr));
406
wstr[WSTR_LEN-1] = 0x0000;
408
cur_dev->serial_number = _wcsdup(wstr);
411
/* Manufacturer String */
412
res = HidD_GetManufacturerString(write_handle, wstr, sizeof(wstr));
413
wstr[WSTR_LEN-1] = 0x0000;
415
cur_dev->manufacturer_string = _wcsdup(wstr);
419
res = HidD_GetProductString(write_handle, wstr, sizeof(wstr));
420
wstr[WSTR_LEN-1] = 0x0000;
422
cur_dev->product_string = _wcsdup(wstr);
426
cur_dev->vendor_id = attrib.VendorID;
427
cur_dev->product_id = attrib.ProductID;
430
cur_dev->release_number = attrib.VersionNumber;
432
/* Interface Number. It can sometimes be parsed out of the path
433
on Windows if a device has multiple interfaces. See
434
http://msdn.microsoft.com/en-us/windows/hardware/gg487473 or
435
search for "Hardware IDs for HID Devices" at MSDN. If it's not
436
in the path, it's set to -1. */
437
cur_dev->interface_number = -1;
439
char *interface_component = strstr(cur_dev->path, "&mi_");
440
if (interface_component) {
441
char *hex_str = interface_component + 4;
443
cur_dev->interface_number = strtol(hex_str, &endptr, 16);
444
if (endptr == hex_str) {
445
/* The parsing failed. Set interface_number to -1. */
446
cur_dev->interface_number = -1;
453
CloseHandle(write_handle);
455
// We no longer need the detail data. It can be freed
456
free(device_interface_detail_data);
462
// Close the device information handle.
463
SetupDiDestroyDeviceInfoList(device_info_set);
469
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
471
// TODO: Merge this with the Linux version. This function is platform-independent.
472
struct hid_device_info *d = devs;
474
struct hid_device_info *next = d->next;
476
free(d->serial_number);
477
free(d->manufacturer_string);
478
free(d->product_string);
485
HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number)
487
// TODO: Merge this functions with the Linux version. This function should be platform independent.
488
struct hid_device_info *devs, *cur_dev;
489
const char *path_to_open = NULL;
490
hid_device *handle = NULL;
492
devs = hid_enumerate(vendor_id, product_id);
495
if (cur_dev->vendor_id == vendor_id &&
496
cur_dev->product_id == product_id) {
498
if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
499
path_to_open = cur_dev->path;
504
path_to_open = cur_dev->path;
508
cur_dev = cur_dev->next;
512
/* Open the device */
513
handle = hid_open_path(path_to_open);
516
hid_free_enumeration(devs);
521
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
525
HIDP_PREPARSED_DATA *pp_data = NULL;
529
if (hid_init() < 0) {
533
dev = new_hid_device();
535
// Open a handle to the device
536
dev->device_handle = open_device(path);
538
// Check validity of write_handle.
539
if (dev->device_handle == INVALID_HANDLE_VALUE) {
540
// Unable to open the device.
541
register_error(dev, "CreateFile");
545
// Get the Input Report length for the device.
546
res = HidD_GetPreparsedData(dev->device_handle, &pp_data);
548
register_error(dev, "HidD_GetPreparsedData");
551
nt_res = HidP_GetCaps(pp_data, &caps);
552
if (nt_res != HIDP_STATUS_SUCCESS) {
553
register_error(dev, "HidP_GetCaps");
556
dev->input_report_length = caps.InputReportByteLength;
557
HidD_FreePreparsedData(pp_data);
559
dev->read_buf = (char*) malloc(dev->input_report_length);
564
HidD_FreePreparsedData(pp_data);
566
CloseHandle(dev->device_handle);
571
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length)
577
memset(&ol, 0, sizeof(ol));
579
res = WriteFile(dev->device_handle, data, length, NULL, &ol);
582
if (GetLastError() != ERROR_IO_PENDING) {
583
// WriteFile() failed. Return error.
584
register_error(dev, "WriteFile");
589
// Wait here until the write is done. This makes
590
// hid_write() synchronous.
591
res = GetOverlappedResult(dev->device_handle, &ol, &bytes_written, TRUE/*wait*/);
593
// The Write operation failed.
594
register_error(dev, "WriteFile");
598
return bytes_written;
602
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
604
DWORD bytes_read = 0;
607
// Copy the handle for convenience.
608
HANDLE ev = dev->ol.hEvent;
610
if (!dev->read_pending) {
611
// Start an Overlapped I/O read.
612
dev->read_pending = TRUE;
614
res = ReadFile(dev->device_handle, dev->read_buf, dev->input_report_length, &bytes_read, &dev->ol);
617
if (GetLastError() != ERROR_IO_PENDING) {
618
// ReadFile() has failed.
619
// Clean up and return error.
620
CancelIo(dev->device_handle);
621
dev->read_pending = FALSE;
622
goto end_of_function;
627
if (milliseconds >= 0) {
628
// See if there is any data yet.
629
res = WaitForSingleObject(ev, milliseconds);
630
if (res != WAIT_OBJECT_0) {
631
// There was no data this time. Return zero bytes available,
632
// but leave the Overlapped I/O running.
637
// Either WaitForSingleObject() told us that ReadFile has completed, or
638
// we are in non-blocking mode. Get the number of bytes read. The actual
639
// data has been copied to the data[] array which was passed to ReadFile().
640
res = GetOverlappedResult(dev->device_handle, &dev->ol, &bytes_read, TRUE/*wait*/);
642
// Set pending back to false, even if GetOverlappedResult() returned error.
643
dev->read_pending = FALSE;
645
if (res && bytes_read > 0) {
646
if (dev->read_buf[0] == 0x0) {
647
/* If report numbers aren't being used, but Windows sticks a report
648
number (0x0) on the beginning of the report anyway. To make this
649
work like the other platforms, and to make it work more like the
650
HID spec, we'll skip over this byte. */
652
memcpy(data, dev->read_buf+1, length);
655
/* Copy the whole buffer, report number and all. */
656
memcpy(data, dev->read_buf, length);
662
register_error(dev, "GetOverlappedResult");
669
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, size_t length)
671
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
674
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *dev, int nonblock)
676
dev->blocking = !nonblock;
677
return 0; /* Success */
680
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
682
BOOL res = HidD_SetFeature(dev->device_handle, (PVOID)data, length);
684
register_error(dev, "HidD_SetFeature");
692
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
696
res = HidD_GetFeature(dev->device_handle, data, length);
698
register_error(dev, "HidD_GetFeature");
701
return 0; /* HidD_GetFeature() doesn't give us an actual length, unfortunately */
703
DWORD bytes_returned;
706
memset(&ol, 0, sizeof(ol));
708
res = DeviceIoControl(dev->device_handle,
709
IOCTL_HID_GET_FEATURE,
712
&bytes_returned, &ol);
715
if (GetLastError() != ERROR_IO_PENDING) {
716
// DeviceIoControl() failed. Return error.
717
register_error(dev, "Send Feature Report DeviceIoControl");
722
// Wait here until the write is done. This makes
723
// hid_get_feature_report() synchronous.
724
res = GetOverlappedResult(dev->device_handle, &ol, &bytes_returned, TRUE/*wait*/);
726
// The operation failed.
727
register_error(dev, "Send Feature Report GetOverLappedResult");
730
return bytes_returned;
734
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)
738
CancelIo(dev->device_handle);
739
CloseHandle(dev->ol.hEvent);
740
CloseHandle(dev->device_handle);
741
LocalFree(dev->last_error_str);
746
int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
750
res = HidD_GetManufacturerString(dev->device_handle, string, 2 * maxlen);
752
register_error(dev, "HidD_GetManufacturerString");
759
int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
763
res = HidD_GetProductString(dev->device_handle, string, 2 * maxlen);
765
register_error(dev, "HidD_GetProductString");
772
int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
776
res = HidD_GetSerialNumberString(dev->device_handle, string, 2 * maxlen);
778
register_error(dev, "HidD_GetSerialNumberString");
785
int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
789
res = HidD_GetIndexedString(dev->device_handle, string_index, string, 2 * maxlen);
791
register_error(dev, "HidD_GetIndexedString");
799
HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
801
return (wchar_t*)dev->last_error_str;
809
unsigned short VendorID = 0xa0a0;
810
unsigned short ProductID = 0x0001;
814
unsigned short VendorID = 0x04d8;
815
unsigned short ProductID = 0x3f;
820
unsigned short VendorID = 0x04d8;
821
unsigned short ProductID = 0x0033;
826
int __cdecl main(int argc, char* argv[])
829
unsigned char buf[65];
831
UNREFERENCED_PARAMETER(argc);
832
UNREFERENCED_PARAMETER(argv);
834
// Set up the command buffer.
835
memset(buf,0x00,sizeof(buf));
841
int handle = open(VendorID, ProductID, L"12345");
843
printf("unable to open device\n");
846
// Toggle LED (cmd 0x80)
848
res = write(handle, buf, 65);
850
printf("Unable to write()\n");
852
// Request state (cmd 0x81)
854
write(handle, buf, 65);
856
printf("Unable to write() (2)\n");
858
// Read requested state
859
read(handle, buf, 65);
861
printf("Unable to read()\n");
863
// Print out the returned buffer.
864
for (int i = 0; i < 4; i++)
865
printf("buf[%d]: %d\n", i, buf[i]);