blob: f587f43a4ba076cb0ae214755ee597583ddf420d [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
Piotr Krysik27f00322017-09-19 08:05:28 +020025from fn_time import fn_time_delta
26import 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 Krysik27f00322017-09-19 08:05:28 +020033 def __init__(self, init_fn=0, init_time=0, time_hint=None):
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 Krysik2164b9b2017-09-13 09:33:27 +020042 self.message_port_register_in(pmt.intern("fn_time"))
43 self.message_port_register_in(pmt.intern("bursts"))
44 self.message_port_register_out(pmt.intern("bursts"))
Piotr Krysik27f00322017-09-19 08:05:28 +020045
46 self.set_msg_handler(pmt.intern("fn_time"), self.process_fn_time_reference)
47 self.set_msg_handler(pmt.intern("bursts"), self.process_txtime_of_burst)
Piotr Krysik2164b9b2017-09-13 09:33:27 +020048
Piotr Krysik27f00322017-09-19 08:05:28 +020049 def process_fn_time_reference(self, msg):
50 time_hint = pmt.to_python(pmt.dict_ref(pmt.car(msg), (pmt.intern("time_hint"),PMT_NIL)))
51 fn_time = pmt.to_python(pmt.dict_ref(pmt.car(msg), (pmt.intern("fn_time"),PMT_NIL)))
52 if time_hint is not None:
53 self.time_hint = time_hint
54 elif fn_time is not None:
55 self.fn_ref = pmt.car(fn_time)
56 self.time_ref = pmt.cdr(fn_time)
57 self.time_hint = self.time_ref
Piotr Krysik2c7aa9f2017-09-27 22:14:32 +020058
Piotr Krysik27f00322017-09-19 08:05:28 +020059 def process_txtime_of_burst(self, msg):
60 burst_with_header = pmt.to_python(pmt.cdr(msg))
61 fn = burst_with_header[11]+burst_with_header[10]*2**8+burst_with_header[9]*2**16+burst_with_header[8]*2**24
62 ts_num = burst_with_header[3]
63 fn_delta, txtime = fn_time_delta(self.fn_ref, self.time_ref, fn, self.time_hint, ts_num)
Piotr Krysik7980eb92017-09-27 22:00:00 +020064 txtime_secs = int(txtime)
65 txtime_fracs = txtime-int(txtime)
66 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 Krysik27f00322017-09-19 08:05:28 +020067 new_msg = pmt.cons(tags_dict, pmt.cdr(msg))
68 self.message_port_pub(pmt.intern("bursts"), new_msg)
Piotr Krysik2164b9b2017-09-13 09:33:27 +020069
Piotr Krysik27f00322017-09-19 08:05:28 +020070 def set_fn_time_reference(self, init_fn, init_time):
71 self.fn_ref = init_fn
72 self.time_ref = init_time
73 self.set_time_hint(init_time)
74
75 def set_time_hint(self, time_hint):
76 self.time_hint = time_hint
77