[PATCH] osmocom-bb[fixeria/trx]: fake_trx/data_msg.py: use integer math for ToA

Vadim Yanitskiy gerrit-no-reply at lists.osmocom.org
Wed Feb 28 17:20:33 UTC 2018


Hello Harald Welte, Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/6990

to look at the new patch set (#2).

fake_trx/data_msg.py: use integer math for ToA

In order to avoid both float arithmetic as well as loosing any
precision, let's use integer math fot ToA (Timing of Arrival),
i.e. let's express ToA values in units of 1/256 symbol periods.

Change-Id: I56b88740f4d782ac7591fc096d1969514784a4e1
---
M src/target/fake_trx/burst_fwd.py
M src/target/fake_trx/burst_gen.py
M src/target/fake_trx/burst_send.py
M src/target/fake_trx/data_dump.py
M src/target/fake_trx/data_msg.py
5 files changed, 47 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/90/6990/2

diff --git a/src/target/fake_trx/burst_fwd.py b/src/target/fake_trx/burst_fwd.py
index db0b05b..4a0e9bb 100644
--- a/src/target/fake_trx/burst_fwd.py
+++ b/src/target/fake_trx/burst_fwd.py
@@ -41,8 +41,8 @@
 	RSSI_RAND_MAX = -60
 
 	# TODO: add options to change this
-	TOA_RAND_TRESHOLD = 0.3
-	TOA_RAND_BASE = 0.00
+	TOA256_RAND_TRESHOLD = 128
+	TOA256_RAND_BASE = 0
 
 	def __init__(self, bts_link, bb_link):
 		self.bts_link = bts_link
@@ -54,8 +54,8 @@
 		self.rssi_max = rssi + self.RSSI_RAND_TRESHOLD
 
 		# Generate a random ToA range
-		self.toa_min = self.TOA_RAND_BASE - self.TOA_RAND_TRESHOLD
-		self.toa_max = self.TOA_RAND_BASE + self.TOA_RAND_TRESHOLD
+		self.toa256_min = self.TOA256_RAND_BASE - self.TOA256_RAND_TRESHOLD
+		self.toa256_max = self.TOA256_RAND_BASE + self.TOA256_RAND_TRESHOLD
 
 	# Converts a L12TRX message to TRX2L1 message
 	def transform_msg(self, msg_raw):
@@ -73,8 +73,8 @@
 		# Randomize both RSSI and ToA values
 		msg_trx2l1.rssi = msg_trx2l1.rand_rssi(
 			min = self.rssi_min, max = self.rssi_max)
-		msg_trx2l1.toa = msg_trx2l1.rand_toa(
-			min = self.toa_min, max = self.toa_max)
+		msg_trx2l1.toa256 = msg_trx2l1.rand_toa256(
+			min = self.toa256_min, max = self.toa256_max)
 
 		return msg_trx2l1
 
diff --git a/src/target/fake_trx/burst_gen.py b/src/target/fake_trx/burst_gen.py
index c2e1ad2..179dbcd 100755
--- a/src/target/fake_trx/burst_gen.py
+++ b/src/target/fake_trx/burst_gen.py
@@ -54,8 +54,8 @@
 	tn = None
 
 	# Message specific header fields
+	toa256 = None
 	rssi = None
-	toa = None
 	pwr = None
 
 	def __init__(self):
@@ -108,8 +108,8 @@
 				msg.pwr = self.pwr
 
 			# Set time of arrival
-			if self.toa is not None:
-				msg.toa = self.toa
+			if self.toa256 is not None:
+				msg.toa256 = self.toa256
 
 			# Set RSSI
 			if self.rssi is not None:
@@ -164,7 +164,8 @@
 			 "  -t --timeslot       Set timeslot index (default random)\n"   \
 			 "     --pwr            Set power level (default random)\n"      \
 			 "     --rssi           Set RSSI (default random)\n"             \
-			 "     --toa            Set TOA (default random)\n\n"
+			 "     --toa            Set ToA in symbols (default random)\n"   \
+			 "     --toa256         Set ToA in 1/256 symbol periods\n"
 
 		print(s % (self.remote_addr, self.base_port))
 
@@ -187,6 +188,7 @@
 					"timeslot=",
 					"rssi=",
 					"toa=",
+					"toa256=",
 					"pwr=",
 				])
 		except getopt.GetoptError as err:
