Skip to content
This repository was archived by the owner on Jun 9, 2022. It is now read-only.

Commit 49c9a43

Browse files
committed
Merge pull request #18
2 parents 1e1bcb9 + 47b965a commit 49c9a43

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

u2flib_host/hid_transport.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,28 @@
5959
# USB Commands
6060
CMD_INIT = 0x06
6161
CMD_WINK = 0x08
62+
CMD_PING = 0x01
6263
CMD_APDU = 0x03
64+
CMD_LOCK = 0x04
6365
U2FHID_YUBIKEY_DEVICE_CONFIG = U2F_VENDOR_FIRST
6466

6567
STAT_ERR = 0xbf
6668

67-
def list_devices():
69+
70+
def list_devices(dev_class=None):
71+
dev_class = dev_class or HIDDevice
6872
devices = []
6973
for d in hid.enumerate(0, 0):
7074
usage_page = d['usage_page']
7175
if usage_page == 0xf1d0 and d['usage'] == 1:
72-
devices.append(HIDDevice(d['path']))
76+
devices.append(dev_class(d['path']))
7377
# Usage page doesn't work on Linux
7478
elif (d['vendor_id'], d['product_id']) in DEVICES:
7579
device = HIDDevice(d['path'])
7680
try:
7781
device.open()
7882
device.close()
79-
devices.append(HIDDevice(d['path']))
83+
devices.append(dev_class(d['path']))
8084
except exc.DeviceError:
8185
pass
8286
return devices
@@ -136,6 +140,15 @@ def _do_send_apdu(self, apdu_data):
136140
def wink(self):
137141
self.call(CMD_WINK)
138142

143+
def ping(self, msg=b'Hello U2F'):
144+
resp = self.call(CMD_PING, msg)
145+
if resp != msg:
146+
raise exc.DeviceError("Incorrect PING readback")
147+
return resp
148+
149+
def lock(self, lock_time=10):
150+
self.call(CMD_LOCK, chr(lock_time))
151+
139152
def _send_req(self, cid, cmd, data):
140153
size = len(data)
141154
bc_l = int2byte(size & 0xff)
@@ -160,7 +173,7 @@ def _read_resp(self, cid, cmd):
160173
resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
161174
resp = b''.join(int2byte(v) for v in resp_vals)
162175
if resp[:5] == cid + int2byte(STAT_ERR):
163-
raise U2FHIDError(byte2int(resp[6]))
176+
raise U2FHIDError(byte2int(resp[7]))
164177

165178
if not resp:
166179
raise exc.DeviceError("Invalid response from device!")

0 commit comments

Comments
 (0)