blob: 668b0be4ef60409d8c71169745531d5045e58ee6 [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...")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020086 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 Yanitskiy8e1fa8b2018-08-10 00:32:41 +070098 " -i --remote-addr Set remote address (default %s)\n" \
99 " -p --base-port Set base port number (default %d)\n\n"
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200100
101 # PHY specific
102 s += " Radio interface specific\n" \
103 " -a --device-args Set device arguments\n" \
Vadim Yanitskiy14b8e852017-12-03 23:13:08 +0700104 " -s --sample-rate Set sample rate\n" \
Vadim Yanitskiy8e1fa8b2018-08-10 00:32:41 +0700105 " -g --rx-gain Set RX gain (default %d)\n" \
106 " -G --tx-gain Set TX gain (default %d)\n" \
107 " --rx-antenna Set RX antenna (default %s)\n" \
108 " --tx-antenna Set TX antenna (default %s)\n" \
109 " --ppm Set frequency correction (default %d)\n"
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200110
Vadim Yanitskiy8e1fa8b2018-08-10 00:32:41 +0700111 print(s % (
112 self.remote_addr,
113 self.base_port,
114 self.phy_rx_gain,
115 self.phy_tx_gain,
116 self.phy_rx_antenna,
117 self.phy_tx_antenna,
118 self.phy_ppm))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200119
120 def parse_argv(self):
121 try:
122 opts, args = getopt.getopt(sys.argv[1:],
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700123 "i:p:a:s:g:G:h",
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200124 ["help", "remote-addr=", "base-port=", "device-args=",
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700125 "sample-rate=", "rx-gain=", "tx-gain=", "ppm=",
Vadim Yanitskiy0e7c9a82017-11-23 19:53:31 +0700126 "rx-antenna=", "tx-antenna="])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200127 except getopt.GetoptError as err:
128 # Print(help and exit)
129 self.print_help()
130 print("[!] " + str(err))
131 sys.exit(2)
132
133 for o, v in opts:
134 if o in ("-h", "--help"):
135 self.print_help()
136 sys.exit(2)
137
138 # TRX specific
139 elif o in ("-i", "--remote-addr"):
140 self.remote_addr = v
141 elif o in ("-p", "--base-port"):
142 if int(v) >= 0 and int(v) <= 65535:
143 self.base_port = int(v)
144 else:
145 print("[!] The port number should be in range [0-65536]")
146 sys.exit(2)
147
148 # PHY specific
149 elif o in ("-a", "--device-args"):
150 self.phy_args = v
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200151 elif o in ("-s", "--sample-rate"):
152 self.phy_sample_rate = int(v)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700153 elif o in ("-g", "--rx-gain"):
154 self.phy_rx_gain = int(v)
155 elif o in ("-G", "--tx-gain"):
156 self.phy_tx_gain = int(v)
157 elif o in ("--rx-antenna"):
158 self.phy_rx_antenna = v
159 elif o in ("--tx-antenna"):
160 self.phy_tx_antenna = v
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200161 elif o in ("--ppm"):
162 self.phy_ppm = int(v)
163
164 def sig_handler(self, signum, frame):
165 print("Signal %d received" % signum)
166 if signum is signal.SIGINT:
167 self.shutdown()
168 sys.exit(0)
169
170if __name__ == '__main__':
171 app = Application()
172 app.run()