Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* @file |
| 3 | * @author Piotr Krysik <ptrkrysik@gmail.com> |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 4 | * @author Vadim Yanitskiy <axilirator@gmail.com> |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 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 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
| 28 | #include <gnuradio/io_signature.h> |
David Holm | 9aadf3d | 2018-09-16 13:13:52 +0200 | [diff] [blame] | 29 | #include <grgsm/endian.h> |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 30 | #include <grgsm/gsmtap.h> |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 31 | |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 32 | #include "txtime_setter_impl.h" |
| 33 | |
Piotr Krysik | ccd9ed9 | 2017-11-30 12:48:01 +0100 | [diff] [blame] | 34 | #define UNKNOWN_FN 0xffffffff |
| 35 | #define MAX_EARLY_TIME_DIFF 10.0 |
| 36 | |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 37 | namespace gr { |
| 38 | namespace gsm { |
| 39 | |
| 40 | txtime_setter::sptr |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 41 | txtime_setter::make( |
| 42 | uint32_t init_fn, uint64_t init_time_secs, |
| 43 | double init_time_fracs, uint64_t time_hint_secs, |
| 44 | double time_hint_fracs, double timing_advance, |
| 45 | double delay_correction) |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 46 | { |
| 47 | return gnuradio::get_initial_sptr |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 48 | (new txtime_setter_impl(init_fn, init_time_secs, |
| 49 | init_time_fracs, time_hint_secs, time_hint_fracs, |
| 50 | timing_advance, delay_correction)); |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* |
| 54 | * The private constructor |
| 55 | */ |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 56 | txtime_setter_impl::txtime_setter_impl( |
| 57 | uint32_t init_fn, uint64_t init_time_secs, |
| 58 | double init_time_fracs, uint64_t time_hint_secs, |
| 59 | double time_hint_fracs, double timing_advance, |
| 60 | double delay_correction |
| 61 | ) : gr::block("txtime_setter", |
| 62 | gr::io_signature::make(0, 0, 0), |
| 63 | gr::io_signature::make(0, 0, 0)), |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 64 | d_time_hint(time_hint_secs,time_hint_fracs), |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 65 | d_time_ref(init_time_secs,init_time_fracs), |
| 66 | d_delay_correction(delay_correction), |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 67 | d_timing_advance(timing_advance), |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 68 | d_fn_ref(init_fn), |
| 69 | d_ts_ref(0) |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 70 | { |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 71 | // Register I/O ports |
| 72 | message_port_register_in(pmt::mp("fn_time")); |
| 73 | message_port_register_in(pmt::mp("bursts_in")); |
| 74 | message_port_register_out(pmt::mp("bursts_out")); |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 75 | |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 76 | // Bind message handlers |
| 77 | set_msg_handler(pmt::mp("fn_time"), |
| 78 | boost::bind(&txtime_setter_impl::process_fn_time_reference, |
| 79 | this, _1)); |
| 80 | set_msg_handler(pmt::mp("bursts_in"), |
| 81 | boost::bind(&txtime_setter_impl::process_txtime_of_burst, |
| 82 | this, _1)); |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Our virtual destructor. |
| 87 | */ |
| 88 | txtime_setter_impl::~txtime_setter_impl() |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | void txtime_setter_impl::process_fn_time_reference(pmt::pmt_t msg) |
| 93 | { |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 94 | pmt::pmt_t fn_time, time_hint; |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 95 | |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 96 | fn_time = pmt::dict_ref(msg, |
Vadim Yanitskiy | 0123232 | 2017-12-09 20:16:44 +0700 | [diff] [blame] | 97 | pmt::intern("fn_time"), pmt::PMT_NIL); |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 98 | time_hint = pmt::dict_ref(msg, |
Vadim Yanitskiy | 0123232 | 2017-12-09 20:16:44 +0700 | [diff] [blame] | 99 | pmt::intern("time_hint"), pmt::PMT_NIL); |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 100 | |
Vadim Yanitskiy | 0123232 | 2017-12-09 20:16:44 +0700 | [diff] [blame] | 101 | if (fn_time != pmt::PMT_NIL) { |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 102 | uint32_t fn_ref = static_cast<uint32_t> |
| 103 | (pmt::to_uint64(pmt::car(pmt::car(fn_time)))); |
| 104 | uint32_t ts = static_cast<uint32_t> |
| 105 | (pmt::to_uint64(pmt::cdr(pmt::car(fn_time)))); |
| 106 | uint64_t time_secs = pmt::to_uint64( |
| 107 | pmt::car(pmt::cdr(fn_time))); |
| 108 | double time_fracs = pmt::to_double( |
| 109 | pmt::cdr(pmt::cdr(fn_time))); |
| 110 | |
| 111 | set_fn_time_reference(fn_ref, ts, time_secs, time_fracs); |
Vadim Yanitskiy | 0123232 | 2017-12-09 20:16:44 +0700 | [diff] [blame] | 112 | } else if (time_hint != pmt::PMT_NIL) { |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 113 | set_time_hint(pmt::to_uint64(pmt::car(fn_time)), |
| 114 | pmt::to_double(pmt::cdr(fn_time))); |
| 115 | } |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 116 | } |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 117 | |
| 118 | void txtime_setter_impl::process_txtime_of_burst(pmt::pmt_t msg_in) |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 119 | { |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 120 | pmt::pmt_t blob = pmt::cdr(msg_in); |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 121 | |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 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; |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 126 | |
Vadim Yanitskiy | fad602b | 2018-12-21 06:55:11 +0700 | [diff] [blame] | 127 | if (d_fn_ref == UNKNOWN_FN) { |
| 128 | std::cout << "Missing reference TDMA frame number, dropping " |
Piotr Krysik | 7fb1738 | 2018-12-28 22:36:40 +0100 | [diff] [blame] | 129 | << "burst (fn=" << frame_nr << ", tn=" << ts_num << ")" |
Vadim Yanitskiy | fad602b | 2018-12-21 06:55:11 +0700 | [diff] [blame] | 130 | << std::endl; |
| 131 | return; |
| 132 | } |
| 133 | |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 134 | time_format txtime = fn_time_delta_cpp(d_fn_ref, d_time_ref, |
| 135 | frame_nr, d_time_hint, ts_num, d_ts_ref); |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 136 | |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 137 | time_spec_t txtime_spec = time_spec_t(txtime.first, txtime.second); |
| 138 | txtime_spec -= d_delay_correction; |
| 139 | txtime_spec -= d_timing_advance; |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 140 | |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 141 | time_spec_t current_time_estimate = time_spec_t(d_time_hint.first, d_time_hint.second); |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 142 | |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 143 | if (txtime_spec <= current_time_estimate) { // Drop too late bursts |
| 144 | std::cout << "lB" << std::flush; |
| 145 | } else if (txtime_spec > current_time_estimate + MAX_EARLY_TIME_DIFF) { // Drop too early bursts |
| 146 | std::cout << "eB" << std::flush; //TODO: too early condition might happen when changing BTSes. |
| 147 | //Wrong fn_time is applied to new or old bursts in such situation. |
| 148 | //This solution is not perfect as MS might be blocked upto |
| 149 | //MAX_EARLY_TIME_DIFF seconds. |
| 150 | //Better solution would be to indentify fn_time and burst coming |
| 151 | //from given BTS (i.e. based on ARFCN) and dropping bursts for which |
| 152 | //the bts_id doesn't match with bts_id of fn_time. |
| 153 | } else { //process bursts that are in the right time-frame |
| 154 | pmt::pmt_t tags_dict = pmt::dict_add( |
| 155 | pmt::make_dict(), |
| 156 | pmt::intern("tx_time"), |
| 157 | pmt::make_tuple( |
| 158 | pmt::from_uint64(txtime_spec.get_full_secs()), |
| 159 | pmt::from_double(txtime_spec.get_frac_secs())) |
| 160 | ); |
Piotr Krysik | ccd9ed9 | 2017-11-30 12:48:01 +0100 | [diff] [blame] | 161 | |
Vadim Yanitskiy | 06f76fb | 2018-09-21 13:49:57 +0700 | [diff] [blame] | 162 | tags_dict = pmt::dict_add(tags_dict, |
| 163 | pmt::intern("fn"), pmt::from_uint64(frame_nr)); |
| 164 | tags_dict = pmt::dict_add(tags_dict, |
| 165 | pmt::intern("ts"), pmt::from_uint64(ts_num)); |
| 166 | |
| 167 | // Send a message to the output |
| 168 | pmt::pmt_t msg_out = pmt::cons(tags_dict, pmt::cdr(msg_in)); |
| 169 | message_port_pub(pmt::mp("bursts_out"), msg_out); |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 170 | } |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 173 | void txtime_setter_impl::set_fn_time_reference( |
| 174 | uint32_t fn, uint32_t ts, uint64_t time_secs, |
| 175 | double time_fracs) |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 176 | { |
| 177 | d_fn_ref = fn; |
| 178 | d_ts_ref = ts; |
| 179 | d_time_ref = std::make_pair(time_secs, time_fracs); |
| 180 | set_time_hint(time_secs, time_fracs); |
| 181 | } |
| 182 | |
Vadim Yanitskiy | 2ce2942 | 2017-11-29 23:58:42 +0700 | [diff] [blame] | 183 | void txtime_setter_impl::set_time_hint( |
| 184 | uint64_t time_hint_secs, double time_hint_fracs) |
Piotr Krysik | 517464c | 2017-11-05 12:23:15 +0100 | [diff] [blame] | 185 | { |
| 186 | d_time_hint = std::make_pair(time_hint_secs, time_hint_fracs); |
| 187 | } |
| 188 | |
| 189 | void txtime_setter_impl::set_delay_correction(double delay_correction) |
| 190 | { |
| 191 | d_delay_correction = delay_correction; |
| 192 | } |
| 193 | |
| 194 | void txtime_setter_impl::set_timing_advance(double timing_advance) |
| 195 | { |
| 196 | d_timing_advance = timing_advance; |
| 197 | } |
| 198 | |
| 199 | } /* namespace gsm */ |
| 200 | } /* namespace gr */ |