blob: 32b767a39959c4b73a26bf596ee7caf83ea501db [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
Vadim Yanitskiy7da82f42019-01-19 09:28:23 +070029import random
Piotr Krysik902f4eb2017-09-19 08:04:33 +020030
31from math import pi
32
Vadim Yanitskiy97dc84e2018-08-10 05:29:23 +070033from gnuradio import eng_notation
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070034from gnuradio import digital
Piotr Krysik902f4eb2017-09-19 08:04:33 +020035from gnuradio import blocks
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070036from gnuradio import uhd
Piotr Krysik902f4eb2017-09-19 08:04:33 +020037from gnuradio import gr
38
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070039from gnuradio import filter
40from gnuradio.filter import firdes
41
Vadim Yanitskiybd4daec2018-12-20 09:32:56 +070042from dict_toggle_sign import dict_toggle_sign
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070043
Vadim Yanitskiyf237f1a2018-12-20 09:49:56 +070044class RadioInterface(gr.top_block):
Piotr Krysik1cc264f2018-09-06 19:57:57 +020045 # PHY specific variables
Piotr Krysik1cc264f2018-09-06 19:57:57 +020046 rx_freq = 935e6
47 tx_freq = 890e6
48 osr = 4
Piotr Krysik902f4eb2017-09-19 08:04:33 +020049
Piotr Krysik1cc264f2018-09-06 19:57:57 +020050 # Application state flags
51 trx_started = False
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070052
Piotr Krysik1cc264f2018-09-06 19:57:57 +020053 # GSM timings (in microseconds [uS])
54 # One timeslot duration is 576.9 μs = 15/26 ms,
55 # or 156.25 symbol periods (a symbol period is 48/13 μs)
56 GSM_SYM_PERIOD_uS = 48.0 / 13.0
57 GSM_TS_PERIOD_uS = GSM_SYM_PERIOD_uS * 156.25
58 GSM_UL_DL_SHIFT_uS = -(GSM_TS_PERIOD_uS * 3)
Vadim Yanitskiy9dded9b2017-12-05 01:00:51 +070059
Piotr Krysikb3d5c572018-09-12 00:35:05 +070060 GSM_SYM_RATE = (1.0 / GSM_SYM_PERIOD_uS) * 1e6
61 SAMPLE_RATE = GSM_SYM_RATE * osr
Vadim Yanitskiy38baac92018-09-07 19:09:12 +070062
Piotr Krysik1cc264f2018-09-06 19:57:57 +020063 # FIXME: shall be measured (automatically?) for
64 # particular device and particular clock rate.
65 # The current value is measured for USRP B2X0 at 26e6.
66 delay_correction = (285.616 + 2 * GSM_SYM_PERIOD_uS) * 1e-6
Piotr Krysik902f4eb2017-09-19 08:04:33 +020067
Piotr Krysik1cc264f2018-09-06 19:57:57 +020068 def __init__(self, phy_args, phy_sample_rate,
69 phy_rx_gain, phy_tx_gain, phy_ppm,
70 phy_rx_antenna, phy_tx_antenna,
Piotr Krysikb8632ff2018-09-13 15:42:55 +020071 phy_freq_offset, trx_bind_addr,
Piotr Krysik06317672018-09-13 14:41:06 +020072 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020073
Piotr Krysik1cc264f2018-09-06 19:57:57 +020074 print("[i] Init Radio interface (L:%s:%u <-> R:%s:%u)"
75 % (trx_bind_addr, trx_base_port + 2,
76 trx_remote_addr, trx_base_port + 102))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020077
Piotr Krysik1cc264f2018-09-06 19:57:57 +020078 # PHY specific variables
79 self.sample_rate = phy_sample_rate
80 self.rx_gain = phy_rx_gain
81 self.tx_gain = phy_tx_gain
82 self.ppm = phy_ppm
Piotr Krysikb8632ff2018-09-13 15:42:55 +020083 self.freq_offset = phy_freq_offset
Piotr Krysik902f4eb2017-09-19 08:04:33 +020084
Piotr Krysik1cc264f2018-09-06 19:57:57 +020085 gr.top_block.__init__(self, "GR-GSM TRX")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020086
Piotr Krysik1cc264f2018-09-06 19:57:57 +020087 # TRX Burst Interface
88 self.trx_burst_if = grgsm.trx_burst_if(
89 trx_bind_addr, trx_remote_addr,
90 str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020091
Piotr Krysik1cc264f2018-09-06 19:57:57 +020092 # RX path definition
93 self.phy_src = uhd.usrp_source(phy_args,
94 uhd.stream_args(cpu_format="fc32",
95 channels=range(1)))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070096
Piotr Krysik1cc264f2018-09-06 19:57:57 +020097 self.phy_src.set_clock_rate(26e6, uhd.ALL_MBOARDS)
98 self.phy_src.set_center_freq(self.rx_freq, 0)
99 self.phy_src.set_antenna(phy_rx_antenna, 0)
100 self.phy_src.set_samp_rate(phy_sample_rate)
101 self.phy_src.set_bandwidth(650e3, 0)
102 self.phy_src.set_gain(phy_rx_gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200103
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200104 self.msg_to_tag_src = grgsm.msg_to_tag()
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200105
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200106 self.rotator_src = grgsm.controlled_rotator_cc(
107 self.calc_phase_inc(self.rx_freq))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200108
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200109 self.lpf = filter.fir_filter_ccf(1, firdes.low_pass(
110 1, phy_sample_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700111
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200112 self.gsm_receiver = grgsm.receiver(self.osr, ([0]), ([]))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200113
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200114 self.ts_filter = grgsm.burst_timeslot_filter(0)
115 self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700116
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200117 # Connections
118 self.connect(
119 (self.phy_src, 0),
120 (self.msg_to_tag_src, 0))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200121
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200122 self.connect(
123 (self.msg_to_tag_src, 0),
124 (self.rotator_src, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700125
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200126 self.connect(
127 (self.rotator_src, 0),
128 (self.lpf, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700129
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200130 self.connect(
131 (self.lpf, 0),
132 (self.gsm_receiver, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700133
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200134 self.msg_connect(
135 (self.gsm_receiver, 'C0'),
136 (self.ts_filter, 'in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700137
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200138 self.msg_connect(
139 (self.ts_filter, 'out'),
140 (self.trx_burst_if, 'bursts'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700141
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700142
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200143 # TX Path Definition
144 self.phy_sink = uhd.usrp_sink(phy_args,
145 uhd.stream_args(cpu_format="fc32",
146 channels=range(1)), "packet_len")
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200147
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200148 self.phy_sink.set_clock_rate(26e6, uhd.ALL_MBOARDS)
149 self.phy_sink.set_antenna(phy_tx_antenna, 0)
150 self.phy_sink.set_samp_rate(phy_sample_rate)
151 self.phy_sink.set_center_freq(self.tx_freq, 0)
152 self.phy_sink.set_gain(self.tx_gain)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700153
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200154 self.tx_time_setter = grgsm.txtime_setter(
155 0xffffffff, 0, 0, 0, 0, 0,
156 self.delay_correction + self.GSM_UL_DL_SHIFT_uS * 1e-6)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700157
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200158 self.tx_burst_proc = grgsm.preprocess_tx_burst()
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700159
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200160 self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(
161 blocks.byte_t, 'packet_len')
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700162
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200163 self.gmsk_mod = grgsm.gsm_gmsk_mod(
164 BT = 0.3, pulse_duration = 4, sps = self.osr)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700165
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200166 self.burst_shaper = digital.burst_shaper_cc(
167 (firdes.window(firdes.WIN_HANN, 16, 0)),
168 0, 20, False, "packet_len")
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700169
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200170 self.msg_to_tag_sink = grgsm.msg_to_tag()
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700171
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200172 self.rotator_sink = grgsm.controlled_rotator_cc(
173 -self.calc_phase_inc(self.tx_freq))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700174
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200175 # Connections
176 self.msg_connect(
177 (self.trx_burst_if, 'bursts'),
178 (self.tx_time_setter, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700179
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200180 self.msg_connect(
181 (self.tx_time_setter, 'bursts_out'),
182 (self.tx_burst_proc, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700183
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200184 self.msg_connect(
185 (self.tx_burst_proc, 'bursts_out'),
186 (self.pdu_to_tagged_stream, 'pdus'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700187
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200188 self.connect(
189 (self.pdu_to_tagged_stream, 0),
190 (self.gmsk_mod, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700191
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200192 self.connect(
193 (self.gmsk_mod, 0),
194 (self.burst_shaper, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700195
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200196 self.connect(
197 (self.burst_shaper, 0),
198 (self.msg_to_tag_sink, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700199
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200200 self.connect(
201 (self.msg_to_tag_sink, 0),
202 (self.rotator_sink, 0))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700203
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200204 self.connect(
205 (self.rotator_sink, 0),
206 (self.phy_sink, 0))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700207
208
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200209 # RX & TX synchronization
210 self.bt_filter = grgsm.burst_type_filter([3])
211 self.burst_to_fn_time = grgsm.burst_to_fn_time()
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700212
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200213 # Connections
214 self.msg_connect(
215 (self.gsm_receiver, 'C0'),
216 (self.bt_filter, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700217
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200218 self.msg_connect(
219 (self.bt_filter, 'bursts_out'),
220 (self.burst_to_fn_time, 'bursts_in'))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700221
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200222 self.msg_connect(
223 (self.burst_to_fn_time, 'fn_time_out'),
224 (self.tx_time_setter, 'fn_time'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200225
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700226
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200227 # AFC (Automatic Frequency Correction)
228 self.gsm_clck_ctrl = grgsm.clock_offset_control(
229 self.rx_freq, phy_sample_rate, osr = self.osr)
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700230
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200231 self.dict_toggle_sign = dict_toggle_sign()
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700232
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200233 # Connections
234 self.msg_connect(
235 (self.gsm_receiver, 'measurements'),
236 (self.gsm_clck_ctrl, 'measurements'))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700237
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200238 self.msg_connect(
239 (self.gsm_clck_ctrl, 'ctrl'),
240 (self.msg_to_tag_src, 'msg'))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700241
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200242 self.msg_connect(
243 (self.gsm_clck_ctrl, 'ctrl'),
244 (self.dict_toggle_sign, 'dict_in'))
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700245
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200246 self.msg_connect(
247 (self.dict_toggle_sign, 'dict_out'),
248 (self.msg_to_tag_sink, 'msg'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200249
Vadim Yanitskiy0017a352018-03-03 23:37:30 +0700250
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200251 # Some UHD devices (such as UmTRX) do start the clock
252 # not from 0, so it's required to reset it manually.
253 # Resetting UHD source will also affect the sink.
254 self.phy_src.set_time_now(uhd.time_spec(0.0))
Vadim Yanitskiy0017a352018-03-03 23:37:30 +0700255
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200256 def shutdown(self):
257 print("[i] Shutdown Radio interface")
258 self.stop()
259 self.wait()
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200260
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200261 def calc_phase_inc(self, fc):
262 return self.ppm / 1.0e6 * 2 * pi * fc / self.sample_rate
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700263
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200264 def set_rx_freq(self, fc):
Piotr Krysikb8632ff2018-09-13 15:42:55 +0200265 fc += self.freq_offset
Piotr Krysik0f3bceb2018-09-13 14:53:26 +0200266 print("[#] Shifting RX freq. to %s (offset is %s)"
267 % (eng_notation.num_to_str(fc),
Piotr Krysikb8632ff2018-09-13 15:42:55 +0200268 eng_notation.num_to_str(self.freq_offset)))
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200269 self.phy_src.set_center_freq(fc, 0)
270 self.rotator_src.set_phase_inc(self.calc_phase_inc(fc))
271 self.rx_freq = fc
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700272
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200273 def set_tx_freq(self, fc):
Piotr Krysikb8632ff2018-09-13 15:42:55 +0200274 fc += self.freq_offset
Piotr Krysik0f3bceb2018-09-13 14:53:26 +0200275 print("[#] Shifting TX freq. to %s (offset is %s)"
276 % (eng_notation.num_to_str(fc),
Piotr Krysikb8632ff2018-09-13 15:42:55 +0200277 eng_notation.num_to_str(self.freq_offset)))
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200278 self.phy_sink.set_center_freq(fc, 0)
279 self.rotator_sink.set_phase_inc(-self.calc_phase_inc(fc))
280 self.tx_freq = fc
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200281
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200282 def set_rx_gain(self, gain):
283 self.phy_src.set_gain(gain, 0)
284 self.rx_gain = gain
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700285
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200286 def set_tx_gain(self, gain):
287 self.phy_sink.set_gain(gain, 0)
288 self.tx_gain = gain
Vadim Yanitskiy34266e72017-12-05 01:01:43 +0700289
Piotr Krysik1cc264f2018-09-06 19:57:57 +0200290 def set_ta(self, ta):
291 print("[i] Setting TA value %d" % ta)
292 advance_time_sec = ta * self.GSM_SYM_PERIOD_uS * 1e-6
293 self.tx_time_setter.set_timing_advance(advance_time_sec)
Vadim Yanitskiy7da82f42019-01-19 09:28:23 +0700294
295 def measure(self, freq):
296 # HACK: generate a random low RSSI value
297 return random.randint(-120, -100)