blob: 26ae49a9b814f6b9c87d5d5b1f572c9ea3c3a512 [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
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070026from ctrl_if import ctrl_if
Piotr Krysik902f4eb2017-09-19 08:04:33 +020027
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070028class ctrl_if_bb(ctrl_if):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020029 def __init__(self, remote_addr, remote_port, bind_port, tb, pm):
30 print("[i] Init CTRL interface")
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070031 ctrl_if.__init__(self, remote_addr, remote_port, bind_port)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020032
33 # Set link to the follow graph (top block)
34 self.tb = tb
35 # Power measurement
36 self.pm = pm
37
38 def shutdown(self):
39 print("[i] Shutdown CTRL interface")
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070040 ctrl_if.shutdown(self)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020041
42 def parse_cmd(self, request):
43 # Power control
44 if self.verify_cmd(request, "POWERON", 0):
45 print("[i] Recv POWERON CMD")
46
47 # Ensure transceiver isn't working
48 if self.tb.trx_started:
49 print("[!] Transceiver already started")
50 return -1
51
Piotr Krysik902f4eb2017-09-19 08:04:33 +020052 print("[i] Starting transceiver...")
53 self.tb.trx_started = True
54 self.tb.start()
55
56 return 0
57
58 elif self.verify_cmd(request, "POWEROFF", 0):
59 print("[i] Recv POWEROFF cmd")
60
61 # TODO: flush all buffers between blocks
62 if self.tb.trx_started:
63 print("[i] Stopping transceiver...")
64 self.tb.trx_started = False
65 self.tb.stop()
66 self.tb.wait()
67
68 return 0
69
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +070070 # Gain control
Piotr Krysik902f4eb2017-09-19 08:04:33 +020071 elif self.verify_cmd(request, "SETRXGAIN", 1):
72 print("[i] Recv SETRXGAIN cmd")
73
74 # TODO: check gain value
75 gain = int(request[1])
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +070076 self.tb.set_rx_gain(gain)
77
78 return 0
79
80 elif self.verify_cmd(request, "SETTXGAIN", 1):
81 print("[i] Recv SETTXGAIN cmd")
82
83 # TODO: check gain value
84 gain = int(request[1])
85 self.tb.set_tx_gain(gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020086
87 return 0
88
89 # Tuning Control
90 elif self.verify_cmd(request, "RXTUNE", 1):
91 print("[i] Recv RXTUNE cmd")
92
93 # TODO: check freq range
94 freq = int(request[1]) * 1000
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070095 self.tb.set_rx_freq(freq)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020096
97 return 0
98
99 elif self.verify_cmd(request, "TXTUNE", 1):
100 print("[i] Recv TXTUNE cmd")
101
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700102 # TODO: check freq range
103 freq = int(request[1]) * 1000
104 self.tb.set_tx_freq(freq)
105
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200106 return 0
107
108 # Timeslot management
109 elif self.verify_cmd(request, "SETSLOT", 2):
110 print("[i] Recv SETSLOT cmd")
111
112 # Obtain TS index
113 tn = int(request[1])
114 if tn not in range(0, 8):
115 print("[!] TS index should be in range: 0..7")
116 return -1
117
118 # Ignore timeslot type for now
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700119 config = int(request[2])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200120 print("[i] Configure timeslot filter to: %s"
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700121 % ("drop all" if config == 0 else "TS %d" % tn))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200122
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700123 if config == 0:
124 # Value 0 means 'drop all'
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700125 self.tb.ts_filter.set_policy(
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700126 grgsm.FILTER_POLICY_DROP_ALL)
127 else:
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700128 self.tb.ts_filter.set_policy(
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700129 grgsm.FILTER_POLICY_DEFAULT)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700130 self.tb.ts_filter.set_tn(tn)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200131
132 return 0
133
134 # Power measurement
135 elif self.verify_cmd(request, "MEASURE", 1):
136 print("[i] Recv MEASURE cmd")
137
138 # TODO: check freq range
139 meas_freq = int(request[1]) * 1000
140
141 # HACK: send fake low power values
142 # until actual power measurement is implemented
143 meas_dbm = str(self.pm.measure(meas_freq))
144
145 return (0, [meas_dbm])
146
147 # Misc
148 elif self.verify_cmd(request, "ECHO", 0):
149 print("[i] Recv ECHO cmd")
150 return 0
151
152 # Wrong / unknown command
153 else:
154 print("[!] Wrong request on CTRL interface")
155 return -1