blob: 030a3f3211cad004e8312339d2385bd0d22f438a [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
Roman Khassraf650ce782015-05-20 12:48:04 +020044 *
Roman Khassraffb6bc502015-04-14 15:44:40 +020045 */
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)
Roman Khassraf650ce782015-05-20 12:48:04 +020051
52 {
53 for (int ii=0; ii<3; ii++)
Roman Khassraffb6bc502015-04-14 15:44:40 +020054 {
Roman Khassraf155a7442015-05-08 19:52:01 +020055 d_bursts_stolen[ii] = false;
Roman Khassraffb6bc502015-04-14 15:44:40 +020056 }
Roman Khassraf650ce782015-05-20 12:48:04 +020057
Roman Khassraffb6bc502015-04-14 15:44:40 +020058 message_port_register_in(pmt::mp("bursts"));
59 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_chans_demapper_impl::filter_tch_chans, this, _1));
60 message_port_register_out(pmt::mp("tch_bursts"));
61 message_port_register_out(pmt::mp("acch_bursts"));
62 }
63
64 /*
65 * Our virtual destructor.
66 */
67 tch_f_chans_demapper_impl::~tch_f_chans_demapper_impl()
68 {
69 }
Roman Khassraf650ce782015-05-20 12:48:04 +020070
Roman Khassraffb6bc502015-04-14 15:44:40 +020071 void tch_f_chans_demapper_impl::filter_tch_chans(pmt::pmt_t msg)
72 {
73 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
74 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
75
76 uint32_t frame_nr = be32toh(header->frame_number);
77 uint32_t fn_mod26 = frame_nr % 26;
Roman Khassraf155a7442015-05-08 19:52:01 +020078 uint32_t fn_mod13 = frame_nr % 13;
79 bool frames_are_consecutive = true;
Roman Khassraffb6bc502015-04-14 15:44:40 +020080 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
81
82 if(header->timeslot == d_timeslot){
83 header->sub_type = GSMTAP_CHANNEL_TCH_F;
Roman Khassraf650ce782015-05-20 12:48:04 +020084
Roman Khassraf155a7442015-05-08 19:52:01 +020085 if (fn_mod13 == 12)
Roman Khassraffb6bc502015-04-14 15:44:40 +020086 {
87 header->sub_type = GSMTAP_CHANNEL_ACCH|GSMTAP_CHANNEL_TCH_F;
Roman Khassraf650ce782015-05-20 12:48:04 +020088
Roman Khassraffb6bc502015-04-14 15:44:40 +020089 // position of SACCH burst based on timeslot
90 // see specification gsm 05.02
91 uint32_t index;
92 bool is_sacch = false;
Roman Khassraf650ce782015-05-20 12:48:04 +020093
Roman Khassraffb6bc502015-04-14 15:44:40 +020094 if (d_timeslot % 2 == 0 && fn_mod26 == 12)
95 {
96 index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
97 is_sacch = true;
98 }
Roman Khassraf650ce782015-05-20 12:48:04 +020099 else if (d_timeslot % 2 == 1 && fn_mod26 == 25)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200100 {
101 index = (((frame_nr - 25) / 26) - ((d_timeslot - 1) / 2)) % 4;
102 is_sacch = true;
103 }
Roman Khassraf650ce782015-05-20 12:48:04 +0200104
Roman Khassraffb6bc502015-04-14 15:44:40 +0200105 if (is_sacch)
106 {
107 d_bursts_sacch[index] = msg;
108 d_frame_numbers_sacch[index] = frame_nr;
Roman Khassraf650ce782015-05-20 12:48:04 +0200109
110 if (index == 3)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200111 {
112 //check for a situation where some bursts were lost
113 //in this situation frame numbers won't be consecutive
Roman Khassraf155a7442015-05-08 19:52:01 +0200114 frames_are_consecutive = true;
Roman Khassraffb6bc502015-04-14 15:44:40 +0200115 for(int jj=1; jj<4; jj++)
116 {
117 if((d_frame_numbers_sacch[jj]-d_frame_numbers_sacch[jj-1]) != 26)
118 {
119 frames_are_consecutive = false;
120 }
121 }
122 if(frames_are_consecutive)
123 {
124 //send bursts to the output
125 for(int jj=0; jj<4; jj++)
126 {
127 message_port_pub(pmt::mp("acch_bursts"), d_bursts_sacch[jj]);
128 }
129 }
130 }
131 }
132 }
133 else
134 {
Roman Khassraf650ce782015-05-20 12:48:04 +0200135 if (fn_mod13 <= 3)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200136 {
Roman Khassraf155a7442015-05-08 19:52:01 +0200137 // add to b1 and b3
138 d_bursts[0][fn_mod13] = msg;
139 d_bursts[2][fn_mod13 + 4] = msg;
Roman Khassraf650ce782015-05-20 12:48:04 +0200140
Roman Khassraf155a7442015-05-08 19:52:01 +0200141 // set framenumber
142 d_frame_numbers[0][fn_mod13] = frame_nr;
143 d_frame_numbers[2][fn_mod13 + 4] = frame_nr;
Roman Khassraffb6bc502015-04-14 15:44:40 +0200144 }
Roman Khassraf155a7442015-05-08 19:52:01 +0200145 else if (fn_mod13 >= 4 && fn_mod13 <= 7)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200146 {
Roman Khassraf155a7442015-05-08 19:52:01 +0200147 // add to b1 and b2
148 d_bursts[0][fn_mod13] = msg;
149 d_bursts[1][fn_mod13 - 4] = msg;
Roman Khassraf650ce782015-05-20 12:48:04 +0200150
Roman Khassraf155a7442015-05-08 19:52:01 +0200151 // set framenumber
152 d_frame_numbers[0][fn_mod13] = frame_nr;
153 d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
Roman Khassraf155a7442015-05-08 19:52:01 +0200154 }
155 else if (fn_mod13 >= 8 && fn_mod13 <= 11)
156 {
157 // add to b2 and b3
158 d_bursts[1][fn_mod13 - 4] = msg;
159 d_bursts[2][fn_mod13 - 8] = msg;
Roman Khassraf650ce782015-05-20 12:48:04 +0200160
Roman Khassraf155a7442015-05-08 19:52:01 +0200161 // set framenumber
162 d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
163 d_frame_numbers[2][fn_mod13 - 8] = frame_nr;
Roman Khassraf155a7442015-05-08 19:52:01 +0200164 }
165
166 // send burst 1 or burst 2 to output
167 if (fn_mod13 == 3 || fn_mod13 == 7 || fn_mod13 == 11)
168 {
169 int tch_burst_nr = 0;
Roman Khassraf650ce782015-05-20 12:48:04 +0200170
171 if (fn_mod13 == 11)
Roman Khassraf155a7442015-05-08 19:52:01 +0200172 {
173 tch_burst_nr = 1;
174 }
175 else if (fn_mod13 == 3)
176 {
177 tch_burst_nr = 2;
178 }
179
Roman Khassraffb6bc502015-04-14 15:44:40 +0200180 //check for a situation where some bursts were lost
181 //in this situation frame numbers won't be consecutive
Roman Khassraf155a7442015-05-08 19:52:01 +0200182 frames_are_consecutive = true;
Roman Khassraf650ce782015-05-20 12:48:04 +0200183
Roman Khassraf155a7442015-05-08 19:52:01 +0200184 for(int jj=1; jj<8; jj++)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200185 {
Roman Khassraf155a7442015-05-08 19:52:01 +0200186 if((d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj-1]) != 1)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200187 {
188 frames_are_consecutive = false;
Roman Khassraf155a7442015-05-08 19:52:01 +0200189 // burst 3 has 1 sacch burst in between
Roman Khassraf650ce782015-05-20 12:48:04 +0200190 if (tch_burst_nr == 2 && jj == 4
191 && d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj - 1] == 2)
Roman Khassraf155a7442015-05-08 19:52:01 +0200192 {
193 frames_are_consecutive = true;
194 }
Roman Khassraffb6bc502015-04-14 15:44:40 +0200195 }
196 }
Roman Khassraf650ce782015-05-20 12:48:04 +0200197
Roman Khassraffb6bc502015-04-14 15:44:40 +0200198 if(frames_are_consecutive)
199 {
200 //send bursts to the output
Roman Khassraf155a7442015-05-08 19:52:01 +0200201 for(int jj=0; jj<8; jj++)
Roman Khassraffb6bc502015-04-14 15:44:40 +0200202 {
Roman Khassraf650ce782015-05-20 12:48:04 +0200203 message_port_pub(pmt::mp("tch_bursts"), d_bursts[tch_burst_nr][jj]);
Roman Khassraffb6bc502015-04-14 15:44:40 +0200204 }
Roman Khassraf155a7442015-05-08 19:52:01 +0200205 d_bursts_stolen[tch_burst_nr] = false;
206 }
Roman Khassraffb6bc502015-04-14 15:44:40 +0200207 }
208 }
209 }
210 }
211 } /* namespace gsm */
212} /* namespace gr */
213