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 | # |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 6 | # (C) 2016-2018 by Vadim Yanitskiy <axilirator@gmail.com> |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 7 | # |
| 8 | # All Rights Reserved |
| 9 | # |
| 10 | # This program is free software; you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation; either version 2 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # This program is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License along |
| 21 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | |
| 24 | import signal |
| 25 | import getopt |
| 26 | import sys |
| 27 | |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 28 | from grgsm.trx import ctrl_if_bb |
| 29 | from grgsm.trx import radio_if |
| 30 | from grgsm.trx import fake_pm |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 31 | |
| 32 | COPYRIGHT = \ |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 33 | "Copyright (C) 2016-2018 by Vadim Yanitskiy <axilirator@gmail.com>\n" \ |
Vadim Yanitskiy | a609d2f | 2018-01-04 20:16:34 +0700 | [diff] [blame] | 34 | "Copyright (C) 2017 by Piotr Krysik <ptrkrysik@gmail.com>\n" \ |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 35 | "License GPLv2+: GNU GPL version 2 or later " \ |
| 36 | "<http://gnu.org/licenses/gpl.html>\n" \ |
| 37 | "This is free software: you are free to change and redistribute it.\n" \ |
| 38 | "There is NO WARRANTY, to the extent permitted by law.\n" |
| 39 | |
| 40 | class Application: |
| 41 | # Application variables |
| 42 | remote_addr = "127.0.0.1" |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 43 | bind_addr = "0.0.0.0" |
Vadim Yanitskiy | 07a2abb | 2018-03-03 23:31:02 +0700 | [diff] [blame] | 44 | base_port = 6700 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 45 | |
| 46 | # PHY specific |
Vadim Yanitskiy | 14b8e85 | 2017-12-03 23:13:08 +0700 | [diff] [blame] | 47 | phy_sample_rate = 4 * 1625000 / 6 |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 48 | phy_tx_antenna = "TX/RX" |
| 49 | phy_rx_antenna = "RX2" |
| 50 | phy_rx_gain = 30 |
| 51 | phy_tx_gain = 10 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 52 | phy_args = "" |
| 53 | phy_ppm = 0 |
| 54 | |
| 55 | def __init__(self): |
| 56 | self.print_copyright() |
| 57 | self.parse_argv() |
| 58 | |
| 59 | # Set up signal handlers |
| 60 | signal.signal(signal.SIGINT, self.sig_handler) |
| 61 | |
| 62 | def run(self): |
| 63 | # Init Radio interface |
Vadim Yanitskiy | 5d68aa5 | 2017-10-17 11:14:48 +0700 | [diff] [blame] | 64 | self.radio = radio_if(self.phy_args, self.phy_sample_rate, |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 65 | self.phy_rx_gain, self.phy_tx_gain, self.phy_ppm, |
| 66 | self.phy_rx_antenna, self.phy_tx_antenna, |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 67 | self.bind_addr, self.remote_addr, |
| 68 | self.base_port) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 69 | |
| 70 | # Power measurement emulation |
| 71 | # Noise: -120 .. -105 |
| 72 | # BTS: -75 .. -50 |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 73 | self.pm = fake_pm(-120, -105, -75, -50) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 74 | |
| 75 | # Init TRX CTRL interface |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 76 | self.server = ctrl_if_bb( |
| 77 | self.remote_addr, self.base_port + 101, |
| 78 | self.bind_addr, self.base_port + 1, |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 79 | self.radio, self.pm) |
| 80 | |
| 81 | print("[i] Init complete") |
| 82 | |
| 83 | # Enter main loop |
| 84 | while True: |
| 85 | self.server.loop() |
| 86 | |
| 87 | def shutdown(self): |
| 88 | print("[i] Shutting down...") |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 89 | self.radio.shutdown() |
| 90 | |
| 91 | def print_copyright(self): |
| 92 | print(COPYRIGHT) |
| 93 | |
| 94 | def print_help(self): |
| 95 | s = " Usage: " + sys.argv[0] + " [options]\n\n" \ |
| 96 | " Some help...\n" \ |
| 97 | " -h --help this text\n\n" |
| 98 | |
| 99 | # TRX specific |
| 100 | s += " TRX interface specific\n" \ |
Vadim Yanitskiy | 4cedbb5 | 2018-08-10 00:32:41 +0700 | [diff] [blame] | 101 | " -i --remote-addr Set remote address (default %s)\n" \ |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 102 | " -b --bind-addr Set bind address (default %s)\n" \ |
Vadim Yanitskiy | 4cedbb5 | 2018-08-10 00:32:41 +0700 | [diff] [blame] | 103 | " -p --base-port Set base port number (default %d)\n\n" |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 104 | |
| 105 | # PHY specific |
| 106 | s += " Radio interface specific\n" \ |
| 107 | " -a --device-args Set device arguments\n" \ |
Vadim Yanitskiy | 14b8e85 | 2017-12-03 23:13:08 +0700 | [diff] [blame] | 108 | " -s --sample-rate Set sample rate\n" \ |
Vadim Yanitskiy | 4cedbb5 | 2018-08-10 00:32:41 +0700 | [diff] [blame] | 109 | " -g --rx-gain Set RX gain (default %d)\n" \ |
| 110 | " -G --tx-gain Set TX gain (default %d)\n" \ |
| 111 | " --rx-antenna Set RX antenna (default %s)\n" \ |
| 112 | " --tx-antenna Set TX antenna (default %s)\n" \ |
| 113 | " --ppm Set frequency correction (default %d)\n" |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 114 | |
Vadim Yanitskiy | 4cedbb5 | 2018-08-10 00:32:41 +0700 | [diff] [blame] | 115 | print(s % ( |
| 116 | self.remote_addr, |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 117 | self.bind_addr, |
Vadim Yanitskiy | 4cedbb5 | 2018-08-10 00:32:41 +0700 | [diff] [blame] | 118 | self.base_port, |
| 119 | self.phy_rx_gain, |
| 120 | self.phy_tx_gain, |
| 121 | self.phy_rx_antenna, |
| 122 | self.phy_tx_antenna, |
| 123 | self.phy_ppm)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 124 | |
| 125 | def parse_argv(self): |
| 126 | try: |
| 127 | opts, args = getopt.getopt(sys.argv[1:], |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 128 | "i:b:p:a:s:g:G:h", |
| 129 | ["help", "remote-addr=", "bind-addr=", "base-port=", |
| 130 | "device-args=", "sample-rate=", "rx-gain=", "tx-gain=", |
| 131 | "ppm=", "rx-antenna=", "tx-antenna="]) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 132 | except getopt.GetoptError as err: |
| 133 | # Print(help and exit) |
| 134 | self.print_help() |
| 135 | print("[!] " + str(err)) |
| 136 | sys.exit(2) |
| 137 | |
| 138 | for o, v in opts: |
| 139 | if o in ("-h", "--help"): |
| 140 | self.print_help() |
| 141 | sys.exit(2) |
| 142 | |
| 143 | # TRX specific |
| 144 | elif o in ("-i", "--remote-addr"): |
| 145 | self.remote_addr = v |
Vadim Yanitskiy | 2165584 | 2018-08-10 00:20:03 +0700 | [diff] [blame^] | 146 | elif o in ("-b", "--bind-addr"): |
| 147 | self.bind_addr = v |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 148 | elif o in ("-p", "--base-port"): |
| 149 | if int(v) >= 0 and int(v) <= 65535: |
| 150 | self.base_port = int(v) |
| 151 | else: |
| 152 | print("[!] The port number should be in range [0-65536]") |
| 153 | sys.exit(2) |
| 154 | |
| 155 | # PHY specific |
| 156 | elif o in ("-a", "--device-args"): |
| 157 | self.phy_args = v |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 158 | elif o in ("-s", "--sample-rate"): |
| 159 | self.phy_sample_rate = int(v) |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 160 | elif o in ("-g", "--rx-gain"): |
| 161 | self.phy_rx_gain = int(v) |
| 162 | elif o in ("-G", "--tx-gain"): |
| 163 | self.phy_tx_gain = int(v) |
| 164 | elif o in ("--rx-antenna"): |
| 165 | self.phy_rx_antenna = v |
| 166 | elif o in ("--tx-antenna"): |
| 167 | self.phy_tx_antenna = v |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 168 | elif o in ("--ppm"): |
| 169 | self.phy_ppm = int(v) |
| 170 | |
| 171 | def sig_handler(self, signum, frame): |
| 172 | print("Signal %d received" % signum) |
| 173 | if signum is signal.SIGINT: |
| 174 | self.shutdown() |
| 175 | sys.exit(0) |
| 176 | |
| 177 | if __name__ == '__main__': |
| 178 | app = Application() |
| 179 | app.run() |