@@ -302,46 +302,74 @@ int HID_API_EXPORT hid_exit(void)
302302 return 0 ;
303303}
304304
305- static void hid_internal_get_ble_info ( struct hid_device_info * dev , DEVINST dev_node )
305+ static void * hid_internal_get_devnode_property ( DEVINST dev_node , const DEVPROPKEY * property_key , DEVPROPTYPE expected_property_type )
306306{
307- ULONG len ;
307+ ULONG len = 0 ;
308308 CONFIGRET cr ;
309309 DEVPROPTYPE property_type ;
310+ PBYTE property_value = NULL ;
311+
312+ cr = CM_Get_DevNode_PropertyW (dev_node , property_key , & property_type , NULL , & len , 0 );
313+ if (cr != CR_BUFFER_SMALL || property_type != expected_property_type )
314+ return NULL ;
315+
316+ property_value = (PBYTE )calloc (len , sizeof (BYTE ));
317+ cr = CM_Get_DevNode_PropertyW (dev_node , property_key , & property_type , property_value , & len , 0 );
318+ if (cr != CR_SUCCESS ) {
319+ free (property_value );
320+ return NULL ;
321+ }
322+
323+ return property_value ;
324+ }
325+
326+ static void * hid_internal_get_device_interface_property (const wchar_t * interface_path , const DEVPROPKEY * property_key , DEVPROPTYPE expected_property_type )
327+ {
328+ ULONG len = 0 ;
329+ CONFIGRET cr ;
330+ DEVPROPTYPE property_type ;
331+ PBYTE property_value = NULL ;
332+
333+ cr = CM_Get_Device_Interface_PropertyW (interface_path , property_key , & property_type , NULL , & len , 0 );
334+ if (cr != CR_BUFFER_SMALL || property_type != expected_property_type )
335+ return NULL ;
336+
337+ property_value = (PBYTE )calloc (len , sizeof (BYTE ));
338+ cr = CM_Get_Device_Interface_PropertyW (interface_path , property_key , & property_type , property_value , & len , 0 );
339+ if (cr != CR_SUCCESS ) {
340+ free (property_value );
341+ return NULL ;
342+ }
310343
311- static DEVPROPKEY DEVPKEY_NAME = { { 0xb725f130 , 0x47ef , 0x101a , 0xa5 , 0xf1 , 0x02 , 0x60 , 0x8c , 0x9e , 0xeb , 0xac }, 10 }; /* DEVPROP_TYPE_STRING */
312- static DEVPROPKEY PKEY_DeviceInterface_Bluetooth_DeviceAddress = { { 0x2bd67d8b , 0x8beb , 0x48d5 , 0x87 , 0xe0 , 0x6c , 0xda , 0x34 , 0x28 , 0x04 , 0x0a }, 1 }; /* DEVPROP_TYPE_STRING */
313- static DEVPROPKEY PKEY_DeviceInterface_Bluetooth_Manufacturer = { { 0x2bd67d8b , 0x8beb , 0x48d5 , 0x87 , 0xe0 , 0x6c , 0xda , 0x34 , 0x28 , 0x04 , 0x0a }, 4 }; /* DEVPROP_TYPE_STRING */
344+ return property_value ;
345+ }
314346
347+ static void hid_internal_get_ble_info (struct hid_device_info * dev , DEVINST dev_node )
348+ {
349+ wchar_t * manufacturer_string , * serial_number , * product_string ;
315350 /* Manufacturer String */
316- len = 0 ;
317- cr = CM_Get_DevNode_PropertyW (dev_node , & PKEY_DeviceInterface_Bluetooth_Manufacturer , & property_type , NULL , & len , 0 );
318- if (cr == CR_BUFFER_SMALL && property_type == DEVPROP_TYPE_STRING ) {
351+ manufacturer_string = hid_internal_get_devnode_property (dev_node , (const DEVPROPKEY * )& PKEY_DeviceInterface_Bluetooth_Manufacturer , DEVPROP_TYPE_STRING );
352+ if (manufacturer_string ) {
319353 free (dev -> manufacturer_string );
320- dev -> manufacturer_string = (wchar_t * )calloc (len , sizeof (BYTE ));
321- CM_Get_DevNode_PropertyW (dev_node , & PKEY_DeviceInterface_Bluetooth_Manufacturer , & property_type , (PBYTE )dev -> manufacturer_string , & len , 0 );
354+ dev -> manufacturer_string = manufacturer_string ;
322355 }
323356
324357 /* Serial Number String (MAC Address) */
325- len = 0 ;
326- cr = CM_Get_DevNode_PropertyW (dev_node , & PKEY_DeviceInterface_Bluetooth_DeviceAddress , & property_type , NULL , & len , 0 );
327- if (cr == CR_BUFFER_SMALL && property_type == DEVPROP_TYPE_STRING ) {
358+ serial_number = hid_internal_get_devnode_property (dev_node , (const DEVPROPKEY * )& PKEY_DeviceInterface_Bluetooth_DeviceAddress , DEVPROP_TYPE_STRING );
359+ if (serial_number ) {
328360 free (dev -> serial_number );
329- dev -> serial_number = (wchar_t * )calloc (len , sizeof (BYTE ));
330- CM_Get_DevNode_PropertyW (dev_node , & PKEY_DeviceInterface_Bluetooth_DeviceAddress , & property_type , (PBYTE )dev -> serial_number , & len , 0 );
361+ dev -> serial_number = serial_number ;
331362 }
332363
333364 /* Get devnode grandparent to reach out Bluetooth LE device node */
334- cr = CM_Get_Parent (& dev_node , dev_node , 0 );
335- if (cr != CR_SUCCESS )
365+ if (CM_Get_Parent (& dev_node , dev_node , 0 ) != CR_SUCCESS )
336366 return ;
337367
338368 /* Product String */
339- len = 0 ;
340- cr = CM_Get_DevNode_PropertyW (dev_node , & DEVPKEY_NAME , & property_type , NULL , & len , 0 );
341- if (cr == CR_BUFFER_SMALL && property_type == DEVPROP_TYPE_STRING ) {
369+ product_string = hid_internal_get_devnode_property (dev_node , & DEVPKEY_NAME , DEVPROP_TYPE_STRING );
370+ if (product_string ) {
342371 free (dev -> product_string );
343- dev -> product_string = (wchar_t * )calloc (len , sizeof (BYTE ));
344- CM_Get_DevNode_PropertyW (dev_node , & DEVPKEY_NAME , & property_type , (PBYTE )dev -> product_string , & len , 0 );
372+ dev -> product_string = product_string ;
345373 }
346374}
347375
@@ -373,23 +401,12 @@ static int hid_internal_get_interface_number(const wchar_t* hardware_id)
373401static void hid_internal_get_info (const wchar_t * interface_path , struct hid_device_info * dev )
374402{
375403 wchar_t * device_id = NULL , * compatible_ids = NULL , * hardware_ids = NULL ;
376- ULONG len ;
377404 CONFIGRET cr ;
378- DEVPROPTYPE property_type ;
379405 DEVINST dev_node ;
380406
381- static DEVPROPKEY DEVPKEY_Device_InstanceId = { { 0x78c34fc8 , 0x104a , 0x4aca , 0x9e , 0xa4 , 0x52 , 0x4d , 0x52 , 0x99 , 0x6e , 0x57 }, 256 }; /* DEVPROP_TYPE_STRING */
382- static DEVPROPKEY DEVPKEY_Device_HardwareIds = { { 0xa45c254e , 0xdf1c , 0x4efd , 0x80 , 0x20 , 0x67 , 0xd1 , 0x46 , 0xa8 , 0x50 , 0xe0 }, 3 }; /* DEVPROP_TYPE_STRING_LIST */
383- static DEVPROPKEY DEVPKEY_Device_CompatibleIds = { { 0xa45c254e , 0xdf1c , 0x4efd , 0x80 , 0x20 , 0x67 , 0xd1 , 0x46 , 0xa8 , 0x50 , 0xe0 }, 4 }; /* DEVPROP_TYPE_STRING_LIST */
384-
385407 /* Get the device id from interface path */
386- len = 0 ;
387- cr = CM_Get_Device_Interface_PropertyW (interface_path , & DEVPKEY_Device_InstanceId , & property_type , NULL , & len , 0 );
388- if (cr == CR_BUFFER_SMALL && property_type == DEVPROP_TYPE_STRING ) {
389- device_id = (wchar_t * )calloc (len , sizeof (BYTE ));
390- cr = CM_Get_Device_Interface_PropertyW (interface_path , & DEVPKEY_Device_InstanceId , & property_type , (PBYTE )device_id , & len , 0 );
391- }
392- if (cr != CR_SUCCESS )
408+ device_id = hid_internal_get_device_interface_property (interface_path , & DEVPKEY_Device_InstanceId , DEVPROP_TYPE_STRING );
409+ if (!device_id )
393410 goto end ;
394411
395412 /* Open devnode from device id */
@@ -398,13 +415,8 @@ static void hid_internal_get_info(const wchar_t* interface_path, struct hid_devi
398415 goto end ;
399416
400417 /* Get the hardware ids from devnode */
401- len = 0 ;
402- cr = CM_Get_DevNode_PropertyW (dev_node , & DEVPKEY_Device_HardwareIds , & property_type , NULL , & len , 0 );
403- if (cr == CR_BUFFER_SMALL && property_type == DEVPROP_TYPE_STRING_LIST ) {
404- hardware_ids = (wchar_t * )calloc (len , sizeof (BYTE ));
405- cr = CM_Get_DevNode_PropertyW (dev_node , & DEVPKEY_Device_HardwareIds , & property_type , (PBYTE )hardware_ids , & len , 0 );
406- }
407- if (cr != CR_SUCCESS )
418+ hardware_ids = hid_internal_get_devnode_property (dev_node , & DEVPKEY_Device_HardwareIds , DEVPROP_TYPE_STRING_LIST );
419+ if (!hardware_ids )
408420 goto end ;
409421
410422 /* Search for interface number in hardware ids */
@@ -424,13 +436,8 @@ static void hid_internal_get_info(const wchar_t* interface_path, struct hid_devi
424436 goto end ;
425437
426438 /* Get the compatible ids from parent devnode */
427- len = 0 ;
428- cr = CM_Get_DevNode_PropertyW (dev_node , & DEVPKEY_Device_CompatibleIds , & property_type , NULL , & len , 0 );
429- if (cr == CR_BUFFER_SMALL && property_type == DEVPROP_TYPE_STRING_LIST ) {
430- compatible_ids = (wchar_t * )calloc (len , sizeof (BYTE ));
431- cr = CM_Get_DevNode_PropertyW (dev_node , & DEVPKEY_Device_CompatibleIds , & property_type , (PBYTE )compatible_ids , & len , 0 );
432- }
433- if (cr != CR_SUCCESS )
439+ compatible_ids = hid_internal_get_devnode_property (dev_node , & DEVPKEY_Device_CompatibleIds , DEVPROP_TYPE_STRING_LIST );
440+ if (!compatible_ids )
434441 goto end ;
435442
436443 /* Now we can parse parent's compatible IDs to find out the device bus type */
@@ -591,7 +598,9 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor
591598
592599 /* Get the Vendor ID and Product ID for this device. */
593600 attrib .Size = sizeof (HIDD_ATTRIBUTES );
594- HidD_GetAttributes (device_handle , & attrib );
601+ if (!HidD_GetAttributes (device_handle , & attrib )) {
602+ goto cont_close ;
603+ }
595604
596605 /* Check the VID/PID to see if we should add this
597606 device to the enumeration list. */
@@ -639,7 +648,6 @@ void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *d
639648 }
640649}
641650
642-
643651HID_API_EXPORT hid_device * HID_API_CALL hid_open (unsigned short vendor_id , unsigned short product_id , const wchar_t * serial_number )
644652{
645653 /* TODO: Merge this functions with the Linux version. This function should be platform independent. */
0 commit comments