blob: 964634c2e2e05419c91b3ee0d16b0e2370371c16 [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
piotrd0bf1492014-02-05 17:27:32 +01002/*
piotrc1d47df2014-04-17 09:45:50 +02003 * Copyright 2014 Piotr Krysik <pkrysik@elka.pw.edu.pl>.
piotrd0bf1492014-02-05 17:27:32 +01004 *
piotr437f5462014-02-04 17:57:25 +01005 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
piotrd0bf1492014-02-05 17:27:32 +01009 *
piotr437f5462014-02-04 17:57:25 +010010 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
piotrd0bf1492014-02-05 17:27:32 +010014 *
piotr437f5462014-02-04 17:57:25 +010015 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <gnuradio/io_signature.h>
26#include "receiver_impl.h"
27
28#include <gnuradio/io_signature.h>
29#include <gnuradio/math.h>
30#include <math.h>
31#include <boost/circular_buffer.hpp>
32#include <algorithm>
33#include <numeric>
34#include <viterbi_detector.h>
35#include <string.h>
36#include <sch.h>
37#include <iostream>
38#include <iomanip>
piotr437f5462014-02-04 17:57:25 +010039#include <assert.h>
piotr6d152d92014-02-21 00:02:44 +010040#include <boost/scoped_ptr.hpp>
ptrkrysik58213792014-10-30 09:05:15 +010041
ptrkrysikd85d4602014-11-13 10:11:53 +010042//files included for debuging
43//#include "plotting/plotting.hpp"
44//#include <pthread.h>
piotr437f5462014-02-04 17:57:25 +010045
46#define SYNC_SEARCH_RANGE 30
47
piotrd0bf1492014-02-05 17:27:32 +010048namespace gr
49{
50namespace gsm
51{
piotr437f5462014-02-04 17:57:25 +010052
piotrd0bf1492014-02-05 17:27:32 +010053typedef std::list<float> list_float;
54typedef std::vector<float> vector_float;
piotr437f5462014-02-04 17:57:25 +010055
piotrd0bf1492014-02-05 17:27:32 +010056typedef boost::circular_buffer<float> circular_buffer_float;
piotr437f5462014-02-04 17:57:25 +010057
piotrd0bf1492014-02-05 17:27:32 +010058receiver::sptr
ptrkrysik7a7b9b02014-11-19 11:27:34 +010059receiver::make(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums)
piotrd0bf1492014-02-05 17:27:32 +010060{
61 return gnuradio::get_initial_sptr
ptrkrysike518bbf2014-11-06 14:50:59 +010062 (new receiver_impl(osr, cell_allocation, tseq_nums));
piotrd0bf1492014-02-05 17:27:32 +010063}
64
65/*
66 * The private constructor
67 */
ptrkrysik7a7b9b02014-11-19 11:27:34 +010068receiver_impl::receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums)
piotrc7c249a2014-05-02 17:24:08 +020069 : gr::sync_block("receiver",
ptrkrysik58213792014-10-30 09:05:15 +010070 gr::io_signature::make(1, -1, sizeof(gr_complex)),
piotr7c82b172014-02-08 14:15:27 +010071 gr::io_signature::make(0, 0, 0)),
piotrd0bf1492014-02-05 17:27:32 +010072 d_OSR(osr),
73 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
piotrd0bf1492014-02-05 17:27:32 +010074 d_counter(0),
75 d_fcch_start_pos(0),
piotr4089c1a2014-08-06 14:10:56 +020076 d_freq_offset_setting(0),
piotrd6d66872014-08-06 15:20:33 +020077 d_state(fcch_search),
piotrd0bf1492014-02-05 17:27:32 +010078 d_burst_nr(osr),
piotr6d152d92014-02-21 00:02:44 +010079 d_failed_sch(0),
ptrkrysike518bbf2014-11-06 14:50:59 +010080 d_signal_dbm(-120),
81 d_tseq_nums(tseq_nums),
82 d_cell_allocation(cell_allocation)
piotrd0bf1492014-02-05 17:27:32 +010083{
84 int i;
piotr4089c1a2014-08-06 14:10:56 +020085 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020086 set_output_multiple(floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR)); // burst and two gurad periods (one gurard period is an arbitrary overlap)
piotrd0bf1492014-02-05 17:27:32 +010087 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
88 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010089 {
piotrf502e0f2014-04-24 10:28:29 +020090 gr_complex startpoint = (train_seq[i][0]==0) ? gr_complex(1.0, 0.0) : gr_complex(-1.0, 0.0); //if first bit of the seqeunce ==0 first symbol ==1
piotr7f3f3662014-07-08 16:47:53 +020091 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010092 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010093 }
ptrkrysike518bbf2014-11-06 14:50:59 +010094 message_port_register_out(pmt::mp("C0"));
95 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +020096 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +020097 configure_receiver(); //configure the receiver - tell it where to find which burst type
piotrd0bf1492014-02-05 17:27:32 +010098}
piotr437f5462014-02-04 17:57:25 +010099
piotrd0bf1492014-02-05 17:27:32 +0100100/*
101 * Our virtual destructor.
102 */
103receiver_impl::~receiver_impl()
104{
105}
106
piotrd0bf1492014-02-05 17:27:32 +0100107int
piotrc7c249a2014-05-02 17:24:08 +0200108receiver_impl::work(int noutput_items,
109 gr_vector_const_void_star &input_items,
110 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100111{
ptrkrysik58213792014-10-30 09:05:15 +0100112// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
113 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200114 std::vector<tag_t> freq_offset_tags;
115 uint64_t start = nitems_read(0);
116 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100117
piotr4089c1a2014-08-06 14:10:56 +0200118 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
119 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
120 bool freq_offset_tag_in_fcch = false;
121 uint64_t tag_offset=-1; //-1 - just some clearly invalid value
122
123 if(!freq_offset_tags.empty()){
124 tag_t freq_offset_tag = freq_offset_tags[0];
125 tag_offset = freq_offset_tag.offset - start;
126
127 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
128 if(d_state == synchronized && b_type == fcch_burst){
129 uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
130 if(tag_offset < last_sample_nr){
piotr4089c1a2014-08-06 14:10:56 +0200131 freq_offset_tag_in_fcch = true;
132 }
133 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
134 } else {
135 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
136 }
137 }
138
piotrd0bf1492014-02-05 17:27:32 +0100139 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100140 {
piotrd0bf1492014-02-05 17:27:32 +0100141 //bootstrapping
ptrkrysik58213792014-10-30 09:05:15 +0100142 case fcch_search:
piotrd0bf1492014-02-05 17:27:32 +0100143 {
piotr4089c1a2014-08-06 14:10:56 +0200144 double freq_offset_tmp;
145 if (find_fcch_burst(input, noutput_items,freq_offset_tmp))
piotrd0bf1492014-02-05 17:27:32 +0100146 {
piotrd6d66872014-08-06 15:20:33 +0200147 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("fcch_search"));
piotr4089c1a2014-08-06 14:10:56 +0200148 message_port_pub(pmt::mp("measurements"), msg);
149
piotrd0bf1492014-02-05 17:27:32 +0100150 d_state = sch_search;
151 }
152 else
153 {
piotrd6d66872014-08-06 15:20:33 +0200154 d_state = fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100155 }
156 break;
157 }
piotr437f5462014-02-04 17:57:25 +0100158
piotrd0bf1492014-02-05 17:27:32 +0100159 case sch_search:
160 {
piotrd0bf1492014-02-05 17:27:32 +0100161 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
162 int t1, t2, t3;
163 int burst_start = 0;
164 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100165
piotrc7c249a2014-05-02 17:24:08 +0200166 if (reach_sch_burst(noutput_items)) //wait for a SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100167 {
168 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
169 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
170 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
171 {
piotr437f5462014-02-04 17:57:25 +0100172 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
piotr437f5462014-02-04 17:57:25 +0100173 d_burst_nr++;
174
piotr7f3f3662014-07-08 16:47:53 +0200175 consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period
piotr437f5462014-02-04 17:57:25 +0100176 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100177 }
178 else
179 {
piotrd6d66872014-08-06 15:20:33 +0200180 d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100181 }
piotrd0bf1492014-02-05 17:27:32 +0100182 }
183 else
184 {
185 d_state = sch_search;
186 }
187 break;
188 }
189 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
190 case synchronized:
191 {
piotrd0bf1492014-02-05 17:27:32 +0100192 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100193 int offset = 0;
194 int to_consume = 0;
195 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100196
ptrkrysik58213792014-10-30 09:05:15 +0100197 burst_type b_type;
piotr6d152d92014-02-21 00:02:44 +0100198
ptrkrysike518bbf2014-11-06 14:50:59 +0100199 for(int input_nr=0; input_nr<d_cell_allocation.size(); input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100200 {
ptrkrysik58213792014-10-30 09:05:15 +0100201 double signal_pwr = 0;
202 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200203
ptrkrysik58213792014-10-30 09:05:15 +0100204 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100205 {
ptrkrysik58213792014-10-30 09:05:15 +0100206 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100207 }
ptrkrysik58213792014-10-30 09:05:15 +0100208 signal_pwr = signal_pwr/(TS_BITS);
209 d_signal_dbm = round(10*log10(signal_pwr/50));
210 if(input_nr==0){
211 d_c0_signal_dbm = d_signal_dbm;
212 }
213
214 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100215 {
ptrkrysik58213792014-10-30 09:05:15 +0100216 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
217 }
218 else
219 {
220 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
221 }
222
223 switch (b_type)
224 {
225 case fcch_burst: //if it's FCCH burst
226 {
227 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
228 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
229 double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
230
ptrkrysik617ba032014-11-21 10:11:05 +0100231 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100232
233 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
234 message_port_pub(pmt::mp("measurements"), msg);
235 break;
236 }
237 case sch_burst: //if it's SCH burst
238 {
239 int t1, t2, t3, d_ncc, d_bcc;
240 d_c0_burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
241
242 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100243 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_SCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100244 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
piotrd0bf1492014-02-05 17:27:32 +0100245 {
ptrkrysik58213792014-10-30 09:05:15 +0100246 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
247 d_failed_sch = 0;
248 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
249 to_consume += offset; //adjust with offset number of samples to be consumed
piotr437f5462014-02-04 17:57:25 +0100250 }
ptrkrysik58213792014-10-30 09:05:15 +0100251 else
252 {
253 d_failed_sch++;
254 if (d_failed_sch >= MAX_SCH_ERRORS)
255 {
256 d_state = fcch_search;
257 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss"));
258 message_port_pub(pmt::mp("measurements"), msg);
259 DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
260 }
261 }
262 break;
piotr437f5462014-02-04 17:57:25 +0100263 }
ptrkrysik58213792014-10-30 09:05:15 +0100264 case normal_burst:
265 {
266 float normal_corr_max; //if it's normal burst
267 d_c0_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc); //get channel impulse response for given training sequence number - d_bcc
268 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100269 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100270 break;
271 }
272 case dummy_or_normal:
273 {
274 unsigned int normal_burst_start, dummy_burst_start;
275 float dummy_corr_max, normal_corr_max;
piotr437f5462014-02-04 17:57:25 +0100276
ptrkrysik58213792014-10-30 09:05:15 +0100277 dummy_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
278 normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc);
279
280 if (normal_corr_max > dummy_corr_max)
281 {
282 d_c0_burst_start = normal_burst_start;
283 detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100284 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100285 }
286 else
287 {
288 d_c0_burst_start = dummy_burst_start;
ptrkrysik617ba032014-11-21 10:11:05 +0100289 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100290 }
291 break;
piotrd0bf1492014-02-05 17:27:32 +0100292 }
ptrkrysik58213792014-10-30 09:05:15 +0100293 case rach_burst:
294 break;
295 case dummy:
ptrkrysik617ba032014-11-21 10:11:05 +0100296 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100297 break;
298 case normal_or_noise:
299 {
300 unsigned int burst_start;
301 float normal_corr_max_tmp;
302 float normal_corr_max=-1e6;
303 int max_tn;
304 std::vector<gr_complex> v(input, input + noutput_items);
305 if(d_signal_dbm>=d_c0_signal_dbm-13)
306 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100307 if(d_tseq_nums.size()==0) //there is no information about training sequence
308 { //however the receiver can detect it
309 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, 0);
310 float ts_max=normal_corr_max; //with use of a very simple algorithm based on finding
311 int ts_max_num=0; //maximum correlation
312 for(int ss=1; ss<=7; ss++)
313 {
314 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, ss);
315 if(ts_max<normal_corr_max)
316 {
317 ts_max = normal_corr_max;
318 ts_max_num = ss;
319 }
320 }
321 d_tseq_nums.push_back(ts_max_num);
ptrkrysik58213792014-10-30 09:05:15 +0100322 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100323 int tseq_num;
324 if(input_nr<=d_tseq_nums.size()){
325 tseq_num = d_tseq_nums[input_nr-1];
326 } else {
327 tseq_num = d_tseq_nums.back();
328 }
329 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
330// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
331 if((normal_corr_max/sqrt(signal_pwr))>=0.9){
332 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100333 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100334 }
ptrkrysik58213792014-10-30 09:05:15 +0100335 }
336 break;
337 }
338 case empty: //if it's empty burst
339 break; //do nothing
340 }
341
342 if(input_nr==0)
343 {
344 d_burst_nr++; //go to next burst
345 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
346 }
347
348 if(input_nr==input_items.size()-1)
349 {
350 consume_each(to_consume);
351 }
352 //and add offset which is introduced by
353 //0.25 fractional part of a guard period
354 }
piotrd0bf1492014-02-05 17:27:32 +0100355 }
356 break;
piotr437f5462014-02-04 17:57:25 +0100357 }
piotr6d152d92014-02-21 00:02:44 +0100358 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100359}
piotr437f5462014-02-04 17:57:25 +0100360
piotr4089c1a2014-08-06 14:10:56 +0200361bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100362{
363 circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find
364 //best match for FCCH burst
365 float phase_diff = 0;
366 gr_complex conjprod;
367 int start_pos = -1;
368 int hit_count = 0;
369 int miss_count = 0;
370 float min_phase_diff;
371 float max_phase_diff;
372 double best_sum = 0;
373 float lowest_max_min_diff = 99999;
374
375 int to_consume = 0;
376 int sample_number = 0;
377 bool end = false;
378 bool result = false;
379 circular_buffer_float::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100380
piotrd0bf1492014-02-05 17:27:32 +0100381 /**@name Possible states of FCCH search algorithm*/
382 //@{
383 enum states
piotr437f5462014-02-04 17:57:25 +0100384 {
piotr437f5462014-02-04 17:57:25 +0100385 init, ///< initialize variables
386 search, ///< search for positive samples
387 found_something, ///< search for FCCH and the best position of it
388 fcch_found, ///< when FCCH was found
389 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100390 } fcch_search_state;
391 //@}
piotr437f5462014-02-04 17:57:25 +0100392
piotrd0bf1492014-02-05 17:27:32 +0100393 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100394
piotrd0bf1492014-02-05 17:27:32 +0100395 while (!end)
396 {
397 switch (fcch_search_state)
398 {
piotr437f5462014-02-04 17:57:25 +0100399
piotrd0bf1492014-02-05 17:27:32 +0100400 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100401 hit_count = 0;
402 miss_count = 0;
403 start_pos = -1;
404 lowest_max_min_diff = 99999;
405 phase_diff_buffer.clear();
406 fcch_search_state = search;
407
408 break;
409
piotr7c82b172014-02-08 14:15:27 +0100410 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100411 sample_number++;
412
piotrd0bf1492014-02-05 17:27:32 +0100413 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
414 {
piotr7c82b172014-02-08 14:15:27 +0100415 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100416 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100417 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100418 fcch_search_state = search_fail;
419 }
420 else
421 {
422 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100423
piotrd0bf1492014-02-05 17:27:32 +0100424 if (phase_diff > 0) //if a positive phase difference was found
425 {
426 to_consume = sample_number;
427 fcch_search_state = found_something; //switch to state in which searches for FCCH
428 }
429 else
430 {
431 fcch_search_state = search;
432 }
piotr437f5462014-02-04 17:57:25 +0100433 }
434
435 break;
436
piotrd0bf1492014-02-05 17:27:32 +0100437 case found_something: // search for FCCH and the best position of it
438 {
439 if (phase_diff > 0)
440 {
piotr437f5462014-02-04 17:57:25 +0100441 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100442 }
443 else
444 {
piotr437f5462014-02-04 17:57:25 +0100445 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100446 }
piotr437f5462014-02-04 17:57:25 +0100447
piotrd0bf1492014-02-05 17:27:32 +0100448 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
449 {
piotr437f5462014-02-04 17:57:25 +0100450 //if miss_count exceeds limit before hit_count
451 fcch_search_state = init; //go to init
452 continue;
piotrd0bf1492014-02-05 17:27:32 +0100453 }
454 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
455 {
piotr437f5462014-02-04 17:57:25 +0100456 //if hit_count and miss_count exceeds limit then FCCH was found
457 fcch_search_state = fcch_found;
458 continue;
piotrd0bf1492014-02-05 17:27:32 +0100459 }
460 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
461 {
piotr437f5462014-02-04 17:57:25 +0100462 //find difference between minimal and maximal element in the buffer
463 //for FCCH this value should be low
464 //this part is searching for a region where this value is lowest
465 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
466 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
467
piotrd0bf1492014-02-05 17:27:32 +0100468 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
469 {
470 lowest_max_min_diff = max_phase_diff - min_phase_diff;
471 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
472 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100473
piotrd0bf1492014-02-05 17:27:32 +0100474 for (buffer_iter = phase_diff_buffer.begin();
475 buffer_iter != (phase_diff_buffer.end());
476 buffer_iter++)
477 {
478 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
479 }
piotr437f5462014-02-04 17:57:25 +0100480 }
piotrd0bf1492014-02-05 17:27:32 +0100481 }
piotr437f5462014-02-04 17:57:25 +0100482
piotrd0bf1492014-02-05 17:27:32 +0100483 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100484
piotrd0bf1492014-02-05 17:27:32 +0100485 if (sample_number >= nitems) //if there's no single sample left to check
486 {
piotr437f5462014-02-04 17:57:25 +0100487 fcch_search_state = search_fail;//FCCH search failed
488 continue;
piotr437f5462014-02-04 17:57:25 +0100489 }
piotrd0bf1492014-02-05 17:27:32 +0100490
491 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
492 phase_diff_buffer.push_back(phase_diff);
493 fcch_search_state = found_something;
494 }
495 break;
496
497 case fcch_found:
498 {
piotrd0bf1492014-02-05 17:27:32 +0100499 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
500
501 d_fcch_start_pos = d_counter + start_pos;
502
503 //compute frequency offset
504 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200505 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
506 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100507
508 end = true;
509 result = true;
piotr437f5462014-02-04 17:57:25 +0100510 break;
piotrd0bf1492014-02-05 17:27:32 +0100511 }
piotr437f5462014-02-04 17:57:25 +0100512
piotrd0bf1492014-02-05 17:27:32 +0100513 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100514 end = true;
515 result = false;
516 break;
517 }
piotr437f5462014-02-04 17:57:25 +0100518 }
519
piotrd0bf1492014-02-05 17:27:32 +0100520 d_counter += to_consume;
521 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100522
piotrd0bf1492014-02-05 17:27:32 +0100523 return result;
524}
525
piotrd0bf1492014-02-05 17:27:32 +0100526double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
527{
528 double phase_sum = 0;
529 unsigned ii;
530
531 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100532 {
piotr437f5462014-02-04 17:57:25 +0100533 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
534 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100535 }
536
piotrd0bf1492014-02-05 17:27:32 +0100537 double phase_offset = phase_sum / (last_sample - first_sample);
538 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
539 return freq_offset;
540}
piotr437f5462014-02-04 17:57:25 +0100541
piotrd0bf1492014-02-05 17:27:32 +0100542inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
543{
544 gr_complex conjprod = val1 * conj(val2);
545 return fast_atan2f(imag(conjprod), real(conjprod));
546}
piotr437f5462014-02-04 17:57:25 +0100547
piotrd0bf1492014-02-05 17:27:32 +0100548bool receiver_impl::reach_sch_burst(const int nitems)
549{
550 //it just consumes samples to get near to a SCH burst
551 int to_consume = 0;
552 bool result = false;
553 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
554
555 //consume samples until d_counter will be equal to sample_nr_near_sch_start
556 if (d_counter < sample_nr_near_sch_start)
557 {
558 if (d_counter + nitems >= sample_nr_near_sch_start)
559 {
560 to_consume = sample_nr_near_sch_start - d_counter;
561 }
562 else
563 {
564 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100565 }
566 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100567 }
568 else
569 {
piotr437f5462014-02-04 17:57:25 +0100570 to_consume = 0;
571 result = true;
piotr437f5462014-02-04 17:57:25 +0100572 }
573
piotrd0bf1492014-02-05 17:27:32 +0100574 d_counter += to_consume;
575 consume_each(to_consume);
576 return result;
577}
578
579int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
580{
581 vector_complex correlation_buffer;
582 vector_float power_buffer;
583 vector_float window_energy_buffer;
584
585 int strongest_window_nr;
586 int burst_start = 0;
587 int chan_imp_resp_center = 0;
588 float max_correlation = 0;
589 float energy = 0;
590
591 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100592 {
piotr437f5462014-02-04 17:57:25 +0100593 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
594 correlation_buffer.push_back(correlation);
595 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100596 }
piotrd0bf1492014-02-05 17:27:32 +0100597 //compute window energies
598 vector_float::iterator iter = power_buffer.begin();
599 bool loop_end = false;
600 while (iter != power_buffer.end())
601 {
piotr437f5462014-02-04 17:57:25 +0100602 vector_float::iterator iter_ii = iter;
603 energy = 0;
604
piotrd0bf1492014-02-05 17:27:32 +0100605 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
606 {
607 if (iter_ii == power_buffer.end())
608 {
609 loop_end = true;
610 break;
611 }
612 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100613 }
piotrd0bf1492014-02-05 17:27:32 +0100614 if (loop_end)
615 {
616 break;
piotr437f5462014-02-04 17:57:25 +0100617 }
618 iter++;
619 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100620 }
piotr437f5462014-02-04 17:57:25 +0100621
piotrd0bf1492014-02-05 17:27:32 +0100622 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100623 // d_channel_imp_resp.clear();
624
piotrd0bf1492014-02-05 17:27:32 +0100625 max_correlation = 0;
626 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
627 {
piotr437f5462014-02-04 17:57:25 +0100628 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100629 if (abs(correlation) > max_correlation)
630 {
631 chan_imp_resp_center = ii;
632 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100633 }
piotrd0bf1492014-02-05 17:27:32 +0100634 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100635 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100636 }
637
piotrd0bf1492014-02-05 17:27:32 +0100638 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
639 return burst_start;
640}
piotr437f5462014-02-04 17:57:25 +0100641
642
piotrd0bf1492014-02-05 17:27:32 +0100643void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
644{
645 float output[BURST_SIZE];
646 gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR];
647 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
648 gr_complex filtered_burst[BURST_SIZE];
649 int start_state = 3;
650 unsigned int stop_states[2] = {4, 12};
651
652 autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR);
653 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100654 {
piotr437f5462014-02-04 17:57:25 +0100655 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100656 }
657
piotrd0bf1492014-02-05 17:27:32 +0100658 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
659
660 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
661
662 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100663 {
piotrd0bf1492014-02-05 17:27:32 +0100664 output_binary[i] = (output[i] > 0);
665 }
666}
piotr437f5462014-02-04 17:57:25 +0100667
piotrd0bf1492014-02-05 17:27:32 +0100668void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
669{
670 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100671
piotrd0bf1492014-02-05 17:27:32 +0100672 int current_symbol;
673 int encoded_symbol;
674 int previous_symbol = 2 * input[0] - 1;
675 gmsk_output[0] = start_point;
676
677 for (int i = 1; i < nitems; i++)
678 {
piotr437f5462014-02-04 17:57:25 +0100679 //change bits representation to NRZ
680 current_symbol = 2 * input[i] - 1;
681 //differentially encode
682 encoded_symbol = current_symbol * previous_symbol;
683 //and do gmsk mapping
684 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
685 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100686 }
piotrd0bf1492014-02-05 17:27:32 +0100687}
piotr437f5462014-02-04 17:57:25 +0100688
piotrd0bf1492014-02-05 17:27:32 +0100689gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
690{
691 gr_complex result(0.0, 0.0);
692 int sample_number = 0;
693
694 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100695 {
piotr437f5462014-02-04 17:57:25 +0100696 sample_number = (ii * d_OSR) ;
697 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100698 }
699
piotrd0bf1492014-02-05 17:27:32 +0100700 result = result / gr_complex(length, 0);
701 return result;
702}
703
704//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100705inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
706{
707 int i, k;
708 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100709 {
piotr437f5462014-02-04 17:57:25 +0100710 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100711 for (i = k; i < nitems; i++)
712 {
713 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100714 }
piotr437f5462014-02-04 17:57:25 +0100715 }
piotrd0bf1492014-02-05 17:27:32 +0100716}
piotr437f5462014-02-04 17:57:25 +0100717
piotrd0bf1492014-02-05 17:27:32 +0100718inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
719{
720 int ii = 0, n, a;
721
722 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100723 {
piotr437f5462014-02-04 17:57:25 +0100724 a = n * d_OSR;
725 output[n] = 0;
726 ii = 0;
727
piotrd0bf1492014-02-05 17:27:32 +0100728 while (ii < filter_length)
729 {
piotrda8a0662014-04-24 10:29:38 +0200730 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100731 break;
piotrda8a0662014-04-24 10:29:38 +0200732 }
piotrd0bf1492014-02-05 17:27:32 +0100733 output[n] += input[a+ii] * filter[ii];
734 ii++;
piotr437f5462014-02-04 17:57:25 +0100735 }
piotr437f5462014-02-04 17:57:25 +0100736 }
piotrd0bf1492014-02-05 17:27:32 +0100737}
piotr437f5462014-02-04 17:57:25 +0100738
piotrd0bf1492014-02-05 17:27:32 +0100739//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100740int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, float *corr_max, int bcc)
piotrd0bf1492014-02-05 17:27:32 +0100741{
742 vector_complex correlation_buffer;
743 vector_float power_buffer;
744 vector_float window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100745
piotrd0bf1492014-02-05 17:27:32 +0100746 int strongest_window_nr;
747 int burst_start = 0;
748 int chan_imp_resp_center = 0;
749 float max_correlation = 0;
750 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200751
piotrd0bf1492014-02-05 17:27:32 +0100752 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100753 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100754 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200755 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100756
ptrkrysik58213792014-10-30 09:05:15 +0100757 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100758 {
piotr437f5462014-02-04 17:57:25 +0100759 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100760 correlation_buffer.push_back(correlation);
761 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100762 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100763// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100764 //compute window energies
765 vector_float::iterator iter = power_buffer.begin();
766 bool loop_end = false;
767 while (iter != power_buffer.end())
768 {
piotr437f5462014-02-04 17:57:25 +0100769 vector_float::iterator iter_ii = iter;
770 energy = 0;
771
piotrd0bf1492014-02-05 17:27:32 +0100772 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++)
773 {
piotrd0bf1492014-02-05 17:27:32 +0100774 if (iter_ii == power_buffer.end())
775 {
776 loop_end = true;
777 break;
778 }
779 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100780 }
piotrd0bf1492014-02-05 17:27:32 +0100781 if (loop_end)
782 {
783 break;
piotr437f5462014-02-04 17:57:25 +0100784 }
785 iter++;
786
787 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100788 }
piotr437f5462014-02-04 17:57:25 +0100789
piotr5c820252014-04-17 09:43:02 +0200790 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
791 //strongest_window_nr = strongest_window_nr-d_OSR;
792 if(strongest_window_nr<0){
793 strongest_window_nr = 0;
794 }
piotr6d152d92014-02-21 00:02:44 +0100795
piotrd0bf1492014-02-05 17:27:32 +0100796 max_correlation = 0;
797 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
798 {
piotr437f5462014-02-04 17:57:25 +0100799 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100800 if (abs(correlation) > max_correlation)
801 {
802 chan_imp_resp_center = ii;
803 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100804 }
piotrd0bf1492014-02-05 17:27:32 +0100805 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100806 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100807 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100808
piotr7e3b0db2014-02-05 22:44:30 +0100809 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100810
ptrkrysik58213792014-10-30 09:05:15 +0100811 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200812 burst_start = search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR; //compute first sample posiiton which corresponds to the first sample of the impulse response
piotr7c82b172014-02-08 14:15:27 +0100813
ptrkrysik58213792014-10-30 09:05:15 +0100814 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100815 return burst_start;
816}
piotr437f5462014-02-04 17:57:25 +0100817
818
ptrkrysik617ba032014-11-21 10:11:05 +0100819void receiver_impl::send_burst(burst_counter burst_nr, const unsigned char * burst_binary, uint8_t burst_type, unsigned int input_nr)
piotrd0bf1492014-02-05 17:27:32 +0100820{
piotr6d152d92014-02-21 00:02:44 +0100821 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100822
piotr6d152d92014-02-21 00:02:44 +0100823 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100824 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100825 tap_header->type = GSMTAP_TYPE_UM_BURST;
826 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100827 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
ptrkrysik617ba032014-11-21 10:11:05 +0100828 tap_header->sub_type = burst_type;
829 std::cout << static_cast<int>(tap_header->sub_type) << std::endl;
830 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
piotr6d152d92014-02-21 00:02:44 +0100831 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100832 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100833
834 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
835 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
836 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
837
838 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
839 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200840
ptrkrysike518bbf2014-11-06 14:50:59 +0100841 if(input_nr==0){
842 message_port_pub(pmt::mp("C0"), msg);
843 } else {
844 message_port_pub(pmt::mp("CX"), msg);
845 }
piotrd0bf1492014-02-05 17:27:32 +0100846}
piotr6d152d92014-02-21 00:02:44 +0100847
piotrd0bf1492014-02-05 17:27:32 +0100848void receiver_impl::configure_receiver()
849{
piotrce92f982014-04-17 23:37:18 +0200850 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100851 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100852
piotrce92f982014-04-17 23:37:18 +0200853 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
854 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
855 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100856
piotrd0bf1492014-02-05 17:27:32 +0100857 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
858 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
859 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
860 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
861 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
862 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
863 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
864 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
865 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
866 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
867 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
868 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
869 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
870 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100871}
piotr437f5462014-02-04 17:57:25 +0100872
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100873void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200874{
ptrkrysike518bbf2014-11-06 14:50:59 +0100875 d_cell_allocation = cell_allocation;
876}
877
878void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
879{
880 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200881}
882
883void receiver_impl::reset()
884{
piotrd6d66872014-08-06 15:20:33 +0200885 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200886}
piotr437f5462014-02-04 17:57:25 +0100887
piotrd0bf1492014-02-05 17:27:32 +0100888} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100889} /* namespace gr */
890