diff options
author | Piotr Krysik <ptrkrysik@gmail.com> | 2018-04-16 22:21:29 +0200 |
---|---|---|
committer | Piotr Krysik <ptrkrysik@gmail.com> | 2018-04-16 22:21:29 +0200 |
commit | 8a8d41a58320d5f30713a6e6362c86d5db6de1de (patch) | |
tree | d95b1badab52b83126d57d410dd8e92e7a255c8e /python | |
parent | 8b9b88d788cc5064c0f6d7aa60a14954aee59eab (diff) | |
parent | fe4db93e6d03e92a958664bfffcd0e2923f20e34 (diff) |
Merge branch 'ptrkrysik/trx' into development
Diffstat (limited to 'python')
-rw-r--r-- | python/CMakeLists.txt | 3 | ||||
-rw-r--r-- | python/__init__.py | 5 | ||||
-rw-r--r-- | python/misc_utils/CMakeLists.txt | 4 | ||||
-rw-r--r-- | python/misc_utils/burst_to_fn_time.py | 28 | ||||
-rw-r--r-- | python/misc_utils/fn_time.py | 48 | ||||
-rwxr-xr-x | python/qa_txtime_bursts_tagger.py | 106 | ||||
-rwxr-xr-x | python/qa_txtime_setter.py | 43 | ||||
-rw-r--r-- | python/transmitter/CMakeLists.txt | 24 | ||||
-rw-r--r-- | python/transmitter/gsm_gmsk_mod.py | 69 | ||||
-rw-r--r-- | python/transmitter/txtime_bursts_tagger.py | 97 | ||||
-rw-r--r-- | python/trx/CMakeLists.txt | 31 | ||||
-rw-r--r-- | python/trx/__init__.py | 29 | ||||
-rw-r--r-- | python/trx/change_sign_of_dict_elements.py | 30 | ||||
-rw-r--r-- | python/trx/ctrl_if.py | 82 | ||||
-rw-r--r-- | python/trx/ctrl_if_bb.py | 155 | ||||
-rw-r--r-- | python/trx/fake_pm.py | 53 | ||||
-rw-r--r-- | python/trx/radio_if.py | 59 | ||||
-rwxr-xr-x | python/trx/radio_if_grc.py | 268 | ||||
-rw-r--r-- | python/trx/udp_link.py | 56 |
19 files changed, 1168 insertions, 22 deletions
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 564ec3b..6f49e71 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -31,6 +31,8 @@ endif() add_subdirectory(misc_utils) add_subdirectory(receiver) add_subdirectory(demapping) +add_subdirectory(transmitter) +add_subdirectory(trx) GR_PYTHON_INSTALL( FILES @@ -58,3 +60,4 @@ GR_ADD_TEST(qa_tch_f_decoder ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa #GR_ADD_TEST(qa_msg_to_tag ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_msg_to_tag.py) #GR_ADD_TEST(qa_controlled_fractional_resampler_cc ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_controlled_fractional_resampler_cc.py) #GR_ADD_TEST(qa_uplink_downlink_splitter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_uplink_downlink_splitter.py) +GR_ADD_TEST(qa_txtime_setter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_txtime_setter.py) diff --git a/python/__init__.py b/python/__init__.py index c2c016b..826b45f 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -55,7 +55,10 @@ from gsm_input import gsm_input from gsm_bcch_ccch_demapper import gsm_bcch_ccch_demapper from gsm_bcch_ccch_sdcch4_demapper import gsm_bcch_ccch_sdcch4_demapper from gsm_sdcch8_demapper import gsm_sdcch8_demapper - +from gsm_gmsk_mod import gsm_gmsk_mod +from fn_time import * +from txtime_bursts_tagger import * +from burst_to_fn_time import * import arfcn diff --git a/python/misc_utils/CMakeLists.txt b/python/misc_utils/CMakeLists.txt index 1accda9..8c7c175 100644 --- a/python/misc_utils/CMakeLists.txt +++ b/python/misc_utils/CMakeLists.txt @@ -22,5 +22,7 @@ GR_PYTHON_INSTALL( arfcn.py clock_offset_corrector_tagged.py hier_block.py - fn_time.py DESTINATION ${GR_PYTHON_DIR}/grgsm + fn_time.py + burst_to_fn_time.py + DESTINATION ${GR_PYTHON_DIR}/grgsm ) diff --git a/python/misc_utils/burst_to_fn_time.py b/python/misc_utils/burst_to_fn_time.py new file mode 100644 index 0000000..e31cf92 --- /dev/null +++ b/python/misc_utils/burst_to_fn_time.py @@ -0,0 +1,28 @@ +""" +Embedded Python Blocks: + +Each this file is saved, GRC will instantiate the first class it finds to get +ports and parameters of your block. The arguments to __init__ will be the +parameters. All of them are required to have default values! +""" +import numpy as np +from gnuradio import gr +import pmt + +class burst_to_fn_time(gr.basic_block): + def __init__(self): # only default arguments here + gr.basic_block.__init__( + self, + name='Burst to fn_time', + in_sig=[], + out_sig=[] + ) + self.message_port_register_in(pmt.intern("bursts_in")) + self.message_port_register_out(pmt.intern("fn_time_out")) + self.set_msg_handler(pmt.intern("bursts_in"), self.convert) + + def convert(self, msg): + fn_time = pmt.dict_ref(pmt.car(msg),pmt.intern("fn_time"),pmt.PMT_NIL) + fn_time_msg = pmt.dict_add(pmt.make_dict(), pmt.intern("fn_time"), fn_time) + if pmt.to_python(fn_time) is not None: + self.message_port_pub(pmt.intern("fn_time_out"), fn_time_msg) diff --git a/python/misc_utils/fn_time.py b/python/misc_utils/fn_time.py index 71e93db..96314b8 100644 --- a/python/misc_utils/fn_time.py +++ b/python/misc_utils/fn_time.py @@ -22,6 +22,7 @@ # from math import floor, ceil from random import uniform +from grgsm import fn_time_delta_cpp __hyper_frame = 26*51*2048 __symb_rate = 13.0e6/48.0 @@ -37,14 +38,21 @@ __frame_period = 8*__ts_period def fnmod_delta(fn1, fn2): h2 = __hyper_frame/2 delta = (fn1%__hyper_frame)-(fn2%__hyper_frame) - + if delta >= h2: delta = delta - __hyper_frame elif delta < -h2: delta = delta + __hyper_frame return delta - + +def fn_time_diff_delta(fn_x, fn_ref, time_diff_hint=0): + frames_diff = int(round(time_diff_hint/__frame_period)) + fn_ref_hint = (fn_ref + frames_diff) + fn_delta = fnmod_delta(fn_x,fn_ref_hint)+frames_diff + + return fn_delta + #fn_time_delta computes difference between reference frame number and a second frame number. It also computes timestamp of the second frame number. The full list of parameters is following: #* fn_ref - reference frame number modulo __hyper_frame #* time_ref - precise timestamp of the first sample in the frame fn_ref @@ -52,33 +60,33 @@ def fnmod_delta(fn1, fn2): #* time_hint - coarse time for fn_x that is used as a hint to avoid ambiguities caused by modulo operation applied to frame numbers. The correct are values from range <time2_precise-__hiperframe/2, time2_precise+__hiperframe/2> (where time2_precise is exact time of the first sample in frame fn_x) #* ts_num - number of timeslot in the frame -def fn_time_delta(fn_ref, time_ref, fn_x, time_hint=None, ts_num=None): +def fn_time_delta(fn_ref, time_ref, fn_x, time_hint=None, ts_num=0, ts_ref=0): if time_hint is None: time_hint = time_ref - if ts_num is None: - ts_num = 0 - - time_diff = time_hint-time_ref - frames_diff = int(round(time_diff/__frame_period)) - fn_ref = (fn_ref + frames_diff) - fn_delta = fnmod_delta(fn_x,fn_ref)+frames_diff - time2_precise = fn_delta*__frame_period+time_ref+ts_num*__ts_period + time_diff_hint = time_hint-time_ref + fn_delta = fn_time_diff_delta(fn_x,fn_ref, time_diff_hint) + time_x_precise = fn_delta*__frame_period+time_ref+(ts_num-ts_ref)*__ts_period - return fn_delta, time2_precise + return fn_delta, time_x_precise if __name__ == "__main__": fn1 = 10000 - time1 = 10 - for fn2 in xrange(__hyper_frame/2+fn1-10,__hyper_frame/2*10+fn1+10,10): - time2 = time1 + (fn2-fn1)*__frame_period + ts_ref = 4 + time1 = 10.5 + for fn2 in xrange(__hyper_frame/2+fn1-10,__hyper_frame/2*10+fn1+100,10): + ts_x = int(uniform(0,8)) + time2 = time1 + (fn2-fn1)*__frame_period + (ts_x-ts_ref)*__ts_period error = uniform(-6200,6200) time2_err = time2+error - fn_delta, time2_precise = fn_time_delta(fn1, time1, fn2, time2_err) + fn_delta, time2_precise = fn_time_delta(fn1, time1, fn2, time2_err, ts_x, ts_ref) + time2_precise_cpp = fn_time_delta_cpp(fn1, (int(time1),time1-int(time1)), fn2, (int(time2_err),time2_err-int(time2_err)), ts_x, ts_ref) if fn_delta != fn2-fn1: - print "dupa:", fn2, error#, 'fn_delta:'+str(fn_delta), time2, error, frames_diff_h4, (time2-time1)/(__hyper_frame*__frame_period), time_diff_h4_prev, time_diff - time_diff = time2 - time2_precise - if time_diff > 0.1: - print "dupa" + print "bad fn:", fn2, error#, 'fn_delta:'+str(fn_delta), time2, error, frames_diff_h4, (time2-time1)/(__hyper_frame*__frame_period), time_diff_hint_h4_prev, time_diff_hint +# time_diff_hint = time2 - time2_precise + time_diff_hint = time2_precise_cpp[0]+time2_precise_cpp[1] - time2_precise + if abs(time_diff_hint) > 0.0001: + print "time2_precise_cpp",time2_precise_cpp," time2_precise",time2_precise," time_ref",time1," time_hint",time2_err + print "" diff --git a/python/qa_txtime_bursts_tagger.py b/python/qa_txtime_bursts_tagger.py new file mode 100755 index 0000000..bdbf1af --- /dev/null +++ b/python/qa_txtime_bursts_tagger.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @file +# @author Piotr Krysik <ptrkrysik@gmail.com> +# @section LICENSE +# +# Gr-gsm is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Gr-gsm is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with gr-gsm; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# +# + +from gnuradio import gr, gr_unittest +from gnuradio import blocks +from txtime_bursts_tagger import txtime_bursts_tagger +#from transmitter.txtime_bursts_tagger import txtime_bursts_tagger +from pmt import * + +def make_time_hint_msg(time_hint): + return cons( dict_add(make_dict(), intern("time_hint"), from_double(time_hint)),PMT_NIL) + +def make_fn_time_msg(fn_ref, time_ref): + return cons( dict_add(make_dict(), intern("fn_time"), cons(from_uint64(fn_ref), from_double(time_ref))),PMT_NIL) + +class qa_txtime_bursts_tagger (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block () + + def tearDown (self): + self.tb = None + + def test_001_t (self): + tb = self.tb + time_ref = 0 + fn_ref = 0 + dut = txtime_bursts_tagger(fn_ref, time_ref) + + framenumbers_input = [1259192, 1076346, 1076242, 235879, 1259218] + timeslots_input = [6, 3, 4, 3, 5] + bursts_input = [ + "0001100001000111100111101111100101000100101011000010011110011101001111101100010100111111100000110100011111101011101100100111110011000100010001010000", + "0001000101000000001001111110000110010110110111110111101000001101001111101100010100111111001110001001110101110001010001000111011010010001011011000000", + "0001001101101101000111001000101011001101001110110001001100111101001111101100010100111111111001001010011010011111010010010101011001001011011100110000", + "0000010010100000001001101010100001011100010001101100111111101101001111101100010100111111101101001110100010101110010110101111100010010000110010110000", + ] + + src = grgsm.burst_source(framenumbers_input, timeslots_input, bursts_input) + sink = grgsm.burst_sink() + + self.tb.msg_connect(src, "out", dut, "bursts") + self.tb.msg_connect(dut, "bursts", sink, "in") + + tb.start() + tb.wait() + print "Dupa" + print sink + + +# msg1 = make_msg(1,"lol1") +# msg2 = make_msg(1,"lol2") +# msg3 = make_msg(2,"lol1") +# msg4 = make_msg(2,"lol1") +# +# port = intern("msgs") + +# tb.msg_connect(g,"msgs",dbg,"store") +# #tb.msg_connect(g,"msgs",dbg,"print_pdu") + +# tb.start() + +# g.to_basic_block()._post(port, msg1) +# g.to_basic_block()._post(port, msg3) +# g.to_basic_block()._post(port, msg2) +# g.to_basic_block()._post(port, msg4) + + + +# while dbg.num_messages() < 4: +# time.sleep(0.1) + +# tb.stop() +# tb.wait() +# print dbg.get_message(0) +# print get_id(dbg.get_message(0)) +# +# self.assertEqual(get_id(dbg.get_message(0)),1) +# self.assertEqual(get_id(dbg.get_message(1)),1) +# self.assertEqual(get_id(dbg.get_message(2)),2) +# self.assertEqual(get_id(dbg.get_message(3)),2) + + + +if __name__ == '__main__': + gr_unittest.run(qa_txtime_bursts_tagger, "qa_txtime_bursts_tagger.xml") diff --git a/python/qa_txtime_setter.py b/python/qa_txtime_setter.py new file mode 100755 index 0000000..d5509a9 --- /dev/null +++ b/python/qa_txtime_setter.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @file +# @author Piotr Krysik <ptrkrysik@gmail.com> +# @section LICENSE +# +# Gr-gsm is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Gr-gsm is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with gr-gsm; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# +# + +from gnuradio import gr, gr_unittest +from gnuradio import blocks +import grgsm_swig as grgsm + +class qa_txtime_setter (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block () + + def tearDown (self): + self.tb = None + + def test_001_t (self): + # set up fg + self.tb.run () + # check data + + +if __name__ == '__main__': + gr_unittest.run(qa_txtime_setter, "qa_txtime_setter.xml") diff --git a/python/transmitter/CMakeLists.txt b/python/transmitter/CMakeLists.txt new file mode 100644 index 0000000..c32d6e1 --- /dev/null +++ b/python/transmitter/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright 2011,2012 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +GR_PYTHON_INSTALL( + FILES + txtime_bursts_tagger.py + gsm_gmsk_mod.py DESTINATION ${GR_PYTHON_DIR}/grgsm +) diff --git a/python/transmitter/gsm_gmsk_mod.py b/python/transmitter/gsm_gmsk_mod.py new file mode 100644 index 0000000..e8ecc7a --- /dev/null +++ b/python/transmitter/gsm_gmsk_mod.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +################################################## +# GNU Radio Python Flow Graph +# Title: GMSK Modulator for GSM +# Author: Piotr Krysik +# Description: GMSK Modulator for GSM +# Generated: Wed Sep 20 21:12:04 2017 +################################################## + +from gnuradio import blocks +from gnuradio import digital +from gnuradio import gr +from gnuradio.analog import cpm +from gnuradio.filter import firdes +import grgsm + +class gsm_gmsk_mod(grgsm.hier_block): + + def __init__(self, BT=4, pulse_duration=4, sps=4): + gr.hier_block2.__init__( + self, "GMSK Modulator for GSM", + gr.io_signature(1, 1, gr.sizeof_char*1), + gr.io_signature(1, 1, gr.sizeof_gr_complex*1), + ) + + ################################################## + # Parameters + ################################################## + self.BT = BT + self.pulse_duration = pulse_duration + self.sps = sps + + ################################################## + # Blocks + ################################################## + self.digital_gmskmod_bc_0 = digital.gmskmod_bc(sps, pulse_duration, BT) + self.digital_diff_decoder_bb_0 = digital.diff_decoder_bb(2) + self.digital_chunks_to_symbols_xx_0 = digital.chunks_to_symbols_bf(([1,-1]), 1) + self.blocks_tagged_stream_multiply_length_0 = blocks.tagged_stream_multiply_length(gr.sizeof_gr_complex*1, "packet_len", sps) + self.blocks_float_to_char_0 = blocks.float_to_char(1, 1) + + ################################################## + # Connections + ################################################## + self.connect((self.blocks_float_to_char_0, 0), (self.digital_gmskmod_bc_0, 0)) + self.connect((self.blocks_tagged_stream_multiply_length_0, 0), (self, 0)) + self.connect((self.digital_chunks_to_symbols_xx_0, 0), (self.blocks_float_to_char_0, 0)) + self.connect((self.digital_diff_decoder_bb_0, 0), (self.digital_chunks_to_symbols_xx_0, 0)) + self.connect((self.digital_gmskmod_bc_0, 0), (self.blocks_tagged_stream_multiply_length_0, 0)) + self.connect((self, 0), (self.digital_diff_decoder_bb_0, 0)) + + def get_BT(self): + return self.BT + + def set_BT(self, BT): + self.BT = BT + + def get_pulse_duration(self): + return self.pulse_duration + + def set_pulse_duration(self, pulse_duration): + self.pulse_duration = pulse_duration + + def get_sps(self): + return self.sps + + def set_sps(self, sps): + self.sps = sps + self.blocks_tagged_stream_multiply_length_0.set_scalar(self.sps) diff --git a/python/transmitter/txtime_bursts_tagger.py b/python/transmitter/txtime_bursts_tagger.py new file mode 100644 index 0000000..de42f65 --- /dev/null +++ b/python/transmitter/txtime_bursts_tagger.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @file +# @author Piotr Krysik <ptrkrysik@gmail.com> +# @section LICENSE +# +# Gr-gsm is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Gr-gsm is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with gr-gsm; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# +# + +from gnuradio import gr +from fn_time import fn_time_delta +import pmt +import numpy + +class txtime_bursts_tagger(gr.basic_block): + """ + A block that adds txtime metadata to a burst + """ + def __init__(self, init_fn=0, init_time=0, time_hint=None, timing_advance=0, delay_correction=0): + gr.basic_block.__init__(self, + name="txtime_bursts_tagger", + in_sig=[], + out_sig=[]) + self.set_fn_time_reference(init_fn, init_time) + if time_hint is not None: + self.set_time_hint(time_hint) + + self.timing_advance = timing_advance + self.delay_correction = delay_correction + + self.message_port_register_in(pmt.intern("fn_time")) + self.message_port_register_in(pmt.intern("bursts")) + self.message_port_register_out(pmt.intern("bursts")) + + self.set_msg_handler(pmt.intern("fn_time"), self.process_fn_time_reference) + self.set_msg_handler(pmt.intern("bursts"), self.process_txtime_of_burst) + + def process_fn_time_reference(self, msg): + time_hint = pmt.to_python(pmt.dict_ref(msg, pmt.intern("time_hint"), pmt.PMT_NIL)) + fn_time = pmt.to_python(pmt.dict_ref(msg, pmt.intern("fn_time"), pmt.PMT_NIL)) + + if time_hint is not None: + self.time_hint = time_hint + elif fn_time is not None: + self.fn_ref = fn_time[0][0] + self.ts = fn_time[0][1] + full = fn_time[1][0] + frac = fn_time[1][1] + + self.time_ref = full+frac + self.time_hint = self.time_ref + + def process_txtime_of_burst(self, msg): + burst_with_header = pmt.to_python(pmt.cdr(msg)) + fn = burst_with_header[11]+burst_with_header[10]*2**8+burst_with_header[9]*2**16+burst_with_header[8]*2**24 + ts_num = burst_with_header[3] + if self.fn_ref is not None: + fn_delta, txtime = fn_time_delta(self.fn_ref, self.time_ref, fn, self.time_hint, ts_num) + txtime_corrected = txtime - self.delay_correction + txtime_final = txtime_corrected - self.timing_advance + + txtime_secs = int(txtime_final) + txtime_fracs = txtime_final-int(txtime_final) + #print "txtime_secs",txtime_secs,"txtime_fracs",txtime_fracs + tags_dict = pmt.dict_add(pmt.make_dict(), pmt.intern("tx_time"), pmt.make_tuple(pmt.from_uint64(txtime_secs),pmt.from_double(txtime_fracs))) + tags_dict = pmt.dict_add(tags_dict, pmt.intern("fn"), pmt.from_uint64(fn)) + new_msg = pmt.cons(tags_dict, pmt.cdr(msg)) + self.message_port_pub(pmt.intern("bursts"), new_msg) + + def set_fn_time_reference(self, init_fn, init_time): + self.fn_ref = init_fn + self.time_ref = init_time + self.set_time_hint(init_time) + + def set_time_hint(self, time_hint): + self.time_hint = time_hint + + def set_delay_correction(delay_correction): + self.delay_correction = delay_correction + + def set_timing_advance(timing_advance): + self.timing_advance = timing_advance + diff --git a/python/trx/CMakeLists.txt b/python/trx/CMakeLists.txt new file mode 100644 index 0000000..c6e3010 --- /dev/null +++ b/python/trx/CMakeLists.txt @@ -0,0 +1,31 @@ +# Copyright 2011,2012 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +GR_PYTHON_INSTALL( + FILES + __init__.py + udp_link.py + ctrl_if.py + ctrl_if_bb.py + fake_pm.py + radio_if.py + radio_if_grc.py + change_sign_of_dict_elements.py + DESTINATION ${GR_PYTHON_DIR}/grgsm/trx +) diff --git a/python/trx/__init__.py b/python/trx/__init__.py new file mode 100644 index 0000000..13b5a52 --- /dev/null +++ b/python/trx/__init__.py @@ -0,0 +1,29 @@ +# +# Copyright 2008,2009 Free Software Foundation, Inc. +# +# This application is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This application is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +''' +This is a set of helper classes for the grgsm_trx application. +''' + +from udp_link import udp_link +from ctrl_if import ctrl_if +from ctrl_if_bb import ctrl_if_bb +from fake_pm import fake_pm +from radio_if_grc import radio_if_grc +from radio_if import radio_if +from change_sign_of_dict_elements import change_sign_of_dict_elements
\ No newline at end of file diff --git a/python/trx/change_sign_of_dict_elements.py b/python/trx/change_sign_of_dict_elements.py new file mode 100644 index 0000000..fd7c585 --- /dev/null +++ b/python/trx/change_sign_of_dict_elements.py @@ -0,0 +1,30 @@ +""" +Embedded Python Blocks: + +Each this file is saved, GRC will instantiate the first class it finds to get +ports and parameters of your block. The arguments to __init__ will be the +parameters. All of them are required to have default values! +""" + +from gnuradio import gr +from pmt import * + +class change_sign_of_dict_elements(gr.basic_block): + def __init__(self): # only default arguments here + gr.basic_block.__init__( + self, + name='Change sign of elts in dict', + in_sig=[], + out_sig=[] + ) + self.message_port_register_in(intern("dict_in")) + self.message_port_register_out(intern("dict_out")) + self.set_msg_handler(intern("dict_in"), self.change_sign) + + def change_sign(self, msg): + if is_dict(msg): + d = to_python(msg) + #print d + for key, value in d.items(): + d[key] *= -1 + self.message_port_pub(intern("dict_out"), to_pmt(d)) diff --git a/python/trx/ctrl_if.py b/python/trx/ctrl_if.py new file mode 100644 index 0000000..7dee0f8 --- /dev/null +++ b/python/trx/ctrl_if.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# CTRL interface implementation +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from grgsm.trx import udp_link + +class ctrl_if(udp_link): + def handle_rx(self, data): + if self.verify_req(data): + request = self.prepare_req(data) + rc = self.parse_cmd(request) + + if type(rc) is tuple: + self.send_response(request, rc[0], rc[1]) + else: + self.send_response(request, rc) + else: + print("[!] Wrong data on CTRL interface") + + def verify_req(self, data): + # Verify command signature + return data.startswith("CMD") + + def prepare_req(self, data): + # Strip signature, paddings and \0 + request = data[4:].strip().strip("\0") + # Split into a command and arguments + request = request.split(" ") + # Now we have something like ["TXTUNE", "941600"] + return request + + def verify_cmd(self, request, cmd, argc): + # Check if requested command matches + if request[0] != cmd: + return False + + # And has enough arguments + if len(request) - 1 != argc: + return False + + # Check if all arguments are numeric + for v in request[1:]: + if not v.isdigit(): + return False + + return True + + def send_response(self, request, response_code, params = None): + # Include status code, for example ["TXTUNE", "0", "941600"] + request.insert(1, str(response_code)) + + # Optionally append command specific parameters + if params is not None: + request += params + + # Add the response signature, and join back to string + response = "RSP " + " ".join(request) + "\0" + # Now we have something like "RSP TXTUNE 0 941600" + self.send(response) + + def parse_cmd(self, request): + raise NotImplementedError diff --git a/python/trx/ctrl_if_bb.py b/python/trx/ctrl_if_bb.py new file mode 100644 index 0000000..26ae49a --- /dev/null +++ b/python/trx/ctrl_if_bb.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# CTRL interface for OsmocomBB +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import grgsm +from ctrl_if import ctrl_if + +class ctrl_if_bb(ctrl_if): + def __init__(self, remote_addr, remote_port, bind_port, tb, pm): + print("[i] Init CTRL interface") + ctrl_if.__init__(self, remote_addr, remote_port, bind_port) + + # Set link to the follow graph (top block) + self.tb = tb + # Power measurement + self.pm = pm + + def shutdown(self): + print("[i] Shutdown CTRL interface") + ctrl_if.shutdown(self) + + def parse_cmd(self, request): + # Power control + if self.verify_cmd(request, "POWERON", 0): + print("[i] Recv POWERON CMD") + + # Ensure transceiver isn't working + if self.tb.trx_started: + print("[!] Transceiver already started") + return -1 + + print("[i] Starting transceiver...") + self.tb.trx_started = True + self.tb.start() + + return 0 + + elif self.verify_cmd(request, "POWEROFF", 0): + print("[i] Recv POWEROFF cmd") + + # TODO: flush all buffers between blocks + if self.tb.trx_started: + print("[i] Stopping transceiver...") + self.tb.trx_started = False + self.tb.stop() + self.tb.wait() + + return 0 + + # Gain control + elif self.verify_cmd(request, "SETRXGAIN", 1): + print("[i] Recv SETRXGAIN cmd") + + # TODO: check gain value + gain = int(request[1]) + self.tb.set_rx_gain(gain) + + return 0 + + elif self.verify_cmd(request, "SETTXGAIN", 1): + print("[i] Recv SETTXGAIN cmd") + + # TODO: check gain value + gain = int(request[1]) + self.tb.set_tx_gain(gain) + + return 0 + + # Tuning Control + elif self.verify_cmd(request, "RXTUNE", 1): + print("[i] Recv RXTUNE cmd") + + # TODO: check freq range + freq = int(request[1]) * 1000 + self.tb.set_rx_freq(freq) + + return 0 + + elif self.verify_cmd(request, "TXTUNE", 1): + print("[i] Recv TXTUNE cmd") + + # TODO: check freq range + freq = int(request[1]) * 1000 + self.tb.set_tx_freq(freq) + + return 0 + + # Timeslot management + elif self.verify_cmd(request, "SETSLOT", 2): + print("[i] Recv SETSLOT cmd") + + # Obtain TS index + tn = int(request[1]) + if tn not in range(0, 8): + print("[!] TS index should be in range: 0..7") + return -1 + + # Ignore timeslot type for now + config = int(request[2]) + print("[i] Configure timeslot filter to: %s" + % ("drop all" if config == 0 else "TS %d" % tn)) + + if config == 0: + # Value 0 means 'drop all' + self.tb.ts_filter.set_policy( + grgsm.FILTER_POLICY_DROP_ALL) + else: + self.tb.ts_filter.set_policy( + grgsm.FILTER_POLICY_DEFAULT) + self.tb.ts_filter.set_tn(tn) + + return 0 + + # Power measurement + elif self.verify_cmd(request, "MEASURE", 1): + print("[i] Recv MEASURE cmd") + + # TODO: check freq range + meas_freq = int(request[1]) * 1000 + + # HACK: send fake low power values + # until actual power measurement is implemented + meas_dbm = str(self.pm.measure(meas_freq)) + + return (0, [meas_dbm]) + + # Misc + elif self.verify_cmd(request, "ECHO", 0): + print("[i] Recv ECHO cmd") + return 0 + + # Wrong / unknown command + else: + print("[!] Wrong request on CTRL interface") + return -1 diff --git a/python/trx/fake_pm.py b/python/trx/fake_pm.py new file mode 100644 index 0000000..72cf771 --- /dev/null +++ b/python/trx/fake_pm.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# Virtual Um-interface (fake transceiver) +# Power measurement emulation for BB +# +# (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from random import randint + +class fake_pm: + # Freq. list for good power level + bts_list = [] + + def __init__(self, noise_min, noise_max, bts_min, bts_max): + # Save power level ranges + self.noise_min = noise_min + self.noise_max = noise_max + self.bts_min = bts_min + self.bts_max = bts_max + + def measure(self, bts): + if bts in self.bts_list: + return randint(self.bts_min, self.bts_max) + else: + return randint(self.noise_min, self.noise_max) + + def update_bts_list(self, new_list): + self.bts_list = new_list + + def add_bts_list(self, add_list): + self.bts_list += add_list + + def del_bts_list(self, del_list): + for item in del_list: + if item in self.bts_list: + self.bts_list.remove(item) diff --git a/python/trx/radio_if.py b/python/trx/radio_if.py new file mode 100644 index 0000000..9e97277 --- /dev/null +++ b/python/trx/radio_if.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# Extending a GRC flowgraph gr-gsm/examples/trx_radio_if/radio_if_grc.grc +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# (C) 2017 by Piotr Krysik <ptrkrysik@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from grgsm.trx import radio_if_grc + +class radio_if(radio_if_grc): + # Application state flags + trx_started = False + + def __init__(self, phy_args, phy_sample_rate, + phy_rx_gain, phy_tx_gain, phy_ppm, + phy_rx_antenna, phy_tx_antenna, + trx_remote_addr, trx_base_port, + phy_tx_freq=938900000, + phy_rx_freq=938900000-45e6, + delay_correction=285.616e-6, + uplink_shift=-(6.0/1625000*(156.25)*3), + timing_advance=0): + + print("[i] Init Radio interface") + + radio_if_grc.__init__(self, + samp_rate=phy_sample_rate, + tx_gain=phy_tx_gain, rx_gain=phy_rx_gain, + tx_freq=phy_tx_freq, rx_freq=phy_rx_freq, + osr=4, ppm=phy_ppm, + trx_base_port=str(trx_base_port), + trx_remote_addr=trx_remote_addr, + delay_correction=delay_correction, + uplink_shift=uplink_shift, + timing_advance=timing_advance) + + def shutdown(self): + print("[i] Shutdown Radio interface") + self.stop() + self.wait() + diff --git a/python/trx/radio_if_grc.py b/python/trx/radio_if_grc.py new file mode 100755 index 0000000..920b0c5 --- /dev/null +++ b/python/trx/radio_if_grc.py @@ -0,0 +1,268 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +################################################## +# GNU Radio Python Flow Graph +# Title: Trx radio interface +# Author: (C) Piotr Krysik 2017 +# Description: Alpha version of trx radio interface +# Generated: Fri Dec 1 10:49:25 2017 +################################################## + +from change_sign_of_dict_elements import change_sign_of_dict_elements +from gnuradio import blocks +from gnuradio import digital +from gnuradio import eng_notation +from gnuradio import filter +from gnuradio import gr +from gnuradio import uhd +from gnuradio.eng_option import eng_option +from gnuradio.filter import firdes +from grgsm import gsm_gmsk_mod +from optparse import OptionParser +import grgsm +import math +import time + + +class radio_if_grc(gr.top_block): + + def __init__(self, delay_correction=285.616e-6, osr=4, ppm=-0.799427, rx_freq=935e6+36*0.2e6, rx_gain=40, samp_rate=13e6/12.0, timing_advance=0, trx_base_port="5710", trx_remote_addr="127.0.0.1", tx_freq=935e6+36*0.2e6-45e6, tx_gain=40, uplink_shift=-(6.0/1625000*(156.25)*3)): + gr.top_block.__init__(self, "Trx radio interface") + + ################################################## + # Parameters + ################################################## + self.delay_correction = delay_correction + self.osr = osr + self.ppm = ppm + self.rx_freq = rx_freq + self.rx_gain = rx_gain + self.samp_rate = samp_rate + self.timing_advance = timing_advance + self.trx_base_port = trx_base_port + self.trx_remote_addr = trx_remote_addr + self.tx_freq = tx_freq + self.tx_gain = tx_gain + self.uplink_shift = uplink_shift + + ################################################## + # Blocks + ################################################## + self.uhd_usrp_source_0 = uhd.usrp_source( + ",".join(("", "")), + uhd.stream_args( + cpu_format="fc32", + channels=range(1), + ), + ) + self.uhd_usrp_source_0.set_clock_rate(26e6, uhd.ALL_MBOARDS) + self.uhd_usrp_source_0.set_samp_rate(samp_rate) + self.uhd_usrp_source_0.set_center_freq(rx_freq, 0) + self.uhd_usrp_source_0.set_gain(rx_gain, 0) + self.uhd_usrp_source_0.set_antenna("RX2", 0) + self.uhd_usrp_sink_0 = uhd.usrp_sink( + ",".join(("", "")), + uhd.stream_args( + cpu_format="fc32", + channels=range(1), + ), + "packet_len", + ) + self.uhd_usrp_sink_0.set_clock_rate(26e6, uhd.ALL_MBOARDS) + self.uhd_usrp_sink_0.set_subdev_spec("A:B", 0) + self.uhd_usrp_sink_0.set_samp_rate(samp_rate) + self.uhd_usrp_sink_0.set_center_freq(tx_freq, 0) + self.uhd_usrp_sink_0.set_gain(tx_gain, 0) + self.uhd_usrp_sink_0.set_antenna("TX/RX", 0) + self.ts_filter = grgsm.burst_timeslot_filter(0) + self.low_pass_filter_0_0 = filter.fir_filter_ccf(1, firdes.low_pass( + 1, samp_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) + self.gsm_txtime_setter_0 = grgsm.txtime_setter(None if (None is not None) else 0xffffffff, 0, 0, 0, 0, timing_advance, delay_correction+uplink_shift) + self.gsm_trx_burst_if_0 = grgsm.trx_burst_if(trx_remote_addr, trx_base_port) + self.gsm_receiver_0 = grgsm.receiver(4, ([0]), ([4]), False) + self.gsm_preprocess_tx_burst_0 = grgsm.preprocess_tx_burst() + self.gsm_msg_to_tag_0_0 = grgsm.msg_to_tag() + self.gsm_msg_to_tag_0 = grgsm.msg_to_tag() + self.gsm_gmsk_mod_0 = gsm_gmsk_mod( + BT=0.3, + pulse_duration=4, + sps=osr, + ) + self.gsm_controlled_rotator_cc_0_0 = grgsm.controlled_rotator_cc(-ppm/1.0e6*2*math.pi*tx_freq/samp_rate) + self.gsm_controlled_rotator_cc_0 = grgsm.controlled_rotator_cc(ppm/1.0e6*2*math.pi*rx_freq/samp_rate) + self.gsm_clock_offset_control_0 = grgsm.clock_offset_control(rx_freq, samp_rate, osr) + self.gsm_burst_type_filter_0 = grgsm.burst_type_filter(([3])) + self.gsm_burst_to_fn_time_0 = grgsm.burst_to_fn_time() + self.digital_burst_shaper_xx_0 = digital.burst_shaper_cc((firdes.window(firdes.WIN_HANN, 16, 0)), 0, 20, False, "packet_len") + self.change_sign_of_dict_elements = change_sign_of_dict_elements() + self.blocks_pdu_to_tagged_stream_0_0 = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") + + ################################################## + # Connections + ################################################## + self.msg_connect((self.change_sign_of_dict_elements, 'dict_out'), (self.gsm_msg_to_tag_0_0, 'msg')) + self.msg_connect((self.gsm_burst_to_fn_time_0, 'fn_time_out'), (self.gsm_txtime_setter_0, 'fn_time')) + self.msg_connect((self.gsm_burst_type_filter_0, 'bursts_out'), (self.gsm_burst_to_fn_time_0, 'bursts_in')) + self.msg_connect((self.gsm_clock_offset_control_0, 'ctrl'), (self.change_sign_of_dict_elements, 'dict_in')) + self.msg_connect((self.gsm_clock_offset_control_0, 'ctrl'), (self.gsm_msg_to_tag_0, 'msg')) + self.msg_connect((self.gsm_preprocess_tx_burst_0, 'bursts_out'), (self.blocks_pdu_to_tagged_stream_0_0, 'pdus')) + self.msg_connect((self.gsm_receiver_0, 'C0'), (self.gsm_burst_type_filter_0, 'bursts_in')) + self.msg_connect((self.gsm_receiver_0, 'measurements'), (self.gsm_clock_offset_control_0, 'measurements')) + self.msg_connect((self.gsm_receiver_0, 'C0'), (self.ts_filter, 'in')) + self.msg_connect((self.gsm_trx_burst_if_0, 'bursts'), (self.gsm_txtime_setter_0, 'bursts_in')) + self.msg_connect((self.gsm_txtime_setter_0, 'bursts_out'), (self.gsm_preprocess_tx_burst_0, 'bursts_in')) + self.msg_connect((self.ts_filter, 'out'), (self.gsm_trx_burst_if_0, 'bursts')) + self.connect((self.blocks_pdu_to_tagged_stream_0_0, 0), (self.gsm_gmsk_mod_0, 0)) + self.connect((self.digital_burst_shaper_xx_0, 0), (self.gsm_msg_to_tag_0_0, 0)) + self.connect((self.gsm_controlled_rotator_cc_0, 0), (self.low_pass_filter_0_0, 0)) + self.connect((self.gsm_controlled_rotator_cc_0_0, 0), (self.uhd_usrp_sink_0, 0)) + self.connect((self.gsm_gmsk_mod_0, 0), (self.digital_burst_shaper_xx_0, 0)) + self.connect((self.gsm_msg_to_tag_0, 0), (self.gsm_controlled_rotator_cc_0, 0)) + self.connect((self.gsm_msg_to_tag_0_0, 0), (self.gsm_controlled_rotator_cc_0_0, 0)) + self.connect((self.low_pass_filter_0_0, 0), (self.gsm_receiver_0, 0)) + self.connect((self.uhd_usrp_source_0, 0), (self.gsm_msg_to_tag_0, 0)) + + def get_delay_correction(self): + return self.delay_correction + + def set_delay_correction(self, delay_correction): + self.delay_correction = delay_correction + self.gsm_txtime_setter_0.set_delay_correction(self.delay_correction+self.uplink_shift) + + def get_osr(self): + return self.osr + + def set_osr(self, osr): + self.osr = osr + self.gsm_gmsk_mod_0.set_sps(self.osr) + + def get_ppm(self): + return self.ppm + + def set_ppm(self, ppm): + self.ppm = ppm + self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) + self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) + + def get_rx_freq(self): + return self.rx_freq + + def set_rx_freq(self, rx_freq): + self.rx_freq = rx_freq + self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) + self.uhd_usrp_source_0.set_center_freq(self.rx_freq, 0) + + def get_rx_gain(self): + return self.rx_gain + + def set_rx_gain(self, rx_gain): + self.rx_gain = rx_gain + self.uhd_usrp_source_0.set_gain(self.rx_gain, 0) + + + def get_samp_rate(self): + return self.samp_rate + + def set_samp_rate(self, samp_rate): + self.samp_rate = samp_rate + self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) + self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) + self.low_pass_filter_0_0.set_taps(firdes.low_pass(1, self.samp_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) + self.uhd_usrp_sink_0.set_samp_rate(self.samp_rate) + self.uhd_usrp_source_0.set_samp_rate(self.samp_rate) + + def get_timing_advance(self): + return self.timing_advance + + def set_timing_advance(self, timing_advance): + self.timing_advance = timing_advance + self.gsm_txtime_setter_0.set_timing_advance(self.timing_advance) + + def get_trx_base_port(self): + return self.trx_base_port + + def set_trx_base_port(self, trx_base_port): + self.trx_base_port = trx_base_port + + def get_trx_remote_addr(self): + return self.trx_remote_addr + + def set_trx_remote_addr(self, trx_remote_addr): + self.trx_remote_addr = trx_remote_addr + + def get_tx_freq(self): + return self.tx_freq + + def set_tx_freq(self, tx_freq): + self.tx_freq = tx_freq + self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) + self.uhd_usrp_sink_0.set_center_freq(self.tx_freq, 0) + + def get_tx_gain(self): + return self.tx_gain + + def set_tx_gain(self, tx_gain): + self.tx_gain = tx_gain + self.uhd_usrp_sink_0.set_gain(self.tx_gain, 0) + + + def get_uplink_shift(self): + return self.uplink_shift + + def set_uplink_shift(self, uplink_shift): + self.uplink_shift = uplink_shift + self.gsm_txtime_setter_0.set_delay_correction(self.delay_correction+self.uplink_shift) + + +def argument_parser(): + parser = OptionParser(option_class=eng_option, usage="%prog: [options]") + parser.add_option( + "", "--delay-correction", dest="delay_correction", type="eng_float", default=eng_notation.num_to_str(285.616e-6), + help="Set delay_correction [default=%default]") + parser.add_option( + "", "--osr", dest="osr", type="intx", default=4, + help="Set OSR [default=%default]") + parser.add_option( + "", "--ppm", dest="ppm", type="eng_float", default=eng_notation.num_to_str(-0.799427), + help="Set Clock offset correction [default=%default]") + parser.add_option( + "-d", "--rx-freq", dest="rx_freq", type="eng_float", default=eng_notation.num_to_str(935e6+36*0.2e6), + help="Set rx_freq [default=%default]") + parser.add_option( + "-g", "--rx-gain", dest="rx_gain", type="eng_float", default=eng_notation.num_to_str(40), + help="Set rx_gain [default=%default]") + parser.add_option( + "", "--samp-rate", dest="samp_rate", type="eng_float", default=eng_notation.num_to_str(13e6/12.0), + help="Set samp_rate [default=%default]") + parser.add_option( + "", "--timing-advance", dest="timing_advance", type="eng_float", default=eng_notation.num_to_str(0), + help="Set timing_advance [default=%default]") + parser.add_option( + "", "--trx-base-port", dest="trx_base_port", type="string", default="5710", + help="Set 5710 [default=%default]") + parser.add_option( + "", "--trx-remote-addr", dest="trx_remote_addr", type="string", default="127.0.0.1", + help="Set 127.0.0.1 [default=%default]") + parser.add_option( + "-u", "--tx-freq", dest="tx_freq", type="eng_float", default=eng_notation.num_to_str(935e6+36*0.2e6-45e6), + help="Set tx_freq [default=%default]") + parser.add_option( + "-r", "--tx-gain", dest="tx_gain", type="eng_float", default=eng_notation.num_to_str(40), + help="Set tx_gain [default=%default]") + parser.add_option( + "", "--uplink-shift", dest="uplink_shift", type="eng_float", default=eng_notation.num_to_str(-(6.0/1625000*(156.25)*3)), + help="Set uplink_shift [default=%default]") + return parser + + +def main(top_block_cls=radio_if_grc, options=None): + if options is None: + options, _ = argument_parser().parse_args() + + tb = top_block_cls(delay_correction=options.delay_correction, osr=options.osr, ppm=options.ppm, rx_freq=options.rx_freq, rx_gain=options.rx_gain, samp_rate=options.samp_rate, timing_advance=options.timing_advance, trx_base_port=options.trx_base_port, trx_remote_addr=options.trx_remote_addr, tx_freq=options.tx_freq, tx_gain=options.tx_gain, uplink_shift=options.uplink_shift) + tb.start() + tb.wait() + + +if __name__ == '__main__': + main() diff --git a/python/trx/udp_link.py b/python/trx/udp_link.py new file mode 100644 index 0000000..675ef5c --- /dev/null +++ b/python/trx/udp_link.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# UDP link implementation +# +# (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import socket +import select + +class udp_link: + def __init__(self, remote_addr, remote_port, bind_port): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.bind((remote_addr, bind_port)) + self.sock.setblocking(0) + + # Save remote info + self.remote_addr = remote_addr + self.remote_port = remote_port + + def loop(self): + r_event, w_event, x_event = select.select([self.sock], [], []) + + # Check for incoming data + if self.sock in r_event: + data, addr = self.sock.recvfrom(128) + self.handle_rx(data.decode()) + + def shutdown(self): + self.sock.close(); + + def send(self, data): + if type(data) not in [bytearray, bytes]: + data = data.encode() + + self.sock.sendto(data, (self.remote_addr, self.remote_port)) + + def handle_rx(self, data): + raise NotImplementedError |