blob: 9f3a24d7bd0c9b485985ff29fc53974ffeb3faea [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
Piotr Krysikccd9ed92017-11-30 12:48:01 +010033#define UNKNOWN_FN 0xffffffff
34#define MAX_EARLY_TIME_DIFF 10.0
35
Piotr Krysik517464c2017-11-05 12:23:15 +010036namespace gr {
37 namespace gsm {
38
39 txtime_setter::sptr
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070040 txtime_setter::make(
41 uint32_t init_fn, uint64_t init_time_secs,
42 double init_time_fracs, uint64_t time_hint_secs,
43 double time_hint_fracs, double timing_advance,
44 double delay_correction)
Piotr Krysik517464c2017-11-05 12:23:15 +010045 {
46 return gnuradio::get_initial_sptr
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070047 (new txtime_setter_impl(init_fn, init_time_secs,
48 init_time_fracs, time_hint_secs, time_hint_fracs,
49 timing_advance, delay_correction));
Piotr Krysik517464c2017-11-05 12:23:15 +010050 }
51
52 /*
53 * The private constructor
54 */
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070055 txtime_setter_impl::txtime_setter_impl(
56 uint32_t init_fn, uint64_t init_time_secs,
57 double init_time_fracs, uint64_t time_hint_secs,
58 double time_hint_fracs, double timing_advance,
59 double delay_correction
60 ) : gr::block("txtime_setter",
61 gr::io_signature::make(0, 0, 0),
62 gr::io_signature::make(0, 0, 0)),
Piotr Krysik517464c2017-11-05 12:23:15 +010063 d_time_hint(time_hint_secs,time_hint_fracs),
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070064 d_time_ref(init_time_secs,init_time_fracs),
65 d_delay_correction(delay_correction),
Piotr Krysik517464c2017-11-05 12:23:15 +010066 d_timing_advance(timing_advance),
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070067 d_fn_ref(init_fn),
68 d_ts_ref(0)
Piotr Krysik517464c2017-11-05 12:23:15 +010069 {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070070 // Register I/O ports
71 message_port_register_in(pmt::mp("fn_time"));
72 message_port_register_in(pmt::mp("bursts_in"));
73 message_port_register_out(pmt::mp("bursts_out"));
Piotr Krysik517464c2017-11-05 12:23:15 +010074
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070075 // Bind message handlers
76 set_msg_handler(pmt::mp("fn_time"),
77 boost::bind(&txtime_setter_impl::process_fn_time_reference,
78 this, _1));
79 set_msg_handler(pmt::mp("bursts_in"),
80 boost::bind(&txtime_setter_impl::process_txtime_of_burst,
81 this, _1));
Piotr Krysik517464c2017-11-05 12:23:15 +010082 }
83
84 /*
85 * Our virtual destructor.
86 */
87 txtime_setter_impl::~txtime_setter_impl()
88 {
89 }
90
91 void txtime_setter_impl::process_fn_time_reference(pmt::pmt_t msg)
92 {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070093 pmt::pmt_t fn_time, time_hint;
Piotr Krysik517464c2017-11-05 12:23:15 +010094
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070095 fn_time = pmt::dict_ref(msg,
Vadim Yanitskiy01232322017-12-09 20:16:44 +070096 pmt::intern("fn_time"), pmt::PMT_NIL);
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +070097 time_hint = pmt::dict_ref(msg,
Vadim Yanitskiy01232322017-12-09 20:16:44 +070098 pmt::intern("time_hint"), pmt::PMT_NIL);
Piotr Krysik517464c2017-11-05 12:23:15 +010099
Vadim Yanitskiy01232322017-12-09 20:16:44 +0700100 if (fn_time != pmt::PMT_NIL) {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700101 uint32_t fn_ref = static_cast<uint32_t>
102 (pmt::to_uint64(pmt::car(pmt::car(fn_time))));
103 uint32_t ts = static_cast<uint32_t>
104 (pmt::to_uint64(pmt::cdr(pmt::car(fn_time))));
105 uint64_t time_secs = pmt::to_uint64(
106 pmt::car(pmt::cdr(fn_time)));
107 double time_fracs = pmt::to_double(
108 pmt::cdr(pmt::cdr(fn_time)));
109
110 set_fn_time_reference(fn_ref, ts, time_secs, time_fracs);
Vadim Yanitskiy01232322017-12-09 20:16:44 +0700111 } else if (time_hint != pmt::PMT_NIL) {
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700112 set_time_hint(pmt::to_uint64(pmt::car(fn_time)),
113 pmt::to_double(pmt::cdr(fn_time)));
114 }
Piotr Krysik517464c2017-11-05 12:23:15 +0100115 }
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700116
117 void txtime_setter_impl::process_txtime_of_burst(pmt::pmt_t msg_in)
Piotr Krysik517464c2017-11-05 12:23:15 +0100118 {
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700119 pmt::pmt_t blob = pmt::cdr(msg_in);
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700120
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700121 // Extract GSMTAP header from message
122 gsmtap_hdr *header = (gsmtap_hdr *) pmt::blob_data(blob);
123 uint32_t frame_nr = be32toh(header->frame_number);
124 uint32_t ts_num = header->timeslot;
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700125
Vadim Yanitskiydc342dc2018-12-21 06:55:11 +0700126 if (d_fn_ref == UNKNOWN_FN) {
127 std::cout << "Missing reference TDMA frame number, dropping "
128 << format("burst (fn=%u, tn=%u)", frame_nr, ts_num)
129 << std::endl;
130 return;
131 }
132
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700133 time_format txtime = fn_time_delta_cpp(d_fn_ref, d_time_ref,
134 frame_nr, d_time_hint, ts_num, d_ts_ref);
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700135
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700136 time_spec_t txtime_spec = time_spec_t(txtime.first, txtime.second);
137 txtime_spec -= d_delay_correction;
138 txtime_spec -= d_timing_advance;
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700139
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700140 time_spec_t current_time_estimate = time_spec_t(d_time_hint.first, d_time_hint.second);
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700141
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700142 if (txtime_spec <= current_time_estimate) { // Drop too late bursts
143 std::cout << "lB" << std::flush;
144 } else if (txtime_spec > current_time_estimate + MAX_EARLY_TIME_DIFF) { // Drop too early bursts
145 std::cout << "eB" << std::flush; //TODO: too early condition might happen when changing BTSes.
146 //Wrong fn_time is applied to new or old bursts in such situation.
147 //This solution is not perfect as MS might be blocked upto
148 //MAX_EARLY_TIME_DIFF seconds.
149 //Better solution would be to indentify fn_time and burst coming
150 //from given BTS (i.e. based on ARFCN) and dropping bursts for which
151 //the bts_id doesn't match with bts_id of fn_time.
152 } else { //process bursts that are in the right time-frame
153 pmt::pmt_t tags_dict = pmt::dict_add(
154 pmt::make_dict(),
155 pmt::intern("tx_time"),
156 pmt::make_tuple(
157 pmt::from_uint64(txtime_spec.get_full_secs()),
158 pmt::from_double(txtime_spec.get_frac_secs()))
159 );
Piotr Krysikccd9ed92017-11-30 12:48:01 +0100160
Vadim Yanitskiyb73b4a82018-09-21 13:49:57 +0700161 tags_dict = pmt::dict_add(tags_dict,
162 pmt::intern("fn"), pmt::from_uint64(frame_nr));
163 tags_dict = pmt::dict_add(tags_dict,
164 pmt::intern("ts"), pmt::from_uint64(ts_num));
165
166 // Send a message to the output
167 pmt::pmt_t msg_out = pmt::cons(tags_dict, pmt::cdr(msg_in));
168 message_port_pub(pmt::mp("bursts_out"), msg_out);
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700169 }
Piotr Krysik517464c2017-11-05 12:23:15 +0100170 }
171
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700172 void txtime_setter_impl::set_fn_time_reference(
173 uint32_t fn, uint32_t ts, uint64_t time_secs,
174 double time_fracs)
Piotr Krysik517464c2017-11-05 12:23:15 +0100175 {
176 d_fn_ref = fn;
177 d_ts_ref = ts;
178 d_time_ref = std::make_pair(time_secs, time_fracs);
179 set_time_hint(time_secs, time_fracs);
180 }
181
Vadim Yanitskiy2ce29422017-11-29 23:58:42 +0700182 void txtime_setter_impl::set_time_hint(
183 uint64_t time_hint_secs, double time_hint_fracs)
Piotr Krysik517464c2017-11-05 12:23:15 +0100184 {
185 d_time_hint = std::make_pair(time_hint_secs, time_hint_fracs);
186 }
187
188 void txtime_setter_impl::set_delay_correction(double delay_correction)
189 {
190 d_delay_correction = delay_correction;
191 }
192
193 void txtime_setter_impl::set_timing_advance(double timing_advance)
194 {
195 d_timing_advance = timing_advance;
196 }
197
198 } /* namespace gsm */
199} /* namespace gr */