blob: db7d9dd026dc8c3b5a5b2c4bd47bd2d4e5f631d8 [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 Krysikdf978692017-09-27 21:58:24 +020077 d_rx_time_received(false),
78 d_time_samp_ref(GSM_SYMBOL_RATE * osr),
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070079 d_OSR(osr),
80 d_process_uplink(process_uplink),
81 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
Piotr Krysikdf978692017-09-27 21:58:24 +020082 d_counter(0), //TODO: use nitems_read instead of d_counter
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070083 d_fcch_start_pos(0),
84 d_freq_offset_setting(0),
85 d_state(fcch_search),
86 d_burst_nr(osr),
87 d_failed_sch(0),
88 d_signal_dbm(-120),
89 d_tseq_nums(tseq_nums),
90 d_cell_allocation(cell_allocation),
91 d_last_time(0.0)
ptrkrysik32c21162015-04-04 14:01:52 +020092 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070093 /**
94 * Don't send samples to the receiver
95 * until there are at least samples for one
96 */
97 set_output_multiple(floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR));
98
99 /**
100 * Prepare SCH sequence bits
101 *
102 * (TS_BITS + 2 * GUARD_PERIOD)
103 * Burst and two guard periods
104 * (one guard period is an arbitrary overlap)
105 */
106 gmsk_mapper(SYNC_BITS, N_SYNC_BITS,
107 d_sch_training_seq, gr_complex(0.0, -1.0));
108
109 /* Prepare bits of training sequences */
110 for (int i = 0; i < TRAIN_SEQ_NUM; i++) {
111 /**
112 * If first bit of the sequence is 0
113 * => first symbol is 1, else -1
114 */
115 gr_complex startpoint = train_seq[i][0] == 0 ?
116 gr_complex(1.0, 0.0) : gr_complex(-1.0, 0.0);
117 gmsk_mapper(train_seq[i], N_TRAIN_BITS,
118 d_norm_training_seq[i], startpoint);
119 }
120
121 /* Register output ports */
122 message_port_register_out(pmt::mp("C0"));
123 message_port_register_out(pmt::mp("CX"));
124 message_port_register_out(pmt::mp("measurements"));
125
126 /**
127 * Configure the receiver,
128 * i.e. tell it where to find which burst type
129 */
130 configure_receiver();
131 }
132
133 /* Our virtual destructor */
134 receiver_impl::~receiver_impl() {}
135
136 int
137 receiver_impl::work(
138 int noutput_items,
139 gr_vector_const_void_star &input_items,
140 gr_vector_void_star &output_items)
141 {
142 gr_complex *input = (gr_complex *) input_items[0];
143 uint64_t start = nitems_read(0);
144 uint64_t stop = start + noutput_items;
145 d_freq_offset_tag_in_fcch = false;
146
147#if 0
148 /* FIXME: jak zrobić to rzutowanie poprawnie */
149 std::vector<const gr_complex *> iii =
150 (std::vector<const gr_complex *>) input_items;
151#endif
Piotr Krysikdf978692017-09-27 21:58:24 +0200152
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700153
154 /* Time synchronization loop */
155 float current_time =
156 static_cast<float>(start / (GSM_SYMBOL_RATE * d_OSR));
157 if ((current_time - d_last_time) > 0.1) {
158 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),
159 pmt::from_double(current_time));
ptrkrysik32c21162015-04-04 14:01:52 +0200160 message_port_pub(pmt::mp("measurements"), msg);
161 d_last_time = current_time;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700162 }
ptrkrysik32c21162015-04-04 14:01:52 +0200163
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700164 /* Frequency correction loop */
165 std::vector<tag_t> freq_offset_tags;
166 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
167 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
168
169 if (!freq_offset_tags.empty()) {
piotr4089c1a2014-08-06 14:10:56 +0200170 tag_t freq_offset_tag = freq_offset_tags[0];
Piotr Krysik43af70d2016-07-20 21:37:24 +0200171 uint64_t tag_offset = freq_offset_tag.offset - start;
Piotr Krysik43af70d2016-07-20 21:37:24 +0200172 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
piotr4089c1a2014-08-06 14:10:56 +0200173
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700174 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
175 if (d_state == synchronized && b_type == fcch_burst){
176 uint64_t last_sample_nr =
177 ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
178 d_freq_offset_tag_in_fcch = tag_offset < last_sample_nr;
piotrd0bf1492014-02-05 17:27:32 +0100179 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700180 }
Piotr Krysikdf978692017-09-27 21:58:24 +0200181
182 /* Obtaining current time with use of rx_time tag provided i.e. by UHD devices */
183 /* And storing it in time_sample_ref for sample number to time conversion */
184 std::vector<tag_t> rx_time_tags;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700185
Piotr Krysikdf978692017-09-27 21:58:24 +0200186 get_tags_in_window(rx_time_tags, 0, 0, noutput_items, pmt::string_to_symbol("rx_time"));
187 if(!rx_time_tags.empty()){
188 d_rx_time_received = true;
189 tag_t rx_time_tag = *(rx_time_tags.begin());
190
191 uint64_t rx_time_full_part = to_uint64(tuple_ref(rx_time_tag.value,0));
192 double rx_time_frac_part = to_double(tuple_ref(rx_time_tag.value,1));
193
194 time_spec_t current_rx_time = time_spec_t(rx_time_full_part, rx_time_frac_part);
195 uint64_t current_start_offset = rx_time_tag.offset;
196 d_time_samp_ref.update(current_rx_time, current_start_offset);
197// std::cout << "Mam rx_time: " << current_rx_time.get_real_secs() << std::endl;
198 }
199
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700200 /* Main state machine */
201 switch (d_state) {
202 case fcch_search:
203 fcch_search_handler(input, noutput_items);
piotrd0bf1492014-02-05 17:27:32 +0100204 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700205 case sch_search:
206 sch_search_handler(input, noutput_items);
piotrd0bf1492014-02-05 17:27:32 +0100207 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700208 case synchronized:
209 synchronized_handler(input, input_items, noutput_items);
210 break;
211 }
212
213 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100214 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700215
216 void
217 receiver_impl::fcch_search_handler(gr_complex *input, int noutput_items)
piotrd0bf1492014-02-05 17:27:32 +0100218 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700219 double freq_offset_tmp;
ptrkrysik58213792014-10-30 09:05:15 +0100220
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700221 /* Check if received samples is a FCCN burst */
222 if (!find_fcch_burst(input, noutput_items, freq_offset_tmp))
223 return;
piotr437f5462014-02-04 17:57:25 +0100224
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700225 /* We found it, compose a message */
226 pmt::pmt_t msg = pmt::make_tuple(
227 pmt::mp("freq_offset"),
228 pmt::from_double(freq_offset_tmp - d_freq_offset_setting),
229 pmt::mp("fcch_search")
230 );
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100231
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700232 /* Notify FCCH loop */
233 message_port_pub(pmt::mp("measurements"), msg);
234
235 /* Update current state */
236 d_state = sch_search;
piotrd0bf1492014-02-05 17:27:32 +0100237 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700238
239 void
240 receiver_impl::sch_search_handler(gr_complex *input, int noutput_items)
241 {
242 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR);
243 unsigned char burst_buf[BURST_SIZE];
244 int rc, t1, t2, t3;
245 int burst_start;
246
247 /* Wait until we get a SCH burst */
248 if (!reach_sch_burst(noutput_items))
249 return;
250
251 /* Get channel impulse response from it */
252 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]);
253
254 /* Detect bits using MLSE detection */
255 detect_burst(input, &channel_imp_resp[0], burst_start, burst_buf);
256
257 /* Attempt to decode BSIC and frame number */
258 rc = decode_sch(&burst_buf[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
259 if (rc) {
260 /**
261 * There is error in the SCH burst,
262 * go back to the FCCH search state
263 */
264 d_state = fcch_search;
265 return;
266 }
267
268 /* Set counter of bursts value */
269 d_burst_nr.set(t1, t2, t3, 0);
270 d_burst_nr++;
271
272 /* Consume samples up to the next guard period */
273 consume_each(burst_start + BURST_SIZE * d_OSR + 4 * d_OSR);
274
275 /* Update current state */
276 d_state = synchronized;
piotr437f5462014-02-04 17:57:25 +0100277 }
piotr437f5462014-02-04 17:57:25 +0100278
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700279 void
280 receiver_impl::synchronized_handler(gr_complex *input,
281 gr_vector_const_void_star &input_items, int noutput_items)
piotr437f5462014-02-04 17:57:25 +0100282 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700283 /**
284 * In this state receiver is synchronized and it processes
285 * bursts according to burst type for given burst number
286 */
287 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR);
288 unsigned int inputs_to_process = d_cell_allocation.size();
289 unsigned char output_binary[BURST_SIZE];
290 burst_type b_type;
291 int to_consume = 0;
292 int offset = 0;
piotr437f5462014-02-04 17:57:25 +0100293
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700294 if (d_process_uplink)
295 inputs_to_process *= 2;
piotr437f5462014-02-04 17:57:25 +0100296
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700297 /* Process all connected inputs */
298 for (int input_nr = 0; input_nr < inputs_to_process; input_nr++) {
299 input = (gr_complex *) input_items[input_nr];
300 double signal_pwr = 0;
301
302 for (int ii = GUARD_PERIOD; ii < TS_BITS; ii++)
303 signal_pwr += abs(input[ii]) * abs(input[ii]);
304
305 signal_pwr = signal_pwr / (TS_BITS);
306 d_signal_dbm = round(10 * log10(signal_pwr / 50));
307
308 if (input_nr == 0)
309 d_c0_signal_dbm = d_signal_dbm;
310
311 /* Get burst type for given burst number */
312 b_type = input_nr == 0 ?
313 d_channel_conf.get_burst_type(d_burst_nr) : normal_or_noise;
314
315 /* Process burst according to its type */
316 switch (b_type) {
317 case fcch_burst:
piotrd0bf1492014-02-05 17:27:32 +0100318 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700319 if (d_freq_offset_tag_in_fcch)
piotr437f5462014-02-04 17:57:25 +0100320 break;
321
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700322 /* Send all-zero sequence message */
323 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
piotr437f5462014-02-04 17:57:25 +0100324
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700325 /* Extract frequency offset */
326 const unsigned first_sample =
327 ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
328 const unsigned last_sample =
329 first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
330 double freq_offset_tmp =
331 compute_freq_offset(input, first_sample, last_sample);
piotr437f5462014-02-04 17:57:25 +0100332
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700333 /* Frequency correction loop */
334 pmt::pmt_t msg = pmt::make_tuple(
335 pmt::mp("freq_offset"),
336 pmt::from_double(freq_offset_tmp - d_freq_offset_setting),
337 pmt::mp("synchronized"));
338 message_port_pub(pmt::mp("measurements"), msg);
339
340 break;
341 }
342
343 case sch_burst:
344 {
345 int d_ncc, d_bcc;
346 int t1, t2, t3;
347 int rc;
348
349 /* Get channel impulse response */
350 d_c0_burst_start = get_sch_chan_imp_resp(input,
351 &channel_imp_resp[0]);
352
353 /* Perform MLSE detection */
354 detect_burst(input, &channel_imp_resp[0],
355 d_c0_burst_start, output_binary);
356
357 /* Compose a message with GSMTAP header and bits */
358 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200359 GSMTAP_BURST_SCH, input_nr, d_c0_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700360
361 /* Attempt to decode SCH burst */
362 rc = decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
363 if (rc) {
364 if (++d_failed_sch >= MAX_SCH_ERRORS) {
365 /* We have to resynchronize, change state */
366 d_state = fcch_search;
367
368 /* Frequency correction loop */
369 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),
370 pmt::from_double(0.0),pmt::mp("sync_loss"));
371 message_port_pub(pmt::mp("measurements"), msg);
piotr437f5462014-02-04 17:57:25 +0100372 }
373
374 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700375 }
piotr437f5462014-02-04 17:57:25 +0100376
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700377 /**
378 * Decoding was successful, now
379 * compute offset from burst_start,
380 * burst should start after a guard period.
381 */
382 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR);
383 to_consume += offset;
384 d_failed_sch = 0;
piotr437f5462014-02-04 17:57:25 +0100385
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700386 break;
piotrd0bf1492014-02-05 17:27:32 +0100387 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700388
389 case normal_burst:
390 {
391 float normal_corr_max;
392 /**
393 * Get channel impulse response for given
394 * training sequence number - d_bcc
395 */
396 d_c0_burst_start = get_norm_chan_imp_resp(input,
397 &channel_imp_resp[0], &normal_corr_max, d_bcc);
398
399 /* Perform MLSE detection */
400 detect_burst(input, &channel_imp_resp[0],
401 d_c0_burst_start, output_binary);
402
403 /* Compose a message with GSMTAP header and bits */
404 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200405 GSMTAP_BURST_NORMAL, input_nr, d_c0_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700406
407 break;
408 }
409
410 case dummy_or_normal:
411 {
412 unsigned int normal_burst_start, dummy_burst_start;
413 float dummy_corr_max, normal_corr_max;
414
415 dummy_burst_start = get_norm_chan_imp_resp(input,
416 &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
417 normal_burst_start = get_norm_chan_imp_resp(input,
418 &channel_imp_resp[0], &normal_corr_max, d_bcc);
419
420 if (normal_corr_max > dummy_corr_max) {
421 d_c0_burst_start = normal_burst_start;
422
423 /* Perform MLSE detection */
424 detect_burst(input, &channel_imp_resp[0],
425 normal_burst_start, output_binary);
426
427 /* Compose a message with GSMTAP header and bits */
428 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200429 GSMTAP_BURST_NORMAL, input_nr, normal_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700430 } else {
431 d_c0_burst_start = dummy_burst_start;
432
433 /* Compose a message with GSMTAP header and bits */
434 send_burst(d_burst_nr, dummy_burst,
Piotr Krysikdf978692017-09-27 21:58:24 +0200435 GSMTAP_BURST_DUMMY, input_nr, dummy_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700436 }
437
438 break;
439 }
440
441 case normal_or_noise:
442 {
443 std::vector<gr_complex> v(input, input + noutput_items);
444 float normal_corr_max = -1e6;
445 float normal_corr_max_tmp;
446 unsigned int burst_start;
447 int max_tn, tseq_num;
448
449 if (d_tseq_nums.size() == 0) {
450 /**
451 * There is no information about training sequence,
452 * however the receiver can detect it with use of a
453 * very simple algorithm based on finding
454 */
455 get_norm_chan_imp_resp(input, &channel_imp_resp[0],
456 &normal_corr_max, 0);
457
458 float ts_max = normal_corr_max;
459 int ts_max_num = 0;
460
461 for (int ss = 1; ss <= 7; ss++) {
462 get_norm_chan_imp_resp(input, &channel_imp_resp[0],
463 &normal_corr_max, ss);
464
465 if (ts_max < normal_corr_max) {
466 ts_max = normal_corr_max;
467 ts_max_num = ss;
468 }
469 }
470
471 d_tseq_nums.push_back(ts_max_num);
472 }
473
474 /* Choose proper training sequence number */
475 tseq_num = input_nr <= d_tseq_nums.size() ?
476 d_tseq_nums[input_nr - 1] : d_tseq_nums.back();
477
478 /* Get channel impulse response */
479 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0],
480 &normal_corr_max, tseq_num);
481
482 /* Perform MLSE detection */
483 detect_burst(input, &channel_imp_resp[0],
484 burst_start, output_binary);
485
486 /* Compose a message with GSMTAP header and bits */
Piotr Krysikdf978692017-09-27 21:58:24 +0200487 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr, burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700488
489 break;
490 }
491
492 case dummy:
493 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
494 break;
495
496 case rach_burst:
497 case empty:
498 /* Do nothing */
499 break;
500 }
501
502 if (input_nr == input_items.size() - 1) {
503 /* Go to the next burst */
504 d_burst_nr++;
505
506 /* Consume samples of the burst up to next guard period */
507 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset();
508 consume_each(to_consume);
509 }
510 }
511 }
512
513 bool
514 receiver_impl::find_fcch_burst(const gr_complex *input,
515 const int nitems, double &computed_freq_offset)
516 {
517 /* Circular buffer used to scan through signal to find */
518 boost::circular_buffer<float>
519 phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR);
520 boost::circular_buffer<float>::iterator buffer_iter;
521
522 float lowest_max_min_diff;
523 float phase_diff; /* Best match for FCCH burst */
524 float min_phase_diff;
525 float max_phase_diff;
526 double best_sum = 0;
527 gr_complex conjprod;
528 int start_pos;
529 int hit_count;
530 int miss_count;
531
532 int sample_number = 0;
533 int to_consume = 0;
534 bool result = false;
535 bool end = false;
536
537 /* Possible states of FCCH search algorithm */
538 enum states
539 {
540 init, /* initialize variables */
541 search, /* search for positive samples */
542 found_something, /* search for FCCH and the best position of it */
543 fcch_found, /* when FCCH was found */
544 search_fail /* when there is no FCCH in the input vector */
545 } fcch_search_state;
546
547 /* Set initial state */
548 fcch_search_state = init;
549
550 while (!end)
551 {
552 switch (fcch_search_state) {
553 case init:
554 {
555 hit_count = 0;
556 miss_count = 0;
557 start_pos = -1;
558 lowest_max_min_diff = 99999;
559 phase_diff_buffer.clear();
560
561 /* Change current state */
562 fcch_search_state = search;
563
564 break;
565 }
566
567 case search:
568 {
569 sample_number++;
570
571 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) {
572 /**
573 * If it isn't possible to find FCCH, because
574 * there is too few samples left to look into,
575 * don't do anything with those samples which are left
576 * and consume only those which were checked
577 */
578 to_consume = sample_number;
579 fcch_search_state = search_fail;
580 break;
581 }
582
583 phase_diff = compute_phase_diff(input[sample_number],
584 input[sample_number - 1]);
585
586 /**
587 * If a positive phase difference was found
588 * switch to state in which searches for FCCH
589 */
590 if (phase_diff > 0) {
591 to_consume = sample_number;
592 fcch_search_state = found_something;
593 } else {
594 fcch_search_state = search;
595 }
596
597 break;
598 }
599
600 case found_something:
601 {
602 if (phase_diff > 0)
603 hit_count++;
604 else
605 miss_count++;
606
607 if ((miss_count >= FCCH_MAX_MISSES * d_OSR)
608 && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
609 {
610 /* If miss_count exceeds limit before hit_count */
611 fcch_search_state = init;
612 continue;
613 }
614
615 if (((miss_count >= FCCH_MAX_MISSES * d_OSR)
616 && (hit_count > FCCH_HITS_NEEDED * d_OSR))
617 || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
618 {
619 /**
620 * If hit_count and miss_count exceeds
621 * limit then FCCH was found
622 */
623 fcch_search_state = fcch_found;
624 continue;
625 }
626
627 if ((miss_count < FCCH_MAX_MISSES * d_OSR)
628 && (hit_count > FCCH_HITS_NEEDED * d_OSR))
629 {
630 /**
631 * Find difference between minimal and maximal
632 * element in the buffer. For FCCH this value
633 * should be low. This part is searching for
634 * a region where this value is lowest.
635 */
636 min_phase_diff = *(min_element(phase_diff_buffer.begin(),
637 phase_diff_buffer.end()));
638 max_phase_diff = *(max_element(phase_diff_buffer.begin(),
639 phase_diff_buffer.end()));
640
641 if (lowest_max_min_diff > max_phase_diff - min_phase_diff) {
642 lowest_max_min_diff = max_phase_diff - min_phase_diff;
643 start_pos = sample_number - FCCH_HITS_NEEDED
644 * d_OSR - FCCH_MAX_MISSES * d_OSR;
645 best_sum = 0;
646
647 for (buffer_iter = phase_diff_buffer.begin();
648 buffer_iter != (phase_diff_buffer.end());
649 buffer_iter++) {
650 /* Store best value of phase offset sum */
651 best_sum += *buffer_iter - (M_PI / 2) / d_OSR;
652 }
653 }
654 }
655
656 /* If there is no single sample left to check */
657 if (++sample_number >= nitems) {
658 fcch_search_state = search_fail;
659 continue;
660 }
661
662 phase_diff = compute_phase_diff(input[sample_number],
663 input[sample_number-1]);
664 phase_diff_buffer.push_back(phase_diff);
665 fcch_search_state = found_something;
666
667 break;
668 }
piotrd0bf1492014-02-05 17:27:32 +0100669
670 case fcch_found:
671 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700672 /* Consume one FCCH burst */
673 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1;
674 d_fcch_start_pos = d_counter + start_pos;
piotrd0bf1492014-02-05 17:27:32 +0100675
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700676 /**
677 * Compute frequency offset
678 *
679 * 1625000.0 / 6 - GMSK symbol rate in GSM
680 */
681 double phase_offset = best_sum / FCCH_HITS_NEEDED;
682 double freq_offset = phase_offset * 1625000.0 / 6 / (2 * M_PI);
683 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100684
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700685 end = true;
686 result = true;
piotrd0bf1492014-02-05 17:27:32 +0100687
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700688 break;
piotrd0bf1492014-02-05 17:27:32 +0100689 }
piotr437f5462014-02-04 17:57:25 +0100690
piotrd0bf1492014-02-05 17:27:32 +0100691 case search_fail:
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700692 end = true;
693 result = false;
694 break;
piotr437f5462014-02-04 17:57:25 +0100695 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700696 }
697
698 d_counter += to_consume;
699 consume_each(to_consume);
700
701 return result;
piotr437f5462014-02-04 17:57:25 +0100702 }
703
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700704 double
705 receiver_impl::compute_freq_offset(const gr_complex * input,
706 unsigned first_sample, unsigned last_sample)
Piotr Krysik654d6522017-01-23 21:53:48 +0100707 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700708 double phase_sum = 0;
709 unsigned ii;
710
711 for (ii = first_sample; ii < last_sample; ii++)
712 {
713 double phase_diff = compute_phase_diff(input[ii],
714 input[ii-1]) - (M_PI / 2) / d_OSR;
Piotr Krysik654d6522017-01-23 21:53:48 +0100715 phase_sum += phase_diff;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700716 }
717
718 double phase_offset = phase_sum / (last_sample - first_sample);
719 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
720
721 return freq_offset;
Piotr Krysik654d6522017-01-23 21:53:48 +0100722 }
723
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700724 inline float
725 receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
piotrd0bf1492014-02-05 17:27:32 +0100726 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700727 gr_complex conjprod = val1 * conj(val2);
728 return fast_atan2f(imag(conjprod), real(conjprod));
piotrd0bf1492014-02-05 17:27:32 +0100729 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700730
731 bool
732 receiver_impl::reach_sch_burst(const int nitems)
piotrd0bf1492014-02-05 17:27:32 +0100733 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700734 /* It just consumes samples to get near to a SCH burst */
735 int to_consume = 0;
736 bool result = false;
737 unsigned sample_nr = d_fcch_start_pos
738 + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
739
740 /* Consume samples until d_counter will be equal to sample_nr */
741 if (d_counter < sample_nr) {
742 to_consume = d_counter + nitems >= sample_nr ?
743 sample_nr - d_counter : nitems;
744 } else {
piotr437f5462014-02-04 17:57:25 +0100745 to_consume = 0;
746 result = true;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700747 }
748
749 d_counter += to_consume;
750 consume_each(to_consume);
751
752 return result;
piotr437f5462014-02-04 17:57:25 +0100753 }
754
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700755 int
756 receiver_impl::get_sch_chan_imp_resp(const gr_complex *input,
757 gr_complex * chan_imp_resp)
piotr437f5462014-02-04 17:57:25 +0100758 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700759 std::vector<gr_complex> correlation_buffer;
760 std::vector<float> window_energy_buffer;
761 std::vector<float> power_buffer;
762
763 int chan_imp_resp_center = 0;
764 int strongest_window_nr;
765 int burst_start;
766 float energy = 0;
767
768 int len = (SYNC_POS + SYNC_SEARCH_RANGE) * d_OSR;
769 for (int ii = SYNC_POS * d_OSR; ii < len; ii++) {
770 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5],
771 N_SYNC_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100772 correlation_buffer.push_back(correlation);
773 power_buffer.push_back(std::pow(abs(correlation), 2));
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700774 }
775
776 /* Compute window energies */
777 std::vector<float>::iterator iter = power_buffer.begin();
778 while (iter != power_buffer.end()) {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100779 std::vector<float>::iterator iter_ii = iter;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700780 bool loop_end = false;
piotr437f5462014-02-04 17:57:25 +0100781 energy = 0;
782
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700783 for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++, iter_ii++) {
784 if (iter_ii == power_buffer.end()) {
785 loop_end = true;
piotrd0bf1492014-02-05 17:27:32 +0100786 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700787 }
788
789 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100790 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700791
792 if (loop_end)
793 break;
794
piotr437f5462014-02-04 17:57:25 +0100795 window_energy_buffer.push_back(energy);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700796 iter++;
797 }
piotr437f5462014-02-04 17:57:25 +0100798
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700799 strongest_window_nr = max_element(window_energy_buffer.begin(),
800 window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100801
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700802#if 0
803 d_channel_imp_resp.clear();
804#endif
805
806 float max_correlation = 0;
807 for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++) {
piotr437f5462014-02-04 17:57:25 +0100808 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700809 if (abs(correlation) > max_correlation) {
810 chan_imp_resp_center = ii;
811 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100812 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700813
814#if 0
815 d_channel_imp_resp.push_back(correlation);
816#endif
817
piotr437f5462014-02-04 17:57:25 +0100818 chan_imp_resp[ii] = correlation;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700819 }
820
821 burst_start = strongest_window_nr + chan_imp_resp_center
822 - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
823 return burst_start;
piotr437f5462014-02-04 17:57:25 +0100824 }
825
826
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700827 void
828 receiver_impl::detect_burst(const gr_complex * input,
829 gr_complex * chan_imp_resp, int burst_start,
830 unsigned char * output_binary)
piotr437f5462014-02-04 17:57:25 +0100831 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700832 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH * d_OSR);
833 unsigned int stop_states[2] = {4, 12};
834 gr_complex filtered_burst[BURST_SIZE];
835 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
836 float output[BURST_SIZE];
837 int start_state = 3;
838
839 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
840 for (int ii = 0; ii < d_chan_imp_length; ii++)
piotr437f5462014-02-04 17:57:25 +0100841 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700842
843 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp,
844 d_chan_imp_length * d_OSR, filtered_burst);
845
846 viterbi_detector(filtered_burst, BURST_SIZE, rhh,
847 start_state, stop_states, 2, output);
848
849 for (int i = 0; i < BURST_SIZE; i++)
850 output_binary[i] = output[i] > 0;
piotr437f5462014-02-04 17:57:25 +0100851 }
852
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700853 void
854 receiver_impl::gmsk_mapper(const unsigned char * input,
855 int nitems, gr_complex * gmsk_output, gr_complex start_point)
piotr437f5462014-02-04 17:57:25 +0100856 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700857 gr_complex j = gr_complex(0.0, 1.0);
858 gmsk_output[0] = start_point;
piotr437f5462014-02-04 17:57:25 +0100859
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700860 int previous_symbol = 2 * input[0] - 1;
861 int current_symbol;
862 int encoded_symbol;
piotr437f5462014-02-04 17:57:25 +0100863
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700864 for (int i = 1; i < nitems; i++) {
865 /* Change bits representation to NRZ */
piotr437f5462014-02-04 17:57:25 +0100866 current_symbol = 2 * input[i] - 1;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700867
868 /* Differentially encode */
piotr437f5462014-02-04 17:57:25 +0100869 encoded_symbol = current_symbol * previous_symbol;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700870
871 /* And do GMSK mapping */
872 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0)
873 * gmsk_output[i-1];
874
piotr437f5462014-02-04 17:57:25 +0100875 previous_symbol = current_symbol;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700876 }
piotr437f5462014-02-04 17:57:25 +0100877 }
878
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700879 gr_complex
880 receiver_impl::correlate_sequence(const gr_complex * sequence,
881 int length, const gr_complex * input)
piotr437f5462014-02-04 17:57:25 +0100882 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700883 gr_complex result(0.0, 0.0);
884
885 for (int ii = 0; ii < length; ii++)
886 result += sequence[ii] * conj(input[ii * d_OSR]);
887
888 return result / gr_complex(length, 0);
889 }
890
891 /* Computes autocorrelation for positive arguments */
892 inline void
893 receiver_impl::autocorrelation(const gr_complex * input,
894 gr_complex * out, int nitems)
895 {
896 for (int k = nitems - 1; k >= 0; k--) {
piotr437f5462014-02-04 17:57:25 +0100897 out[k] = gr_complex(0, 0);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700898 for (int i = k; i < nitems; i++)
899 out[k] += input[i] * conj(input[i - k]);
900 }
piotr437f5462014-02-04 17:57:25 +0100901 }
902
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700903 inline void
904 receiver_impl::mafi(const gr_complex * input, int nitems,
905 gr_complex * filter, int filter_length, gr_complex * output)
piotr437f5462014-02-04 17:57:25 +0100906 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700907 for (int n = 0; n < nitems; n++) {
908 int a = n * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100909 output[n] = 0;
piotr437f5462014-02-04 17:57:25 +0100910
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700911 for (int ii = 0; ii < filter_length; ii++) {
912 if ((a + ii) >= nitems * d_OSR)
piotrd0bf1492014-02-05 17:27:32 +0100913 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700914
915 output[n] += input[a + ii] * filter[ii];
piotr437f5462014-02-04 17:57:25 +0100916 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700917 }
918 }
919
920 /* Especially computations of strongest_window_nr */
921 int
922 receiver_impl::get_norm_chan_imp_resp(const gr_complex *input,
923 gr_complex *chan_imp_resp, float *corr_max, int bcc)
924 {
925 std::vector<gr_complex> correlation_buffer;
926 std::vector<float> window_energy_buffer;
927 std::vector<float> power_buffer;
928
929 int search_center = (int) (TRAIN_POS + GUARD_PERIOD) * d_OSR;
930 int search_start_pos = search_center + 1 - 5 * d_OSR;
931 int search_stop_pos = search_center
932 + d_chan_imp_length * d_OSR + 5 * d_OSR;
933
934 for (int ii = search_start_pos; ii < search_stop_pos; ii++) {
935 gr_complex correlation = correlate_sequence(
936 &d_norm_training_seq[bcc][TRAIN_BEGINNING],
937 N_TRAIN_BITS - 10, &input[ii]);
938 correlation_buffer.push_back(correlation);
939 power_buffer.push_back(std::pow(abs(correlation), 2));
940 }
941
942#if 0
943 plot(power_buffer);
944#endif
945
946 /* Compute window energies */
947 std::vector<float>::iterator iter = power_buffer.begin();
948 while (iter != power_buffer.end()) {
949 std::vector<float>::iterator iter_ii = iter;
950 bool loop_end = false;
951 float energy = 0;
952
953 int len = d_chan_imp_length * d_OSR;
954 for (int ii = 0; ii < len; ii++, iter_ii++) {
955 if (iter_ii == power_buffer.end()) {
956 loop_end = true;
957 break;
958 }
959
960 energy += (*iter_ii);
961 }
962
963 if (loop_end)
964 break;
piotr437f5462014-02-04 17:57:25 +0100965
966 window_energy_buffer.push_back(energy);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700967 iter++;
968 }
piotr437f5462014-02-04 17:57:25 +0100969
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700970 /* Calculate the strongest window number */
971 int strongest_window_nr = max_element(window_energy_buffer.begin(),
972 window_energy_buffer.end() - d_chan_imp_length * d_OSR)
973 - window_energy_buffer.begin();
974
975 if (strongest_window_nr < 0)
976 strongest_window_nr = 0;
977
978 float max_correlation = 0;
979 for (int ii = 0; ii < d_chan_imp_length * d_OSR; ii++) {
piotr437f5462014-02-04 17:57:25 +0100980 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100981 if (abs(correlation) > max_correlation)
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700982 max_correlation = abs(correlation);
983
984#if 0
985 d_channel_imp_resp.push_back(correlation);
986#endif
987
piotr437f5462014-02-04 17:57:25 +0100988 chan_imp_resp[ii] = correlation;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700989 }
990
991 *corr_max = max_correlation;
992
993 /**
994 * Compute first sample position, which corresponds
995 * to the first sample of the impulse response
996 */
997 return search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100998 }
999
1000
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001001 void
1002 receiver_impl::send_burst(burst_counter burst_nr,
1003 const unsigned char * burst_binary, uint8_t burst_type,
Piotr Krysikdf978692017-09-27 21:58:24 +02001004 unsigned int input_nr, unsigned int burst_start)
ptrkrysik380dea82015-08-06 10:11:58 +02001005 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001006 /* Buffer for GSMTAP header and burst */
1007 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
1008 uint32_t frame_number;
1009 uint16_t arfcn;
1010 uint8_t tn;
ptrkrysik617ba032014-11-21 10:11:05 +01001011
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001012 /* Set pointers to GSMTAP header and burst inside buffer */
1013 struct gsmtap_hdr *tap_header = (struct gsmtap_hdr *) buf;
1014 uint8_t *burst = buf + sizeof(gsmtap_hdr);
1015
1016 tap_header->version = GSMTAP_VERSION;
1017 tap_header->hdr_len = sizeof(gsmtap_hdr) / 4;
1018 tap_header->type = GSMTAP_TYPE_UM_BURST;
1019 tap_header->sub_type = burst_type;
1020
1021 bool dl_burst = !(input_nr >= d_cell_allocation.size());
1022 if (dl_burst) {
1023 tn = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
1024 frame_number = htobe32(d_burst_nr.get_frame_nr());
1025 arfcn = htobe16(d_cell_allocation[input_nr]);
1026 } else {
1027 input_nr -= d_cell_allocation.size();
1028 tn = static_cast<uint8_t>
1029 (d_burst_nr.subtract_timeslots(3).get_timeslot_nr());
1030 frame_number = htobe32(
1031 d_burst_nr.subtract_timeslots(3).get_frame_nr());
1032 arfcn = htobe16(
1033 d_cell_allocation[input_nr] | GSMTAP_ARFCN_F_UPLINK);
1034 }
1035
1036 tap_header->frame_number = frame_number;
1037 tap_header->timeslot = tn;
1038 tap_header->arfcn = arfcn;
1039
1040 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
1041 tap_header->snr_db = 0; /* FIXME: Can we calculate this? */
1042
Piotr Krysikdf978692017-09-27 21:58:24 +02001043 pmt::pmt_t pdu_header = pmt::make_dict();
1044
1045 /* Add timestamp of the first sample - if available */
1046 if(d_rx_time_received) {
1047 time_spec_t time_spec_of_first_sample = d_time_samp_ref.offset_to_time(nitems_read(0)+burst_start);
1048 uint64_t full = time_spec_of_first_sample.get_full_secs();
1049 double frac = time_spec_of_first_sample.get_frac_secs();
1050 pdu_header =
1051 pmt::dict_add(pdu_header, pmt::mp("fn_time"), pmt::cons(pmt::from_uint64(frame_number), pmt::cons(pmt::from_uint64(full), pmt::from_double(frac))));
1052 }
1053
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001054 /* Copy burst to the buffer */
1055 memcpy(burst, burst_binary, BURST_SIZE);
1056
1057 /* Allocate a new message */
1058 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
1059 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob);
1060
1061 /* Send message */
1062 if (input_nr == 0)
ptrkrysike518bbf2014-11-06 14:50:59 +01001063 message_port_pub(pmt::mp("C0"), msg);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001064 else
ptrkrysike518bbf2014-11-06 14:50:59 +01001065 message_port_pub(pmt::mp("CX"), msg);
1066 }
piotr6d152d92014-02-21 00:02:44 +01001067
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001068 void
1069 receiver_impl::configure_receiver(void)
1070 {
1071 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
1072 d_channel_conf.set_burst_types(TIMESLOT0, TEST51,
1073 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1074 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES,
1075 sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
1076 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES,
1077 sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
1078 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES,
1079 sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +01001080
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001081 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
1082 d_channel_conf.set_burst_types(TIMESLOT1, TEST51,
1083 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001084
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001085 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
1086 d_channel_conf.set_burst_types(TIMESLOT2, TEST51,
1087 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001088
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001089 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
1090 d_channel_conf.set_burst_types(TIMESLOT3, TEST51,
1091 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
ptrkrysike518bbf2014-11-06 14:50:59 +01001092
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001093 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
1094 d_channel_conf.set_burst_types(TIMESLOT4, TEST51,
1095 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrf2b6a1b2014-08-04 11:28:59 +02001096
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001097 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
1098 d_channel_conf.set_burst_types(TIMESLOT5, TEST51,
1099 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001100
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001101 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
1102 d_channel_conf.set_burst_types(TIMESLOT6, TEST51,
1103 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1104
1105 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
1106 d_channel_conf.set_burst_types(TIMESLOT7, TEST51,
1107 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1108 }
1109
1110 void
1111 receiver_impl::set_cell_allocation(
1112 const std::vector<int> &cell_allocation)
1113 {
1114 d_cell_allocation = cell_allocation;
1115 }
1116
1117 void
1118 receiver_impl::set_tseq_nums(const std::vector<int> &tseq_nums)
1119 {
1120 d_tseq_nums = tseq_nums;
1121 }
1122
1123 void
1124 receiver_impl::reset(void)
1125 {
1126 d_state = fcch_search;
1127 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001128 } /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +01001129} /* namespace gr */