summaryrefslogtreecommitdiffstats
path: root/src/target/fake_trx/burst_send.py
blob: d6c5c0c584d21ca64d852019e3c39533a4696f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# Auxiliary tool to send existing bursts via TRX DATA interface
#
# (C) 2017-2018 by Vadim Yanitskiy <axilirator@gmail.com>
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import signal
import getopt
import sys

from data_dump import DATADumpFile
from data_if import DATAInterface
from gsm_shared import *
from data_msg import *

COPYRIGHT = \
	"Copyright (C) 2017-2018 by Vadim Yanitskiy <axilirator@gmail.com>\n" \
	"License GPLv2+: GNU GPL version 2 or later " \
	"<http://gnu.org/licenses/gpl.html>\n" \
	"This is free software: you are free to change and redistribute it.\n" \
	"There is NO WARRANTY, to the extent permitted by law.\n"

class Application:
	# Application variables
	remote_addr = "127.0.0.1"
	base_port = 5700
	conn_mode = "TRX"

	# Burst source
	capture_file = None

	# Count limitations
	msg_skip = None
	msg_count = None

	# Pass filtering
	pf_fn_lt = None
	pf_fn_gt = None
	pf_tn = None

	def __init__(self):
		self.print_copyright()
		self.parse_argv()

		# Set up signal handlers
		signal.signal(signal.SIGINT, self.sig_handler)

		# Open requested capture file
		self.ddf = DATADumpFile(self.capture_file)

	def run(self):
		# Init DATA interface with TRX or L1
		if self.conn_mode == "TRX":
			self.data_if = DATAInterface(self.remote_addr,
				self.base_port + 2, self.base_port + 102)
			l12trx = True
		elif self.conn_mode == "L1":
			self.data_if = DATAInterface(self.remote_addr,
				self.base_port + 102, self.base_port + 2)
			l12trx = False
		else:
			self.print_help("[!] Unknown connection type")
			sys.exit(2)

		# Read messages from the capture
		messages = self.ddf.parse_all(
			skip = self.msg_skip, count = self.msg_count)
		if messages is False:
			pass # FIXME!!!

		for msg in messages:
			# Pass filter
			if not self.msg_pass_filter(l12trx, msg):
				continue

			# HACK: as ToA parsing is not implemented yet,
			# we have to use a fixed value for now...
			msg.toa256 = 0

			print("[i] Sending a burst %s to %s..."
				% (msg.desc_hdr(), self.conn_mode))

			# Send message
			self.data_if.send_msg(msg)

	def msg_pass_filter(self, l12trx, msg):
		# Direction filter
		if isinstance(msg, DATAMSG_L12TRX) and not l12trx:
			return False
		elif isinstance(msg, DATAMSG_TRX2L1) and l12trx:
			return False

		# Timeslot filter
		if self.pf_tn is not None:
			if msg.tn != self.pf_tn:
				return False

		# Frame number filter
		if self.pf_fn_lt is not None:
			if msg.fn > self.pf_fn_lt:
				return False
		if self.pf_fn_gt is not None:
			if msg.fn < self.pf_fn_gt:
				return False

		# Burst passed ;)
		return True

	def print_copyright(self):
		print(COPYRIGHT)

	def print_help(self, msg = None):
		s  = " Usage: " + sys.argv[0] + " [options]\n\n" \
			 " Some help...\n" \
			 "  -h --help              this text\n\n"

		s += " TRX interface specific\n" \
			 "  -m --conn-mode         Send bursts to: TRX (default) / L1\n"    \
			 "  -r --remote-addr       Set remote address (default %s)\n"       \
			 "  -p --base-port         Set base port number (default %d)\n\n"

		s += " Burst source\n" \
			 "  -i --capture-file      Read bursts from capture file\n\n" \

		s += " Count limitations (disabled by default)\n" \
			 "  --msg-skip      NUM    Skip NUM messages before sending\n"   \
			 "  --msg-count     NUM    Stop after sending NUM messages\n\n"  \

		s += " Filtering (disabled by default)\n" \
			 "  --timeslot      NUM    TDMA timeslot number [0..7]\n"        \
			 "  --frame-num-lt  NUM    TDMA frame number lower than NUM\n"   \
			 "  --frame-num-gt  NUM    TDMA frame number greater than NUM\n"

		print(s % (self.remote_addr, self.base_port))

		if msg is not None:
			print(msg)

	def parse_argv(self):
		try:
			opts, args = getopt.getopt(sys.argv[1:],
				"m:r:p:i:h",
				[
					"help",
					"conn-mode=",
					"remote-addr=",
					"base-port=",
					"capture-file=",
					"msg-skip=",
					"msg-count=",
					"timeslot=",
					"frame-num-lt=",
					"frame-num-gt=",
				])
		except getopt.GetoptError as err:
			self.print_help("[!] " + str(err))
			sys.exit(2)

		for o, v in opts:
			if o in ("-h", "--help"):
				self.print_help()
				sys.exit(2)

			# Capture file
			elif o in ("-i", "--capture-file"):
				self.capture_file = v

			# TRX interface specific
			elif o in ("-m", "--conn-mode"):
				self.conn_mode = v
			elif o in ("-r", "--remote-addr"):
				self.remote_addr = v
			elif o in ("-p", "--base-port"):
				self.base_port = int(v)

			# Count limitations
			elif o == "--msg-skip":
				self.msg_skip = int(v)
			elif o == "--msg-count":
				self.msg_count = int(v)

			# Timeslot pass filter
			elif o == "--timeslot":
				self.pf_tn = int(v)
				if self.pf_tn < 0 or self.pf_tn > 7:
					self.print_help("[!] Wrong timeslot value")
					sys.exit(2)

			# Frame number pass filter
			elif o == "--frame-num-lt":
				self.pf_fn_lt = int(v)
			elif o == "--frame-num-gt":
				self.pf_fn_gt = int(v)

		if self.capture_file is None:
			self.print_help("[!] Please specify a capture file")
			sys.exit(2)

	def sig_handler(self, signum, frame):
		print("Signal %d received" % signum)
		if signum is signal.SIGINT:
			sys.exit(0)

if __name__ == '__main__':
	app = Application()
	app.run()