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 | # |
| 6 | # (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> |
| 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 = \ |
| 33 | "Copyright (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>\n" \ |
| 34 | "License GPLv2+: GNU GPL version 2 or later " \ |
| 35 | "<http://gnu.org/licenses/gpl.html>\n" \ |
| 36 | "This is free software: you are free to change and redistribute it.\n" \ |
| 37 | "There is NO WARRANTY, to the extent permitted by law.\n" |
| 38 | |
| 39 | class Application: |
| 40 | # Application variables |
| 41 | remote_addr = "127.0.0.1" |
| 42 | base_port = 5700 |
| 43 | |
| 44 | # PHY specific |
| 45 | phy_sample_rate = 2000000 |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 46 | phy_tx_antenna = "TX/RX" |
| 47 | phy_rx_antenna = "RX2" |
| 48 | phy_rx_gain = 30 |
| 49 | phy_tx_gain = 10 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 50 | phy_args = "" |
| 51 | phy_ppm = 0 |
| 52 | |
| 53 | def __init__(self): |
| 54 | self.print_copyright() |
| 55 | self.parse_argv() |
| 56 | |
| 57 | # Set up signal handlers |
| 58 | signal.signal(signal.SIGINT, self.sig_handler) |
| 59 | |
| 60 | def run(self): |
| 61 | # Init Radio interface |
Vadim Yanitskiy | 5d68aa5 | 2017-10-17 11:14:48 +0700 | [diff] [blame] | 62 | self.radio = radio_if(self.phy_args, self.phy_sample_rate, |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 63 | self.phy_rx_gain, self.phy_tx_gain, self.phy_ppm, |
| 64 | self.phy_rx_antenna, self.phy_tx_antenna, |
| 65 | self.remote_addr, self.base_port) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 66 | |
| 67 | # Power measurement emulation |
| 68 | # Noise: -120 .. -105 |
| 69 | # BTS: -75 .. -50 |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 70 | self.pm = fake_pm(-120, -105, -75, -50) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 71 | |
| 72 | # Init TRX CTRL interface |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 73 | self.server = ctrl_if_bb(self.remote_addr, |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 74 | self.base_port + 101, self.base_port + 1, |
| 75 | self.radio, self.pm) |
| 76 | |
| 77 | print("[i] Init complete") |
| 78 | |
| 79 | # Enter main loop |
| 80 | while True: |
| 81 | self.server.loop() |
| 82 | |
| 83 | def shutdown(self): |
| 84 | print("[i] Shutting down...") |
| 85 | self.server.shutdown() |
| 86 | self.radio.shutdown() |
| 87 | |
| 88 | def print_copyright(self): |
| 89 | print(COPYRIGHT) |
| 90 | |
| 91 | def print_help(self): |
| 92 | s = " Usage: " + sys.argv[0] + " [options]\n\n" \ |
| 93 | " Some help...\n" \ |
| 94 | " -h --help this text\n\n" |
| 95 | |
| 96 | # TRX specific |
| 97 | s += " TRX interface specific\n" \ |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 98 | " -i --remote-addr Set remote address (default 127.0.0.1)\n" \ |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 99 | " -p --base-port Set base port number (default 5700)\n\n" |
| 100 | |
| 101 | # PHY specific |
| 102 | s += " Radio interface specific\n" \ |
| 103 | " -a --device-args Set device arguments\n" \ |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 104 | " -s --sample-rate Set sample rate (default 2000000)\n" \ |
| 105 | " -g --rx-gain Set RX gain (default 30)\n" \ |
| 106 | " -G --tx-gain Set TX gain (default 10)\n" \ |
| 107 | " --rx-antenna Set RX antenna (default RX2)\n" \ |
| 108 | " --tx-antenna Set TX antenna (default TX/RX)\n" \ |
| 109 | " --ppm Set frequency correction (default 0)\n" |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 110 | |
| 111 | print(s) |
| 112 | |
| 113 | def parse_argv(self): |
| 114 | try: |
| 115 | opts, args = getopt.getopt(sys.argv[1:], |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 116 | "i:p:a:s:g:G:h", |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 117 | ["help", "remote-addr=", "base-port=", "device-args=", |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 118 | "sample-rate=", "rx-gain=", "tx-gain=", "ppm=", |
| 119 | "rx-antenna", "tx-antenna"]) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 120 | except getopt.GetoptError as err: |
| 121 | # Print(help and exit) |
| 122 | self.print_help() |
| 123 | print("[!] " + str(err)) |
| 124 | sys.exit(2) |
| 125 | |
| 126 | for o, v in opts: |
| 127 | if o in ("-h", "--help"): |
| 128 | self.print_help() |
| 129 | sys.exit(2) |
| 130 | |
| 131 | # TRX specific |
| 132 | elif o in ("-i", "--remote-addr"): |
| 133 | self.remote_addr = v |
| 134 | elif o in ("-p", "--base-port"): |
| 135 | if int(v) >= 0 and int(v) <= 65535: |
| 136 | self.base_port = int(v) |
| 137 | else: |
| 138 | print("[!] The port number should be in range [0-65536]") |
| 139 | sys.exit(2) |
| 140 | |
| 141 | # PHY specific |
| 142 | elif o in ("-a", "--device-args"): |
| 143 | self.phy_args = v |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 144 | elif o in ("-s", "--sample-rate"): |
| 145 | self.phy_sample_rate = int(v) |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 146 | elif o in ("-g", "--rx-gain"): |
| 147 | self.phy_rx_gain = int(v) |
| 148 | elif o in ("-G", "--tx-gain"): |
| 149 | self.phy_tx_gain = int(v) |
| 150 | elif o in ("--rx-antenna"): |
| 151 | self.phy_rx_antenna = v |
| 152 | elif o in ("--tx-antenna"): |
| 153 | self.phy_tx_antenna = v |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 154 | elif o in ("--ppm"): |
| 155 | self.phy_ppm = int(v) |
| 156 | |
| 157 | def sig_handler(self, signum, frame): |
| 158 | print("Signal %d received" % signum) |
| 159 | if signum is signal.SIGINT: |
| 160 | self.shutdown() |
| 161 | sys.exit(0) |
| 162 | |
| 163 | if __name__ == '__main__': |
| 164 | app = Application() |
| 165 | app.run() |