blob: 698cf805991d38a11bd4ed56c679b882ea199a7f [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,
42 verbose=False):
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020043
Piotr Krysik3e192c42016-02-13 07:31:51 +010044 gr.top_block.__init__(self, "Gr-gsm Decode")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020045
46 ##################################################
47 # Parameters
48 ##################################################
49 self.timeslot = timeslot
50 self.subslot = subslot
51 self.chan_mode = chan_mode
52 self.burst_file = burst_file
53 self.cfile = cfile
54 self.fc = fc
55 self.samp_rate = samp_rate
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020056 self.a5 = a5
57 self.kc = a5_kc
58 if len(a5_kc) < 8:
59 self.kc = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
60 self.speech_file = speech_file
61 self.speech_codec = speech_codec
Roman Khassraf262b53d2015-09-13 12:14:26 +020062 self.verbose = verbose
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020063
64 ##################################################
65 # Blocks
66 ##################################################
67
68 if self.burst_file:
69 self.burst_file_source = grgsm.burst_file_source(burst_file)
70 elif self.cfile:
71 self.file_source = blocks.file_source(gr.sizeof_gr_complex*1, self.cfile, False)
72 self.receiver = grgsm.receiver(4, ([0]), ([]))
Piotr Krysik8040e012016-03-08 21:42:11 +010073 if self.fc is not None:
74 self.input_adapter = grgsm.gsm_input(ppm=0, osr=4, fc=self.fc, samp_rate_in=samp_rate)
75 self.offset_control = grgsm.clock_offset_control(self.fc)
76 else:
77 self.input_adapter = grgsm.gsm_input(ppm=0, osr=4, samp_rate_in=samp_rate)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020078
79 self.dummy_burst_filter = grgsm.dummy_burst_filter()
80 self.timeslot_filter = grgsm.burst_timeslot_filter(self.timeslot)
81
82 self.subslot_filter = None
83 if self.chan_mode == 'BCCH_SDCCH4' and self.subslot is not None:
84 self.subslot_filter = grgsm.burst_sdcch_subslot_filter(grgsm.SS_FILTER_SDCCH4, self.subslot)
85 elif self.chan_mode == 'SDCCH8' and self.subslot is not None:
86 self.subslot_filter = grgsm.burst_sdcch_subslot_filter(grgsm.SS_FILTER_SDCCH8, self.subslot)
87
88 if self.chan_mode == 'BCCH':
89 self.bcch_demapper = grgsm.universal_ctrl_chans_demapper(self.timeslot,
90 ([2, 6, 12, 16, 22, 26, 32, 36, 42, 46]),
91 ([1, 2, 2, 2, 2, 2, 2, 2, 2, 2]))
92 elif self.chan_mode == 'BCCH_SDCCH4':
93 self.bcch_sdcch4_demapper = grgsm.universal_ctrl_chans_demapper(self.timeslot,
94 ([2, 6, 12, 16, 22, 26, 32, 36, 42, 46]),
95 ([1, 2, 2, 2, 7, 7, 7, 7, 135, 135]))
96 elif self.chan_mode == 'SDCCH8':
97 self.sdcch8_demapper = grgsm.universal_ctrl_chans_demapper(self.timeslot,
98 ([0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44]),
99 ([8, 8, 8, 8, 8, 8, 8, 8, 136, 136, 136, 136]))
100 elif self.chan_mode == 'TCHF':
101 self.tch_f_demapper = grgsm.tch_f_chans_demapper(self.timeslot)
102 self.tch_f_decoder = grgsm.tch_f_decoder(speech_codec, speech_file)
103
104 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
105 self.decryption = grgsm.decryption(self.kc, self.a5)
106 self.cch_decoder_decrypted = grgsm.control_channels_decoder()
107 if self.chan_mode == 'TCHF':
108 self.decryption_tch_sacch = grgsm.decryption(self.kc, self.a5)
109
110 self.cch_decoder = grgsm.control_channels_decoder()
111
Piotr Krysik72a16172016-02-22 14:30:02 +0100112 self.socket_pdu = blocks.socket_pdu("UDP_CLIENT", "127.0.0.1", "4729", 10000)
Roman Khassraf262b53d2015-09-13 12:14:26 +0200113 if self.verbose:
114 self.message_printer = grgsm.message_printer(pmt.intern(""), True, True, False)
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")
132
133 if (self.chan_mode == 'BCCH_SDCCH4' or self.chan_mode == 'SDCCH8') and self.subslot_filter is not None:
134 self.msg_connect(self.timeslot_filter, "out", self.subslot_filter, "in")
135
136 if self.chan_mode == 'BCCH':
137 if self.subslot_filter is not None:
138 self.msg_connect(self.subslot_filter, "out", self.bcch_demapper, "bursts")
139 else:
140 self.msg_connect(self.timeslot_filter, "out", self.bcch_demapper, "bursts")
141
142 self.msg_connect(self.bcch_demapper, "bursts", self.cch_decoder, "bursts")
143 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200144 if self.verbose:
145 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200146
147 elif self.chan_mode == 'BCCH_SDCCH4':
148 if self.subslot_filter is not None:
149 self.msg_connect(self.subslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
150 else:
151 self.msg_connect(self.timeslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
152
153 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
154 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.decryption, "bursts")
155 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
156 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200157 if self.verbose:
158 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200159
160 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.cch_decoder, "bursts")
161 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200162 if self.verbose:
163 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200164
165 elif self.chan_mode == 'SDCCH8':
166 if self.subslot_filter is not None:
167 self.msg_connect(self.subslot_filter, "out", self.sdcch8_demapper, "bursts")
168 else:
169 self.msg_connect(self.timeslot_filter, "out", self.sdcch8_demapper, "bursts")
170
171 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
172 self.msg_connect(self.sdcch8_demapper, "bursts", self.decryption, "bursts")
173 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
174 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200175 if self.verbose:
176 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200177
178 self.msg_connect(self.sdcch8_demapper, "bursts", self.cch_decoder, "bursts")
179 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200180 if self.verbose:
181 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200182
183 elif self.chan_mode == 'TCHF':
184 self.msg_connect(self.timeslot_filter, "out", self.tch_f_demapper, "bursts")
185 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
186 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.decryption_tch_sacch, "bursts")
187 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.decryption, "bursts")
188
189 self.msg_connect(self.decryption_tch_sacch, "bursts", self.cch_decoder, "bursts")
190 self.msg_connect(self.decryption, "bursts", self.tch_f_decoder, "bursts")
191 else:
192 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.cch_decoder, "bursts")
193 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.tch_f_decoder, "bursts")
194
195 self.msg_connect(self.tch_f_decoder, "msgs", self.socket_pdu, "pdus")
196 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200197 if self.verbose:
198 self.msg_connect(self.tch_f_decoder, "msgs", self.message_printer, "msgs")
199 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200200
201
202if __name__ == '__main__':
203
204 # List of channel configurations
205 channel_modes = ['BCCH', 'BCCH_SDCCH4', 'SDCCH8', 'TCHF']
206
207 # mapping options to grgsm's enums
208 tch_codecs = collections.OrderedDict([
209 ('FR', grgsm.TCH_FS),
210 ('EFR', grgsm.TCH_EFR),
211 ('AMR12.2', grgsm.TCH_AFS12_2),
212 ('AMR10.2', grgsm.TCH_AFS10_2),
213 ('AMR7.95', grgsm.TCH_AFS7_95),
214 ('AMR7.4', grgsm.TCH_AFS7_4),
215 ('AMR6.7', grgsm.TCH_AFS6_7),
216 ('AMR5.9', grgsm.TCH_AFS5_9),
217 ('AMR5.15', grgsm.TCH_AFS5_15),
218 ('AMR4.75', grgsm.TCH_AFS4_75)
219 ])
220
221 kc = []
222
223 parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
224
225 def kc_callback(option, opt_str, value, parser):
226
227 """ Callback function that parses Kc """
228
229 # format 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF
230 if ',' in value:
231 value_str = value.split(',')
232
233 for s in value_str:
234 val = int(s, 16)
235 if val < 0 or val > 255:
236 parser.error("Invalid Kc % s\n" % s)
237 kc.append(val)
238 if len(kc) != 8:
239 parser.error("Invalid Kc length")
240 # format: 1234567890ABCDEF
241 elif len(value) == 16:
242 for i in range(8):
243 s = value[2*i: 2*i + 2]
244 val = int(s, 16)
245 if val < 0 or val > 255:
246 parser.error("Invalid Kc % s\n" % s)
247 kc.append(val)
248 else:
249 parser.error("Invalid Kc format")
250
251 # define options
252 parser.add_option("-m", "--mode", dest="chan_mode", default='BCCH',
253 type='choice', choices=channel_modes,
254 help="Channel mode. Valid options are 'BCCH' (Non-combined C0), "
255 "'BCCH_SDCCH4'(Combined C0), 'SDCCH8' (Stand-alone control channel) "
256 "and 'TCHF' (Traffic Channel, Full rate) ")
257 parser.add_option("-t", "--timeslot", dest="timeslot", type="intx", default=0,
258 help="Timeslot to decode [default=%default]")
259 parser.add_option("-u", "--subslot", dest="subslot", type="intx",
260 help="Subslot to decode. Use in combination with channel type BCCH_SDCCH4 and SDCCH8")
261 parser.add_option("-b", "--burst-file", dest="burst_file", help="Input file (bursts)")
262 parser.add_option("-c", "--cfile", dest="cfile", help="Input file (cfile)")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200263 parser.add_option("-v", "--verbose", action="store_true",
264 help="If set, the decoded messages (with frame number and count) are printed to stdout")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200265
266 # group cfile options together
267 cfile_options = OptionGroup(
268 parser, 'Cfile Options', 'Options for decoding cfile input.',
269 )
270 cfile_options.add_option("-f", "--fc", dest="fc", type="eng_float",
271 help="Frequency of cfile capture [default=eng_notation.num_to_str(939.4e6)]")
272 cfile_options.add_option("-a", "--arfcn", dest="arfcn", type="intx",
273 help="Set ARFCN instead of frequency. "
274 "In some cases you may have to provide the GSM band also")
275 cfile_options.add_option("--band", dest="band",
276 help="Specify the GSM band for the frequency.\nAvailable bands are: "
277 + ", ".join(grgsm.arfcn.get_bands()) + "."
278 + "If no band is specified, it will be determined automatically, defaulting to 0.")
279 cfile_options.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float",
Piotr Krysik13d9e7a2016-04-24 15:23:15 +0200280 default=eng_notation.num_to_str(1e6),
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200281 help="Sample rate of cfile capture [default=%default]")
282 parser.add_option_group(cfile_options)
283
284 # group decryption options
285 decryption_options = OptionGroup(
286 parser, 'Decryption Options', 'Options for setting the A5 decryption parameters.',
287 )
288 decryption_options.add_option("-e", "--a5", dest="a5", type="intx", default=1,
289 help="A5 version [default=%default]. A5 versions 1 - 3 supported")
290 decryption_options.add_option("-k", "--kc", action="callback", callback=kc_callback, type="string",
291 help="A5 session key Kc. Valid formats are "
292 "'0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF' and '1234567890ABCDEF'")
293 parser.add_option_group(decryption_options)
294
295 # group TCH options
296 tch_options = OptionGroup(
297 parser, 'TCH Options', 'Options for setting Traffic channel decoding parameters.',
298 )
299 tch_options.add_option("-d", "--speech-codec", dest="speech_codec", default='FR',
300 type='choice', choices=tch_codecs.keys(),
301 help="TCH-F speech codec [default=%default]. "
302 "Valid options are " + ", ".join(tch_codecs.keys()))
303 tch_options.add_option("-o", "--output-tch", dest="speech_output_file", default="/tmp/speech.au.gsm",
304 help="TCH/F speech output file [default=%default].")
305 parser.add_option_group(tch_options)
306
307 # parse
308 (options, args) = parser.parse_args()
309
310 # some verifications
311 if (options.cfile is None and options.burst_file is None) or \
312 (options.cfile is not None and options.burst_file is not None):
313 parser.error("Please provide a cfile or a burst file (but not both) as input\n")
314
315 if options.timeslot < 0 or options.timeslot > 7:
316 parser.error("Invalid timeslot. Must be a in range 0-7\n")
317
318 if options.subslot is not None and (options.subslot < 0 or options.subslot > 7):
319 parser.error("Invalid subslot. Must be a in range 0-7\n")
320
321 if options.a5 < 0 or options.a5 > 3:
322 parser.error("Invalid A5 version\n")
323
Roman Khassraff4307482015-09-13 12:23:59 +0200324 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 +0100325 print("You haven't provided a frequency or an ARFCN - working without automatic frequency offset correction.\n")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200326
327 # handle frequency / arfcn input
328 arfcn = 0
329 fc = 939.4e6
330 if options.arfcn:
331 if options.band:
332 if options.band not in grgsm.arfcn.get_bands():
333 parser.error("Invalid GSM band\n")
334 elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band):
335 parser.error("ARFCN is not valid in the specified band\n")
336 else:
337 arfcn = options.arfcn
338 fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band)
339 else:
340 arfcn = options.arfcn
341 for band in grgsm.arfcn.get_bands():
342 if grgsm.arfcn.is_valid_arfcn(arfcn, band):
343 fc = grgsm.arfcn.arfcn2downlink(arfcn, band)
344 break
345 elif options.fc:
346 fc = options.fc
347 if options.band:
348 if options.band not in grgsm.arfcn.get_bands():
349 parser.error("Invalid GSM band\n")
350 elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band):
351 parser.error("Frequency is not valid in the specified band\n")
352 else:
353 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band)
354 else:
355 for band in grgsm.arfcn.get_bands():
356 if grgsm.arfcn.is_valid_downlink(options.fc, band):
357 arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band)
358 break
359
360 # open udp port 4729 to avoid icmp messages
361 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
362 sock.bind(("localhost", 4729))
363
364 # instanciate decoder
Piotr Krysik3e192c42016-02-13 07:31:51 +0100365 tb = grgsm_decoder(timeslot=options.timeslot, subslot=options.subslot, chan_mode=options.chan_mode,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200366 burst_file=options.burst_file,
Piotr Krysik8040e012016-03-08 21:42:11 +0100367 cfile=options.cfile, fc=fc, samp_rate=options.samp_rate,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200368 a5=options.a5, a5_kc=kc,
Roman Khassraf262b53d2015-09-13 12:14:26 +0200369 speech_file=options.speech_output_file, speech_codec=tch_codecs.get(options.speech_codec),
370 verbose=options.verbose)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200371
372 # run
373 tb.start()
374 tb.wait()
375
376 # we are done, close socket
377 sock.close()