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 |
| 46 | phy_subdev_spec = False |
| 47 | phy_gain = 30 |
| 48 | phy_args = "" |
| 49 | phy_ppm = 0 |
| 50 | |
| 51 | def __init__(self): |
| 52 | self.print_copyright() |
| 53 | self.parse_argv() |
| 54 | |
| 55 | # Set up signal handlers |
| 56 | signal.signal(signal.SIGINT, self.sig_handler) |
| 57 | |
| 58 | def run(self): |
| 59 | # Init Radio interface |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 60 | self.radio = radio_if(self.phy_args, self.phy_subdev_spec, |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 61 | self.phy_sample_rate, self.phy_gain, self.phy_ppm, |
| 62 | self.remote_addr, self.base_port) |
| 63 | |
| 64 | # Power measurement emulation |
| 65 | # Noise: -120 .. -105 |
| 66 | # BTS: -75 .. -50 |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 67 | self.pm = fake_pm(-120, -105, -75, -50) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 68 | |
| 69 | # Init TRX CTRL interface |
Vadim Yanitskiy | ba7ad29 | 2017-10-17 07:21:59 +0700 | [diff] [blame] | 70 | self.server = ctrl_if_bb(self.remote_addr, |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 71 | self.base_port + 101, self.base_port + 1, |
| 72 | self.radio, self.pm) |
| 73 | |
| 74 | print("[i] Init complete") |
| 75 | |
| 76 | # Enter main loop |
| 77 | while True: |
| 78 | self.server.loop() |
| 79 | |
| 80 | def shutdown(self): |
| 81 | print("[i] Shutting down...") |
| 82 | self.server.shutdown() |
| 83 | self.radio.shutdown() |
| 84 | |
| 85 | def print_copyright(self): |
| 86 | print(COPYRIGHT) |
| 87 | |
| 88 | def print_help(self): |
| 89 | s = " Usage: " + sys.argv[0] + " [options]\n\n" \ |
| 90 | " Some help...\n" \ |
| 91 | " -h --help this text\n\n" |
| 92 | |
| 93 | # TRX specific |
| 94 | s += " TRX interface specific\n" \ |
| 95 | " -s --remote-addr Set remote address (default 127.0.0.1)\n" \ |
| 96 | " -p --base-port Set base port number (default 5700)\n\n" |
| 97 | |
| 98 | # PHY specific |
| 99 | s += " Radio interface specific\n" \ |
| 100 | " -a --device-args Set device arguments\n" \ |
| 101 | " -s --sample-rate Set PHY sample rate (default 2000000)\n" \ |
| 102 | " -S --subdev-spec Set PHY sub-device specification\n" \ |
| 103 | " -g --gain Set PHY gain (default 30)\n" \ |
| 104 | " --ppm Set PHY frequency correction (default 0)\n" |
| 105 | |
| 106 | print(s) |
| 107 | |
| 108 | def parse_argv(self): |
| 109 | try: |
| 110 | opts, args = getopt.getopt(sys.argv[1:], |
| 111 | "a:p:i:s:S:g:h", |
| 112 | ["help", "remote-addr=", "base-port=", "device-args=", |
| 113 | "gain=", "subdev-spec=", "sample-rate=", "ppm="]) |
| 114 | except getopt.GetoptError as err: |
| 115 | # Print(help and exit) |
| 116 | self.print_help() |
| 117 | print("[!] " + str(err)) |
| 118 | sys.exit(2) |
| 119 | |
| 120 | for o, v in opts: |
| 121 | if o in ("-h", "--help"): |
| 122 | self.print_help() |
| 123 | sys.exit(2) |
| 124 | |
| 125 | # TRX specific |
| 126 | elif o in ("-i", "--remote-addr"): |
| 127 | self.remote_addr = v |
| 128 | elif o in ("-p", "--base-port"): |
| 129 | if int(v) >= 0 and int(v) <= 65535: |
| 130 | self.base_port = int(v) |
| 131 | else: |
| 132 | print("[!] The port number should be in range [0-65536]") |
| 133 | sys.exit(2) |
| 134 | |
| 135 | # PHY specific |
| 136 | elif o in ("-a", "--device-args"): |
| 137 | self.phy_args = v |
| 138 | elif o in ("-g", "--gain"): |
| 139 | self.phy_gain = int(v) |
| 140 | elif o in ("-S", "--subdev-spec"): |
| 141 | self.phy_subdev_spec = v |
| 142 | elif o in ("-s", "--sample-rate"): |
| 143 | self.phy_sample_rate = int(v) |
| 144 | elif o in ("--ppm"): |
| 145 | self.phy_ppm = int(v) |
| 146 | |
| 147 | def sig_handler(self, signum, frame): |
| 148 | print("Signal %d received" % signum) |
| 149 | if signum is signal.SIGINT: |
| 150 | self.shutdown() |
| 151 | sys.exit(0) |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | app = Application() |
| 155 | app.run() |