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