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 | # |
| 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 Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 25 | import grgsm |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 26 | from ctrl_if import ctrl_if |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 27 | |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 28 | class ctrl_if_bb(ctrl_if): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 29 | def __init__(self, remote_addr, remote_port, bind_port, tb, pm): |
| 30 | print("[i] Init CTRL interface") |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 31 | ctrl_if.__init__(self, remote_addr, remote_port, bind_port) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 32 | |
| 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 Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 40 | ctrl_if.shutdown(self) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 41 | |
| 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 Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 52 | 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 |
Vadim Yanitskiy | 34266e7 | 2017-12-05 01:01:43 +0700 | [diff] [blame] | 65 | self.tb.set_ta(0) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 66 | self.tb.stop() |
| 67 | self.tb.wait() |
| 68 | |
| 69 | return 0 |
| 70 | |
Vadim Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 71 | # Gain control |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 72 | elif self.verify_cmd(request, "SETRXGAIN", 1): |
| 73 | print("[i] Recv SETRXGAIN cmd") |
| 74 | |
| 75 | # TODO: check gain value |
| 76 | gain = int(request[1]) |
Vadim Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 77 | self.tb.set_rx_gain(gain) |
| 78 | |
| 79 | return 0 |
| 80 | |
| 81 | elif self.verify_cmd(request, "SETTXGAIN", 1): |
| 82 | print("[i] Recv SETTXGAIN cmd") |
| 83 | |
| 84 | # TODO: check gain value |
| 85 | gain = int(request[1]) |
| 86 | self.tb.set_tx_gain(gain) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 87 | |
| 88 | return 0 |
| 89 | |
| 90 | # Tuning Control |
| 91 | elif self.verify_cmd(request, "RXTUNE", 1): |
| 92 | print("[i] Recv RXTUNE cmd") |
| 93 | |
| 94 | # TODO: check freq range |
| 95 | freq = int(request[1]) * 1000 |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 96 | self.tb.set_rx_freq(freq) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 97 | |
| 98 | return 0 |
| 99 | |
| 100 | elif self.verify_cmd(request, "TXTUNE", 1): |
| 101 | print("[i] Recv TXTUNE cmd") |
| 102 | |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 103 | # TODO: check freq range |
| 104 | freq = int(request[1]) * 1000 |
| 105 | self.tb.set_tx_freq(freq) |
| 106 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 107 | return 0 |
| 108 | |
| 109 | # Timeslot management |
| 110 | elif self.verify_cmd(request, "SETSLOT", 2): |
| 111 | print("[i] Recv SETSLOT cmd") |
| 112 | |
| 113 | # Obtain TS index |
| 114 | tn = int(request[1]) |
| 115 | if tn not in range(0, 8): |
| 116 | print("[!] TS index should be in range: 0..7") |
| 117 | return -1 |
| 118 | |
| 119 | # Ignore timeslot type for now |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 120 | config = int(request[2]) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 121 | print("[i] Configure timeslot filter to: %s" |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 122 | % ("drop all" if config == 0 else "TS %d" % tn)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 123 | |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 124 | if config == 0: |
| 125 | # Value 0 means 'drop all' |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 126 | self.tb.ts_filter.set_policy( |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 127 | grgsm.FILTER_POLICY_DROP_ALL) |
| 128 | else: |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 129 | self.tb.ts_filter.set_policy( |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 130 | grgsm.FILTER_POLICY_DEFAULT) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 131 | self.tb.ts_filter.set_tn(tn) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 132 | |
| 133 | return 0 |
| 134 | |
| 135 | # Power measurement |
| 136 | elif self.verify_cmd(request, "MEASURE", 1): |
| 137 | print("[i] Recv MEASURE cmd") |
| 138 | |
| 139 | # TODO: check freq range |
| 140 | meas_freq = int(request[1]) * 1000 |
| 141 | |
| 142 | # HACK: send fake low power values |
| 143 | # until actual power measurement is implemented |
| 144 | meas_dbm = str(self.pm.measure(meas_freq)) |
| 145 | |
| 146 | return (0, [meas_dbm]) |
| 147 | |
Vadim Yanitskiy | 34266e7 | 2017-12-05 01:01:43 +0700 | [diff] [blame] | 148 | # Timing Advance control |
| 149 | elif self.verify_cmd(request, "SETTA", 1): |
| 150 | print("[i] Recv SETTA cmd") |
| 151 | |
| 152 | # Check TA range |
| 153 | ta = int(request[1]) |
| 154 | if ta < 0 or ta > 63: |
| 155 | print("[!] TA value must be in range: 0..63") |
| 156 | return -1 |
| 157 | |
| 158 | self.tb.set_ta(ta) |
| 159 | return 0 |
| 160 | |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 161 | # Misc |
| 162 | elif self.verify_cmd(request, "ECHO", 0): |
| 163 | print("[i] Recv ECHO cmd") |
| 164 | return 0 |
| 165 | |
| 166 | # Wrong / unknown command |
| 167 | else: |
| 168 | print("[!] Wrong request on CTRL interface") |
| 169 | return -1 |