Roman Khassraf | 2b1ebf1 | 2015-08-29 17:43:43 +0200 | [diff] [blame^] | 1 | #!/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 | |
| 24 | from gnuradio import blocks |
| 25 | from gnuradio import eng_notation |
| 26 | from gnuradio import gr |
| 27 | from gnuradio.eng_option import eng_option |
| 28 | from optparse import OptionParser, OptionGroup |
| 29 | import collections |
| 30 | import grgsm |
| 31 | import socket |
| 32 | |
| 33 | |
| 34 | class airprobe_decoder(gr.top_block): |
| 35 | |
| 36 | def __init__(self, timeslot=0, subslot=None, chan_mode='BCCH', |
| 37 | burst_file=None, |
| 38 | cfile=None, fc=939.4e6, samp_rate=2e6, arfcn=None, |
| 39 | a5=1, a5_kc=None, |
| 40 | speech_file=None, speech_codec=None): |
| 41 | |
| 42 | gr.top_block.__init__(self, "Airprobe Decode") |
| 43 | |
| 44 | ################################################## |
| 45 | # Parameters |
| 46 | ################################################## |
| 47 | self.timeslot = timeslot |
| 48 | self.subslot = subslot |
| 49 | self.chan_mode = chan_mode |
| 50 | self.burst_file = burst_file |
| 51 | self.cfile = cfile |
| 52 | self.fc = fc |
| 53 | self.samp_rate = samp_rate |
| 54 | self.arfcn = arfcn |
| 55 | self.a5 = a5 |
| 56 | self.kc = a5_kc |
| 57 | if len(a5_kc) < 8: |
| 58 | self.kc = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] |
| 59 | self.speech_file = speech_file |
| 60 | self.speech_codec = speech_codec |
| 61 | |
| 62 | ################################################## |
| 63 | # Blocks |
| 64 | ################################################## |
| 65 | |
| 66 | if self.burst_file: |
| 67 | self.burst_file_source = grgsm.burst_file_source(burst_file) |
| 68 | elif self.cfile: |
| 69 | self.file_source = blocks.file_source(gr.sizeof_gr_complex*1, self.cfile, False) |
| 70 | self.receiver = grgsm.receiver(4, ([0]), ([])) |
| 71 | self.input_adapter = grgsm.gsm_input( |
| 72 | ppm=0, |
| 73 | osr=4, |
| 74 | fc=fc, |
| 75 | samp_rate_in=samp_rate, |
| 76 | ) |
| 77 | self.offset_control = grgsm.clock_offset_control(fc) |
| 78 | |
| 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 | |
| 112 | self.socket_pdu = blocks.socket_pdu("UDP_CLIENT", "127.0.0.1", "4729", 10000, False) |
| 113 | |
| 114 | ################################################## |
| 115 | # Asynch Message Connections |
| 116 | ################################################## |
| 117 | |
| 118 | if self.burst_file: |
| 119 | self.msg_connect(self.burst_file_source, "out", self.dummy_burst_filter, "in") |
| 120 | elif self.cfile: |
| 121 | self.connect((self.file_source, 0), (self.input_adapter, 0)) |
| 122 | self.connect((self.input_adapter, 0), (self.receiver, 0)) |
| 123 | self.msg_connect(self.offset_control, "ppm", self.input_adapter, "ppm_in") |
| 124 | self.msg_connect(self.receiver, "measurements", self.offset_control, "measurements") |
| 125 | self.msg_connect(self.receiver, "C0", self.dummy_burst_filter, "in") |
| 126 | |
| 127 | self.msg_connect(self.dummy_burst_filter, "out", self.timeslot_filter, "in") |
| 128 | |
| 129 | if (self.chan_mode == 'BCCH_SDCCH4' or self.chan_mode == 'SDCCH8') and self.subslot_filter is not None: |
| 130 | self.msg_connect(self.timeslot_filter, "out", self.subslot_filter, "in") |
| 131 | |
| 132 | if self.chan_mode == 'BCCH': |
| 133 | if self.subslot_filter is not None: |
| 134 | self.msg_connect(self.subslot_filter, "out", self.bcch_demapper, "bursts") |
| 135 | else: |
| 136 | self.msg_connect(self.timeslot_filter, "out", self.bcch_demapper, "bursts") |
| 137 | |
| 138 | self.msg_connect(self.bcch_demapper, "bursts", self.cch_decoder, "bursts") |
| 139 | self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus") |
| 140 | |
| 141 | elif self.chan_mode == 'BCCH_SDCCH4': |
| 142 | if self.subslot_filter is not None: |
| 143 | self.msg_connect(self.subslot_filter, "out", self.bcch_sdcch4_demapper, "bursts") |
| 144 | else: |
| 145 | self.msg_connect(self.timeslot_filter, "out", self.bcch_sdcch4_demapper, "bursts") |
| 146 | |
| 147 | if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]: |
| 148 | self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.decryption, "bursts") |
| 149 | self.msg_connect(self.decryption, "bursts", self.cch_decoder_decrypted, "bursts") |
| 150 | self.msg_connect(self.cch_decoder_decrypted, "msgs", self.socket_pdu, "pdus") |
| 151 | |
| 152 | self.msg_connect(self.bcch_sdcch4_demapper, "bursts", self.cch_decoder, "bursts") |
| 153 | self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus") |
| 154 | |
| 155 | elif self.chan_mode == 'SDCCH8': |
| 156 | if self.subslot_filter is not None: |
| 157 | self.msg_connect(self.subslot_filter, "out", self.sdcch8_demapper, "bursts") |
| 158 | else: |
| 159 | self.msg_connect(self.timeslot_filter, "out", self.sdcch8_demapper, "bursts") |
| 160 | |
| 161 | if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]: |
| 162 | self.msg_connect(self.sdcch8_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") |
| 165 | |
| 166 | self.msg_connect(self.sdcch8_demapper, "bursts", self.cch_decoder, "bursts") |
| 167 | self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus") |
| 168 | |
| 169 | elif self.chan_mode == 'TCHF': |
| 170 | self.msg_connect(self.timeslot_filter, "out", self.tch_f_demapper, "bursts") |
| 171 | if self.kc != [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]: |
| 172 | self.msg_connect(self.tch_f_demapper, "acch_bursts", self.decryption_tch_sacch, "bursts") |
| 173 | self.msg_connect(self.tch_f_demapper, "tch_bursts", self.decryption, "bursts") |
| 174 | |
| 175 | self.msg_connect(self.decryption_tch_sacch, "bursts", self.cch_decoder, "bursts") |
| 176 | self.msg_connect(self.decryption, "bursts", self.tch_f_decoder, "bursts") |
| 177 | else: |
| 178 | self.msg_connect(self.tch_f_demapper, "acch_bursts", self.cch_decoder, "bursts") |
| 179 | self.msg_connect(self.tch_f_demapper, "tch_bursts", self.tch_f_decoder, "bursts") |
| 180 | |
| 181 | self.msg_connect(self.tch_f_decoder, "msgs", self.socket_pdu, "pdus") |
| 182 | self.msg_connect(self.cch_decoder, "msgs", self.socket_pdu, "pdus") |
| 183 | |
| 184 | |
| 185 | if __name__ == '__main__': |
| 186 | |
| 187 | # List of channel configurations |
| 188 | channel_modes = ['BCCH', 'BCCH_SDCCH4', 'SDCCH8', 'TCHF'] |
| 189 | |
| 190 | # mapping options to grgsm's enums |
| 191 | tch_codecs = collections.OrderedDict([ |
| 192 | ('FR', grgsm.TCH_FS), |
| 193 | ('EFR', grgsm.TCH_EFR), |
| 194 | ('AMR12.2', grgsm.TCH_AFS12_2), |
| 195 | ('AMR10.2', grgsm.TCH_AFS10_2), |
| 196 | ('AMR7.95', grgsm.TCH_AFS7_95), |
| 197 | ('AMR7.4', grgsm.TCH_AFS7_4), |
| 198 | ('AMR6.7', grgsm.TCH_AFS6_7), |
| 199 | ('AMR5.9', grgsm.TCH_AFS5_9), |
| 200 | ('AMR5.15', grgsm.TCH_AFS5_15), |
| 201 | ('AMR4.75', grgsm.TCH_AFS4_75) |
| 202 | ]) |
| 203 | |
| 204 | kc = [] |
| 205 | |
| 206 | parser = OptionParser(option_class=eng_option, usage="%prog: [options]") |
| 207 | |
| 208 | def kc_callback(option, opt_str, value, parser): |
| 209 | |
| 210 | """ Callback function that parses Kc """ |
| 211 | |
| 212 | # format 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF |
| 213 | if ',' in value: |
| 214 | value_str = value.split(',') |
| 215 | |
| 216 | for s in value_str: |
| 217 | val = int(s, 16) |
| 218 | if val < 0 or val > 255: |
| 219 | parser.error("Invalid Kc % s\n" % s) |
| 220 | kc.append(val) |
| 221 | if len(kc) != 8: |
| 222 | parser.error("Invalid Kc length") |
| 223 | # format: 1234567890ABCDEF |
| 224 | elif len(value) == 16: |
| 225 | for i in range(8): |
| 226 | s = value[2*i: 2*i + 2] |
| 227 | val = int(s, 16) |
| 228 | if val < 0 or val > 255: |
| 229 | parser.error("Invalid Kc % s\n" % s) |
| 230 | kc.append(val) |
| 231 | else: |
| 232 | parser.error("Invalid Kc format") |
| 233 | |
| 234 | # define options |
| 235 | parser.add_option("-m", "--mode", dest="chan_mode", default='BCCH', |
| 236 | type='choice', choices=channel_modes, |
| 237 | help="Channel mode. Valid options are 'BCCH' (Non-combined C0), " |
| 238 | "'BCCH_SDCCH4'(Combined C0), 'SDCCH8' (Stand-alone control channel) " |
| 239 | "and 'TCHF' (Traffic Channel, Full rate) ") |
| 240 | parser.add_option("-t", "--timeslot", dest="timeslot", type="intx", default=0, |
| 241 | help="Timeslot to decode [default=%default]") |
| 242 | parser.add_option("-u", "--subslot", dest="subslot", type="intx", |
| 243 | help="Subslot to decode. Use in combination with channel type BCCH_SDCCH4 and SDCCH8") |
| 244 | parser.add_option("-b", "--burst-file", dest="burst_file", help="Input file (bursts)") |
| 245 | parser.add_option("-c", "--cfile", dest="cfile", help="Input file (cfile)") |
| 246 | |
| 247 | # group cfile options together |
| 248 | cfile_options = OptionGroup( |
| 249 | parser, 'Cfile Options', 'Options for decoding cfile input.', |
| 250 | ) |
| 251 | cfile_options.add_option("-f", "--fc", dest="fc", type="eng_float", |
| 252 | help="Frequency of cfile capture [default=eng_notation.num_to_str(939.4e6)]") |
| 253 | cfile_options.add_option("-a", "--arfcn", dest="arfcn", type="intx", |
| 254 | help="Set ARFCN instead of frequency. " |
| 255 | "In some cases you may have to provide the GSM band also") |
| 256 | cfile_options.add_option("--band", dest="band", |
| 257 | help="Specify the GSM band for the frequency.\nAvailable bands are: " |
| 258 | + ", ".join(grgsm.arfcn.get_bands()) + "." |
| 259 | + "If no band is specified, it will be determined automatically, defaulting to 0.") |
| 260 | cfile_options.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float", |
| 261 | default=eng_notation.num_to_str(2e6), |
| 262 | help="Sample rate of cfile capture [default=%default]") |
| 263 | parser.add_option_group(cfile_options) |
| 264 | |
| 265 | # group decryption options |
| 266 | decryption_options = OptionGroup( |
| 267 | parser, 'Decryption Options', 'Options for setting the A5 decryption parameters.', |
| 268 | ) |
| 269 | decryption_options.add_option("-e", "--a5", dest="a5", type="intx", default=1, |
| 270 | help="A5 version [default=%default]. A5 versions 1 - 3 supported") |
| 271 | decryption_options.add_option("-k", "--kc", action="callback", callback=kc_callback, type="string", |
| 272 | help="A5 session key Kc. Valid formats are " |
| 273 | "'0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF' and '1234567890ABCDEF'") |
| 274 | parser.add_option_group(decryption_options) |
| 275 | |
| 276 | # group TCH options |
| 277 | tch_options = OptionGroup( |
| 278 | parser, 'TCH Options', 'Options for setting Traffic channel decoding parameters.', |
| 279 | ) |
| 280 | tch_options.add_option("-d", "--speech-codec", dest="speech_codec", default='FR', |
| 281 | type='choice', choices=tch_codecs.keys(), |
| 282 | help="TCH-F speech codec [default=%default]. " |
| 283 | "Valid options are " + ", ".join(tch_codecs.keys())) |
| 284 | tch_options.add_option("-o", "--output-tch", dest="speech_output_file", default="/tmp/speech.au.gsm", |
| 285 | help="TCH/F speech output file [default=%default].") |
| 286 | parser.add_option_group(tch_options) |
| 287 | |
| 288 | # parse |
| 289 | (options, args) = parser.parse_args() |
| 290 | |
| 291 | # some verifications |
| 292 | if (options.cfile is None and options.burst_file is None) or \ |
| 293 | (options.cfile is not None and options.burst_file is not None): |
| 294 | parser.error("Please provide a cfile or a burst file (but not both) as input\n") |
| 295 | |
| 296 | if options.timeslot < 0 or options.timeslot > 7: |
| 297 | parser.error("Invalid timeslot. Must be a in range 0-7\n") |
| 298 | |
| 299 | if options.subslot is not None and (options.subslot < 0 or options.subslot > 7): |
| 300 | parser.error("Invalid subslot. Must be a in range 0-7\n") |
| 301 | |
| 302 | if options.a5 < 0 or options.a5 > 3: |
| 303 | parser.error("Invalid A5 version\n") |
| 304 | |
| 305 | if (options.fc is None and options.arfcn is None) or (options.fc is not None and options.arfcn is not None): |
| 306 | parser.error("You have to provide either a frequency or an ARFCN (but not both).\n") |
| 307 | |
| 308 | # handle frequency / arfcn input |
| 309 | arfcn = 0 |
| 310 | fc = 939.4e6 |
| 311 | if options.arfcn: |
| 312 | if options.band: |
| 313 | if options.band not in grgsm.arfcn.get_bands(): |
| 314 | parser.error("Invalid GSM band\n") |
| 315 | elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band): |
| 316 | parser.error("ARFCN is not valid in the specified band\n") |
| 317 | else: |
| 318 | arfcn = options.arfcn |
| 319 | fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band) |
| 320 | else: |
| 321 | arfcn = options.arfcn |
| 322 | for band in grgsm.arfcn.get_bands(): |
| 323 | if grgsm.arfcn.is_valid_arfcn(arfcn, band): |
| 324 | fc = grgsm.arfcn.arfcn2downlink(arfcn, band) |
| 325 | break |
| 326 | elif options.fc: |
| 327 | fc = options.fc |
| 328 | if options.band: |
| 329 | if options.band not in grgsm.arfcn.get_bands(): |
| 330 | parser.error("Invalid GSM band\n") |
| 331 | elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band): |
| 332 | parser.error("Frequency is not valid in the specified band\n") |
| 333 | else: |
| 334 | arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band) |
| 335 | else: |
| 336 | for band in grgsm.arfcn.get_bands(): |
| 337 | if grgsm.arfcn.is_valid_downlink(options.fc, band): |
| 338 | arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band) |
| 339 | break |
| 340 | |
| 341 | # open udp port 4729 to avoid icmp messages |
| 342 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 343 | sock.bind(("localhost", 4729)) |
| 344 | |
| 345 | # instanciate decoder |
| 346 | tb = airprobe_decoder(timeslot=options.timeslot, subslot=options.subslot, chan_mode=options.chan_mode, |
| 347 | burst_file=options.burst_file, |
| 348 | cfile=options.cfile, arfcn=arfcn, fc=fc, samp_rate=options.samp_rate, |
| 349 | a5=options.a5, a5_kc=kc, |
| 350 | speech_file=options.speech_output_file, speech_codec=tch_codecs.get(options.speech_codec)) |
| 351 | |
| 352 | # run |
| 353 | tb.start() |
| 354 | tb.wait() |
| 355 | |
| 356 | # we are done, close socket |
| 357 | sock.close() |