apmorton / pyhidapi

hidapi bindings in ctypes
MIT License
111 stars 42 forks source link

usage_page and usage sometime empty #38

Closed drc0 closed 4 years ago

drc0 commented 4 years ago

When enumerating with

import hid
import time

vid = 0x123
pid = 0x456
usage_page = 0x2f11
usage_id = 0x3030

path = ''
for d in hid.enumerate():
  if d['product_id'] == pid and d['vendor_id'] == vid and d['usage_page'] == usage_page and d['usage'] == usage_id:
    path = d['path']

sometimes d['usage'] is 0 and I cant get a stable device path for the connection.

apmorton commented 4 years ago

This is likely an upstream issue. This ctypes wrapper simply returns the data from the hid_enumerate call.

Its largely going to depend on your specific platform, but you can see here how on windows the usage_page and usage values can silently fail to be retrieved and end up being 0: https://github.com/signal11/hidapi/blob/a6a622ffb680c55da0de787ff93b80280498330f/windows/hid.c#L416-L420

drc0 commented 4 years ago

Ok thank you. I'm testing on linux, and what made me wonder is that here is told to discriminate against usage_id and usage_page https://beta.docs.qmk.fm/using-qmk/software-features/feature_rawhid#host-windows-macos-linux and here is an example with node-hid https://github.com/BlankSourceCode/qmk-hid-display/blob/7e9282b92ceb2c685957f994a9f739780e3aa10b/index.js#L255, this worked perfectly but probably out of luck, now I can only communicate with the device that has interface number 1 (both with node-hid and pyhidapi) and not discriminating against usage_* , I don't know if that makes sense. Anyway feel free to close this issue if you want, and thanks for the insight.

apmorton commented 4 years ago

You definitely don't need the usage/page to open a device with hidapi - a vid/pid is enough.

The only time I could see the usage/page being useful is:

If you know the vid/pid then you can just call:

hid.enumerate(vid, pid)
drc0 commented 4 years ago

Just to add: In this case the device has multiple interface numbers: it is a promicro with qmk firmware.

{'path': b'/dev/hidraw2', 'vendor_id': 51984, 'product_id': 4403, 'serial_number': '0', 'release_number': 256, 'manufacturer_string': 'ASD', 'product_string': 'BDN9 keypad', 'usage_page': 0, 'usage': 0, 'interface_number': 0}
{'path': b'/dev/hidraw3', 'vendor_id': 51984, 'product_id': 4403, 'serial_number': '0', 'release_number': 256, 'manufacturer_string': 'ASD', 'product_string': 'BDN9 keypad', 'usage_page': 0, 'usage': 0, 'interface_number': 1}
{'path': b'/dev/hidraw4', 'vendor_id': 51984, 'product_id': 4403, 'serial_number': '0', 'release_number': 256, 'manufacturer_string': 'ASD', 'product_string': 'BDN9 keypad', 'usage_page': 0, 'usage': 0, 'interface_number': 2}
{'path': b'/dev/hidraw5', 'vendor_id': 51984, 'product_id': 4403, 'serial_number': '0', 'release_number': 256, 'manufacturer_string': 'ASD', 'product_string': 'BDN9 keypad', 'usage_page': 0, 'usage': 0, 'interface_number': 3}