blob: 77e02d0de6759a73bbccdbdd9a6078bfefc0a2f3 [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
52 # Ensure transceiver is ready to start
53 if not self.tb.check_available():
54 print("[!] Transceiver isn't ready to start")
55 return -1
56
57 print("[i] Starting transceiver...")
58 self.tb.trx_started = True
59 self.tb.start()
60
61 return 0
62
63 elif self.verify_cmd(request, "POWEROFF", 0):
64 print("[i] Recv POWEROFF cmd")
65
66 # TODO: flush all buffers between blocks
67 if self.tb.trx_started:
68 print("[i] Stopping transceiver...")
69 self.tb.trx_started = False
70 self.tb.stop()
71 self.tb.wait()
72
73 return 0
74
75 elif self.verify_cmd(request, "SETRXGAIN", 1):
76 print("[i] Recv SETRXGAIN cmd")
77
78 # TODO: check gain value
79 gain = int(request[1])
80 self.tb.set_gain(gain)
81
82 return 0
83
84 # Tuning Control
85 elif self.verify_cmd(request, "RXTUNE", 1):
86 print("[i] Recv RXTUNE cmd")
87
88 # TODO: check freq range
89 freq = int(request[1]) * 1000
90 self.tb.set_fc(freq)
91
92 return 0
93
94 elif self.verify_cmd(request, "TXTUNE", 1):
95 print("[i] Recv TXTUNE cmd")
96
97 # TODO: is not implemented yet
98 return 0
99
100 # Timeslot management
101 elif self.verify_cmd(request, "SETSLOT", 2):
102 print("[i] Recv SETSLOT cmd")
103
104 # Obtain TS index
105 tn = int(request[1])
106 if tn not in range(0, 8):
107 print("[!] TS index should be in range: 0..7")
108 return -1
109
110 # Ignore timeslot type for now
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700111 config = int(request[2])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200112 print("[i] Configure timeslot filter to: %s"
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700113 % ("drop all" if config == 0 else "TS %d" % tn))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200114
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700115 if config == 0:
116 # Value 0 means 'drop all'
117 self.tb.gsm_ts_filter.set_policy(
118 grgsm.FILTER_POLICY_DROP_ALL)
119 else:
120 self.tb.gsm_ts_filter.set_policy(
121 grgsm.FILTER_POLICY_DEFAULT)
122 self.tb.gsm_ts_filter.set_tn(tn)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200123
124 return 0
125
126 # Power measurement
127 elif self.verify_cmd(request, "MEASURE", 1):
128 print("[i] Recv MEASURE cmd")
129
130 # TODO: check freq range
131 meas_freq = int(request[1]) * 1000
132
133 # HACK: send fake low power values
134 # until actual power measurement is implemented
135 meas_dbm = str(self.pm.measure(meas_freq))
136
137 return (0, [meas_dbm])
138
139 # Misc
140 elif self.verify_cmd(request, "ECHO", 0):
141 print("[i] Recv ECHO cmd")
142 return 0
143
144 # Wrong / unknown command
145 else:
146 print("[!] Wrong request on CTRL interface")
147 return -1