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 | 42b2186 | 2017-10-19 09:20:23 +0200 | [diff] [blame] | 33 | def __init__(self, init_fn=0, init_time=0, time_hint=None, timing_advance=0, delay_correction=0): |
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 | 42b2186 | 2017-10-19 09:20:23 +0200 | [diff] [blame] | 42 | self.timing_advance = timing_advance |
| 43 | self.delay_correction = delay_correction |
| 44 | |
Piotr Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 45 | 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 Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 48 | |
| 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 Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 51 | |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 52 | def process_fn_time_reference(self, msg): |
Piotr Krysik | 96f2ea7 | 2017-10-16 15:47:08 +0200 | [diff] [blame] | 53 | 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 Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 56 | if time_hint is not None: |
| 57 | self.time_hint = time_hint |
| 58 | elif fn_time is not None: |
Piotr Krysik | 96f2ea7 | 2017-10-16 15:47:08 +0200 | [diff] [blame] | 59 | 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 Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 65 | self.time_hint = self.time_ref |
Piotr Krysik | 2c7aa9f | 2017-09-27 22:14:32 +0200 | [diff] [blame] | 66 | |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 67 | 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 Krysik | 96f2ea7 | 2017-10-16 15:47:08 +0200 | [diff] [blame] | 71 | 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 Krysik | 42b2186 | 2017-10-19 09:20:23 +0200 | [diff] [blame] | 73 | txtime_corrected = txtime - self.delay_correction |
| 74 | txtime_final = txtime_corrected - self.timing_advance |
| 75 | |
Piotr Krysik | 33fbc28 | 2017-10-31 05:54:31 +0100 | [diff] [blame] | 76 | txtime_secs = int(txtime_final) |
| 77 | txtime_fracs = txtime_final-int(txtime_final) |
Piotr Krysik | 96f2ea7 | 2017-10-16 15:47:08 +0200 | [diff] [blame] | 78 | #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 Krysik | 42b2186 | 2017-10-19 09:20:23 +0200 | [diff] [blame] | 80 | tags_dict = pmt.dict_add(tags_dict, pmt.intern("fn"), pmt.from_uint64(fn)) |
Piotr Krysik | 96f2ea7 | 2017-10-16 15:47:08 +0200 | [diff] [blame] | 81 | new_msg = pmt.cons(tags_dict, pmt.cdr(msg)) |
| 82 | self.message_port_pub(pmt.intern("bursts"), new_msg) |
Piotr Krysik | 2164b9b | 2017-09-13 09:33:27 +0200 | [diff] [blame] | 83 | |
Piotr Krysik | 27f0032 | 2017-09-19 08:05:28 +0200 | [diff] [blame] | 84 | 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 Krysik | 42b2186 | 2017-10-19 09:20:23 +0200 | [diff] [blame] | 92 | 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 | |