Skip to content

Commit 208e43a

Browse files
Replace NFC polling mechanism with a simple transceive operation
1 parent 2699490 commit 208e43a

File tree

1 file changed

+23
-52
lines changed

1 file changed

+23
-52
lines changed

ledgerblue/comm.py

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -196,63 +196,33 @@ def __init__(self, debug = False):
196196
self.debug = debug
197197
self.clf = nfc.ContactlessFrontend('usb')
198198
self.tag = self.clf.connect(rdwr={'on-connect': lambda tag: False})
199-
print(self.tag)
200-
if self.tag.ndef is not None:
201-
for record in self.tag.ndef.records:
202-
print(record)
203-
204-
def _exchange_write(self, apdu, timeout=TIMEOUT):
205-
success = False
206-
nb_ex = 0
207-
while success is False:
208-
try:
209-
if DEBUG_NFC_APDU:
210-
debug = bytearray([NFC_CLA, NFC_INS_WRITE, NFC_P1, NFC_P2, len(apdu)]) + apdu
211-
print(debug.hex())
212-
response = self.tag.send_apdu(NFC_CLA, NFC_INS_WRITE, NFC_P1, NFC_P2, apdu, check_status=False)
213-
if DEBUG_NFC_APDU:
214-
print(response.hex())
215-
sw = (response[-2] << 8) + response[-1]
216-
if (sw&0xF000) != 0x9000 and (sw&0xFF00) != 0x6100 and (sw&0xFF00) != 0x6C00:
217-
raise BaseException("Invalid status word received: " + hex(sw))
218-
except Type4TagCommandError as ex:
219-
if (nb_ex > 2):
220-
raise ex
221-
time.sleep(0.1)
222-
nb_ex = nb_ex+1
223-
continue
224-
success = True
225-
return response
226-
227-
def _exchange_read(self, timeout=TIMEOUT):
228-
sw = 0x6100
229-
nb_ex = 0
230-
while sw == 0x6100:
231-
try:
232-
if DEBUG_NFC_APDU:
233-
debug = bytearray([NFC_CLA, NFC_INS_READ, NFC_P1, NFC_P2])
234-
print(debug.hex())
235-
response = self.tag.send_apdu(NFC_CLA, NFC_INS_READ, NFC_P1, NFC_P2, None, check_status=False)
236-
if DEBUG_NFC_APDU:
237-
print(response.hex())
238-
sw = (response[-2] << 8) + response[-1]
239-
if (sw&0xF000) != 0x9000 and (sw&0xFF00) != 0x6100 and (sw&0xFF00) != 0x6C00:
240-
raise BaseException("Invalid status word received: " + hex(sw))
241-
except Type4TagCommandError as ex:
242-
if (nb_ex > 2):
243-
raise ex
244-
time.sleep(0.1)
245-
nb_ex = nb_ex+1
246-
time.sleep(0.001)
247-
return response
199+
# print(self.tag)
200+
# if self.tag.ndef is not None:
201+
# for record in self.tag.ndef.records:
202+
# print(record)
248203

249204
def exchange(self, apdu, timeout=TIMEOUT):
250205
if self.debug:
251206
print(f"[NFC] => {apdu.hex()}")
252-
response = self._exchange_write(apdu, timeout)
207+
response = self.tag.transceive(apdu, 5.0)
253208
sw = (response[-2] << 8) + response[-1]
254-
if response != 0x9000:
255-
response = self._exchange_read(timeout)
209+
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
210+
possibleCause = "Unknown reason"
211+
if sw == 0x6982:
212+
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
213+
if sw == 0x6985:
214+
possibleCause = "Condition of use not satisfied (denied by the user?)"
215+
if sw == 0x6a84 or sw == 0x6a85:
216+
possibleCause = "Not enough space?"
217+
if sw == 0x6a83:
218+
possibleCause = "Maybe this app requires a library to be installed first?"
219+
if sw == 0x6484:
220+
possibleCause = "Are you using the correct targetId?"
221+
if sw == 0x6d00:
222+
possibleCause = "Unexpected state of device: verify that the right application is opened?"
223+
if sw == 0x6e00:
224+
possibleCause = "Unexpected state of device: verify that the right application is opened?"
225+
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
256226
if self.debug:
257227
print(f"[NFC] <= {response.hex()}")
258228
return response
@@ -261,6 +231,7 @@ def apduMaxDataSize(self):
261231
return 255
262232

263233
def close(self):
234+
self.clf.close()
264235
pass
265236

266237
class DongleBLE(Dongle, DongleWait):

0 commit comments

Comments
 (0)