blob: cc5417882947744868b338c6cda2bc425a9e272e [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
ptrkrysik529895b2014-12-02 18:07:38 +01002/*
3 * @file
4 * @author Piotr Krysik <ptrkrysik@gmail.com>
5 * @section LICENSE
6 *
7 * 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.
ptrkrysik529895b2014-12-02 18:07:38 +010011 *
12 * 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.
ptrkrysik529895b2014-12-02 18:07:38 +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#ifndef INCLUDED_GSM_RECEIVER_IMPL_H
24#define INCLUDED_GSM_RECEIVER_IMPL_H
25
ptrkrysik3be74a72014-12-13 10:11:00 +010026#include <grgsm/receiver/receiver.h>
27#include <grgsm/gsmtap.h>
piotr437f5462014-02-04 17:57:25 +010028#include <gsm_constants.h>
29#include <receiver_config.h>
30
31namespace gr {
32 namespace gsm {
piotr437f5462014-02-04 17:57:25 +010033 class receiver_impl : public receiver
34 {
35 private:
ptrkrysik58213792014-10-30 09:05:15 +010036 unsigned int d_c0_burst_start;
37 float d_c0_signal_dbm;
piotr437f5462014-02-04 17:57:25 +010038 /**@name Configuration of the receiver */
39 //@{
40 const int d_OSR; ///< oversampling ratio
41 const int d_chan_imp_length; ///< channel impulse length
ptrkrysik58213792014-10-30 09:05:15 +010042 float d_signal_dbm;
ptrkrysike518bbf2014-11-06 14:50:59 +010043 std::vector<int> d_tseq_nums; ///< stores training sequence numbers for channels different than C0
ptrkrysik7a7b9b02014-11-19 11:27:34 +010044 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 +010045 //@}
46
47 gr_complex d_sch_training_seq[N_SYNC_BITS]; ///<encoded training sequence of a SCH burst
ptrkrysik58213792014-10-30 09:05:15 +010048 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 +010049
piotr437f5462014-02-04 17:57:25 +010050 /** Counts samples consumed by the receiver
51 *
52 * It is used in beetween find_fcch_burst and reach_sch_burst calls.
53 * My intention was to synchronize this counter with some internal sample
54 * counter of the USRP. Simple access to such USRP's counter isn't possible
55 * so this variable isn't used in the "synchronized" state of the receiver yet.
56 */
57 unsigned d_counter;
58
59 /**@name Variables used to store result of the find_fcch_burst fuction */
60 //@{
61 unsigned d_fcch_start_pos; ///< position of the first sample of the fcch burst
piotr4089c1a2014-08-06 14:10:56 +020062 float d_freq_offset_setting; ///< frequency offset set in frequency shifter located upstream
piotr437f5462014-02-04 17:57:25 +010063 //@}
64 std::list<double> d_freq_offset_vals;
65
66 /**@name Identifiers of the BTS extracted from the SCH burst */
67 //@{
68 int d_ncc; ///< network color code
69 int d_bcc; ///< base station color code
70 //@}
71
72 /**@name Internal state of the gsm receiver */
73 //@{
74 enum states {
piotrd6d66872014-08-06 15:20:33 +020075 fcch_search, sch_search, // synchronization search part
piotr437f5462014-02-04 17:57:25 +010076 synchronized // receiver is synchronized in this state
77 } d_state;
78 //@}
79
80 /**@name Variables which make internal state in the "synchronized" state */
81 //@{
82 burst_counter d_burst_nr; ///< frame number and timeslot number
83 channel_configuration d_channel_conf; ///< mapping of burst_counter to burst_type
84 //@}
ptrkrysike518bbf2014-11-06 14:50:59 +010085
piotr437f5462014-02-04 17:57:25 +010086 unsigned d_failed_sch; ///< number of subsequent erroneous SCH bursts
87
88 /** Function whis is used to search a FCCH burst and to compute frequency offset before
89 * "synchronized" state of the receiver
90 *
piotr437f5462014-02-04 17:57:25 +010091 * @param input vector with input signal
92 * @param nitems number of samples in the input vector
93 * @return
94 */
piotr4089c1a2014-08-06 14:10:56 +020095 bool find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset);
piotr437f5462014-02-04 17:57:25 +010096
97 /** Computes frequency offset from FCCH burst samples
98 *
piotr4089c1a2014-08-06 14:10:56 +020099 * @param[in] input vector with input samples
100 * @param[in] first_sample number of the first sample of the FCCH busrt
101 * @param[in] last_sample number of the last sample of the FCCH busrt
102 * @param[out] computed_freq_offset contains frequency offset estimate if FCCH burst was located
103 * @return true if frequency offset was faound
piotr437f5462014-02-04 17:57:25 +0100104 */
105 double compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample);
106
piotr437f5462014-02-04 17:57:25 +0100107 /** Computes angle between two complex numbers
108 *
109 * @param val1 first complex number
110 * @param val2 second complex number
111 * @return
112 */
113 inline float compute_phase_diff(gr_complex val1, gr_complex val2);
114
115 /** Function whis is used to get near to SCH burst
116 *
117 * @param nitems number of samples in the gsm_receiver's buffer
118 * @return true if SCH burst is near, false otherwise
119 */
120 bool reach_sch_burst(const int nitems);
121
122 /** Extracts channel impulse response from a SCH burst and computes first sample number of this burst
123 *
124 * @param input vector with input samples
125 * @param chan_imp_resp complex vector where channel impulse response will be stored
126 * @return number of first sample of the burst
127 */
128 int get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp);
129
130 /** MLSE detection of a burst bits
131 *
132 * Detects bits of burst using viterbi algorithm.
133 * @param input vector with input samples
134 * @param chan_imp_resp vector with the channel impulse response
135 * @param burst_start number of the first sample of the burst
136 * @param output_binary vector with output bits
137 */
138 void detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary);
139
140 /** Encodes differentially input bits and maps them into MSK states
141 *
142 * @param input vector with input bits
143 * @param nitems number of samples in the "input" vector
144 * @param gmsk_output bits mapped into MSK states
145 * @param start_point first state
146 */
147 void gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point);
148
149 /** Correlates MSK mapped sequence with input signal
150 *
151 * @param sequence MKS mapped sequence
152 * @param length length of the sequence
153 * @param input_signal vector with input samples
154 * @return correlation value
155 */
156 gr_complex correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input);
157
158 /** Computes autocorrelation of input vector for positive arguments
159 *
160 * @param input vector with input samples
161 * @param out output vector
162 * @param nitems length of the input vector
163 */
164 inline void autocorrelation(const gr_complex * input, gr_complex * out, int nitems);
165
166 /** Filters input signal through channel impulse response
167 *
168 * @param input vector with input samples
169 * @param nitems number of samples to pass through filter
170 * @param filter filter taps - channel impulse response
171 * @param filter_length nember of filter taps
172 * @param output vector with filtered samples
173 */
174 inline void mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output);
175
176 /** Extracts channel impulse response from a normal burst and computes first sample number of this burst
177 *
178 * @param input vector with input samples
179 * @param chan_imp_resp complex vector where channel impulse response will be stored
180 * @param search_range possible absolute offset of a channel impulse response start
181 * @param bcc base station color code - number of a training sequence
182 * @return first sample number of normal burst
183 */
piotr7e3b0db2014-02-05 22:44:30 +0100184 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 +0100185
186 /**
ptrkrysike518bbf2014-11-06 14:50:59 +0100187 * Sends burst through a C0 (for burst from C0 channel) or Cx (for other bursts) message port
piotr437f5462014-02-04 17:57:25 +0100188 *
ptrkrysike518bbf2014-11-06 14:50:59 +0100189 * @param burst_nr - frame number of the burst
190 * @param burst_binary - content of the burst
191 * @b_type - type of the burst
piotr437f5462014-02-04 17:57:25 +0100192 */
ptrkrysik617ba032014-11-21 10:11:05 +0100193 void send_burst(burst_counter burst_nr, const unsigned char * burst_binary, uint8_t burst_type, unsigned int input_nr);
piotr437f5462014-02-04 17:57:25 +0100194
195 /**
ptrkrysike518bbf2014-11-06 14:50:59 +0100196 * Configures burst types in different channels
piotr437f5462014-02-04 17:57:25 +0100197 */
198 void configure_receiver();
piotrf2b6a1b2014-08-04 11:28:59 +0200199
200
201
piotr437f5462014-02-04 17:57:25 +0100202 public:
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100203 receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums);
piotr437f5462014-02-04 17:57:25 +0100204 ~receiver_impl();
205
piotrc7c249a2014-05-02 17:24:08 +0200206 int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items);
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100207 virtual void set_cell_allocation(const std::vector<int> &cell_allocation);
ptrkrysike518bbf2014-11-06 14:50:59 +0100208 virtual void set_tseq_nums(const std::vector<int> & tseq_nums);
piotrf2b6a1b2014-08-04 11:28:59 +0200209 virtual void reset();
piotr437f5462014-02-04 17:57:25 +0100210 };
211 } // namespace gsm
212} // namespace gr
213
214#endif /* INCLUDED_GSM_RECEIVER_IMPL_H */
215