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