blob: 2c3dc791efce762589ea8bed617288114e83067c [file] [log] [blame]
Vasil Velichkov1828a312018-05-07 15:55:39 +03001/* -*- c++ -*- */
2/*
3 * @file
4 * @author (C) 2018 by Andrew Artyushok <loony.developer@gmail.com>
5 * @author (C) 2018 by Vasil Velichkov <vvvelichkov@gmail.com>
6 * @section LICENSE
7 *
8 * Gr-gsm is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3, or (at your option)
11 * any later version.
12 *
13 * Gr-gsm is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with gr-gsm; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <gnuradio/io_signature.h>
29#include "tch_h_chans_demapper_impl.h"
30#include <grgsm/endian.h>
31#include <grgsm/gsmtap.h>
32
33#define BURST_SIZE 148
34
35namespace gr {
36namespace gsm {
37
38tch_h_chans_demapper::sptr
39tch_h_chans_demapper::make(unsigned int timeslot_nr, unsigned int tch_h_channel)
40{
41 return gnuradio::get_initial_sptr
42 (new tch_h_chans_demapper_impl(timeslot_nr, tch_h_channel));
43}
44
45/*
46 * The private constructor
47 *
48 */
49tch_h_chans_demapper_impl::tch_h_chans_demapper_impl(unsigned int timeslot_nr, unsigned int tch_h_channel)
50 : gr::block("tch_h_chans_demapper",
51 gr::io_signature::make(0, 0, 0),
52 gr::io_signature::make(0, 0, 0)),
53 d_timeslot(timeslot_nr),
54 d_tch_h_channel(tch_h_channel)
55
56{
57 //std::cout << "d_tch_type is " << d_tch_type << ", tch_h_channel is " << tch_h_channel << std::endl;
58
59 message_port_register_in(pmt::mp("bursts"));
60 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_h_chans_demapper_impl::filter_tch_chans, this, _1));
61 message_port_register_out(pmt::mp("tch_bursts"));
62 message_port_register_out(pmt::mp("acch_bursts"));
63}
64
65/*
66 * Our virtual destructor.
67 */
68tch_h_chans_demapper_impl::~tch_h_chans_demapper_impl()
69{
70}
71
72void tch_h_chans_demapper_impl::filter_tch_chans(pmt::pmt_t msg)
73{
74 pmt::pmt_t header_plus_burst = pmt::cdr(msg);
75 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
76 if(header->timeslot != d_timeslot) {
77 return;
78 }
79
80 uint32_t frame_nr = be32toh(header->frame_number);
81 uint32_t fn_mod26 = frame_nr % 26;
82 uint32_t fn_mod13 = frame_nr % 13;
83 int8_t* burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr);
84
85 int8_t new_msg[sizeof(gsmtap_hdr)+BURST_SIZE];
86 gsmtap_hdr * new_hdr = (gsmtap_hdr*)new_msg;
87 memcpy(new_msg, header, sizeof(gsmtap_hdr)+BURST_SIZE);
88
89 new_hdr->sub_type = (fn_mod13 == 12 ? GSMTAP_CHANNEL_ACCH : 0) | GSMTAP_CHANNEL_TCH_H;
90
91 pmt::pmt_t msg_binary_blob = pmt::make_blob(new_msg,sizeof(gsmtap_hdr)+BURST_SIZE);
92 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
93
94 //distinguishing uplink and downlink bursts
95 bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false;
96
97 if(uplink_burst)
98 {
99 sacch_tch_demapper(fn_mod13, fn_mod26, frame_nr, d_bursts_sacch_ul,
100 d_frame_numbers_sacch_ul, d_bursts_ul, d_frame_numbers_ul, msg_out);
101 }
102 else
103 {
104 sacch_tch_demapper(fn_mod13, fn_mod26, frame_nr, d_bursts_sacch_dl,
105 d_frame_numbers_sacch_dl, d_bursts_dl, d_frame_numbers_dl, msg_out);
106 }
107}
108
109void tch_h_chans_demapper_impl::sacch_tch_demapper(uint32_t fn_mod13, u_int32_t fn_mod26, uint32_t frame_nr,
110 pmt::pmt_t *d_bursts_sacch,
111 uint32_t *d_frame_numbers_sacch, pmt::pmt_t d_bursts[3][8],
112 uint32_t d_frame_numbers[3][8], pmt::pmt_t msg_out)
113{
114 bool frames_are_consecutive = true;
115 if (fn_mod13 == 12)
116 {
117 // position of SACCH burst based on timeslot
118 // see specification gsm 05.02
119 uint32_t index;
120 bool is_sacch = false;
121
122 if (d_tch_h_channel == 0 && fn_mod26 == 12)
123 {
124 index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
125 is_sacch = true;
126 }
127 else if (d_tch_h_channel == 1 && fn_mod26 == 25)
128 {
129 index = (((frame_nr - 25) / 26) - (d_timeslot / 2)) % 4;
130 is_sacch = true;
131 }
132
133 if (is_sacch)
134 {
135 d_bursts_sacch[index] = msg_out;
136 d_frame_numbers_sacch[index] = frame_nr;
137
138 if (index == 3)
139 {
140 //check for a situation where some bursts were lost
141 //in this situation frame numbers won't be consecutive
142 frames_are_consecutive = true;
143 for(int jj=1; jj<4; jj++)
144 {
145 if((d_frame_numbers_sacch[jj]-d_frame_numbers_sacch[jj-1]) != 26)
146 {
147 frames_are_consecutive = false;
148 }
149 }
150 if(frames_are_consecutive)
151 {
152 //send bursts to the output
153 for(int jj=0; jj<4; jj++)
154 {
155 message_port_pub(pmt::mp("acch_bursts"), d_bursts_sacch[jj]);
156 }
157 }
158 }
159 }
160 }
161 else
162 {
163 bool our_sub=false;
164 // So, here I change the fn_mod13, with this changes I can
165 // process both subslot (d_tch_h_channel) with the same code.
166 //
167 // Theory about this on github
168 //
169 // Example:
170 //
171 // For subslot=0 our burst is 0,2,4,6 etc
172 // For subslot=1 it 1,3,5 etc
173 // But if we change it with this code,
174 // both will be 0,1,2,3,4 etc
175 // And we can proced two subslot like one.
176 if(fn_mod13%2==d_tch_h_channel) {
177 if(d_tch_h_channel==0) {
178 fn_mod13=fn_mod13/2;
179 }else{
180 fn_mod13-=1;
181 fn_mod13=fn_mod13/2;
182 }
183 // We work only with our subslot
184 our_sub=true;
185 }
186 if(our_sub) {
187
188 if (fn_mod13 <= 1)
189 {
190 // add to b1 and b3
191 d_bursts[0][fn_mod13] = msg_out;
192 d_bursts[2][fn_mod13+2] = msg_out;
193
194
195
196 d_frame_numbers[0][fn_mod13] = frame_nr;
197 d_frame_numbers[2][fn_mod13+2] = frame_nr;
198 }
199 else if (fn_mod13 >=2 && fn_mod13 <=3)
200 {
201 // add to b1 and b2
202 d_bursts[0][fn_mod13] = msg_out;
203 d_bursts[1][fn_mod13-2] = msg_out;
204
205
206 d_frame_numbers[0][fn_mod13] = frame_nr;
207 d_frame_numbers[1][fn_mod13-2] = frame_nr;
208 //d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
209 }
210 else if (fn_mod13 >=4 && fn_mod13 <=5)
211 {
212 // add to b1 and b2
213 d_bursts[1][fn_mod13-2] = msg_out;
214 d_bursts[2][fn_mod13-4] = msg_out;
215
216
217
218 d_frame_numbers[1][fn_mod13-2] = frame_nr;
219 d_frame_numbers[2][fn_mod13-4] = frame_nr;
220 //d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
221 }
222 // send burst 1 or burst 2 to output
223 //if ( fn_mod26 == 2 || fn_mod26 == 5 || fn_mod26 == 9)
224 if(fn_mod13 == 1 || fn_mod13 == 3|| fn_mod13 == 5)
225 {
226 int tch_burst_nr = 0;
227
228 if(fn_mod13==5) {
229 tch_burst_nr = 1;
230 }
231 if(fn_mod13==1) {
232 tch_burst_nr = 2;
233 }
234
235 //check for a situation where some bursts were lost
236 //in this situation frame numbers won't be consecutive
237 frames_are_consecutive = true;
238 // std::cout<<"br="<<tch_burst_nr<<" Array: ";
239 // std::cout<<d_frame_numbers[tch_burst_nr][0]<<" ";
240 bool one_tree=true;
241 for(int jj=1; jj<4; jj++)
242 {
243
244 if (((d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj-1]) != 2) && frames_are_consecutive)
245 {
246 frames_are_consecutive = false;
247
248 if (tch_burst_nr == 2 && jj == 2
249 && d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj - 1] == 3)
250 {
251 // std::cout<<" YEAH ";
252 frames_are_consecutive = true;
253 }
254
255 }
256 //}
257 }
258 // std::cout<<std::endl;
259
260 if(frames_are_consecutive)
261 {
262
263 for(int jj=0; jj<4; jj++)
264 {
265
266 message_port_pub(pmt::mp("tch_bursts"), d_bursts[tch_burst_nr][jj]);
267 }
268
269 // useless
270 // d_bursts_stolen[tch_burst_nr] = false;
271 }
272 }
273 }
274
275}
276}
277} /* namespace gsm */
278} /* namespace gr */