blob: 91fc4cae88d81d1ea8da57212ab392099d414f0f [file] [log] [blame]
Piotr Krysik517464c2017-11-05 12:23:15 +01001/* -*- c++ -*- */
2/* @file
3 * @author Piotr Krysik <ptrkrysik@gmail.com>
4 * @section LICENSE
5 *
6 * Gr-gsm is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
10 *
11 * Gr-gsm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gr-gsm; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
28#include <grgsm/gsmtap.h>
29#include "txtime_setter_impl.h"
30
31namespace gr {
32 namespace gsm {
33
34 txtime_setter::sptr
35 txtime_setter::make(uint32_t init_fn, uint64_t init_time_secs, double init_time_fracs, uint64_t time_hint_secs, double time_hint_fracs, double timing_advance, double delay_correction)
36 {
37 return gnuradio::get_initial_sptr
38 (new txtime_setter_impl(init_fn, init_time_secs, init_time_fracs, time_hint_secs, time_hint_fracs, timing_advance, delay_correction));
39 }
40
41 /*
42 * The private constructor
43 */
44 txtime_setter_impl::txtime_setter_impl(uint32_t init_fn, uint64_t init_time_secs, double init_time_fracs, uint64_t time_hint_secs, double time_hint_fracs, double timing_advance, double delay_correction)
45 : gr::block("txtime_setter",
46 gr::io_signature::make(0, 0, 0),
47 gr::io_signature::make(0, 0, 0)),
48 d_fn_ref(init_fn),
49 d_ts_ref(0),
50 d_time_ref(init_time_secs,init_time_fracs),
51 d_time_hint(time_hint_secs,time_hint_fracs),
52 d_timing_advance(timing_advance),
53 d_delay_correction(delay_correction)
54 {
55 message_port_register_in(pmt::intern("fn_time"));
56 message_port_register_in(pmt::intern("bursts_in"));
57 message_port_register_out(pmt::intern("bursts_out"));
58
59 set_msg_handler(pmt::intern("fn_time"), boost::bind(&txtime_setter_impl::process_fn_time_reference, this, _1));
60 set_msg_handler(pmt::intern("bursts_in"), boost::bind(&txtime_setter_impl::process_txtime_of_burst, this, _1));
61 }
62
63 /*
64 * Our virtual destructor.
65 */
66 txtime_setter_impl::~txtime_setter_impl()
67 {
68 }
69
70 void txtime_setter_impl::process_fn_time_reference(pmt::pmt_t msg)
71 {
72// d_time_hint = pmt::to_python(pmt::dict_ref(msg, pmt::intern("time_hint"), pmt::PMT_NIL))
73 pmt::pmt_t not_found = pmt::intern("not_found");
74 pmt::pmt_t fn_time = pmt::dict_ref(msg, pmt::intern("fn_time"), not_found);
75 pmt::pmt_t time_hint = pmt::dict_ref(msg, pmt::intern("fn_time"), not_found);
76
77 if(fn_time != not_found)
78 {
79 uint32_t fn_ref = static_cast<uint32_t>(pmt::to_uint64(pmt::car(pmt::car(fn_time))));
80 uint32_t ts = static_cast<uint32_t>(pmt::to_uint64(pmt::cdr(pmt::car(fn_time))));
81 uint64_t time_secs = pmt::to_uint64(pmt::car(pmt::cdr(fn_time)));
82 double time_fracs = pmt::to_double(pmt::cdr(pmt::cdr(fn_time)));
83 set_fn_time_reference(fn_ref, ts, time_secs, time_fracs);
84 } else if(time_hint != not_found) {
85 set_time_hint(pmt::to_uint64(pmt::car(fn_time)), pmt::to_double(pmt::cdr(fn_time)));
86 }
87
88 }
89
90 void txtime_setter_impl::process_txtime_of_burst(pmt::pmt_t burst)
91 {
92 pmt::pmt_t header_plus_burst = pmt::cdr(burst);
93 int8_t * burst_int8 = (int8_t *)pmt::blob_data(header_plus_burst);
94 gsmtap_hdr * header = (gsmtap_hdr *)(burst_int8);
95
96 uint32_t frame_nr = be32toh(header->frame_number);
97 uint32_t ts_num = header->timeslot;
98
99 if(d_fn_ref != 0xffffffff)
100 {
101 time_format txtime = fn_time_delta_cpp(d_fn_ref, d_time_ref, frame_nr, d_time_hint, ts_num, d_ts_ref);
102 time_spec_t txtime_spec = time_spec_t(txtime.first, txtime.second);
103 txtime_spec = txtime_spec - d_delay_correction;
104 txtime_spec = txtime_spec - d_timing_advance;
105
Piotr Krysikba8b0a92017-11-11 11:19:26 +0100106 if(txtime_spec <= time_spec_t(d_time_hint.first, d_time_hint.second))
107 {
108 std::cout << "lB" << std::flush;
109 }
110 else if(txtime_spec > time_spec_t(d_time_hint.first, d_time_hint.second)+10.0)
111 {
112 std::cout << "eB" << std::flush;
113 }
114 else
115 {
116 pmt::pmt_t tags_dict = pmt::dict_add(pmt::make_dict(), pmt::intern("tx_time"), pmt::make_tuple(pmt::from_uint64(txtime_spec.get_full_secs()),pmt::from_double(txtime_spec.get_frac_secs())));
117 tags_dict = pmt::dict_add(tags_dict, pmt::intern("fn"), pmt::from_uint64(frame_nr));
118 tags_dict = pmt::dict_add(tags_dict, pmt::intern("ts"), pmt::from_uint64(ts_num));
119 pmt::pmt_t new_msg = pmt::cons(tags_dict, pmt::cdr(burst));
120 message_port_pub(pmt::intern("bursts_out"), new_msg);
121 }
Piotr Krysik517464c2017-11-05 12:23:15 +0100122 }
123 }
124
125 void txtime_setter_impl::set_fn_time_reference(uint32_t fn, uint32_t ts, uint64_t time_secs, double time_fracs)
126 {
127 d_fn_ref = fn;
128 d_ts_ref = ts;
129 d_time_ref = std::make_pair(time_secs, time_fracs);
130 set_time_hint(time_secs, time_fracs);
131 }
132
133 void txtime_setter_impl::set_time_hint(uint64_t time_hint_secs, double time_hint_fracs)
134 {
135 d_time_hint = std::make_pair(time_hint_secs, time_hint_fracs);
136 }
137
138 void txtime_setter_impl::set_delay_correction(double delay_correction)
139 {
140 d_delay_correction = delay_correction;
141 }
142
143 void txtime_setter_impl::set_timing_advance(double timing_advance)
144 {
145 d_timing_advance = timing_advance;
146 }
147
148 } /* namespace gsm */
149} /* namespace gr */
150