blob: 119b237e87b313f0efc57e4e45d2c5b715e6c782 [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>
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
29import osmosdr
30
31from math import pi
32
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 Krysik902f4eb2017-09-19 08:04:33 +020062 # PHY specific variables
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070063 rx_freq = 935e6
64 tx_freq = 890e6
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070065 osr = 4
Piotr Krysik902f4eb2017-09-19 08:04:33 +020066
67 # Application state flags
68 trx_started = False
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070069
Vadim Yanitskiy9dded9b2017-12-05 01:00:51 +070070 # GSM timings (in microseconds [uS])
71 # One timeslot duration is 576.9 μs = 15/26 ms,
72 # or 156.25 symbol periods (a symbol period is 48/13 μs)
73 GSM_SYM_PERIOD_uS = 48.0 / 13.0
74 GSM_TS_PERIOD_uS = GSM_SYM_PERIOD_uS * 156.25
75 GSM_UL_DL_SHIFT_uS = -(GSM_TS_PERIOD_uS * 3)
76
77 # FIXME: shall be measured (automatically?) for
78 # particular device and particular clock rate.
79 # The current value is measured for USRP B2X0 at 26e6.
80 delay_correction = (285.616 + 2 * GSM_SYM_PERIOD_uS) * 1e-6
Piotr Krysik902f4eb2017-09-19 08:04:33 +020081
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070082 def __init__(self, phy_args, phy_sample_rate,
83 phy_rx_gain, phy_tx_gain, phy_ppm,
84 phy_rx_antenna, phy_tx_antenna,
Vadim Yanitskiy5d68aa52017-10-17 11:14:48 +070085 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020086
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070087 print("[i] Init Radio interface")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020088
89 # PHY specific variables
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070090 self.sample_rate = phy_sample_rate
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070091 self.rx_gain = phy_rx_gain
92 self.tx_gain = phy_tx_gain
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070093 self.ppm = phy_ppm
Piotr Krysik902f4eb2017-09-19 08:04:33 +020094
95 gr.top_block.__init__(self, "GR-GSM TRX")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020096
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070097 # TRX Burst Interface
98 self.trx_burst_if = grgsm.trx_burst_if(
99 trx_remote_addr, str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200100
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200101
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700102 # RX path definition
103 self.phy_src = uhd.usrp_source(phy_args,
104 uhd.stream_args(cpu_format="fc32",
105 channels=range(1)))
106
Vadim Yanitskiy63703bb2017-12-03 23:40:21 +0700107 self.phy_src.set_clock_rate(26e6, uhd.ALL_MBOARDS)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700108 self.phy_src.set_center_freq(self.rx_freq, 0)
109 self.phy_src.set_antenna(phy_rx_antenna, 0)
110 self.phy_src.set_samp_rate(phy_sample_rate)
111 self.phy_src.set_bandwidth(650e3, 0)
112 self.phy_src.set_gain(phy_rx_gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200113
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700114 self.msg_to_tag_src = grgsm.msg_to_tag()
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200115
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700116 self.rotator_src = grgsm.controlled_rotator_cc(
117 self.calc_phase_inc(self.rx_freq))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200118
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700119 self.lpf = filter.fir_filter_ccf(1, firdes.low_pass(
120 1, phy_sample_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76))
121
122 self.gsm_receiver = grgsm.receiver(self.osr, ([0]), ([]))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200123
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700124 self.ts_filter = grgsm.burst_timeslot_filter(0)
125 self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700126
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200127 # Connections
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700128 self.connect(
129 (self.phy_src, 0),
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700130 (self.msg_to_tag_src, 0))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200131
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700132 self.connect(
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700133 (self.msg_to_tag_src, 0),
134 (self.rotator_src, 0))
135
136 self.connect(
137 (self.rotator_src, 0),
138 (self.lpf, 0))
139
140 self.connect(
141 (self.lpf, 0),
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700142 (self.gsm_receiver, 0))
143
144 self.msg_connect(
145 (self.gsm_receiver, 'C0'),
146 (self.ts_filter, 'in'))
147
148 self.msg_connect(
149 (self.ts_filter, 'out'),
150 (self.trx_burst_if, 'bursts'))
151
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700152
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700153 # TX Path Definition
154 self.phy_sink = uhd.usrp_sink(phy_args,
155 uhd.stream_args(cpu_format="fc32",
156 channels=range(1)), "packet_len")
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200157
Vadim Yanitskiy63703bb2017-12-03 23:40:21 +0700158 self.phy_sink.set_clock_rate(26e6, uhd.ALL_MBOARDS)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700159 self.phy_sink.set_antenna(phy_tx_antenna, 0)
160 self.phy_sink.set_samp_rate(phy_sample_rate)
161 self.phy_sink.set_center_freq(self.tx_freq, 0)
162 self.phy_sink.set_gain(self.tx_gain)
163
164 self.tx_time_setter = grgsm.txtime_setter(
165 0xffffffff, 0, 0, 0, 0, 0,
Vadim Yanitskiy9dded9b2017-12-05 01:00:51 +0700166 self.delay_correction + self.GSM_UL_DL_SHIFT_uS * 1e-6)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700167
168 self.tx_burst_proc = grgsm.preprocess_tx_burst()
169
170 self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(
171 blocks.byte_t, 'packet_len')
172
173 self.gmsk_mod = grgsm.gsm_gmsk_mod(
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700174 BT = 0.3, pulse_duration = 4, sps = self.osr)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700175
176 self.burst_shaper = digital.burst_shaper_cc(
177 (firdes.window(firdes.WIN_HANN, 16, 0)),
178 0, 20, False, "packet_len")
179
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700180 self.msg_to_tag_sink = grgsm.msg_to_tag()
181
182 self.rotator_sink = grgsm.controlled_rotator_cc(
183 -self.calc_phase_inc(self.tx_freq))
184
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700185 # Connections
186 self.msg_connect(
187 (self.trx_burst_if, 'bursts'),
188 (self.tx_time_setter, 'bursts_in'))
189
190 self.msg_connect(
191 (self.tx_time_setter, 'bursts_out'),
192 (self.tx_burst_proc, 'bursts_in'))
193
194 self.msg_connect(
195 (self.tx_burst_proc, 'bursts_out'),
196 (self.pdu_to_tagged_stream, 'pdus'))
197
198 self.connect(
199 (self.pdu_to_tagged_stream, 0),
200 (self.gmsk_mod, 0))
201
202 self.connect(
203 (self.gmsk_mod, 0),
204 (self.burst_shaper, 0))
205
206 self.connect(
207 (self.burst_shaper, 0),
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700208 (self.msg_to_tag_sink, 0))
209
210 self.connect(
211 (self.msg_to_tag_sink, 0),
212 (self.rotator_sink, 0))
213
214 self.connect(
215 (self.rotator_sink, 0),
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700216 (self.phy_sink, 0))
217
218
219 # RX & TX synchronization
Vadim Yanitskiy4650fad2017-12-01 05:08:35 +0700220 self.bt_filter = grgsm.burst_type_filter([3])
Vadim Yanitskiy5cba7042017-12-04 01:51:00 +0700221 self.burst_to_fn_time = grgsm.burst_to_fn_time()
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700222
223 # Connections
224 self.msg_connect(
225 (self.gsm_receiver, 'C0'),
226 (self.bt_filter, 'bursts_in'))
227
228 self.msg_connect(
229 (self.bt_filter, 'bursts_out'),
230 (self.burst_to_fn_time, 'bursts_in'))
231
232 self.msg_connect(
233 (self.burst_to_fn_time, 'fn_time_out'),
234 (self.tx_time_setter, 'fn_time'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200235
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700236
237 # AFC (Automatic Frequency Correction)
238 self.gsm_clck_ctrl = grgsm.clock_offset_control(
239 self.rx_freq, phy_sample_rate, osr = self.osr)
240
241 self.dict_toggle_sign = dict_toggle_sign()
242
243 # Connections
244 self.msg_connect(
245 (self.gsm_receiver, 'measurements'),
246 (self.gsm_clck_ctrl, 'measurements'))
247
248 self.msg_connect(
249 (self.gsm_clck_ctrl, 'ctrl'),
250 (self.msg_to_tag_src, 'msg'))
251
252 self.msg_connect(
253 (self.gsm_clck_ctrl, 'ctrl'),
254 (self.dict_toggle_sign, 'dict_in'))
255
256 self.msg_connect(
257 (self.dict_toggle_sign, 'dict_out'),
258 (self.msg_to_tag_sink, 'msg'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200259
Vadim Yanitskiy0017a352018-03-03 23:37:30 +0700260
261 # Some UHD devices (such as UmTRX) do start the clock
262 # not from 0, so it's required to reset it manually.
263 # Resetting UHD source will also affect the sink.
264 self.phy_src.set_time_now(uhd.time_spec(0.0))
265
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200266 def shutdown(self):
267 print("[i] Shutdown Radio interface")
268 self.stop()
269 self.wait()
270
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700271 def calc_phase_inc(self, fc):
272 return self.ppm / 1.0e6 * 2 * pi * fc / self.sample_rate
273
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700274 def set_rx_freq(self, fc):
275 self.phy_src.set_center_freq(fc, 0)
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700276 self.rotator_src.set_phase_inc(self.calc_phase_inc(fc))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700277 self.rx_freq = fc
278
279 def set_tx_freq(self, fc):
280 self.phy_sink.set_center_freq(fc, 0)
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700281 self.rotator_sink.set_phase_inc(-self.calc_phase_inc(fc))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700282 self.tx_freq = fc
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200283
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700284 def set_rx_gain(self, gain):
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700285 self.phy_src.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700286 self.rx_gain = gain
287
288 def set_tx_gain(self, gain):
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700289 self.phy_sink.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700290 self.tx_gain = gain
Vadim Yanitskiy34266e72017-12-05 01:01:43 +0700291
292 def set_ta(self, ta):
293 print("[i] Setting TA value %d" % ta)
294 advance_time_sec = ta * self.GSM_SYM_PERIOD_uS * 1e-6
295 self.tx_time_setter.set_timing_advance(advance_time_sec)