blob: 0e194d074dc67fbb43391b47a11b65323c1a483a [file] [log] [blame]
Piotr Krysik902f4eb2017-09-19 08:04:33 +02001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4# GR-GSM based transceiver
5# CTRL interface for OsmocomBB
6#
7# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
8#
9# All Rights Reserved
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License along
22# with this program; if not, write to the Free Software Foundation, Inc.,
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +070025import grgsm
Piotr Krysik902f4eb2017-09-19 08:04:33 +020026
Vadim Yanitskiyf237f1a2018-12-20 09:49:56 +070027from ctrl_if import CTRLInterface
28
29class CTRLInterfaceBB(CTRLInterface):
Vadim Yanitskiy7da82f42019-01-19 09:28:23 +070030 def __init__(self, remote_addr, remote_port, bind_addr, bind_port, tb):
Vadim Yanitskiyf237f1a2018-12-20 09:49:56 +070031 CTRLInterface.__init__(self, remote_addr, remote_port,
Vadim Yanitskiy473b35b2018-08-10 00:20:03 +070032 bind_addr, bind_port)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020033
Vadim Yanitskiy2adbee42018-08-10 00:51:36 +070034 print("[i] Init CTRL interface (%s)" % self.desc_link())
35
Piotr Krysik902f4eb2017-09-19 08:04:33 +020036 # Set link to the follow graph (top block)
37 self.tb = tb
Piotr Krysik902f4eb2017-09-19 08:04:33 +020038
Piotr Krysik902f4eb2017-09-19 08:04:33 +020039 def parse_cmd(self, request):
40 # Power control
41 if self.verify_cmd(request, "POWERON", 0):
42 print("[i] Recv POWERON CMD")
43
44 # Ensure transceiver isn't working
45 if self.tb.trx_started:
46 print("[!] Transceiver already started")
47 return -1
48
Piotr Krysik902f4eb2017-09-19 08:04:33 +020049 print("[i] Starting transceiver...")
50 self.tb.trx_started = True
51 self.tb.start()
52
53 return 0
54
55 elif self.verify_cmd(request, "POWEROFF", 0):
56 print("[i] Recv POWEROFF cmd")
57
58 # TODO: flush all buffers between blocks
59 if self.tb.trx_started:
60 print("[i] Stopping transceiver...")
61 self.tb.trx_started = False
Vadim Yanitskiy34266e72017-12-05 01:01:43 +070062 self.tb.set_ta(0)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020063 self.tb.stop()
64 self.tb.wait()
65
Vadim Yanitskiybaebe452019-01-19 10:20:59 +070066 # POWEROFF is also used to reset transceiver
67 self.tb.reset()
68
Piotr Krysik902f4eb2017-09-19 08:04:33 +020069 return 0
70
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +070071 # Gain control
Piotr Krysik902f4eb2017-09-19 08:04:33 +020072 elif self.verify_cmd(request, "SETRXGAIN", 1):
73 print("[i] Recv SETRXGAIN cmd")
74
75 # TODO: check gain value
76 gain = int(request[1])
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +070077 self.tb.set_rx_gain(gain)
78
79 return 0
80
81 elif self.verify_cmd(request, "SETTXGAIN", 1):
82 print("[i] Recv SETTXGAIN cmd")
83
84 # TODO: check gain value
85 gain = int(request[1])
86 self.tb.set_tx_gain(gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020087
88 return 0
89
90 # Tuning Control
91 elif self.verify_cmd(request, "RXTUNE", 1):
92 print("[i] Recv RXTUNE cmd")
93
94 # TODO: check freq range
95 freq = int(request[1]) * 1000
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070096 self.tb.set_rx_freq(freq)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020097
98 return 0
99
100 elif self.verify_cmd(request, "TXTUNE", 1):
101 print("[i] Recv TXTUNE cmd")
102
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700103 # TODO: check freq range
104 freq = int(request[1]) * 1000
105 self.tb.set_tx_freq(freq)
106
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200107 return 0
108
109 # Timeslot management
110 elif self.verify_cmd(request, "SETSLOT", 2):
111 print("[i] Recv SETSLOT cmd")
112
113 # Obtain TS index
114 tn = int(request[1])
115 if tn not in range(0, 8):
116 print("[!] TS index should be in range: 0..7")
117 return -1
118
119 # Ignore timeslot type for now
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700120 config = int(request[2])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200121 print("[i] Configure timeslot filter to: %s"
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700122 % ("drop all" if config == 0 else "TS %d" % tn))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200123
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700124 if config == 0:
125 # Value 0 means 'drop all'
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700126 self.tb.ts_filter.set_policy(
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700127 grgsm.FILTER_POLICY_DROP_ALL)
128 else:
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700129 self.tb.ts_filter.set_policy(
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700130 grgsm.FILTER_POLICY_DEFAULT)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700131 self.tb.ts_filter.set_tn(tn)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200132
133 return 0
134
135 # Power measurement
136 elif self.verify_cmd(request, "MEASURE", 1):
137 print("[i] Recv MEASURE cmd")
138
139 # TODO: check freq range
140 meas_freq = int(request[1]) * 1000
Vadim Yanitskiy7da82f42019-01-19 09:28:23 +0700141 meas_dbm = str(self.tb.measure(meas_freq))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200142
143 return (0, [meas_dbm])
144
Vadim Yanitskiy34266e72017-12-05 01:01:43 +0700145 # Timing Advance control
146 elif self.verify_cmd(request, "SETTA", 1):
147 print("[i] Recv SETTA cmd")
148
149 # Check TA range
150 ta = int(request[1])
151 if ta < 0 or ta > 63:
152 print("[!] TA value must be in range: 0..63")
153 return -1
154
155 self.tb.set_ta(ta)
156 return 0
157
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200158 # Misc
159 elif self.verify_cmd(request, "ECHO", 0):
160 print("[i] Recv ECHO cmd")
161 return 0
162
163 # Wrong / unknown command
164 else:
165 print("[!] Wrong request on CTRL interface")
166 return -1