blob: d57b4a12f3fadd503d1830544dcf18ebdb5ab7f8 [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
piotrd0bf1492014-02-05 17:27:32 +01002/*
ptrkrysik529895b2014-12-02 18:07:38 +01003 * @file
Piotr Krysika6268a52017-08-23 16:02:19 +02004 * @author (C) 2009-2017 by Piotr Krysik <ptrkrysik@gmail.com>
ptrkrysik529895b2014-12-02 18:07:38 +01005 * @section LICENSE
piotrd0bf1492014-02-05 17:27:32 +01006 *
ptrkrysik529895b2014-12-02 18:07:38 +01007 * Gr-gsm is free software; you can redistribute it and/or modify
piotr437f5462014-02-04 17:57:25 +01008 * 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.
piotrd0bf1492014-02-05 17:27:32 +010011 *
ptrkrysik529895b2014-12-02 18:07:38 +010012 * Gr-gsm is distributed in the hope that it will be useful,
piotr437f5462014-02-04 17:57:25 +010013 * 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.
piotrd0bf1492014-02-05 17:27:32 +010016 *
piotr437f5462014-02-04 17:57:25 +010017 * You should have received a copy of the GNU General Public License
ptrkrysik529895b2014-12-02 18:07:38 +010018 * along with gr-gsm; see the file COPYING. If not, write to
piotr437f5462014-02-04 17:57:25 +010019 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
piotr437f5462014-02-04 17:57:25 +010028#include <gnuradio/math.h>
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070029
piotr437f5462014-02-04 17:57:25 +010030#include <algorithm>
piotr437f5462014-02-04 17:57:25 +010031#include <string.h>
piotr437f5462014-02-04 17:57:25 +010032#include <iostream>
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070033#include <numeric>
34#include <math.h>
35#include <vector>
ptrkrysik3be74a72014-12-13 10:11:00 +010036
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070037#include <boost/circular_buffer.hpp>
38#include <boost/scoped_ptr.hpp>
ptrkrysik3be74a72014-12-13 10:11:00 +010039#include <grgsm/endian.h>
ptrkrysik58213792014-10-30 09:05:15 +010040
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070041#include "receiver_impl.h"
42#include "viterbi_detector.h"
43#include "sch.h"
44
45#if 0
46/* Files included for debuging */
47#include "plotting/plotting.hpp"
48#include <pthread.h>
49#include <iomanip>
50#endif
piotr437f5462014-02-04 17:57:25 +010051
52#define SYNC_SEARCH_RANGE 30
53
piotrd0bf1492014-02-05 17:27:32 +010054namespace gr
55{
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070056 namespace gsm
57 {
piotrd0bf1492014-02-05 17:27:32 +010058
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070059 /* The public constructor */
60 receiver::sptr
61 receiver::make(
62 int osr, const std::vector<int> &cell_allocation,
63 const std::vector<int> &tseq_nums, bool process_uplink)
piotr437f5462014-02-04 17:57:25 +010064 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070065 return gnuradio::get_initial_sptr
66 (new receiver_impl(osr, cell_allocation,
67 tseq_nums, process_uplink));
piotr437f5462014-02-04 17:57:25 +010068 }
69
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070070 /* The private constructor */
71 receiver_impl::receiver_impl(
72 int osr, const std::vector<int> &cell_allocation,
73 const std::vector<int> &tseq_nums, bool process_uplink
74 ) : gr::sync_block("receiver",
75 gr::io_signature::make(1, -1, sizeof(gr_complex)),
76 gr::io_signature::make(0, 0, 0)),
Piotr Krysik96f2ea72017-10-16 15:47:08 +020077 d_samples_consumed(0),
Piotr Krysikdf978692017-09-27 21:58:24 +020078 d_rx_time_received(false),
79 d_time_samp_ref(GSM_SYMBOL_RATE * osr),
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070080 d_OSR(osr),
81 d_process_uplink(process_uplink),
82 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
Piotr Krysikdf978692017-09-27 21:58:24 +020083 d_counter(0), //TODO: use nitems_read instead of d_counter
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070084 d_fcch_start_pos(0),
85 d_freq_offset_setting(0),
86 d_state(fcch_search),
87 d_burst_nr(osr),
88 d_failed_sch(0),
89 d_signal_dbm(-120),
90 d_tseq_nums(tseq_nums),
91 d_cell_allocation(cell_allocation),
92 d_last_time(0.0)
ptrkrysik32c21162015-04-04 14:01:52 +020093 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070094 /**
95 * Don't send samples to the receiver
96 * until there are at least samples for one
97 */
98 set_output_multiple(floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR));
99
100 /**
101 * Prepare SCH sequence bits
102 *
103 * (TS_BITS + 2 * GUARD_PERIOD)
104 * Burst and two guard periods
105 * (one guard period is an arbitrary overlap)
106 */
107 gmsk_mapper(SYNC_BITS, N_SYNC_BITS,
108 d_sch_training_seq, gr_complex(0.0, -1.0));
109
110 /* Prepare bits of training sequences */
111 for (int i = 0; i < TRAIN_SEQ_NUM; i++) {
112 /**
113 * If first bit of the sequence is 0
114 * => first symbol is 1, else -1
115 */
116 gr_complex startpoint = train_seq[i][0] == 0 ?
117 gr_complex(1.0, 0.0) : gr_complex(-1.0, 0.0);
118 gmsk_mapper(train_seq[i], N_TRAIN_BITS,
119 d_norm_training_seq[i], startpoint);
120 }
121
122 /* Register output ports */
123 message_port_register_out(pmt::mp("C0"));
124 message_port_register_out(pmt::mp("CX"));
125 message_port_register_out(pmt::mp("measurements"));
126
127 /**
128 * Configure the receiver,
129 * i.e. tell it where to find which burst type
130 */
131 configure_receiver();
132 }
133
134 /* Our virtual destructor */
135 receiver_impl::~receiver_impl() {}
136
137 int
138 receiver_impl::work(
139 int noutput_items,
140 gr_vector_const_void_star &input_items,
141 gr_vector_void_star &output_items)
142 {
143 gr_complex *input = (gr_complex *) input_items[0];
144 uint64_t start = nitems_read(0);
145 uint64_t stop = start + noutput_items;
146 d_freq_offset_tag_in_fcch = false;
147
148#if 0
149 /* FIXME: jak zrobić to rzutowanie poprawnie */
150 std::vector<const gr_complex *> iii =
151 (std::vector<const gr_complex *>) input_items;
152#endif
Piotr Krysikdf978692017-09-27 21:58:24 +0200153
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700154
155 /* Time synchronization loop */
156 float current_time =
157 static_cast<float>(start / (GSM_SYMBOL_RATE * d_OSR));
158 if ((current_time - d_last_time) > 0.1) {
159 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),
160 pmt::from_double(current_time));
ptrkrysik32c21162015-04-04 14:01:52 +0200161 message_port_pub(pmt::mp("measurements"), msg);
162 d_last_time = current_time;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700163 }
ptrkrysik32c21162015-04-04 14:01:52 +0200164
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700165 /* Frequency correction loop */
166 std::vector<tag_t> freq_offset_tags;
167 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
168 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
169
170 if (!freq_offset_tags.empty()) {
piotr4089c1a2014-08-06 14:10:56 +0200171 tag_t freq_offset_tag = freq_offset_tags[0];
Piotr Krysik43af70d2016-07-20 21:37:24 +0200172 uint64_t tag_offset = freq_offset_tag.offset - start;
Piotr Krysik43af70d2016-07-20 21:37:24 +0200173 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
piotr4089c1a2014-08-06 14:10:56 +0200174
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700175 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
176 if (d_state == synchronized && b_type == fcch_burst){
177 uint64_t last_sample_nr =
178 ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
179 d_freq_offset_tag_in_fcch = tag_offset < last_sample_nr;
piotrd0bf1492014-02-05 17:27:32 +0100180 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700181 }
Piotr Krysikdf978692017-09-27 21:58:24 +0200182
183 /* Obtaining current time with use of rx_time tag provided i.e. by UHD devices */
184 /* And storing it in time_sample_ref for sample number to time conversion */
185 std::vector<tag_t> rx_time_tags;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700186
187 /* Main state machine */
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200188 d_samples_consumed = 0;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700189 switch (d_state) {
190 case fcch_search:
191 fcch_search_handler(input, noutput_items);
piotrd0bf1492014-02-05 17:27:32 +0100192 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700193 case sch_search:
194 sch_search_handler(input, noutput_items);
piotrd0bf1492014-02-05 17:27:32 +0100195 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700196 case synchronized:
197 synchronized_handler(input, input_items, noutput_items);
198 break;
199 }
200
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200201 get_tags_in_window(rx_time_tags, 0, 0, d_samples_consumed, pmt::string_to_symbol("rx_time"));
202 if(!rx_time_tags.empty()){
203 d_rx_time_received = true;
204 tag_t rx_time_tag = *(rx_time_tags.begin());
205
206 uint64_t rx_time_full_part = to_uint64(tuple_ref(rx_time_tag.value,0));
207 double rx_time_frac_part = to_double(tuple_ref(rx_time_tag.value,1));
208
209 time_spec_t current_rx_time = time_spec_t(rx_time_full_part, rx_time_frac_part);
210 uint64_t current_start_offset = rx_time_tag.offset;
211 d_time_samp_ref.update(current_rx_time, current_start_offset);
212 std::cout << "Mam rx_time: " << current_rx_time.get_real_secs() << std::endl;
213 }
214
215 return d_samples_consumed;
piotrd0bf1492014-02-05 17:27:32 +0100216 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700217
218 void
219 receiver_impl::fcch_search_handler(gr_complex *input, int noutput_items)
piotrd0bf1492014-02-05 17:27:32 +0100220 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700221 double freq_offset_tmp;
ptrkrysik58213792014-10-30 09:05:15 +0100222
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700223 /* Check if received samples is a FCCN burst */
224 if (!find_fcch_burst(input, noutput_items, freq_offset_tmp))
225 return;
piotr437f5462014-02-04 17:57:25 +0100226
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700227 /* We found it, compose a message */
228 pmt::pmt_t msg = pmt::make_tuple(
229 pmt::mp("freq_offset"),
230 pmt::from_double(freq_offset_tmp - d_freq_offset_setting),
231 pmt::mp("fcch_search")
232 );
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100233
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700234 /* Notify FCCH loop */
235 message_port_pub(pmt::mp("measurements"), msg);
236
237 /* Update current state */
238 d_state = sch_search;
piotrd0bf1492014-02-05 17:27:32 +0100239 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700240
241 void
242 receiver_impl::sch_search_handler(gr_complex *input, int noutput_items)
243 {
244 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR);
245 unsigned char burst_buf[BURST_SIZE];
246 int rc, t1, t2, t3;
247 int burst_start;
248
249 /* Wait until we get a SCH burst */
250 if (!reach_sch_burst(noutput_items))
251 return;
252
253 /* Get channel impulse response from it */
254 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]);
255
256 /* Detect bits using MLSE detection */
257 detect_burst(input, &channel_imp_resp[0], burst_start, burst_buf);
258
259 /* Attempt to decode BSIC and frame number */
260 rc = decode_sch(&burst_buf[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
261 if (rc) {
262 /**
263 * There is error in the SCH burst,
264 * go back to the FCCH search state
265 */
266 d_state = fcch_search;
267 return;
268 }
269
270 /* Set counter of bursts value */
271 d_burst_nr.set(t1, t2, t3, 0);
272 d_burst_nr++;
273
274 /* Consume samples up to the next guard period */
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200275 unsigned int to_consume = burst_start + BURST_SIZE * d_OSR + 4 * d_OSR;
276// consume_each(to_consume);
277 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700278
279 /* Update current state */
280 d_state = synchronized;
piotr437f5462014-02-04 17:57:25 +0100281 }
piotr437f5462014-02-04 17:57:25 +0100282
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700283 void
284 receiver_impl::synchronized_handler(gr_complex *input,
285 gr_vector_const_void_star &input_items, int noutput_items)
piotr437f5462014-02-04 17:57:25 +0100286 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700287 /**
288 * In this state receiver is synchronized and it processes
289 * bursts according to burst type for given burst number
290 */
291 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR);
292 unsigned int inputs_to_process = d_cell_allocation.size();
293 unsigned char output_binary[BURST_SIZE];
294 burst_type b_type;
295 int to_consume = 0;
296 int offset = 0;
piotr437f5462014-02-04 17:57:25 +0100297
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700298 if (d_process_uplink)
299 inputs_to_process *= 2;
piotr437f5462014-02-04 17:57:25 +0100300
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700301 /* Process all connected inputs */
302 for (int input_nr = 0; input_nr < inputs_to_process; input_nr++) {
303 input = (gr_complex *) input_items[input_nr];
304 double signal_pwr = 0;
305
306 for (int ii = GUARD_PERIOD; ii < TS_BITS; ii++)
307 signal_pwr += abs(input[ii]) * abs(input[ii]);
308
309 signal_pwr = signal_pwr / (TS_BITS);
310 d_signal_dbm = round(10 * log10(signal_pwr / 50));
311
312 if (input_nr == 0)
313 d_c0_signal_dbm = d_signal_dbm;
314
315 /* Get burst type for given burst number */
316 b_type = input_nr == 0 ?
317 d_channel_conf.get_burst_type(d_burst_nr) : normal_or_noise;
318
319 /* Process burst according to its type */
320 switch (b_type) {
321 case fcch_burst:
piotrd0bf1492014-02-05 17:27:32 +0100322 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700323 if (d_freq_offset_tag_in_fcch)
piotr437f5462014-02-04 17:57:25 +0100324 break;
325
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700326 /* Send all-zero sequence message */
327 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
piotr437f5462014-02-04 17:57:25 +0100328
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700329 /* Extract frequency offset */
330 const unsigned first_sample =
331 ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
332 const unsigned last_sample =
333 first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
334 double freq_offset_tmp =
335 compute_freq_offset(input, first_sample, last_sample);
piotr437f5462014-02-04 17:57:25 +0100336
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700337 /* Frequency correction loop */
338 pmt::pmt_t msg = pmt::make_tuple(
339 pmt::mp("freq_offset"),
340 pmt::from_double(freq_offset_tmp - d_freq_offset_setting),
341 pmt::mp("synchronized"));
342 message_port_pub(pmt::mp("measurements"), msg);
343
344 break;
345 }
346
347 case sch_burst:
348 {
349 int d_ncc, d_bcc;
350 int t1, t2, t3;
351 int rc;
352
353 /* Get channel impulse response */
354 d_c0_burst_start = get_sch_chan_imp_resp(input,
355 &channel_imp_resp[0]);
356
357 /* Perform MLSE detection */
358 detect_burst(input, &channel_imp_resp[0],
359 d_c0_burst_start, output_binary);
360
361 /* Compose a message with GSMTAP header and bits */
362 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200363 GSMTAP_BURST_SCH, input_nr, d_c0_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700364
365 /* Attempt to decode SCH burst */
366 rc = decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
367 if (rc) {
368 if (++d_failed_sch >= MAX_SCH_ERRORS) {
369 /* We have to resynchronize, change state */
370 d_state = fcch_search;
371
372 /* Frequency correction loop */
373 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),
374 pmt::from_double(0.0),pmt::mp("sync_loss"));
375 message_port_pub(pmt::mp("measurements"), msg);
piotr437f5462014-02-04 17:57:25 +0100376 }
377
378 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700379 }
piotr437f5462014-02-04 17:57:25 +0100380
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700381 /**
382 * Decoding was successful, now
383 * compute offset from burst_start,
384 * burst should start after a guard period.
385 */
386 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR);
387 to_consume += offset;
388 d_failed_sch = 0;
piotr437f5462014-02-04 17:57:25 +0100389
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700390 break;
piotrd0bf1492014-02-05 17:27:32 +0100391 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700392
393 case normal_burst:
394 {
395 float normal_corr_max;
396 /**
397 * Get channel impulse response for given
398 * training sequence number - d_bcc
399 */
400 d_c0_burst_start = get_norm_chan_imp_resp(input,
401 &channel_imp_resp[0], &normal_corr_max, d_bcc);
402
403 /* Perform MLSE detection */
404 detect_burst(input, &channel_imp_resp[0],
405 d_c0_burst_start, output_binary);
406
407 /* Compose a message with GSMTAP header and bits */
408 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200409 GSMTAP_BURST_NORMAL, input_nr, d_c0_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700410
411 break;
412 }
413
414 case dummy_or_normal:
415 {
416 unsigned int normal_burst_start, dummy_burst_start;
417 float dummy_corr_max, normal_corr_max;
418
419 dummy_burst_start = get_norm_chan_imp_resp(input,
420 &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
421 normal_burst_start = get_norm_chan_imp_resp(input,
422 &channel_imp_resp[0], &normal_corr_max, d_bcc);
423
424 if (normal_corr_max > dummy_corr_max) {
425 d_c0_burst_start = normal_burst_start;
426
427 /* Perform MLSE detection */
428 detect_burst(input, &channel_imp_resp[0],
429 normal_burst_start, output_binary);
430
431 /* Compose a message with GSMTAP header and bits */
432 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200433 GSMTAP_BURST_NORMAL, input_nr, normal_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700434 } else {
435 d_c0_burst_start = dummy_burst_start;
436
437 /* Compose a message with GSMTAP header and bits */
438 send_burst(d_burst_nr, dummy_burst,
Piotr Krysikdf978692017-09-27 21:58:24 +0200439 GSMTAP_BURST_DUMMY, input_nr, dummy_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700440 }
441
442 break;
443 }
444
445 case normal_or_noise:
446 {
447 std::vector<gr_complex> v(input, input + noutput_items);
448 float normal_corr_max = -1e6;
449 float normal_corr_max_tmp;
450 unsigned int burst_start;
451 int max_tn, tseq_num;
452
453 if (d_tseq_nums.size() == 0) {
454 /**
455 * There is no information about training sequence,
456 * however the receiver can detect it with use of a
457 * very simple algorithm based on finding
458 */
459 get_norm_chan_imp_resp(input, &channel_imp_resp[0],
460 &normal_corr_max, 0);
461
462 float ts_max = normal_corr_max;
463 int ts_max_num = 0;
464
465 for (int ss = 1; ss <= 7; ss++) {
466 get_norm_chan_imp_resp(input, &channel_imp_resp[0],
467 &normal_corr_max, ss);
468
469 if (ts_max < normal_corr_max) {
470 ts_max = normal_corr_max;
471 ts_max_num = ss;
472 }
473 }
474
475 d_tseq_nums.push_back(ts_max_num);
476 }
477
478 /* Choose proper training sequence number */
479 tseq_num = input_nr <= d_tseq_nums.size() ?
480 d_tseq_nums[input_nr - 1] : d_tseq_nums.back();
481
482 /* Get channel impulse response */
483 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0],
484 &normal_corr_max, tseq_num);
485
486 /* Perform MLSE detection */
487 detect_burst(input, &channel_imp_resp[0],
488 burst_start, output_binary);
489
490 /* Compose a message with GSMTAP header and bits */
Piotr Krysikdf978692017-09-27 21:58:24 +0200491 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr, burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700492
493 break;
494 }
495
496 case dummy:
497 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
498 break;
499
500 case rach_burst:
501 case empty:
502 /* Do nothing */
503 break;
504 }
505
506 if (input_nr == input_items.size() - 1) {
507 /* Go to the next burst */
508 d_burst_nr++;
509
510 /* Consume samples of the burst up to next guard period */
511 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset();
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200512// consume_each(to_consume);
513 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700514 }
515 }
516 }
517
518 bool
519 receiver_impl::find_fcch_burst(const gr_complex *input,
520 const int nitems, double &computed_freq_offset)
521 {
522 /* Circular buffer used to scan through signal to find */
523 boost::circular_buffer<float>
524 phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR);
525 boost::circular_buffer<float>::iterator buffer_iter;
526
527 float lowest_max_min_diff;
528 float phase_diff; /* Best match for FCCH burst */
529 float min_phase_diff;
530 float max_phase_diff;
531 double best_sum = 0;
532 gr_complex conjprod;
533 int start_pos;
534 int hit_count;
535 int miss_count;
536
537 int sample_number = 0;
538 int to_consume = 0;
539 bool result = false;
540 bool end = false;
541
542 /* Possible states of FCCH search algorithm */
543 enum states
544 {
545 init, /* initialize variables */
546 search, /* search for positive samples */
547 found_something, /* search for FCCH and the best position of it */
548 fcch_found, /* when FCCH was found */
549 search_fail /* when there is no FCCH in the input vector */
550 } fcch_search_state;
551
552 /* Set initial state */
553 fcch_search_state = init;
554
555 while (!end)
556 {
557 switch (fcch_search_state) {
558 case init:
559 {
560 hit_count = 0;
561 miss_count = 0;
562 start_pos = -1;
563 lowest_max_min_diff = 99999;
564 phase_diff_buffer.clear();
565
566 /* Change current state */
567 fcch_search_state = search;
568
569 break;
570 }
571
572 case search:
573 {
574 sample_number++;
575
576 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) {
577 /**
578 * If it isn't possible to find FCCH, because
579 * there is too few samples left to look into,
580 * don't do anything with those samples which are left
581 * and consume only those which were checked
582 */
583 to_consume = sample_number;
584 fcch_search_state = search_fail;
585 break;
586 }
587
588 phase_diff = compute_phase_diff(input[sample_number],
589 input[sample_number - 1]);
590
591 /**
592 * If a positive phase difference was found
593 * switch to state in which searches for FCCH
594 */
595 if (phase_diff > 0) {
596 to_consume = sample_number;
597 fcch_search_state = found_something;
598 } else {
599 fcch_search_state = search;
600 }
601
602 break;
603 }
604
605 case found_something:
606 {
607 if (phase_diff > 0)
608 hit_count++;
609 else
610 miss_count++;
611
612 if ((miss_count >= FCCH_MAX_MISSES * d_OSR)
613 && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
614 {
615 /* If miss_count exceeds limit before hit_count */
616 fcch_search_state = init;
617 continue;
618 }
619
620 if (((miss_count >= FCCH_MAX_MISSES * d_OSR)
621 && (hit_count > FCCH_HITS_NEEDED * d_OSR))
622 || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
623 {
624 /**
625 * If hit_count and miss_count exceeds
626 * limit then FCCH was found
627 */
628 fcch_search_state = fcch_found;
629 continue;
630 }
631
632 if ((miss_count < FCCH_MAX_MISSES * d_OSR)
633 && (hit_count > FCCH_HITS_NEEDED * d_OSR))
634 {
635 /**
636 * Find difference between minimal and maximal
637 * element in the buffer. For FCCH this value
638 * should be low. This part is searching for
639 * a region where this value is lowest.
640 */
641 min_phase_diff = *(min_element(phase_diff_buffer.begin(),
642 phase_diff_buffer.end()));
643 max_phase_diff = *(max_element(phase_diff_buffer.begin(),
644 phase_diff_buffer.end()));
645
646 if (lowest_max_min_diff > max_phase_diff - min_phase_diff) {
647 lowest_max_min_diff = max_phase_diff - min_phase_diff;
648 start_pos = sample_number - FCCH_HITS_NEEDED
649 * d_OSR - FCCH_MAX_MISSES * d_OSR;
650 best_sum = 0;
651
652 for (buffer_iter = phase_diff_buffer.begin();
653 buffer_iter != (phase_diff_buffer.end());
654 buffer_iter++) {
655 /* Store best value of phase offset sum */
656 best_sum += *buffer_iter - (M_PI / 2) / d_OSR;
657 }
658 }
659 }
660
661 /* If there is no single sample left to check */
662 if (++sample_number >= nitems) {
663 fcch_search_state = search_fail;
664 continue;
665 }
666
667 phase_diff = compute_phase_diff(input[sample_number],
668 input[sample_number-1]);
669 phase_diff_buffer.push_back(phase_diff);
670 fcch_search_state = found_something;
671
672 break;
673 }
piotrd0bf1492014-02-05 17:27:32 +0100674
675 case fcch_found:
676 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700677 /* Consume one FCCH burst */
678 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1;
679 d_fcch_start_pos = d_counter + start_pos;
piotrd0bf1492014-02-05 17:27:32 +0100680
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700681 /**
682 * Compute frequency offset
683 *
684 * 1625000.0 / 6 - GMSK symbol rate in GSM
685 */
686 double phase_offset = best_sum / FCCH_HITS_NEEDED;
687 double freq_offset = phase_offset * 1625000.0 / 6 / (2 * M_PI);
688 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100689
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700690 end = true;
691 result = true;
piotrd0bf1492014-02-05 17:27:32 +0100692
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700693 break;
piotrd0bf1492014-02-05 17:27:32 +0100694 }
piotr437f5462014-02-04 17:57:25 +0100695
piotrd0bf1492014-02-05 17:27:32 +0100696 case search_fail:
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700697 end = true;
698 result = false;
699 break;
piotr437f5462014-02-04 17:57:25 +0100700 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700701 }
702
703 d_counter += to_consume;
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200704// consume_each(to_consume);
705 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700706
707 return result;
piotr437f5462014-02-04 17:57:25 +0100708 }
709
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700710 double
711 receiver_impl::compute_freq_offset(const gr_complex * input,
712 unsigned first_sample, unsigned last_sample)
Piotr Krysik654d6522017-01-23 21:53:48 +0100713 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700714 double phase_sum = 0;
715 unsigned ii;
716
717 for (ii = first_sample; ii < last_sample; ii++)
718 {
719 double phase_diff = compute_phase_diff(input[ii],
720 input[ii-1]) - (M_PI / 2) / d_OSR;
Piotr Krysik654d6522017-01-23 21:53:48 +0100721 phase_sum += phase_diff;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700722 }
723
724 double phase_offset = phase_sum / (last_sample - first_sample);
725 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
726
727 return freq_offset;
Piotr Krysik654d6522017-01-23 21:53:48 +0100728 }
729
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700730 inline float
731 receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
piotrd0bf1492014-02-05 17:27:32 +0100732 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700733 gr_complex conjprod = val1 * conj(val2);
734 return fast_atan2f(imag(conjprod), real(conjprod));
piotrd0bf1492014-02-05 17:27:32 +0100735 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700736
737 bool
738 receiver_impl::reach_sch_burst(const int nitems)
piotrd0bf1492014-02-05 17:27:32 +0100739 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700740 /* It just consumes samples to get near to a SCH burst */
741 int to_consume = 0;
742 bool result = false;
743 unsigned sample_nr = d_fcch_start_pos
744 + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
745
746 /* Consume samples until d_counter will be equal to sample_nr */
747 if (d_counter < sample_nr) {
748 to_consume = d_counter + nitems >= sample_nr ?
749 sample_nr - d_counter : nitems;
750 } else {
piotr437f5462014-02-04 17:57:25 +0100751 to_consume = 0;
752 result = true;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700753 }
754
755 d_counter += to_consume;
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200756// consume_each(to_consume);
757 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700758
759 return result;
piotr437f5462014-02-04 17:57:25 +0100760 }
761
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700762 int
763 receiver_impl::get_sch_chan_imp_resp(const gr_complex *input,
764 gr_complex * chan_imp_resp)
piotr437f5462014-02-04 17:57:25 +0100765 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700766 std::vector<gr_complex> correlation_buffer;
767 std::vector<float> window_energy_buffer;
768 std::vector<float> power_buffer;
769
770 int chan_imp_resp_center = 0;
771 int strongest_window_nr;
772 int burst_start;
773 float energy = 0;
774
775 int len = (SYNC_POS + SYNC_SEARCH_RANGE) * d_OSR;
776 for (int ii = SYNC_POS * d_OSR; ii < len; ii++) {
777 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5],
778 N_SYNC_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100779 correlation_buffer.push_back(correlation);
780 power_buffer.push_back(std::pow(abs(correlation), 2));
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700781 }
782
783 /* Compute window energies */
784 std::vector<float>::iterator iter = power_buffer.begin();
785 while (iter != power_buffer.end()) {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100786 std::vector<float>::iterator iter_ii = iter;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700787 bool loop_end = false;
piotr437f5462014-02-04 17:57:25 +0100788 energy = 0;
789
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700790 for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++, iter_ii++) {
791 if (iter_ii == power_buffer.end()) {
792 loop_end = true;
piotrd0bf1492014-02-05 17:27:32 +0100793 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700794 }
795
796 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100797 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700798
799 if (loop_end)
800 break;
801
piotr437f5462014-02-04 17:57:25 +0100802 window_energy_buffer.push_back(energy);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700803 iter++;
804 }
piotr437f5462014-02-04 17:57:25 +0100805
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700806 strongest_window_nr = max_element(window_energy_buffer.begin(),
807 window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100808
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700809#if 0
810 d_channel_imp_resp.clear();
811#endif
812
813 float max_correlation = 0;
814 for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++) {
piotr437f5462014-02-04 17:57:25 +0100815 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700816 if (abs(correlation) > max_correlation) {
817 chan_imp_resp_center = ii;
818 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100819 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700820
821#if 0
822 d_channel_imp_resp.push_back(correlation);
823#endif
824
piotr437f5462014-02-04 17:57:25 +0100825 chan_imp_resp[ii] = correlation;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700826 }
827
828 burst_start = strongest_window_nr + chan_imp_resp_center
829 - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
830 return burst_start;
piotr437f5462014-02-04 17:57:25 +0100831 }
832
833
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700834 void
835 receiver_impl::detect_burst(const gr_complex * input,
836 gr_complex * chan_imp_resp, int burst_start,
837 unsigned char * output_binary)
piotr437f5462014-02-04 17:57:25 +0100838 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700839 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH * d_OSR);
840 unsigned int stop_states[2] = {4, 12};
841 gr_complex filtered_burst[BURST_SIZE];
842 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
843 float output[BURST_SIZE];
844 int start_state = 3;
845
846 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
847 for (int ii = 0; ii < d_chan_imp_length; ii++)
piotr437f5462014-02-04 17:57:25 +0100848 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700849
850 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp,
851 d_chan_imp_length * d_OSR, filtered_burst);
852
853 viterbi_detector(filtered_burst, BURST_SIZE, rhh,
854 start_state, stop_states, 2, output);
855
856 for (int i = 0; i < BURST_SIZE; i++)
857 output_binary[i] = output[i] > 0;
piotr437f5462014-02-04 17:57:25 +0100858 }
859
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700860 void
861 receiver_impl::gmsk_mapper(const unsigned char * input,
862 int nitems, gr_complex * gmsk_output, gr_complex start_point)
piotr437f5462014-02-04 17:57:25 +0100863 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700864 gr_complex j = gr_complex(0.0, 1.0);
865 gmsk_output[0] = start_point;
piotr437f5462014-02-04 17:57:25 +0100866
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700867 int previous_symbol = 2 * input[0] - 1;
868 int current_symbol;
869 int encoded_symbol;
piotr437f5462014-02-04 17:57:25 +0100870
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700871 for (int i = 1; i < nitems; i++) {
872 /* Change bits representation to NRZ */
piotr437f5462014-02-04 17:57:25 +0100873 current_symbol = 2 * input[i] - 1;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700874
875 /* Differentially encode */
piotr437f5462014-02-04 17:57:25 +0100876 encoded_symbol = current_symbol * previous_symbol;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700877
878 /* And do GMSK mapping */
879 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0)
880 * gmsk_output[i-1];
881
piotr437f5462014-02-04 17:57:25 +0100882 previous_symbol = current_symbol;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700883 }
piotr437f5462014-02-04 17:57:25 +0100884 }
885
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700886 gr_complex
887 receiver_impl::correlate_sequence(const gr_complex * sequence,
888 int length, const gr_complex * input)
piotr437f5462014-02-04 17:57:25 +0100889 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700890 gr_complex result(0.0, 0.0);
891
892 for (int ii = 0; ii < length; ii++)
893 result += sequence[ii] * conj(input[ii * d_OSR]);
894
895 return result / gr_complex(length, 0);
896 }
897
898 /* Computes autocorrelation for positive arguments */
899 inline void
900 receiver_impl::autocorrelation(const gr_complex * input,
901 gr_complex * out, int nitems)
902 {
903 for (int k = nitems - 1; k >= 0; k--) {
piotr437f5462014-02-04 17:57:25 +0100904 out[k] = gr_complex(0, 0);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700905 for (int i = k; i < nitems; i++)
906 out[k] += input[i] * conj(input[i - k]);
907 }
piotr437f5462014-02-04 17:57:25 +0100908 }
909
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700910 inline void
911 receiver_impl::mafi(const gr_complex * input, int nitems,
912 gr_complex * filter, int filter_length, gr_complex * output)
piotr437f5462014-02-04 17:57:25 +0100913 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700914 for (int n = 0; n < nitems; n++) {
915 int a = n * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100916 output[n] = 0;
piotr437f5462014-02-04 17:57:25 +0100917
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700918 for (int ii = 0; ii < filter_length; ii++) {
919 if ((a + ii) >= nitems * d_OSR)
piotrd0bf1492014-02-05 17:27:32 +0100920 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700921
922 output[n] += input[a + ii] * filter[ii];
piotr437f5462014-02-04 17:57:25 +0100923 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700924 }
925 }
926
927 /* Especially computations of strongest_window_nr */
928 int
929 receiver_impl::get_norm_chan_imp_resp(const gr_complex *input,
930 gr_complex *chan_imp_resp, float *corr_max, int bcc)
931 {
932 std::vector<gr_complex> correlation_buffer;
933 std::vector<float> window_energy_buffer;
934 std::vector<float> power_buffer;
935
936 int search_center = (int) (TRAIN_POS + GUARD_PERIOD) * d_OSR;
937 int search_start_pos = search_center + 1 - 5 * d_OSR;
938 int search_stop_pos = search_center
939 + d_chan_imp_length * d_OSR + 5 * d_OSR;
940
941 for (int ii = search_start_pos; ii < search_stop_pos; ii++) {
942 gr_complex correlation = correlate_sequence(
943 &d_norm_training_seq[bcc][TRAIN_BEGINNING],
944 N_TRAIN_BITS - 10, &input[ii]);
945 correlation_buffer.push_back(correlation);
946 power_buffer.push_back(std::pow(abs(correlation), 2));
947 }
948
949#if 0
950 plot(power_buffer);
951#endif
952
953 /* Compute window energies */
954 std::vector<float>::iterator iter = power_buffer.begin();
955 while (iter != power_buffer.end()) {
956 std::vector<float>::iterator iter_ii = iter;
957 bool loop_end = false;
958 float energy = 0;
959
960 int len = d_chan_imp_length * d_OSR;
961 for (int ii = 0; ii < len; ii++, iter_ii++) {
962 if (iter_ii == power_buffer.end()) {
963 loop_end = true;
964 break;
965 }
966
967 energy += (*iter_ii);
968 }
969
970 if (loop_end)
971 break;
piotr437f5462014-02-04 17:57:25 +0100972
973 window_energy_buffer.push_back(energy);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700974 iter++;
975 }
piotr437f5462014-02-04 17:57:25 +0100976
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700977 /* Calculate the strongest window number */
978 int strongest_window_nr = max_element(window_energy_buffer.begin(),
979 window_energy_buffer.end() - d_chan_imp_length * d_OSR)
980 - window_energy_buffer.begin();
981
982 if (strongest_window_nr < 0)
983 strongest_window_nr = 0;
984
985 float max_correlation = 0;
986 for (int ii = 0; ii < d_chan_imp_length * d_OSR; ii++) {
piotr437f5462014-02-04 17:57:25 +0100987 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100988 if (abs(correlation) > max_correlation)
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700989 max_correlation = abs(correlation);
990
991#if 0
992 d_channel_imp_resp.push_back(correlation);
993#endif
994
piotr437f5462014-02-04 17:57:25 +0100995 chan_imp_resp[ii] = correlation;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700996 }
997
998 *corr_max = max_correlation;
999
1000 /**
1001 * Compute first sample position, which corresponds
1002 * to the first sample of the impulse response
1003 */
1004 return search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR;
piotr437f5462014-02-04 17:57:25 +01001005 }
1006
1007
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001008 void
1009 receiver_impl::send_burst(burst_counter burst_nr,
1010 const unsigned char * burst_binary, uint8_t burst_type,
Piotr Krysikdf978692017-09-27 21:58:24 +02001011 unsigned int input_nr, unsigned int burst_start)
ptrkrysik380dea82015-08-06 10:11:58 +02001012 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001013 /* Buffer for GSMTAP header and burst */
1014 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
1015 uint32_t frame_number;
1016 uint16_t arfcn;
1017 uint8_t tn;
ptrkrysik617ba032014-11-21 10:11:05 +01001018
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001019 /* Set pointers to GSMTAP header and burst inside buffer */
1020 struct gsmtap_hdr *tap_header = (struct gsmtap_hdr *) buf;
1021 uint8_t *burst = buf + sizeof(gsmtap_hdr);
1022
1023 tap_header->version = GSMTAP_VERSION;
1024 tap_header->hdr_len = sizeof(gsmtap_hdr) / 4;
1025 tap_header->type = GSMTAP_TYPE_UM_BURST;
1026 tap_header->sub_type = burst_type;
1027
1028 bool dl_burst = !(input_nr >= d_cell_allocation.size());
1029 if (dl_burst) {
1030 tn = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
1031 frame_number = htobe32(d_burst_nr.get_frame_nr());
1032 arfcn = htobe16(d_cell_allocation[input_nr]);
1033 } else {
1034 input_nr -= d_cell_allocation.size();
1035 tn = static_cast<uint8_t>
1036 (d_burst_nr.subtract_timeslots(3).get_timeslot_nr());
1037 frame_number = htobe32(
1038 d_burst_nr.subtract_timeslots(3).get_frame_nr());
1039 arfcn = htobe16(
1040 d_cell_allocation[input_nr] | GSMTAP_ARFCN_F_UPLINK);
1041 }
1042
1043 tap_header->frame_number = frame_number;
1044 tap_header->timeslot = tn;
1045 tap_header->arfcn = arfcn;
1046
1047 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
1048 tap_header->snr_db = 0; /* FIXME: Can we calculate this? */
1049
Piotr Krysikdf978692017-09-27 21:58:24 +02001050 pmt::pmt_t pdu_header = pmt::make_dict();
1051
1052 /* Add timestamp of the first sample - if available */
1053 if(d_rx_time_received) {
1054 time_spec_t time_spec_of_first_sample = d_time_samp_ref.offset_to_time(nitems_read(0)+burst_start);
1055 uint64_t full = time_spec_of_first_sample.get_full_secs();
1056 double frac = time_spec_of_first_sample.get_frac_secs();
Piotr Krysik96f2ea72017-10-16 15:47:08 +02001057 pdu_header =
1058 pmt::dict_add(pdu_header, pmt::mp("fn_time"),
1059 pmt::cons(
1060 pmt::cons(pmt::from_uint64(be32toh(frame_number)), pmt::from_uint64(tn)),
1061 pmt::cons(pmt::from_uint64(full), pmt::from_double(frac))));
Piotr Krysikdf978692017-09-27 21:58:24 +02001062 }
1063
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001064 /* Copy burst to the buffer */
1065 memcpy(burst, burst_binary, BURST_SIZE);
1066
1067 /* Allocate a new message */
1068 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
Piotr Krysik96f2ea72017-10-16 15:47:08 +02001069 pmt::pmt_t msg = pmt::cons(pdu_header, blob);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001070
1071 /* Send message */
1072 if (input_nr == 0)
ptrkrysike518bbf2014-11-06 14:50:59 +01001073 message_port_pub(pmt::mp("C0"), msg);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001074 else
ptrkrysike518bbf2014-11-06 14:50:59 +01001075 message_port_pub(pmt::mp("CX"), msg);
1076 }
piotr6d152d92014-02-21 00:02:44 +01001077
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001078 void
1079 receiver_impl::configure_receiver(void)
1080 {
1081 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
1082 d_channel_conf.set_burst_types(TIMESLOT0, TEST51,
1083 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1084 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES,
1085 sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
1086 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES,
1087 sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
1088 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES,
1089 sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +01001090
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001091 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
1092 d_channel_conf.set_burst_types(TIMESLOT1, TEST51,
1093 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001094
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001095 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
1096 d_channel_conf.set_burst_types(TIMESLOT2, TEST51,
1097 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001098
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001099 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
1100 d_channel_conf.set_burst_types(TIMESLOT3, TEST51,
1101 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
ptrkrysike518bbf2014-11-06 14:50:59 +01001102
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001103 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
1104 d_channel_conf.set_burst_types(TIMESLOT4, TEST51,
1105 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrf2b6a1b2014-08-04 11:28:59 +02001106
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001107 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
1108 d_channel_conf.set_burst_types(TIMESLOT5, TEST51,
1109 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001110
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001111 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
1112 d_channel_conf.set_burst_types(TIMESLOT6, TEST51,
1113 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1114
1115 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
1116 d_channel_conf.set_burst_types(TIMESLOT7, TEST51,
1117 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1118 }
1119
1120 void
1121 receiver_impl::set_cell_allocation(
1122 const std::vector<int> &cell_allocation)
1123 {
1124 d_cell_allocation = cell_allocation;
1125 }
1126
1127 void
1128 receiver_impl::set_tseq_nums(const std::vector<int> &tseq_nums)
1129 {
1130 d_tseq_nums = tseq_nums;
1131 }
1132
1133 void
1134 receiver_impl::reset(void)
1135 {
1136 d_state = fcch_search;
1137 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001138 } /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +01001139} /* namespace gr */