blob: 41eb3e8167132d51912e50a75bdac1d55a4e6fb4 [file] [log] [blame]
Vadim Yanitskiy89fc14b2017-06-16 21:00:29 +07001#!/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.eng_option import eng_option
33from gnuradio import eng_notation
34from gnuradio.filter import firdes
35from gnuradio import blocks
36from gnuradio import gr
37
38class RadioInterface(gr.top_block):
39 # PHY specific variables
40 samp_rate = 2000000
41 shiftoff = 400e3
42 subdev_spec = "" # TODO: use it
43 device_args = ""
44 fc = 941.6e6 # TODO: set ARFCN to 0?
45 gain = 30
46 ppm = 0
47
48 # Application state flags
49 trx_started = False
50 fc_set = False
51
52 def __init__(self, phy_args, phy_subdev_spec,
53 phy_sample_rate, phy_gain, phy_ppm,
54 trx_remote_addr, trx_base_port):
55 print("[i] Init Radio interface")
56
57 # TRX block specific variables
58 self.trx_remote_addr = trx_remote_addr
59 self.trx_base_port = trx_base_port
60
61 # PHY specific variables
62 self.subdev_spec = phy_subdev_spec
63 self.samp_rate = phy_sample_rate
64 self.device_args = phy_args
65 self.gain = phy_gain
66 self.ppm = phy_ppm
67
68 gr.top_block.__init__(self, "GR-GSM TRX")
69 shift_fc = self.fc - self.shiftoff
70
71 ##################################################
72 # PHY Definition
73 ##################################################
74 self.phy = osmosdr.source(
75 args = "numchan=%d %s" % (1, self.device_args))
76
77 self.phy.set_bandwidth(250e3 + abs(self.shiftoff), 0)
78 self.phy.set_center_freq(shift_fc, 0)
79 self.phy.set_sample_rate(self.samp_rate)
80 self.phy.set_freq_corr(self.ppm, 0)
81 self.phy.set_iq_balance_mode(2, 0)
82 self.phy.set_dc_offset_mode(2, 0)
83 self.phy.set_gain_mode(False, 0)
84 self.phy.set_gain(self.gain, 0)
85 self.phy.set_if_gain(20, 0)
86 self.phy.set_bb_gain(20, 0)
87 self.phy.set_antenna("", 0)
88
89 ##################################################
90 # GR-GSM Magic
91 ##################################################
92 self.blocks_rotator = blocks.rotator_cc(
93 -2 * pi * self.shiftoff / self.samp_rate)
94
95 self.gsm_input = grgsm.gsm_input(
96 ppm = self.ppm, osr = 4, fc = self.fc,
97 samp_rate_in = self.samp_rate)
98
99 self.gsm_receiver = grgsm.receiver(4, ([0]), ([]))
100
101 self.gsm_clck_ctrl = grgsm.clock_offset_control(
102 shift_fc, self.samp_rate, osr = 4)
103
104 # TODO: implement configurable TS filter
105 self.gsm_ts_filter = grgsm.burst_timeslot_filter(0)
106
107 self.gsm_trx_if = grgsm.trx(self.trx_remote_addr,
108 str(self.trx_base_port))
109
110 ##################################################
111 # Connections
112 ##################################################
113 self.connect((self.phy, 0), (self.blocks_rotator, 0))
114 self.connect((self.blocks_rotator, 0), (self.gsm_input, 0))
115 self.connect((self.gsm_input, 0), (self.gsm_receiver, 0))
116
117 self.msg_connect((self.gsm_receiver, 'measurements'),
118 (self.gsm_clck_ctrl, 'measurements'))
119
120 self.msg_connect((self.gsm_clck_ctrl, 'ctrl'),
121 (self.gsm_input, 'ctrl_in'))
122
123 self.msg_connect((self.gsm_receiver, 'C0'),
124 (self.gsm_ts_filter, 'in'))
125
126 self.msg_connect((self.gsm_ts_filter, 'out'),
127 (self.gsm_trx_if, 'bursts'))
128
129 def check_available(self):
130 return self.fc_set
131
132 def shutdown(self):
133 print("[i] Shutdown Radio interface")
134 self.stop()
135 self.wait()
136
137 def get_args(self):
138 return self.args
139
140 def set_args(self, args):
141 self.args = args
142
143 def get_fc(self):
144 return self.fc
145
146 def set_fc(self, fc):
147 self.phy.set_center_freq(fc - self.shiftoff, 0)
148 self.gsm_input.set_fc(fc)
149 self.fc_set = True
150 self.fc = fc
151
152 def get_gain(self):
153 return self.gain
154
155 def set_gain(self, gain):
156 self.phy.set_gain(gain, 0)
157 self.gain = gain
158
159 def get_ppm(self):
160 return self.ppm
161
162 def set_ppm(self, ppm):
163 self.rtlsdr_source_0.set_freq_corr(ppm, 0)
164 self.ppm = ppm
165
166 def get_samp_rate(self):
167 return self.samp_rate
168
169 def set_samp_rate(self, samp_rate):
170 self.blocks_rotator.set_phase_inc(
171 -2 * pi * self.shiftoff / samp_rate)
172 self.gsm_input.set_samp_rate_in(samp_rate)
173 self.phy.set_sample_rate(samp_rate)
174 self.samp_rate = samp_rate
175
176 def get_shiftoff(self):
177 return self.shiftoff
178
179 def set_shiftoff(self, shiftoff):
180 self.blocks_rotator.set_phase_inc(
181 -2 * pi * shiftoff / self.samp_rate)
182 self.phy.set_bandwidth(250e3 + abs(shiftoff), 0)
183 self.phy.set_center_freq(self.fc - shiftoff, 0)
184 self.shiftoff = shiftoff