add PlmnAdapter for decoding PLMN bcd-strings like 262f01 to 262-01
The human representation of a PLMN is usually MCC-MNC like 262-01
or 262-001. Let's add a PlmnAdapter for use within construct, so we
can properly decode that.
Change-Id: I96f276e6dcdb54a5a3d2bcde5ee6dbaf981ed789
diff --git a/pySim/construct.py b/pySim/construct.py
index cef9557..f78adfe 100644
--- a/pySim/construct.py
+++ b/pySim/construct.py
@@ -58,6 +58,23 @@
def _encode(self, obj, context, path):
return h2b(swap_nibbles(obj))
+class PlmnAdapter(BcdAdapter):
+ """convert a bytes(3) type to BCD string like 262-02 or 262-002."""
+ def _decode(self, obj, context, path):
+ bcd = super()._decode(obj, context, path)
+ if bcd[3] == 'f':
+ return '-'.join([bcd[:3], bcd[4:]])
+ else:
+ return '-'.join([bcd[:3], bcd[3:]])
+
+ def _encode(self, obj, context, path):
+ l = obj.split('-')
+ if len(l[1]) == 2:
+ bcd = l[0] + 'f' + l[1]
+ else:
+ bcd = l[0] + l[1]
+ return super()._encode(bcd, context, path)
+
class InvertAdapter(Adapter):
"""inverse logic (false->true, true->false)."""
@staticmethod