laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/33698 )
Change subject: cards: card_detect for pure UICC/SIM differentiation ......................................................................
cards: card_detect for pure UICC/SIM differentiation
Change-Id: I01009955fe1bef811a30d21ef347fb1bbccc7619 --- M pySim-shell.py M pySim/cards.py 2 files changed, 34 insertions(+), 2 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/98/33698/1
diff --git a/pySim-shell.py b/pySim-shell.py index 061ac36..4d4f2e2 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -95,7 +95,7 @@ return None, None
generic_card = False - card = card_detect("auto", scc) + card = card_detect(scc) if card is None: print("Warning: Could not detect card type - assuming a generic card type...") card = SimCardBase(scc) diff --git a/pySim/cards.py b/pySim/cards.py index 4598217..c6aee69 100644 --- a/pySim/cards.py +++ b/pySim/cards.py @@ -24,6 +24,7 @@
from typing import Optional, Dict, Tuple from pySim.ts_102_221 import EF_DIR, EF_ICCID +from pySim.ts_51_011 import DF_GSM from pySim.transport import LinkBase import abc
@@ -75,13 +76,17 @@ class SimCardBase(CardBase): """Here we only add methods for commands specified in TS 51.011, without any higher-layer processing.""" + name = 'SIM'
def __init__(self, scc: LinkBase): super(SimCardBase, self).__init__(scc) self._scc.cla_byte = "A0" self._scc.sel_ctrl = "0000"
- name = 'SIM' + def probe(self): + # EF.DIR is a mandatory EF on all ICCIDs; however it *may* also exist on a TS 51.011 SIM + df_gsm = DF_GSM() + return self.file_exists(df_gsm.fid)
class UiccCardBase(SimCardBase): @@ -94,6 +99,11 @@ # See also: ETSI TS 102 221, Table 9.3 self._adm_chv_num = 0xA0
+ def probe(self): + # EF.DIR is a mandatory EF on all ICCIDs; however it *may* also exist on a TS 51.011 SIM + ef_dir = EF_DIR() + return self.file_exists(ef_dir.fid) + def read_aids(self) -> List[Hexstr]: """Fetch all the AIDs present on UICC""" self._aids = [] @@ -155,3 +165,16 @@ # If we cannot get the full AID, try with short AID return self._scc.select_adf(aid) return (None, None) + +def card_detect(scc: LinkBase) -> Optional[CardBase]: + # UICC always has higher preference, as a UICC might also contain a SIM application + uicc = UiccCardBase(scc) + if uicc.probe(): + return uicc + + # this is for detecting a real, classic TS 11.11 SIM card without any UICC support + sim = SimCardBase(scc) + if sim.probe(): + return sim + + return None