| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| 24 |
|
| 25 |
|
| 26 |
#define _CRT_SECURE_NO_WARNINGS |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef __cplusplus |
| 30 |
extern "C" { |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "hidapi_winapi.h" |
| 34 |
|
| 35 |
#include <windows.h> |
| 36 |
|
| 37 |
#ifndef _NTDEF_ |
| 38 |
typedef LONG NTSTATUS; |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifdef __MINGW32__ |
| 42 |
#include <ntdef.h> |
| 43 |
#include <winbase.h> |
| 44 |
#define WC_ERR_INVALID_CHARS 0x00000080 |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef __CYGWIN__ |
| 48 |
#include <ntdef.h> |
| 49 |
#include <wctype.h> |
| 50 |
#define _wcsdup wcsdup |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#include "hidapi_cfgmgr32.h" |
| 56 |
#include "hidapi_hidclass.h" |
| 57 |
#include "hidapi_hidsdi.h" |
| 58 |
|
| 59 |
#include <stdio.h> |
| 60 |
#include <stdlib.h> |
| 61 |
#include <string.h> |
| 62 |
|
| 63 |
#ifdef MIN |
| 64 |
#undef MIN |
| 65 |
#endif |
| 66 |
#define MIN(x,y) ((x) < (y)? (x): (y)) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#define MAX_STRING_WCHARS 256 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#define MAX_STRING_WCHARS_USB 126 |
| 80 |
|
| 81 |
static struct hid_api_version api_version = { |
| 82 |
.major = HID_API_VERSION_MAJOR, |
| 83 |
.minor = HID_API_VERSION_MINOR, |
| 84 |
.patch = HID_API_VERSION_PATCH |
| 85 |
}; |
| 86 |
|
| 87 |
#ifndef HIDAPI_USE_DDK |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static HidD_GetHidGuid_ HidD_GetHidGuid; |
| 94 |
static HidD_GetAttributes_ HidD_GetAttributes; |
| 95 |
static HidD_GetSerialNumberString_ HidD_GetSerialNumberString; |
| 96 |
static HidD_GetManufacturerString_ HidD_GetManufacturerString; |
| 97 |
static HidD_GetProductString_ HidD_GetProductString; |
| 98 |
static HidD_SetFeature_ HidD_SetFeature; |
| 99 |
static HidD_GetFeature_ HidD_GetFeature; |
| 100 |
static HidD_SetOutputReport_ HidD_SetOutputReport; |
| 101 |
static HidD_GetInputReport_ HidD_GetInputReport; |
| 102 |
static HidD_GetIndexedString_ HidD_GetIndexedString; |
| 103 |
static HidD_GetPreparsedData_ HidD_GetPreparsedData; |
| 104 |
static HidD_FreePreparsedData_ HidD_FreePreparsedData; |
| 105 |
static HidP_GetCaps_ HidP_GetCaps; |
| 106 |
static HidD_SetNumInputBuffers_ HidD_SetNumInputBuffers; |
| 107 |
|
| 108 |
static CM_Locate_DevNodeW_ CM_Locate_DevNodeW = NULL; |
| 109 |
static CM_Get_Parent_ CM_Get_Parent = NULL; |
| 110 |
static CM_Get_DevNode_PropertyW_ CM_Get_DevNode_PropertyW = NULL; |
| 111 |
static CM_Get_Device_Interface_PropertyW_ CM_Get_Device_Interface_PropertyW = NULL; |
| 112 |
static CM_Get_Device_Interface_List_SizeW_ CM_Get_Device_Interface_List_SizeW = NULL; |
| 113 |
static CM_Get_Device_Interface_ListW_ CM_Get_Device_Interface_ListW = NULL; |
| 114 |
|
| 115 |
static HMODULE hid_lib_handle = NULL; |
| 116 |
static HMODULE cfgmgr32_lib_handle = NULL; |
| 117 |
static BOOLEAN hidapi_initialized = FALSE; |
| 118 |
|
| 119 |
static void free_library_handles() |
| 120 |
{ |
| 121 |
if (hid_lib_handle) |
| 122 |
FreeLibrary(hid_lib_handle); |
| 123 |
hid_lib_handle = NULL; |
| 124 |
if (cfgmgr32_lib_handle) |
| 125 |
FreeLibrary(cfgmgr32_lib_handle); |
| 126 |
cfgmgr32_lib_handle = NULL; |
| 127 |
} |
| 128 |
|
| 129 |
static int lookup_functions() |
| 130 |
{ |
| 131 |
hid_lib_handle = LoadLibraryW(L"hid.dll"); |
| 132 |
if (hid_lib_handle == NULL) { |
| 133 |
goto err; |
| 134 |
} |
| 135 |
|
| 136 |
cfgmgr32_lib_handle = LoadLibraryW(L"cfgmgr32.dll"); |
| 137 |
if (cfgmgr32_lib_handle == NULL) { |
| 138 |
goto err; |
| 139 |
} |
| 140 |
|
| 141 |
#if defined(__GNUC__) |
| 142 |
# pragma GCC diagnostic push |
| 143 |
# pragma GCC diagnostic ignored "-Wcast-function-type" |
| 144 |
#endif |
| 145 |
#define RESOLVE(lib_handle, x) x = (x##_)GetProcAddress(lib_handle, #x); if (!x) goto err; |
| 146 |
|
| 147 |
RESOLVE(hid_lib_handle, HidD_GetHidGuid); |
| 148 |
RESOLVE(hid_lib_handle, HidD_GetAttributes); |
| 149 |
RESOLVE(hid_lib_handle, HidD_GetSerialNumberString); |
| 150 |
RESOLVE(hid_lib_handle, HidD_GetManufacturerString); |
| 151 |
RESOLVE(hid_lib_handle, HidD_GetProductString); |
| 152 |
RESOLVE(hid_lib_handle, HidD_SetFeature); |
| 153 |
RESOLVE(hid_lib_handle, HidD_GetFeature); |
| 154 |
RESOLVE(hid_lib_handle, HidD_SetOutputReport); |
| 155 |
RESOLVE(hid_lib_handle, HidD_GetInputReport); |
| 156 |
RESOLVE(hid_lib_handle, HidD_GetIndexedString); |
| 157 |
RESOLVE(hid_lib_handle, HidD_GetPreparsedData); |
| 158 |
RESOLVE(hid_lib_handle, HidD_FreePreparsedData); |
| 159 |
RESOLVE(hid_lib_handle, HidP_GetCaps); |
| 160 |
RESOLVE(hid_lib_handle, HidD_SetNumInputBuffers); |
| 161 |
|
| 162 |
RESOLVE(cfgmgr32_lib_handle, CM_Locate_DevNodeW); |
| 163 |
RESOLVE(cfgmgr32_lib_handle, CM_Get_Parent); |
| 164 |
RESOLVE(cfgmgr32_lib_handle, CM_Get_DevNode_PropertyW); |
| 165 |
RESOLVE(cfgmgr32_lib_handle, CM_Get_Device_Interface_PropertyW); |
| 166 |
RESOLVE(cfgmgr32_lib_handle, CM_Get_Device_Interface_List_SizeW); |
| 167 |
RESOLVE(cfgmgr32_lib_handle, CM_Get_Device_Interface_ListW); |
| 168 |
|
| 169 |
#undef RESOLVE |
| 170 |
#if defined(__GNUC__) |
| 171 |
# pragma GCC diagnostic pop |
| 172 |
#endif |
| 173 |
|
| 174 |
return 0; |
| 175 |
|
| 176 |
err: |
| 177 |
free_library_handles(); |
| 178 |
return -1; |
| 179 |
} |
| 180 |
|
| 181 |
#endif |
| 182 |
|
| 183 |
struct hid_device_ { |
| 184 |
HANDLE device_handle; |
| 185 |
BOOL blocking; |
| 186 |
USHORT output_report_length; |
| 187 |
unsigned char *write_buf; |
| 188 |
size_t input_report_length; |
| 189 |
USHORT feature_report_length; |
| 190 |
unsigned char *feature_buf; |
| 191 |
wchar_t *last_error_str; |
| 192 |
wchar_t *last_read_error_str; |
| 193 |
BOOL read_pending; |
| 194 |
char *read_buf; |
| 195 |
OVERLAPPED ol; |
| 196 |
OVERLAPPED write_ol; |
| 197 |
struct hid_device_info* device_info; |
| 198 |
DWORD write_timeout_ms; |
| 199 |
}; |
| 200 |
|
| 201 |
static hid_device *new_hid_device() |
| 202 |
{ |
| 203 |
hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); |
| 204 |
|
| 205 |
if (dev == NULL) { |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
dev->device_handle = INVALID_HANDLE_VALUE; |
| 210 |
dev->blocking = TRUE; |
| 211 |
dev->output_report_length = 0; |
| 212 |
dev->write_buf = NULL; |
| 213 |
dev->input_report_length = 0; |
| 214 |
dev->feature_report_length = 0; |
| 215 |
dev->feature_buf = NULL; |
| 216 |
dev->last_error_str = NULL; |
| 217 |
dev->last_read_error_str = NULL; |
| 218 |
dev->read_pending = FALSE; |
| 219 |
dev->read_buf = NULL; |
| 220 |
memset(&dev->ol, 0, sizeof(dev->ol)); |
| 221 |
dev->ol.hEvent = CreateEvent(NULL, FALSE, FALSE , NULL); |
| 222 |
memset(&dev->write_ol, 0, sizeof(dev->write_ol)); |
| 223 |
dev->write_ol.hEvent = CreateEvent(NULL, FALSE, FALSE , NULL); |
| 224 |
dev->device_info = NULL; |
| 225 |
dev->write_timeout_ms = 1000; |
| 226 |
|
| 227 |
return dev; |
| 228 |
} |
| 229 |
|
| 230 |
static void free_hid_device(hid_device *dev) |
| 231 |
{ |
| 232 |
CloseHandle(dev->ol.hEvent); |
| 233 |
CloseHandle(dev->write_ol.hEvent); |
| 234 |
CloseHandle(dev->device_handle); |
| 235 |
free(dev->last_error_str); |
| 236 |
free(dev->last_read_error_str); |
| 237 |
dev->last_error_str = NULL; |
| 238 |
dev->last_read_error_str = NULL; |
| 239 |
free(dev->write_buf); |
| 240 |
free(dev->feature_buf); |
| 241 |
free(dev->read_buf); |
| 242 |
hid_free_enumeration(dev->device_info); |
| 243 |
free(dev); |
| 244 |
} |
| 245 |
|
| 246 |
static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR *op) |
| 247 |
{ |
| 248 |
free(*error_buffer); |
| 249 |
*error_buffer = NULL; |
| 250 |
|
| 251 |
|
| 252 |
if (!op) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
WCHAR system_err_buf[1024]; |
| 257 |
DWORD error_code = GetLastError(); |
| 258 |
|
| 259 |
DWORD system_err_len = FormatMessageW( |
| 260 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 261 |
NULL, |
| 262 |
error_code, |
| 263 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 264 |
system_err_buf, ARRAYSIZE(system_err_buf), |
| 265 |
NULL); |
| 266 |
|
| 267 |
DWORD op_len = (DWORD)wcslen(op); |
| 268 |
|
| 269 |
DWORD op_prefix_len = |
| 270 |
op_len |
| 271 |
+ 15 |
| 272 |
; |
| 273 |
DWORD msg_len = |
| 274 |
+ op_prefix_len |
| 275 |
+ system_err_len |
| 276 |
; |
| 277 |
|
| 278 |
*error_buffer = (WCHAR *)calloc(msg_len + 1, sizeof (WCHAR)); |
| 279 |
WCHAR *msg = *error_buffer; |
| 280 |
|
| 281 |
if (!msg) |
| 282 |
return; |
| 283 |
|
| 284 |
int printf_written = swprintf(msg, msg_len + 1, L"%.*ls: (0x%08X) %.*ls", (int)op_len, op, error_code, (int)system_err_len, system_err_buf); |
| 285 |
|
| 286 |
if (printf_written < 0) |
| 287 |
{ |
| 288 |
|
| 289 |
msg[0] = L'\0'; |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
while(msg[msg_len-1] == L'\r' || msg[msg_len-1] == L'\n' || msg[msg_len-1] == L' ') |
| 296 |
{ |
| 297 |
msg[msg_len-1] = L'\0'; |
| 298 |
msg_len--; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
#if defined(__GNUC__) |
| 303 |
# pragma GCC diagnostic push |
| 304 |
# pragma GCC diagnostic ignored "-Warray-bounds" |
| 305 |
#endif |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
static void register_string_error_to_buffer(wchar_t **error_buffer, const WCHAR *string_error) |
| 312 |
{ |
| 313 |
free(*error_buffer); |
| 314 |
*error_buffer = NULL; |
| 315 |
|
| 316 |
if (string_error) { |
| 317 |
*error_buffer = _wcsdup(string_error); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
#if defined(__GNUC__) |
| 322 |
# pragma GCC diagnostic pop |
| 323 |
#endif |
| 324 |
|
| 325 |
static void register_winapi_error(hid_device *dev, const WCHAR *op) |
| 326 |
{ |
| 327 |
register_winapi_error_to_buffer(&dev->last_error_str, op); |
| 328 |
} |
| 329 |
|
| 330 |
static void register_string_error(hid_device *dev, const WCHAR *string_error) |
| 331 |
{ |
| 332 |
register_string_error_to_buffer(&dev->last_error_str, string_error); |
| 333 |
} |
| 334 |
|
| 335 |
static wchar_t *last_global_error_str = NULL; |
| 336 |
|
| 337 |
static void register_global_winapi_error(const WCHAR *op) |
| 338 |
{ |
| 339 |
register_winapi_error_to_buffer(&last_global_error_str, op); |
| 340 |
} |
| 341 |
|
| 342 |
static void register_global_error(const WCHAR *string_error) |
| 343 |
{ |
| 344 |
register_string_error_to_buffer(&last_global_error_str, string_error); |
| 345 |
} |
| 346 |
|
| 347 |
static HANDLE open_device(const wchar_t *path, BOOL open_rw) |
| 348 |
{ |
| 349 |
HANDLE handle; |
| 350 |
DWORD desired_access = (open_rw)? (GENERIC_WRITE | GENERIC_READ): 0; |
| 351 |
DWORD share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE; |
| 352 |
|
| 353 |
handle = CreateFileW(path, |
| 354 |
desired_access, |
| 355 |
share_mode, |
| 356 |
NULL, |
| 357 |
OPEN_EXISTING, |
| 358 |
FILE_FLAG_OVERLAPPED, |
| 359 |
0); |
| 360 |
|
| 361 |
return handle; |
| 362 |
} |
| 363 |
|
| 364 |
HID_API_EXPORT const struct hid_api_version* HID_API_CALL hid_version(void) |
| 365 |
{ |
| 366 |
return &api_version; |
| 367 |
} |
| 368 |
|
| 369 |
HID_API_EXPORT const char* HID_API_CALL hid_version_str(void) |
| 370 |
{ |
| 371 |
return HID_API_VERSION_STR; |
| 372 |
} |
| 373 |
|
| 374 |
int HID_API_EXPORT hid_init(void) |
| 375 |
{ |
| 376 |
register_global_error(NULL); |
| 377 |
#ifndef HIDAPI_USE_DDK |
| 378 |
if (!hidapi_initialized) { |
| 379 |
if (lookup_functions() < 0) { |
| 380 |
register_global_winapi_error(L"resolve DLL functions"); |
| 381 |
return -1; |
| 382 |
} |
| 383 |
hidapi_initialized = TRUE; |
| 384 |
} |
| 385 |
#endif |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
int HID_API_EXPORT hid_exit(void) |
| 390 |
{ |
| 391 |
#ifndef HIDAPI_USE_DDK |
| 392 |
free_library_handles(); |
| 393 |
hidapi_initialized = FALSE; |
| 394 |
#endif |
| 395 |
register_global_error(NULL); |
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
static void* hid_internal_get_devnode_property(DEVINST dev_node, const DEVPROPKEY* property_key, DEVPROPTYPE expected_property_type) |
| 400 |
{ |
| 401 |
ULONG len = 0; |
| 402 |
CONFIGRET cr; |
| 403 |
DEVPROPTYPE property_type; |
| 404 |
PBYTE property_value = NULL; |
| 405 |
|
| 406 |
cr = CM_Get_DevNode_PropertyW(dev_node, property_key, &property_type, NULL, &len, 0); |
| 407 |
if (cr != CR_BUFFER_SMALL || property_type != expected_property_type) |
| 408 |
return NULL; |
| 409 |
|
| 410 |
property_value = (PBYTE)calloc(len, sizeof(BYTE)); |
| 411 |
cr = CM_Get_DevNode_PropertyW(dev_node, property_key, &property_type, property_value, &len, 0); |
| 412 |
if (cr != CR_SUCCESS) { |
| 413 |
free(property_value); |
| 414 |
return NULL; |
| 415 |
} |
| 416 |
|
| 417 |
return property_value; |
| 418 |
} |
| 419 |
|
| 420 |
static void* hid_internal_get_device_interface_property(const wchar_t* interface_path, const DEVPROPKEY* property_key, DEVPROPTYPE expected_property_type) |
| 421 |
{ |
| 422 |
ULONG len = 0; |
| 423 |
CONFIGRET cr; |
| 424 |
DEVPROPTYPE property_type; |
| 425 |
PBYTE property_value = NULL; |
| 426 |
|
| 427 |
cr = CM_Get_Device_Interface_PropertyW(interface_path, property_key, &property_type, NULL, &len, 0); |
| 428 |
if (cr != CR_BUFFER_SMALL || property_type != expected_property_type) |
| 429 |
return NULL; |
| 430 |
|
| 431 |
property_value = (PBYTE)calloc(len, sizeof(BYTE)); |
| 432 |
cr = CM_Get_Device_Interface_PropertyW(interface_path, property_key, &property_type, property_value, &len, 0); |
| 433 |
if (cr != CR_SUCCESS) { |
| 434 |
free(property_value); |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
|
| 438 |
return property_value; |
| 439 |
} |
| 440 |
|
| 441 |
static void hid_internal_towupper(wchar_t* string) |
| 442 |
{ |
| 443 |
for (wchar_t* p = string; *p; ++p) *p = towupper(*p); |
| 444 |
} |
| 445 |
|
| 446 |
static int hid_internal_extract_int_token_value(wchar_t* string, const wchar_t* token) |
| 447 |
{ |
| 448 |
int token_value; |
| 449 |
wchar_t* startptr, * endptr; |
| 450 |
|
| 451 |
startptr = wcsstr(string, token); |
| 452 |
if (!startptr) |
| 453 |
return -1; |
| 454 |
|
| 455 |
startptr += wcslen(token); |
| 456 |
token_value = wcstol(startptr, &endptr, 16); |
| 457 |
if (endptr == startptr) |
| 458 |
return -1; |
| 459 |
|
| 460 |
return token_value; |
| 461 |
} |
| 462 |
|
| 463 |
static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_node) |
| 464 |
{ |
| 465 |
wchar_t *device_id = NULL, *hardware_ids = NULL; |
| 466 |
|
| 467 |
device_id = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING); |
| 468 |
if (!device_id) |
| 469 |
goto end; |
| 470 |
|
| 471 |
|
| 472 |
hid_internal_towupper(device_id); |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if (hid_internal_extract_int_token_value(device_id, L"IG_") != -1) { |
| 479 |
|
| 480 |
if (CM_Get_Parent(&dev_node, dev_node, 0) != CR_SUCCESS) |
| 481 |
goto end; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
hardware_ids = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_HardwareIds, DEVPROP_TYPE_STRING_LIST); |
| 486 |
if (!hardware_ids) |
| 487 |
goto end; |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
for (wchar_t* hardware_id = hardware_ids; *hardware_id; hardware_id += wcslen(hardware_id) + 1) { |
| 494 |
|
| 495 |
hid_internal_towupper(hardware_id); |
| 496 |
|
| 497 |
if (dev->release_number == 0) { |
| 498 |
|
| 499 |
int release_number = hid_internal_extract_int_token_value(hardware_id, L"REV_"); |
| 500 |
if (release_number != -1) { |
| 501 |
dev->release_number = (unsigned short)release_number; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
if (dev->interface_number == -1) { |
| 506 |
|
| 507 |
int interface_number = hid_internal_extract_int_token_value(hardware_id, L"MI_"); |
| 508 |
if (interface_number != -1) { |
| 509 |
dev->interface_number = interface_number; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
if (wcslen(dev->manufacturer_string) == 0) { |
| 516 |
wchar_t* manufacturer_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_Manufacturer, DEVPROP_TYPE_STRING); |
| 517 |
if (manufacturer_string) { |
| 518 |
free(dev->manufacturer_string); |
| 519 |
dev->manufacturer_string = manufacturer_string; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
if (wcslen(dev->serial_number) == 0) { |
| 525 |
DEVINST usb_dev_node = dev_node; |
| 526 |
if (dev->interface_number != -1) { |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
if (CM_Get_Parent(&usb_dev_node, dev_node, 0) != CR_SUCCESS) |
| 531 |
goto end; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
free(device_id); |
| 536 |
device_id = (wchar_t *)hid_internal_get_devnode_property(usb_dev_node, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING); |
| 537 |
if (!device_id) |
| 538 |
goto end; |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
for (wchar_t *ptr = device_id + wcslen(device_id); ptr > device_id; --ptr) { |
| 545 |
|
| 546 |
|
| 547 |
if (*ptr == L'&') |
| 548 |
break; |
| 549 |
|
| 550 |
if (*ptr == L'\\') { |
| 551 |
free(dev->serial_number); |
| 552 |
dev->serial_number = _wcsdup(ptr + 1); |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
if (dev->interface_number == -1) |
| 560 |
dev->interface_number = 0; |
| 561 |
|
| 562 |
end: |
| 563 |
free(device_id); |
| 564 |
free(hardware_ids); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_node) |
| 572 |
{ |
| 573 |
if (wcslen(dev->manufacturer_string) == 0) { |
| 574 |
|
| 575 |
wchar_t* manufacturer_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_Manufacturer, DEVPROP_TYPE_STRING); |
| 576 |
if (manufacturer_string) { |
| 577 |
free(dev->manufacturer_string); |
| 578 |
dev->manufacturer_string = manufacturer_string; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
if (wcslen(dev->serial_number) == 0) { |
| 583 |
|
| 584 |
wchar_t* serial_number = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_DeviceAddress, DEVPROP_TYPE_STRING); |
| 585 |
if (serial_number) { |
| 586 |
free(dev->serial_number); |
| 587 |
dev->serial_number = serial_number; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
if (wcslen(dev->product_string) == 0) { |
| 592 |
|
| 593 |
wchar_t* product_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_ModelNumber, DEVPROP_TYPE_STRING); |
| 594 |
if (!product_string) { |
| 595 |
DEVINST parent_dev_node = 0; |
| 596 |
|
| 597 |
if (CM_Get_Parent(&parent_dev_node, dev_node, 0) == CR_SUCCESS) { |
| 598 |
|
| 599 |
product_string = (wchar_t *)hid_internal_get_devnode_property(parent_dev_node, &DEVPKEY_NAME, DEVPROP_TYPE_STRING); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
if (product_string) { |
| 604 |
free(dev->product_string); |
| 605 |
dev->product_string = product_string; |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
#define HID_API_BUS_FLAG_BLE 0x01 |
| 613 |
|
| 614 |
typedef struct hid_internal_detect_bus_type_result_ { |
| 615 |
DEVINST dev_node; |
| 616 |
hid_bus_type bus_type; |
| 617 |
unsigned int bus_flags; |
| 618 |
} hid_internal_detect_bus_type_result; |
| 619 |
|
| 620 |
static hid_internal_detect_bus_type_result hid_internal_detect_bus_type(const wchar_t* interface_path) |
| 621 |
{ |
| 622 |
wchar_t *device_id = NULL, *compatible_ids = NULL; |
| 623 |
CONFIGRET cr; |
| 624 |
DEVINST dev_node; |
| 625 |
hid_internal_detect_bus_type_result result = { 0 }; |
| 626 |
|
| 627 |
|
| 628 |
device_id = (wchar_t *)hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING); |
| 629 |
if (!device_id) |
| 630 |
goto end; |
| 631 |
|
| 632 |
|
| 633 |
cr = CM_Locate_DevNodeW(&dev_node, (DEVINSTID_W)device_id, CM_LOCATE_DEVNODE_NORMAL); |
| 634 |
if (cr != CR_SUCCESS) |
| 635 |
goto end; |
| 636 |
|
| 637 |
|
| 638 |
cr = CM_Get_Parent(&dev_node, dev_node, 0); |
| 639 |
if (cr != CR_SUCCESS) |
| 640 |
goto end; |
| 641 |
|
| 642 |
|
| 643 |
compatible_ids = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_CompatibleIds, DEVPROP_TYPE_STRING_LIST); |
| 644 |
if (!compatible_ids) |
| 645 |
goto end; |
| 646 |
|
| 647 |
|
| 648 |
for (wchar_t* compatible_id = compatible_ids; *compatible_id; compatible_id += wcslen(compatible_id) + 1) { |
| 649 |
|
| 650 |
hid_internal_towupper(compatible_id); |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
if (wcsstr(compatible_id, L"USB") != NULL) { |
| 656 |
result.bus_type = HID_API_BUS_USB; |
| 657 |
break; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
if (wcsstr(compatible_id, L"BTHENUM") != NULL) { |
| 663 |
result.bus_type = HID_API_BUS_BLUETOOTH; |
| 664 |
break; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
if (wcsstr(compatible_id, L"BTHLEDEVICE") != NULL) { |
| 669 |
result.bus_type = HID_API_BUS_BLUETOOTH; |
| 670 |
result.bus_flags |= HID_API_BUS_FLAG_BLE; |
| 671 |
break; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
if (wcsstr(compatible_id, L"PNP0C50") != NULL) { |
| 677 |
result.bus_type = HID_API_BUS_I2C; |
| 678 |
break; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
if (wcsstr(compatible_id, L"PNP0C51") != NULL) { |
| 684 |
result.bus_type = HID_API_BUS_SPI; |
| 685 |
break; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
result.dev_node = dev_node; |
| 690 |
|
| 691 |
end: |
| 692 |
free(device_id); |
| 693 |
free(compatible_ids); |
| 694 |
return result; |
| 695 |
} |
| 696 |
|
| 697 |
static char *hid_internal_UTF16toUTF8(const wchar_t *src) |
| 698 |
{ |
| 699 |
char *dst = NULL; |
| 700 |
int len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, NULL, 0, NULL, NULL); |
| 701 |
if (len) { |
| 702 |
dst = (char*)calloc(len, sizeof(char)); |
| 703 |
if (dst == NULL) { |
| 704 |
return NULL; |
| 705 |
} |
| 706 |
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, dst, len, NULL, NULL); |
| 707 |
} |
| 708 |
|
| 709 |
return dst; |
| 710 |
} |
| 711 |
|
| 712 |
static wchar_t *hid_internal_UTF8toUTF16(const char *src) |
| 713 |
{ |
| 714 |
wchar_t *dst = NULL; |
| 715 |
int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, NULL, 0); |
| 716 |
if (len) { |
| 717 |
dst = (wchar_t*)calloc(len, sizeof(wchar_t)); |
| 718 |
if (dst == NULL) { |
| 719 |
return NULL; |
| 720 |
} |
| 721 |
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, dst, len); |
| 722 |
} |
| 723 |
|
| 724 |
return dst; |
| 725 |
} |
| 726 |
|
| 727 |
static struct hid_device_info *hid_internal_get_device_info(const wchar_t *path, HANDLE handle) |
| 728 |
{ |
| 729 |
struct hid_device_info *dev = NULL; |
| 730 |
HIDD_ATTRIBUTES attrib; |
| 731 |
PHIDP_PREPARSED_DATA pp_data = NULL; |
| 732 |
HIDP_CAPS caps; |
| 733 |
wchar_t string[MAX_STRING_WCHARS + 1]; |
| 734 |
ULONG len; |
| 735 |
ULONG size; |
| 736 |
hid_internal_detect_bus_type_result detect_bus_type_result; |
| 737 |
|
| 738 |
|
| 739 |
dev = (struct hid_device_info*)calloc(1, sizeof(struct hid_device_info)); |
| 740 |
|
| 741 |
if (dev == NULL) { |
| 742 |
return NULL; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
dev->next = NULL; |
| 747 |
dev->path = hid_internal_UTF16toUTF8(path); |
| 748 |
dev->interface_number = -1; |
| 749 |
|
| 750 |
attrib.Size = sizeof(HIDD_ATTRIBUTES); |
| 751 |
if (HidD_GetAttributes(handle, &attrib)) { |
| 752 |
|
| 753 |
dev->vendor_id = attrib.VendorID; |
| 754 |
dev->product_id = attrib.ProductID; |
| 755 |
|
| 756 |
|
| 757 |
dev->release_number = attrib.VersionNumber; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
if (HidD_GetPreparsedData(handle, &pp_data)) { |
| 762 |
if (HidP_GetCaps(pp_data, &caps) == HIDP_STATUS_SUCCESS) { |
| 763 |
dev->usage_page = caps.UsagePage; |
| 764 |
dev->usage = caps.Usage; |
| 765 |
} |
| 766 |
|
| 767 |
HidD_FreePreparsedData(pp_data); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
detect_bus_type_result = hid_internal_detect_bus_type(path); |
| 772 |
dev->bus_type = detect_bus_type_result.bus_type; |
| 773 |
|
| 774 |
len = dev->bus_type == HID_API_BUS_USB ? MAX_STRING_WCHARS_USB : MAX_STRING_WCHARS; |
| 775 |
string[len] = L'\0'; |
| 776 |
size = len * sizeof(wchar_t); |
| 777 |
|
| 778 |
|
| 779 |
string[0] = L'\0'; |
| 780 |
HidD_GetSerialNumberString(handle, string, size); |
| 781 |
dev->serial_number = _wcsdup(string); |
| 782 |
|
| 783 |
|
| 784 |
string[0] = L'\0'; |
| 785 |
HidD_GetManufacturerString(handle, string, size); |
| 786 |
dev->manufacturer_string = _wcsdup(string); |
| 787 |
|
| 788 |
|
| 789 |
string[0] = L'\0'; |
| 790 |
HidD_GetProductString(handle, string, size); |
| 791 |
dev->product_string = _wcsdup(string); |
| 792 |
|
| 793 |
|
| 794 |
switch (dev->bus_type) { |
| 795 |
case HID_API_BUS_USB: |
| 796 |
hid_internal_get_usb_info(dev, detect_bus_type_result.dev_node); |
| 797 |
break; |
| 798 |
|
| 799 |
case HID_API_BUS_BLUETOOTH: |
| 800 |
if (detect_bus_type_result.bus_flags & HID_API_BUS_FLAG_BLE) |
| 801 |
hid_internal_get_ble_info(dev, detect_bus_type_result.dev_node); |
| 802 |
break; |
| 803 |
|
| 804 |
case HID_API_BUS_UNKNOWN: |
| 805 |
case HID_API_BUS_SPI: |
| 806 |
case HID_API_BUS_I2C: |
| 807 |
case HID_API_BUS_VIRTUAL: |
| 808 |
|
| 809 |
break; |
| 810 |
} |
| 811 |
|
| 812 |
return dev; |
| 813 |
} |
| 814 |
|
| 815 |
struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id) |
| 816 |
{ |
| 817 |
struct hid_device_info *root = NULL; |
| 818 |
struct hid_device_info *cur_dev = NULL; |
| 819 |
GUID interface_class_guid; |
| 820 |
CONFIGRET cr; |
| 821 |
wchar_t* device_interface_list = NULL; |
| 822 |
DWORD len; |
| 823 |
|
| 824 |
if (hid_init() < 0) { |
| 825 |
|
| 826 |
return NULL; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
HidD_GetHidGuid(&interface_class_guid); |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
do { |
| 837 |
cr = CM_Get_Device_Interface_List_SizeW(&len, &interface_class_guid, NULL, CM_GET_DEVICE_INTERFACE_LIST_PRESENT); |
| 838 |
if (cr != CR_SUCCESS) { |
| 839 |
register_global_error(L"Failed to get size of HID device interface list"); |
| 840 |
break; |
| 841 |
} |
| 842 |
|
| 843 |
if (device_interface_list != NULL) { |
| 844 |
free(device_interface_list); |
| 845 |
} |
| 846 |
|
| 847 |
device_interface_list = (wchar_t*)calloc(len, sizeof(wchar_t)); |
| 848 |
if (device_interface_list == NULL) { |
| 849 |
register_global_error(L"Failed to allocate memory for HID device interface list"); |
| 850 |
return NULL; |
| 851 |
} |
| 852 |
cr = CM_Get_Device_Interface_ListW(&interface_class_guid, NULL, device_interface_list, len, CM_GET_DEVICE_INTERFACE_LIST_PRESENT); |
| 853 |
if (cr != CR_SUCCESS && cr != CR_BUFFER_SMALL) { |
| 854 |
register_global_error(L"Failed to get HID device interface list"); |
| 855 |
} |
| 856 |
} while (cr == CR_BUFFER_SMALL); |
| 857 |
|
| 858 |
if (cr != CR_SUCCESS) { |
| 859 |
goto end_of_function; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
for (wchar_t* device_interface = device_interface_list; *device_interface; device_interface += wcslen(device_interface) + 1) { |
| 864 |
HANDLE device_handle = INVALID_HANDLE_VALUE; |
| 865 |
HIDD_ATTRIBUTES attrib; |
| 866 |
|
| 867 |
|
| 868 |
device_handle = open_device(device_interface, FALSE); |
| 869 |
|
| 870 |
|
| 871 |
if (device_handle == INVALID_HANDLE_VALUE) { |
| 872 |
|
| 873 |
continue; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
attrib.Size = sizeof(HIDD_ATTRIBUTES); |
| 878 |
if (!HidD_GetAttributes(device_handle, &attrib)) { |
| 879 |
goto cont_close; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) && |
| 885 |
(product_id == 0x0 || attrib.ProductID == product_id)) { |
| 886 |
|
| 887 |
|
| 888 |
struct hid_device_info *tmp = hid_internal_get_device_info(device_interface, device_handle); |
| 889 |
|
| 890 |
if (tmp == NULL) { |
| 891 |
goto cont_close; |
| 892 |
} |
| 893 |
|
| 894 |
if (cur_dev) { |
| 895 |
cur_dev->next = tmp; |
| 896 |
} |
| 897 |
else { |
| 898 |
root = tmp; |
| 899 |
} |
| 900 |
cur_dev = tmp; |
| 901 |
} |
| 902 |
|
| 903 |
cont_close: |
| 904 |
CloseHandle(device_handle); |
| 905 |
} |
| 906 |
|
| 907 |
if (root == NULL) { |
| 908 |
if (vendor_id == 0 && product_id == 0) { |
| 909 |
register_global_error(L"No HID devices found in the system."); |
| 910 |
} else { |
| 911 |
register_global_error(L"No HID devices with requested VID/PID found in the system."); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
end_of_function: |
| 916 |
free(device_interface_list); |
| 917 |
|
| 918 |
return root; |
| 919 |
} |
| 920 |
|
| 921 |
void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs) |
| 922 |
{ |
| 923 |
|
| 924 |
struct hid_device_info *d = devs; |
| 925 |
while (d) { |
| 926 |
struct hid_device_info *next = d->next; |
| 927 |
free(d->path); |
| 928 |
free(d->serial_number); |
| 929 |
free(d->manufacturer_string); |
| 930 |
free(d->product_string); |
| 931 |
free(d); |
| 932 |
d = next; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number) |
| 937 |
{ |
| 938 |
|
| 939 |
struct hid_device_info *devs, *cur_dev; |
| 940 |
const char *path_to_open = NULL; |
| 941 |
hid_device *handle = NULL; |
| 942 |
|
| 943 |
|
| 944 |
devs = hid_enumerate(vendor_id, product_id); |
| 945 |
if (!devs) { |
| 946 |
|
| 947 |
return NULL; |
| 948 |
} |
| 949 |
|
| 950 |
cur_dev = devs; |
| 951 |
while (cur_dev) { |
| 952 |
if (cur_dev->vendor_id == vendor_id && |
| 953 |
cur_dev->product_id == product_id) { |
| 954 |
if (serial_number) { |
| 955 |
if (cur_dev->serial_number && wcscmp(serial_number, cur_dev->serial_number) == 0) { |
| 956 |
path_to_open = cur_dev->path; |
| 957 |
break; |
| 958 |
} |
| 959 |
} |
| 960 |
else { |
| 961 |
path_to_open = cur_dev->path; |
| 962 |
break; |
| 963 |
} |
| 964 |
} |
| 965 |
cur_dev = cur_dev->next; |
| 966 |
} |
| 967 |
|
| 968 |
if (path_to_open) { |
| 969 |
|
| 970 |
handle = hid_open_path(path_to_open); |
| 971 |
} else { |
| 972 |
register_global_error(L"Device with requested VID/PID/(SerialNumber) not found"); |
| 973 |
} |
| 974 |
|
| 975 |
hid_free_enumeration(devs); |
| 976 |
|
| 977 |
return handle; |
| 978 |
} |
| 979 |
|
| 980 |
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) |
| 981 |
{ |
| 982 |
hid_device *dev = NULL; |
| 983 |
wchar_t* interface_path = NULL; |
| 984 |
HANDLE device_handle = INVALID_HANDLE_VALUE; |
| 985 |
PHIDP_PREPARSED_DATA pp_data = NULL; |
| 986 |
HIDP_CAPS caps; |
| 987 |
|
| 988 |
if (hid_init() < 0) { |
| 989 |
|
| 990 |
goto end_of_function; |
| 991 |
} |
| 992 |
|
| 993 |
interface_path = hid_internal_UTF8toUTF16(path); |
| 994 |
if (!interface_path) { |
| 995 |
register_global_error(L"Path conversion failure"); |
| 996 |
goto end_of_function; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
device_handle = open_device(interface_path, TRUE); |
| 1001 |
|
| 1002 |
|
| 1003 |
if (device_handle == INVALID_HANDLE_VALUE) { |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
device_handle = open_device(interface_path, FALSE); |
| 1010 |
|
| 1011 |
|
| 1012 |
if (device_handle == INVALID_HANDLE_VALUE) { |
| 1013 |
register_global_winapi_error(L"open_device"); |
| 1014 |
goto end_of_function; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
if (!HidD_SetNumInputBuffers(device_handle, 64)) { |
| 1020 |
register_global_winapi_error(L"set input buffers"); |
| 1021 |
goto end_of_function; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
if (!HidD_GetPreparsedData(device_handle, &pp_data)) { |
| 1026 |
register_global_winapi_error(L"get preparsed data"); |
| 1027 |
goto end_of_function; |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (HidP_GetCaps(pp_data, &caps) != HIDP_STATUS_SUCCESS) { |
| 1031 |
register_global_error(L"HidP_GetCaps"); |
| 1032 |
goto end_of_function; |
| 1033 |
} |
| 1034 |
|
| 1035 |
dev = new_hid_device(); |
| 1036 |
|
| 1037 |
if (dev == NULL) { |
| 1038 |
register_global_error(L"hid_device allocation error"); |
| 1039 |
goto end_of_function; |
| 1040 |
} |
| 1041 |
|
| 1042 |
dev->device_handle = device_handle; |
| 1043 |
device_handle = INVALID_HANDLE_VALUE; |
| 1044 |
|
| 1045 |
dev->output_report_length = caps.OutputReportByteLength; |
| 1046 |
dev->input_report_length = caps.InputReportByteLength; |
| 1047 |
dev->feature_report_length = caps.FeatureReportByteLength; |
| 1048 |
dev->read_buf = (char*) malloc(dev->input_report_length); |
| 1049 |
dev->device_info = hid_internal_get_device_info(interface_path, dev->device_handle); |
| 1050 |
|
| 1051 |
end_of_function: |
| 1052 |
free(interface_path); |
| 1053 |
|
| 1054 |
if (device_handle != INVALID_HANDLE_VALUE) { |
| 1055 |
CloseHandle(device_handle); |
| 1056 |
} |
| 1057 |
|
| 1058 |
if (pp_data) { |
| 1059 |
HidD_FreePreparsedData(pp_data); |
| 1060 |
} |
| 1061 |
|
| 1062 |
return dev; |
| 1063 |
} |
| 1064 |
|
| 1065 |
void HID_API_EXPORT_CALL hid_winapi_set_write_timeout(hid_device *dev, unsigned long timeout) |
| 1066 |
{ |
| 1067 |
dev->write_timeout_ms = timeout; |
| 1068 |
} |
| 1069 |
|
| 1070 |
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length) |
| 1071 |
{ |
| 1072 |
DWORD bytes_written = 0; |
| 1073 |
int function_result = -1; |
| 1074 |
BOOL res; |
| 1075 |
BOOL overlapped = FALSE; |
| 1076 |
|
| 1077 |
unsigned char *buf; |
| 1078 |
|
| 1079 |
if (!data || !length) { |
| 1080 |
register_string_error(dev, L"Zero buffer/length"); |
| 1081 |
return function_result; |
| 1082 |
} |
| 1083 |
|
| 1084 |
register_string_error(dev, NULL); |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
if (length >= dev->output_report_length) { |
| 1093 |
|
| 1094 |
buf = (unsigned char *) data; |
| 1095 |
} else { |
| 1096 |
if (dev->write_buf == NULL) { |
| 1097 |
dev->write_buf = (unsigned char *) malloc(dev->output_report_length); |
| 1098 |
|
| 1099 |
if (dev->write_buf == NULL) { |
| 1100 |
register_string_error(dev, L"hid_write/malloc"); |
| 1101 |
goto end_of_function; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
buf = dev->write_buf; |
| 1106 |
memcpy(buf, data, length); |
| 1107 |
memset(buf + length, 0, dev->output_report_length - length); |
| 1108 |
length = dev->output_report_length; |
| 1109 |
} |
| 1110 |
|
| 1111 |
res = WriteFile(dev->device_handle, buf, (DWORD) length, &bytes_written, &dev->write_ol); |
| 1112 |
|
| 1113 |
if (!res) { |
| 1114 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 1115 |
|
| 1116 |
register_winapi_error(dev, L"WriteFile"); |
| 1117 |
goto end_of_function; |
| 1118 |
} |
| 1119 |
overlapped = TRUE; |
| 1120 |
} else { |
| 1121 |
|
| 1122 |
function_result = bytes_written; |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (overlapped) { |
| 1126 |
|
| 1127 |
|
| 1128 |
res = WaitForSingleObject(dev->write_ol.hEvent, dev->write_timeout_ms); |
| 1129 |
if (res != WAIT_OBJECT_0) { |
| 1130 |
|
| 1131 |
register_winapi_error(dev, L"hid_write/WaitForSingleObject"); |
| 1132 |
goto end_of_function; |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
res = GetOverlappedResult(dev->device_handle, &dev->write_ol, &bytes_written, FALSE); |
| 1137 |
if (res) { |
| 1138 |
function_result = bytes_written; |
| 1139 |
} |
| 1140 |
else { |
| 1141 |
|
| 1142 |
register_winapi_error(dev, L"hid_write/GetOverlappedResult"); |
| 1143 |
goto end_of_function; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
end_of_function: |
| 1148 |
return function_result; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) |
| 1153 |
{ |
| 1154 |
DWORD bytes_read = 0; |
| 1155 |
size_t copy_len = 0; |
| 1156 |
BOOL res = FALSE; |
| 1157 |
BOOL overlapped = FALSE; |
| 1158 |
|
| 1159 |
if (!data || !length) { |
| 1160 |
register_string_error_to_buffer(&dev->last_read_error_str, L"Zero buffer/length"); |
| 1161 |
return -1; |
| 1162 |
} |
| 1163 |
|
| 1164 |
register_string_error_to_buffer(&dev->last_read_error_str, NULL); |
| 1165 |
|
| 1166 |
|
| 1167 |
HANDLE ev = dev->ol.hEvent; |
| 1168 |
|
| 1169 |
if (!dev->read_pending) { |
| 1170 |
|
| 1171 |
dev->read_pending = TRUE; |
| 1172 |
memset(dev->read_buf, 0, dev->input_report_length); |
| 1173 |
ResetEvent(ev); |
| 1174 |
res = ReadFile(dev->device_handle, dev->read_buf, (DWORD) dev->input_report_length, &bytes_read, &dev->ol); |
| 1175 |
|
| 1176 |
if (!res) { |
| 1177 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 1178 |
|
| 1179 |
|
| 1180 |
register_winapi_error_to_buffer(&dev->last_read_error_str, L"ReadFile"); |
| 1181 |
CancelIo(dev->device_handle); |
| 1182 |
dev->read_pending = FALSE; |
| 1183 |
goto end_of_function; |
| 1184 |
} |
| 1185 |
overlapped = TRUE; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
else { |
| 1189 |
overlapped = TRUE; |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (overlapped) { |
| 1193 |
|
| 1194 |
res = WaitForSingleObject(ev, milliseconds >= 0 ? (DWORD)milliseconds : INFINITE); |
| 1195 |
if (res != WAIT_OBJECT_0) { |
| 1196 |
|
| 1197 |
|
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
res = GetOverlappedResult(dev->device_handle, &dev->ol, &bytes_read, FALSE); |
| 1206 |
} |
| 1207 |
|
| 1208 |
dev->read_pending = FALSE; |
| 1209 |
|
| 1210 |
if (res && bytes_read > 0) { |
| 1211 |
if (dev->read_buf[0] == 0x0) { |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
bytes_read--; |
| 1217 |
copy_len = length > bytes_read ? bytes_read : length; |
| 1218 |
memcpy(data, dev->read_buf+1, copy_len); |
| 1219 |
} |
| 1220 |
else { |
| 1221 |
|
| 1222 |
copy_len = length > bytes_read ? bytes_read : length; |
| 1223 |
memcpy(data, dev->read_buf, copy_len); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
if (!res) { |
| 1227 |
register_winapi_error_to_buffer(&dev->last_read_error_str, L"hid_read_timeout/GetOverlappedResult"); |
| 1228 |
} |
| 1229 |
|
| 1230 |
end_of_function: |
| 1231 |
if (!res) { |
| 1232 |
return -1; |
| 1233 |
} |
| 1234 |
|
| 1235 |
return (int) copy_len; |
| 1236 |
} |
| 1237 |
|
| 1238 |
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, size_t length) |
| 1239 |
{ |
| 1240 |
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0); |
| 1241 |
} |
| 1242 |
|
| 1243 |
HID_API_EXPORT const wchar_t * HID_API_CALL hid_read_error(hid_device *dev) |
| 1244 |
{ |
| 1245 |
if (dev->last_read_error_str == NULL) |
| 1246 |
return L"Success"; |
| 1247 |
return dev->last_read_error_str; |
| 1248 |
} |
| 1249 |
|
| 1250 |
int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *dev, int nonblock) |
| 1251 |
{ |
| 1252 |
dev->blocking = !nonblock; |
| 1253 |
return 0; |
| 1254 |
} |
| 1255 |
|
| 1256 |
int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length) |
| 1257 |
{ |
| 1258 |
BOOL res = FALSE; |
| 1259 |
unsigned char *buf; |
| 1260 |
size_t length_to_send; |
| 1261 |
|
| 1262 |
if (!data || !length) { |
| 1263 |
register_string_error(dev, L"Zero buffer/length"); |
| 1264 |
return -1; |
| 1265 |
} |
| 1266 |
|
| 1267 |
register_string_error(dev, NULL); |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
if (length >= dev->feature_report_length) { |
| 1275 |
buf = (unsigned char *) data; |
| 1276 |
length_to_send = length; |
| 1277 |
} else { |
| 1278 |
if (dev->feature_buf == NULL) { |
| 1279 |
dev->feature_buf = (unsigned char *) malloc(dev->feature_report_length); |
| 1280 |
|
| 1281 |
if (dev->feature_buf == NULL) { |
| 1282 |
register_string_error(dev, L"hid_send_feature_report/malloc"); |
| 1283 |
return -1; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
buf = dev->feature_buf; |
| 1288 |
memcpy(buf, data, length); |
| 1289 |
memset(buf + length, 0, dev->feature_report_length - length); |
| 1290 |
length_to_send = dev->feature_report_length; |
| 1291 |
} |
| 1292 |
|
| 1293 |
res = HidD_SetFeature(dev->device_handle, (PVOID)buf, (DWORD) length_to_send); |
| 1294 |
|
| 1295 |
if (!res) { |
| 1296 |
register_winapi_error(dev, L"HidD_SetFeature"); |
| 1297 |
return -1; |
| 1298 |
} |
| 1299 |
|
| 1300 |
return (int) length; |
| 1301 |
} |
| 1302 |
|
| 1303 |
static int hid_get_report(hid_device *dev, DWORD report_type, unsigned char *data, size_t length) |
| 1304 |
{ |
| 1305 |
BOOL res; |
| 1306 |
DWORD bytes_returned = 0; |
| 1307 |
|
| 1308 |
OVERLAPPED ol; |
| 1309 |
memset(&ol, 0, sizeof(ol)); |
| 1310 |
|
| 1311 |
if (!data || !length) { |
| 1312 |
register_string_error(dev, L"Zero buffer/length"); |
| 1313 |
return -1; |
| 1314 |
} |
| 1315 |
|
| 1316 |
register_string_error(dev, NULL); |
| 1317 |
|
| 1318 |
res = DeviceIoControl(dev->device_handle, |
| 1319 |
report_type, |
| 1320 |
data, (DWORD) length, |
| 1321 |
data, (DWORD) length, |
| 1322 |
&bytes_returned, &ol); |
| 1323 |
|
| 1324 |
if (!res) { |
| 1325 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 1326 |
|
| 1327 |
register_winapi_error(dev, L"Get Input/Feature Report DeviceIoControl"); |
| 1328 |
return -1; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
res = GetOverlappedResult(dev->device_handle, &ol, &bytes_returned, TRUE); |
| 1335 |
if (!res) { |
| 1336 |
|
| 1337 |
register_winapi_error(dev, L"Get Input/Feature Report GetOverLappedResult"); |
| 1338 |
return -1; |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
if (data[0] == 0x0) { |
| 1345 |
bytes_returned++; |
| 1346 |
} |
| 1347 |
|
| 1348 |
return bytes_returned; |
| 1349 |
} |
| 1350 |
|
| 1351 |
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length) |
| 1352 |
{ |
| 1353 |
|
| 1354 |
return hid_get_report(dev, IOCTL_HID_GET_FEATURE, data, length); |
| 1355 |
} |
| 1356 |
|
| 1357 |
int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device* dev, const unsigned char* data, size_t length) |
| 1358 |
{ |
| 1359 |
BOOL res = FALSE; |
| 1360 |
unsigned char *buf; |
| 1361 |
size_t length_to_send; |
| 1362 |
|
| 1363 |
if (!data || !length) { |
| 1364 |
register_string_error(dev, L"Zero buffer/length"); |
| 1365 |
return -1; |
| 1366 |
} |
| 1367 |
|
| 1368 |
register_string_error(dev, NULL); |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
if (length >= dev->output_report_length) { |
| 1376 |
buf = (unsigned char *) data; |
| 1377 |
length_to_send = length; |
| 1378 |
} else { |
| 1379 |
if (dev->write_buf == NULL) { |
| 1380 |
dev->write_buf = (unsigned char *) malloc(dev->output_report_length); |
| 1381 |
|
| 1382 |
if (dev->write_buf == NULL) { |
| 1383 |
register_string_error(dev, L"hid_send_output_report/malloc"); |
| 1384 |
return -1; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
buf = dev->write_buf; |
| 1389 |
memcpy(buf, data, length); |
| 1390 |
memset(buf + length, 0, dev->output_report_length - length); |
| 1391 |
length_to_send = dev->output_report_length; |
| 1392 |
} |
| 1393 |
|
| 1394 |
res = HidD_SetOutputReport(dev->device_handle, (PVOID)buf, (DWORD) length_to_send); |
| 1395 |
if (!res) { |
| 1396 |
register_string_error(dev, L"HidD_SetOutputReport"); |
| 1397 |
return -1; |
| 1398 |
} |
| 1399 |
|
| 1400 |
return (int) length; |
| 1401 |
} |
| 1402 |
|
| 1403 |
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length) |
| 1404 |
{ |
| 1405 |
|
| 1406 |
return hid_get_report(dev, IOCTL_HID_GET_INPUT_REPORT, data, length); |
| 1407 |
} |
| 1408 |
|
| 1409 |
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev) |
| 1410 |
{ |
| 1411 |
if (!dev) |
| 1412 |
return; |
| 1413 |
|
| 1414 |
CancelIo(dev->device_handle); |
| 1415 |
free_hid_device(dev); |
| 1416 |
} |
| 1417 |
|
| 1418 |
int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen) |
| 1419 |
{ |
| 1420 |
if (!string || !maxlen) { |
| 1421 |
register_string_error(dev, L"Zero buffer/length"); |
| 1422 |
return -1; |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (!dev->device_info) { |
| 1426 |
register_string_error(dev, L"NULL device info"); |
| 1427 |
return -1; |
| 1428 |
} |
| 1429 |
|
| 1430 |
wcsncpy(string, dev->device_info->manufacturer_string, maxlen); |
| 1431 |
string[maxlen - 1] = L'\0'; |
| 1432 |
|
| 1433 |
register_string_error(dev, NULL); |
| 1434 |
|
| 1435 |
return 0; |
| 1436 |
} |
| 1437 |
|
| 1438 |
int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen) |
| 1439 |
{ |
| 1440 |
if (!string || !maxlen) { |
| 1441 |
register_string_error(dev, L"Zero buffer/length"); |
| 1442 |
return -1; |
| 1443 |
} |
| 1444 |
|
| 1445 |
if (!dev->device_info) { |
| 1446 |
register_string_error(dev, L"NULL device info"); |
| 1447 |
return -1; |
| 1448 |
} |
| 1449 |
|
| 1450 |
wcsncpy(string, dev->device_info->product_string, maxlen); |
| 1451 |
string[maxlen - 1] = L'\0'; |
| 1452 |
|
| 1453 |
register_string_error(dev, NULL); |
| 1454 |
|
| 1455 |
return 0; |
| 1456 |
} |
| 1457 |
|
| 1458 |
int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen) |
| 1459 |
{ |
| 1460 |
if (!string || !maxlen) { |
| 1461 |
register_string_error(dev, L"Zero buffer/length"); |
| 1462 |
return -1; |
| 1463 |
} |
| 1464 |
|
| 1465 |
if (!dev->device_info) { |
| 1466 |
register_string_error(dev, L"NULL device info"); |
| 1467 |
return -1; |
| 1468 |
} |
| 1469 |
|
| 1470 |
wcsncpy(string, dev->device_info->serial_number, maxlen); |
| 1471 |
string[maxlen - 1] = L'\0'; |
| 1472 |
|
| 1473 |
register_string_error(dev, NULL); |
| 1474 |
|
| 1475 |
return 0; |
| 1476 |
} |
| 1477 |
|
| 1478 |
HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info(hid_device *dev) |
| 1479 |
{ |
| 1480 |
if (!dev->device_info) |
| 1481 |
{ |
| 1482 |
register_string_error(dev, L"NULL device info"); |
| 1483 |
return NULL; |
| 1484 |
} |
| 1485 |
|
| 1486 |
register_string_error(dev, NULL); |
| 1487 |
|
| 1488 |
return dev->device_info; |
| 1489 |
} |
| 1490 |
|
| 1491 |
int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen) |
| 1492 |
{ |
| 1493 |
BOOL res; |
| 1494 |
|
| 1495 |
if (dev->device_info && dev->device_info->bus_type == HID_API_BUS_USB && maxlen > MAX_STRING_WCHARS_USB) { |
| 1496 |
string[MAX_STRING_WCHARS_USB] = L'\0'; |
| 1497 |
maxlen = MAX_STRING_WCHARS_USB; |
| 1498 |
} |
| 1499 |
|
| 1500 |
res = HidD_GetIndexedString(dev->device_handle, string_index, string, (ULONG)maxlen * sizeof(wchar_t)); |
| 1501 |
if (!res) { |
| 1502 |
register_winapi_error(dev, L"HidD_GetIndexedString"); |
| 1503 |
return -1; |
| 1504 |
} |
| 1505 |
|
| 1506 |
register_string_error(dev, NULL); |
| 1507 |
|
| 1508 |
return 0; |
| 1509 |
} |
| 1510 |
|
| 1511 |
int HID_API_EXPORT_CALL hid_winapi_get_container_id(hid_device *dev, GUID *container_id) |
| 1512 |
{ |
| 1513 |
wchar_t *interface_path = NULL, *device_id = NULL; |
| 1514 |
CONFIGRET cr = CR_FAILURE; |
| 1515 |
DEVINST dev_node; |
| 1516 |
DEVPROPTYPE property_type; |
| 1517 |
ULONG len; |
| 1518 |
|
| 1519 |
if (!container_id) { |
| 1520 |
register_string_error(dev, L"Invalid Container ID"); |
| 1521 |
return -1; |
| 1522 |
} |
| 1523 |
|
| 1524 |
register_string_error(dev, NULL); |
| 1525 |
|
| 1526 |
interface_path = hid_internal_UTF8toUTF16(dev->device_info->path); |
| 1527 |
if (!interface_path) { |
| 1528 |
register_string_error(dev, L"Path conversion failure"); |
| 1529 |
goto end; |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
device_id = (wchar_t *)hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING); |
| 1534 |
if (!device_id) { |
| 1535 |
register_string_error(dev, L"Failed to get device interface property InstanceId"); |
| 1536 |
goto end; |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
cr = CM_Locate_DevNodeW(&dev_node, (DEVINSTID_W)device_id, CM_LOCATE_DEVNODE_NORMAL); |
| 1541 |
if (cr != CR_SUCCESS) { |
| 1542 |
register_string_error(dev, L"Failed to locate device node"); |
| 1543 |
goto end; |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
len = sizeof(*container_id); |
| 1548 |
cr = CM_Get_DevNode_PropertyW(dev_node, &DEVPKEY_Device_ContainerId, &property_type, (PBYTE)container_id, &len, 0); |
| 1549 |
if (cr == CR_SUCCESS && property_type != DEVPROP_TYPE_GUID) |
| 1550 |
cr = CR_FAILURE; |
| 1551 |
|
| 1552 |
if (cr != CR_SUCCESS) |
| 1553 |
register_string_error(dev, L"Failed to read ContainerId property from device node"); |
| 1554 |
|
| 1555 |
end: |
| 1556 |
free(interface_path); |
| 1557 |
free(device_id); |
| 1558 |
|
| 1559 |
return cr == CR_SUCCESS ? 0 : -1; |
| 1560 |
} |
| 1561 |
|
| 1562 |
|
| 1563 |
int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size) |
| 1564 |
{ |
| 1565 |
PHIDP_PREPARSED_DATA pp_data = NULL; |
| 1566 |
|
| 1567 |
if (!HidD_GetPreparsedData(dev->device_handle, &pp_data) || pp_data == NULL) { |
| 1568 |
register_string_error(dev, L"HidD_GetPreparsedData"); |
| 1569 |
return -1; |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
int res = hid_winapi_descriptor_reconstruct_pp_data(pp_data, buf, buf_size); |
| 1574 |
|
| 1575 |
HidD_FreePreparsedData(pp_data); |
| 1576 |
|
| 1577 |
if (res == 0) { |
| 1578 |
register_string_error(dev, NULL); |
| 1579 |
} |
| 1580 |
else { |
| 1581 |
register_string_error(dev, L"Failed to reconstruct descriptor from PREPARSED_DATA"); |
| 1582 |
} |
| 1583 |
|
| 1584 |
return res; |
| 1585 |
} |
| 1586 |
|
| 1587 |
HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) |
| 1588 |
{ |
| 1589 |
if (dev) { |
| 1590 |
if (dev->last_error_str == NULL) |
| 1591 |
return L"Success"; |
| 1592 |
return (wchar_t*)dev->last_error_str; |
| 1593 |
} |
| 1594 |
|
| 1595 |
if (last_global_error_str == NULL) |
| 1596 |
return L"Success"; |
| 1597 |
return last_global_error_str; |
| 1598 |
} |
| 1599 |
|
| 1600 |
#ifndef hidapi_winapi_EXPORTS |
| 1601 |
#include "hidapi_descriptor_reconstruct.c" |
| 1602 |
#endif |
| 1603 |
|
| 1604 |
#ifdef __cplusplus |
| 1605 |
} |
| 1606 |
#endif |