Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # GR-GSM based transceiver |
| 5 | # CTRL interface for OsmocomBB |
| 6 | # |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 7 | # (C) 2016-2019 by Vadim Yanitskiy <axilirator@gmail.com> |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 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 Yanitskiy | 44d8c1e | 2018-12-20 09:49:56 +0700 | [diff] [blame] | 25 | from ctrl_if import CTRLInterface |
| 26 | |
| 27 | class CTRLInterfaceBB(CTRLInterface): |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 28 | def __init__(self, trx, *ctrl_if_args): |
| 29 | CTRLInterface.__init__(self, *ctrl_if_args) |
Vadim Yanitskiy | 641133e | 2018-08-10 00:51:36 +0700 | [diff] [blame] | 30 | print("[i] Init CTRL interface (%s)" % self.desc_link()) |
| 31 | |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 32 | # Transceiver instance we belong to |
| 33 | self.trx = trx |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 34 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 35 | def parse_cmd(self, request): |
| 36 | # Power control |
| 37 | if self.verify_cmd(request, "POWERON", 0): |
| 38 | print("[i] Recv POWERON CMD") |
| 39 | |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 40 | # Start transceiver |
| 41 | if not self.trx.start(): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 42 | return -1 |
| 43 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 44 | return 0 |
| 45 | |
| 46 | elif self.verify_cmd(request, "POWEROFF", 0): |
| 47 | print("[i] Recv POWEROFF cmd") |
| 48 | |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 49 | # Stop transceiver |
| 50 | self.trx.stop() |
Vadim Yanitskiy | 04863d9 | 2019-01-19 10:20:59 +0700 | [diff] [blame] | 51 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 52 | return 0 |
| 53 | |
Vadim Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 54 | # Gain control |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 55 | 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 Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 60 | self.trx.radio_if.set_rx_gain(gain) |
Vadim Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 61 | |
| 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 Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 69 | self.trx.radio_if.set_tx_gain(gain) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 70 | |
| 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 Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 79 | self.trx.radio_if.set_rx_freq(freq) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 80 | |
| 81 | return 0 |
| 82 | |
| 83 | elif self.verify_cmd(request, "TXTUNE", 1): |
| 84 | print("[i] Recv TXTUNE cmd") |
| 85 | |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 86 | # TODO: check freq range |
| 87 | freq = int(request[1]) * 1000 |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 88 | self.trx.radio_if.set_tx_freq(freq) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 89 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 90 | 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 Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 102 | # Channel combination number (see GSM TS 05.02) |
| 103 | # TODO: check this value |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 104 | config = int(request[2]) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 105 | |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 106 | # TODO: check return value |
| 107 | self.trx.radio_if.set_slot(tn, config) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 108 | |
| 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 Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 117 | meas_dbm = self.trx.measure(meas_freq) |
| 118 | if meas_dbm is None: |
| 119 | return -1 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 120 | |
Vadim Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 121 | return (0, [str(meas_dbm)]) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 122 | |
Vadim Yanitskiy | 34266e7 | 2017-12-05 01:01:43 +0700 | [diff] [blame] | 123 | # 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 Yanitskiy | 2cbc3b5 | 2019-01-19 10:22:59 +0700 | [diff] [blame] | 133 | self.trx.radio_if.set_ta(ta) |
Vadim Yanitskiy | 34266e7 | 2017-12-05 01:01:43 +0700 | [diff] [blame] | 134 | return 0 |
| 135 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 136 | # 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 |