aboutsummaryrefslogtreecommitdiffstats
path: root/lib/demapping/tch_f_chans_demapper_impl.cc
blob: 53f46fbe2a6ed2c0ce1310e4bfeab9948981d537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- c++ -*- */
/*
 * @file
 * @author Roman Khassraf <rkhassraf@gmail.com>
 * @section LICENSE
 *
 * Gr-gsm is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Gr-gsm is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gr-gsm; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "tch_f_chans_demapper_impl.h"
#include <grgsm/endian.h>
#include <grgsm/gsmtap.h>

#define BURST_SIZE 148

namespace gr {
  namespace gsm {

    tch_f_chans_demapper::sptr
    tch_f_chans_demapper::make(unsigned int timeslot_nr)
    {
      return gnuradio::get_initial_sptr
        (new tch_f_chans_demapper_impl(timeslot_nr));
    }

    /*
     * The private constructor
     *
     */
    tch_f_chans_demapper_impl::tch_f_chans_demapper_impl(unsigned int timeslot_nr)
      : gr::block("tch_f_chans_demapper",
              gr::io_signature::make(0, 0, 0),
              gr::io_signature::make(0, 0, 0)),
       d_timeslot(timeslot_nr)

    {
//        for (int ii=0; ii<3; ii++)
//        {
        //            d_bursts_stolen[ii] = false;
        //        }

        message_port_register_in(pmt::mp("bursts"));
        set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_chans_demapper_impl::filter_tch_chans, this, _1));
        message_port_register_out(pmt::mp("tch_bursts"));
        message_port_register_out(pmt::mp("acch_bursts"));
    }

    /*
     * Our virtual destructor.
     */
    tch_f_chans_demapper_impl::~tch_f_chans_demapper_impl()
    {
    }

    void tch_f_chans_demapper_impl::filter_tch_chans(pmt::pmt_t msg)
    {
        pmt::pmt_t header_plus_burst = pmt::cdr(msg);
        gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);

        uint32_t frame_nr = be32toh(header->frame_number);
        uint32_t fn_mod26 = frame_nr % 26;
        uint32_t fn_mod13 = frame_nr % 13;
        int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);

        if(header->timeslot == d_timeslot){
            int8_t new_msg[sizeof(gsmtap_hdr)+BURST_SIZE];
            gsmtap_hdr * new_hdr = (gsmtap_hdr*)new_msg;
            memcpy(new_msg, header, sizeof(gsmtap_hdr)+BURST_SIZE);

            new_hdr->sub_type = GSMTAP_CHANNEL_TCH_F;
            if (fn_mod13 == 12)
                new_hdr->sub_type = GSMTAP_CHANNEL_ACCH|GSMTAP_CHANNEL_TCH_F;

            pmt::pmt_t msg_binary_blob = pmt::make_blob(new_msg,sizeof(gsmtap_hdr)+BURST_SIZE);
            pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);

            //distinguishing uplink and downlink bursts
            bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false;

            if(uplink_burst)
            {
                sacch_tch_demapper(fn_mod13, fn_mod26, frame_nr,d_bursts_sacch_ul,
                                   d_frame_numbers_sacch_ul, d_bursts_ul, d_frame_numbers_ul, msg_out);
            }
            else
            {
                sacch_tch_demapper(fn_mod13, fn_mod26, frame_nr,d_bursts_sacch_dl,
                                   d_frame_numbers_sacch_dl, d_bursts_dl, d_frame_numbers_dl, msg_out);
            }

        }
    }

    void tch_f_chans_demapper_impl::sacch_tch_demapper(uint32_t fn_mod13, u_int32_t fn_mod26, uint32_t frame_nr,
                                                       pmt::pmt_t *d_bursts_sacch,
                                                       uint32_t *d_frame_numbers_sacch, pmt::pmt_t d_bursts[3][8],
                                                       uint32_t d_frame_numbers[3][8], pmt::pmt_t msg_out)
    {
        bool frames_are_consecutive = true;
        if (fn_mod13 == 12)
        {
            // position of SACCH burst based on timeslot
            // see specification gsm 05.02
            uint32_t index;
            bool is_sacch = false;

            if (d_timeslot % 2 == 0 && fn_mod26 == 12)
            {
                index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
                is_sacch = true;
            }
            else if (d_timeslot % 2 == 1 && fn_mod26 == 25)
            {
                index = (((frame_nr - 25) / 26) - ((d_timeslot - 1) / 2)) % 4;
                is_sacch = true;
            }

            if (is_sacch)
            {
                d_bursts_sacch[index] = msg_out;
                d_frame_numbers_sacch[index] = frame_nr;

                if (index == 3)
                {
                    //check for a situation where some bursts were lost
                    //in this situation frame numbers won't be consecutive
                    frames_are_consecutive = true;
                    for(int jj=1; jj<4; jj++)
                    {
                        if((d_frame_numbers_sacch[jj]-d_frame_numbers_sacch[jj-1]) != 26)
                        {
                            frames_are_consecutive = false;
                        }
                    }
                    if(frames_are_consecutive)
                    {
                        //send bursts to the output
                        for(int jj=0; jj<4; jj++)
                        {
                            message_port_pub(pmt::mp("acch_bursts"), d_bursts_sacch[jj]);
                        }
                    }
                }
            }
        }
        else
        {
            if (fn_mod13 <= 3)
            {
                // add to b1 and b3
                d_bursts[0][fn_mod13] = msg_out;
                d_bursts[2][fn_mod13 + 4] = msg_out;

                // set framenumber for later checking of continuity
                d_frame_numbers[0][fn_mod13] = frame_nr;
                d_frame_numbers[2][fn_mod13 + 4] = frame_nr;
            }
            else if (fn_mod13 >= 4 && fn_mod13 <= 7)
            {
                // add to b1 and b2
                d_bursts[0][fn_mod13] = msg_out;
                d_bursts[1][fn_mod13 - 4] = msg_out;

                // set framenumber for later checking of continuity
                d_frame_numbers[0][fn_mod13] = frame_nr;
                d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
            }
            else if (fn_mod13 >= 8 && fn_mod13 <= 11)
            {
                // add to b2 and b3
                d_bursts[1][fn_mod13 - 4] = msg_out;
                d_bursts[2][fn_mod13 - 8] = msg_out;

                // set framenumber for later checking of continuity
                d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
                d_frame_numbers[2][fn_mod13 - 8] = frame_nr;
            }

            // send burst 1 or burst 2 to output
            if (fn_mod13 == 3 || fn_mod13 == 7 || fn_mod13 == 11)
            {
                int tch_burst_nr = 0;

                if (fn_mod13 == 11)
                {
                    tch_burst_nr = 1;
                }
                else if (fn_mod13 == 3)
                {
                    tch_burst_nr = 2;
                }

                //check for a situation where some bursts were lost
                //in this situation frame numbers won't be consecutive
                frames_are_consecutive = true;

                for(int jj=1; jj<8; jj++)
                {
                    if (((d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj-1]) != 1) && frames_are_consecutive)
                    {
                        frames_are_consecutive = false;
                        // burst 3 has 1 sacch burst in between
                        if (tch_burst_nr == 2 && jj == 4
                            && d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj - 1] == 2)
                        {
                            frames_are_consecutive = true;
                        }
                    }
                }

                if(frames_are_consecutive)
                {
                    //send bursts to the output
                    for(int jj=0; jj<8; jj++)
                    {
                        message_port_pub(pmt::mp("tch_bursts"), d_bursts[tch_burst_nr][jj]);
                    }
                    // useless
//                        d_bursts_stolen[tch_burst_nr] = false;
                }
            }
        }
    }
  } /* namespace gsm */
} /* namespace gr */