blob: b744bf251c75fb7e22a0ef7b63c46c86abd8dacd [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
Roman Khassraf5a804002015-08-24 20:11:10 +020041 def __init__(self, fc, gain, samp_rate, ppm, arfcn, cfile=None, burst_file=None, band=None, verbose=False):
Roman Khassraf529e2152015-08-22 19:52:41 +020042
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
Roman Khassraf5a804002015-08-24 20:11:10 +020055 self.band = band
Roman Khassraf529e2152015-08-22 19:52:41 +020056 self.verbose = verbose
57 self.shiftoff = shiftoff = 400e3
Roman Khassraf7eda4802015-08-24 20:00:48 +020058
Roman Khassraf529e2152015-08-22 19:52:41 +020059 ##################################################
60 # Processing Blocks
61 ##################################################
62
63 self.rtlsdr_source = osmosdr.source( args="numchan=" + str(1) + " " + "" )
64 self.rtlsdr_source.set_sample_rate(samp_rate)
Roman Khassraf7eda4802015-08-24 20:00:48 +020065 self.rtlsdr_source.set_center_freq(fc - shiftoff, 0)
66 self.rtlsdr_source.set_freq_corr(ppm, 0)
Roman Khassraf529e2152015-08-22 19:52:41 +020067 self.rtlsdr_source.set_dc_offset_mode(2, 0)
68 self.rtlsdr_source.set_iq_balance_mode(2, 0)
69 self.rtlsdr_source.set_gain_mode(True, 0)
Roman Khassraf7eda4802015-08-24 20:00:48 +020070 self.rtlsdr_source.set_gain(gain, 0)
Roman Khassraf529e2152015-08-22 19:52:41 +020071 self.rtlsdr_source.set_if_gain(20, 0)
72 self.rtlsdr_source.set_bb_gain(20, 0)
73 self.rtlsdr_source.set_antenna("", 0)
74 self.rtlsdr_source.set_bandwidth(250e3+abs(shiftoff), 0)
75 self.blocks_rotator = blocks.rotator_cc(-2*pi*shiftoff/samp_rate)
76
77 if self.verbose or self.burst_file:
78 self.gsm_receiver = grgsm.receiver(4, ([self.arfcn]), ([]))
79 self.gsm_input = grgsm.gsm_input(
80 ppm=0,
81 osr=4,
82 fc=fc,
83 samp_rate_in=samp_rate,
84 )
85 self.gsm_clock_offset_control = grgsm.clock_offset_control(fc-shiftoff)
86
87 if self.burst_file:
88 self.gsm_burst_file_sink = grgsm.burst_file_sink(self.burst_file)
89
90 if self.cfile:
91 self.blocks_file_sink = blocks.file_sink(gr.sizeof_gr_complex*1, self.cfile, False)
92 self.blocks_file_sink.set_unbuffered(False)
93
94 if self.verbose:
95 self.gsm_bursts_printer_0 = grgsm.bursts_printer(pmt.intern(""),
96 False, False, False, False)
97
98 ##################################################
99 # Connections
100 ##################################################
101
102 self.connect((self.rtlsdr_source, 0), (self.blocks_rotator, 0))
Roman Khassraf529e2152015-08-22 19:52:41 +0200103
104 if self.cfile:
105 self.connect((self.blocks_rotator, 0), (self.blocks_file_sink, 0))
106
107 if self.verbose or self.burst_file:
108 self.connect((self.gsm_input, 0), (self.gsm_receiver, 0))
109 self.connect((self.blocks_rotator, 0), (self.gsm_input, 0))
110 self.msg_connect(self.gsm_clock_offset_control, "ppm", self.gsm_input, "ppm_in")
111 self.msg_connect(self.gsm_receiver, "measurements", self.gsm_clock_offset_control, "measurements")
112
113 if self.burst_file:
114 self.msg_connect(self.gsm_receiver, "C0", self.gsm_burst_file_sink, "in")
115 if self.verbose:
116 self.msg_connect(self.gsm_receiver, "C0", self.gsm_bursts_printer_0, "bursts")
117
Roman Khassraf529e2152015-08-22 19:52:41 +0200118 def get_fc(self):
119 return self.fc
120
121 def set_fc(self, fc):
122 self.fc = fc
Roman Khassraf529e2152015-08-22 19:52:41 +0200123 if self.verbose or self.burst_file:
124 self.gsm_input.set_fc(self.fc)
Roman Khassraf5a804002015-08-24 20:11:10 +0200125
126 def get_arfcn(self):
127 return self.arfcn
128
129 def set_arfcn(self, arfcn):
130 self.arfcn = arfcn
131 if self.verbose or self.burst_file:
132 self.gsm_receiver.set_cell_allocation([self.arfcn])
133 if options.band:
134 new_freq = grgsm.arfcn.arfcn2downlink(self.arfcn, self.band)
135 else:
136 for band in grgsm.arfcn.get_bands():
137 if grgsm.arfcn.is_valid_arfcn(arfcn, band):
138 new_freq = grgsm.arfcn.arfcn2downlink(arfcn, band)
139 break
140 self.set_fc(new_freq)
Roman Khassraf529e2152015-08-22 19:52:41 +0200141
142 def get_gain(self):
143 return self.gain
144
145 def set_gain(self, gain):
146 self.gain = gain
Roman Khassraf529e2152015-08-22 19:52:41 +0200147
148 def get_samp_rate(self):
149 return self.samp_rate
150
151 def set_samp_rate(self, samp_rate):
152 self.samp_rate = samp_rate
Roman Khassraf529e2152015-08-22 19:52:41 +0200153 self.rtlsdr_source.set_sample_rate(self.samp_rate)
154 if self.verbose or self.burst_file:
155 self.gsm_input.set_samp_rate_in(self.samp_rate)
156
157 def get_ppm(self):
158 return self.ppm
159
160 def set_ppm(self, ppm):
161 self.ppm = ppm
162 self.set_ppm_slider(self.ppm)
163
Roman Khassraf529e2152015-08-22 19:52:41 +0200164if __name__ == '__main__':
Roman Khassraf529e2152015-08-22 19:52:41 +0200165
166 parser = OptionParser(option_class=eng_option, usage="%prog [options]",
167 description="RTL-SDR capturing app of gr-gsm.")
Roman Khassraf529e2152015-08-22 19:52:41 +0200168
169 parser.add_option("-f", "--fc", dest="fc", type="eng_float",
170 help="Set frequency [default=%default]")
171
172 parser.add_option("-a", "--arfcn", dest="arfcn", type="intx",
173 help="Set ARFCN instead of frequency. In some cases you may have to provide the GSM band also")
174
175 parser.add_option("-g", "--gain", dest="gain", type="eng_float",
176 default=eng_notation.num_to_str(30),
177 help="Set gain [default=%default]")
178
179 parser.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float",
180 default=eng_notation.num_to_str(2000000.052982),
181 help="Set samp_rate [default=%default]")
182
183 parser.add_option("-p", "--ppm", dest="ppm", type="intx", default=0,
184 help="Set ppm [default=%default]")
185
186 parser.add_option("-b", "--burst-file", dest="burst_file",
187 help="File where the captured bursts are saved")
188
189 parser.add_option("-c", "--cfile", dest="cfile",
190 help="File where the captured data are saved")
191
Roman Khassraf529e2152015-08-22 19:52:41 +0200192 bands_list = ", ".join(grgsm.arfcn.get_bands())
193 parser.add_option("--band", dest="band",
194 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 +0200195
196 parser.add_option("-v", "--verbose", action="store_true",
197 help="If set, the captured bursts are printed to stdout")
198
Roman Khassraf529e2152015-08-22 19:52:41 +0200199 (options, args) = parser.parse_args()
200
201 if options.cfile is None and options.burst_file is None:
202 parser.error("Please provide a cfile or a burst file (or both) to save the captured data\n")
203
204 if (options.fc is None and options.arfcn is None) or (options.fc is not None and options.arfcn is not None):
205 parser.error("You have to provide either a frequency or an ARFCN (but not both).\n")
206
207 arfcn = 0
208 fc = 939.4e6
209 if options.arfcn:
210 if options.band:
211 if options.band not in grgsm.arfcn.get_bands():
212 parser.error("Invalid GSM band\n")
213 elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band):
214 parser.error("ARFCN is not valid in the specified band\n")
215 else:
216 arfcn = options.arfcn
217 fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band)
218 else:
219 arfcn = options.arfcn
220 for band in grgsm.arfcn.get_bands():
221 if grgsm.arfcn.is_valid_arfcn(arfcn, band):
222 fc = grgsm.arfcn.arfcn2downlink(arfcn, band)
223 break
224 elif options.fc:
225 fc = options.fc
226 if options.band:
227 if options.band not in grgsm.arfcn.get_bands():
228 parser.error("Invalid GSM band\n")
229 elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band):
230 parser.error("Frequency is not valid in the specified band\n")
231 else:
232 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band)
233 else:
234 for band in grgsm.arfcn.get_bands():
235 if grgsm.arfcn.is_valid_downlink(options.fc, band):
236 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band)
237 break
238
Roman Khassraf529e2152015-08-22 19:52:41 +0200239 tb = airprobe_rtlsdr_capture(fc=fc, gain=options.gain, samp_rate=options.samp_rate,
Roman Khassraf7eda4802015-08-24 20:00:48 +0200240 ppm=options.ppm, arfcn=arfcn, cfile=options.cfile,
Roman Khassraf5a804002015-08-24 20:11:10 +0200241 burst_file=options.burst_file, band=options.band, verbose=options.verbose)
Roman Khassraf7eda4802015-08-24 20:00:48 +0200242
243 def signal_handler(signal, frame):
244 tb.stop()
245 tb.wait()
246 sys.exit(0)
247
248 signal.signal(signal.SIGINT, signal_handler)
Roman Khassraf529e2152015-08-22 19:52:41 +0200249
250 tb.start()
Roman Khassraf7eda4802015-08-24 20:00:48 +0200251 tb.wait()