mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-27 15:58:34 +03:00
pySim.apdu: Refactor cmd_to_dict() method
Let's factor out the "automatic processing using _tlv / _construct" as a separate method. This way we enable a derived class to first call that automatic processing method, and then amend its output in a second step. Change-Id: I1f066c0f1502020c88d99026c25bf2e283c3b4f5
This commit is contained in:
@@ -292,22 +292,26 @@ class ApduCommand(Apdu, metaclass=ApduCommandMeta):
|
|||||||
if callable(method):
|
if callable(method):
|
||||||
return method()
|
return method()
|
||||||
else:
|
else:
|
||||||
r = {}
|
return self._cmd_to_dict()
|
||||||
method = getattr(self, '_decode_p1p2', None)
|
|
||||||
if callable(method):
|
def _cmd_to_dict(self) -> Dict:
|
||||||
r = self._decode_p1p2()
|
"""back-end function performing automatic decoding using _construct / _tlv."""
|
||||||
|
r = {}
|
||||||
|
method = getattr(self, '_decode_p1p2', None)
|
||||||
|
if callable(method):
|
||||||
|
r = self._decode_p1p2()
|
||||||
|
else:
|
||||||
|
r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big'))
|
||||||
|
r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big'))
|
||||||
|
r['p3'] = self.p3
|
||||||
|
if self.cmd_data:
|
||||||
|
if self._tlv:
|
||||||
|
ie = self._tlv()
|
||||||
|
ie.from_tlv(self.cmd_data)
|
||||||
|
r['body'] = ie.to_dict()
|
||||||
else:
|
else:
|
||||||
r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big'))
|
r['body'] = parse_construct(self._construct, self.cmd_data)
|
||||||
r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big'))
|
return r
|
||||||
r['p3'] = self.p3
|
|
||||||
if self.cmd_data:
|
|
||||||
if self._tlv:
|
|
||||||
ie = self._tlv()
|
|
||||||
ie.from_tlv(self.cmd_data)
|
|
||||||
r['body'] = ie.to_dict()
|
|
||||||
else:
|
|
||||||
r['body'] = parse_construct(self._construct, self.cmd_data)
|
|
||||||
return r
|
|
||||||
|
|
||||||
def rsp_to_dict(self) -> Dict:
|
def rsp_to_dict(self) -> Dict:
|
||||||
"""Convert the Response part of the APDU to a dict."""
|
"""Convert the Response part of the APDU to a dict."""
|
||||||
|
|||||||
Reference in New Issue
Block a user