Change in pysim[master]: pySim-read.py: Added a common card detection function for both pySim-...

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
Fri Mar 27 17:22:57 UTC 2020


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

Change subject: pySim-read.py: Added a common card detection function for both pySim-prog.py and pySim-read.py
......................................................................

pySim-read.py: Added a common card detection function for both pySim-prog.py and pySim-read.py

This function is used to detect the card type and return Card class/Card subclasses object
if its a know card or else None. Also, an initial step towards refactoring of code.

Change-Id: I71f57c6403dc933bd9d54f90df3d3fe105b4f66f
---
M pySim-prog.py
M pySim-read.py
M pySim/cards.py
M pysim-testdata/Fairwaves-SIM.ok
M pysim-testdata/Wavemobile-SIM.ok
M pysim-testdata/fakemagicsim.ok
M pysim-testdata/sysmoISIM-SJA2.ok
M pysim-testdata/sysmoUSIM-SJS1.ok
M pysim-testdata/sysmosim-gr1.ok
9 files changed, 44 insertions(+), 36 deletions(-)

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



diff --git a/pySim-prog.py b/pySim-prog.py
index a7081ff..e3045a6 100755
--- a/pySim-prog.py
+++ b/pySim-prog.py
@@ -39,7 +39,7 @@
 	import simplejson as json
 
 from pySim.commands import SimCardCommands
-from pySim.cards import _cards_classes
+from pySim.cards import _cards_classes, card_detect
 from pySim.utils import h2b, swap_nibbles, rpad, derive_milenage_opc, calculate_luhn, dec_iccid
 from pySim.ts_51_011 import EF
 from pySim.card_handler import *
@@ -609,36 +609,6 @@
 	fh.close()
 
 
-def card_detect(opts, scc):
-
-	# Detect type if needed
-	card = None
-	ctypes = dict([(kls.name, kls) for kls in _cards_classes])
-
-	if opts.type in ("auto", "auto_once"):
-		for kls in _cards_classes:
-			card = kls.autodetect(scc)
-			if card:
-				print("Autodetected card type: %s" % card.name)
-				card.reset()
-				break
-
-		if card is None:
-			print("Autodetection failed")
-			return
-
-		if opts.type == "auto_once":
-			opts.type = card.name
-
-	elif opts.type in ctypes:
-		card = ctypes[opts.type](scc)
-
-	else:
-		raise ValueError("Unknown card type: %s" % opts.type)
-
-	return card
-
-
 def process_card(opts, first, card_handler):
 
 	if opts.dry_run is False:
@@ -647,7 +617,7 @@
 
 	if opts.dry_run is False:
 		# Get card
-		card = card_detect(opts, scc)
+		card = card_detect(opts.type, scc)
 		if card is None:
 			print("No card detected!")
 			return -1
diff --git a/pySim-read.py b/pySim-read.py
index 55b43d7..bad2047 100755
--- a/pySim-read.py
+++ b/pySim-read.py
@@ -31,6 +31,7 @@
 from pySim.ts_51_011 import EF, DF
 
 from pySim.commands import SimCardCommands
+from pySim.cards import card_detect, Card
 from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn, format_xplmn_w_act, dec_spn
 
 
@@ -94,6 +95,9 @@
 	# Program the card
 	print("Reading ...")
 
+	# Initialize Card object by auto detecting the card
+	card = card_detect("auto", scc) or Card(scc)
+
 	# EF.ICCID
 	(res, sw) = scc.read_binary(EF['ICCID'])
 	if sw == '9000':
diff --git a/pySim/cards.py b/pySim/cards.py
index fe7f0fd..a43da9c 100644
--- a/pySim/cards.py
+++ b/pySim/cards.py
@@ -1104,3 +1104,31 @@
 			card.reset()
 			return card
 	return None
+
+def card_detect(ctype, scc):
+	# Detect type if needed
+	card = None
+	ctypes = dict([(kls.name, kls) for kls in _cards_classes])
+
+	if ctype in ("auto", "auto_once"):
+		for kls in _cards_classes:
+			card = kls.autodetect(scc)
+			if card:
+				print("Autodetected card type: %s" % card.name)
+				card.reset()
+				break
+
+		if card is None:
+			print("Autodetection failed")
+			return None
+
+		if ctype == "auto_once":
+			ctype = card.name
+
+	elif ctype in ctypes:
+		card = ctypes[ctype](scc)
+
+	else:
+		raise ValueError("Unknown card type: %s" % ctype)
+
+	return card
diff --git a/pysim-testdata/Fairwaves-SIM.ok b/pysim-testdata/Fairwaves-SIM.ok
index dfcf05d..ffe9c42 100644
--- a/pysim-testdata/Fairwaves-SIM.ok
+++ b/pysim-testdata/Fairwaves-SIM.ok
@@ -1,5 +1,6 @@
 Using PC/SC reader (dev=0) interface
 Reading ...
