blob: 4814263ce6456039b2fe30c32e470fe415488139 [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#
Vadim Yanitskiy180a0372019-01-19 10:22:59 +07007# (C) 2016-2019 by Vadim Yanitskiy <axilirator@gmail.com>
Piotr Krysik902f4eb2017-09-19 08:04:33 +02008#
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 Yanitskiyf237f1a2018-12-20 09:49:56 +070025from ctrl_if import CTRLInterface
26
27class CTRLInterfaceBB(CTRLInterface):
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070028 def __init__(self, trx, *ctrl_if_args):
29 CTRLInterface.__init__(self, *ctrl_if_args)
Vadim Yanitskiy2adbee42018-08-10 00:51:36 +070030 print("[i] Init CTRL interface (%s)" % self.desc_link())
31
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070032 # Transceiver instance we belong to
33 self.trx = trx
Piotr Krysik902f4eb2017-09-19 08:04:33 +020034
Piotr Krysik902f4eb2017-09-19 08:04:33 +020035 def parse_cmd(self, request):
36 # Power control
37 if self.verify_cmd(request, "POWERON", 0):
38 print("[i] Recv POWERON CMD")
39
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070040 # Start transceiver
41 if not self.trx.start():
Piotr Krysik902f4eb2017-09-19 08:04:33 +020042 return -1
43
Piotr Krysik902f4eb2017-09-19 08:04:33 +020044 return 0
45
46 elif self.verify_cmd(request, "POWEROFF", 0):
47 print("[i] Recv POWEROFF cmd")
48
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070049 # Stop transceiver
50 self.trx.stop()
Vadim Yanitskiybaebe452019-01-19 10:20:59 +070051
Piotr Krysik902f4eb2017-09-19 08:04:33 +020052 return 0
53
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +070054 # Gain control
Piotr Krysik902f4eb2017-09-19 08:04:33 +020055 elif self.verify_cmd(request, "SETRXGAIN", 1):
56 print("[i] Recv SETRXGAIN cmd")
57
58 # TODO: check gain value
59 gain = int(request[1])
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070060 self.trx.radio_if.set_rx_gain(gain)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +070061
62 return 0
63
64 elif self.verify_cmd(request, "SETTXGAIN", 1):
65 print("[i] Recv SETTXGAIN cmd")
66
67 # TODO: check gain value
68 gain = int(request[1])
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070069 self.trx.radio_if.set_tx_gain(gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020070
71 return 0
72
73 # Tuning Control
74 elif self.verify_cmd(request, "RXTUNE", 1):
75 print("[i] Recv RXTUNE cmd")
76
77 # TODO: check freq range
78 freq = int(request[1]) * 1000
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070079 self.trx.radio_if.set_rx_freq(freq)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020080
81 return 0
82
83 elif self.verify_cmd(request, "TXTUNE", 1):
84 print("[i] Recv TXTUNE cmd")
85
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070086 # TODO: check freq range
87 freq = int(request[1]) * 1000
Vadim Yanitskiy180a0372019-01-19 10:22:59 +070088 self.trx.radio_if.set_tx_freq(freq)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070089
Piotr Krysik902f4eb2017-09-19 08:04:33 +020090 return 0
91
92 # Timeslot management
93 elif self.verify_cmd(request, "SETSLOT", 2):
94 print("[i] Recv SETSLOT cmd")
95
96 # Obtain TS index
97 tn = int(request[1])
98 if tn not in range(0, 8):
99 print("[!] TS index should be in range: 0..7")
100 return -1
101
Vadim Yanitskiy180a0372019-01-19 10:22:59 +0700102 # Channel combination number (see GSM TS 05.02)
103 # TODO: check this value
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700104 config = int(request[2])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200105
Vadim Yanitskiy180a0372019-01-19 10:22:59 +0700106 # TODO: check return value
107 self.trx.radio_if.set_slot(tn, config)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200108
109 return 0
110
111 # Power measurement
112 elif self.verify_cmd(request, "MEASURE", 1):
113 print("[i] Recv MEASURE cmd")
114
115 # TODO: check freq range
116 meas_freq = int(request[1]) * 1000
Vadim Yanitskiy180a0372019-01-19 10:22:59 +0700117 meas_dbm = self.trx.measure(meas_freq)
118 if meas_dbm is None:
119 return -1
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200120
Vadim Yanitskiy180a0372019-01-19 10:22:59 +0700121 return (0, [str(meas_dbm)])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200122
Vadim Yanitskiy34266e72017-12-05 01:01:43 +0700123 # Timing Advance control
124 elif self.verify_cmd(request, "SETTA", 1):
125 print("[i] Recv SETTA cmd")
126
127 # Check TA range
128 ta = int(request[1])
129 if ta < 0 or ta > 63:
130 print("[!] TA value must be in range: 0..63")
131 return -1
132
Vadim Yanitskiy180a0372019-01-19 10:22:59 +0700133 self.trx.radio_if.set_ta(ta)
Vadim Yanitskiy34266e72017-12-05 01:01:43 +0700134 return 0
135
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200136 # Misc
137 elif self.verify_cmd(request, "ECHO", 0):
138 print("[i] Recv ECHO cmd")
139 return 0
140
141 # Wrong / unknown command
142 else:
143 print("[!] Wrong request on CTRL interface")
144 return -1