Change in pysim[master]: utils: Introduce CommandSet abstraction

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Tue May 4 11:50:20 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/24036 )

Change subject: utils: Introduce CommandSet abstraction
......................................................................

utils: Introduce CommandSet abstraction

This will allow us to match INS -> name and add more related
bits in the future (e.g. for decoding APDU traces)

Change-Id: I314ff15186dc05778ea12363cac0a310b6c7713c
---
M pySim/utils.py
1 file changed, 66 insertions(+), 0 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/pySim/utils.py b/pySim/utils.py
index fc6ebfd..3e67386 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -1216,3 +1216,69 @@
             encoded += e.encode(decoded[i])
             i += 1
         return encoded
+
+class CardCommand:
+    """A single card command / instruction."""
+    def __init__(self, name, ins, cla_list=None, desc=None):
+        self.name = name
+        self.ins = ins
+        self.cla_list = cla_list or []
+        self.cla_list = [x.lower() for x in self.cla_list]
+        self.desc = desc
+
+    def __str__(self):
+        return self.name
+
+    def __repr__(self):
+        return '%s(INS=%02x,CLA=%s)' % (self.name, self.ins, self.cla_list)
+
+    def match_cla(self, cla):
+        """Does the given CLA match the CLA list of the command?."""
+        if not isinstance(cla, str):
+            cla = '%02u' % cla
+        cla = cla.lower()
+        for cla_match in self.cla_list:
+            cla_masked = ""
+            for i in range(0, 2):
+                if cla_match[i] == 'x':
+                    cla_masked += 'x'
+                else:
+                    cla_masked += cla[i]
+            if cla_masked == cla_match:
+                return True
+        return False
+
+
+class CardCommandSet:
+    """A set of card instructions, typically specified within one spec."""
+    def __init__(self, name, cmds=[]):
+        self.name = name
+        self.cmds = { c.ins : c for c in cmds }
+
+    def __str__(self):
+        return self.name
+
+    def __getitem__(self, idx):
+        return self.cmds[idx]
+
+    def __add__(self, other):
+        if isinstance(other, CardCommand):
+            if other.ins in self.cmds:
+                raise ValueError('%s: INS 0x%02x already defined: %s' %
+                                 (self, other.ins, self.cmds[other.ins]))
+            self.cmds[other.ins] = other
+        elif isinstance(other, CardCommandSet):
+            for c in other.cmds.keys():
+                self.cmds[c] = other.cmds[c]
+        else:
+            raise ValueError('%s: Unsupported type to add operator: %s' % (self, other))
+
+    def lookup(self, ins, cla=None):
+        """look-up the command within the CommandSet."""
+        ins = int(ins)
+        if not ins in self.cmds:
+            return None
+        cmd = self.cmds[ins]
+        if cla and not cmd.match_cla(cla):
+            return None
+        return cmd

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/24036
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I314ff15186dc05778ea12363cac0a310b6c7713c
Gerrit-Change-Number: 24036
Gerrit-PatchSet: 5
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210504/b2ecb6de/attachment.htm>


More information about the gerrit-log mailing list