Change in pysim[master]: pySim-shell: Add suspend_uicc command

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
Wed Oct 20 11:09:51 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/25840 )


Change subject: pySim-shell: Add suspend_uicc command
......................................................................

pySim-shell: Add suspend_uicc command

This is an optional command, and it is not supported by e.g.  sysmoISIM-SJA2

Change-Id: Icc726ffd672744e56cc8dd3762891af507942c1e
---
M pySim-shell.py
M pySim/commands.py
2 files changed, 50 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/40/25840/1

diff --git a/pySim-shell.py b/pySim-shell.py
index 3286db2..42ce0a5 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -546,6 +546,19 @@
 		fcp_dec = self._cmd.rs.status()
 		self._cmd.poutput_json(fcp_dec)
 
+	suspend_uicc_parser = argparse.ArgumentParser()
+	suspend_uicc_parser.add_argument('--min-duration-secs', type=int, default=60,
+									 help='Proposed minimum duration of suspension')
+	suspend_uicc_parser.add_argument('--max-duration-secs', type=int, default=24*60*60,
+									 help='Proposed maximum duration of suspension')
+
+	# not ISO7816-4 but TS 102 221
+	@cmd2.with_argparser(suspend_uicc_parser)
+	def do_suspend_uicc(self, opts):
+		(duration, token, sw) = self._cmd.card._scc.suspend_uicc(min_len_secs=opts.min_duration_secs,
+																 max_len_secs=opts.max_duration_secs)
+		self._cmd.poutput('Negotiated Duration: %u secs, Token: %s, SW: %s' % (duration, token, sw))
+
 
 option_parser = argparse.ArgumentParser(prog='pySim-shell', description='interactive SIM card shell',
                                         formatter_class=argparse.ArgumentDefaultsHelpFormatter)
diff --git a/pySim/commands.py b/pySim/commands.py
index d53cb3e..af31e82 100644
--- a/pySim/commands.py
+++ b/pySim/commands.py
@@ -23,7 +23,7 @@
 
 from construct import *
 from pySim.construct import LV
-from pySim.utils import rpad, b2h, h2b, sw_match, bertlv_encode_len
+from pySim.utils import rpad, b2h, h2b, sw_match, bertlv_encode_len, Hexstr
 from pySim.exceptions import SwMatchError
 
 class SimCardCommands(object):
@@ -448,3 +448,39 @@
 		data_length = len(payload) // 2
 		data, sw = self._tp.send_apdu(('80100000%02x' % data_length) + payload)
 		return (data, sw)
+
+	# ETSI TS 102 221 11.1.22
+	def suspend_uicc(self, min_len_secs:int=60, max_len_secs:int=43200):
+		"""Send SUSPEND UICC to the card."""
+		def encode_duration(secs:int) -> Hexstr:
+			if secs >= 10*24*60*60:
+				return '04%02x' % (secs // (10*24*60*60))
+			elif secs >= 24*60*60:
+				return '03%02x' % (secs // (24*60*60))
+			elif secs >= 60*60:
+				return '02%02x' % (secs // (60*60))
+			elif secs >= 60:
+				return '01%02x' % (secs // 60)
+			else:
+				return '00%02x' % secs
+		def decode_duration(enc:Hexstr) -> int:
+			time_unit = enc[:2]
+			length = h2i(enc[2:4])
+			if time_unit == '04':
+				return length * 10*24*60*60
+			elif time_unit == '03':
+				return length * 24*60*60
+			elif time_unit == '02':
+				return length * 60*60
+			elif time_unit == '01':
+				return length * 60
+			elif time_unit == '00':
+				return length
+			else:
+				raise ValueError('Time unit must be 0x00..0x04')
+		min_dur_enc = encode_duration(min_len_secs)
+		max_dur_enc = encode_duration(max_len_secs)
+		data, sw = self._tp.send_apdu_checksw('8076000004' + min_dur_enc + max_dur_enc)
+		negotiated_duration_secs = decode_duration(data[:4])
+		resume_token = data[4:]
+		return (negotiated_duration_secs, resumne_token, sw)

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Icc726ffd672744e56cc8dd3762891af507942c1e
Gerrit-Change-Number: 25840
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211020/0251f53d/attachment.htm>


More information about the gerrit-log mailing list