blob: a2458f15bf0708af3edef382a8a75a618607208f [file] [log] [blame]
Roman Khassraffb6bc502015-04-14 15:44:40 +02001/* -*- c++ -*- */
2/*
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
8 * 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.
11 *
12 * Gr-gsm is distributed in the hope that it will be useful,
13 * 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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with gr-gsm; see the file COPYING. If not, write to
19 * 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 "tch_f_chans_demapper_impl.h"
29#include <grgsm/endian.h>
30#include <grgsm/gsmtap.h>
31
32namespace gr {
33 namespace gsm {
34
35 tch_f_chans_demapper::sptr
36 tch_f_chans_demapper::make(unsigned int timeslot_nr)
37 {
38 return gnuradio::get_initial_sptr
39 (new tch_f_chans_demapper_impl(timeslot_nr));
40 }
41
42 /*
43 * The private constructor
44 *
45 */
46 tch_f_chans_demapper_impl::tch_f_chans_demapper_impl(unsigned int timeslot_nr)
47 : gr::block("tch_f_chans_demapper",
48 gr::io_signature::make(0, 0, 0),
49 gr::io_signature::make(0, 0, 0)),
50 d_timeslot(timeslot_nr)
51
52 {
53 for(int ii=0; ii<13; ii++)
54 {
55 int startpos = ((int)(ii / 4) * 4);
56 d_starts_fn_mod26[ii] = startpos;
57 d_starts_fn_mod26[ii + 13] = startpos + 13;
58 }
59
60 d_bursts_stolen = false;
61
62 message_port_register_in(pmt::mp("bursts"));
63 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_chans_demapper_impl::filter_tch_chans, this, _1));
64 message_port_register_out(pmt::mp("tch_bursts"));
65 message_port_register_out(pmt::mp("acch_bursts"));
66 }
67
68 /*
69 * Our virtual destructor.
70 */
71 tch_f_chans_demapper_impl::~tch_f_chans_demapper_impl()
72 {
73 }
74
75 void tch_f_chans_demapper_impl::filter_tch_chans(pmt::pmt_t msg)
76 {
77 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
78 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
79
80 uint32_t frame_nr = be32toh(header->frame_number);
81 uint32_t fn_mod26 = frame_nr % 26;
82 uint32_t fn26_start = d_starts_fn_mod26[fn_mod26];
83 uint32_t fn26_stop = fn26_start + 3;
84 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
85
86 if(header->timeslot == d_timeslot){
87 header->sub_type = GSMTAP_CHANNEL_TCH_F;
88
89 if (fn_mod26 == 12 || fn_mod26 == 25)
90 {
91 header->sub_type = GSMTAP_CHANNEL_ACCH|GSMTAP_CHANNEL_TCH_F;
92
93 // position of SACCH burst based on timeslot
94 // see specification gsm 05.02
95 uint32_t index;
96 bool is_sacch = false;
97
98 if (d_timeslot % 2 == 0 && fn_mod26 == 12)
99 {
100 index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
101 is_sacch = true;
102 }
103 else if (d_timeslot % 2 == 1 && fn_mod26 == 25)
104 {
105 index = (((frame_nr - 25) / 26) - ((d_timeslot - 1) / 2)) % 4;
106 is_sacch = true;
107 }
108
109 if (is_sacch)
110 {
111 d_bursts_sacch[index] = msg;
112 d_frame_numbers_sacch[index] = frame_nr;
113
114 if (index == 3)
115 {
116 //check for a situation where some bursts were lost
117 //in this situation frame numbers won't be consecutive
118 bool frames_are_consecutive = true;
119 for(int jj=1; jj<4; jj++)
120 {
121 if((d_frame_numbers_sacch[jj]-d_frame_numbers_sacch[jj-1]) != 26)
122 {
123 frames_are_consecutive = false;
124 }
125 }
126 if(frames_are_consecutive)
127 {
128 //send bursts to the output
129 for(int jj=0; jj<4; jj++)
130 {
131 message_port_pub(pmt::mp("acch_bursts"), d_bursts_sacch[jj]);
132 }
133 }
134 }
135 }
136 }
137 else
138 {
139 if(fn_mod26>=fn26_start && fn_mod26<=fn26_stop)
140 {
141 uint32_t ii = fn_mod26 - fn26_start;
142 d_frame_numbers[ii] = frame_nr;
143 d_bursts[ii] = msg;
144
145 // check if stealing bits are set
146 // see gsm 05.03
147 if (static_cast<int>(burst_bits[60]) == 1 && static_cast<int>(burst_bits[87]) == 1)
148 {
149 d_bursts_stolen = true;
150 }
151 }
152
153 if(fn_mod26==fn26_stop)
154 {
155 //check for a situation where some bursts were lost
156 //in this situation frame numbers won't be consecutive
157 bool frames_are_consecutive = true;
158
159 for(int jj=1; jj<4; jj++)
160 {
161 if((d_frame_numbers[jj]-d_frame_numbers[jj-1])!=1)
162 {
163 frames_are_consecutive = false;
164 }
165 }
166 if(frames_are_consecutive)
167 {
168 //send bursts to the output
169 for(int jj=0; jj<4; jj++)
170 {
171 if (d_bursts_stolen)
172 {
173 message_port_pub(pmt::mp("acch_bursts"), d_bursts[jj]);
174 }
175 else
176 {
177 message_port_pub(pmt::mp("tch_bursts"), d_bursts[jj]);
178 }
179 }
180 d_bursts_stolen = false;
181 }
182 }
183 }
184 }
185 }
186 } /* namespace gsm */
187} /* namespace gr */
188