summaryrefslogtreecommitdiffstats
path: root/src/target/fake_trx/burst_fwd.py
blob: d822747188d3998ec00db17abe1fef2880da71c7 (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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# Virtual Um-interface (fake transceiver)
# BTS <-> BB burst forwarding
#
# (C) 2017 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.

class BurstForwarder:
	# Timeslot filter
	ts_pass = 0

	def __init__(self, bts_link, bb_link):
		self.bts_link = bts_link
		self.bb_link = bb_link

	def set_slot(self, ts):
		if ts > 0 and ts < 8:
			self.ts_pass = ts
		else:
			raise ValueError("Incorrect index for timeslot filter")

	def process_payload(self, data):
		payload = bytearray(data)
		length = len(payload)

		# HACK: set fake RSSI value (-53)
		payload[5] = 0x35

		# HACK: add fake TOA value (6th and 7th bytes)
		payload[6:2] = [0x00, 0x00]

		# Convert ubits to {255..0}
		for i in range(8, length):
			payload[i] = 255 if payload[i] else 0

		return payload

	# Downlink handler: BTS -> BB
	def bts2bb(self):
		# Read data from socket
		data, addr = self.bts_link.sock.recvfrom(512)
		payload = self.process_payload(data)

		# Timeslot filter
		if payload[0] != self.ts_pass:
			return None

		# Send burst to BB
		self.bb_link.send(payload)

	# Uplink handler: BB -> BTS
	def bb2bts(self):
		# Read data from socket
		data, addr = self.bb_link.sock.recvfrom(512)
		payload = self.process_payload(data)

		# Send burst to BTS
		self.bts_link.send(payload)