blob: eafc7263f605aa5adb5fc6c9e226d5a0332eebce [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"
43 base_port = 5700
44
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 Yanitskiy790b6f02017-10-17 11:47:36 +070099 " -i --remote-addr Set remote address (default 127.0.0.1)\n" \
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200100 " -p --base-port Set base port number (default 5700)\n\n"
101
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 Yanitskiy790b6f02017-10-17 11:47:36 +0700106 " -g --rx-gain Set RX gain (default 30)\n" \
107 " -G --tx-gain Set TX gain (default 10)\n" \
108 " --rx-antenna Set RX antenna (default RX2)\n" \
109 " --tx-antenna Set TX antenna (default TX/RX)\n" \
110 " --ppm Set frequency correction (default 0)\n"
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200111
112 print(s)
113
114 def parse_argv(self):
115 try:
116 opts, args = getopt.getopt(sys.argv[1:],
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700117 "i:p:a:s:g:G:h",
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200118 ["help", "remote-addr=", "base-port=", "device-args=",
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700119 "sample-rate=", "rx-gain=", "tx-gain=", "ppm=",
Vadim Yanitskiy0e7c9a82017-11-23 19:53:31 +0700120 "rx-antenna=", "tx-antenna="])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200121 except getopt.GetoptError as err:
122 # Print(help and exit)
123 self.print_help()
124 print("[!] " + str(err))
125 sys.exit(2)
126
127 for o, v in opts:
128 if o in ("-h", "--help"):
129 self.print_help()
130 sys.exit(2)
131
132 # TRX specific
133 elif o in ("-i", "--remote-addr"):
134 self.remote_addr = v
135 elif o in ("-p", "--base-port"):
136 if int(v) >= 0 and int(v) <= 65535:
137 self.base_port = int(v)
138 else:
139 print("[!] The port number should be in range [0-65536]")
140 sys.exit(2)
141
142 # PHY specific
143 elif o in ("-a", "--device-args"):
144 self.phy_args = v
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200145 elif o in ("-s", "--sample-rate"):
146 self.phy_sample_rate = int(v)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700147 elif o in ("-g", "--rx-gain"):
148 self.phy_rx_gain = int(v)
149 elif o in ("-G", "--tx-gain"):
150 self.phy_tx_gain = int(v)
151 elif o in ("--rx-antenna"):
152 self.phy_rx_antenna = v
153 elif o in ("--tx-antenna"):
154 self.phy_tx_antenna = v
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200155 elif o in ("--ppm"):
156 self.phy_ppm = int(v)
157
158 def sig_handler(self, signum, frame):
159 print("Signal %d received" % signum)
160 if signum is signal.SIGINT:
161 self.shutdown()
162 sys.exit(0)
163
164if __name__ == '__main__':
165 app = Application()
166 app.run()