blob: 11f1d405e35f9b7e82c226c3a4ef6e55956115be [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#
Vadim Yanitskiy97dc84e2018-08-10 05:29:23 +07007# (C) 2016-2018 by Vadim Yanitskiy <axilirator@gmail.com>
Piotr Krysikd2f162f2017-11-30 12:50:00 +01008# (C) 2017 by Piotr Krysik <ptrkrysik@gmail.com>
Piotr Krysik902f4eb2017-09-19 08:04:33 +02009#
10# All Rights Reserved
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License along
23# with this program; if not, write to the Free Software Foundation, Inc.,
24# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26import pmt
27import time
28import grgsm
Piotr Krysik902f4eb2017-09-19 08:04:33 +020029
30from math import pi
31
Vadim Yanitskiy97dc84e2018-08-10 05:29:23 +070032from gnuradio import eng_notation
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070033from 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 Yanitskiyd222ee52017-12-03 23:49:09 +070038from gnuradio import filter
39from gnuradio.filter import firdes
40
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070041
42# HACK: should be implemented in C++!
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070043class dict_toggle_sign(gr.basic_block):
44 def __init__(self): # only default arguments here
45 gr.basic_block.__init__(self,
46 name='Change sign of elts in dict',
47 in_sig=[],
48 out_sig=[]
49 )
50 self.message_port_register_in(pmt.intern("dict_in"))
51 self.message_port_register_out(pmt.intern("dict_out"))
52 self.set_msg_handler(pmt.intern("dict_in"), self.change_sign)
53
54 def change_sign(self, msg):
55 if pmt.is_dict(msg):
56 d = pmt.to_python(msg)
57 for key, value in d.items():
58 d[key] *= -1
59 self.message_port_pub(pmt.intern("dict_out"), pmt.to_pmt(d))
60
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070061class radio_if(gr.top_block):
Piotr Krysik1cc264f2018-09-06 19:57:57 +020062 # PHY specific variables
63 freq_offset_hz = None
64 rx_freq = 935e6
65 tx_freq = 890e6
66 osr = 4
Piotr Krysik902f4eb2017-09-19 08:04:33 +020067
Piotr Krysik1cc264f2018-09-06 19:57:57 +020068 # Application state flags
69 trx_started = False
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070070
Piotr Krysik1cc264f2018-09-06 19:57:57 +020071 # GSM timings (in microseconds [uS])
72 # One timeslot duration is 576.9 μs = 15/26 ms,
73 # or 156.25 symbol periods (a symbol period is 48/13 μs)
74 GSM_SYM_PERIOD_uS = 48.0 / 13.0
75 GSM_TS_PERIOD_uS = GSM_SYM_PERIOD_uS * 156.25
76 GSM_UL_DL_SHIFT_uS = -(GSM_TS_PERIOD_uS * 3)
Vadim Yanitskiy9dded9b2017-12-05 01:00:51 +070077
Piotr Krysikb3d5c572018-09-12 00:35:05 +070078 GSM_SYM_RATE = (1.0 / GSM_SYM_PERIOD_uS) * 1e6
79 SAMPLE_RATE = GSM_SYM_RATE * osr
Vadim Yanitskiy38baac92018-09-07 19:09:12 +070080
Piotr Krysik1cc264f2018-09-06 19:57:57 +020081 # FIXME: shall be measured (automatically?) for
82 # particular device and particular clock rate.
83 # The current value is measured for USRP B2X0 at 26e6.
84 delay_correction = (285.616 + 2 * GSM_SYM_PERIOD_uS) * 1e-6
Piotr Krysik902f4eb2017-09-19 08:04:33 +020085
Piotr Krysik1cc264f2018-09-06 19:57:57 +020086 def __init__(self, phy_args, phy_sample_rate,
87 phy_rx_gain, phy_tx_gain, phy_ppm,
88 phy_rx_antenna, phy_tx_antenna,
Piotr Krysik06317672018-09-13 14:41:06 +020089 phy_freq_offset_hz, trx_bind_addr,
90 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020091
Piotr Krysik1cc264f2018-09-06 19:57:57 +020092 print("[i] Init Radio interface (L:%s:%u <-> R:%s:%u)"
93 % (trx_bind_addr, trx_base_port + 2,
94 trx_remote_addr, trx_base_port + 102))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020095
Piotr Krysik1cc264f2018-09-06 19:57:57 +020096 # PHY specific variables
97 self.sample_rate = phy_sample_rate
98 self.rx_gain = phy_rx_gain
99 self.tx_gain = phy_tx_gain
100 self.ppm = phy_ppm
Piotr Krysik06317672018-09-13 14:41:06 +0200101 self.freq_offset_hz = phy_freq_offset_hz
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200102
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200103 gr.top_block.__init__(self, "GR-GSM TRX")
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200104
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200105 # TRX Burst Interface
106 self.trx_burst_if = grgsm.trx_burst_if(
107 trx_bind_addr, trx_remote_addr,
108 str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200109
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200110 # RX path definition
111 self.phy_src = uhd.usrp_source(phy_args,
112 uhd.stream_args(cpu_format="fc32",
113 channels=range(1)))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700114
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200115 self.phy_src.set_clock_rate(26e6, uhd.ALL_MBOARDS)
116 self.phy_src.set_center_freq(self.rx_freq, 0)
117 self.phy_src.set_antenna(phy_rx_antenna, 0)
118 self.phy_src.set_samp_rate(phy_sample_rate)
119 self.phy_src.set_bandwidth(650e3, 0)
120 self.phy_src.set_gain(phy_rx_gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200121
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200122 self.msg_to_tag_src = grgsm.msg_to_tag()
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200123
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200124 self.rotator_src = grgsm.controlled_rotator_cc(
125 self.calc_phase_inc(self.rx_freq))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200126
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200127 self.lpf = filter.fir_filter_ccf(1, firdes.low_pass(
128 1, phy_sample_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700129
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200130 self.gsm_receiver = grgsm.receiver(self.osr, ([0]), ([]))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200131
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200132 self.ts_filter = grgsm.burst_timeslot_filter(0)
133 self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700134
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200135 # Connections
136 self.connect(
137 (self.phy_src, 0),
138 (self.msg_to_tag_src, 0))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200139
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200140 self.connect(
141 (self.msg_to_tag_src, 0),
142 (self.rotator_src, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700143
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200144 self.connect(
145 (self.rotator_src, 0),
146 (self.lpf, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700147
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200148 self.connect(
149 (self.lpf, 0),
150 (self.gsm_receiver, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700151
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200152 self.msg_connect(
153 (self.gsm_receiver, 'C0'),
154 (self.ts_filter, 'in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700155
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200156 self.msg_connect(
157 (self.ts_filter, 'out'),
158 (self.trx_burst_if, 'bursts'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700159
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700160
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200161 # TX Path Definition
162 self.phy_sink = uhd.usrp_sink(phy_args,
163 uhd.stream_args(cpu_format="fc32",
164 channels=range(1)), "packet_len")
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200165
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200166 self.phy_sink.set_clock_rate(26e6, uhd.ALL_MBOARDS)
167 self.phy_sink.set_antenna(phy_tx_antenna, 0)
168 self.phy_sink.set_samp_rate(phy_sample_rate)
169 self.phy_sink.set_center_freq(self.tx_freq, 0)
170 self.phy_sink.set_gain(self.tx_gain)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700171
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200172 self.tx_time_setter = grgsm.txtime_setter(
173 0xffffffff, 0, 0, 0, 0, 0,
174 self.delay_correction + self.GSM_UL_DL_SHIFT_uS * 1e-6)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700175
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200176 self.tx_burst_proc = grgsm.preprocess_tx_burst()
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700177
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200178 self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(
179 blocks.byte_t, 'packet_len')
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700180
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200181 self.gmsk_mod = grgsm.gsm_gmsk_mod(
182 BT = 0.3, pulse_duration = 4, sps = self.osr)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700183
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200184 self.burst_shaper = digital.burst_shaper_cc(
185 (firdes.window(firdes.WIN_HANN, 16, 0)),
186 0, 20, False, "packet_len")
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700187
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200188 self.msg_to_tag_sink = grgsm.msg_to_tag()
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700189
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200190 self.rotator_sink = grgsm.controlled_rotator_cc(
191 -self.calc_phase_inc(self.tx_freq))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700192
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200193 # Connections
194 self.msg_connect(
195 (self.trx_burst_if, 'bursts'),
196 (self.tx_time_setter, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700197
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200198 self.msg_connect(
199 (self.tx_time_setter, 'bursts_out'),
200 (self.tx_burst_proc, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700201
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200202 self.msg_connect(
203 (self.tx_burst_proc, 'bursts_out'),
204 (self.pdu_to_tagged_stream, 'pdus'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700205
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200206 self.connect(
207 (self.pdu_to_tagged_stream, 0),
208 (self.gmsk_mod, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700209
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200210 self.connect(
211 (self.gmsk_mod, 0),
212 (self.burst_shaper, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700213
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200214 self.connect(
215 (self.burst_shaper, 0),
216 (self.msg_to_tag_sink, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700217
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200218 self.connect(
219 (self.msg_to_tag_sink, 0),
220 (self.rotator_sink, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700221
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200222 self.connect(
223 (self.rotator_sink, 0),
224 (self.phy_sink, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700225
226
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200227 # RX & TX synchronization
228 self.bt_filter = grgsm.burst_type_filter([3])
229 self.burst_to_fn_time = grgsm.burst_to_fn_time()
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700230
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200231 # Connections
232 self.msg_connect(
233 (self.gsm_receiver, 'C0'),
234 (self.bt_filter, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700235
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200236 self.msg_connect(
237 (self.bt_filter, 'bursts_out'),
238 (self.burst_to_fn_time, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700239
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200240 self.msg_connect(
241 (self.burst_to_fn_time, 'fn_time_out'),
242 (self.tx_time_setter, 'fn_time'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200243
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700244
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200245 # AFC (Automatic Frequency Correction)
246 self.gsm_clck_ctrl = grgsm.clock_offset_control(
247 self.rx_freq, phy_sample_rate, osr = self.osr)
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700248
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200249 self.dict_toggle_sign = dict_toggle_sign()
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700250
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200251 # Connections
252 self.msg_connect(
253 (self.gsm_receiver, 'measurements'),
254 (self.gsm_clck_ctrl, 'measurements'))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700255
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200256 self.msg_connect(
257 (self.gsm_clck_ctrl, 'ctrl'),
258 (self.msg_to_tag_src, 'msg'))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700259
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200260 self.msg_connect(
261 (self.gsm_clck_ctrl, 'ctrl'),
262 (self.dict_toggle_sign, 'dict_in'))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700263
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200264 self.msg_connect(
265 (self.dict_toggle_sign, 'dict_out'),
266 (self.msg_to_tag_sink, 'msg'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200267
Vadim Yanitskiy0017a352018-03-03 23:37:30 +0700268
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200269 # Some UHD devices (such as UmTRX) do start the clock
270 # not from 0, so it's required to reset it manually.
271 # Resetting UHD source will also affect the sink.
272 self.phy_src.set_time_now(uhd.time_spec(0.0))
Vadim Yanitskiy0017a352018-03-03 23:37:30 +0700273
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200274 def shutdown(self):
275 print("[i] Shutdown Radio interface")
276 self.stop()
277 self.wait()
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200278
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200279 def calc_phase_inc(self, fc):
280 return self.ppm / 1.0e6 * 2 * pi * fc / self.sample_rate
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700281
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200282 def set_rx_freq(self, fc):
283 if self.freq_offset_hz is not None:
284 fc += self.freq_offset_hz
285 print("[#] Shifting RX freq. to %s (offset is %s)"
286 % (eng_notation.num_to_str(fc),
287 eng_notation.num_to_str(self.freq_offset_hz)))
288 self.phy_src.set_center_freq(fc, 0)
289 self.rotator_src.set_phase_inc(self.calc_phase_inc(fc))
290 self.rx_freq = fc
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700291
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200292 def set_tx_freq(self, fc):
293 if self.freq_offset_hz is not None:
294 fc += self.freq_offset_hz
295 print("[#] Shifting TX freq. to %s (offset is %s)"
296 % (eng_notation.num_to_str(fc),
297 eng_notation.num_to_str(self.freq_offset_hz)))
298 self.phy_sink.set_center_freq(fc, 0)
299 self.rotator_sink.set_phase_inc(-self.calc_phase_inc(fc))
300 self.tx_freq = fc
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200301
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200302 def set_rx_gain(self, gain):
303 self.phy_src.set_gain(gain, 0)
304 self.rx_gain = gain
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700305
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200306 def set_tx_gain(self, gain):
307 self.phy_sink.set_gain(gain, 0)
308 self.tx_gain = gain
Vadim Yanitskiy34266e72017-12-05 01:01:43 +0700309
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200310 def set_ta(self, ta):
311 print("[i] Setting TA value %d" % ta)
312 advance_time_sec = ta * self.GSM_SYM_PERIOD_uS * 1e-6
313 self.tx_time_setter.set_timing_advance(advance_time_sec)