Attention is currently required from: dexter.
fixeria has posted comments on this change by dexter. ( https://gerrit.osmocom.org/c/pysim/+/41229?usp=email )
Change subject: pySim-shell: set callback function to print formatted warnings
......................................................................
Patch Set 1:
(1 comment)
Patchset:
PS1:
Honestly, this looks overengineered to me. Especially the `Info` class, which is a `Warning` but not really a warning. Why not using the standard logging module, which does have all the necessary facilities (logging categories, logging targets, etc.)? You can then define a logging target that prints messages using `cmd2`.
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/41229?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I187f117e7e1ccdb2a85dfdfb18e84bd7561704eb
Gerrit-Change-Number: 41229
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-CC: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Sun, 19 Oct 2025 13:41:20 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Attention is currently required from: laforge, tempest, tnt, ttsou.
Hoernchen has posted comments on this change by tempest. ( https://gerrit.osmocom.org/c/osmo-trx/+/41231?usp=email )
Change subject: trx: Fix tx/rx-sps 4 for USRP1 devices
......................................................................
Patch Set 1: Code-Review+1
(1 comment)
Patchset:
PS1:
I don't have one of the arcane usrp1 devices nor the time to test it, but I suspected that it broke the arcane ancient stuff at some point. lgtm if it works for you.
--
To view, visit https://gerrit.osmocom.org/c/osmo-trx/+/41231?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Change-Id: I3b08c23a3e7d4a226cd23b9fe42558f596a63257
Gerrit-Change-Number: 41231
Gerrit-PatchSet: 1
Gerrit-Owner: tempest <jackleea1b(a)gmail.com>
Gerrit-Reviewer: Hoernchen <ewild(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: tnt <tnt(a)246tNt.com>
Gerrit-Reviewer: ttsou <tom(a)tsou.cc>
Gerrit-CC: pespin <pespin(a)sysmocom.de>
Gerrit-Attention: laforge <laforge(a)osmocom.org>
Gerrit-Attention: tempest <jackleea1b(a)gmail.com>
Gerrit-Attention: tnt <tnt(a)246tNt.com>
Gerrit-Attention: ttsou <tom(a)tsou.cc>
Gerrit-Comment-Date: Sun, 19 Oct 2025 13:09:02 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
tempest has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-trx/+/41231?usp=email )
Change subject: trx: Fix tx/rx-sps 4 for USRP1 devices
......................................................................
trx: Fix tx/rx-sps 4 for USRP1 devices
The existing code sets the pingOffset variable to -7231(269 - 7500) with tx-sps = 4. The (apparent) correct value for pingOffset for tx/rx-sps 4 is 272.
While I'm not completely sure how this patch works to fix the increased sps value, my theory is that(as seen in the block comment on lines 72-77) OpenBTS, where this code is adapted from, only supported a split sps setup(tx-sps=4, rx_sps=1 for sps=4), which created a differing decimation rate between Tx and Rx on the board. In this case, apparently you needed to account for an advance in the timestamp for a correct output.
However, in the case tx_sps == rx_sps, the decimation/sample rates will be the same, and(I guess) this advance doesn't need to be accounted for. I also had to change a line in the initialization of the Rx end of the USRP device to allow it to have a correct decimation rate when the sps values are 4.
Also commented out the line that would create different pingOffset values for split sps values, since osmo-trx won't allow nonequal values for tx-sps and rx-sps. I left it visible in the file for reference however.
Tested & working on the URAN-1, a USRP1 clone designed for GSM.
Change-Id: I3b08c23a3e7d4a226cd23b9fe42558f596a63257
---
M Transceiver52M/device/usrp1/USRPDevice.cpp
1 file changed, 8 insertions(+), 7 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/31/41231/1
diff --git a/Transceiver52M/device/usrp1/USRPDevice.cpp b/Transceiver52M/device/usrp1/USRPDevice.cpp
index 63a9bcc..21cbd7e 100644
--- a/Transceiver52M/device/usrp1/USRPDevice.cpp
+++ b/Transceiver52M/device/usrp1/USRPDevice.cpp
@@ -75,18 +75,19 @@
* split sample rate Tx/Rx - 4/1 sps we need to need to
* compensate for advance rather than delay.
*/
- if (tx_sps == 1)
- pingOffset = 272;
- else if (tx_sps == 4)
- pingOffset = 269 - 7500;
- else
- pingOffset = 0;
+ pingOffset = 272;
+
+ /* Split SPS (4/1) unsupported on USRP1
+ * if(tx_sps == 4 && rx_sps == 1)
+ * pingOffset = 269 - 7500;
+ */
#ifdef SWLOOPBACK
samplePeriod = 1.0e6/actualSampleRate;
loopbackBufferSize = 0;
gettimeofday(&lastReadTime,NULL);
firstRead = false;
+ pingOffset = 0;
#endif
}
@@ -101,7 +102,7 @@
m_uRx.reset();
try {
m_uRx = usrp_standard_rx_sptr(usrp_standard_rx::make(
- 0, decimRate * tx_sps, 1, -1,
+ 0, decimRate, 1, -1,
usrp_standard_rx::FPGA_MODE_NORMAL,
1024, 16 * 8, rbf));
m_uRx->set_fpga_master_clock_freq(masterClockRate);
--
To view, visit https://gerrit.osmocom.org/c/osmo-trx/+/41231?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Change-Id: I3b08c23a3e7d4a226cd23b9fe42558f596a63257
Gerrit-Change-Number: 41231
Gerrit-PatchSet: 1
Gerrit-Owner: tempest <jackleea1b(a)gmail.com>
tempest has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-trx/+/41230?usp=email )
Change subject: Fix tx/rx-sps 4 for USRP1 devices
......................................................................
Fix tx/rx-sps 4 for USRP1 devices
In Transceiver52M/device/usrp1/USRPDevice.cpp, the existing code sets the pingOffset variable to -7231(269 - 7500) with tx-sps = 4. The (apparent) correct value for pingOffset for tx/rx-sps 4 is 272.
While I'm not completely sure how this patch works to fix the increased sps value, my theory is that(as seen in the block comment on lines 72-77) OpenBTS, where this code is adapted from, only supported a split sps setup(tx-sps=4, rx_sps=1 for sps=4), which created a differing decimation rate between Tx and Rx on the board. In this case, apparently you needed to account for an advance in the timestamp for a correct output.
However, in the case tx_sps == rx_sps, the decimation/sample rates will be the same, and(I guess) this advance doesn't need to be accounted for. I also had to change a line in the initialization of the Rx end of the USRP device to allow it to have a correct decimation rate when the sps values are 4.
Furthermore, I commented out the line that would create different pingOffset values for split sps values, since osmo-trx won't allow nonequal values for tx-sps and rx-sps. I left it visible in the file for reference however.
Tested and working(with EDGE!) on the URAN-1, a clone of the USRP1 designed for GSM applications.
Change-Id: Ie5263b6fd00c2bf18bd1132355aeebd9b4d9ffda
---
M Transceiver52M/device/usrp1/USRPDevice.cpp
1 file changed, 8 insertions(+), 7 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/30/41230/1
diff --git a/Transceiver52M/device/usrp1/USRPDevice.cpp b/Transceiver52M/device/usrp1/USRPDevice.cpp
index 63a9bcc..fcd60c4 100644
--- a/Transceiver52M/device/usrp1/USRPDevice.cpp
+++ b/Transceiver52M/device/usrp1/USRPDevice.cpp
@@ -75,18 +75,19 @@
* split sample rate Tx/Rx - 4/1 sps we need to need to
* compensate for advance rather than delay.
*/
- if (tx_sps == 1)
- pingOffset = 272;
- else if (tx_sps == 4)
- pingOffset = 269 - 7500;
- else
- pingOffset = 0;
+ pingOffset = 272;
+
+/* Split SPS (4/1) unsupported on USRP1
+ if(tx_sps == 4 && rx_sps == 1)
+ pingOffset = 269 - 7500;
+*/
#ifdef SWLOOPBACK
samplePeriod = 1.0e6/actualSampleRate;
loopbackBufferSize = 0;
gettimeofday(&lastReadTime,NULL);
firstRead = false;
+ pingOffset = 0;
#endif
}
@@ -101,7 +102,7 @@
m_uRx.reset();
try {
m_uRx = usrp_standard_rx_sptr(usrp_standard_rx::make(
- 0, decimRate * tx_sps, 1, -1,
+ 0, decimRate, 1, -1,
usrp_standard_rx::FPGA_MODE_NORMAL,
1024, 16 * 8, rbf));
m_uRx->set_fpga_master_clock_freq(masterClockRate);
--
To view, visit https://gerrit.osmocom.org/c/osmo-trx/+/41230?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Change-Id: Ie5263b6fd00c2bf18bd1132355aeebd9b4d9ffda
Gerrit-Change-Number: 41230
Gerrit-PatchSet: 1
Gerrit-Owner: tempest <jackleea1b(a)gmail.com>
Attention is currently required from: laforge.
dexter has posted comments on this change by dexter. ( https://gerrit.osmocom.org/c/pysim/+/41228?usp=email )
Change subject: runtime: check record/file size before write
......................................................................
Patch Set 2:
(1 comment)
File pySim/runtime.py:
https://gerrit.osmocom.org/c/pysim/+/41228/comment/a6a1c1b5_0c3e4a90?usp=em… :
PS1, Line 551:
> I don't think printing is the best option. […]
I also think that just printing is not the ideal solution here. Unfortunately we do not have a reference to the app object. Also I think we have a general problem here. I have looked around. Python indeed has Warning classes which work similar to exceptions but do not disturb the program flow. I will put a patch before this one.
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/41228?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I7fa717d803ae79398d2c5daf92a7336be660c5ad
Gerrit-Change-Number: 41228
Gerrit-PatchSet: 2
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Attention: laforge <laforge(a)osmocom.org>
Gerrit-Comment-Date: Fri, 17 Oct 2025 16:08:50 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: laforge <laforge(a)osmocom.org>
Attention is currently required from: laforge.
Hello Jenkins Builder, laforge,
I'd like you to reexamine a change. Please visit
https://gerrit.osmocom.org/c/pysim/+/41228?usp=email
to look at the new patch set (#2).
The following approvals got outdated and were removed:
Code-Review+1 by laforge, Verified+1 by Jenkins Builder
Change subject: runtime: check record/file size before write
......................................................................
runtime: check record/file size before write
When writing data to a transparent or linear fixed (record oriented)
and the data to write exceeds the record/file size, then the UICC will
respond with an error "6700: Checking errors - Wrong length"
In particular when the data is supplied as a JSON object and not as a
hex string, it may not be immediately obvious to the average user what
the problem actually is.
Let's check the record/file size before writing the data and raise an
exception in case the data excieeds the record/file size. Let's also
print an informative string message in case the data length is less
than the record/file size to make the user aware of unwritten bytes
at the end of a record/file.
Related: OS#6864
Change-Id: I7fa717d803ae79398d2c5daf92a7336be660c5ad
---
M pySim/runtime.py
1 file changed, 43 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/28/41228/2
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/41228?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I7fa717d803ae79398d2c5daf92a7336be660c5ad
Gerrit-Change-Number: 41228
Gerrit-PatchSet: 2
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Attention: laforge <laforge(a)osmocom.org>
dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/41229?usp=email )
Change subject: pySim-shell: set callback function to print formatted warnings
......................................................................
pySim-shell: set callback function to print formatted warnings
In many sub modules we still use print() to occassionally print status
messages or warnings. This technically does not hurt, but it is an unclean
solution which we should replace with something more mature.
The python provided warnings module provides a warn() function that can
be used to send warnings to higher layers. The higher layers can receive
the warnings via a callback and then decide what to do with it. In our
application we will format and print the warnings using the cmd2 provided
functions (or print in case the cmd2 object does not exist yet.)
Let's also add a custom warning class "Info", which we can use to print
informative messages
To illustrate how the approach can be used in sub-modules, this also
replaces the print() calls in runtimpe.py with warn() calls.
Related: OS#6864
Change-Id: I187f117e7e1ccdb2a85dfdfb18e84bd7561704eb
---
M pySim-shell.py
M pySim/runtime.py
A pySim/warnings.py
3 files changed, 56 insertions(+), 6 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/29/41229/1
diff --git a/pySim-shell.py b/pySim-shell.py
index 977666c..6b81628 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -24,17 +24,20 @@
import re
import cmd2
+import warnings
from packaging import version
from cmd2 import style
# cmd2 >= 2.3.0 has deprecated the bg/fg in favor of Bg/Fg :(
if version.parse(cmd2.__version__) < version.parse("2.3.0"):
from cmd2 import fg, bg # pylint: disable=no-name-in-module
RED = fg.red
+ YELLOW = fg.yellow
LIGHT_RED = fg.bright_red
LIGHT_GREEN = fg.bright_green
else:
from cmd2 import Fg, Bg # pylint: disable=no-name-in-module
RED = Fg.RED
+ YELLOW = Fg.YELLOW
LIGHT_RED = Fg.LIGHT_RED
LIGHT_GREEN = Fg.LIGHT_GREEN
from cmd2 import CommandSet, with_default_category, with_argparser
@@ -67,6 +70,15 @@
from pySim.app import init_card
+def __format_warning(message, category, filename, lineno, debug=False):
+ if category.__name__ == "Info":
+ color = None
+ else:
+ color = YELLOW
+ if debug == True:
+ return style("%s: %s, in file: %s:%s" % (category.__name__, message, filename, lineno), fg=color)
+ else:
+ return style("%s: %s" % (category.__name__, message), fg=color)
class Cmd2Compat(cmd2.Cmd):
"""Backwards-compatibility wrapper around cmd2.Cmd to support older and newer
@@ -92,6 +104,9 @@
(C) 2021-2023 by Harald Welte, sysmocom - s.f.m.c. GmbH and contributors
Online manual available at https://downloads.osmocom.org/docs/pysim/master/html/shell.html """
+ def __print_warning(self, message, category, filename, lineno, file=None, line=None):
+ self.poutput(__format_warning(message, category, filename, lineno, self.debug))
+
def __init__(self, card, rs, sl, ch, script=None):
if version.parse(cmd2.__version__) < version.parse("2.0.0"):
kwargs = {'use_ipython': True}
@@ -101,6 +116,7 @@
# pylint: disable=unexpected-keyword-arg
super().__init__(persistent_history_file='~/.pysim_shell_history', allow_cli_args=False,
auto_load_commands=False, startup_script=script, **kwargs)
+ warnings.showwarning = self.__print_warning
self.intro = style(self.BANNER, fg=RED)
self.default_category = 'pySim-shell built-in commands'
self.card = None
@@ -1098,6 +1114,12 @@
if __name__ == '__main__':
+ # Ensure that we are able to print formatted warnings from the beginning. When the PysimApp is created, this
+ # this callback will be replaced with a different callback (see above)
+ def __print_warning(message, category, filename, lineno, file=None, line=None):
+ print(__format_warning(message, category, filename, lineno))
+ warnings.showwarning = __print_warning
+
startup_errors = False
opts = option_parser.parse_args()
diff --git a/pySim/runtime.py b/pySim/runtime.py
index 5bb730e..f47c053 100644
--- a/pySim/runtime.py
+++ b/pySim/runtime.py
@@ -17,11 +17,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from warnings import warn
from typing import Optional, Tuple
from osmocom.utils import h2b, i2h, is_hex, Hexstr
from osmocom.tlv import bertlv_parse_one
from pySim.exceptions import *
+from pySim.warnings import Info
from pySim.filesystem import *
def lchan_nr_from_cla(cla: int) -> int:
@@ -66,7 +68,7 @@
for addon_cls in self.profile.addons:
addon = addon_cls()
if addon.probe(self.card):
- print("Detected %s Add-on \"%s\"" % (self.profile, addon))
+ warn("Detected %s Add-on \"%s\"" % (self.profile, addon), Info)
for f in addon.files_in_mf:
self.mf.add_file(f)
@@ -100,18 +102,18 @@
apps_taken = []
if aids_card:
aids_taken = []
- print("AIDs on card:")
+ warn("AIDs on card:", Info)
for a in aids_card:
for f in apps_profile:
if f.aid in a:
- print(" %s: %s (EF.DIR)" % (f.name, a))
+ warn(" %s: %s (EF.DIR)" % (f.name, a), Info)
aids_taken.append(a)
apps_taken.append(f)
aids_unknown = set(aids_card) - set(aids_taken)
for a in aids_unknown:
- print(" unknown: %s (EF.DIR)" % a)
+ warn(" unknown: %s (EF.DIR)" % a, Info)
else:
- print("warning: EF.DIR seems to be empty!")
+ warn("EF.DIR seems to be empty!", Warning)
# Some card applications may not be registered in EF.DIR, we will actively
# probe for those applications
@@ -126,7 +128,7 @@
_data, sw = self.card.select_adf_by_aid(f.aid)
self.selected_adf = f
if sw == "9000":
- print(" %s: %s" % (f.name, f.aid))
+ warn(" %s: %s" % (f.name, f.aid), Info)
apps_taken.append(f)
except (SwMatchError, ProtocolError):
pass
diff --git a/pySim/warnings.py b/pySim/warnings.py
new file mode 100644
index 0000000..628fb81
--- /dev/null
+++ b/pySim/warnings.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+
+""" pySim: Warnings
+"""
+
+#
+# (C) 2024 by Sysmocom s.f.m.c. GmbH
+# All Rights Reserved
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+class Info(Warning):
+ """Informative message, technically not a warning."""
+
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/41229?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I187f117e7e1ccdb2a85dfdfb18e84bd7561704eb
Gerrit-Change-Number: 41229
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Attention is currently required from: dexter.
laforge has posted comments on this change by dexter. ( https://gerrit.osmocom.org/c/pysim/+/41228?usp=email )
Change subject: runtime: check record/file size before write
......................................................................
Patch Set 1: Code-Review+1
(1 comment)
File pySim/runtime.py:
https://gerrit.osmocom.org/c/pysim/+/41228/comment/3dce52c3_0b3379db?usp=em… :
PS1, Line 551:
I don't think printing is the best option. It probably works, but I think we should use the cmd2 functions for generating output, *iff* we are assuming this code is called from pySim-shell.
I'm not sure the RuntimeLchan nor the RuntimeState have any reference to the cmd_app, and that's probably a good sign of abstraction. Existing code like the select() methods are passing the cmd_app via an optional function argument. This could be used here, too. If the caller passes in cmd_app, then use that to generate output; otherwise fall back to printing.
Another option would be to define something like a "Warning" exception class and raise that from here. The pySim-shell code could then explicitly catch such an excception and print it yellow, rather than going through the normal red exception case?
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/41228?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I7fa717d803ae79398d2c5daf92a7336be660c5ad
Gerrit-Change-Number: 41228
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Fri, 17 Oct 2025 10:45:09 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/41228?usp=email )
Change subject: runtime: check record/file size before write
......................................................................
runtime: check record/file size before write
When writing data to a transparent or linear fixed (record oriented)
and the data to write exceeds the record/file size, then the UICC will
respond with an error "6700: Checking errors - Wrong length"
In particular when the data is supplied as a JSON object and not as a
hex string, it may not be immediately obvious to the average user what
the problem actually is.
Let's check the record/file size before writing the data and raise an
exception in case the data excieeds the record/file size. Let's also
print an informative string message in case the data length is less
than the record/file size to make the user aware of unwritten bytes
at the end of a record/file.
Related: OS#6864
Change-Id: I7fa717d803ae79398d2c5daf92a7336be660c5ad
---
M pySim/runtime.py
1 file changed, 43 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/28/41228/1
diff --git a/pySim/runtime.py b/pySim/runtime.py
index 5bb730e..cc6eeb2 100644
--- a/pySim/runtime.py
+++ b/pySim/runtime.py
@@ -510,6 +510,47 @@
dec_data = self.selected_file.decode_hex(data)
return (dec_data, sw)
+ def __get_writeable_size(self):
+ """ Determine the writable size (file or record) using the cached FCP parameters of the currently selected
+ file. Return None in case the writeable size cannot be determined (no FCP available, FCP lacks size
+ information).
+ """
+ fcp = self.selected_file_fcp
+ if not fcp:
+ return None
+
+ structure = fcp.get('file_descriptor', {}).get('file_descriptor_byte', {}).get('structure')
+ if not structure:
+ return None
+
+ if structure == 'transparent':
+ return fcp.get('file_size')
+ elif structure == 'linear_fixed':
+ return fcp.get('file_descriptor', {}).get('record_len')
+ else:
+ return None
+
+ def __check_writeable_size(self, data_len):
+ """ Guard against unsuccessful writes caused by attempts to write data that exceeds the file limits. """
+
+ writeable_size = self.__get_writeable_size()
+ if not writeable_size:
+ return
+
+ if isinstance(self.selected_file, TransparentEF):
+ writeable_name = "file"
+ elif isinstance(self.selected_file, LinFixedEF):
+ writeable_name = "record"
+ else:
+ writeable_name = "object"
+
+ if data_len > writeable_size:
+ raise TypeError("Data length (%u) exceeds %s size (%u) by %u bytes" %
+ (data_len, writeable_name, writeable_size, data_len - writeable_size))
+ elif data_len < writeable_size:
+ print("Data length (%u) less than %s size (%u), leaving %u unwritten bytes at the end of the %s" %
+ (data_len, writeable_name, writeable_size, writeable_size - data_len, writeable_name))
+
def update_binary(self, data_hex: str, offset: int = 0):
"""Update transparent EF binary data.
@@ -520,6 +561,7 @@
if not isinstance(self.selected_file, TransparentEF):
raise TypeError("Only works with TransparentEF, but %s is %s" % (self.selected_file,
self.selected_file.__class__.__mro__))
+ self.__check_writeable_size(len(data_hex) // 2 + offset)
return self.scc.update_binary(self.selected_file.fid, data_hex, offset, conserve=self.rs.conserve_write)
def update_binary_dec(self, data: dict):
@@ -567,6 +609,7 @@
if not isinstance(self.selected_file, LinFixedEF):
raise TypeError("Only works with Linear Fixed EF, but %s is %s" % (self.selected_file,
self.selected_file.__class__.__mro__))
+ self.__check_writeable_size(len(data_hex) // 2)
return self.scc.update_record(self.selected_file.fid, rec_nr, data_hex,
conserve=self.rs.conserve_write,
leftpad=self.selected_file.leftpad)
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/41228?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I7fa717d803ae79398d2c5daf92a7336be660c5ad
Gerrit-Change-Number: 41228
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>