blob: 8f412bfbfe3e89d0b54e2ef402e97928fc23040b [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':
Piotr Krysik773a1942016-05-20 12:45:54 +020091 self.bcch_demapper = grgsm.gsm_bcch_ccch_demapper(self.timeslot)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020092 elif self.chan_mode == 'BCCH_SDCCH4':
Piotr Krysik773a1942016-05-20 12:45:54 +020093 self.bcch_sdcch4_demapper = grgsm.gsm_bcch_ccch_sdcch4_demapper(self.timeslot)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020094 elif self.chan_mode == 'SDCCH8':
Piotr Krysik773a1942016-05-20 12:45:54 +020095 self.sdcch8_demapper = grgsm.gsm_sdcch8_demapper(self.timeslot)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020096 elif self.chan_mode == 'TCHF':
97 self.tch_f_demapper = grgsm.tch_f_chans_demapper(self.timeslot)
98 self.tch_f_decoder = grgsm.tch_f_decoder(speech_codec, speech_file)
99
100 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
101 self.decryption = grgsm.decryption(self.kc, self.a5)
102 self.cch_decoder_decrypted = grgsm.control_channels_decoder()
103 if self.chan_mode == 'TCHF':
104 self.decryption_tch_sacch = grgsm.decryption(self.kc, self.a5)
105
106 self.cch_decoder = grgsm.control_channels_decoder()
107
Piotr Krysik72a16172016-02-22 14:30:02 +0100108 self.socket_pdu = blocks.socket_pdu("UDP_CLIENT", "127.0.0.1", "4729", 10000)
Roman Khassraf262b53d2015-09-13 12:14:26 +0200109 if self.verbose:
110 self.message_printer = grgsm.message_printer(pmt.intern(""), True, True, False)
111
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200112 if self.print_bursts:
Piotr Krysik7685bc02016-04-29 12:09:01 +0200113 self.bursts_printer = grgsm.bursts_printer(pmt.intern(""), True, True, True, True)
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200114
115
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200116
117 ##################################################
118 # Asynch Message Connections
119 ##################################################
120
121 if self.burst_file:
122 self.msg_connect(self.burst_file_source, "out", self.dummy_burst_filter, "in")
123 elif self.cfile:
124 self.connect((self.file_source, 0), (self.input_adapter, 0))
125 self.connect((self.input_adapter, 0), (self.receiver, 0))
Piotr Krysik8040e012016-03-08 21:42:11 +0100126 if self.fc is not None:
127 self.msg_connect(self.offset_control, "ppm", self.input_adapter, "ppm_in")
128 self.msg_connect(self.receiver, "measurements", self.offset_control, "measurements")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200129 self.msg_connect(self.receiver, "C0", self.dummy_burst_filter, "in")
130
131 self.msg_connect(self.dummy_burst_filter, "out", self.timeslot_filter, "in")
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200132 if self.print_bursts:
133 self.msg_connect(self.timeslot_filter, "out", self.bursts_printer, 'bursts')
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200134
135 if (self.chan_mode == 'BCCH_SDCCH4' or self.chan_mode == 'SDCCH8') and self.subslot_filter is not None:
136 self.msg_connect(self.timeslot_filter, "out", self.subslot_filter, "in")
137
138 if self.chan_mode == 'BCCH':
139 if self.subslot_filter is not None:
140 self.msg_connect(self.subslot_filter, "out", self.bcch_demapper, "bursts")
141 else:
142 self.msg_connect(self.timeslot_filter, "out", self.bcch_demapper, "bursts")
143
144 self.msg_connect(self.bcch_demapper, "bursts", self.cch_decoder, "bursts")
145 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200146 if self.verbose:
147 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200148
149 elif self.chan_mode == 'BCCH_SDCCH4':
150 if self.subslot_filter is not None:
151 self.msg_connect(self.subslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
152 else:
153 self.msg_connect(self.timeslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
154
155 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
156 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.decryption, "bursts")
157 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
158 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200159 if self.verbose:
160 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200161
162 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.cch_decoder, "bursts")
163 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200164 if self.verbose:
165 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200166
167 elif self.chan_mode == 'SDCCH8':
168 if self.subslot_filter is not None:
169 self.msg_connect(self.subslot_filter, "out", self.sdcch8_demapper, "bursts")
170 else:
171 self.msg_connect(self.timeslot_filter, "out", self.sdcch8_demapper, "bursts")
172
173 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
174 self.msg_connect(self.sdcch8_demapper, "bursts", self.decryption, "bursts")
175 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
176 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200177 if self.verbose:
178 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200179
180 self.msg_connect(self.sdcch8_demapper, "bursts", self.cch_decoder, "bursts")
181 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200182 if self.verbose:
183 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200184
185 elif self.chan_mode == 'TCHF':
186 self.msg_connect(self.timeslot_filter, "out", self.tch_f_demapper, "bursts")
187 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
188 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.decryption_tch_sacch, "bursts")
189 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.decryption, "bursts")
190
191 self.msg_connect(self.decryption_tch_sacch, "bursts", self.cch_decoder, "bursts")
192 self.msg_connect(self.decryption, "bursts", self.tch_f_decoder, "bursts")
193 else:
194 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.cch_decoder, "bursts")
195 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.tch_f_decoder, "bursts")
196
197 self.msg_connect(self.tch_f_decoder, "msgs", self.socket_pdu, "pdus")
198 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200199 if self.verbose:
200 self.msg_connect(self.tch_f_decoder, "msgs", self.message_printer, "msgs")
201 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200202
203
204if __name__ == '__main__':
205
206 # List of channel configurations
207 channel_modes = ['BCCH', 'BCCH_SDCCH4', 'SDCCH8', 'TCHF']
208
209 # mapping options to grgsm's enums
210 tch_codecs = collections.OrderedDict([
211 ('FR', grgsm.TCH_FS),
212 ('EFR', grgsm.TCH_EFR),
213 ('AMR12.2', grgsm.TCH_AFS12_2),
214 ('AMR10.2', grgsm.TCH_AFS10_2),
215 ('AMR7.95', grgsm.TCH_AFS7_95),
216 ('AMR7.4', grgsm.TCH_AFS7_4),
217 ('AMR6.7', grgsm.TCH_AFS6_7),
218 ('AMR5.9', grgsm.TCH_AFS5_9),
219 ('AMR5.15', grgsm.TCH_AFS5_15),
220 ('AMR4.75', grgsm.TCH_AFS4_75)
221 ])
222
223 kc = []
224
225 parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
226
227 def kc_callback(option, opt_str, value, parser):
228
229 """ Callback function that parses Kc """
230
231 # format 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF
232 if ',' in value:
233 value_str = value.split(',')
234
235 for s in value_str:
236 val = int(s, 16)
237 if val < 0 or val > 255:
238 parser.error("Invalid Kc % s\n" % s)
239 kc.append(val)
240 if len(kc) != 8:
241 parser.error("Invalid Kc length")
242 # format: 1234567890ABCDEF
243 elif len(value) == 16:
244 for i in range(8):
245 s = value[2*i: 2*i + 2]
246 val = int(s, 16)
247 if val < 0 or val > 255:
248 parser.error("Invalid Kc % s\n" % s)
249 kc.append(val)
250 else:
251 parser.error("Invalid Kc format")
252
253 # define options
254 parser.add_option("-m", "--mode", dest="chan_mode", default='BCCH',
255 type='choice', choices=channel_modes,
256 help="Channel mode. Valid options are 'BCCH' (Non-combined C0), "
257 "'BCCH_SDCCH4'(Combined C0), 'SDCCH8' (Stand-alone control channel) "
258 "and 'TCHF' (Traffic Channel, Full rate) ")
259 parser.add_option("-t", "--timeslot", dest="timeslot", type="intx", default=0,
260 help="Timeslot to decode [default=%default]")
261 parser.add_option("-u", "--subslot", dest="subslot", type="intx",
262 help="Subslot to decode. Use in combination with channel type BCCH_SDCCH4 and SDCCH8")
263 parser.add_option("-b", "--burst-file", dest="burst_file", help="Input file (bursts)")
264 parser.add_option("-c", "--cfile", dest="cfile", help="Input file (cfile)")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200265 parser.add_option("-v", "--verbose", action="store_true",
266 help="If set, the decoded messages (with frame number and count) are printed to stdout")
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200267 parser.add_option("-p", "--print-bursts", action="store_true",
268 help="If set, the raw bursts (with frame number and count) are printed to stdout")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200269
270 # group cfile options together
271 cfile_options = OptionGroup(
272 parser, 'Cfile Options', 'Options for decoding cfile input.',
273 )
274 cfile_options.add_option("-f", "--fc", dest="fc", type="eng_float",
275 help="Frequency of cfile capture [default=eng_notation.num_to_str(939.4e6)]")
276 cfile_options.add_option("-a", "--arfcn", dest="arfcn", type="intx",
277 help="Set ARFCN instead of frequency. "
278 "In some cases you may have to provide the GSM band also")
279 cfile_options.add_option("--band", dest="band",
280 help="Specify the GSM band for the frequency.\nAvailable bands are: "
281 + ", ".join(grgsm.arfcn.get_bands()) + "."
282 + "If no band is specified, it will be determined automatically, defaulting to 0.")
283 cfile_options.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float",
Piotr Krysik13d9e7a2016-04-24 15:23:15 +0200284 default=eng_notation.num_to_str(1e6),
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200285 help="Sample rate of cfile capture [default=%default]")
286 parser.add_option_group(cfile_options)
287
288 # group decryption options
289 decryption_options = OptionGroup(
290 parser, 'Decryption Options', 'Options for setting the A5 decryption parameters.',
291 )
292 decryption_options.add_option("-e", "--a5", dest="a5", type="intx", default=1,
293 help="A5 version [default=%default]. A5 versions 1 - 3 supported")
294 decryption_options.add_option("-k", "--kc", action="callback", callback=kc_callback, type="string",
295 help="A5 session key Kc. Valid formats are "
296 "'0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF' and '1234567890ABCDEF'")
297 parser.add_option_group(decryption_options)
298
299 # group TCH options
300 tch_options = OptionGroup(
301 parser, 'TCH Options', 'Options for setting Traffic channel decoding parameters.',
302 )
303 tch_options.add_option("-d", "--speech-codec", dest="speech_codec", default='FR',
304 type='choice', choices=tch_codecs.keys(),
305 help="TCH-F speech codec [default=%default]. "
306 "Valid options are " + ", ".join(tch_codecs.keys()))
307 tch_options.add_option("-o", "--output-tch", dest="speech_output_file", default="/tmp/speech.au.gsm",
308 help="TCH/F speech output file [default=%default].")
309 parser.add_option_group(tch_options)
310
311 # parse
312 (options, args) = parser.parse_args()
313
314 # some verifications
315 if (options.cfile is None and options.burst_file is None) or \
316 (options.cfile is not None and options.burst_file is not None):
317 parser.error("Please provide a cfile or a burst file (but not both) as input\n")
318
319 if options.timeslot < 0 or options.timeslot > 7:
320 parser.error("Invalid timeslot. Must be a in range 0-7\n")
321
322 if options.subslot is not None and (options.subslot < 0 or options.subslot > 7):
323 parser.error("Invalid subslot. Must be a in range 0-7\n")
324
325 if options.a5 < 0 or options.a5 > 3:
326 parser.error("Invalid A5 version\n")
327
Roman Khassraff4307482015-09-13 12:23:59 +0200328 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 +0100329 print("You haven't provided a frequency or an ARFCN - working without automatic frequency offset correction.\n")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200330
331 # handle frequency / arfcn input
332 arfcn = 0
333 fc = 939.4e6
334 if options.arfcn:
335 if options.band:
336 if options.band not in grgsm.arfcn.get_bands():
337 parser.error("Invalid GSM band\n")
338 elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band):
339 parser.error("ARFCN is not valid in the specified band\n")
340 else:
341 arfcn = options.arfcn
342 fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band)
343 else:
344 arfcn = options.arfcn
345 for band in grgsm.arfcn.get_bands():
346 if grgsm.arfcn.is_valid_arfcn(arfcn, band):
347 fc = grgsm.arfcn.arfcn2downlink(arfcn, band)
348 break
349 elif options.fc:
350 fc = options.fc
351 if options.band:
352 if options.band not in grgsm.arfcn.get_bands():
353 parser.error("Invalid GSM band\n")
354 elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band):
355 parser.error("Frequency is not valid in the specified band\n")
356 else:
357 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band)
358 else:
359 for band in grgsm.arfcn.get_bands():
360 if grgsm.arfcn.is_valid_downlink(options.fc, band):
361 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band)
362 break
363
364 # open udp port 4729 to avoid icmp messages
365 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
366 sock.bind(("localhost", 4729))
367
368 # instanciate decoder
Piotr Krysik3e192c42016-02-13 07:31:51 +0100369 tb = grgsm_decoder(timeslot=options.timeslot, subslot=options.subslot, chan_mode=options.chan_mode,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200370 burst_file=options.burst_file,
Piotr Krysik8040e012016-03-08 21:42:11 +0100371 cfile=options.cfile, fc=fc, samp_rate=options.samp_rate,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200372 a5=options.a5, a5_kc=kc,
Roman Khassraf262b53d2015-09-13 12:14:26 +0200373 speech_file=options.speech_output_file, speech_codec=tch_codecs.get(options.speech_codec),
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200374 verbose=options.verbose,
375 print_bursts=options.print_bursts)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200376
377 # run
378 tb.start()
379 tb.wait()
380
381 # we are done, close socket
382 sock.close()