Piotr Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 1 | #!/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 Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 24 | from gnuradio import gr |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 25 | from fn_time import fn_time_delta |
| 26 | import pmt |
| 27 | import numpy |
Piotr Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 28 | |
| 29 | class txtime_bursts_tagger(gr.basic_block): |
| 30 | """ |
| 31 | A block that adds txtime metadata to a burst |
| 32 | """ |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 33 | def __init__(self, init_fn=0, init_time=0, time_hint=None): |
Piotr Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 34 | gr.basic_block.__init__(self, |
| 35 | name="txtime_bursts_tagger", |
| 36 | in_sig=[], |
| 37 | out_sig=[]) |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 38 | 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 Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 42 | 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 Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 45 | |
| 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 Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 48 | |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 49 | 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 Krysik | 2c7aa9f | 2017-09-27 22:14:32 +0200 | [diff] [blame^] | 58 | |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 59 | 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 Krysik | 7980eb9 | 2017-09-27 22:00:00 +0200 | [diff] [blame] | 64 | 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 Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 67 | new_msg = pmt.cons(tags_dict, pmt.cdr(msg)) |
| 68 | self.message_port_pub(pmt.intern("bursts"), new_msg) |
Piotr Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 69 | |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 70 | 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 | |