blob: ffaf9ee327bc1aacbd7bee363d5bee413ed72023 [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>
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070034#include <vector>
ptrkrysik3be74a72014-12-13 10:11:00 +010035
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070036#include <boost/circular_buffer.hpp>
37#include <boost/scoped_ptr.hpp>
ptrkrysik3be74a72014-12-13 10:11:00 +010038#include <grgsm/endian.h>
ptrkrysik58213792014-10-30 09:05:15 +010039
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070040#include "receiver_impl.h"
41#include "viterbi_detector.h"
42#include "sch.h"
43
44#if 0
45/* Files included for debuging */
46#include "plotting/plotting.hpp"
47#include <pthread.h>
48#include <iomanip>
49#endif
piotr437f5462014-02-04 17:57:25 +010050
51#define SYNC_SEARCH_RANGE 30
52
piotrd0bf1492014-02-05 17:27:32 +010053namespace gr
54{
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070055 namespace gsm
56 {
piotrd0bf1492014-02-05 17:27:32 +010057
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070058 /* The public constructor */
59 receiver::sptr
60 receiver::make(
61 int osr, const std::vector<int> &cell_allocation,
62 const std::vector<int> &tseq_nums, bool process_uplink)
piotr437f5462014-02-04 17:57:25 +010063 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070064 return gnuradio::get_initial_sptr
65 (new receiver_impl(osr, cell_allocation,
66 tseq_nums, process_uplink));
piotr437f5462014-02-04 17:57:25 +010067 }
68
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +070069 /* The private constructor */
70 receiver_impl::receiver_impl(
71 int osr, const std::vector<int> &cell_allocation,
72 const std::vector<int> &tseq_nums, bool process_uplink
73 ) : gr::sync_block("receiver",
74 gr::io_signature::make(1, -1, sizeof(gr_complex)),
75 gr::io_signature::make(0, 0, 0)),
Piotr Krysik96f2ea72017-10-16 15:47:08 +020076 d_samples_consumed(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
152
153 /* Time synchronization loop */
154 float current_time =
155 static_cast<float>(start / (GSM_SYMBOL_RATE * d_OSR));
156 if ((current_time - d_last_time) > 0.1) {
157 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),
158 pmt::from_double(current_time));
ptrkrysik32c21162015-04-04 14:01:52 +0200159 message_port_pub(pmt::mp("measurements"), msg);
160 d_last_time = current_time;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700161 }
ptrkrysik32c21162015-04-04 14:01:52 +0200162
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700163 /* Frequency correction loop */
164 std::vector<tag_t> freq_offset_tags;
165 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
166 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
167
168 if (!freq_offset_tags.empty()) {
piotr4089c1a2014-08-06 14:10:56 +0200169 tag_t freq_offset_tag = freq_offset_tags[0];
Piotr Krysik43af70d2016-07-20 21:37:24 +0200170 uint64_t tag_offset = freq_offset_tag.offset - start;
Piotr Krysik43af70d2016-07-20 21:37:24 +0200171 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
piotr4089c1a2014-08-06 14:10:56 +0200172
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700173 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
174 if (d_state == synchronized && b_type == fcch_burst){
175 uint64_t last_sample_nr =
176 ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
177 d_freq_offset_tag_in_fcch = tag_offset < last_sample_nr;
piotrd0bf1492014-02-05 17:27:32 +0100178 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700179 }
Piotr Krysikdf978692017-09-27 21:58:24 +0200180
181 /* Obtaining current time with use of rx_time tag provided i.e. by UHD devices */
182 /* And storing it in time_sample_ref for sample number to time conversion */
183 std::vector<tag_t> rx_time_tags;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700184
185 /* Main state machine */
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200186 d_samples_consumed = 0;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700187 switch (d_state) {
188 case fcch_search:
189 fcch_search_handler(input, noutput_items);
piotrd0bf1492014-02-05 17:27:32 +0100190 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700191 case sch_search:
192 sch_search_handler(input, noutput_items);
piotrd0bf1492014-02-05 17:27:32 +0100193 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700194 case synchronized:
195 synchronized_handler(input, input_items, noutput_items);
196 break;
197 }
198
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200199 get_tags_in_window(rx_time_tags, 0, 0, d_samples_consumed, pmt::string_to_symbol("rx_time"));
200 if(!rx_time_tags.empty()){
201 d_rx_time_received = true;
202 tag_t rx_time_tag = *(rx_time_tags.begin());
203
204 uint64_t rx_time_full_part = to_uint64(tuple_ref(rx_time_tag.value,0));
205 double rx_time_frac_part = to_double(tuple_ref(rx_time_tag.value,1));
206
207 time_spec_t current_rx_time = time_spec_t(rx_time_full_part, rx_time_frac_part);
208 uint64_t current_start_offset = rx_time_tag.offset;
209 d_time_samp_ref.update(current_rx_time, current_start_offset);
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200210 }
211
212 return d_samples_consumed;
piotrd0bf1492014-02-05 17:27:32 +0100213 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700214
215 void
216 receiver_impl::fcch_search_handler(gr_complex *input, int noutput_items)
piotrd0bf1492014-02-05 17:27:32 +0100217 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700218 double freq_offset_tmp;
ptrkrysik58213792014-10-30 09:05:15 +0100219
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700220 /* Check if received samples is a FCCN burst */
221 if (!find_fcch_burst(input, noutput_items, freq_offset_tmp))
222 return;
piotr437f5462014-02-04 17:57:25 +0100223
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700224 /* We found it, compose a message */
225 pmt::pmt_t msg = pmt::make_tuple(
226 pmt::mp("freq_offset"),
227 pmt::from_double(freq_offset_tmp - d_freq_offset_setting),
228 pmt::mp("fcch_search")
229 );
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100230
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700231 /* Notify FCCH loop */
232 message_port_pub(pmt::mp("measurements"), msg);
233
234 /* Update current state */
235 d_state = sch_search;
piotrd0bf1492014-02-05 17:27:32 +0100236 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700237
238 void
239 receiver_impl::sch_search_handler(gr_complex *input, int noutput_items)
240 {
241 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR);
242 unsigned char burst_buf[BURST_SIZE];
243 int rc, t1, t2, t3;
244 int burst_start;
245
246 /* Wait until we get a SCH burst */
247 if (!reach_sch_burst(noutput_items))
248 return;
249
250 /* Get channel impulse response from it */
251 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]);
252
253 /* Detect bits using MLSE detection */
254 detect_burst(input, &channel_imp_resp[0], burst_start, burst_buf);
255
256 /* Attempt to decode BSIC and frame number */
257 rc = decode_sch(&burst_buf[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
258 if (rc) {
259 /**
260 * There is error in the SCH burst,
261 * go back to the FCCH search state
262 */
263 d_state = fcch_search;
264 return;
265 }
266
267 /* Set counter of bursts value */
268 d_burst_nr.set(t1, t2, t3, 0);
269 d_burst_nr++;
270
271 /* Consume samples up to the next guard period */
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200272 unsigned int to_consume = burst_start + BURST_SIZE * d_OSR + 4 * d_OSR;
273// consume_each(to_consume);
274 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700275
276 /* Update current state */
277 d_state = synchronized;
piotr437f5462014-02-04 17:57:25 +0100278 }
piotr437f5462014-02-04 17:57:25 +0100279
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700280 void
281 receiver_impl::synchronized_handler(gr_complex *input,
282 gr_vector_const_void_star &input_items, int noutput_items)
piotr437f5462014-02-04 17:57:25 +0100283 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700284 /**
285 * In this state receiver is synchronized and it processes
286 * bursts according to burst type for given burst number
287 */
288 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR);
Piotr Krysik3b116892018-02-27 08:43:30 +0100289 size_t inputs_to_process = d_cell_allocation.size();
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700290 unsigned char output_binary[BURST_SIZE];
291 burst_type b_type;
292 int to_consume = 0;
293 int offset = 0;
piotr437f5462014-02-04 17:57:25 +0100294
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700295 if (d_process_uplink)
296 inputs_to_process *= 2;
piotr437f5462014-02-04 17:57:25 +0100297
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700298 /* Process all connected inputs */
Piotr Krysik3b116892018-02-27 08:43:30 +0100299 for (size_t input_nr = 0; input_nr < inputs_to_process; input_nr++) {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700300 input = (gr_complex *) input_items[input_nr];
301 double signal_pwr = 0;
302
303 for (int ii = GUARD_PERIOD; ii < TS_BITS; ii++)
304 signal_pwr += abs(input[ii]) * abs(input[ii]);
305
306 signal_pwr = signal_pwr / (TS_BITS);
307 d_signal_dbm = round(10 * log10(signal_pwr / 50));
308
309 if (input_nr == 0)
310 d_c0_signal_dbm = d_signal_dbm;
311
312 /* Get burst type for given burst number */
313 b_type = input_nr == 0 ?
314 d_channel_conf.get_burst_type(d_burst_nr) : normal_or_noise;
315
316 /* Process burst according to its type */
317 switch (b_type) {
318 case fcch_burst:
piotrd0bf1492014-02-05 17:27:32 +0100319 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700320 if (d_freq_offset_tag_in_fcch)
piotr437f5462014-02-04 17:57:25 +0100321 break;
322
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700323 /* Send all-zero sequence message */
324 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
piotr437f5462014-02-04 17:57:25 +0100325
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700326 /* Extract frequency offset */
327 const unsigned first_sample =
328 ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
329 const unsigned last_sample =
330 first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
331 double freq_offset_tmp =
332 compute_freq_offset(input, first_sample, last_sample);
piotr437f5462014-02-04 17:57:25 +0100333
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700334 /* Frequency correction loop */
335 pmt::pmt_t msg = pmt::make_tuple(
336 pmt::mp("freq_offset"),
337 pmt::from_double(freq_offset_tmp - d_freq_offset_setting),
338 pmt::mp("synchronized"));
339 message_port_pub(pmt::mp("measurements"), msg);
340
341 break;
342 }
343
344 case sch_burst:
345 {
Piotr Krysikbfb47dc2017-11-11 11:12:02 +0100346 int ncc, bcc;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700347 int t1, t2, t3;
348 int rc;
349
350 /* Get channel impulse response */
351 d_c0_burst_start = get_sch_chan_imp_resp(input,
352 &channel_imp_resp[0]);
353
354 /* Perform MLSE detection */
355 detect_burst(input, &channel_imp_resp[0],
356 d_c0_burst_start, output_binary);
357
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700358 /* Attempt to decode SCH burst */
Piotr Krysikbfb47dc2017-11-11 11:12:02 +0100359 rc = decode_sch(&output_binary[3], &t1, &t2, &t3, &ncc, &bcc);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700360 if (rc) {
361 if (++d_failed_sch >= MAX_SCH_ERRORS) {
362 /* We have to resynchronize, change state */
363 d_state = fcch_search;
364
365 /* Frequency correction loop */
366 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),
367 pmt::from_double(0.0),pmt::mp("sync_loss"));
368 message_port_pub(pmt::mp("measurements"), msg);
piotr437f5462014-02-04 17:57:25 +0100369 }
370
371 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700372 }
piotr437f5462014-02-04 17:57:25 +0100373
Piotr Krysikbfb47dc2017-11-11 11:12:02 +0100374 /* Compose a message with GSMTAP header and bits */
375 send_burst(d_burst_nr, output_binary,
376 GSMTAP_BURST_SCH, input_nr, d_c0_burst_start);
377
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700378 /**
379 * Decoding was successful, now
380 * compute offset from burst_start,
381 * burst should start after a guard period.
382 */
383 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR);
384 to_consume += offset;
385 d_failed_sch = 0;
piotr437f5462014-02-04 17:57:25 +0100386
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700387 break;
piotrd0bf1492014-02-05 17:27:32 +0100388 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700389
390 case normal_burst:
391 {
392 float normal_corr_max;
393 /**
394 * Get channel impulse response for given
395 * training sequence number - d_bcc
396 */
397 d_c0_burst_start = get_norm_chan_imp_resp(input,
398 &channel_imp_resp[0], &normal_corr_max, d_bcc);
399
400 /* Perform MLSE detection */
401 detect_burst(input, &channel_imp_resp[0],
402 d_c0_burst_start, output_binary);
403
404 /* Compose a message with GSMTAP header and bits */
405 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200406 GSMTAP_BURST_NORMAL, input_nr, d_c0_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700407
408 break;
409 }
410
411 case dummy_or_normal:
412 {
413 unsigned int normal_burst_start, dummy_burst_start;
414 float dummy_corr_max, normal_corr_max;
415
416 dummy_burst_start = get_norm_chan_imp_resp(input,
417 &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
418 normal_burst_start = get_norm_chan_imp_resp(input,
419 &channel_imp_resp[0], &normal_corr_max, d_bcc);
420
421 if (normal_corr_max > dummy_corr_max) {
422 d_c0_burst_start = normal_burst_start;
423
424 /* Perform MLSE detection */
425 detect_burst(input, &channel_imp_resp[0],
426 normal_burst_start, output_binary);
427
428 /* Compose a message with GSMTAP header and bits */
429 send_burst(d_burst_nr, output_binary,
Piotr Krysikdf978692017-09-27 21:58:24 +0200430 GSMTAP_BURST_NORMAL, input_nr, normal_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700431 } else {
432 d_c0_burst_start = dummy_burst_start;
433
434 /* Compose a message with GSMTAP header and bits */
435 send_burst(d_burst_nr, dummy_burst,
Piotr Krysikdf978692017-09-27 21:58:24 +0200436 GSMTAP_BURST_DUMMY, input_nr, dummy_burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700437 }
438
439 break;
440 }
441
442 case normal_or_noise:
443 {
444 std::vector<gr_complex> v(input, input + noutput_items);
445 float normal_corr_max = -1e6;
Piotr Krysik3b116892018-02-27 08:43:30 +0100446// float normal_corr_max_tmp;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700447 unsigned int burst_start;
448 int max_tn, tseq_num;
449
450 if (d_tseq_nums.size() == 0) {
451 /**
452 * There is no information about training sequence,
453 * however the receiver can detect it with use of a
454 * very simple algorithm based on finding
455 */
456 get_norm_chan_imp_resp(input, &channel_imp_resp[0],
457 &normal_corr_max, 0);
458
459 float ts_max = normal_corr_max;
460 int ts_max_num = 0;
461
462 for (int ss = 1; ss <= 7; ss++) {
463 get_norm_chan_imp_resp(input, &channel_imp_resp[0],
464 &normal_corr_max, ss);
465
466 if (ts_max < normal_corr_max) {
467 ts_max = normal_corr_max;
468 ts_max_num = ss;
469 }
470 }
471
472 d_tseq_nums.push_back(ts_max_num);
473 }
474
475 /* Choose proper training sequence number */
476 tseq_num = input_nr <= d_tseq_nums.size() ?
477 d_tseq_nums[input_nr - 1] : d_tseq_nums.back();
478
479 /* Get channel impulse response */
480 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0],
481 &normal_corr_max, tseq_num);
482
483 /* Perform MLSE detection */
484 detect_burst(input, &channel_imp_resp[0],
485 burst_start, output_binary);
486
487 /* Compose a message with GSMTAP header and bits */
Piotr Krysikdf978692017-09-27 21:58:24 +0200488 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr, burst_start);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700489
490 break;
491 }
492
493 case dummy:
494 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
495 break;
496
497 case rach_burst:
498 case empty:
499 /* Do nothing */
500 break;
501 }
502
503 if (input_nr == input_items.size() - 1) {
504 /* Go to the next burst */
505 d_burst_nr++;
506
507 /* Consume samples of the burst up to next guard period */
508 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset();
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200509// consume_each(to_consume);
510 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700511 }
512 }
513 }
514
515 bool
516 receiver_impl::find_fcch_burst(const gr_complex *input,
517 const int nitems, double &computed_freq_offset)
518 {
519 /* Circular buffer used to scan through signal to find */
520 boost::circular_buffer<float>
521 phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR);
522 boost::circular_buffer<float>::iterator buffer_iter;
523
524 float lowest_max_min_diff;
525 float phase_diff; /* Best match for FCCH burst */
526 float min_phase_diff;
527 float max_phase_diff;
528 double best_sum = 0;
529 gr_complex conjprod;
530 int start_pos;
531 int hit_count;
532 int miss_count;
533
534 int sample_number = 0;
535 int to_consume = 0;
536 bool result = false;
537 bool end = false;
538
539 /* Possible states of FCCH search algorithm */
540 enum states
541 {
542 init, /* initialize variables */
543 search, /* search for positive samples */
544 found_something, /* search for FCCH and the best position of it */
545 fcch_found, /* when FCCH was found */
546 search_fail /* when there is no FCCH in the input vector */
547 } fcch_search_state;
548
549 /* Set initial state */
550 fcch_search_state = init;
551
552 while (!end)
553 {
554 switch (fcch_search_state) {
555 case init:
556 {
557 hit_count = 0;
558 miss_count = 0;
559 start_pos = -1;
560 lowest_max_min_diff = 99999;
561 phase_diff_buffer.clear();
562
563 /* Change current state */
564 fcch_search_state = search;
565
566 break;
567 }
568
569 case search:
570 {
571 sample_number++;
572
573 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) {
574 /**
575 * If it isn't possible to find FCCH, because
576 * there is too few samples left to look into,
577 * don't do anything with those samples which are left
578 * and consume only those which were checked
579 */
580 to_consume = sample_number;
581 fcch_search_state = search_fail;
582 break;
583 }
584
585 phase_diff = compute_phase_diff(input[sample_number],
586 input[sample_number - 1]);
587
588 /**
589 * If a positive phase difference was found
590 * switch to state in which searches for FCCH
591 */
592 if (phase_diff > 0) {
593 to_consume = sample_number;
594 fcch_search_state = found_something;
595 } else {
596 fcch_search_state = search;
597 }
598
599 break;
600 }
601
602 case found_something:
603 {
604 if (phase_diff > 0)
605 hit_count++;
606 else
607 miss_count++;
608
609 if ((miss_count >= FCCH_MAX_MISSES * d_OSR)
610 && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
611 {
612 /* If miss_count exceeds limit before hit_count */
613 fcch_search_state = init;
614 continue;
615 }
616
617 if (((miss_count >= FCCH_MAX_MISSES * d_OSR)
618 && (hit_count > FCCH_HITS_NEEDED * d_OSR))
619 || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
620 {
621 /**
622 * If hit_count and miss_count exceeds
623 * limit then FCCH was found
624 */
625 fcch_search_state = fcch_found;
626 continue;
627 }
628
629 if ((miss_count < FCCH_MAX_MISSES * d_OSR)
630 && (hit_count > FCCH_HITS_NEEDED * d_OSR))
631 {
632 /**
633 * Find difference between minimal and maximal
634 * element in the buffer. For FCCH this value
635 * should be low. This part is searching for
636 * a region where this value is lowest.
637 */
638 min_phase_diff = *(min_element(phase_diff_buffer.begin(),
639 phase_diff_buffer.end()));
640 max_phase_diff = *(max_element(phase_diff_buffer.begin(),
641 phase_diff_buffer.end()));
642
643 if (lowest_max_min_diff > max_phase_diff - min_phase_diff) {
644 lowest_max_min_diff = max_phase_diff - min_phase_diff;
645 start_pos = sample_number - FCCH_HITS_NEEDED
646 * d_OSR - FCCH_MAX_MISSES * d_OSR;
647 best_sum = 0;
648
649 for (buffer_iter = phase_diff_buffer.begin();
650 buffer_iter != (phase_diff_buffer.end());
651 buffer_iter++) {
652 /* Store best value of phase offset sum */
653 best_sum += *buffer_iter - (M_PI / 2) / d_OSR;
654 }
655 }
656 }
657
658 /* If there is no single sample left to check */
659 if (++sample_number >= nitems) {
660 fcch_search_state = search_fail;
661 continue;
662 }
663
664 phase_diff = compute_phase_diff(input[sample_number],
665 input[sample_number-1]);
666 phase_diff_buffer.push_back(phase_diff);
667 fcch_search_state = found_something;
668
669 break;
670 }
piotrd0bf1492014-02-05 17:27:32 +0100671
672 case fcch_found:
673 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700674 /* Consume one FCCH burst */
675 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1;
676 d_fcch_start_pos = d_counter + start_pos;
piotrd0bf1492014-02-05 17:27:32 +0100677
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700678 /**
679 * Compute frequency offset
680 *
681 * 1625000.0 / 6 - GMSK symbol rate in GSM
682 */
683 double phase_offset = best_sum / FCCH_HITS_NEEDED;
684 double freq_offset = phase_offset * 1625000.0 / 6 / (2 * M_PI);
685 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100686
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700687 end = true;
688 result = true;
piotrd0bf1492014-02-05 17:27:32 +0100689
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700690 break;
piotrd0bf1492014-02-05 17:27:32 +0100691 }
piotr437f5462014-02-04 17:57:25 +0100692
piotrd0bf1492014-02-05 17:27:32 +0100693 case search_fail:
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700694 end = true;
695 result = false;
696 break;
piotr437f5462014-02-04 17:57:25 +0100697 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700698 }
699
700 d_counter += to_consume;
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200701// consume_each(to_consume);
702 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700703
704 return result;
piotr437f5462014-02-04 17:57:25 +0100705 }
706
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700707 double
708 receiver_impl::compute_freq_offset(const gr_complex * input,
709 unsigned first_sample, unsigned last_sample)
Piotr Krysik654d6522017-01-23 21:53:48 +0100710 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700711 double phase_sum = 0;
712 unsigned ii;
713
714 for (ii = first_sample; ii < last_sample; ii++)
715 {
716 double phase_diff = compute_phase_diff(input[ii],
717 input[ii-1]) - (M_PI / 2) / d_OSR;
Piotr Krysik654d6522017-01-23 21:53:48 +0100718 phase_sum += phase_diff;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700719 }
720
721 double phase_offset = phase_sum / (last_sample - first_sample);
722 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
723
724 return freq_offset;
Piotr Krysik654d6522017-01-23 21:53:48 +0100725 }
726
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700727 inline float
728 receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
piotrd0bf1492014-02-05 17:27:32 +0100729 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700730 gr_complex conjprod = val1 * conj(val2);
731 return fast_atan2f(imag(conjprod), real(conjprod));
piotrd0bf1492014-02-05 17:27:32 +0100732 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700733
734 bool
735 receiver_impl::reach_sch_burst(const int nitems)
piotrd0bf1492014-02-05 17:27:32 +0100736 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700737 /* It just consumes samples to get near to a SCH burst */
738 int to_consume = 0;
739 bool result = false;
740 unsigned sample_nr = d_fcch_start_pos
741 + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
742
743 /* Consume samples until d_counter will be equal to sample_nr */
744 if (d_counter < sample_nr) {
745 to_consume = d_counter + nitems >= sample_nr ?
746 sample_nr - d_counter : nitems;
747 } else {
piotr437f5462014-02-04 17:57:25 +0100748 to_consume = 0;
749 result = true;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700750 }
751
752 d_counter += to_consume;
Piotr Krysik96f2ea72017-10-16 15:47:08 +0200753// consume_each(to_consume);
754 d_samples_consumed += to_consume;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700755
756 return result;
piotr437f5462014-02-04 17:57:25 +0100757 }
758
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700759 int
760 receiver_impl::get_sch_chan_imp_resp(const gr_complex *input,
761 gr_complex * chan_imp_resp)
piotr437f5462014-02-04 17:57:25 +0100762 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700763 std::vector<gr_complex> correlation_buffer;
764 std::vector<float> window_energy_buffer;
765 std::vector<float> power_buffer;
766
767 int chan_imp_resp_center = 0;
768 int strongest_window_nr;
769 int burst_start;
770 float energy = 0;
771
772 int len = (SYNC_POS + SYNC_SEARCH_RANGE) * d_OSR;
773 for (int ii = SYNC_POS * d_OSR; ii < len; ii++) {
774 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5],
775 N_SYNC_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100776 correlation_buffer.push_back(correlation);
777 power_buffer.push_back(std::pow(abs(correlation), 2));
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700778 }
779
780 /* Compute window energies */
781 std::vector<float>::iterator iter = power_buffer.begin();
782 while (iter != power_buffer.end()) {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100783 std::vector<float>::iterator iter_ii = iter;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700784 bool loop_end = false;
piotr437f5462014-02-04 17:57:25 +0100785 energy = 0;
786
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700787 for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++, iter_ii++) {
788 if (iter_ii == power_buffer.end()) {
789 loop_end = true;
piotrd0bf1492014-02-05 17:27:32 +0100790 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700791 }
792
793 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100794 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700795
796 if (loop_end)
797 break;
798
piotr437f5462014-02-04 17:57:25 +0100799 window_energy_buffer.push_back(energy);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700800 iter++;
801 }
piotr437f5462014-02-04 17:57:25 +0100802
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700803 strongest_window_nr = max_element(window_energy_buffer.begin(),
804 window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100805
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700806#if 0
807 d_channel_imp_resp.clear();
808#endif
809
810 float max_correlation = 0;
811 for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++) {
piotr437f5462014-02-04 17:57:25 +0100812 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700813 if (abs(correlation) > max_correlation) {
814 chan_imp_resp_center = ii;
815 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100816 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700817
818#if 0
819 d_channel_imp_resp.push_back(correlation);
820#endif
821
piotr437f5462014-02-04 17:57:25 +0100822 chan_imp_resp[ii] = correlation;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700823 }
824
825 burst_start = strongest_window_nr + chan_imp_resp_center
826 - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
827 return burst_start;
piotr437f5462014-02-04 17:57:25 +0100828 }
829
830
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700831 void
832 receiver_impl::detect_burst(const gr_complex * input,
833 gr_complex * chan_imp_resp, int burst_start,
834 unsigned char * output_binary)
piotr437f5462014-02-04 17:57:25 +0100835 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700836 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH * d_OSR);
837 unsigned int stop_states[2] = {4, 12};
838 gr_complex filtered_burst[BURST_SIZE];
839 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
840 float output[BURST_SIZE];
841 int start_state = 3;
842
843 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
844 for (int ii = 0; ii < d_chan_imp_length; ii++)
piotr437f5462014-02-04 17:57:25 +0100845 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700846
847 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp,
848 d_chan_imp_length * d_OSR, filtered_burst);
849
850 viterbi_detector(filtered_burst, BURST_SIZE, rhh,
851 start_state, stop_states, 2, output);
852
853 for (int i = 0; i < BURST_SIZE; i++)
854 output_binary[i] = output[i] > 0;
piotr437f5462014-02-04 17:57:25 +0100855 }
856
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700857 void
858 receiver_impl::gmsk_mapper(const unsigned char * input,
859 int nitems, gr_complex * gmsk_output, gr_complex start_point)
piotr437f5462014-02-04 17:57:25 +0100860 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700861 gr_complex j = gr_complex(0.0, 1.0);
862 gmsk_output[0] = start_point;
piotr437f5462014-02-04 17:57:25 +0100863
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700864 int previous_symbol = 2 * input[0] - 1;
865 int current_symbol;
866 int encoded_symbol;
piotr437f5462014-02-04 17:57:25 +0100867
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700868 for (int i = 1; i < nitems; i++) {
869 /* Change bits representation to NRZ */
piotr437f5462014-02-04 17:57:25 +0100870 current_symbol = 2 * input[i] - 1;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700871
872 /* Differentially encode */
piotr437f5462014-02-04 17:57:25 +0100873 encoded_symbol = current_symbol * previous_symbol;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700874
875 /* And do GMSK mapping */
876 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0)
877 * gmsk_output[i-1];
878
piotr437f5462014-02-04 17:57:25 +0100879 previous_symbol = current_symbol;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700880 }
piotr437f5462014-02-04 17:57:25 +0100881 }
882
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700883 gr_complex
884 receiver_impl::correlate_sequence(const gr_complex * sequence,
885 int length, const gr_complex * input)
piotr437f5462014-02-04 17:57:25 +0100886 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700887 gr_complex result(0.0, 0.0);
888
889 for (int ii = 0; ii < length; ii++)
890 result += sequence[ii] * conj(input[ii * d_OSR]);
891
892 return result / gr_complex(length, 0);
893 }
894
895 /* Computes autocorrelation for positive arguments */
896 inline void
897 receiver_impl::autocorrelation(const gr_complex * input,
898 gr_complex * out, int nitems)
899 {
900 for (int k = nitems - 1; k >= 0; k--) {
piotr437f5462014-02-04 17:57:25 +0100901 out[k] = gr_complex(0, 0);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700902 for (int i = k; i < nitems; i++)
903 out[k] += input[i] * conj(input[i - k]);
904 }
piotr437f5462014-02-04 17:57:25 +0100905 }
906
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700907 inline void
908 receiver_impl::mafi(const gr_complex * input, int nitems,
909 gr_complex * filter, int filter_length, gr_complex * output)
piotr437f5462014-02-04 17:57:25 +0100910 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700911 for (int n = 0; n < nitems; n++) {
912 int a = n * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100913 output[n] = 0;
piotr437f5462014-02-04 17:57:25 +0100914
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700915 for (int ii = 0; ii < filter_length; ii++) {
916 if ((a + ii) >= nitems * d_OSR)
piotrd0bf1492014-02-05 17:27:32 +0100917 break;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700918
919 output[n] += input[a + ii] * filter[ii];
piotr437f5462014-02-04 17:57:25 +0100920 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700921 }
922 }
923
924 /* Especially computations of strongest_window_nr */
925 int
926 receiver_impl::get_norm_chan_imp_resp(const gr_complex *input,
927 gr_complex *chan_imp_resp, float *corr_max, int bcc)
928 {
929 std::vector<gr_complex> correlation_buffer;
930 std::vector<float> window_energy_buffer;
931 std::vector<float> power_buffer;
932
933 int search_center = (int) (TRAIN_POS + GUARD_PERIOD) * d_OSR;
934 int search_start_pos = search_center + 1 - 5 * d_OSR;
935 int search_stop_pos = search_center
936 + d_chan_imp_length * d_OSR + 5 * d_OSR;
937
938 for (int ii = search_start_pos; ii < search_stop_pos; ii++) {
939 gr_complex correlation = correlate_sequence(
940 &d_norm_training_seq[bcc][TRAIN_BEGINNING],
941 N_TRAIN_BITS - 10, &input[ii]);
942 correlation_buffer.push_back(correlation);
943 power_buffer.push_back(std::pow(abs(correlation), 2));
944 }
945
946#if 0
947 plot(power_buffer);
948#endif
949
950 /* Compute window energies */
951 std::vector<float>::iterator iter = power_buffer.begin();
952 while (iter != power_buffer.end()) {
953 std::vector<float>::iterator iter_ii = iter;
954 bool loop_end = false;
955 float energy = 0;
956
957 int len = d_chan_imp_length * d_OSR;
958 for (int ii = 0; ii < len; ii++, iter_ii++) {
959 if (iter_ii == power_buffer.end()) {
960 loop_end = true;
961 break;
962 }
963
964 energy += (*iter_ii);
965 }
966
967 if (loop_end)
968 break;
piotr437f5462014-02-04 17:57:25 +0100969
970 window_energy_buffer.push_back(energy);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700971 iter++;
972 }
piotr437f5462014-02-04 17:57:25 +0100973
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700974 /* Calculate the strongest window number */
975 int strongest_window_nr = max_element(window_energy_buffer.begin(),
976 window_energy_buffer.end() - d_chan_imp_length * d_OSR)
977 - window_energy_buffer.begin();
978
979 if (strongest_window_nr < 0)
980 strongest_window_nr = 0;
981
982 float max_correlation = 0;
983 for (int ii = 0; ii < d_chan_imp_length * d_OSR; ii++) {
piotr437f5462014-02-04 17:57:25 +0100984 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100985 if (abs(correlation) > max_correlation)
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700986 max_correlation = abs(correlation);
987
988#if 0
989 d_channel_imp_resp.push_back(correlation);
990#endif
991
piotr437f5462014-02-04 17:57:25 +0100992 chan_imp_resp[ii] = correlation;
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +0700993 }
994
995 *corr_max = max_correlation;
996
997 /**
998 * Compute first sample position, which corresponds
999 * to the first sample of the impulse response
1000 */
1001 return search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR;
piotr437f5462014-02-04 17:57:25 +01001002 }
1003
1004
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001005 void
1006 receiver_impl::send_burst(burst_counter burst_nr,
1007 const unsigned char * burst_binary, uint8_t burst_type,
Piotr Krysik8a8d41a2018-04-16 22:21:29 +02001008 size_t input_nr, unsigned int burst_start)
ptrkrysik380dea82015-08-06 10:11:58 +02001009 {
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001010 /* Buffer for GSMTAP header and burst */
1011 uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
1012 uint32_t frame_number;
1013 uint16_t arfcn;
1014 uint8_t tn;
ptrkrysik617ba032014-11-21 10:11:05 +01001015
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001016 /* Set pointers to GSMTAP header and burst inside buffer */
1017 struct gsmtap_hdr *tap_header = (struct gsmtap_hdr *) buf;
1018 uint8_t *burst = buf + sizeof(gsmtap_hdr);
1019
1020 tap_header->version = GSMTAP_VERSION;
1021 tap_header->hdr_len = sizeof(gsmtap_hdr) / 4;
1022 tap_header->type = GSMTAP_TYPE_UM_BURST;
1023 tap_header->sub_type = burst_type;
1024
1025 bool dl_burst = !(input_nr >= d_cell_allocation.size());
1026 if (dl_burst) {
1027 tn = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
1028 frame_number = htobe32(d_burst_nr.get_frame_nr());
1029 arfcn = htobe16(d_cell_allocation[input_nr]);
1030 } else {
1031 input_nr -= d_cell_allocation.size();
1032 tn = static_cast<uint8_t>
1033 (d_burst_nr.subtract_timeslots(3).get_timeslot_nr());
1034 frame_number = htobe32(
1035 d_burst_nr.subtract_timeslots(3).get_frame_nr());
1036 arfcn = htobe16(
1037 d_cell_allocation[input_nr] | GSMTAP_ARFCN_F_UPLINK);
1038 }
1039
1040 tap_header->frame_number = frame_number;
1041 tap_header->timeslot = tn;
1042 tap_header->arfcn = arfcn;
1043
1044 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
1045 tap_header->snr_db = 0; /* FIXME: Can we calculate this? */
1046
Piotr Krysikdf978692017-09-27 21:58:24 +02001047 pmt::pmt_t pdu_header = pmt::make_dict();
1048
1049 /* Add timestamp of the first sample - if available */
1050 if(d_rx_time_received) {
1051 time_spec_t time_spec_of_first_sample = d_time_samp_ref.offset_to_time(nitems_read(0)+burst_start);
1052 uint64_t full = time_spec_of_first_sample.get_full_secs();
1053 double frac = time_spec_of_first_sample.get_frac_secs();
Piotr Krysik96f2ea72017-10-16 15:47:08 +02001054 pdu_header =
1055 pmt::dict_add(pdu_header, pmt::mp("fn_time"),
1056 pmt::cons(
1057 pmt::cons(pmt::from_uint64(be32toh(frame_number)), pmt::from_uint64(tn)),
1058 pmt::cons(pmt::from_uint64(full), pmt::from_double(frac))));
Piotr Krysikdf978692017-09-27 21:58:24 +02001059 }
1060
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001061 /* Copy burst to the buffer */
1062 memcpy(burst, burst_binary, BURST_SIZE);
1063
1064 /* Allocate a new message */
1065 pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE);
Piotr Krysik96f2ea72017-10-16 15:47:08 +02001066 pmt::pmt_t msg = pmt::cons(pdu_header, blob);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001067
1068 /* Send message */
1069 if (input_nr == 0)
ptrkrysike518bbf2014-11-06 14:50:59 +01001070 message_port_pub(pmt::mp("C0"), msg);
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001071 else
ptrkrysike518bbf2014-11-06 14:50:59 +01001072 message_port_pub(pmt::mp("CX"), msg);
1073 }
piotr6d152d92014-02-21 00:02:44 +01001074
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001075 void
1076 receiver_impl::configure_receiver(void)
1077 {
1078 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
1079 d_channel_conf.set_burst_types(TIMESLOT0, TEST51,
1080 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1081 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES,
1082 sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
1083 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES,
1084 sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
1085 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES,
1086 sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +01001087
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001088 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
1089 d_channel_conf.set_burst_types(TIMESLOT1, TEST51,
1090 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001091
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001092 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
1093 d_channel_conf.set_burst_types(TIMESLOT2, TEST51,
1094 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001095
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001096 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
1097 d_channel_conf.set_burst_types(TIMESLOT3, TEST51,
1098 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
ptrkrysike518bbf2014-11-06 14:50:59 +01001099
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001100 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
1101 d_channel_conf.set_burst_types(TIMESLOT4, TEST51,
1102 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrf2b6a1b2014-08-04 11:28:59 +02001103
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001104 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
1105 d_channel_conf.set_burst_types(TIMESLOT5, TEST51,
1106 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +01001107
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001108 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
1109 d_channel_conf.set_burst_types(TIMESLOT6, TEST51,
1110 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1111
1112 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
1113 d_channel_conf.set_burst_types(TIMESLOT7, TEST51,
1114 sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
1115 }
1116
1117 void
1118 receiver_impl::set_cell_allocation(
1119 const std::vector<int> &cell_allocation)
1120 {
1121 d_cell_allocation = cell_allocation;
1122 }
1123
1124 void
1125 receiver_impl::set_tseq_nums(const std::vector<int> &tseq_nums)
1126 {
1127 d_tseq_nums = tseq_nums;
1128 }
1129
1130 void
1131 receiver_impl::reset(void)
1132 {
1133 d_state = fcch_search;
1134 }
Vadim Yanitskiycc82cf02017-07-24 19:21:02 +07001135 } /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +01001136} /* namespace gr */