blob: 9c26ec6a161de90f6203c79003865d4f6a112c7e [file] [log] [blame]
Piotr Krysik902f4eb2017-09-19 08:04:33 +02001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4# GR-GSM based transceiver
5# Follow graph implementation
6#
7# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
8#
9# All Rights Reserved
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License along
22# with this program; if not, write to the Free Software Foundation, Inc.,
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25import pmt
26import time
27import grgsm
28import osmosdr
29
30from math import pi
31
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070032from gnuradio.filter import firdes
33from gnuradio import digital
Piotr Krysik902f4eb2017-09-19 08:04:33 +020034from gnuradio import blocks
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070035from gnuradio import uhd
Piotr Krysik902f4eb2017-09-19 08:04:33 +020036from gnuradio import gr
37
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070038
39# HACK: should be implemented in C++!
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070040class burst_to_fn_time(gr.basic_block):
41 def __init__(self): # only default arguments here
42 gr.basic_block.__init__(
43 self,
44 name='Burst to fn_time',
45 in_sig=[],
46 out_sig=[]
47 )
48 self.message_port_register_in(pmt.intern("bursts_in"))
49 self.message_port_register_out(pmt.intern("fn_time_out"))
50 self.set_msg_handler(pmt.intern("bursts_in"), self.convert)
51
52 def convert(self, msg):
53 fn_time = pmt.dict_ref(pmt.car(msg),pmt.intern("fn_time"),pmt.PMT_NIL)
54 fn_time_msg = pmt.dict_add(pmt.make_dict(), pmt.intern("fn_time"), fn_time)
55 if pmt.to_python(fn_time) is not None:
56 self.message_port_pub(pmt.intern("fn_time_out"), fn_time_msg)
57
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070058class radio_if(gr.top_block):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020059 # PHY specific variables
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070060 rx_freq = 935e6
61 tx_freq = 890e6
Piotr Krysik902f4eb2017-09-19 08:04:33 +020062
63 # Application state flags
64 trx_started = False
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070065
66 # GSM timings
67 delay_correction = 285.616e-6
68 ul_dl_shift = -(6.0/1625000*(156.25)*3)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020069
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070070 def __init__(self, phy_args, phy_sample_rate,
71 phy_rx_gain, phy_tx_gain, phy_ppm,
72 phy_rx_antenna, phy_tx_antenna,
Vadim Yanitskiy5d68aa52017-10-17 11:14:48 +070073 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020074
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070075 print("[i] Init Radio interface")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020076
77 # PHY specific variables
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070078 self.rx_gain = phy_rx_gain
79 self.tx_gain = phy_tx_gain
Piotr Krysik902f4eb2017-09-19 08:04:33 +020080
81 gr.top_block.__init__(self, "GR-GSM TRX")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020082
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070083 # TRX Burst Interface
84 self.trx_burst_if = grgsm.trx_burst_if(
85 trx_remote_addr, str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020086
Piotr Krysik902f4eb2017-09-19 08:04:33 +020087
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070088 # RX path definition
89 self.phy_src = uhd.usrp_source(phy_args,
90 uhd.stream_args(cpu_format="fc32",
91 channels=range(1)))
92
93 self.phy_src.set_center_freq(self.rx_freq, 0)
94 self.phy_src.set_antenna(phy_rx_antenna, 0)
95 self.phy_src.set_samp_rate(phy_sample_rate)
96 self.phy_src.set_bandwidth(650e3, 0)
97 self.phy_src.set_gain(phy_rx_gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020098
99 self.gsm_input = grgsm.gsm_input(
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700100 ppm = phy_ppm - int(phy_ppm), osr = 4,
101 fc = self.rx_freq, samp_rate_in = phy_sample_rate)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200102
103 self.gsm_receiver = grgsm.receiver(4, ([0]), ([]))
104
105 self.gsm_clck_ctrl = grgsm.clock_offset_control(
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700106 self.rx_freq, phy_sample_rate, osr = 4)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200107
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700108 self.ts_filter = grgsm.burst_timeslot_filter(0)
109 self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700110
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200111 # Connections
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700112 self.connect(
113 (self.phy_src, 0),
114 (self.gsm_input, 0))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200115
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700116 self.connect(
117 (self.gsm_input, 0),
118 (self.gsm_receiver, 0))
119
120 self.msg_connect(
121 (self.gsm_receiver, 'C0'),
122 (self.ts_filter, 'in'))
123
124 self.msg_connect(
125 (self.ts_filter, 'out'),
126 (self.trx_burst_if, 'bursts'))
127
128 self.msg_connect(
129 (self.gsm_receiver, 'measurements'),
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200130 (self.gsm_clck_ctrl, 'measurements'))
131
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700132 self.msg_connect(
133 (self.gsm_clck_ctrl, 'ctrl'),
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200134 (self.gsm_input, 'ctrl_in'))
135
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700136
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700137 # TX Path Definition
138 self.phy_sink = uhd.usrp_sink(phy_args,
139 uhd.stream_args(cpu_format="fc32",
140 channels=range(1)), "packet_len")
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200141
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700142 self.phy_sink.set_antenna(phy_tx_antenna, 0)
143 self.phy_sink.set_samp_rate(phy_sample_rate)
144 self.phy_sink.set_center_freq(self.tx_freq, 0)
145 self.phy_sink.set_gain(self.tx_gain)
146
147 self.tx_time_setter = grgsm.txtime_setter(
148 0xffffffff, 0, 0, 0, 0, 0,
149 self.delay_correction + self.ul_dl_shift)
150
151 self.tx_burst_proc = grgsm.preprocess_tx_burst()
152
153 self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(
154 blocks.byte_t, 'packet_len')
155
156 self.gmsk_mod = grgsm.gsm_gmsk_mod(
157 BT = 4, pulse_duration = 4, sps = 4)
158
159 self.burst_shaper = digital.burst_shaper_cc(
160 (firdes.window(firdes.WIN_HANN, 16, 0)),
161 0, 20, False, "packet_len")
162
163 # Connections
164 self.msg_connect(
165 (self.trx_burst_if, 'bursts'),
166 (self.tx_time_setter, 'bursts_in'))
167
168 self.msg_connect(
169 (self.tx_time_setter, 'bursts_out'),
170 (self.tx_burst_proc, 'bursts_in'))
171
172 self.msg_connect(
173 (self.tx_burst_proc, 'bursts_out'),
174 (self.pdu_to_tagged_stream, 'pdus'))
175
176 self.connect(
177 (self.pdu_to_tagged_stream, 0),
178 (self.gmsk_mod, 0))
179
180 self.connect(
181 (self.gmsk_mod, 0),
182 (self.burst_shaper, 0))
183
184 self.connect(
185 (self.burst_shaper, 0),
186 (self.phy_sink, 0))
187
188
189 # RX & TX synchronization
Vadim Yanitskiy4650fad2017-12-01 05:08:35 +0700190 self.bt_filter = grgsm.burst_type_filter([3])
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700191 self.burst_to_fn_time = burst_to_fn_time()
192
193 # Connections
194 self.msg_connect(
195 (self.gsm_receiver, 'C0'),
196 (self.bt_filter, 'bursts_in'))
197
198 self.msg_connect(
199 (self.bt_filter, 'bursts_out'),
200 (self.burst_to_fn_time, 'bursts_in'))
201
202 self.msg_connect(
203 (self.burst_to_fn_time, 'fn_time_out'),
204 (self.tx_time_setter, 'fn_time'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200205
206 def shutdown(self):
207 print("[i] Shutdown Radio interface")
208 self.stop()
209 self.wait()
210
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700211 def set_rx_freq(self, fc):
212 self.phy_src.set_center_freq(fc, 0)
213 self.gsm_clck_ctrl.set_fc(fc)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200214 self.gsm_input.set_fc(fc)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700215 self.rx_freq = fc
216
217 def set_tx_freq(self, fc):
218 self.phy_sink.set_center_freq(fc, 0)
219 self.tx_freq = fc
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200220
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700221 def set_rx_gain(self, gain):
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700222 self.phy_src.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700223 self.rx_gain = gain
224
225 def set_tx_gain(self, gain):
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700226 self.phy_sink.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700227 self.tx_gain = gain