blob: e0678abfac26d0fca9722fe530fc39212ddb4dcd [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 import digital
Piotr Krysik902f4eb2017-09-19 08:04:33 +020033from gnuradio import blocks
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070034from gnuradio import uhd
Piotr Krysik902f4eb2017-09-19 08:04:33 +020035from gnuradio import gr
36
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070037from gnuradio import filter
38from gnuradio.filter import firdes
39
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070040
41# HACK: should be implemented in C++!
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070042class dict_toggle_sign(gr.basic_block):
43 def __init__(self): # only default arguments here
44 gr.basic_block.__init__(self,
45 name='Change sign of elts in dict',
46 in_sig=[],
47 out_sig=[]
48 )
49 self.message_port_register_in(pmt.intern("dict_in"))
50 self.message_port_register_out(pmt.intern("dict_out"))
51 self.set_msg_handler(pmt.intern("dict_in"), self.change_sign)
52
53 def change_sign(self, msg):
54 if pmt.is_dict(msg):
55 d = pmt.to_python(msg)
56 for key, value in d.items():
57 d[key] *= -1
58 self.message_port_pub(pmt.intern("dict_out"), pmt.to_pmt(d))
59
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070060class radio_if(gr.top_block):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020061 # PHY specific variables
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070062 rx_freq = 935e6
63 tx_freq = 890e6
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070064 osr = 4
Piotr Krysik902f4eb2017-09-19 08:04:33 +020065
66 # Application state flags
67 trx_started = False
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070068
Vadim Yanitskiy9dded9b2017-12-05 01:00:51 +070069 # GSM timings (in microseconds [uS])
70 # One timeslot duration is 576.9 μs = 15/26 ms,
71 # or 156.25 symbol periods (a symbol period is 48/13 μs)
72 GSM_SYM_PERIOD_uS = 48.0 / 13.0
73 GSM_TS_PERIOD_uS = GSM_SYM_PERIOD_uS * 156.25
74 GSM_UL_DL_SHIFT_uS = -(GSM_TS_PERIOD_uS * 3)
75
76 # FIXME: shall be measured (automatically?) for
77 # particular device and particular clock rate.
78 # The current value is measured for USRP B2X0 at 26e6.
79 delay_correction = (285.616 + 2 * GSM_SYM_PERIOD_uS) * 1e-6
Piotr Krysik902f4eb2017-09-19 08:04:33 +020080
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070081 def __init__(self, phy_args, phy_sample_rate,
82 phy_rx_gain, phy_tx_gain, phy_ppm,
83 phy_rx_antenna, phy_tx_antenna,
Vadim Yanitskiy5d68aa52017-10-17 11:14:48 +070084 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020085
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070086 print("[i] Init Radio interface")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020087
88 # PHY specific variables
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070089 self.sample_rate = phy_sample_rate
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070090 self.rx_gain = phy_rx_gain
91 self.tx_gain = phy_tx_gain
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +070092 self.ppm = phy_ppm
Piotr Krysik902f4eb2017-09-19 08:04:33 +020093
94 gr.top_block.__init__(self, "GR-GSM TRX")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020095
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +070096 # TRX Burst Interface
97 self.trx_burst_if = grgsm.trx_burst_if(
98 trx_remote_addr, str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020099
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200100
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700101 # RX path definition
102 self.phy_src = uhd.usrp_source(phy_args,
103 uhd.stream_args(cpu_format="fc32",
104 channels=range(1)))
105
Vadim Yanitskiy63703bb2017-12-03 23:40:21 +0700106 self.phy_src.set_clock_rate(26e6, uhd.ALL_MBOARDS)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700107 self.phy_src.set_center_freq(self.rx_freq, 0)
108 self.phy_src.set_antenna(phy_rx_antenna, 0)
109 self.phy_src.set_samp_rate(phy_sample_rate)
110 self.phy_src.set_bandwidth(650e3, 0)
111 self.phy_src.set_gain(phy_rx_gain)
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200112
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700113 self.msg_to_tag_src = grgsm.msg_to_tag()
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200114
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700115 self.rotator_src = grgsm.controlled_rotator_cc(
116 self.calc_phase_inc(self.rx_freq))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200117
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700118 self.lpf = filter.fir_filter_ccf(1, firdes.low_pass(
119 1, phy_sample_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76))
120
121 self.gsm_receiver = grgsm.receiver(self.osr, ([0]), ([]))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200122
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700123 self.ts_filter = grgsm.burst_timeslot_filter(0)
124 self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700125
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200126 # Connections
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700127 self.connect(
128 (self.phy_src, 0),
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700129 (self.msg_to_tag_src, 0))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200130
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700131 self.connect(
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700132 (self.msg_to_tag_src, 0),
133 (self.rotator_src, 0))
134
135 self.connect(
136 (self.rotator_src, 0),
137 (self.lpf, 0))
138
139 self.connect(
140 (self.lpf, 0),
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700141 (self.gsm_receiver, 0))
142
143 self.msg_connect(
144 (self.gsm_receiver, 'C0'),
145 (self.ts_filter, 'in'))
146
147 self.msg_connect(
148 (self.ts_filter, 'out'),
149 (self.trx_burst_if, 'bursts'))
150
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700151
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700152 # TX Path Definition
153 self.phy_sink = uhd.usrp_sink(phy_args,
154 uhd.stream_args(cpu_format="fc32",
155 channels=range(1)), "packet_len")
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200156
Vadim Yanitskiy63703bb2017-12-03 23:40:21 +0700157 self.phy_sink.set_clock_rate(26e6, uhd.ALL_MBOARDS)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700158 self.phy_sink.set_antenna(phy_tx_antenna, 0)
159 self.phy_sink.set_samp_rate(phy_sample_rate)
160 self.phy_sink.set_center_freq(self.tx_freq, 0)
161 self.phy_sink.set_gain(self.tx_gain)
162
163 self.tx_time_setter = grgsm.txtime_setter(
164 0xffffffff, 0, 0, 0, 0, 0,
Vadim Yanitskiy9dded9b2017-12-05 01:00:51 +0700165 self.delay_correction + self.GSM_UL_DL_SHIFT_uS * 1e-6)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700166
167 self.tx_burst_proc = grgsm.preprocess_tx_burst()
168
169 self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream(
170 blocks.byte_t, 'packet_len')
171
172 self.gmsk_mod = grgsm.gsm_gmsk_mod(
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700173 BT = 0.3, pulse_duration = 4, sps = self.osr)
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700174
175 self.burst_shaper = digital.burst_shaper_cc(
176 (firdes.window(firdes.WIN_HANN, 16, 0)),
177 0, 20, False, "packet_len")
178
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700179 self.msg_to_tag_sink = grgsm.msg_to_tag()
180
181 self.rotator_sink = grgsm.controlled_rotator_cc(
182 -self.calc_phase_inc(self.tx_freq))
183
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700184 # Connections
185 self.msg_connect(
186 (self.trx_burst_if, 'bursts'),
187 (self.tx_time_setter, 'bursts_in'))
188
189 self.msg_connect(
190 (self.tx_time_setter, 'bursts_out'),
191 (self.tx_burst_proc, 'bursts_in'))
192
193 self.msg_connect(
194 (self.tx_burst_proc, 'bursts_out'),
195 (self.pdu_to_tagged_stream, 'pdus'))
196
197 self.connect(
198 (self.pdu_to_tagged_stream, 0),
199 (self.gmsk_mod, 0))
200
201 self.connect(
202 (self.gmsk_mod, 0),
203 (self.burst_shaper, 0))
204
205 self.connect(
206 (self.burst_shaper, 0),
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700207 (self.msg_to_tag_sink, 0))
208
209 self.connect(
210 (self.msg_to_tag_sink, 0),
211 (self.rotator_sink, 0))
212
213 self.connect(
214 (self.rotator_sink, 0),
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700215 (self.phy_sink, 0))
216
217
218 # RX & TX synchronization
Vadim Yanitskiy4650fad2017-12-01 05:08:35 +0700219 self.bt_filter = grgsm.burst_type_filter([3])
Vadim Yanitskiy5cba7042017-12-04 01:51:00 +0700220 self.burst_to_fn_time = grgsm.burst_to_fn_time()
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700221
222 # Connections
223 self.msg_connect(
224 (self.gsm_receiver, 'C0'),
225 (self.bt_filter, 'bursts_in'))
226
227 self.msg_connect(
228 (self.bt_filter, 'bursts_out'),
229 (self.burst_to_fn_time, 'bursts_in'))
230
231 self.msg_connect(
232 (self.burst_to_fn_time, 'fn_time_out'),
233 (self.tx_time_setter, 'fn_time'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200234
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700235
236 # AFC (Automatic Frequency Correction)
237 self.gsm_clck_ctrl = grgsm.clock_offset_control(
238 self.rx_freq, phy_sample_rate, osr = self.osr)
239
240 self.dict_toggle_sign = dict_toggle_sign()
241
242 # Connections
243 self.msg_connect(
244 (self.gsm_receiver, 'measurements'),
245 (self.gsm_clck_ctrl, 'measurements'))
246
247 self.msg_connect(
248 (self.gsm_clck_ctrl, 'ctrl'),
249 (self.msg_to_tag_src, 'msg'))
250
251 self.msg_connect(
252 (self.gsm_clck_ctrl, 'ctrl'),
253 (self.dict_toggle_sign, 'dict_in'))
254
255 self.msg_connect(
256 (self.dict_toggle_sign, 'dict_out'),
257 (self.msg_to_tag_sink, 'msg'))
258
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200259 def shutdown(self):
260 print("[i] Shutdown Radio interface")
261 self.stop()
262 self.wait()
263
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700264 def calc_phase_inc(self, fc):
265 return self.ppm / 1.0e6 * 2 * pi * fc / self.sample_rate
266
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700267 def set_rx_freq(self, fc):
268 self.phy_src.set_center_freq(fc, 0)
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700269 self.rotator_src.set_phase_inc(self.calc_phase_inc(fc))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700270 self.rx_freq = fc
271
272 def set_tx_freq(self, fc):
273 self.phy_sink.set_center_freq(fc, 0)
Vadim Yanitskiyd222ee52017-12-03 23:49:09 +0700274 self.rotator_sink.set_phase_inc(-self.calc_phase_inc(fc))
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700275 self.tx_freq = fc
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200276
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700277 def set_rx_gain(self, gain):
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700278 self.phy_src.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700279 self.rx_gain = gain
280
281 def set_tx_gain(self, gain):
Vadim Yanitskiy89aa4692017-11-14 00:15:20 +0700282 self.phy_sink.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700283 self.tx_gain = gain