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