2020-07-16 06:45:54 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-07-28 07:56:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-03-12 18:09:56 +00:00
|
|
|
# TRX Toolkit
|
2017-11-21 11:35:24 +00:00
|
|
|
# Auxiliary tool to generate and send random bursts via TRX DATA
|
|
|
|
# interface, which may be useful for fuzzing and testing
|
2017-07-28 07:56:09 +00:00
|
|
|
#
|
2019-07-23 22:04:36 +00:00
|
|
|
# (C) 2017-2019 by Vadim Yanitskiy <axilirator@gmail.com>
|
2017-07-28 07:56:09 +00:00
|
|
|
#
|
|
|
|
# All Rights Reserved
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
2019-07-23 22:04:36 +00:00
|
|
|
APP_CR_HOLDERS = [("2017-2019", "Vadim Yanitskiy <axilirator@gmail.com>")]
|
2018-03-12 13:48:24 +00:00
|
|
|
|
trx_toolkit: use generic logging module instead of print()
There are multiple advantages of using Python's logging module:
- advanced message formatting (file name, line number, etc.),
- multiple logging targets (e.g. stderr, file, socket),
- logging levels (e.g. DEBUG, INFO, ERROR),
- the pythonic way ;)
so, let's replace multiple print() calls by logging calls,
add use the following logging message format by default:
[%(levelname)s] %(filename)s:%(lineno)d %(message)s
Examples:
[INFO] ctrl_if_bts.py:57 Starting transceiver...
[DEBUG] clck_gen.py:87 IND CLOCK 26826
[DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
[INFO] ctrl_if_bts.py:73 Stopping transceiver...
[INFO] fake_trx.py:127 Shutting down...
Please note that there is no way to filter messages by logging
level yet. This is to be introduced soon, together with argparse.
Change-Id: I7fcafabafe8323b58990997a47afdd48b6d1f357
2018-12-06 22:00:26 +00:00
|
|
|
import logging as log
|
2017-07-28 07:56:09 +00:00
|
|
|
import signal
|
2018-12-06 23:52:59 +00:00
|
|
|
import argparse
|
2017-07-28 07:56:09 +00:00
|
|
|
import sys
|
|
|
|
|
2018-12-07 02:34:00 +00:00
|
|
|
from app_common import ApplicationBase
|
2017-07-28 07:56:09 +00:00
|
|
|
from rand_burst_gen import RandBurstGen
|
2018-02-20 11:19:48 +00:00
|
|
|
from data_dump import DATADumpFile
|
2017-08-03 13:51:57 +00:00
|
|
|
from data_if import DATAInterface
|
2018-01-24 16:58:48 +00:00
|
|
|
from gsm_shared import *
|
2018-01-27 14:47:31 +00:00
|
|
|
from data_msg import *
|
2017-07-28 07:56:09 +00:00
|
|
|
|
2018-12-07 02:34:00 +00:00
|
|
|
class Application(ApplicationBase):
|
2017-07-28 07:56:09 +00:00
|
|
|
def __init__(self):
|
2018-12-07 03:04:14 +00:00
|
|
|
self.app_print_copyright(APP_CR_HOLDERS)
|
2018-12-06 23:52:59 +00:00
|
|
|
self.argv = self.parse_argv()
|
2017-07-28 07:56:09 +00:00
|
|
|
|
|
|
|
# Set up signal handlers
|
|
|
|
signal.signal(signal.SIGINT, self.sig_handler)
|
|
|
|
|
trx_toolkit: use generic logging module instead of print()
There are multiple advantages of using Python's logging module:
- advanced message formatting (file name, line number, etc.),
- multiple logging targets (e.g. stderr, file, socket),
- logging levels (e.g. DEBUG, INFO, ERROR),
- the pythonic way ;)
so, let's replace multiple print() calls by logging calls,
add use the following logging message format by default:
[%(levelname)s] %(filename)s:%(lineno)d %(message)s
Examples:
[INFO] ctrl_if_bts.py:57 Starting transceiver...
[DEBUG] clck_gen.py:87 IND CLOCK 26826
[DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
[INFO] ctrl_if_bts.py:73 Stopping transceiver...
[INFO] fake_trx.py:127 Shutting down...
Please note that there is no way to filter messages by logging
level yet. This is to be introduced soon, together with argparse.
Change-Id: I7fcafabafe8323b58990997a47afdd48b6d1f357
2018-12-06 22:00:26 +00:00
|
|
|
# Configure logging
|
2018-12-07 02:34:00 +00:00
|
|
|
self.app_init_logging(self.argv)
|
trx_toolkit: use generic logging module instead of print()
There are multiple advantages of using Python's logging module:
- advanced message formatting (file name, line number, etc.),
- multiple logging targets (e.g. stderr, file, socket),
- logging levels (e.g. DEBUG, INFO, ERROR),
- the pythonic way ;)
so, let's replace multiple print() calls by logging calls,
add use the following logging message format by default:
[%(levelname)s] %(filename)s:%(lineno)d %(message)s
Examples:
[INFO] ctrl_if_bts.py:57 Starting transceiver...
[DEBUG] clck_gen.py:87 IND CLOCK 26826
[DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
[INFO] ctrl_if_bts.py:73 Stopping transceiver...
[INFO] fake_trx.py:127 Shutting down...
Please note that there is no way to filter messages by logging
level yet. This is to be introduced soon, together with argparse.
Change-Id: I7fcafabafe8323b58990997a47afdd48b6d1f357
2018-12-06 22:00:26 +00:00
|
|
|
|
2018-02-20 11:19:48 +00:00
|
|
|
# Open requested capture file
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.output_file is not None:
|
|
|
|
self.ddf = DATADumpFile(self.argv.output_file)
|
2018-02-20 11:19:48 +00:00
|
|
|
|
2017-07-28 07:56:09 +00:00
|
|
|
def run(self):
|
|
|
|
# Init DATA interface with TRX or L1
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.conn_mode == "TRX":
|
|
|
|
self.data_if = DATAInterface(
|
|
|
|
self.argv.remote_addr, self.argv.base_port + 2,
|
|
|
|
self.argv.bind_addr, self.argv.base_port + 102)
|
|
|
|
elif self.argv.conn_mode == "L1":
|
|
|
|
self.data_if = DATAInterface(
|
|
|
|
self.argv.remote_addr, self.argv.base_port + 102,
|
|
|
|
self.argv.bind_addr, self.argv.base_port + 2)
|
2017-07-28 07:56:09 +00:00
|
|
|
|
|
|
|
# Init random burst generator
|
2018-01-27 14:11:46 +00:00
|
|
|
burst_gen = RandBurstGen()
|
2017-07-28 07:56:09 +00:00
|
|
|
|
2018-01-27 14:47:31 +00:00
|
|
|
# Init an empty DATA message
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.conn_mode == "TRX":
|
2021-04-28 00:05:15 +00:00
|
|
|
msg = TxMsg(ver = self.argv.hdr_ver)
|
2018-12-06 23:52:59 +00:00
|
|
|
elif self.argv.conn_mode == "L1":
|
2021-04-28 00:05:15 +00:00
|
|
|
msg = RxMsg(ver = self.argv.hdr_ver)
|
2018-01-27 14:47:31 +00:00
|
|
|
|
2017-08-03 13:34:39 +00:00
|
|
|
# Generate a random frame number or use provided one
|
2018-12-06 23:52:59 +00:00
|
|
|
fn_init = msg.rand_fn() if self.argv.tdma_fn is None \
|
|
|
|
else self.argv.tdma_fn
|
2017-08-03 13:34:39 +00:00
|
|
|
|
2017-07-28 07:56:09 +00:00
|
|
|
# Send as much bursts as required
|
2018-12-06 23:52:59 +00:00
|
|
|
for i in range(self.argv.burst_count):
|
2018-01-27 14:47:31 +00:00
|
|
|
# Randomize the message header
|
|
|
|
msg.rand_hdr()
|
|
|
|
|
|
|
|
# Increase and set frame number
|
|
|
|
msg.fn = (fn_init + i) % GSM_HYPERFRAME
|
|
|
|
|
|
|
|
# Set timeslot number
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.tdma_tn is not None:
|
|
|
|
msg.tn = self.argv.tdma_tn
|
2018-01-27 14:47:31 +00:00
|
|
|
|
|
|
|
# Set transmit power level
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.pwr is not None:
|
|
|
|
msg.pwr = self.argv.pwr
|
2018-01-27 14:47:31 +00:00
|
|
|
|
2018-01-27 15:09:53 +00:00
|
|
|
# Set time of arrival
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.toa is not None:
|
|
|
|
msg.toa256 = int(float(self.argv.toa) * 256.0 + 0.5)
|
|
|
|
elif self.argv.toa256 is not None:
|
|
|
|
msg.toa256 = self.argv.toa256
|
2018-01-27 15:09:53 +00:00
|
|
|
|
|
|
|
# Set RSSI
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.rssi is not None:
|
|
|
|
msg.rssi = self.argv.rssi
|
2018-01-27 14:47:31 +00:00
|
|
|
|
2019-07-23 22:04:36 +00:00
|
|
|
if msg.ver >= 0x01:
|
|
|
|
# TODO: Only GMSK and TSC set 0 for now
|
|
|
|
msg.mod_type = Modulation.ModGMSK
|
|
|
|
self.tsc_set = 0
|
|
|
|
|
|
|
|
if self.argv.tsc is not None:
|
|
|
|
msg.tsc = self.argv.tsc
|
|
|
|
|
|
|
|
if self.argv.ci is not None:
|
|
|
|
msg.ci = self.argv.ci
|
|
|
|
|
2017-07-28 07:56:09 +00:00
|
|
|
# Generate a random burst
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.burst_type == "NB":
|
2018-01-27 14:47:31 +00:00
|
|
|
burst = burst_gen.gen_nb()
|
2018-12-06 23:52:59 +00:00
|
|
|
elif self.argv.burst_type == "FB":
|
2018-01-27 14:47:31 +00:00
|
|
|
burst = burst_gen.gen_fb()
|
2018-12-06 23:52:59 +00:00
|
|
|
elif self.argv.burst_type == "SB":
|
2018-01-27 14:47:31 +00:00
|
|
|
burst = burst_gen.gen_sb()
|
2018-12-06 23:52:59 +00:00
|
|
|
elif self.argv.burst_type == "AB":
|
2018-01-27 14:47:31 +00:00
|
|
|
burst = burst_gen.gen_ab()
|
|
|
|
|
|
|
|
# Convert to soft-bits in case of TRX -> L1 message
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.conn_mode == "L1":
|
2018-01-27 14:47:31 +00:00
|
|
|
burst = msg.ubit2sbit(burst)
|
2017-07-28 07:56:09 +00:00
|
|
|
|
2018-01-27 14:47:31 +00:00
|
|
|
# Set burst
|
|
|
|
msg.burst = burst
|
|
|
|
|
trx_toolkit: use generic logging module instead of print()
There are multiple advantages of using Python's logging module:
- advanced message formatting (file name, line number, etc.),
- multiple logging targets (e.g. stderr, file, socket),
- logging levels (e.g. DEBUG, INFO, ERROR),
- the pythonic way ;)
so, let's replace multiple print() calls by logging calls,
add use the following logging message format by default:
[%(levelname)s] %(filename)s:%(lineno)d %(message)s
Examples:
[INFO] ctrl_if_bts.py:57 Starting transceiver...
[DEBUG] clck_gen.py:87 IND CLOCK 26826
[DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
[INFO] ctrl_if_bts.py:73 Stopping transceiver...
[INFO] fake_trx.py:127 Shutting down...
Please note that there is no way to filter messages by logging
level yet. This is to be introduced soon, together with argparse.
Change-Id: I7fcafabafe8323b58990997a47afdd48b6d1f357
2018-12-06 22:00:26 +00:00
|
|
|
log.info("Sending %d/%d %s burst %s to %s..."
|
2018-12-06 23:52:59 +00:00
|
|
|
% (i + 1, self.argv.burst_count, self.argv.burst_type,
|
|
|
|
msg.desc_hdr(), self.argv.conn_mode))
|
2018-01-27 14:47:31 +00:00
|
|
|
|
|
|
|
# Send message
|
|
|
|
self.data_if.send_msg(msg)
|
2017-07-28 07:56:09 +00:00
|
|
|
|
2018-02-20 11:19:48 +00:00
|
|
|
# Append a new message to the capture
|
2018-12-06 23:52:59 +00:00
|
|
|
if self.argv.output_file is not None:
|
2018-02-20 11:19:48 +00:00
|
|
|
self.ddf.append_msg(msg)
|
|
|
|
|
2017-07-28 07:56:09 +00:00
|
|
|
def parse_argv(self):
|
2018-12-06 23:52:59 +00:00
|
|
|
parser = argparse.ArgumentParser(prog = "burst_gen",
|
|
|
|
description = "Auxiliary tool to generate and send random bursts")
|
|
|
|
|
2018-12-07 02:34:00 +00:00
|
|
|
# Register common logging options
|
|
|
|
self.app_reg_logging_options(parser)
|
|
|
|
|
2018-12-06 23:52:59 +00:00
|
|
|
trx_group = parser.add_argument_group("TRX interface")
|
|
|
|
trx_group.add_argument("-r", "--remote-addr",
|
|
|
|
dest = "remote_addr", type = str, default = "127.0.0.1",
|
|
|
|
help = "Set remote address (default %(default)s)")
|
|
|
|
trx_group.add_argument("-b", "--bind-addr",
|
|
|
|
dest = "bind_addr", type = str, default = "0.0.0.0",
|
|
|
|
help = "Set bind address (default %(default)s)")
|
|
|
|
trx_group.add_argument("-p", "--base-port",
|
|
|
|
dest = "base_port", type = int, default = 6700,
|
|
|
|
help = "Set base port number (default %(default)s)")
|
|
|
|
trx_group.add_argument("-m", "--conn-mode",
|
|
|
|
dest = "conn_mode", type = str,
|
|
|
|
choices = ["TRX", "L1"], default = "TRX",
|
|
|
|
help = "Where to send bursts (default %(default)s)")
|
|
|
|
trx_group.add_argument("-o", "--output-file",
|
|
|
|
dest = "output_file", type = str,
|
|
|
|
help = "Write bursts to a capture file")
|
|
|
|
|
|
|
|
bg_group = parser.add_argument_group("Burst generation")
|
|
|
|
bg_group.add_argument("-B", "--burst-type",
|
|
|
|
dest = "burst_type", type = str,
|
|
|
|
choices = ["NB", "FB", "SB", "AB"], default = "NB",
|
|
|
|
help = "Random burst type (default %(default)s)")
|
|
|
|
bg_group.add_argument("-c", "--burst-count", metavar = "N",
|
|
|
|
dest = "burst_count", type = int, default = 1,
|
|
|
|
help = "How many bursts to send (default %(default)s)")
|
2019-07-23 22:04:36 +00:00
|
|
|
bg_group.add_argument("-v", "--hdr-version", metavar = "VER",
|
|
|
|
dest = "hdr_ver", type = int,
|
2021-04-28 00:05:15 +00:00
|
|
|
default = 0, choices = Msg.KNOWN_VERSIONS,
|
2019-07-23 22:04:36 +00:00
|
|
|
help = "TRXD header version (default %(default)s)")
|
2018-12-06 23:52:59 +00:00
|
|
|
bg_group.add_argument("-f", "--frame-number", metavar = "FN",
|
|
|
|
dest = "tdma_fn", type = int,
|
|
|
|
help = "Set TDMA frame number (default random)")
|
|
|
|
bg_group.add_argument("-t", "--timeslot", metavar = "TN",
|
|
|
|
dest = "tdma_tn", type = int, choices = range(0, 8),
|
|
|
|
help = "Set TDMA timeslot (default random)")
|
|
|
|
|
|
|
|
bg_pwr_group = bg_group.add_mutually_exclusive_group()
|
|
|
|
bg_pwr_group.add_argument("--pwr", metavar = "dBm",
|
|
|
|
dest = "pwr", type = int,
|
|
|
|
help = "Set power level (default random)")
|
|
|
|
bg_pwr_group.add_argument("--rssi", metavar = "dBm",
|
|
|
|
dest = "rssi", type = int,
|
|
|
|
help = "Set RSSI (default random)")
|
|
|
|
|
|
|
|
bg_toa_group = bg_group.add_mutually_exclusive_group()
|
|
|
|
bg_toa_group.add_argument("--toa",
|
|
|
|
dest = "toa", type = int,
|
|
|
|
help = "Set Timing of Arrival in symbols (default random)")
|
|
|
|
bg_toa_group.add_argument("--toa256",
|
|
|
|
dest = "toa256", type = int,
|
|
|
|
help = "Set Timing of Arrival in 1/256 symbol periods")
|
|
|
|
|
2019-07-23 22:04:36 +00:00
|
|
|
bg_group.add_argument("--tsc", metavar = "TSC",
|
|
|
|
dest = "tsc", type = int, choices = range(0, 8),
|
|
|
|
help = "Set Training Sequence Code (default random)")
|
|
|
|
bg_group.add_argument("--ci", metavar = "CI",
|
|
|
|
dest = "ci", type = int,
|
|
|
|
help = "C/I: Carrier-to-Interference ratio "
|
|
|
|
"in centiBels (default random)")
|
|
|
|
|
2018-12-06 23:52:59 +00:00
|
|
|
return parser.parse_args()
|
2018-01-27 13:55:57 +00:00
|
|
|
|
2017-07-28 07:56:09 +00:00
|
|
|
def sig_handler(self, signum, frame):
|
trx_toolkit: use generic logging module instead of print()
There are multiple advantages of using Python's logging module:
- advanced message formatting (file name, line number, etc.),
- multiple logging targets (e.g. stderr, file, socket),
- logging levels (e.g. DEBUG, INFO, ERROR),
- the pythonic way ;)
so, let's replace multiple print() calls by logging calls,
add use the following logging message format by default:
[%(levelname)s] %(filename)s:%(lineno)d %(message)s
Examples:
[INFO] ctrl_if_bts.py:57 Starting transceiver...
[DEBUG] clck_gen.py:87 IND CLOCK 26826
[DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
[INFO] ctrl_if_bts.py:73 Stopping transceiver...
[INFO] fake_trx.py:127 Shutting down...
Please note that there is no way to filter messages by logging
level yet. This is to be introduced soon, together with argparse.
Change-Id: I7fcafabafe8323b58990997a47afdd48b6d1f357
2018-12-06 22:00:26 +00:00
|
|
|
log.info("Signal %d received" % signum)
|
2019-07-23 22:40:37 +00:00
|
|
|
if signum == signal.SIGINT:
|
2017-07-28 07:56:09 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app = Application()
|
2017-08-03 14:00:12 +00:00
|
|
|
app.run()
|