Skip to content

Instantly share code, notes, and snippets.

@jonasmalacofilho
Created November 1, 2019 18:40
Show Gist options
  • Save jonasmalacofilho/344dc3fe5b4f894dd0a5924121137895 to your computer and use it in GitHub Desktop.
Save jonasmalacofilho/344dc3fe5b4f894dd0a5924121137895 to your computer and use it in GitHub Desktop.
__version__ = '0.0.1'
import sys
if sys.platform.startswith('linux'):
try:
import hidraw
hid = hidraw
except:
pass
else:
import hid
def check_iters(iters):
if not iters:
raise ValueError('Not defined for iters == []')
return iters[1:] == iters[:-1]
def print_hid_device_line(info):
vid = info['vendor_id']
pid = info['product_id']
path = info['path'].decode()
print(f'hid {vid:04x}:{pid:04x} on {path}')
if __name__ == "__main__":
errors = 0
args = sys.argv[1:]
niters = int(args[0]) if args else 3
print(f'Script to check the HID enumeration order, version {__version__}')
print(f'Iteration count is {niters}')
print()
print('Checking: enumerate')
iters = []
for i in range(niters):
print(f'iteration: {i}')
order = []
for info in hid.enumerate():
print_hid_device_line(info)
order.append(info['path'])
iters.append(order)
if check_iters(iters):
print('passed')
else:
errors += 1
print('FAILED')
print()
nhids = len(iters[0])
print(f'HID count is {nhids}')
print()
print(f'Checking: enumerate, break after {nhids//2} HIDs')
iters = []
for i in range(niters):
print(f'iteration: {i}')
order = []
cnt = 0
for info in hid.enumerate():
print_hid_device_line(info)
order.append(info['path'])
cnt += 1
if cnt == nhids // 2:
break
iters.append(order)
if check_iters(iters):
print('passed')
else:
errors += 1
print('FAILED')
print()
print(f'Checking: enumerate and open/close, break after {nhids//2} HIDs')
iters = []
for i in range(niters):
print(f'iteration: {i}')
order = []
cnt = 0
for info in hid.enumerate():
print_hid_device_line(info)
path = info['path']
dev = hid.device()
try:
dev.open_path(path)
dev.close()
except:
print(' ignoring: could not open')
order.append(path)
cnt += 1
if cnt == nhids // 2:
break
iters.append(order)
if check_iters(iters):
print('passed')
else:
errors += 1
print('FAILED')
print()
if not errors:
print('all ok')
else:
print('SOME ERRORS WHERE FOUND')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment