blob: 2b7e9cf874762a321fac8ac675cf57d2d4f8f390 [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
2/*
3 * Copyright 2014 <+YOU OR YOUR COMPANY+>.
4 *
5 * 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.
9 *
10 * 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.
14 *
15 * 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#ifndef INCLUDED_GSM_RECEIVER_IMPL_H
22#define INCLUDED_GSM_RECEIVER_IMPL_H
23
ptrkrysik4c825772014-08-16 11:34:54 +020024#include <gsm/receiver/receiver.h>
piotrfaacc722014-07-20 23:48:32 +020025#include <gsm/gsmtap.h>
piotr437f5462014-02-04 17:57:25 +010026#include <gsm_constants.h>
27#include <receiver_config.h>
28
29namespace gr {
30 namespace gsm {
31
32 typedef std::vector<gr_complex> vector_complex;
33
34 class receiver_impl : public receiver
35 {
36 private:
ptrkrysik58213792014-10-30 09:05:15 +010037 unsigned int d_c0_burst_start;
38 float d_c0_signal_dbm;
piotr437f5462014-02-04 17:57:25 +010039 /**@name Configuration of the receiver */
40 //@{
41 const int d_OSR; ///< oversampling ratio
42 const int d_chan_imp_length; ///< channel impulse length
ptrkrysik58213792014-10-30 09:05:15 +010043 float d_signal_dbm;
ptrkrysike518bbf2014-11-06 14:50:59 +010044 std::vector<int> d_tseq_nums; ///< stores training sequence numbers for channels different than C0
ptrkrysik7a7b9b02014-11-19 11:27:34 +010045 std::vector<int> d_cell_allocation; ///< stores cell allocation - absolute rf channel numbers (ARFCNs) assigned to the given cell. The variable should at least contain C0 channel number.
piotr437f5462014-02-04 17:57:25 +010046 //@}
47
48 gr_complex d_sch_training_seq[N_SYNC_BITS]; ///<encoded training sequence of a SCH burst
ptrkrysik58213792014-10-30 09:05:15 +010049 gr_complex d_norm_training_seq[TRAIN_SEQ_NUM][N_TRAIN_BITS]; ///<encoded training sequences of a normal and dummy burst
piotr437f5462014-02-04 17:57:25 +010050
piotr437f5462014-02-04 17:57:25 +010051 /** Counts samples consumed by the receiver
52 *
53 * It is used in beetween find_fcch_burst and reach_sch_burst calls.
54 * My intention was to synchronize this counter with some internal sample
55 * counter of the USRP. Simple access to such USRP's counter isn't possible
56 * so this variable isn't used in the "synchronized" state of the receiver yet.
57 */
58 unsigned d_counter;
59
60 /**@name Variables used to store result of the find_fcch_burst fuction */
61 //@{
62 unsigned d_fcch_start_pos; ///< position of the first sample of the fcch burst
piotr4089c1a2014-08-06 14:10:56 +020063 float d_freq_offset_setting; ///< frequency offset set in frequency shifter located upstream
piotr437f5462014-02-04 17:57:25 +010064 //@}
65 std::list<double> d_freq_offset_vals;
66
67 /**@name Identifiers of the BTS extracted from the SCH burst */
68 //@{
69 int d_ncc; ///< network color code
70 int d_bcc; ///< base station color code
71 //@}
72
73 /**@name Internal state of the gsm receiver */
74 //@{
75 enum states {
piotrd6d66872014-08-06 15:20:33 +020076 fcch_search, sch_search, // synchronization search part
piotr437f5462014-02-04 17:57:25 +010077 synchronized // receiver is synchronized in this state
78 } d_state;
79 //@}
80
81 /**@name Variables which make internal state in the "synchronized" state */
82 //@{
83 burst_counter d_burst_nr; ///< frame number and timeslot number
84 channel_configuration d_channel_conf; ///< mapping of burst_counter to burst_type
85 //@}
ptrkrysike518bbf2014-11-06 14:50:59 +010086
piotr437f5462014-02-04 17:57:25 +010087 unsigned d_failed_sch; ///< number of subsequent erroneous SCH bursts
88
89 /** Function whis is used to search a FCCH burst and to compute frequency offset before
90 * "synchronized" state of the receiver
91 *
piotr437f5462014-02-04 17:57:25 +010092 * @param input vector with input signal
93 * @param nitems number of samples in the input vector
94 * @return
95 */
piotr4089c1a2014-08-06 14:10:56 +020096 bool find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset);
piotr437f5462014-02-04 17:57:25 +010097
98 /** Computes frequency offset from FCCH burst samples
99 *
piotr4089c1a2014-08-06 14:10:56 +0200100 * @param[in] input vector with input samples
101 * @param[in] first_sample number of the first sample of the FCCH busrt
102 * @param[in] last_sample number of the last sample of the FCCH busrt
103 * @param[out] computed_freq_offset contains frequency offset estimate if FCCH burst was located
104 * @return true if frequency offset was faound
piotr437f5462014-02-04 17:57:25 +0100105 */
106 double compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample);
107
piotr437f5462014-02-04 17:57:25 +0100108 /** Computes angle between two complex numbers
109 *
110 * @param val1 first complex number
111 * @param val2 second complex number
112 * @return
113 */
114 inline float compute_phase_diff(gr_complex val1, gr_complex val2);
115
116 /** Function whis is used to get near to SCH burst
117 *
118 * @param nitems number of samples in the gsm_receiver's buffer
119 * @return true if SCH burst is near, false otherwise
120 */
121 bool reach_sch_burst(const int nitems);
122
123 /** Extracts channel impulse response from a SCH burst and computes first sample number of this burst
124 *
125 * @param input vector with input samples
126 * @param chan_imp_resp complex vector where channel impulse response will be stored
127 * @return number of first sample of the burst
128 */
129 int get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp);
130
131 /** MLSE detection of a burst bits
132 *
133 * Detects bits of burst using viterbi algorithm.
134 * @param input vector with input samples
135 * @param chan_imp_resp vector with the channel impulse response
136 * @param burst_start number of the first sample of the burst
137 * @param output_binary vector with output bits
138 */
139 void detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary);
140
141 /** Encodes differentially input bits and maps them into MSK states
142 *
143 * @param input vector with input bits
144 * @param nitems number of samples in the "input" vector
145 * @param gmsk_output bits mapped into MSK states
146 * @param start_point first state
147 */
148 void gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point);
149
150 /** Correlates MSK mapped sequence with input signal
151 *
152 * @param sequence MKS mapped sequence
153 * @param length length of the sequence
154 * @param input_signal vector with input samples
155 * @return correlation value
156 */
157 gr_complex correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input);
158
159 /** Computes autocorrelation of input vector for positive arguments
160 *
161 * @param input vector with input samples
162 * @param out output vector
163 * @param nitems length of the input vector
164 */
165 inline void autocorrelation(const gr_complex * input, gr_complex * out, int nitems);
166
167 /** Filters input signal through channel impulse response
168 *
169 * @param input vector with input samples
170 * @param nitems number of samples to pass through filter
171 * @param filter filter taps - channel impulse response
172 * @param filter_length nember of filter taps
173 * @param output vector with filtered samples
174 */
175 inline void mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output);
176
177 /** Extracts channel impulse response from a normal burst and computes first sample number of this burst
178 *
179 * @param input vector with input samples
180 * @param chan_imp_resp complex vector where channel impulse response will be stored
181 * @param search_range possible absolute offset of a channel impulse response start
182 * @param bcc base station color code - number of a training sequence
183 * @return first sample number of normal burst
184 */
piotr7e3b0db2014-02-05 22:44:30 +0100185 int get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, float *corr_max, int bcc);
piotr437f5462014-02-04 17:57:25 +0100186
187 /**
ptrkrysike518bbf2014-11-06 14:50:59 +0100188 * Sends burst through a C0 (for burst from C0 channel) or Cx (for other bursts) message port
piotr437f5462014-02-04 17:57:25 +0100189 *
ptrkrysike518bbf2014-11-06 14:50:59 +0100190 * @param burst_nr - frame number of the burst
191 * @param burst_binary - content of the burst
192 * @b_type - type of the burst
piotr437f5462014-02-04 17:57:25 +0100193 */
ptrkrysike518bbf2014-11-06 14:50:59 +0100194 void send_burst(burst_counter burst_nr, const unsigned char * burst_binary, burst_type b_type, unsigned int input_nr);
piotr437f5462014-02-04 17:57:25 +0100195
196 /**
ptrkrysike518bbf2014-11-06 14:50:59 +0100197 * Configures burst types in different channels
piotr437f5462014-02-04 17:57:25 +0100198 */
199 void configure_receiver();
piotrf2b6a1b2014-08-04 11:28:59 +0200200
201
202
piotr437f5462014-02-04 17:57:25 +0100203 public:
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100204 receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums);
piotr437f5462014-02-04 17:57:25 +0100205 ~receiver_impl();
206
piotrc7c249a2014-05-02 17:24:08 +0200207 int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items);
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100208 virtual void set_cell_allocation(const std::vector<int> &cell_allocation);
ptrkrysike518bbf2014-11-06 14:50:59 +0100209 virtual void set_tseq_nums(const std::vector<int> & tseq_nums);
piotrf2b6a1b2014-08-04 11:28:59 +0200210 virtual void reset();
piotr437f5462014-02-04 17:57:25 +0100211 };
212 } // namespace gsm
213} // namespace gr
214
215#endif /* INCLUDED_GSM_RECEIVER_IMPL_H */
216