From f11f1308b1ed9f33b1373dfd132e74225d0c6c60 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 24 Jan 2023 17:59:59 +0100 Subject: [PATCH] ts_51_011: Implement Extended BCD Coding TS 51.011 specifies an "Extended BCD Coding" in Table 12 of Section 10.5.1. It allows to express the '*' and '#' symbols used in GSM SS and/or USSD codes. This improves decoding from "dialing_nr": "a753b1200f", to "dialing_nr": "*753#1200f", Change-Id: Ifcec13e9b296dba7bec34b7872192b7ce185c23c Related: OS#5784 --- pySim/ts_51_011.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pySim/ts_51_011.py b/pySim/ts_51_011.py index de49e7ab..df1bfedb 100644 --- a/pySim/ts_51_011.py +++ b/pySim/ts_51_011.py @@ -340,6 +340,22 @@ EF_SST_map = { # DF.TELECOM ###################################################################### + +# TS 51.011 Section 10.5.1 / Table 12 +class ExtendedBcdAdapter(Adapter): + """Replace some hex-characters with other ASCII characters""" + # we only translate a=* / b=# as they habe a clear representation + # in terms of USSD / SS service codes + def _decode(self, obj, context, path): + if not isinstance(obj, str): + return obj + return obj.lower().replace("a","*").replace("b","#") + + def _encode(self, obj, context, path): + if not isinstance(obj, str): + return obj + return obj.replace("*","a").replace("#","b") + # TS 51.011 Section 10.5.1 class EF_ADN(LinFixedEF): def __init__(self, fid='6f3a', sfid=None, name='EF.ADN', desc='Abbreviated Dialing Numbers', **kwargs): @@ -347,7 +363,7 @@ class EF_ADN(LinFixedEF): self._construct = Struct('alpha_id'/COptional(GsmStringAdapter(Rpad(Bytes(this._.total_len-14)), codec='ascii')), 'len_of_bcd'/Int8ub, 'ton_npi'/TonNpi, - 'dialing_nr'/BcdAdapter(Rpad(Bytes(10))), + 'dialing_nr'/ExtendedBcdAdapter(BcdAdapter(Rpad(Bytes(10)))), 'cap_conf_id'/Int8ub, 'ext1_record_id'/Int8ub)