@@ -221,8 +223,10 @@
 				self.pwr = int(v)
 			elif o == "--rssi":
 				self.rssi = int(v)
+			elif o == "--toa256":
+				self.toa256 = int(v)
 			elif o == "--toa":
-				self.toa = float(v)
+				self.toa256 = int(float(v) * 256.0 + 0.5)
 
 	def check_argv(self):
 		# Check connection mode
diff --git a/src/target/fake_trx/burst_send.py b/src/target/fake_trx/burst_send.py
index 61a526f..d6c5c0c 100755
--- a/src/target/fake_trx/burst_send.py
+++ b/src/target/fake_trx/burst_send.py
@@ -91,8 +91,8 @@
 				continue
 
 			# HACK: as ToA parsing is not implemented yet,
-			# we have to use a fixed 0.00 value for now...
-			msg.toa = 0.00
+			# we have to use a fixed value for now...
+			msg.toa256 = 0
 
 			print("[i] Sending a burst %s to %s..."
 				% (msg.desc_hdr(), self.conn_mode))
diff --git a/src/target/fake_trx/data_dump.py b/src/target/fake_trx/data_dump.py
index 6102dc5..5e16da8 100644
--- a/src/target/fake_trx/data_dump.py
+++ b/src/target/fake_trx/data_dump.py
@@ -258,9 +258,9 @@
 		msg.rand_hdr()
 
 		# HACK: as ToA parsing is not implemented yet,
-		# we have to use a fixed 0.00 value for now...
+		# we have to use a fixed value for now...
 		if isinstance(msg, DATAMSG_TRX2L1):
-			msg.toa = 0.00
+			msg.toa256 = 0
 
 		# Append
 		messages_ref.append(msg)
@@ -294,8 +294,8 @@
 		assert(messages_check[i].tn == messages_ref[i].tn)
 
 		# HACK: as ToA parsing is not implemented yet,
-		# we have to use a fixed 0.00 value for now...
-		messages_check[i].toa = 0.00
+		# we have to use a fixed value for now...
+		messages_check[i].toa256 = 0
 
 		# Validate a message
 		assert(messages_check[i].validate())
@@ -320,8 +320,8 @@
 		assert(messages_check[i].tn == messages_ref[i].tn)
 
 		# HACK: as ToA parsing is not implemented yet,
-		# we have to use a fixed 0.00 value for now...
-		messages_check[i].toa = 0.00
+		# we have to use a fixed value for now...
+		messages_check[i].toa256 = 0
 
 		# Validate a message
 		assert(messages_check[i].validate())
@@ -346,9 +346,9 @@
 	assert(msg10.tn == messages_ref[10].tn)
 
 	# HACK: as ToA parsing is not implemented yet,
-	# we have to use a fixed 0.00 value for now...
-	msg0.toa = 0.00
-	msg10.toa = 0.00
+	# we have to use a fixed value for now...
+	msg0.toa256 = 0
+	msg10.toa256 = 0
 
 	# Validate both messages
 	assert(msg0.validate())
@@ -373,8 +373,8 @@
 		assert(messages_check[i].tn == messages_ref[i + 10].tn)
 
 		# HACK: as ToA parsing is not implemented yet,
-		# we have to use a fixed 0.00 value for now...
-		messages_check[i].toa = 0.00
+		# we have to use a fixed value for now...
+		messages_check[i].toa256 = 0
 
 		# Validate a message
 		assert(messages_check[i].validate())
diff --git a/src/target/fake_trx/data_msg.py b/src/target/fake_trx/data_msg.py
index 045e281..5dbebcf 100644
--- a/src/target/fake_trx/data_msg.py
+++ b/src/target/fake_trx/data_msg.py
@@ -303,12 +303,12 @@
 	RSSI_MAX = -50
 
 	# TODO: verify this range
