python/trx: introduce and use Transceiver class

Change-Id: I6dc88edbb69a68746cc8e01206dc86f7ea2fa80f
diff --git a/python/trx/ctrl_if_bb.py b/python/trx/ctrl_if_bb.py
index 0e194d0..4814263 100644
--- a/python/trx/ctrl_if_bb.py
+++ b/python/trx/ctrl_if_bb.py
@@ -4,7 +4,7 @@
 # GR-GSM based transceiver
 # CTRL interface for OsmocomBB
 #
-# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
+# (C) 2016-2019 by Vadim Yanitskiy <axilirator@gmail.com>
 #
 # All Rights Reserved
 #
@@ -22,49 +22,32 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-import grgsm
-
 from ctrl_if import CTRLInterface
 
 class CTRLInterfaceBB(CTRLInterface):
-	def __init__(self, remote_addr, remote_port, bind_addr, bind_port, tb):
-		CTRLInterface.__init__(self, remote_addr, remote_port,
-			bind_addr, bind_port)
-
+	def __init__(self, trx, *ctrl_if_args):
+		CTRLInterface.__init__(self, *ctrl_if_args)
 		print("[i] Init CTRL interface (%s)" % self.desc_link())
 
-		# Set link to the follow graph (top block)
-		self.tb = tb
+		# Transceiver instance we belong to
+		self.trx = trx
 
 	def parse_cmd(self, request):
 		# Power control
 		if self.verify_cmd(request, "POWERON", 0):
 			print("[i] Recv POWERON CMD")
 
-			# Ensure transceiver isn't working
-			if self.tb.trx_started:
-				print("[!] Transceiver already started")
+			# Start transceiver
+			if not self.trx.start():
 				return -1
 
-			print("[i] Starting transceiver...")
-			self.tb.trx_started = True
-			self.tb.start()
-
 			return 0
 
 		elif self.verify_cmd(request, "POWEROFF", 0):
 			print("[i] Recv POWEROFF cmd")
 
-			# TODO: flush all buffers between blocks
-			if self.tb.trx_started:
-				print("[i] Stopping transceiver...")
-				self.tb.trx_started = False
-				self.tb.set_ta(0)
-				self.tb.stop()
-				self.tb.wait()
-
-			# POWEROFF is also used to reset transceiver
-			self.tb.reset()
+			# Stop transceiver
+			self.trx.stop()
 
 			return 0
 
@@ -74,7 +57,7 @@
 
 			# TODO: check gain value
 			gain = int(request[1])
-			self.tb.set_rx_gain(gain)
+			self.trx.radio_if.set_rx_gain(gain)
 
 			return 0
 
@@ -83,7 +66,7 @@
 
 			# TODO: check gain value
 			gain = int(request[1])
-			self.tb.set_tx_gain(gain)
+			self.trx.radio_if.set_tx_gain(gain)
 
 			return 0
 
@@ -93,7 +76,7 @@
 
 			# TODO: check freq range
 			freq = int(request[1]) * 1000
-			self.tb.set_rx_freq(freq)
+			self.trx.radio_if.set_rx_freq(freq)
 
 			return 0
 
@@ -102,7 +85,7 @@
 
 			# TODO: check freq range
 			freq = int(request[1]) * 1000
-			self.tb.set_tx_freq(freq)
+			self.trx.radio_if.set_tx_freq(freq)
 
 			return 0
 
@@ -116,19 +99,12 @@
 				print("[!] TS index should be in range: 0..7")
 				return -1
 
-			# Ignore timeslot type for now
+			# Channel combination number (see GSM TS 05.02)
+			# TODO: check this value
 			config = int(request[2])
-			print("[i] Configure timeslot filter to: %s"
-				% ("drop all" if config == 0 else "TS %d" % tn))
 
-			if config == 0:
-				# Value 0 means 'drop all'
-				self.tb.ts_filter.set_policy(
-					grgsm.FILTER_POLICY_DROP_ALL)
-			else:
-				self.tb.ts_filter.set_policy(
-					grgsm.FILTER_POLICY_DEFAULT)
-				self.tb.ts_filter.set_tn(tn)
+			# TODO: check return value
+			self.trx.radio_if.set_slot(tn, config)
 
 			return 0
 
@@ -138,9 +114,11 @@
 
 			# TODO: check freq range
 			meas_freq = int(request[1]) * 1000
-			meas_dbm = str(self.tb.measure(meas_freq))
+			meas_dbm = self.trx.measure(meas_freq)
+			if meas_dbm is None:
+				return -1
 
-			return (0, [meas_dbm])
+			return (0, [str(meas_dbm)])
 
 		# Timing Advance control
 		elif self.verify_cmd(request, "SETTA", 1):
@@ -152,7 +130,7 @@
 				print("[!] TA value must be in range: 0..63")
 				return -1
 
-			self.tb.set_ta(ta)
+			self.trx.radio_if.set_ta(ta)
 			return 0
 
 		# Misc