Skip to content

Commit

Permalink
Add generic response chaining capability
Browse files Browse the repository at this point in the history
  • Loading branch information
henryk committed Oct 15, 2010
1 parent 2cc3781 commit 20b8d81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cards/generic_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ def _real_send(self, apdu):
def _send_with_retry(self, apdu):
result = self._real_send(apdu)

if self.check_sw(result.sw, self.PURPOSE_GET_RESPONSE):
while self.check_sw(result.sw, self.PURPOSE_GET_RESPONSE):
## Need to call GetResponse
gr_apdu = self.COMMAND_GET_RESPONSE
result = self._real_send(gr_apdu)
tmp = self._real_send(gr_apdu)

if not callable(result.append):
result = tmp
break
else:
result = result.append(tmp)

return result

Expand Down
9 changes: 9 additions & 0 deletions tests/utilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ def testCreateSequence(self):

self.assertEqual(self.a4.render(), a4_2.render())

class APDUChainTests(unittest.TestCase):

def testChain(self):
a = utils.R_APDU("abcd\x61\x04")
b = utils.R_APDU("efgh\x90\x00")

c = a.append(b)

self.assertEqual("abcdefgh\x90\x00", c.render())
7 changes: 7 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ def __repr__(self):
parts.append("data=%r" % self.data)

return "%s(%s)" % (self.__class__.__name__, ", ".join(parts))

# Stub for implementation in subclasses
# Semantics should be: c=a.append(b) <=> c.data == a.data + b.data and c.status == b.status
append = None

class Command_Frame(Transmission_Frame):
pass
Expand Down Expand Up @@ -472,6 +476,9 @@ def _format_fields(self):
def render(self):
"Return this APDU as a binary string"
return self.data + self.sw

def append(self, other):
return R_APDU(self.data + other.data + other.sw)

APDU.COMMAND_CLASS = C_APDU
APDU.RESPONSE_CLASS = R_APDU
Expand Down

0 comments on commit 20b8d81

Please sign in to comment.