From 47485362af6218e8385c39285c5e7c91916d7385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Sun, 28 Feb 2010 06:09:59 +0100 Subject: [PATCH] + Add parse_response_4B + Limit read binary response in PN532 case to 0xf8 bytes --- cards/pn532_card.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ readers.py | 3 ++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/cards/pn532_card.py b/cards/pn532_card.py index 464ee42..659c9fc 100644 --- a/cards/pn532_card.py +++ b/cards/pn532_card.py @@ -11,6 +11,9 @@ class PN532_Virtual_Card(Card): APDU_TRANSCEIVE_PN532 = C_APDU(cla=0xff, ins=0, p1=0, p2=0) + # The ACR122 has a maximum response data size of 0xf8 + APDU_READ_BINARY = C_APDU(cla=0, ins=0xb0, le=0xf8) + def cmd_pn532(self, *cmd): "Transmit a command to the PN532 and receive the response" result = self.pn532_transceive(binascii.unhexlify("".join("".join(cmd).split()))) @@ -25,6 +28,12 @@ class PN532_Virtual_Card(Card): self.cmd_pn532("d4 4a 01 00") def pn532_transceive(self, cmd): + if len(cmd) > 1: + if cmd[0] == "\xd4": + s = "pn532_prepare_command_%02X" % ord(cmd[1]) + if hasattr(self, s): + cmd = getattr(self, s)(cmd) + if hasattr(self.reader, "pn532_transceive_raw"): return R_APDU(self.reader.pn532_transceive_raw(cmd)) else: @@ -82,6 +91,42 @@ class PN532_Virtual_Card(Card): result.append( "SAM status: %02X" % ord(response[-1]) ) return result + def pn532_prepare_command_4A(self, cmd): + if len(cmd) > 3: + self._last_baudrate_polled = ord(cmd[3]) + else: + self._last_baudrate_polled = None + return cmd + + def pn532_parse_response_4B(self, response): + response = map(ord, response) + numtg = response[0] + result = ["Targets detected: %i" % numtg] + pos = 1 + last_pos = pos + + while pos < len(response): + if self._last_baudrate_polled == 0: + s = "Target %i: ISO 14443-A, SENS_RES: %02X %02X, SEL_RES: %02X" % \ + ( response[pos], response[pos+1], response[pos+2], response[pos+3] ) + pos = pos + 4 + if response[pos] > 0: + s = s+", NFCID (%02X bytes): %s" % (response[pos], " ".join(map(lambda a: "%02X" % a, response[pos+1:(pos+1+response[pos])]))) + pos = pos + response[pos] + pos = pos + 1 # NFCID length does not count length byte + if len(response) > pos and response[pos] > 0: + s = s+", ATS (%02X bytes): %s" % (response[pos], " ".join(map(lambda a: "%02X" % a, response[pos:(pos+response[pos])]))) + pos = pos + response[pos] + # ATS length does count length byte + + result.append(s) + + if last_pos == pos: + print "Implementation error, no advance, abort" + break + + return result + def can_handle(cls, reader): """Determine whether this class can handle a given reader object.""" if reader.name.startswith("ACS ACR 38U-CCID"): diff --git a/readers.py b/readers.py index cd1f949..f34ebd9 100644 --- a/readers.py +++ b/readers.py @@ -167,12 +167,13 @@ class ACR122_Reader(Smartcard_Reader): if ord(response[2]) > 0: return True else: - response = self.pn532_transceive("\xd4\x4a\x01\x03") + response = self.pn532_transceive("\xd4\x4a\x01\x03\x00") if ord(response[2]) > 0: return True def _internal_connect(self): self._parent.connect() + self.pn532_transceive("\xd4\x32\x05\x00\x00\x00") self.pn532_acquire_card() yield self._CONNECT_DONE