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
Thu Sep 16 11:13:36 UTC 2021


dexter has uploaded this change for review. ( 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
---
M pySim-shell.py
1 file changed, 54 insertions(+), 32 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/84/25484/1

diff --git a/pySim-shell.py b/pySim-shell.py
index 244ad3c..5f45cf4 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -50,6 +50,41 @@
 
 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;
+
+	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'
@@ -472,44 +507,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 = card_handler(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"
@@ -518,6 +521,25 @@
 	if os.path.isfile(csv_default):
 		card_key_provider_register(CardKeyProviderCsv(csv_default))
 
+	# Init card reader driver
+	sl = init_reader(opts)
+	if (sl == None):
+		exit(1)
+
+	# Create command layer
+	scc = SimCardCommands(transport=sl)
+
+	card_handler = card_handler(sl)
+
+	# Detect and initalize the card in the reader. This may fail when there
+	# is no card in the reader or the card is unresponsive. PysimApp is
+	# able to tolerate and recover from that.
+	rs, card = init_card(sl)
+	if (rs == None or card == 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: 1
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210916/8154a658/attachment.htm>


More information about the gerrit-log mailing list