-	TOA_MIN = -10.0
-	TOA_MAX = 10.0
+	TOA256_MIN = -256 * 10
+	TOA256_MAX = 256 * 10
 
 	# Specific message fields
 	rssi = None
-	toa = None
+	toa256 = None
 
 	# Validates the message fields
 	def validate(self):
@@ -322,10 +322,10 @@
 		if self.rssi < self.RSSI_MIN or self.rssi > self.RSSI_MAX:
 			return False
 
-		if self.toa is None:
+		if self.toa256 is None:
 			return False
 
-		if self.toa < self.TOA_MIN or self.toa > self.TOA_MAX:
+		if self.toa256 < self.TOA256_MIN or self.toa256 > self.TOA256_MAX:
 			return False
 
 		return True
@@ -341,20 +341,20 @@
 		return random.randint(min, max)
 
 	# Generates a ToA (Time of Arrival) value
-	def rand_toa(self, min = None, max = None):
+	def rand_toa256(self, min = None, max = None):
 		if min is None:
-			min = self.TOA_MIN
+			min = self.TOA256_MIN
 
 		if max is None:
-			max = self.TOA_MAX
+			max = self.TOA256_MAX
 
-		return random.uniform(min, max)
+		return random.randint(min, max)
 
 	# Randomizes message specific header
 	def rand_hdr(self):
 		DATAMSG.rand_hdr(self)
 		self.rssi = self.rand_rssi()
-		self.toa = self.rand_toa()
+		self.toa256 = self.rand_toa256()
 
 	# Generates human-readable header description
 	def desc_hdr(self):
@@ -364,8 +364,8 @@
 		if self.rssi is not None:
 			result += ("rssi=%d " % self.rssi)
 
-		if self.toa is not None:
-			result += ("toa=%.2f " % self.toa)
+		if self.toa256 is not None:
+			result += ("toa256=%d " % self.toa256)
 
 		# Strip useless whitespace and return
 		return result.strip()
@@ -378,12 +378,10 @@
 		# Put RSSI
 		buf.append(-self.rssi)
 
-		# Round ToA (Time of Arrival) to closest integer
-		toa = int(self.toa * 256.0 + 0.5)
-
-		# Encode ToA
-		buf.append((toa >> 8) & 0xff)
-		buf.append(toa & 0xff)
+		# Encode ToA (Time of Arrival)
+		# Big endian, 2 bytes (int32_t)
+		buf.append((self.toa256 >> 8) & 0xff)
+		buf.append(self.toa256 & 0xff)
 
 		return buf
 
@@ -394,7 +392,7 @@
 
 		# Parse ToA (Time of Arrival)
 		# FIXME: parsing unsupported
-		self.toa = None
+		self.toa256 = None
 
 	# Generates message specific burst
 	def gen_burst(self):
@@ -457,7 +455,7 @@
 	# Fill in message specific fields
 	msg_trx2l1_ref.rssi = -88
 	msg_l12trx_ref.pwr = 0x33
-	msg_trx2l1_ref.toa = -0.6
+	msg_trx2l1_ref.toa256 = -256
 
 	# Specify the reference bursts
 	msg_l12trx_ref.burst = burst_l12trx_ref
@@ -498,7 +496,7 @@
 	assert(msg_l12trx_dec.pwr == msg_l12trx_ref.pwr)
 
 	# FIXME: ToA check disabled until the parsing is implemented
-	# assert(msg_trx2l1_dec.toa == msg_trx2l1_ref.toa)
+	# assert(msg_trx2l1_dec.toa256 == msg_trx2l1_ref.toa256)
 
 	print("[?] Compare message specific data: OK")
 

-- 
To view, visit https://gerrit.osmocom.org/6990
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I56b88740f4d782ac7591fc096d1969514784a4e1
Gerrit-PatchSet: 2
Gerrit-Project: osmocom-bb
Gerrit-Branch: fixeria/trx
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Vadim Yanitskiy <axilirator at gmail.com>


More information about the gerrit-log mailing list