blob: 000440f84e102890dfedb1c29fd4036e1eda2fd1 [file] [log] [blame]
Roman Khassraf2b1ebf12015-08-29 17:43:43 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# @file
Piotr Krysika6268a52017-08-23 16:02:19 +02004# @author (C) 2015 by Roman Khassraf <rkhassraf@gmail.com>
Roman Khassraf2b1ebf12015-08-29 17:43:43 +02005# @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
Vasil Velichkov9feb7da2019-09-02 20:26:52 +030033import sys
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020034
35
Piotr Krysik3e192c42016-02-13 07:31:51 +010036class grgsm_decoder(gr.top_block):
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020037
38 def __init__(self, timeslot=0, subslot=None, chan_mode='BCCH',
39 burst_file=None,
Piotr Krysik231aca72016-07-17 10:44:43 +020040 cfile=None, fc=None, samp_rate=2e6,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020041 a5=1, a5_kc=None,
Roman Khassraf262b53d2015-09-13 12:14:26 +020042 speech_file=None, speech_codec=None,
Roman Khassrafc0addfb2016-09-27 20:11:42 +020043 enable_voice_boundary_detection=False,
Vasil Velichkov7f259fd2018-05-06 02:13:36 +030044 tch_h_channel=0,
45 multi_rate=0,
Piotr Krysik0679ceb2016-04-29 11:16:22 +020046 verbose=False,
Piotr Krysik231aca72016-07-17 10:44:43 +020047 print_bursts=False, ppm=0):
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020048
Piotr Krysik3e192c42016-02-13 07:31:51 +010049 gr.top_block.__init__(self, "Gr-gsm Decode")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020050
51 ##################################################
52 # Parameters
53 ##################################################
54 self.timeslot = timeslot
55 self.subslot = subslot
56 self.chan_mode = chan_mode
57 self.burst_file = burst_file
58 self.cfile = cfile
59 self.fc = fc
60 self.samp_rate = samp_rate
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020061 self.a5 = a5
62 self.kc = a5_kc
63 if len(a5_kc) < 8:
64 self.kc = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
65 self.speech_file = speech_file
66 self.speech_codec = speech_codec
Roman Khassraf262b53d2015-09-13 12:14:26 +020067 self.verbose = verbose
Piotr Krysik0679ceb2016-04-29 11:16:22 +020068 self.print_bursts = print_bursts
Roman Khassrafc0addfb2016-09-27 20:11:42 +020069 self.enable_voice_boundary_detection = enable_voice_boundary_detection
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020070
71 ##################################################
72 # Blocks
73 ##################################################
74
75 if self.burst_file:
76 self.burst_file_source = grgsm.burst_file_source(burst_file)
77 elif self.cfile:
78 self.file_source = blocks.file_source(gr.sizeof_gr_complex*1, self.cfile, False)
79 self.receiver = grgsm.receiver(4, ([0]), ([]))
Piotr Krysik8040e012016-03-08 21:42:11 +010080 if self.fc is not None:
Piotr Krysik231aca72016-07-17 10:44:43 +020081 self.input_adapter = grgsm.gsm_input(ppm=ppm, osr=4, fc=self.fc, samp_rate_in=samp_rate)
Piotr Krysik388bc0d2016-07-15 14:17:05 +020082 self.offset_control = grgsm.clock_offset_control(self.fc, self.samp_rate)
Piotr Krysik8040e012016-03-08 21:42:11 +010083 else:
Piotr Krysik231aca72016-07-17 10:44:43 +020084 self.input_adapter = grgsm.gsm_input(ppm=ppm, osr=4, samp_rate_in=samp_rate)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020085
86 self.dummy_burst_filter = grgsm.dummy_burst_filter()
87 self.timeslot_filter = grgsm.burst_timeslot_filter(self.timeslot)
88
89 self.subslot_filter = None
90 if self.chan_mode == 'BCCH_SDCCH4' and self.subslot is not None:
91 self.subslot_filter = grgsm.burst_sdcch_subslot_filter(grgsm.SS_FILTER_SDCCH4, self.subslot)
92 elif self.chan_mode == 'SDCCH8' and self.subslot is not None:
93 self.subslot_filter = grgsm.burst_sdcch_subslot_filter(grgsm.SS_FILTER_SDCCH8, self.subslot)
94
95 if self.chan_mode == 'BCCH':
Piotr Krysik773a1942016-05-20 12:45:54 +020096 self.bcch_demapper = grgsm.gsm_bcch_ccch_demapper(self.timeslot)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020097 elif self.chan_mode == 'BCCH_SDCCH4':
Piotr Krysik773a1942016-05-20 12:45:54 +020098 self.bcch_sdcch4_demapper = grgsm.gsm_bcch_ccch_sdcch4_demapper(self.timeslot)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +020099 elif self.chan_mode == 'SDCCH8':
Piotr Krysik773a1942016-05-20 12:45:54 +0200100 self.sdcch8_demapper = grgsm.gsm_sdcch8_demapper(self.timeslot)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200101 elif self.chan_mode == 'TCHF':
102 self.tch_f_demapper = grgsm.tch_f_chans_demapper(self.timeslot)
Roman Khassrafc0addfb2016-09-27 20:11:42 +0200103 self.tch_f_decoder = grgsm.tch_f_decoder(speech_codec, enable_voice_boundary_detection)
Piotr Krysik27791722016-07-20 08:12:23 +0200104 self.tch_f_pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len")
105 self.tch_f_file_sink = blocks.file_sink(gr.sizeof_char*1, speech_file, False)
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300106 elif self.chan_mode == 'TCHH':
107 self.tch_h_demapper = grgsm.tch_h_chans_demapper(self.timeslot, tch_h_channel)
108 self.tch_h_decoder = grgsm.tch_h_decoder(tch_h_channel, multi_rate, enable_voice_boundary_detection)
109 self.tch_f_pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len")
110 self.tch_f_file_sink = blocks.file_sink(gr.sizeof_char*1, speech_file, False)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200111
112 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
113 self.decryption = grgsm.decryption(self.kc, self.a5)
114 self.cch_decoder_decrypted = grgsm.control_channels_decoder()
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300115 if self.chan_mode in ('TCHF', 'TCHH'):
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200116 self.decryption_tch_sacch = grgsm.decryption(self.kc, self.a5)
117
118 self.cch_decoder = grgsm.control_channels_decoder()
119
Piotr Krysikbeb36f42016-06-09 13:58:52 +0200120 self.socket_pdu_server = blocks.socket_pdu("UDP_SERVER", "127.0.0.1", "4729", 10000) #added in order to avoid generating ICMP messages
Piotr Krysik72a16172016-02-22 14:30:02 +0100121 self.socket_pdu = blocks.socket_pdu("UDP_CLIENT", "127.0.0.1", "4729", 10000)
Piotr Krysikbeb36f42016-06-09 13:58:52 +0200122
Roman Khassraf262b53d2015-09-13 12:14:26 +0200123 if self.verbose:
124 self.message_printer = grgsm.message_printer(pmt.intern(""), True, True, False)
125
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200126 if self.print_bursts:
Piotr Krysik7685bc02016-04-29 12:09:01 +0200127 self.bursts_printer = grgsm.bursts_printer(pmt.intern(""), True, True, True, True)
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200128
129
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200130
131 ##################################################
132 # Asynch Message Connections
133 ##################################################
134
135 if self.burst_file:
136 self.msg_connect(self.burst_file_source, "out", self.dummy_burst_filter, "in")
137 elif self.cfile:
138 self.connect((self.file_source, 0), (self.input_adapter, 0))
139 self.connect((self.input_adapter, 0), (self.receiver, 0))
Piotr Krysik8040e012016-03-08 21:42:11 +0100140 if self.fc is not None:
Piotr Krysik6577ec22016-07-15 13:21:09 +0200141 self.msg_connect(self.offset_control, "ctrl", self.input_adapter, "ctrl_in")
Piotr Krysik8040e012016-03-08 21:42:11 +0100142 self.msg_connect(self.receiver, "measurements", self.offset_control, "measurements")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200143 self.msg_connect(self.receiver, "C0", self.dummy_burst_filter, "in")
144
145 self.msg_connect(self.dummy_burst_filter, "out", self.timeslot_filter, "in")
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200146 if self.print_bursts:
147 self.msg_connect(self.timeslot_filter, "out", self.bursts_printer, 'bursts')
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200148
149 if (self.chan_mode == 'BCCH_SDCCH4' or self.chan_mode == 'SDCCH8') and self.subslot_filter is not None:
150 self.msg_connect(self.timeslot_filter, "out", self.subslot_filter, "in")
151
152 if self.chan_mode == 'BCCH':
153 if self.subslot_filter is not None:
154 self.msg_connect(self.subslot_filter, "out", self.bcch_demapper, "bursts")
155 else:
156 self.msg_connect(self.timeslot_filter, "out", self.bcch_demapper, "bursts")
157
158 self.msg_connect(self.bcch_demapper, "bursts", self.cch_decoder, "bursts")
159 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200160 if self.verbose:
161 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200162
163 elif self.chan_mode == 'BCCH_SDCCH4':
164 if self.subslot_filter is not None:
165 self.msg_connect(self.subslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
166 else:
167 self.msg_connect(self.timeslot_filter, "out", self.bcch_sdcch4_demapper, "bursts")
168
169 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
170 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.decryption, "bursts")
171 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
172 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200173 if self.verbose:
174 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200175
176 self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.cch_decoder, "bursts")
177 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200178 if self.verbose:
179 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200180
181 elif self.chan_mode == 'SDCCH8':
182 if self.subslot_filter is not None:
183 self.msg_connect(self.subslot_filter, "out", self.sdcch8_demapper, "bursts")
184 else:
185 self.msg_connect(self.timeslot_filter, "out", self.sdcch8_demapper, "bursts")
186
187 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
188 self.msg_connect(self.sdcch8_demapper, "bursts", self.decryption, "bursts")
189 self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts")
190 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200191 if self.verbose:
192 self.msg_connect(self.cch_decoder_decrypted, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200193
194 self.msg_connect(self.sdcch8_demapper, "bursts", self.cch_decoder, "bursts")
195 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200196 if self.verbose:
197 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200198
199 elif self.chan_mode == 'TCHF':
200 self.msg_connect(self.timeslot_filter, "out", self.tch_f_demapper, "bursts")
201 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
202 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.decryption_tch_sacch, "bursts")
203 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.decryption, "bursts")
204
205 self.msg_connect(self.decryption_tch_sacch, "bursts", self.cch_decoder, "bursts")
206 self.msg_connect(self.decryption, "bursts", self.tch_f_decoder, "bursts")
207 else:
208 self.msg_connect(self.tch_f_demapper, "acch_bursts", self.cch_decoder, "bursts")
209 self.msg_connect(self.tch_f_demapper, "tch_bursts", self.tch_f_decoder, "bursts")
210
211 self.msg_connect(self.tch_f_decoder, "msgs", self.socket_pdu, "pdus")
212 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
Piotr Krysik27791722016-07-20 08:12:23 +0200213 self.msg_connect(self.tch_f_decoder, "voice", self.tch_f_pdu_to_tagged_stream, "pdus")
214 self.connect((self.tch_f_pdu_to_tagged_stream, 0), (self.tch_f_file_sink, 0))
215
216
Roman Khassraf262b53d2015-09-13 12:14:26 +0200217 if self.verbose:
218 self.msg_connect(self.tch_f_decoder, "msgs", self.message_printer, "msgs")
219 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300220
221 elif self.chan_mode == 'TCHH':
222 self.msg_connect(self.timeslot_filter, "out", self.tch_h_demapper, "bursts")
223 if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]:
224 self.msg_connect(self.tch_h_demapper, "acch_bursts", self.decryption_tch_sacch, "bursts")
225 self.msg_connect(self.tch_h_demapper, "tch_bursts", self.decryption, "bursts")
226
227 self.msg_connect(self.decryption_tch_sacch, "bursts", self.cch_decoder, "bursts")
228 self.msg_connect(self.decryption, "bursts", self.tch_h_decoder, "bursts")
229 else:
230 self.msg_connect(self.tch_h_demapper, "acch_bursts", self.cch_decoder, "bursts")
231 self.msg_connect(self.tch_h_demapper, "tch_bursts", self.tch_h_decoder, "bursts")
232
233 self.msg_connect(self.tch_h_decoder, "msgs", self.socket_pdu, "pdus")
234 self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus")
235 self.msg_connect(self.tch_h_decoder, "voice", self.tch_f_pdu_to_tagged_stream, "pdus")
236 self.connect((self.tch_f_pdu_to_tagged_stream, 0), (self.tch_f_file_sink, 0))
237
238 if self.verbose:
239 self.msg_connect(self.tch_h_decoder, "msgs", self.message_printer, "msgs")
240 self.msg_connect(self.cch_decoder, "msgs", self.message_printer, "msgs")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200241
242if __name__ == '__main__':
243
244 # List of channel configurations
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300245 channel_modes = ['BCCH', 'BCCH_SDCCH4', 'SDCCH8', 'TCHF', 'TCHH']
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200246
247 # mapping options to grgsm's enums
248 tch_codecs = collections.OrderedDict([
249 ('FR', grgsm.TCH_FS),
250 ('EFR', grgsm.TCH_EFR),
251 ('AMR12.2', grgsm.TCH_AFS12_2),
252 ('AMR10.2', grgsm.TCH_AFS10_2),
253 ('AMR7.95', grgsm.TCH_AFS7_95),
254 ('AMR7.4', grgsm.TCH_AFS7_4),
255 ('AMR6.7', grgsm.TCH_AFS6_7),
256 ('AMR5.9', grgsm.TCH_AFS5_9),
257 ('AMR5.15', grgsm.TCH_AFS5_15),
258 ('AMR4.75', grgsm.TCH_AFS4_75)
259 ])
260
261 kc = []
262
263 parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
264
265 def kc_callback(option, opt_str, value, parser):
266
267 """ Callback function that parses Kc """
268
269 # format 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF
270 if ',' in value:
271 value_str = value.split(',')
272
273 for s in value_str:
274 val = int(s, 16)
275 if val < 0 or val > 255:
276 parser.error("Invalid Kc % s\n" % s)
277 kc.append(val)
278 if len(kc) != 8:
279 parser.error("Invalid Kc length")
280 # format: 1234567890ABCDEF
281 elif len(value) == 16:
282 for i in range(8):
283 s = value[2*i: 2*i + 2]
284 val = int(s, 16)
285 if val < 0 or val > 255:
286 parser.error("Invalid Kc % s\n" % s)
287 kc.append(val)
288 else:
289 parser.error("Invalid Kc format")
290
291 # define options
292 parser.add_option("-m", "--mode", dest="chan_mode", default='BCCH',
293 type='choice', choices=channel_modes,
294 help="Channel mode. Valid options are 'BCCH' (Non-combined C0), "
295 "'BCCH_SDCCH4'(Combined C0), 'SDCCH8' (Stand-alone control channel) "
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300296 "'TCHF' (Traffic Channel, Full rate), 'TCHH' (Traffic Channel, Half rate) ")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200297 parser.add_option("-t", "--timeslot", dest="timeslot", type="intx", default=0,
298 help="Timeslot to decode [default=%default]")
299 parser.add_option("-u", "--subslot", dest="subslot", type="intx",
300 help="Subslot to decode. Use in combination with channel type BCCH_SDCCH4 and SDCCH8")
301 parser.add_option("-b", "--burst-file", dest="burst_file", help="Input file (bursts)")
302 parser.add_option("-c", "--cfile", dest="cfile", help="Input file (cfile)")
Roman Khassraf262b53d2015-09-13 12:14:26 +0200303 parser.add_option("-v", "--verbose", action="store_true",
304 help="If set, the decoded messages (with frame number and count) are printed to stdout")
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200305 parser.add_option("-p", "--print-bursts", action="store_true",
306 help="If set, the raw bursts (with frame number and count) are printed to stdout")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200307
308 # group cfile options together
309 cfile_options = OptionGroup(
310 parser, 'Cfile Options', 'Options for decoding cfile input.',
311 )
312 cfile_options.add_option("-f", "--fc", dest="fc", type="eng_float",
Piotr Krysik231aca72016-07-17 10:44:43 +0200313 help="Frequency of cfile capture")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200314 cfile_options.add_option("-a", "--arfcn", dest="arfcn", type="intx",
Piotr Krysikfcb45b02017-09-11 10:07:25 +0200315 help="Set ARFCN instead of frequency (for PCS1900 add 0x8000 (2**15) to the ARFCN number).")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200316 cfile_options.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float",
Piotr Krysik13d9e7a2016-04-24 15:23:15 +0200317 default=eng_notation.num_to_str(1e6),
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200318 help="Sample rate of cfile capture [default=%default]")
Piotr Krysik231aca72016-07-17 10:44:43 +0200319 cfile_options.add_option("--ppm", dest="ppm", type="float", default=0,
320 help="Set frequency offset correction [default=%default]")
321
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200322 parser.add_option_group(cfile_options)
323
324 # group decryption options
325 decryption_options = OptionGroup(
326 parser, 'Decryption Options', 'Options for setting the A5 decryption parameters.',
327 )
328 decryption_options.add_option("-e", "--a5", dest="a5", type="intx", default=1,
329 help="A5 version [default=%default]. A5 versions 1 - 3 supported")
330 decryption_options.add_option("-k", "--kc", action="callback", callback=kc_callback, type="string",
331 help="A5 session key Kc. Valid formats are "
332 "'0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF' and '1234567890ABCDEF'")
333 parser.add_option_group(decryption_options)
334
335 # group TCH options
336 tch_options = OptionGroup(
337 parser, 'TCH Options', 'Options for setting Traffic channel decoding parameters.',
338 )
339 tch_options.add_option("-d", "--speech-codec", dest="speech_codec", default='FR',
Vasil Velichkov46c90be2019-09-02 04:06:41 +0300340 type='choice', choices=list(tch_codecs.keys()),
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200341 help="TCH-F speech codec [default=%default]. "
342 "Valid options are " + ", ".join(tch_codecs.keys()))
343 tch_options.add_option("-o", "--output-tch", dest="speech_output_file", default="/tmp/speech.au.gsm",
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300344 help="tch/f speech output file [default=%default].")
345 tch_options.add_option("--sub-channel", dest="tch_h_channel", default="0", type='intx',
346 help="TCH/H sub-channel. [default=0]")
347 tch_options.add_option("--multi-rate", dest="multi_rate", default="", type='string',
Vasil Velichkovc5bb4362019-03-13 20:53:24 +0200348 help="The MultiRate configuration element from the Assignment Command message. "
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300349 "Example: 28111a40. See 3GPP TS 44.018 - 10.5.2.21aa MultiRate configuration")
Piotr Krysik2770fc42017-07-23 22:05:10 +0200350 tch_options.add_option("--voice-boundary", dest="enable_voice_boundary_detection", action="store_true", default=False,
Roman Khassrafc0addfb2016-09-27 20:11:42 +0200351 help="Enable voice boundary detection for traffic channels. This can help reduce noice in the output.")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200352 parser.add_option_group(tch_options)
353
354 # parse
355 (options, args) = parser.parse_args()
356
357 # some verifications
358 if (options.cfile is None and options.burst_file is None) or \
359 (options.cfile is not None and options.burst_file is not None):
360 parser.error("Please provide a cfile or a burst file (but not both) as input\n")
361
362 if options.timeslot < 0 or options.timeslot > 7:
363 parser.error("Invalid timeslot. Must be a in range 0-7\n")
364
365 if options.subslot is not None and (options.subslot < 0 or options.subslot > 7):
366 parser.error("Invalid subslot. Must be a in range 0-7\n")
367
368 if options.a5 < 0 or options.a5 > 3:
369 parser.error("Invalid A5 version\n")
370
Piotr Krysik231aca72016-07-17 10:44:43 +0200371 if options.cfile and (options.fc is None and options.arfcn is None):
Vasil Velichkovdfcf7fd2019-09-27 01:53:43 +0300372 sys.stderr.write("You haven't provided a frequency or an ARFCN - working without automatic frequency offset correction.\n")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200373
374 # handle frequency / arfcn input
Piotr Krysik231aca72016-07-17 10:44:43 +0200375 arfcn = None
376 fc = None
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200377 if options.arfcn:
Piotr Krysikfcb45b02017-09-11 10:07:25 +0200378 if not grgsm.arfcn.is_valid_arfcn(options.arfcn):
379 parser.error("ARFCN is not valid\n")
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200380 else:
381 arfcn = options.arfcn
Piotr Krysikfcb45b02017-09-11 10:07:25 +0200382 fc = grgsm.arfcn.arfcn2downlink(arfcn)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200383 elif options.fc:
384 fc = options.fc
Piotr Krysikfcb45b02017-09-11 10:07:25 +0200385 arfcn = grgsm.arfcn.downlink2arfcn(options.fc)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200386
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200387 # instanciate decoder
Piotr Krysik3e192c42016-02-13 07:31:51 +0100388 tb = grgsm_decoder(timeslot=options.timeslot, subslot=options.subslot, chan_mode=options.chan_mode,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200389 burst_file=options.burst_file,
Piotr Krysik8040e012016-03-08 21:42:11 +0100390 cfile=options.cfile, fc=fc, samp_rate=options.samp_rate,
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200391 a5=options.a5, a5_kc=kc,
Roman Khassraf262b53d2015-09-13 12:14:26 +0200392 speech_file=options.speech_output_file, speech_codec=tch_codecs.get(options.speech_codec),
Roman Khassrafc0addfb2016-09-27 20:11:42 +0200393 enable_voice_boundary_detection=options.enable_voice_boundary_detection,
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300394 tch_h_channel=options.tch_h_channel,
395 multi_rate=options.multi_rate,
Piotr Krysik0679ceb2016-04-29 11:16:22 +0200396 verbose=options.verbose,
Piotr Krysik196982b2016-07-17 10:43:00 +0200397 print_bursts=options.print_bursts, ppm=options.ppm)
Roman Khassraf2b1ebf12015-08-29 17:43:43 +0200398
399 # run
400 tb.start()
401 tb.wait()