blob: c5bf233b041f59fc04fb693bb8972da216c90029 [file] [log] [blame]
Roman Khassraf529e2152015-08-22 19:52:41 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# @file
4# @author Roman Khassraf <rkhassraf@gmail.com>
5# @section LICENSE
6#
7# Gr-gsm is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3, or (at your option)
10# any later version.
11#
12# Gr-gsm is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with gr-gsm; see the file COPYING. If not, write to
19# the Free Software Foundation, Inc., 51 Franklin Street,
20# Boston, MA 02110-1301, USA.
21#
22#
23
Roman Khassraf529e2152015-08-22 19:52:41 +020024from gnuradio import blocks
25from gnuradio import eng_notation
26from gnuradio import gr
Roman Khassraf529e2152015-08-22 19:52:41 +020027from gnuradio.eng_option import eng_option
28from gnuradio.filter import firdes
29from math import pi
30from optparse import OptionParser
31
Roman Khassraf529e2152015-08-22 19:52:41 +020032import grgsm
33import osmosdr
34import pmt
Roman Khassraf7eda4802015-08-24 20:00:48 +020035import signal
36import sys
Roman Khassraf529e2152015-08-22 19:52:41 +020037
38
Roman Khassraf7eda4802015-08-24 20:00:48 +020039class airprobe_rtlsdr_capture(gr.top_block):
Roman Khassraf529e2152015-08-22 19:52:41 +020040
41 def __init__(self, fc, gain, samp_rate, ppm, arfcn, cfile=None, burst_file=None, verbose=False):
42
43 gr.top_block.__init__(self, "Airprobe RTL-SDR Capture")
Roman Khassraf7eda4802015-08-24 20:00:48 +020044
Roman Khassraf529e2152015-08-22 19:52:41 +020045 ##################################################
46 # Parameters
47 ##################################################
48 self.fc = fc
49 self.gain = gain
50 self.samp_rate = samp_rate
51 self.ppm = ppm
52 self.arfcn = arfcn
53 self.cfile = cfile
54 self.burst_file = burst_file
55 self.verbose = verbose
56 self.shiftoff = shiftoff = 400e3
Roman Khassraf7eda4802015-08-24 20:00:48 +020057
Roman Khassraf529e2152015-08-22 19:52:41 +020058 ##################################################
59 # Processing Blocks
60 ##################################################
61
62 self.rtlsdr_source = osmosdr.source( args="numchan=" + str(1) + " " + "" )
63 self.rtlsdr_source.set_sample_rate(samp_rate)
Roman Khassraf7eda4802015-08-24 20:00:48 +020064 self.rtlsdr_source.set_center_freq(fc - shiftoff, 0)
65 self.rtlsdr_source.set_freq_corr(ppm, 0)
Roman Khassraf529e2152015-08-22 19:52:41 +020066 self.rtlsdr_source.set_dc_offset_mode(2, 0)
67 self.rtlsdr_source.set_iq_balance_mode(2, 0)
68 self.rtlsdr_source.set_gain_mode(True, 0)
Roman Khassraf7eda4802015-08-24 20:00:48 +020069 self.rtlsdr_source.set_gain(gain, 0)
Roman Khassraf529e2152015-08-22 19:52:41 +020070 self.rtlsdr_source.set_if_gain(20, 0)
71 self.rtlsdr_source.set_bb_gain(20, 0)
72 self.rtlsdr_source.set_antenna("", 0)
73 self.rtlsdr_source.set_bandwidth(250e3+abs(shiftoff), 0)
74 self.blocks_rotator = blocks.rotator_cc(-2*pi*shiftoff/samp_rate)
75
76 if self.verbose or self.burst_file:
77 self.gsm_receiver = grgsm.receiver(4, ([self.arfcn]), ([]))
78 self.gsm_input = grgsm.gsm_input(
79 ppm=0,
80 osr=4,
81 fc=fc,
82 samp_rate_in=samp_rate,
83 )
84 self.gsm_clock_offset_control = grgsm.clock_offset_control(fc-shiftoff)
85
86 if self.burst_file:
87 self.gsm_burst_file_sink = grgsm.burst_file_sink(self.burst_file)
88
89 if self.cfile:
90 self.blocks_file_sink = blocks.file_sink(gr.sizeof_gr_complex*1, self.cfile, False)
91 self.blocks_file_sink.set_unbuffered(False)
92
93 if self.verbose:
94 self.gsm_bursts_printer_0 = grgsm.bursts_printer(pmt.intern(""),
95 False, False, False, False)
96
97 ##################################################
98 # Connections
99 ##################################################
100
101 self.connect((self.rtlsdr_source, 0), (self.blocks_rotator, 0))
Roman Khassraf529e2152015-08-22 19:52:41 +0200102
103 if self.cfile:
104 self.connect((self.blocks_rotator, 0), (self.blocks_file_sink, 0))
105
106 if self.verbose or self.burst_file:
107 self.connect((self.gsm_input, 0), (self.gsm_receiver, 0))
108 self.connect((self.blocks_rotator, 0), (self.gsm_input, 0))
109 self.msg_connect(self.gsm_clock_offset_control, "ppm", self.gsm_input, "ppm_in")
110 self.msg_connect(self.gsm_receiver, "measurements", self.gsm_clock_offset_control, "measurements")
111
112 if self.burst_file:
113 self.msg_connect(self.gsm_receiver, "C0", self.gsm_burst_file_sink, "in")
114 if self.verbose:
115 self.msg_connect(self.gsm_receiver, "C0", self.gsm_bursts_printer_0, "bursts")
116
Roman Khassraf529e2152015-08-22 19:52:41 +0200117 def get_fc(self):
118 return self.fc
119
120 def set_fc(self, fc):
121 self.fc = fc
Roman Khassraf529e2152015-08-22 19:52:41 +0200122 if self.verbose or self.burst_file:
123 self.gsm_input.set_fc(self.fc)
124
125 def get_gain(self):
126 return self.gain
127
128 def set_gain(self, gain):
129 self.gain = gain
Roman Khassraf529e2152015-08-22 19:52:41 +0200130
131 def get_samp_rate(self):
132 return self.samp_rate
133
134 def set_samp_rate(self, samp_rate):
135 self.samp_rate = samp_rate
Roman Khassraf529e2152015-08-22 19:52:41 +0200136 self.rtlsdr_source.set_sample_rate(self.samp_rate)
137 if self.verbose or self.burst_file:
138 self.gsm_input.set_samp_rate_in(self.samp_rate)
139
140 def get_ppm(self):
141 return self.ppm
142
143 def set_ppm(self, ppm):
144 self.ppm = ppm
145 self.set_ppm_slider(self.ppm)
146
Roman Khassraf529e2152015-08-22 19:52:41 +0200147if __name__ == '__main__':
Roman Khassraf529e2152015-08-22 19:52:41 +0200148
149 parser = OptionParser(option_class=eng_option, usage="%prog [options]",
150 description="RTL-SDR capturing app of gr-gsm.")
Roman Khassraf529e2152015-08-22 19:52:41 +0200151
152 parser.add_option("-f", "--fc", dest="fc", type="eng_float",
153 help="Set frequency [default=%default]")
154
155 parser.add_option("-a", "--arfcn", dest="arfcn", type="intx",
156 help="Set ARFCN instead of frequency. In some cases you may have to provide the GSM band also")
157
158 parser.add_option("-g", "--gain", dest="gain", type="eng_float",
159 default=eng_notation.num_to_str(30),
160 help="Set gain [default=%default]")
161
162 parser.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float",
163 default=eng_notation.num_to_str(2000000.052982),
164 help="Set samp_rate [default=%default]")
165
166 parser.add_option("-p", "--ppm", dest="ppm", type="intx", default=0,
167 help="Set ppm [default=%default]")
168
169 parser.add_option("-b", "--burst-file", dest="burst_file",
170 help="File where the captured bursts are saved")
171
172 parser.add_option("-c", "--cfile", dest="cfile",
173 help="File where the captured data are saved")
174
Roman Khassraf529e2152015-08-22 19:52:41 +0200175 bands_list = ", ".join(grgsm.arfcn.get_bands())
176 parser.add_option("--band", dest="band",
177 help="Specify the GSM band for the frequency.\nAvailable bands are: " + bands_list + ".\nIf no band is specified, it will be determined automatically, defaulting to 0." )
Roman Khassraf7eda4802015-08-24 20:00:48 +0200178
179 parser.add_option("-v", "--verbose", action="store_true",
180 help="If set, the captured bursts are printed to stdout")
181
Roman Khassraf529e2152015-08-22 19:52:41 +0200182 (options, args) = parser.parse_args()
183
184 if options.cfile is None and options.burst_file is None:
185 parser.error("Please provide a cfile or a burst file (or both) to save the captured data\n")
186
187 if (options.fc is None and options.arfcn is None) or (options.fc is not None and options.arfcn is not None):
188 parser.error("You have to provide either a frequency or an ARFCN (but not both).\n")
189
190 arfcn = 0
191 fc = 939.4e6
192 if options.arfcn:
193 if options.band:
194 if options.band not in grgsm.arfcn.get_bands():
195 parser.error("Invalid GSM band\n")
196 elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band):
197 parser.error("ARFCN is not valid in the specified band\n")
198 else:
199 arfcn = options.arfcn
200 fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band)
201 else:
202 arfcn = options.arfcn
203 for band in grgsm.arfcn.get_bands():
204 if grgsm.arfcn.is_valid_arfcn(arfcn, band):
205 fc = grgsm.arfcn.arfcn2downlink(arfcn, band)
206 break
207 elif options.fc:
208 fc = options.fc
209 if options.band:
210 if options.band not in grgsm.arfcn.get_bands():
211 parser.error("Invalid GSM band\n")
212 elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band):
213 parser.error("Frequency is not valid in the specified band\n")
214 else:
215 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band)
216 else:
217 for band in grgsm.arfcn.get_bands():
218 if grgsm.arfcn.is_valid_downlink(options.fc, band):
219 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band)
220 break
221
Roman Khassraf529e2152015-08-22 19:52:41 +0200222 tb = airprobe_rtlsdr_capture(fc=fc, gain=options.gain, samp_rate=options.samp_rate,
Roman Khassraf7eda4802015-08-24 20:00:48 +0200223 ppm=options.ppm, arfcn=arfcn, cfile=options.cfile,
224 burst_file=options.burst_file, verbose=options.verbose)
225
226 def signal_handler(signal, frame):
227 tb.stop()
228 tb.wait()
229 sys.exit(0)
230
231 signal.signal(signal.SIGINT, signal_handler)
Roman Khassraf529e2152015-08-22 19:52:41 +0200232
233 tb.start()
Roman Khassraf7eda4802015-08-24 20:00:48 +0200234 tb.wait()