blob: b925fc10a9e3b8225081fa3297bbd1afc5de064b [file] [log] [blame]
Piotr Krysik902f4eb2017-09-19 08:04:33 +02001#!/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
24import signal
25import getopt
26import sys
27
Vadim Yanitskiyba7ad292017-10-17 07:21:59 +070028from grgsm.trx import ctrl_if_bb
29from grgsm.trx import radio_if
30from grgsm.trx import fake_pm
Piotr Krysik902f4eb2017-09-19 08:04:33 +020031
32COPYRIGHT = \
33 "Copyright (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>\n" \
Vadim Yanitskiy3674f482018-01-04 20:16:34 +070034 "Copyright (C) 2017 by Piotr Krysik <ptrkrysik@gmail.com>\n" \
Piotr Krysik902f4eb2017-09-19 08:04:33 +020035 "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
40class Application:
41 # Application variables
42 remote_addr = "127.0.0.1"
Vadim Yanitskiycae78212018-03-03 23:31:02 +070043 base_port = 6700
Piotr Krysik902f4eb2017-09-19 08:04:33 +020044
45 # PHY specific
Vadim Yanitskiy14b8e852017-12-03 23:13:08 +070046 phy_sample_rate = 4 * 1625000 / 6
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070047 phy_tx_antenna = "TX/RX"
48 phy_rx_antenna = "RX2"
49 phy_rx_gain = 30
50 phy_tx_gain = 10
Piotr Krysik902f4eb2017-09-19 08:04:33 +020051 phy_args = ""
52 phy_ppm = 0
53
54 def __init__(self):
55 self.print_copyright()
56 self.parse_argv()
57
58 # Set up signal handlers
59 signal.signal(signal.SIGINT, self.sig_handler)
60
61 def run(self):
62 # Init Radio interface
Vadim Yanitskiy5d68aa52017-10-17 11:14:48 +070063 self.radio = radio_if(self.phy_args, self.phy_sample_rate,
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070064 self.phy_rx_gain, self.phy_tx_gain, self.phy_ppm,
65 self.phy_rx_antenna, self.phy_tx_antenna,
66 self.remote_addr, self.base_port)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020067
68 # Power measurement emulation
69 # Noise: -120 .. -105
70 # BTS: -75 .. -50
Vadim Yanitskiyba7ad292017-10-17 07:21:59 +070071 self.pm = fake_pm(-120, -105, -75, -50)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020072
73 # Init TRX CTRL interface
Vadim Yanitskiyba7ad292017-10-17 07:21:59 +070074 self.server = ctrl_if_bb(self.remote_addr,
Piotr Krysik902f4eb2017-09-19 08:04:33 +020075 self.base_port + 101, self.base_port + 1,
76 self.radio, self.pm)
77
78 print("[i] Init complete")
79
80 # Enter main loop
81 while True:
82 self.server.loop()
83
84 def shutdown(self):
85 print("[i] Shutting down...")
86 self.server.shutdown()
87 self.radio.shutdown()
88
89 def print_copyright(self):
90 print(COPYRIGHT)
91
92 def print_help(self):
93 s = " Usage: " + sys.argv[0] + " [options]\n\n" \
94 " Some help...\n" \
95 " -h --help this text\n\n"
96
97 # TRX specific
98 s += " TRX interface specific\n" \
Vadim Yanitskiy8e1fa8b2018-08-10 00:32:41 +070099 " -i --remote-addr Set remote address (default %s)\n" \
100 " -p --base-port Set base port number (default %d)\n\n"
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200101
102 # PHY specific
103 s += " Radio interface specific\n" \
104 " -a --device-args Set device arguments\n" \
Vadim Yanitskiy14b8e852017-12-03 23:13:08 +0700105 " -s --sample-rate Set sample rate\n" \
Vadim Yanitskiy8e1fa8b2018-08-10 00:32:41 +0700106 " -g --rx-gain Set RX gain (default %d)\n" \
107 " -G --tx-gain Set TX gain (default %d)\n" \
108 " --rx-antenna Set RX antenna (default %s)\n" \
109 " --tx-antenna Set TX antenna (default %s)\n" \
110 " --ppm Set frequency correction (default %d)\n"
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200111
Vadim Yanitskiy8e1fa8b2018-08-10 00:32:41 +0700112 print(s % (
113 self.remote_addr,
114 self.base_port,
115 self.phy_rx_gain,
116 self.phy_tx_gain,
117 self.phy_rx_antenna,
118 self.phy_tx_antenna,
119 self.phy_ppm))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200120
121 def parse_argv(self):
122 try:
123 opts, args = getopt.getopt(sys.argv[1:],
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700124 "i:p:a:s:g:G:h",
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200125 ["help", "remote-addr=", "base-port=", "device-args=",
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700126 "sample-rate=", "rx-gain=", "tx-gain=", "ppm=",
Vadim Yanitskiy0e7c9a82017-11-23 19:53:31 +0700127 "rx-antenna=", "tx-antenna="])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200128 except getopt.GetoptError as err:
129 # Print(help and exit)
130 self.print_help()
131 print("[!] " + str(err))
132 sys.exit(2)
133
134 for o, v in opts:
135 if o in ("-h", "--help"):
136 self.print_help()
137 sys.exit(2)
138
139 # TRX specific
140 elif o in ("-i", "--remote-addr"):
141 self.remote_addr = v
142 elif o in ("-p", "--base-port"):
143 if int(v) >= 0 and int(v) <= 65535:
144 self.base_port = int(v)
145 else:
146 print("[!] The port number should be in range [0-65536]")
147 sys.exit(2)
148
149 # PHY specific
150 elif o in ("-a", "--device-args"):
151 self.phy_args = v
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200152 elif o in ("-s", "--sample-rate"):
153 self.phy_sample_rate = int(v)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700154 elif o in ("-g", "--rx-gain"):
155 self.phy_rx_gain = int(v)
156 elif o in ("-G", "--tx-gain"):
157 self.phy_tx_gain = int(v)
158 elif o in ("--rx-antenna"):
159 self.phy_rx_antenna = v
160 elif o in ("--tx-antenna"):
161 self.phy_tx_antenna = v
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200162 elif o in ("--ppm"):
163 self.phy_ppm = int(v)
164
165 def sig_handler(self, signum, frame):
166 print("Signal %d received" % signum)
167 if signum is signal.SIGINT:
168 self.shutdown()
169 sys.exit(0)
170
171if __name__ == '__main__':
172 app = Application()
173 app.run()