blob: 03cc82ea90271246dc812558ede7e0ea07066852 [file] [log] [blame]
Piotr Krysik517464c2017-11-05 12:23:15 +01001/* -*- c++ -*- */
2/* @file
3 * @author Piotr Krysik <ptrkrysik@gmail.com>
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +07004 * @author Vadim Yanitskiy <axilirator@gmail.com>
Piotr Krysik517464c2017-11-05 12:23:15 +01005 * @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
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <gnuradio/io_signature.h>
29#include <grgsm/gsmtap.h>
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070030
Piotr Krysik517464c2017-11-05 12:23:15 +010031#include "txtime_setter_impl.h"
32
33namespace gr {
34 namespace gsm {
35
36 txtime_setter::sptr
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070037 txtime_setter::make(
38 uint32_t init_fn, uint64_t init_time_secs,
39 double init_time_fracs, uint64_t time_hint_secs,
40 double time_hint_fracs, double timing_advance,
41 double delay_correction)
Piotr Krysik517464c2017-11-05 12:23:15 +010042 {
43 return gnuradio::get_initial_sptr
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070044 (new txtime_setter_impl(init_fn, init_time_secs,
45 init_time_fracs, time_hint_secs, time_hint_fracs,
46 timing_advance, delay_correction));
Piotr Krysik517464c2017-11-05 12:23:15 +010047 }
48
49 /*
50 * The private constructor
51 */
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070052 txtime_setter_impl::txtime_setter_impl(
53 uint32_t init_fn, uint64_t init_time_secs,
54 double init_time_fracs, uint64_t time_hint_secs,
55 double time_hint_fracs, double timing_advance,
56 double delay_correction
57 ) : gr::block("txtime_setter",
58 gr::io_signature::make(0, 0, 0),
59 gr::io_signature::make(0, 0, 0)),
Piotr Krysik517464c2017-11-05 12:23:15 +010060 d_time_hint(time_hint_secs,time_hint_fracs),
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070061 d_time_ref(init_time_secs,init_time_fracs),
62 d_delay_correction(delay_correction),
Piotr Krysik517464c2017-11-05 12:23:15 +010063 d_timing_advance(timing_advance),
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070064 d_fn_ref(init_fn),
65 d_ts_ref(0)
Piotr Krysik517464c2017-11-05 12:23:15 +010066 {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070067 // Register I/O ports
68 message_port_register_in(pmt::mp("fn_time"));
69 message_port_register_in(pmt::mp("bursts_in"));
70 message_port_register_out(pmt::mp("bursts_out"));
Piotr Krysik517464c2017-11-05 12:23:15 +010071
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070072 // Bind message handlers
73 set_msg_handler(pmt::mp("fn_time"),
74 boost::bind(&txtime_setter_impl::process_fn_time_reference,
75 this, _1));
76 set_msg_handler(pmt::mp("bursts_in"),
77 boost::bind(&txtime_setter_impl::process_txtime_of_burst,
78 this, _1));
Piotr Krysik517464c2017-11-05 12:23:15 +010079 }
80
81 /*
82 * Our virtual destructor.
83 */
84 txtime_setter_impl::~txtime_setter_impl()
85 {
86 }
87
88 void txtime_setter_impl::process_fn_time_reference(pmt::pmt_t msg)
89 {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070090 pmt::pmt_t fn_time, time_hint;
Piotr Krysik517464c2017-11-05 12:23:15 +010091
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070092 fn_time = pmt::dict_ref(msg,
Vadim Yanitskiy01232322017-12-09 20:16:44 +070093 pmt::intern("fn_time"), pmt::PMT_NIL);
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070094 time_hint = pmt::dict_ref(msg,
Vadim Yanitskiy01232322017-12-09 20:16:44 +070095 pmt::intern("time_hint"), pmt::PMT_NIL);
Piotr Krysik517464c2017-11-05 12:23:15 +010096
Vadim Yanitskiy01232322017-12-09 20:16:44 +070097 if (fn_time != pmt::PMT_NIL) {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070098 uint32_t fn_ref = static_cast<uint32_t>
99 (pmt::to_uint64(pmt::car(pmt::car(fn_time))));
100 uint32_t ts = static_cast<uint32_t>
101 (pmt::to_uint64(pmt::cdr(pmt::car(fn_time))));
102 uint64_t time_secs = pmt::to_uint64(
103 pmt::car(pmt::cdr(fn_time)));
104 double time_fracs = pmt::to_double(
105 pmt::cdr(pmt::cdr(fn_time)));
106
107 set_fn_time_reference(fn_ref, ts, time_secs, time_fracs);
Vadim Yanitskiy01232322017-12-09 20:16:44 +0700108 } else if (time_hint != pmt::PMT_NIL) {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700109 set_time_hint(pmt::to_uint64(pmt::car(fn_time)),
110 pmt::to_double(pmt::cdr(fn_time)));
111 }
Piotr Krysik517464c2017-11-05 12:23:15 +0100112 }
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700113
114 void txtime_setter_impl::process_txtime_of_burst(pmt::pmt_t msg_in)
Piotr Krysik517464c2017-11-05 12:23:15 +0100115 {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700116 // Nothing to do...
117 if (d_fn_ref == 0xffffffff)
118 return;
119
120 pmt::pmt_t blob = pmt::cdr(msg_in);
121
122 // Extract GSMTAP header from message
123 gsmtap_hdr *header = (gsmtap_hdr *) pmt::blob_data(blob);
124 uint32_t frame_nr = be32toh(header->frame_number);
125 uint32_t ts_num = header->timeslot;
126
127 time_format txtime = fn_time_delta_cpp(d_fn_ref, d_time_ref,
128 frame_nr, d_time_hint, ts_num, d_ts_ref);
129
130 time_spec_t txtime_spec = time_spec_t(txtime.first, txtime.second);
131 txtime_spec -= d_delay_correction;
132 txtime_spec -= d_timing_advance;
133
134 // FIXME: better name?
135 time_spec_t foo = time_spec_t(d_time_hint.first, d_time_hint.second);
136
137 // Drop too late / early bursts
138 if (txtime_spec <= foo) {
139 std::cout << "lB" << std::flush;
140 return;
141 } else if (txtime_spec > foo + 10.0) {
142 std::cout << "eB" << std::flush;
143 return;
144 }
145
146 pmt::pmt_t tags_dict = pmt::dict_add(
147 pmt::make_dict(),
148 pmt::intern("tx_time"),
149 pmt::make_tuple(
150 pmt::from_uint64(txtime_spec.get_full_secs()),
151 pmt::from_double(txtime_spec.get_frac_secs()))
152 );
153
154 tags_dict = pmt::dict_add(tags_dict,
155 pmt::intern("fn"), pmt::from_uint64(frame_nr));
156 tags_dict = pmt::dict_add(tags_dict,
157 pmt::intern("ts"), pmt::from_uint64(ts_num));
158
159 // Send a message to the output
160 pmt::pmt_t msg_out = pmt::cons(tags_dict, pmt::cdr(msg_in));
161 message_port_pub(pmt::mp("bursts_out"), msg_out);
Piotr Krysik517464c2017-11-05 12:23:15 +0100162 }
163
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700164 void txtime_setter_impl::set_fn_time_reference(
165 uint32_t fn, uint32_t ts, uint64_t time_secs,
166 double time_fracs)
Piotr Krysik517464c2017-11-05 12:23:15 +0100167 {
168 d_fn_ref = fn;
169 d_ts_ref = ts;
170 d_time_ref = std::make_pair(time_secs, time_fracs);
171 set_time_hint(time_secs, time_fracs);
172 }
173
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700174 void txtime_setter_impl::set_time_hint(
175 uint64_t time_hint_secs, double time_hint_fracs)
Piotr Krysik517464c2017-11-05 12:23:15 +0100176 {
177 d_time_hint = std::make_pair(time_hint_secs, time_hint_fracs);
178 }
179
180 void txtime_setter_impl::set_delay_correction(double delay_correction)
181 {
182 d_delay_correction = delay_correction;
183 }
184
185 void txtime_setter_impl::set_timing_advance(double timing_advance)
186 {
187 d_timing_advance = timing_advance;
188 }
189
190 } /* namespace gsm */
191} /* namespace gr */