blob: 952c8d1e09f98bd94a667d26a4f52a23a7df24c4 [file] [log] [blame]
Piotr Krysik2164b9b2017-09-13 09:33:27 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# @file
4# @author Piotr Krysik <ptrkrysik@gmail.com>
5# @section LICENSE
6#
7# Gr-gsm is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3, or (at your option)
10# any later version.
11#
12# Gr-gsm is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with gr-gsm; see the file COPYING. If not, write to
19# the Free Software Foundation, Inc., 51 Franklin Street,
20# Boston, MA 02110-1301, USA.
21#
22#
23
Piotr Krysik2164b9b2017-09-13 09:33:27 +020024from gnuradio import gr
Vasil Velichkov46c90be2019-09-02 04:06:41 +030025from .fn_time import fn_time_delta
Piotr Krysik27f00322017-09-19 08:05:28 +020026import pmt
27import numpy
Piotr Krysik2164b9b2017-09-13 09:33:27 +020028
29class txtime_bursts_tagger(gr.basic_block):
30 """
31 A block that adds txtime metadata to a burst
32 """
Piotr Krysik42b21862017-10-19 09:20:23 +020033 def __init__(self, init_fn=0, init_time=0, time_hint=None, timing_advance=0, delay_correction=0):
Piotr Krysik2164b9b2017-09-13 09:33:27 +020034 gr.basic_block.__init__(self,
35 name="txtime_bursts_tagger",
36 in_sig=[],
37 out_sig=[])
Piotr Krysik27f00322017-09-19 08:05:28 +020038 self.set_fn_time_reference(init_fn, init_time)
39 if time_hint is not None:
40 self.set_time_hint(time_hint)
41
Piotr Krysik42b21862017-10-19 09:20:23 +020042 self.timing_advance = timing_advance
43 self.delay_correction = delay_correction
44
Piotr Krysik2164b9b2017-09-13 09:33:27 +020045 self.message_port_register_in(pmt.intern("fn_time"))
46 self.message_port_register_in(pmt.intern("bursts"))
47 self.message_port_register_out(pmt.intern("bursts"))
Piotr Krysik27f00322017-09-19 08:05:28 +020048
49 self.set_msg_handler(pmt.intern("fn_time"), self.process_fn_time_reference)
50 self.set_msg_handler(pmt.intern("bursts"), self.process_txtime_of_burst)
Piotr Krysik2164b9b2017-09-13 09:33:27 +020051
Piotr Krysik27f00322017-09-19 08:05:28 +020052 def process_fn_time_reference(self, msg):
Piotr Krysik96f2ea72017-10-16 15:47:08 +020053 time_hint = pmt.to_python(pmt.dict_ref(msg, pmt.intern("time_hint"), pmt.PMT_NIL))
54 fn_time = pmt.to_python(pmt.dict_ref(msg, pmt.intern("fn_time"), pmt.PMT_NIL))
55
Piotr Krysik27f00322017-09-19 08:05:28 +020056 if time_hint is not None:
57 self.time_hint = time_hint
58 elif fn_time is not None:
Piotr Krysik96f2ea72017-10-16 15:47:08 +020059 self.fn_ref = fn_time[0][0]
60 self.ts = fn_time[0][1]
61 full = fn_time[1][0]
62 frac = fn_time[1][1]
63
64 self.time_ref = full+frac
Piotr Krysik27f00322017-09-19 08:05:28 +020065 self.time_hint = self.time_ref
Piotr Krysik2c7aa9f2017-09-27 22:14:32 +020066
Piotr Krysik27f00322017-09-19 08:05:28 +020067 def process_txtime_of_burst(self, msg):
68 burst_with_header = pmt.to_python(pmt.cdr(msg))
69 fn = burst_with_header[11]+burst_with_header[10]*2**8+burst_with_header[9]*2**16+burst_with_header[8]*2**24
70 ts_num = burst_with_header[3]
Piotr Krysik96f2ea72017-10-16 15:47:08 +020071 if self.fn_ref is not None:
72 fn_delta, txtime = fn_time_delta(self.fn_ref, self.time_ref, fn, self.time_hint, ts_num)
Piotr Krysik42b21862017-10-19 09:20:23 +020073 txtime_corrected = txtime - self.delay_correction
74 txtime_final = txtime_corrected - self.timing_advance
75
Piotr Krysik33fbc282017-10-31 05:54:31 +010076 txtime_secs = int(txtime_final)
77 txtime_fracs = txtime_final-int(txtime_final)
Piotr Krysik96f2ea72017-10-16 15:47:08 +020078 #print "txtime_secs",txtime_secs,"txtime_fracs",txtime_fracs
79 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)))
Piotr Krysik42b21862017-10-19 09:20:23 +020080 tags_dict = pmt.dict_add(tags_dict, pmt.intern("fn"), pmt.from_uint64(fn))
Piotr Krysik96f2ea72017-10-16 15:47:08 +020081 new_msg = pmt.cons(tags_dict, pmt.cdr(msg))
82 self.message_port_pub(pmt.intern("bursts"), new_msg)
Piotr Krysik2164b9b2017-09-13 09:33:27 +020083
Piotr Krysik27f00322017-09-19 08:05:28 +020084 def set_fn_time_reference(self, init_fn, init_time):
85 self.fn_ref = init_fn
86 self.time_ref = init_time
87 self.set_time_hint(init_time)
88
89 def set_time_hint(self, time_hint):
90 self.time_hint = time_hint
91
Piotr Krysik42b21862017-10-19 09:20:23 +020092 def set_delay_correction(delay_correction):
93 self.delay_correction = delay_correction
94
95 def set_timing_advance(timing_advance):
96 self.timing_advance = timing_advance
97