blob: 42592c4dbbaa5954bba3db114906d752a4202612 [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
4 * @author Piotr Krysik <ptrkrysik@gmail.com>
5 * @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>
28#include "receiver_impl.h"
29
30#include <gnuradio/io_signature.h>
31#include <gnuradio/math.h>
32#include <math.h>
33#include <boost/circular_buffer.hpp>
34#include <algorithm>
35#include <numeric>
36#include <viterbi_detector.h>
37#include <string.h>
38#include <sch.h>
39#include <iostream>
40#include <iomanip>
piotr437f5462014-02-04 17:57:25 +010041#include <assert.h>
piotr6d152d92014-02-21 00:02:44 +010042#include <boost/scoped_ptr.hpp>
ptrkrysik58213792014-10-30 09:05:15 +010043
ptrkrysikd85d4602014-11-13 10:11:53 +010044//files included for debuging
45//#include "plotting/plotting.hpp"
46//#include <pthread.h>
piotr437f5462014-02-04 17:57:25 +010047
48#define SYNC_SEARCH_RANGE 30
49
piotrd0bf1492014-02-05 17:27:32 +010050namespace gr
51{
52namespace gsm
53{
piotr437f5462014-02-04 17:57:25 +010054
piotrd0bf1492014-02-05 17:27:32 +010055typedef std::list<float> list_float;
56typedef std::vector<float> vector_float;
piotr437f5462014-02-04 17:57:25 +010057
piotrd0bf1492014-02-05 17:27:32 +010058typedef boost::circular_buffer<float> circular_buffer_float;
piotr437f5462014-02-04 17:57:25 +010059
piotrd0bf1492014-02-05 17:27:32 +010060receiver::sptr
ptrkrysik7a7b9b02014-11-19 11:27:34 +010061receiver::make(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums)
piotrd0bf1492014-02-05 17:27:32 +010062{
63 return gnuradio::get_initial_sptr
ptrkrysike518bbf2014-11-06 14:50:59 +010064 (new receiver_impl(osr, cell_allocation, tseq_nums));
piotrd0bf1492014-02-05 17:27:32 +010065}
66
67/*
68 * The private constructor
69 */
ptrkrysik7a7b9b02014-11-19 11:27:34 +010070receiver_impl::receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums)
piotrc7c249a2014-05-02 17:24:08 +020071 : gr::sync_block("receiver",
ptrkrysik58213792014-10-30 09:05:15 +010072 gr::io_signature::make(1, -1, sizeof(gr_complex)),
piotr7c82b172014-02-08 14:15:27 +010073 gr::io_signature::make(0, 0, 0)),
piotrd0bf1492014-02-05 17:27:32 +010074 d_OSR(osr),
75 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
piotrd0bf1492014-02-05 17:27:32 +010076 d_counter(0),
77 d_fcch_start_pos(0),
piotr4089c1a2014-08-06 14:10:56 +020078 d_freq_offset_setting(0),
piotrd6d66872014-08-06 15:20:33 +020079 d_state(fcch_search),
piotrd0bf1492014-02-05 17:27:32 +010080 d_burst_nr(osr),
piotr6d152d92014-02-21 00:02:44 +010081 d_failed_sch(0),
ptrkrysike518bbf2014-11-06 14:50:59 +010082 d_signal_dbm(-120),
83 d_tseq_nums(tseq_nums),
84 d_cell_allocation(cell_allocation)
piotrd0bf1492014-02-05 17:27:32 +010085{
86 int i;
piotr4089c1a2014-08-06 14:10:56 +020087 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020088 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 +010089 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
90 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010091 {
piotrf502e0f2014-04-24 10:28:29 +020092 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 +020093 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010094 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010095 }
ptrkrysike518bbf2014-11-06 14:50:59 +010096 message_port_register_out(pmt::mp("C0"));
97 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +020098 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +020099 configure_receiver(); //configure the receiver - tell it where to find which burst type
piotrd0bf1492014-02-05 17:27:32 +0100100}
piotr437f5462014-02-04 17:57:25 +0100101
piotrd0bf1492014-02-05 17:27:32 +0100102/*
103 * Our virtual destructor.
104 */
105receiver_impl::~receiver_impl()
106{
107}
108
piotrd0bf1492014-02-05 17:27:32 +0100109int
piotrc7c249a2014-05-02 17:24:08 +0200110receiver_impl::work(int noutput_items,
111 gr_vector_const_void_star &input_items,
112 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100113{
ptrkrysik58213792014-10-30 09:05:15 +0100114// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
115 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200116 std::vector<tag_t> freq_offset_tags;
117 uint64_t start = nitems_read(0);
118 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100119
piotr4089c1a2014-08-06 14:10:56 +0200120 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
121 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
122 bool freq_offset_tag_in_fcch = false;
123 uint64_t tag_offset=-1; //-1 - just some clearly invalid value
124
125 if(!freq_offset_tags.empty()){
126 tag_t freq_offset_tag = freq_offset_tags[0];
127 tag_offset = freq_offset_tag.offset - start;
128
129 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
130 if(d_state == synchronized && b_type == fcch_burst){
131 uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
132 if(tag_offset < last_sample_nr){
piotr4089c1a2014-08-06 14:10:56 +0200133 freq_offset_tag_in_fcch = true;
134 }
135 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
136 } else {
137 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
138 }
139 }
140
piotrd0bf1492014-02-05 17:27:32 +0100141 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100142 {
piotrd0bf1492014-02-05 17:27:32 +0100143 //bootstrapping
ptrkrysik58213792014-10-30 09:05:15 +0100144 case fcch_search:
piotrd0bf1492014-02-05 17:27:32 +0100145 {
piotr4089c1a2014-08-06 14:10:56 +0200146 double freq_offset_tmp;
147 if (find_fcch_burst(input, noutput_items,freq_offset_tmp))
piotrd0bf1492014-02-05 17:27:32 +0100148 {
piotrd6d66872014-08-06 15:20:33 +0200149 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 +0200150 message_port_pub(pmt::mp("measurements"), msg);
151
piotrd0bf1492014-02-05 17:27:32 +0100152 d_state = sch_search;
153 }
154 else
155 {
piotrd6d66872014-08-06 15:20:33 +0200156 d_state = fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100157 }
158 break;
159 }
piotr437f5462014-02-04 17:57:25 +0100160
piotrd0bf1492014-02-05 17:27:32 +0100161 case sch_search:
162 {
piotrd0bf1492014-02-05 17:27:32 +0100163 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
164 int t1, t2, t3;
165 int burst_start = 0;
166 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100167
piotrc7c249a2014-05-02 17:24:08 +0200168 if (reach_sch_burst(noutput_items)) //wait for a SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100169 {
170 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
171 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
172 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
173 {
piotr437f5462014-02-04 17:57:25 +0100174 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
piotr437f5462014-02-04 17:57:25 +0100175 d_burst_nr++;
176
piotr7f3f3662014-07-08 16:47:53 +0200177 consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period
piotr437f5462014-02-04 17:57:25 +0100178 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100179 }
180 else
181 {
piotrd6d66872014-08-06 15:20:33 +0200182 d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100183 }
piotrd0bf1492014-02-05 17:27:32 +0100184 }
185 else
186 {
187 d_state = sch_search;
188 }
189 break;
190 }
191 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
192 case synchronized:
193 {
piotrd0bf1492014-02-05 17:27:32 +0100194 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100195 int offset = 0;
196 int to_consume = 0;
197 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100198
ptrkrysik58213792014-10-30 09:05:15 +0100199 burst_type b_type;
piotr6d152d92014-02-21 00:02:44 +0100200
ptrkrysike518bbf2014-11-06 14:50:59 +0100201 for(int input_nr=0; input_nr<d_cell_allocation.size(); input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100202 {
ptrkrysik58213792014-10-30 09:05:15 +0100203 double signal_pwr = 0;
204 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200205
ptrkrysik58213792014-10-30 09:05:15 +0100206 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100207 {
ptrkrysik58213792014-10-30 09:05:15 +0100208 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100209 }
ptrkrysik58213792014-10-30 09:05:15 +0100210 signal_pwr = signal_pwr/(TS_BITS);
211 d_signal_dbm = round(10*log10(signal_pwr/50));
212 if(input_nr==0){
213 d_c0_signal_dbm = d_signal_dbm;
214 }
215
216 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100217 {
ptrkrysik58213792014-10-30 09:05:15 +0100218 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
219 }
220 else
221 {
222 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
223 }
224
225 switch (b_type)
226 {
227 case fcch_burst: //if it's FCCH burst
228 {
229 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
230 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
231 double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
232
ptrkrysik617ba032014-11-21 10:11:05 +0100233 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100234
235 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
236 message_port_pub(pmt::mp("measurements"), msg);
237 break;
238 }
239 case sch_burst: //if it's SCH burst
240 {
241 int t1, t2, t3, d_ncc, d_bcc;
242 d_c0_burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
243
244 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100245 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_SCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100246 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
piotrd0bf1492014-02-05 17:27:32 +0100247 {
ptrkrysik58213792014-10-30 09:05:15 +0100248 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
249 d_failed_sch = 0;
250 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
251 to_consume += offset; //adjust with offset number of samples to be consumed
piotr437f5462014-02-04 17:57:25 +0100252 }
ptrkrysik58213792014-10-30 09:05:15 +0100253 else
254 {
255 d_failed_sch++;
256 if (d_failed_sch >= MAX_SCH_ERRORS)
257 {
258 d_state = fcch_search;
259 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss"));
260 message_port_pub(pmt::mp("measurements"), msg);
261 DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
262 }
263 }
264 break;
piotr437f5462014-02-04 17:57:25 +0100265 }
ptrkrysik58213792014-10-30 09:05:15 +0100266 case normal_burst:
267 {
268 float normal_corr_max; //if it's normal burst
269 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
270 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100271 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100272 break;
273 }
274 case dummy_or_normal:
275 {
276 unsigned int normal_burst_start, dummy_burst_start;
277 float dummy_corr_max, normal_corr_max;
piotr437f5462014-02-04 17:57:25 +0100278
ptrkrysik58213792014-10-30 09:05:15 +0100279 dummy_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
280 normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc);
281
282 if (normal_corr_max > dummy_corr_max)
283 {
284 d_c0_burst_start = normal_burst_start;
285 detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100286 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100287 }
288 else
289 {
290 d_c0_burst_start = dummy_burst_start;
ptrkrysik617ba032014-11-21 10:11:05 +0100291 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100292 }
293 break;
piotrd0bf1492014-02-05 17:27:32 +0100294 }
ptrkrysik58213792014-10-30 09:05:15 +0100295 case rach_burst:
296 break;
297 case dummy:
ptrkrysik617ba032014-11-21 10:11:05 +0100298 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100299 break;
300 case normal_or_noise:
301 {
302 unsigned int burst_start;
303 float normal_corr_max_tmp;
304 float normal_corr_max=-1e6;
305 int max_tn;
306 std::vector<gr_complex> v(input, input + noutput_items);
307 if(d_signal_dbm>=d_c0_signal_dbm-13)
308 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100309 if(d_tseq_nums.size()==0) //there is no information about training sequence
310 { //however the receiver can detect it
311 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, 0);
312 float ts_max=normal_corr_max; //with use of a very simple algorithm based on finding
313 int ts_max_num=0; //maximum correlation
314 for(int ss=1; ss<=7; ss++)
315 {
316 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, ss);
317 if(ts_max<normal_corr_max)
318 {
319 ts_max = normal_corr_max;
320 ts_max_num = ss;
321 }
322 }
323 d_tseq_nums.push_back(ts_max_num);
ptrkrysik58213792014-10-30 09:05:15 +0100324 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100325 int tseq_num;
326 if(input_nr<=d_tseq_nums.size()){
327 tseq_num = d_tseq_nums[input_nr-1];
328 } else {
329 tseq_num = d_tseq_nums.back();
330 }
331 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
332// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
333 if((normal_corr_max/sqrt(signal_pwr))>=0.9){
334 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100335 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100336 }
ptrkrysik58213792014-10-30 09:05:15 +0100337 }
338 break;
339 }
340 case empty: //if it's empty burst
341 break; //do nothing
342 }
343
344 if(input_nr==0)
345 {
346 d_burst_nr++; //go to next burst
347 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
348 }
349
350 if(input_nr==input_items.size()-1)
351 {
352 consume_each(to_consume);
353 }
354 //and add offset which is introduced by
355 //0.25 fractional part of a guard period
356 }
piotrd0bf1492014-02-05 17:27:32 +0100357 }
358 break;
piotr437f5462014-02-04 17:57:25 +0100359 }
piotr6d152d92014-02-21 00:02:44 +0100360 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100361}
piotr437f5462014-02-04 17:57:25 +0100362
piotr4089c1a2014-08-06 14:10:56 +0200363bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100364{
365 circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find
366 //best match for FCCH burst
367 float phase_diff = 0;
368 gr_complex conjprod;
369 int start_pos = -1;
370 int hit_count = 0;
371 int miss_count = 0;
372 float min_phase_diff;
373 float max_phase_diff;
374 double best_sum = 0;
375 float lowest_max_min_diff = 99999;
376
377 int to_consume = 0;
378 int sample_number = 0;
379 bool end = false;
380 bool result = false;
381 circular_buffer_float::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100382
piotrd0bf1492014-02-05 17:27:32 +0100383 /**@name Possible states of FCCH search algorithm*/
384 //@{
385 enum states
piotr437f5462014-02-04 17:57:25 +0100386 {
piotr437f5462014-02-04 17:57:25 +0100387 init, ///< initialize variables
388 search, ///< search for positive samples
389 found_something, ///< search for FCCH and the best position of it
390 fcch_found, ///< when FCCH was found
391 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100392 } fcch_search_state;
393 //@}
piotr437f5462014-02-04 17:57:25 +0100394
piotrd0bf1492014-02-05 17:27:32 +0100395 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100396
piotrd0bf1492014-02-05 17:27:32 +0100397 while (!end)
398 {
399 switch (fcch_search_state)
400 {
piotr437f5462014-02-04 17:57:25 +0100401
piotrd0bf1492014-02-05 17:27:32 +0100402 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100403 hit_count = 0;
404 miss_count = 0;
405 start_pos = -1;
406 lowest_max_min_diff = 99999;
407 phase_diff_buffer.clear();
408 fcch_search_state = search;
409
410 break;
411
piotr7c82b172014-02-08 14:15:27 +0100412 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100413 sample_number++;
414
piotrd0bf1492014-02-05 17:27:32 +0100415 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
416 {
piotr7c82b172014-02-08 14:15:27 +0100417 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100418 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100419 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100420 fcch_search_state = search_fail;
421 }
422 else
423 {
424 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100425
piotrd0bf1492014-02-05 17:27:32 +0100426 if (phase_diff > 0) //if a positive phase difference was found
427 {
428 to_consume = sample_number;
429 fcch_search_state = found_something; //switch to state in which searches for FCCH
430 }
431 else
432 {
433 fcch_search_state = search;
434 }
piotr437f5462014-02-04 17:57:25 +0100435 }
436
437 break;
438
piotrd0bf1492014-02-05 17:27:32 +0100439 case found_something: // search for FCCH and the best position of it
440 {
441 if (phase_diff > 0)
442 {
piotr437f5462014-02-04 17:57:25 +0100443 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100444 }
445 else
446 {
piotr437f5462014-02-04 17:57:25 +0100447 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100448 }
piotr437f5462014-02-04 17:57:25 +0100449
piotrd0bf1492014-02-05 17:27:32 +0100450 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
451 {
piotr437f5462014-02-04 17:57:25 +0100452 //if miss_count exceeds limit before hit_count
453 fcch_search_state = init; //go to init
454 continue;
piotrd0bf1492014-02-05 17:27:32 +0100455 }
456 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
457 {
piotr437f5462014-02-04 17:57:25 +0100458 //if hit_count and miss_count exceeds limit then FCCH was found
459 fcch_search_state = fcch_found;
460 continue;
piotrd0bf1492014-02-05 17:27:32 +0100461 }
462 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
463 {
piotr437f5462014-02-04 17:57:25 +0100464 //find difference between minimal and maximal element in the buffer
465 //for FCCH this value should be low
466 //this part is searching for a region where this value is lowest
467 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
468 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
469
piotrd0bf1492014-02-05 17:27:32 +0100470 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
471 {
472 lowest_max_min_diff = max_phase_diff - min_phase_diff;
473 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
474 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100475
piotrd0bf1492014-02-05 17:27:32 +0100476 for (buffer_iter = phase_diff_buffer.begin();
477 buffer_iter != (phase_diff_buffer.end());
478 buffer_iter++)
479 {
480 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
481 }
piotr437f5462014-02-04 17:57:25 +0100482 }
piotrd0bf1492014-02-05 17:27:32 +0100483 }
piotr437f5462014-02-04 17:57:25 +0100484
piotrd0bf1492014-02-05 17:27:32 +0100485 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100486
piotrd0bf1492014-02-05 17:27:32 +0100487 if (sample_number >= nitems) //if there's no single sample left to check
488 {
piotr437f5462014-02-04 17:57:25 +0100489 fcch_search_state = search_fail;//FCCH search failed
490 continue;
piotr437f5462014-02-04 17:57:25 +0100491 }
piotrd0bf1492014-02-05 17:27:32 +0100492
493 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
494 phase_diff_buffer.push_back(phase_diff);
495 fcch_search_state = found_something;
496 }
497 break;
498
499 case fcch_found:
500 {
piotrd0bf1492014-02-05 17:27:32 +0100501 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
502
503 d_fcch_start_pos = d_counter + start_pos;
504
505 //compute frequency offset
506 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200507 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
508 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100509
510 end = true;
511 result = true;
piotr437f5462014-02-04 17:57:25 +0100512 break;
piotrd0bf1492014-02-05 17:27:32 +0100513 }
piotr437f5462014-02-04 17:57:25 +0100514
piotrd0bf1492014-02-05 17:27:32 +0100515 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100516 end = true;
517 result = false;
518 break;
519 }
piotr437f5462014-02-04 17:57:25 +0100520 }
521
piotrd0bf1492014-02-05 17:27:32 +0100522 d_counter += to_consume;
523 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100524
piotrd0bf1492014-02-05 17:27:32 +0100525 return result;
526}
527
piotrd0bf1492014-02-05 17:27:32 +0100528double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
529{
530 double phase_sum = 0;
531 unsigned ii;
532
533 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100534 {
piotr437f5462014-02-04 17:57:25 +0100535 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
536 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100537 }
538
piotrd0bf1492014-02-05 17:27:32 +0100539 double phase_offset = phase_sum / (last_sample - first_sample);
540 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
541 return freq_offset;
542}
piotr437f5462014-02-04 17:57:25 +0100543
piotrd0bf1492014-02-05 17:27:32 +0100544inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
545{
546 gr_complex conjprod = val1 * conj(val2);
547 return fast_atan2f(imag(conjprod), real(conjprod));
548}
piotr437f5462014-02-04 17:57:25 +0100549
piotrd0bf1492014-02-05 17:27:32 +0100550bool receiver_impl::reach_sch_burst(const int nitems)
551{
552 //it just consumes samples to get near to a SCH burst
553 int to_consume = 0;
554 bool result = false;
555 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
556
557 //consume samples until d_counter will be equal to sample_nr_near_sch_start
558 if (d_counter < sample_nr_near_sch_start)
559 {
560 if (d_counter + nitems >= sample_nr_near_sch_start)
561 {
562 to_consume = sample_nr_near_sch_start - d_counter;
563 }
564 else
565 {
566 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100567 }
568 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100569 }
570 else
571 {
piotr437f5462014-02-04 17:57:25 +0100572 to_consume = 0;
573 result = true;
piotr437f5462014-02-04 17:57:25 +0100574 }
575
piotrd0bf1492014-02-05 17:27:32 +0100576 d_counter += to_consume;
577 consume_each(to_consume);
578 return result;
579}
580
581int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
582{
583 vector_complex correlation_buffer;
584 vector_float power_buffer;
585 vector_float window_energy_buffer;
586
587 int strongest_window_nr;
588 int burst_start = 0;
589 int chan_imp_resp_center = 0;
590 float max_correlation = 0;
591 float energy = 0;
592
593 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100594 {
piotr437f5462014-02-04 17:57:25 +0100595 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
596 correlation_buffer.push_back(correlation);
597 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100598 }
piotrd0bf1492014-02-05 17:27:32 +0100599 //compute window energies
600 vector_float::iterator iter = power_buffer.begin();
601 bool loop_end = false;
602 while (iter != power_buffer.end())
603 {
piotr437f5462014-02-04 17:57:25 +0100604 vector_float::iterator iter_ii = iter;
605 energy = 0;
606
piotrd0bf1492014-02-05 17:27:32 +0100607 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
608 {
609 if (iter_ii == power_buffer.end())
610 {
611 loop_end = true;
612 break;
613 }
614 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100615 }
piotrd0bf1492014-02-05 17:27:32 +0100616 if (loop_end)
617 {
618 break;
piotr437f5462014-02-04 17:57:25 +0100619 }
620 iter++;
621 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100622 }
piotr437f5462014-02-04 17:57:25 +0100623
piotrd0bf1492014-02-05 17:27:32 +0100624 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100625 // d_channel_imp_resp.clear();
626
piotrd0bf1492014-02-05 17:27:32 +0100627 max_correlation = 0;
628 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
629 {
piotr437f5462014-02-04 17:57:25 +0100630 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100631 if (abs(correlation) > max_correlation)
632 {
633 chan_imp_resp_center = ii;
634 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100635 }
piotrd0bf1492014-02-05 17:27:32 +0100636 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100637 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100638 }
639
piotrd0bf1492014-02-05 17:27:32 +0100640 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
641 return burst_start;
642}
piotr437f5462014-02-04 17:57:25 +0100643
644
piotrd0bf1492014-02-05 17:27:32 +0100645void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
646{
647 float output[BURST_SIZE];
648 gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR];
649 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
650 gr_complex filtered_burst[BURST_SIZE];
651 int start_state = 3;
652 unsigned int stop_states[2] = {4, 12};
653
654 autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR);
655 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100656 {
piotr437f5462014-02-04 17:57:25 +0100657 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100658 }
659
piotrd0bf1492014-02-05 17:27:32 +0100660 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
661
662 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
663
664 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100665 {
piotrd0bf1492014-02-05 17:27:32 +0100666 output_binary[i] = (output[i] > 0);
667 }
668}
piotr437f5462014-02-04 17:57:25 +0100669
piotrd0bf1492014-02-05 17:27:32 +0100670void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
671{
672 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100673
piotrd0bf1492014-02-05 17:27:32 +0100674 int current_symbol;
675 int encoded_symbol;
676 int previous_symbol = 2 * input[0] - 1;
677 gmsk_output[0] = start_point;
678
679 for (int i = 1; i < nitems; i++)
680 {
piotr437f5462014-02-04 17:57:25 +0100681 //change bits representation to NRZ
682 current_symbol = 2 * input[i] - 1;
683 //differentially encode
684 encoded_symbol = current_symbol * previous_symbol;
685 //and do gmsk mapping
686 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
687 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100688 }
piotrd0bf1492014-02-05 17:27:32 +0100689}
piotr437f5462014-02-04 17:57:25 +0100690
piotrd0bf1492014-02-05 17:27:32 +0100691gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
692{
693 gr_complex result(0.0, 0.0);
694 int sample_number = 0;
695
696 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100697 {
piotr437f5462014-02-04 17:57:25 +0100698 sample_number = (ii * d_OSR) ;
699 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100700 }
701
piotrd0bf1492014-02-05 17:27:32 +0100702 result = result / gr_complex(length, 0);
703 return result;
704}
705
706//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100707inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
708{
709 int i, k;
710 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100711 {
piotr437f5462014-02-04 17:57:25 +0100712 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100713 for (i = k; i < nitems; i++)
714 {
715 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100716 }
piotr437f5462014-02-04 17:57:25 +0100717 }
piotrd0bf1492014-02-05 17:27:32 +0100718}
piotr437f5462014-02-04 17:57:25 +0100719
piotrd0bf1492014-02-05 17:27:32 +0100720inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
721{
722 int ii = 0, n, a;
723
724 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100725 {
piotr437f5462014-02-04 17:57:25 +0100726 a = n * d_OSR;
727 output[n] = 0;
728 ii = 0;
729
piotrd0bf1492014-02-05 17:27:32 +0100730 while (ii < filter_length)
731 {
piotrda8a0662014-04-24 10:29:38 +0200732 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100733 break;
piotrda8a0662014-04-24 10:29:38 +0200734 }
piotrd0bf1492014-02-05 17:27:32 +0100735 output[n] += input[a+ii] * filter[ii];
736 ii++;
piotr437f5462014-02-04 17:57:25 +0100737 }
piotr437f5462014-02-04 17:57:25 +0100738 }
piotrd0bf1492014-02-05 17:27:32 +0100739}
piotr437f5462014-02-04 17:57:25 +0100740
piotrd0bf1492014-02-05 17:27:32 +0100741//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100742int 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 +0100743{
744 vector_complex correlation_buffer;
745 vector_float power_buffer;
746 vector_float window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100747
piotrd0bf1492014-02-05 17:27:32 +0100748 int strongest_window_nr;
749 int burst_start = 0;
750 int chan_imp_resp_center = 0;
751 float max_correlation = 0;
752 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200753
piotrd0bf1492014-02-05 17:27:32 +0100754 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100755 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100756 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200757 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100758
ptrkrysik58213792014-10-30 09:05:15 +0100759 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100760 {
piotr437f5462014-02-04 17:57:25 +0100761 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100762 correlation_buffer.push_back(correlation);
763 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100764 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100765// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100766 //compute window energies
767 vector_float::iterator iter = power_buffer.begin();
768 bool loop_end = false;
769 while (iter != power_buffer.end())
770 {
piotr437f5462014-02-04 17:57:25 +0100771 vector_float::iterator iter_ii = iter;
772 energy = 0;
773
piotrd0bf1492014-02-05 17:27:32 +0100774 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++)
775 {
piotrd0bf1492014-02-05 17:27:32 +0100776 if (iter_ii == power_buffer.end())
777 {
778 loop_end = true;
779 break;
780 }
781 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100782 }
piotrd0bf1492014-02-05 17:27:32 +0100783 if (loop_end)
784 {
785 break;
piotr437f5462014-02-04 17:57:25 +0100786 }
787 iter++;
788
789 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100790 }
piotr437f5462014-02-04 17:57:25 +0100791
piotr5c820252014-04-17 09:43:02 +0200792 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
793 //strongest_window_nr = strongest_window_nr-d_OSR;
794 if(strongest_window_nr<0){
795 strongest_window_nr = 0;
796 }
piotr6d152d92014-02-21 00:02:44 +0100797
piotrd0bf1492014-02-05 17:27:32 +0100798 max_correlation = 0;
799 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
800 {
piotr437f5462014-02-04 17:57:25 +0100801 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100802 if (abs(correlation) > max_correlation)
803 {
804 chan_imp_resp_center = ii;
805 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100806 }
piotrd0bf1492014-02-05 17:27:32 +0100807 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100808 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100809 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100810
piotr7e3b0db2014-02-05 22:44:30 +0100811 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100812
ptrkrysik58213792014-10-30 09:05:15 +0100813 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200814 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 +0100815
ptrkrysik58213792014-10-30 09:05:15 +0100816 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100817 return burst_start;
818}
piotr437f5462014-02-04 17:57:25 +0100819
820
ptrkrysik617ba032014-11-21 10:11:05 +0100821void 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 +0100822{
piotr6d152d92014-02-21 00:02:44 +0100823 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100824
piotr6d152d92014-02-21 00:02:44 +0100825 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100826 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100827 tap_header->type = GSMTAP_TYPE_UM_BURST;
828 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100829 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
ptrkrysik617ba032014-11-21 10:11:05 +0100830 tap_header->sub_type = burst_type;
ptrkrysik617ba032014-11-21 10:11:05 +0100831 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
piotr6d152d92014-02-21 00:02:44 +0100832 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100833 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100834
835 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
836 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
837 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
838
839 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
840 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200841
ptrkrysike518bbf2014-11-06 14:50:59 +0100842 if(input_nr==0){
843 message_port_pub(pmt::mp("C0"), msg);
844 } else {
845 message_port_pub(pmt::mp("CX"), msg);
846 }
piotrd0bf1492014-02-05 17:27:32 +0100847}
piotr6d152d92014-02-21 00:02:44 +0100848
piotrd0bf1492014-02-05 17:27:32 +0100849void receiver_impl::configure_receiver()
850{
piotrce92f982014-04-17 23:37:18 +0200851 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100852 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100853
piotrce92f982014-04-17 23:37:18 +0200854 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
855 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
856 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100857
piotrd0bf1492014-02-05 17:27:32 +0100858 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
859 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
860 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
861 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
862 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
863 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
864 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
865 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
866 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
867 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
868 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
869 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
870 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
871 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100872}
piotr437f5462014-02-04 17:57:25 +0100873
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100874void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200875{
ptrkrysike518bbf2014-11-06 14:50:59 +0100876 d_cell_allocation = cell_allocation;
877}
878
879void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
880{
881 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200882}
883
884void receiver_impl::reset()
885{
piotrd6d66872014-08-06 15:20:33 +0200886 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200887}
piotr437f5462014-02-04 17:57:25 +0100888
piotrd0bf1492014-02-05 17:27:32 +0100889} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100890} /* namespace gr */
891