5959# USB Commands
6060CMD_INIT = 0x06
6161CMD_WINK = 0x08
62+ CMD_PING = 0x01
6263CMD_APDU = 0x03
64+ CMD_LOCK = 0x04
6365U2FHID_YUBIKEY_DEVICE_CONFIG = U2F_VENDOR_FIRST
6466
6567STAT_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