blob: b0d54f98a7650f72e57ba86b75ea3af04eae7ba0 [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
25from ctrl_if import CTRLInterface
26
27class CTRLInterfaceBB(CTRLInterface):
28 def __init__(self, remote_addr, remote_port, bind_port, tb, pm):
29 print("[i] Init CTRL interface")
30 CTRLInterface.__init__(self, remote_addr, remote_port, bind_port)
31
32 # Set link to the follow graph (top block)
33 self.tb = tb
34 # Power measurement
35 self.pm = pm
36
37 def shutdown(self):
38 print("[i] Shutdown CTRL interface")
39 CTRLInterface.shutdown(self)
40
41 def parse_cmd(self, request):
42 # Power control
43 if self.verify_cmd(request, "POWERON", 0):
44 print("[i] Recv POWERON CMD")
45
46 # Ensure transceiver isn't working
47 if self.tb.trx_started:
48 print("[!] Transceiver already started")
49 return -1
50
51 # Ensure transceiver is ready to start
52 if not self.tb.check_available():
53 print("[!] Transceiver isn't ready to start")
54 return -1
55
56 print("[i] Starting transceiver...")
57 self.tb.trx_started = True
58 self.tb.start()
59
60 return 0
61
62 elif self.verify_cmd(request, "POWEROFF", 0):
63 print("[i] Recv POWEROFF cmd")
64
65 # TODO: flush all buffers between blocks
66 if self.tb.trx_started:
67 print("[i] Stopping transceiver...")
68 self.tb.trx_started = False
69 self.tb.stop()
70 self.tb.wait()
71
72 return 0
73
74 elif self.verify_cmd(request, "SETRXGAIN", 1):
75 print("[i] Recv SETRXGAIN cmd")
76
77 # TODO: check gain value
78 gain = int(request[1])
79 self.tb.set_gain(gain)
80
81 return 0
82
83 # Tuning Control
84 elif self.verify_cmd(request, "RXTUNE", 1):
85 print("[i] Recv RXTUNE cmd")
86
87 # TODO: check freq range
88 freq = int(request[1]) * 1000
89 self.tb.set_fc(freq)
90
91 return 0
92
93 elif self.verify_cmd(request, "TXTUNE", 1):
94 print("[i] Recv TXTUNE cmd")
95
96 # TODO: is not implemented yet
97 return 0
98
99 # Timeslot management
100 elif self.verify_cmd(request, "SETSLOT", 2):
101 print("[i] Recv SETSLOT cmd")
102
103 # Obtain TS index
104 tn = int(request[1])
105 if tn not in range(0, 8):
106 print("[!] TS index should be in range: 0..7")
107 return -1
108
109 # Ignore timeslot type for now
110 # Value 0 means 'drop all'
111 config = -1 if int(request[2]) == 0 else tn
112
113 print("[i] Configure timeslot filter to: %s"
114 % ("drop all" if config == -1 else "TS %d" % tn))
115
116 # HACK: configure built-in timeslot filter
117 self.tb.gsm_trx_if.ts_filter_set_tn(config)
118
119 return 0
120
121 # Power measurement
122 elif self.verify_cmd(request, "MEASURE", 1):
123 print("[i] Recv MEASURE cmd")
124
125 # TODO: check freq range
126 meas_freq = int(request[1]) * 1000
127
128 # HACK: send fake low power values
129 # until actual power measurement is implemented
130 meas_dbm = str(self.pm.measure(meas_freq))
131
132 return (0, [meas_dbm])
133
134 # Misc
135 elif self.verify_cmd(request, "ECHO", 0):
136 print("[i] Recv ECHO cmd")
137 return 0
138
139 # Wrong / unknown command
140 else:
141 print("[!] Wrong request on CTRL interface")
142 return -1