diff options
author | Harald Welte <laforge@osmocom.org> | 2023-12-27 17:06:58 +0100 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2023-12-28 08:08:54 +0100 |
commit | 842fbdb15d50588bc427e9ed75ad720e578efb38 (patch) | |
tree | 2f294b90d42bd0593268b4eaac64ee87360263fc /pySim/construct.py | |
parent | dffe7af5789d9a59d6e12c43a449eaf2955f8443 (diff) |
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
Diffstat (limited to 'pySim/construct.py')
-rw-r--r-- | pySim/construct.py | 17 |
1 files changed, 17 insertions, 0 deletions
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 @@ class BcdAdapter(Adapter): 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 |