blob: 489af2ad9c368cd8e716af80690b8dce29ef4c0a [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):
Piotr Krysik96f2ea72017-10-16 15:47:08 +020050 time_hint = pmt.to_python(pmt.dict_ref(msg, pmt.intern("time_hint"), pmt.PMT_NIL))
51 fn_time = pmt.to_python(pmt.dict_ref(msg, pmt.intern("fn_time"), pmt.PMT_NIL))
52
53# if self.fn_ref is None:
Piotr Krysik27f00322017-09-19 08:05:28 +020054 if time_hint is not None:
55 self.time_hint = time_hint
56 elif fn_time is not None:
Piotr Krysik96f2ea72017-10-16 15:47:08 +020057 self.fn_ref = fn_time[0][0]
58 self.ts = fn_time[0][1]
59 full = fn_time[1][0]
60 frac = fn_time[1][1]
61
62 self.time_ref = full+frac
Piotr Krysik27f00322017-09-19 08:05:28 +020063 self.time_hint = self.time_ref
Piotr Krysik2c7aa9f2017-09-27 22:14:32 +020064
Piotr Krysik27f00322017-09-19 08:05:28 +020065 def process_txtime_of_burst(self, msg):
66 burst_with_header = pmt.to_python(pmt.cdr(msg))
67 fn = burst_with_header[11]+burst_with_header[10]*2**8+burst_with_header[9]*2**16+burst_with_header[8]*2**24
68 ts_num = burst_with_header[3]
Piotr Krysik96f2ea72017-10-16 15:47:08 +020069 if self.fn_ref is not None:
70 fn_delta, txtime = fn_time_delta(self.fn_ref, self.time_ref, fn, self.time_hint, ts_num)
71 txtime_secs = int(txtime)
72 txtime_fracs = txtime-int(txtime)
73 #print "txtime_secs",txtime_secs,"txtime_fracs",txtime_fracs
74 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)))
75 new_msg = pmt.cons(tags_dict, pmt.cdr(msg))
76 self.message_port_pub(pmt.intern("bursts"), new_msg)
Piotr Krysik2164b9b2017-09-13 09:33:27 +020077
Piotr Krysik27f00322017-09-19 08:05:28 +020078 def set_fn_time_reference(self, init_fn, init_time):
79 self.fn_ref = init_fn
80 self.time_ref = init_time
81 self.set_time_hint(init_time)
82
83 def set_time_hint(self, time_hint):
84 self.time_hint = time_hint
85