+Autodetected card type: Fairwaves-SIM
 ICCID: 8988219000000117833
 IMSI: 001010000000111
 GID1: ffffffffffffffff
diff --git a/pysim-testdata/Wavemobile-SIM.ok b/pysim-testdata/Wavemobile-SIM.ok
index 8447023..03191b3 100644
--- a/pysim-testdata/Wavemobile-SIM.ok
+++ b/pysim-testdata/Wavemobile-SIM.ok
@@ -1,9 +1,10 @@
-Using PC/SC reader (dev=4) interface
+Using PC/SC reader (dev=3) interface
 Reading ...
+Autodetected card type: Wavemobile-SIM
 ICCID: 89445310150011013678
 IMSI: 001010000000102
-GID1: Can't read file -- SW match failed! Expected 9000 and got 9404.
-GID2: Can't read file -- SW match failed! Expected 9000 and got 9404.
+GID1: Can't read file -- SW match failed! Expected 9000 and got 6a82.
+GID2: Can't read file -- SW match failed! Expected 9000 and got 6a82.
 SMSP: e1ffffffffffffffffffffffff0581005155f5ffffffffffff000000ffffffffffffffffffffffffffff
 SPN: wavemobile
 Display HPLMN: False
@@ -45,7 +46,7 @@
 	ffffff0000 # unused
 	ffffff0000 # unused
 
-HPLMNAcT: Can't read file -- SW match failed! Expected 9000 and got 9404.
+HPLMNAcT: Can't read file -- SW match failed! Expected 9000 and got 6a82.
 ACC: abce
 MSISDN: Not available
 AD: 00ffff02
diff --git a/pysim-testdata/fakemagicsim.ok b/pysim-testdata/fakemagicsim.ok
index 816b3dc..4ee5c7f 100644
--- a/pysim-testdata/fakemagicsim.ok
+++ b/pysim-testdata/fakemagicsim.ok
@@ -1,5 +1,6 @@
 Using PC/SC reader (dev=2) interface
 Reading ...
+Autodetected card type: fakemagicsim
 ICCID: 1122334455667788990
 IMSI: 001010000000102
 GID1: Can't read file -- SW match failed! Expected 9000 and got 9404.
diff --git a/pysim-testdata/sysmoISIM-SJA2.ok b/pysim-testdata/sysmoISIM-SJA2.ok
index ce4ce7c..0ccb8e9 100644
--- a/pysim-testdata/sysmoISIM-SJA2.ok
+++ b/pysim-testdata/sysmoISIM-SJA2.ok
@@ -1,5 +1,6 @@
 Using PC/SC reader (dev=0) interface
 Reading ...
+Autodetected card type: sysmoISIM-SJA2
 ICCID: 8988211900000000004
 IMSI: 001010000000102
 GID1: ffffffffffffffffffff
diff --git a/pysim-testdata/sysmoUSIM-SJS1.ok b/pysim-testdata/sysmoUSIM-SJS1.ok
index 83a8e9e..0299e3f 100644
--- a/pysim-testdata/sysmoUSIM-SJS1.ok
+++ b/pysim-testdata/sysmoUSIM-SJS1.ok
@@ -1,5 +1,6 @@
 Using PC/SC reader (dev=1) interface
 Reading ...
+Autodetected card type: sysmoUSIM-SJS1
 ICCID: 1122334455667788990
 IMSI: 001010000000102
 GID1: ffffffffffffffffffff
diff --git a/pysim-testdata/sysmosim-gr1.ok b/pysim-testdata/sysmosim-gr1.ok
index 50f949d..04e4965 100644
--- a/pysim-testdata/sysmosim-gr1.ok
+++ b/pysim-testdata/sysmosim-gr1.ok
@@ -1,5 +1,6 @@
 Using PC/SC reader (dev=3) interface
 Reading ...
+Autodetected card type: sysmosim-gr1
 ICCID: 1122334455667788990
 IMSI: 001010000000102
 GID1: Can't read file -- SW match failed! Expected 9000 and got 9404.

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I71f57c6403dc933bd9d54f90df3d3fe105b4f66f
Gerrit-Change-Number: 17520
Gerrit-PatchSet: 6
Gerrit-Owner: herlesupreeth <herlesupreeth at gmail.com>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: fixeria <axilirator at gmail.com>
Gerrit-Reviewer: herlesupreeth <herlesupreeth at gmail.com>
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/20200327/12aedb71/attachment.htm>


More information about the gerrit-log mailing list