I do some experiments using hidapi lib to read controller/joystick data. Currently I am facing a problem when using PS4 Controller (might be a Problem using other devices too).
On Windows (I guess on Linux too) you can connect the controller via USB and Bluetooth. When enumerating over the devices using hid api lib it detectes the controller twice, one on USB Bus and a second one on BT Bus. The problem is that there is no way to find out which BT device and USB device belongs to each other.
Does have anyone an idea?
EDIT:
first controller
BT:
path "\\\\?\\HID#{00001124-0000-1000-8000-00805f9b34fb}_VID&0002054c_PID&09cc#8&12e42c53&4&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"
vendor_id 1356
product_id 2508
serial_number "dc0c2d6f3597"
release_number 0
manufacturer_string "Sony Interactive Entertainment"
product_string "Wireless Controller"
usage_page 1
usage 5
interface_number -1
bus_type HID_API_BUS_BLUETOOTH
USB:
path "\\\\?\\HID#VID_054C&PID_09CC&MI_03#9&2a666ca3&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"
vendor_id 1356
product_id 2508
serial_number ""
release_number 256
manufacturer_string "Sony Interactive Entertainment"
product_string "Wireless Controller"
usage_page 1
usage 5
interface_number 3
bus_type HID_API_BUS_USB
second controller
BT:
path "\\\\?\\HID#{00001124-0000-1000-8000-00805f9b34fb}_VID&0002054c_PID&09cc#8&2e3024ab&1&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"
vendor_id 1356
product_id 2508
serial_number "dc0c2d6be031"
release_number 0
manufacturer_string "Sony Interactive Entertainment"
product_string "Wireless Controller"
usage_page 1
usage 5
interface_number -1
bus_type HID_API_BUS_BLUETOOTH
USB:
path "\\\\?\\HID#VID_054C&PID_09CC&MI_03#a&150a354e&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"
vendor_id 1356
product_id 2508
serial_number ""
release_number 256
manufacturer_string "Sony Interactive Entertainment"
product_string "Wireless Controller"
usage_page 1
usage 5
interface_number 3
bus_type HID_API_BUS_USB
I found a solution.
there is a unique triple vendor_id, product_id and serial_number. If it is the same device it does have the same ids. Bluetooth seems to sends a serial number every time (at least with all other controllers I tested) while USB is rarely set. For DualShock 4 there is a report to obtain the MAC address which is similar to the serial number
struct ReportFeatureInMacAll {
uint8_t ReportID = 0x12; // (0x09 for BT)
uint8_t ClientMac[6];
uint8_t Hard08;
uint8_t Hard25;
uint8_t Hard00;
uint8_t HostMac[6];
}
auto dev = hid_open_path(hidDeviceInfo->path);
ReportFeatureInMacAll report;
hid_get_feature_report(dev, (uint8_t *)&report, sizeof(report));
hid_close(dev);
Now I am able to distinguish between them.