Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 1 | #!/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 | |
| 25 | import pmt |
| 26 | import time |
| 27 | import grgsm |
| 28 | import osmosdr |
| 29 | |
| 30 | from math import pi |
| 31 | |
| 32 | from gnuradio import blocks |
| 33 | from gnuradio import gr |
| 34 | |
| 35 | class RadioInterface(gr.top_block): |
| 36 | # 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 | |
| 101 | self.gsm_trx_if = grgsm.trx(self.trx_remote_addr, |
| 102 | str(self.trx_base_port)) |
| 103 | |
| 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'), |
| 118 | (self.gsm_trx_if, 'bursts')) |
| 119 | |
| 120 | def check_available(self): |
| 121 | return self.fc_set |
| 122 | |
| 123 | def shutdown(self): |
| 124 | print("[i] Shutdown Radio interface") |
| 125 | self.stop() |
| 126 | self.wait() |
| 127 | |
| 128 | def get_args(self): |
| 129 | return self.args |
| 130 | |
| 131 | def set_args(self, args): |
| 132 | self.args = args |
| 133 | |
| 134 | def get_fc(self): |
| 135 | return self.fc |
| 136 | |
| 137 | def set_fc(self, fc): |
| 138 | self.phy.set_center_freq(fc - self.shiftoff, 0) |
| 139 | self.gsm_input.set_fc(fc) |
| 140 | self.fc_set = True |
| 141 | self.fc = fc |
| 142 | |
| 143 | def get_gain(self): |
| 144 | return self.gain |
| 145 | |
| 146 | def set_gain(self, gain): |
| 147 | self.phy.set_gain(gain, 0) |
| 148 | self.gain = gain |
| 149 | |
| 150 | def get_ppm(self): |
| 151 | return self.ppm |
| 152 | |
| 153 | def set_ppm(self, ppm): |
| 154 | self.rtlsdr_source_0.set_freq_corr(ppm, 0) |
| 155 | self.ppm = ppm |
| 156 | |
| 157 | def get_samp_rate(self): |
| 158 | return self.samp_rate |
| 159 | |
| 160 | def set_samp_rate(self, samp_rate): |
| 161 | self.blocks_rotator.set_phase_inc( |
| 162 | -2 * pi * self.shiftoff / samp_rate) |
| 163 | self.gsm_input.set_samp_rate_in(samp_rate) |
| 164 | self.phy.set_sample_rate(samp_rate) |
| 165 | self.samp_rate = samp_rate |
| 166 | |
| 167 | def get_shiftoff(self): |
| 168 | return self.shiftoff |
| 169 | |
| 170 | def set_shiftoff(self, shiftoff): |
| 171 | self.blocks_rotator.set_phase_inc( |
| 172 | -2 * pi * shiftoff / self.samp_rate) |
| 173 | self.phy.set_bandwidth(250e3 + abs(shiftoff), 0) |
| 174 | self.phy.set_center_freq(self.fc - shiftoff, 0) |
| 175 | self.shiftoff = shiftoff |