blob: 128adab4b69acfc2dabbc5d231bb3ed6ecc68b05 [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" \
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
39class Application:
40 # Application variables
41 remote_addr = "127.0.0.1"
42 base_port = 5700
43
44 # PHY specific
Vadim Yanitskiy14b8e852017-12-03 23:13:08 +070045 phy_sample_rate = 4 * 1625000 / 6
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070046 phy_tx_antenna = "TX/RX"
47 phy_rx_antenna = "RX2"
48 phy_rx_gain = 30
49 phy_tx_gain = 10
Piotr Krysik902f4eb2017-09-19 08:04:33 +020050 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 Yanitskiy5d68aa52017-10-17 11:14:48 +070062 self.radio = radio_if(self.phy_args, self.phy_sample_rate,
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070063 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 Krysik902f4eb2017-09-19 08:04:33 +020066
67 # Power measurement emulation
68 # Noise: -120 .. -105
69 # BTS: -75 .. -50
Vadim Yanitskiyba7ad292017-10-17 07:21:59 +070070 self.pm = fake_pm(-120, -105, -75, -50)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020071
72 # Init TRX CTRL interface
Vadim Yanitskiyba7ad292017-10-17 07:21:59 +070073 self.server = ctrl_if_bb(self.remote_addr,
Piotr Krysik902f4eb2017-09-19 08:04:33 +020074 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 Yanitskiy790b6f02017-10-17 11:47:36 +070098 " -i --remote-addr Set remote address (default 127.0.0.1)\n" \
Piotr Krysik902f4eb2017-09-19 08:04:33 +020099 " -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 Yanitskiy14b8e852017-12-03 23:13:08 +0700104 " -s --sample-rate Set sample rate\n" \
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700105 " -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 Krysik902f4eb2017-09-19 08:04:33 +0200110
111 print(s)
112
113 def parse_argv(self):
114 try:
115 opts, args = getopt.getopt(sys.argv[1:],
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700116 "i:p:a:s:g:G:h",
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200117 ["help", "remote-addr=", "base-port=", "device-args=",
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700118 "sample-rate=", "rx-gain=", "tx-gain=", "ppm=",
Vadim Yanitskiy0e7c9a82017-11-23 19:53:31 +0700119 "rx-antenna=", "tx-antenna="])
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200120 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 Krysik902f4eb2017-09-19 08:04:33 +0200144 elif o in ("-s", "--sample-rate"):
145 self.phy_sample_rate = int(v)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700146 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 Krysik902f4eb2017-09-19 08:04:33 +0200154 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
163if __name__ == '__main__':
164 app = Application()
165 app.run()