Change in pysim[master]: pySim-shell: refactor __main__ section

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/.

dexter gerrit-no-reply at lists.osmocom.org
Fri Oct 8 08:31:34 UTC 2021


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

Change subject: pySim-shell: refactor __main__ section
......................................................................

pySim-shell: refactor __main__ section

The code in __main__ which initalizes the reader and the card and
runtime state is not so well structured. Lets put the generation of the
card and rs (RuntimeState) object into a separate function. Also do not
wait indefinetly for a card. 3 seconds should be enough. If the card or
reader did not respond until then, then there will be a problem in any
case.

Change-Id: Id2a0f2012b84ce61f5c0c14404df559fca4ddfcd
Related: SYS#5617
---
M pySim-shell.py
1 file changed, 52 insertions(+), 32 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  osmith: Looks good to me, but someone else must approve
  Jenkins Builder: Verified



diff --git a/pySim-shell.py b/pySim-shell.py
index 6699c6f..f821e41 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -50,6 +50,44 @@
 
 from pySim.card_key_provider import CardKeyProviderCsv, card_key_provider_register, card_key_provider_get_field
 
+def init_card(sl):
+	"""
+	Detect card in reader and setup card profile and runtime state. This
+	function must be called at least once on startup. The card and runtime
+	state object (rs) is required for all pySim-shell commands.
+	"""
+
+	# Wait up to three seconds for a card in reader and try to detect
+	# the card type.
+	print("Waiting for card...")
+	try:
+		sl.wait_for_card(3)
+	except NoCardError:
+		print("No card detected!")
+		return None, None;
+	except:
+		print("Card not readable!")
+		return None, None;
+
+	card = card_detect("auto", scc)
+	if card is None:
+		print("Could not detect card type!")
+		return None, None;
+
+	# Create runtime state with card profile
+	profile = CardProfileUICC()
+	profile.add_application(CardApplicationUSIM)
+	profile.add_application(CardApplicationISIM)
+	rs = RuntimeState(card, profile)
+
+	# FIXME: do this dynamically
+	rs.mf.add_file(DF_TELECOM())
+	rs.mf.add_file(DF_GSM())
+
+	# inform the transport that we can do context-specific SW interpretation
+	sl.set_sw_interpreter(rs)
+
+	return rs, card
 
 class PysimApp(cmd2.Cmd):
 	CUSTOM_CATEGORY = 'pySim Commands'
@@ -471,44 +509,12 @@
 	# Parse options
 	opts = option_parser.parse_args()
 
-	# Init card reader driver
-	sl = init_reader(opts)
-	if (sl == None):
-		exit(1)
-
-	# Create command layer
-	scc = SimCardCommands(transport=sl)
-
-	sl.wait_for_card();
-
-	card_handler = CardHandler(sl)
-
-	card = card_detect("auto", scc)
-	if card is None:
-		print("No card detected!")
-		sys.exit(2)
-
-	profile = CardProfileUICC()
-	profile.add_application(CardApplicationUSIM)
-	profile.add_application(CardApplicationISIM)
-
-	rs = RuntimeState(card, profile)
-	# inform the transport that we can do context-specific SW interpretation
-	sl.set_sw_interpreter(rs)
-
-	# FIXME: do this dynamically
-	rs.mf.add_file(DF_TELECOM())
-	rs.mf.add_file(DF_GSM())
-
 	# If a script file is specified, be sure that it actually exists
 	if opts.script:
 		if not os.access(opts.script, os.R_OK):
 			print("Invalid script file!")
 			sys.exit(2)
 
-	app = PysimApp(card, rs, opts.script)
-	rs.select('MF', app)
-
 	# Register csv-file as card data provider, either from specified CSV
 	# or from CSV file in home directory
 	csv_default = str(Path.home()) + "/.osmocom/pysim/card_data.csv"
@@ -517,6 +523,20 @@
 	if os.path.isfile(csv_default):
 		card_key_provider_register(CardKeyProviderCsv(csv_default))
 
+	# Init card reader driver
+	sl = init_reader(opts)
+	if sl is None:
+		exit(1)
+
+	# Create command layer
+	scc = SimCardCommands(transport=sl)
+
+	rs, card = init_card(sl)
+	if (rs is None or card is None):
+		exit(1)
+	app = PysimApp(card, rs, opts.script)
+	rs.select('MF', app)
+
 	# If the user supplies an ADM PIN at via commandline args authenticate
 	# immediately so that the user does not have to use the shell commands
 	pin_adm = sanitize_pin_adm(opts.pin_adm, opts.pin_adm_hex)

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Id2a0f2012b84ce61f5c0c14404df559fca4ddfcd
Gerrit-Change-Number: 25484
Gerrit-PatchSet: 11
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: neels <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-CC: fixeria <vyanitskiy at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211008/b50bc222/attachment.htm>


More information about the gerrit-log mailing list