blob: c68986b3d609ff1bc80450a096d4dc484164b322 [file] [log] [blame]
Roman Khassraf2b1ebf12015-08-29 17:43:43 +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
24from gnuradio import blocks
25from gnuradio import eng_notation
26from gnuradio import gr
27from gnuradio.eng_option import eng_option
28from optparse import OptionParser, OptionGroup
29import collections
30import grgsm
Roman Khassraf262b53d2015-09-13 12:14:26 +020031import pmt
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020032import socket
33
34
Piotr Krysik3e192c42016-02-13 07:31:51 +010035class grgsm_decoder(gr.top_block):
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020036
37 def __init__(self, timeslot=0, subslot=None, chan_mode='BCCH',
38 burst_file=None,
Piotr Krysik8040e012016-03-08 21:42:11 +010039 cfile=None, fc=939.4e6, samp_rate=2e6,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020040 a5=1, a5_kc=None,
Roman Khassraf262b53d2015-09-13 12:14:26 +020041 speech_file=None, speech_codec=None,
Piotr Krysik0679ceb2016-04-29 11:16:22 +020042 verbose=False,
43 print_bursts=False):
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020044
Piotr Krysik3e192c42016-02-13 07:31:51 +010045 gr.top_block.__init__(self, "Gr-gsm Decode")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020046
47 ##################################################
48 # Parameters
49 ##################################################
50 self.timeslot = timeslot
51 self.subslot = subslot
52 self.chan_mode = chan_mode
53 self.burst_file = burst_file
54 self.cfile = cfile
55 self.fc = fc
56 self.samp_rate = samp_rate
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020057 self.a5 = a5
58 self.kc = a5_kc
59 if len(a5_kc) < 8:
60 self.kc = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
61 self.speech_file = speech_file
62 self.speech_codec = speech_codec
Roman Khassraf262b53d2015-09-13 12:14:26 +020063 self.verbose = verbose
Piotr Krysik0679ceb2016-04-29 11:16:22 +020064 self.print_bursts = print_bursts
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020065
66 ##################################################
67 # Blocks
68 ##################################################
69
70 if self.burst_file:
71 self.burst_file_source = grgsm.burst_file_source(burst_file)
72 elif self.cfile:
73 self.file_source = blocks.file_source(gr.sizeof_gr_complex*1, self.cfile, False)
74 self.receiver = grgsm.receiver(4, ([0]), ([]))
Piotr Krysik8040e012016-03-08 21:42:11 +010075 if self.fc is not None:
76 self.input_adapter = grgsm.gsm_input(ppm=0, osr=4, fc=self.fc, samp_rate_in=samp_rate)
77 self.offset_control = grgsm.clock_offset_control(self.fc)
78 else:
79 self.input_adapter = grgsm.gsm_input(ppm=0, osr=4, samp_rate_in=samp_rate)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020080
81 self.dummy_burst_filter = grgsm.dummy_burst_filter()
82 self.timeslot_filter = grgsm.burst_timeslot_filter(self.timeslot)
83
84 self.subslot_filter = None
85 if self.chan_mode == 'BCCH_SDCCH4' and self.subslot is not None:
86 self.subslot_filter = grgsm.burst_sdcch_subslot_filter(grgsm.SS_FILTER_SDCCH4, self.subslot)
87 elif self.chan_mode == 'SDCCH8' and self.subslot is not None:
88 self.subslot_filter = grgsm.burst_sdcch_subslot_filter(grgsm.SS_FILTER_SDCCH8, self.subslot)
89
90 if self.chan_mode == 'BCCH':
91 self.bcch_demapper = grgsm.universal_ctrl_chans_demapper(self.timeslot,
92 ([2, 6, 12, 16, 22, 26, 32, 36, 42, 46]),
93 ([1, 2, 2, 2, 2, 2, 2, 2, 2, 2]))
94 elif self.chan_mode == 'BCCH_SDCCH4':
95 self.bcch_sdcch4_demapper = grgsm.universal_ctrl_chans_demapper(self.timeslot,
96 ([2, 6, 12, 16, 22, 26, 32, 36, 42, 46]),
97 ([1, 2, 2, 2, 7, 7, 7, 7, 135, 135]))
98 elif self.chan_mode == 'SDCCH8':
99 self.sdcch8_demapper = grgsm.universal_ctrl_chans_demapper(self.timeslot,
100 ([0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44]),
101 ([8, 8, 8, 8, 8, 8, 8, 8, 136, 136, 136, 136]))
102 elif self.chan_mode == 'TCHF':
103 self.tch_f_demapper = grgsm.tch_f_chans_demapper(self.timeslot)
104 self.tch_f_decoder = grgsm.tch_f_decoder(speech_codec, speech_file)
105
106 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
107 self.decryption = grgsm.decryption(self.kc, self.a5)
108 self.cch_decoder_decrypted = grgsm.control_channels_decoder()
109 if self.chan_mode == 'TCHF':
110 self.decryption_tch_sacch = grgsm.decryption(self.kc, self.a5)
111
112 self.cch_decoder = grgsm.control_channels_decoder()
113
Piotr Krysik72a16172016-02-22 14:30:02 +0100114 self.socket_pdu = blocks.socket_pdu("UDP_CLIENT", "127.0.0.1", "4729", 10000)
Roman Khassraf262b53d2015-09-13 12:14:26 +0200115 if self.verbose:
116 self.message_printer = grgsm.message_printer(pmt.intern(""), True, True, False)
117
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200118 if self.print_bursts:
119 self.bursts_printer = grgsm.bursts_printer(pmt.intern(""), True, True, False, True)
120
121
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200122
123 ##################################################
124 # Asynch Message Connections
125 ##################################################
126
127 if self.burst_file:
128 self.msg_connect(self.burst_file_source, "out", self.dummy_burst_filter, "in")
129 elif self.cfile:
130 self.connect((self.file_source, 0), (self.input_adapter, 0))
131 self.connect((self.input_adapter, 0), (self.receiver, 0))
Piotr Krysik8040e012016-03-08 21:42:11 +0100132 if self.fc is not None:
133 self.msg_connect(self.offset_control, "ppm", self.input_adapter, "ppm_in")
134 self.msg_connect(self.receiver, "measurements", self.offset_control, "measurements")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200135 self.msg_connect(self.receiver, "C0", self.dummy_burst_filter, "in")
136
137 self.msg_connect(self.dummy_burst_filter, "out", self.timeslot_filter, "in")
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200138 if self.print_bursts:
139 self.msg_connect(self.timeslot_filter, "out", self.bursts_printer, 'bursts')
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200140
141 if (self.chan_mode == 'BCCH_SDCCH4' or self.chan_mode == 'SDCCH8') and self.subslot_filter is not None:
142 self.msg_connect(self.timeslot_filter, "out", self.subslot_filter, "in")
143
144 if self.chan_mode == 'BCCH':
145 if self.subslot_filter is not None:
146 self.msg_connect(self.subslot_filter, "out", self.bcch_demapper, "bursts")
147 else:
148 self.msg_connect(self.timeslot_filter, "out", self.bcch_demapper, "bursts")
149
150 self.msg_connect(self.bcch_demapper, "bursts", self.cch_decoder, "bursts")
151 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200152 if self.verbose:
153 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200154
155 elif self.chan_mode == 'BCCH_SDCCH4':
156 if self.subslot_filter is not None:
157 self.msg_connect(self.subslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
158 else:
159 self.msg_connect(self.timeslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
160
161 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
162 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.decryption, "bursts")
163 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
164 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200165 if self.verbose:
166 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200167
168 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.cch_decoder, "bursts")
169 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200170 if self.verbose:
171 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200172
173 elif self.chan_mode == 'SDCCH8':
174 if self.subslot_filter is not None:
175 self.msg_connect(self.subslot_filter, "out", self.sdcch8_demapper, "bursts")
176 else:
177 self.msg_connect(self.timeslot_filter, "out", self.sdcch8_demapper, "bursts")
178
179 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
180 self.msg_connect(self.sdcch8_demapper, "bursts", self.decryption, "bursts")
181 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
182 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200183 if self.verbose:
184 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200185
186 self.msg_connect(self.sdcch8_demapper, "bursts", self.cch_decoder, "bursts")
187 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200188 if self.verbose:
189 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200190
191 elif self.chan_mode == 'TCHF':
192 self.msg_connect(self.timeslot_filter, "out", self.tch_f_demapper, "bursts")
193 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
194 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.decryption_tch_sacch, "bursts")
195 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.decryption, "bursts")
196
197 self.msg_connect(self.decryption_tch_sacch, "bursts", self.cch_decoder, "bursts")
198 self.msg_connect(self.decryption, "bursts", self.tch_f_decoder, "bursts")
199 else:
200 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.cch_decoder, "bursts")
201 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.tch_f_decoder, "bursts")
202
203 self.msg_connect(self.tch_f_decoder, "msgs", self.socket_pdu, "pdus")
204 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200205 if self.verbose:
206 self.msg_connect(self.tch_f_decoder, "msgs", self.message_printer, "msgs")
207 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200208
209
210if __name__ == '__main__':
211
212 # List of channel configurations
213 channel_modes = ['BCCH', 'BCCH_SDCCH4', 'SDCCH8', 'TCHF']
214
215 # mapping options to grgsm's enums
216 tch_codecs = collections.OrderedDict([
217 ('FR', grgsm.TCH_FS),
218 ('EFR', grgsm.TCH_EFR),
219 ('AMR12.2', grgsm.TCH_AFS12_2),
220 ('AMR10.2', grgsm.TCH_AFS10_2),
221 ('AMR7.95', grgsm.TCH_AFS7_95),
222 ('AMR7.4', grgsm.TCH_AFS7_4),
223 ('AMR6.7', grgsm.TCH_AFS6_7),
224 ('AMR5.9', grgsm.TCH_AFS5_9),
225 ('AMR5.15', grgsm.TCH_AFS5_15),
226 ('AMR4.75', grgsm.TCH_AFS4_75)
227 ])
228
229 kc = []
230
231 parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
232
233 def kc_callback(option, opt_str, value, parser):
234
235 """ Callback function that parses Kc """
236
237 # format 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF
238 if ',' in value:
239 value_str = value.split(',')
240
241 for s in value_str:
242 val = int(s, 16)
243 if val < 0 or val > 255:
244 parser.error("Invalid Kc % s\n" % s)
245 kc.append(val)
246 if len(kc) != 8:
247 parser.error("Invalid Kc length")
248 # format: 1234567890ABCDEF
249 elif len(value) == 16:
250 for i in range(8):
251 s = value[2*i: 2*i + 2]
252 val = int(s, 16)
253 if val < 0 or val > 255:
254 parser.error("Invalid Kc % s\n" % s)
255 kc.append(val)
256 else:
257 parser.error("Invalid Kc format")
258
259 # define options
260 parser.add_option("-m", "--mode", dest="chan_mode", default='BCCH',
261 type='choice', choices=channel_modes,
262 help="Channel mode. Valid options are 'BCCH' (Non-combined C0), "
263 "'BCCH_SDCCH4'(Combined C0), 'SDCCH8' (Stand-alone control channel) "
264 "and 'TCHF' (Traffic Channel, Full rate) ")
265 parser.add_option("-t", "--timeslot", dest="timeslot", type="intx", default=0,
266 help="Timeslot to decode [default=%default]")
267 parser.add_option("-u", "--subslot", dest="subslot", type="intx",
268 help="Subslot to decode. Use in combination with channel type BCCH_SDCCH4 and SDCCH8")
269 parser.add_option("-b", "--burst-file", dest="burst_file", help="Input file (bursts)")
270 parser.add_option("-c", "--cfile", dest="cfile", help="Input file (cfile)")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200271 parser.add_option("-v", "--verbose", action="store_true",
272 help="If set, the decoded messages (with frame number and count) are printed to stdout")
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200273 parser.add_option("-p", "--print-bursts", action="store_true",
274 help="If set, the raw bursts (with frame number and count) are printed to stdout")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200275
276 # group cfile options together
277 cfile_options = OptionGroup(
278 parser, 'Cfile Options', 'Options for decoding cfile input.',
279 )
280 cfile_options.add_option("-f", "--fc", dest="fc", type="eng_float",
281 help="Frequency of cfile capture [default=eng_notation.num_to_str(939.4e6)]")
282 cfile_options.add_option("-a", "--arfcn", dest="arfcn", type="intx",
283 help="Set ARFCN instead of frequency. "
284 "In some cases you may have to provide the GSM band also")
285 cfile_options.add_option("--band", dest="band",
286 help="Specify the GSM band for the frequency.\nAvailable bands are: "
287 + ", ".join(grgsm.arfcn.get_bands()) + "."
288 + "If no band is specified, it will be determined automatically, defaulting to 0.")
289 cfile_options.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float",
290 default=eng_notation.num_to_str(2e6),
291 help="Sample rate of cfile capture [default=%default]")
292 parser.add_option_group(cfile_options)
293
294 # group decryption options
295 decryption_options = OptionGroup(
296 parser, 'Decryption Options', 'Options for setting the A5 decryption parameters.',
297 )
298 decryption_options.add_option("-e", "--a5", dest="a5", type="intx", default=1,
299 help="A5 version [default=%default]. A5 versions 1 - 3 supported")
300 decryption_options.add_option("-k", "--kc", action="callback", callback=kc_callback, type="string",
301 help="A5 session key Kc. Valid formats are "
302 "'0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF' and '1234567890ABCDEF'")
303 parser.add_option_group(decryption_options)
304
305 # group TCH options
306 tch_options = OptionGroup(
307 parser, 'TCH Options', 'Options for setting Traffic channel decoding parameters.',
308 )
309 tch_options.add_option("-d", "--speech-codec", dest="speech_codec", default='FR',
310 type='choice', choices=tch_codecs.keys(),
311 help="TCH-F speech codec [default=%default]. "
312 "Valid options are " + ", ".join(tch_codecs.keys()))
313 tch_options.add_option("-o", "--output-tch", dest="speech_output_file", default="/tmp/speech.au.gsm",
314 help="TCH/F speech output file [default=%default].")
315 parser.add_option_group(tch_options)
316
317 # parse
318 (options, args) = parser.parse_args()
319
320 # some verifications
321 if (options.cfile is None and options.burst_file is None) or \
322 (options.cfile is not None and options.burst_file is not None):
323 parser.error("Please provide a cfile or a burst file (but not both) as input\n")
324
325 if options.timeslot < 0 or options.timeslot > 7:
326 parser.error("Invalid timeslot. Must be a in range 0-7\n")
327
328 if options.subslot is not None and (options.subslot < 0 or options.subslot > 7):
329 parser.error("Invalid subslot. Must be a in range 0-7\n")
330
331 if options.a5 < 0 or options.a5 > 3:
332 parser.error("Invalid A5 version\n")
333
Roman Khassraff4307482015-09-13 12:23:59 +0200334 if options.cfile and (options.fc is None and options.arfcn is None) or (options.fc is not None and options.arfcn is not None):
Piotr Krysik8040e012016-03-08 21:42:11 +0100335 print("You haven't provided a frequency or an ARFCN - working without automatic frequency offset correction.\n")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200336
337 # handle frequency / arfcn input
338 arfcn = 0
339 fc = 939.4e6
340 if options.arfcn:
341 if options.band:
342 if options.band not in grgsm.arfcn.get_bands():
343 parser.error("Invalid GSM band\n")
344 elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band):
345 parser.error("ARFCN is not valid in the specified band\n")
346 else:
347 arfcn = options.arfcn
348 fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band)
349 else:
350 arfcn = options.arfcn
351 for band in grgsm.arfcn.get_bands():
352 if grgsm.arfcn.is_valid_arfcn(arfcn, band):
353 fc = grgsm.arfcn.arfcn2downlink(arfcn, band)
354 break
355 elif options.fc:
356 fc = options.fc
357 if options.band:
358 if options.band not in grgsm.arfcn.get_bands():
359 parser.error("Invalid GSM band\n")
360 elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band):
361 parser.error("Frequency is not valid in the specified band\n")
362 else:
363 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band)
364 else:
365 for band in grgsm.arfcn.get_bands():
366 if grgsm.arfcn.is_valid_downlink(options.fc, band):
367 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band)
368 break
369
370 # open udp port 4729 to avoid icmp messages
371 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
372 sock.bind(("localhost", 4729))
373
374 # instanciate decoder
Piotr Krysik3e192c42016-02-13 07:31:51 +0100375 tb = grgsm_decoder(timeslot=options.timeslot, subslot=options.subslot, chan_mode=options.chan_mode,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200376 burst_file=options.burst_file,
Piotr Krysik8040e012016-03-08 21:42:11 +0100377 cfile=options.cfile, fc=fc, samp_rate=options.samp_rate,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200378 a5=options.a5, a5_kc=kc,
Roman Khassraf262b53d2015-09-13 12:14:26 +0200379 speech_file=options.speech_output_file, speech_codec=tch_codecs.get(options.speech_codec),
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200380 verbose=options.verbose,
381 print_bursts=options.print_bursts)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200382
383 # run
384 tb.start()
385 tb.wait()
386
387 # we are done, close socket
388 sock.close()