blob: 0164cde3f2ada20aedfae1d6312dc319fcf911fb [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
32from gnuradio import blocks
33from gnuradio import gr
34
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070035class radio_if(gr.top_block):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020036 # PHY specific variables
37 samp_rate = 2000000
38 shiftoff = 400e3
Piotr Krysik902f4eb2017-09-19 08:04:33 +020039 device_args = ""
40 fc = 941.6e6 # TODO: set ARFCN to 0?
41 gain = 30
42 ppm = 0
43
44 # Application state flags
45 trx_started = False
46 fc_set = False
47
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070048 def __init__(self, phy_args, phy_sample_rate,
49 phy_rx_gain, phy_tx_gain, phy_ppm,
50 phy_rx_antenna, phy_tx_antenna,
Vadim Yanitskiy5d68aa52017-10-17 11:14:48 +070051 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020052
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070053 print("[i] Init Radio interface")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020054
55 # PHY specific variables
Piotr Krysik902f4eb2017-09-19 08:04:33 +020056 self.samp_rate = phy_sample_rate
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070057 self.rx_gain = phy_rx_gain
58 self.tx_gain = phy_tx_gain
Piotr Krysik902f4eb2017-09-19 08:04:33 +020059 self.ppm = phy_ppm
60
61 gr.top_block.__init__(self, "GR-GSM TRX")
62 shift_fc = self.fc - self.shiftoff
63
64 ##################################################
65 # PHY Definition
66 ##################################################
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070067 self.phy = osmosdr.source(args = "numchan=%d %s" % (1, phy_args))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020068
69 self.phy.set_bandwidth(250e3 + abs(self.shiftoff), 0)
70 self.phy.set_center_freq(shift_fc, 0)
71 self.phy.set_sample_rate(self.samp_rate)
72 self.phy.set_freq_corr(self.ppm, 0)
73 self.phy.set_iq_balance_mode(2, 0)
74 self.phy.set_dc_offset_mode(2, 0)
75 self.phy.set_gain_mode(False, 0)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070076 self.phy.set_gain(self.rx_gain, 0)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020077 self.phy.set_if_gain(20, 0)
78 self.phy.set_bb_gain(20, 0)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070079 self.phy.set_antenna(phy_rx_antenna, 0)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020080
81 ##################################################
82 # GR-GSM Magic
83 ##################################################
84 self.blocks_rotator = blocks.rotator_cc(
85 -2 * pi * self.shiftoff / self.samp_rate)
86
87 self.gsm_input = grgsm.gsm_input(
88 ppm = self.ppm, osr = 4, fc = self.fc,
89 samp_rate_in = self.samp_rate)
90
91 self.gsm_receiver = grgsm.receiver(4, ([0]), ([]))
92
93 self.gsm_clck_ctrl = grgsm.clock_offset_control(
94 shift_fc, self.samp_rate, osr = 4)
95
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +070096 self.gsm_ts_filter = grgsm.burst_timeslot_filter(0)
97 self.gsm_ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
98
Vadim Yanitskiy1a88dec2017-10-17 09:00:47 +070099 self.gsm_trx_burst_if = grgsm.trx_burst_if(
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +0700100 trx_remote_addr, str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200101
102 ##################################################
103 # Connections
104 ##################################################
105 self.connect((self.phy, 0), (self.blocks_rotator, 0))
106 self.connect((self.blocks_rotator, 0), (self.gsm_input, 0))
107 self.connect((self.gsm_input, 0), (self.gsm_receiver, 0))
108
109 self.msg_connect((self.gsm_receiver, 'measurements'),
110 (self.gsm_clck_ctrl, 'measurements'))
111
112 self.msg_connect((self.gsm_clck_ctrl, 'ctrl'),
113 (self.gsm_input, 'ctrl_in'))
114
115 self.msg_connect((self.gsm_receiver, 'C0'),
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700116 (self.gsm_ts_filter, 'in'))
117
118 self.msg_connect((self.gsm_ts_filter, 'out'),
Vadim Yanitskiy1a88dec2017-10-17 09:00:47 +0700119 (self.gsm_trx_burst_if, 'bursts'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200120
121 def check_available(self):
122 return self.fc_set
123
124 def shutdown(self):
125 print("[i] Shutdown Radio interface")
126 self.stop()
127 self.wait()
128
129 def get_args(self):
130 return self.args
131
132 def set_args(self, args):
133 self.args = args
134
135 def get_fc(self):
136 return self.fc
137
138 def set_fc(self, fc):
139 self.phy.set_center_freq(fc - self.shiftoff, 0)
140 self.gsm_input.set_fc(fc)
141 self.fc_set = True
142 self.fc = fc
143
144 def get_gain(self):
145 return self.gain
146
147 def set_gain(self, gain):
148 self.phy.set_gain(gain, 0)
149 self.gain = gain
150
151 def get_ppm(self):
152 return self.ppm
153
154 def set_ppm(self, ppm):
155 self.rtlsdr_source_0.set_freq_corr(ppm, 0)
156 self.ppm = ppm
157
158 def get_samp_rate(self):
159 return self.samp_rate
160
161 def set_samp_rate(self, samp_rate):
162 self.blocks_rotator.set_phase_inc(
163 -2 * pi * self.shiftoff / samp_rate)
164 self.gsm_input.set_samp_rate_in(samp_rate)
165 self.phy.set_sample_rate(samp_rate)
166 self.samp_rate = samp_rate
167
168 def get_shiftoff(self):
169 return self.shiftoff
170
171 def set_shiftoff(self, shiftoff):
172 self.blocks_rotator.set_phase_inc(
173 -2 * pi * shiftoff / self.samp_rate)
174 self.phy.set_bandwidth(250e3 + abs(shiftoff), 0)
175 self.phy.set_center_freq(self.fc - shiftoff, 0)
176 self.shiftoff = shiftoff