blob: dbc1c9f81adde597d37136e448a9c29efe0c907b [file] [log] [blame]
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +07001#!/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 Yanitskiyf3eccbf2017-07-19 15:17:37 +070028from ctrl_if_bb import CTRLInterfaceBB
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070029from radio_if import RadioInterface
30
31COPYRIGHT = \
32 "Copyright (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>\n" \
33 "License GPLv2+: GNU GPL version 2 or later " \
34 "<http://gnu.org/licenses/gpl.html>\n" \
35 "This is free software: you are free to change and redistribute it.\n" \
36 "There is NO WARRANTY, to the extent permitted by law.\n"
37
38class Application:
39 # Application variables
40 remote_addr = "127.0.0.1"
41 base_port = 5700
42
43 # PHY specific
44 phy_sample_rate = 2000000
45 phy_subdev_spec = False
46 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
59 self.radio = RadioInterface(self.phy_args, self.phy_subdev_spec,
60 self.phy_sample_rate, self.phy_gain, self.phy_ppm,
61 self.remote_addr, self.base_port)
62
63 # Init TRX CTRL interface
Vadim Yanitskiyf3eccbf2017-07-19 15:17:37 +070064 self.server = CTRLInterfaceBB(self.remote_addr,
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +070065 self.base_port + 101, self.base_port + 1, self.radio)
66
67 print("[i] Init complete")
68
69 # Enter main loop
70 while True:
71 self.server.loop()
72
73 def shutdown(self):
74 print("[i] Shutting down...")
75 self.server.shutdown()
76 self.radio.shutdown()
77
78 def print_copyright(self):
79 print(COPYRIGHT)
80
81 def print_help(self):
82 s = " Usage: " + sys.argv[0] + " [options]\n\n" \
83 " Some help...\n" \
84 " -h --help this text\n\n"
85
86 # TRX specific
87 s += " TRX interface specific\n" \
88 " -s --remote-addr Set remote address (default 127.0.0.1)\n" \
89 " -p --base-port Set base port number (default 5700)\n\n"
90
91 # PHY specific
92 s += " Radio interface specific\n" \
93 " -a --device-args Set device arguments\n" \
94 " -s --sample-rate Set PHY sample rate (default 2000000)\n" \
95 " -S --subdev-spec Set PHY sub-device specification\n" \
96 " -g --gain Set PHY gain (default 30)\n" \
97 " --ppm Set PHY frequency correction (default 0)\n"
98
99 print(s)
100
101 def parse_argv(self):
102 try:
103 opts, args = getopt.getopt(sys.argv[1:],
104 "a:p:i:s:S:g:h",
105 ["help", "remote-addr=", "base-port=", "device-args=",
106 "gain=", "subdev-spec=", "sample-rate=", "ppm="])
107 except getopt.GetoptError as err:
108 # Print(help and exit)
109 self.print_help()
110 print("[!] " + str(err))
111 sys.exit(2)
112
113 for o, v in opts:
114 if o in ("-h", "--help"):
115 self.print_help()
116 sys.exit(2)
117
118 # TRX specific
119 elif o in ("-i", "--remote-addr"):
120 self.remote_addr = v
121 elif o in ("-p", "--base-port"):
122 if int(v) >= 0 and int(v) <= 65535:
123 self.base_port = int(v)
124 else:
125 print("[!] The port number should be in range [0-65536]")
126 sys.exit(2)
127
128 # PHY specific
129 elif o in ("-a", "--device-args"):
130 self.phy_args = v
131 elif o in ("-g", "--gain"):
132 self.phy_gain = int(v)
133 elif o in ("-S", "--subdev-spec"):
134 self.phy_subdev_spec = v
135 elif o in ("-s", "--sample-rate"):
136 self.phy_sample_rate = int(v)
137 elif o in ("--ppm"):
138 self.phy_ppm = int(v)
139
140 def sig_handler(self, signum, frame):
141 print("Signal %d received" % signum)
142 if signum is signal.SIGINT:
143 self.shutdown()
144 sys.exit(0)
145
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +0700146if __name__ == '__main__':
Vadim Yanitskiy4b5c2992017-07-19 14:54:14 +0700147 app = Application()
148 app.run()