blob: 73f49e1899ba7c89222ff52d591d4b53d97d2451 [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;
Vasil Velichkov7f259fd2018-05-06 02:13:36 +030090 new_hdr->sub_slot = d_tch_h_channel;
Vasil Velichkov1828a312018-05-07 15:55:39 +030091
92 pmt::pmt_t msg_binary_blob = pmt::make_blob(new_msg,sizeof(gsmtap_hdr)+BURST_SIZE);
93 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
94
95 //distinguishing uplink and downlink bursts
96 bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false;
97
98 if(uplink_burst)
99 {
100 sacch_tch_demapper(fn_mod13, fn_mod26, frame_nr, d_bursts_sacch_ul,
101 d_frame_numbers_sacch_ul, d_bursts_ul, d_frame_numbers_ul, msg_out);
102 }
103 else
104 {
105 sacch_tch_demapper(fn_mod13, fn_mod26, frame_nr, d_bursts_sacch_dl,
106 d_frame_numbers_sacch_dl, d_bursts_dl, d_frame_numbers_dl, msg_out);
107 }
108}
109
110void tch_h_chans_demapper_impl::sacch_tch_demapper(uint32_t fn_mod13, u_int32_t fn_mod26, uint32_t frame_nr,
111 pmt::pmt_t *d_bursts_sacch,
112 uint32_t *d_frame_numbers_sacch, pmt::pmt_t d_bursts[3][8],
113 uint32_t d_frame_numbers[3][8], pmt::pmt_t msg_out)
114{
115 bool frames_are_consecutive = true;
116 if (fn_mod13 == 12)
117 {
118 // position of SACCH burst based on timeslot
119 // see specification gsm 05.02
120 uint32_t index;
121 bool is_sacch = false;
122
123 if (d_tch_h_channel == 0 && fn_mod26 == 12)
124 {
125 index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
126 is_sacch = true;
127 }
128 else if (d_tch_h_channel == 1 && fn_mod26 == 25)
129 {
130 index = (((frame_nr - 25) / 26) - (d_timeslot / 2)) % 4;
131 is_sacch = true;
132 }
133
134 if (is_sacch)
135 {
136 d_bursts_sacch[index] = msg_out;
137 d_frame_numbers_sacch[index] = frame_nr;
138
139 if (index == 3)
140 {
141 //check for a situation where some bursts were lost
142 //in this situation frame numbers won't be consecutive
143 frames_are_consecutive = true;
144 for(int jj=1; jj<4; jj++)
145 {
146 if((d_frame_numbers_sacch[jj]-d_frame_numbers_sacch[jj-1]) != 26)
147 {
148 frames_are_consecutive = false;
149 }
150 }
151 if(frames_are_consecutive)
152 {
153 //send bursts to the output
154 for(int jj=0; jj<4; jj++)
155 {
156 message_port_pub(pmt::mp("acch_bursts"), d_bursts_sacch[jj]);
157 }
158 }
159 }
160 }
161 }
162 else
163 {
164 bool our_sub=false;
165 // So, here I change the fn_mod13, with this changes I can
166 // process both subslot (d_tch_h_channel) with the same code.
167 //
168 // Theory about this on github
169 //
170 // Example:
171 //
172 // For subslot=0 our burst is 0,2,4,6 etc
173 // For subslot=1 it 1,3,5 etc
174 // But if we change it with this code,
175 // both will be 0,1,2,3,4 etc
176 // And we can proced two subslot like one.
177 if(fn_mod13%2==d_tch_h_channel) {
178 if(d_tch_h_channel==0) {
179 fn_mod13=fn_mod13/2;
180 }else{
181 fn_mod13-=1;
182 fn_mod13=fn_mod13/2;
183 }
184 // We work only with our subslot
185 our_sub=true;
186 }
187 if(our_sub) {
188
189 if (fn_mod13 <= 1)
190 {
191 // add to b1 and b3
192 d_bursts[0][fn_mod13] = msg_out;
193 d_bursts[2][fn_mod13+2] = msg_out;
194
195
196
197 d_frame_numbers[0][fn_mod13] = frame_nr;
198 d_frame_numbers[2][fn_mod13+2] = frame_nr;
199 }
200 else if (fn_mod13 >=2 && fn_mod13 <=3)
201 {
202 // add to b1 and b2
203 d_bursts[0][fn_mod13] = msg_out;
204 d_bursts[1][fn_mod13-2] = msg_out;
205
206
207 d_frame_numbers[0][fn_mod13] = frame_nr;
208 d_frame_numbers[1][fn_mod13-2] = frame_nr;
209 //d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
210 }
211 else if (fn_mod13 >=4 && fn_mod13 <=5)
212 {
213 // add to b1 and b2
214 d_bursts[1][fn_mod13-2] = msg_out;
215 d_bursts[2][fn_mod13-4] = msg_out;
216
217
218
219 d_frame_numbers[1][fn_mod13-2] = frame_nr;
220 d_frame_numbers[2][fn_mod13-4] = frame_nr;
221 //d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
222 }
223 // send burst 1 or burst 2 to output
224 //if ( fn_mod26 == 2 || fn_mod26 == 5 || fn_mod26 == 9)
225 if(fn_mod13 == 1 || fn_mod13 == 3|| fn_mod13 == 5)
226 {
227 int tch_burst_nr = 0;
228
229 if(fn_mod13==5) {
230 tch_burst_nr = 1;
231 }
232 if(fn_mod13==1) {
233 tch_burst_nr = 2;
234 }
235
236 //check for a situation where some bursts were lost
237 //in this situation frame numbers won't be consecutive
238 frames_are_consecutive = true;
239 // std::cout<<"br="<<tch_burst_nr<<" Array: ";
240 // std::cout<<d_frame_numbers[tch_burst_nr][0]<<" ";
241 bool one_tree=true;
242 for(int jj=1; jj<4; jj++)
243 {
244
245 if (((d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj-1]) != 2) && frames_are_consecutive)
246 {
247 frames_are_consecutive = false;
248
249 if (tch_burst_nr == 2 && jj == 2
250 && d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj - 1] == 3)
251 {
252 // std::cout<<" YEAH ";
253 frames_are_consecutive = true;
254 }
255
256 }
257 //}
258 }
259 // std::cout<<std::endl;
260
261 if(frames_are_consecutive)
262 {
263
264 for(int jj=0; jj<4; jj++)
265 {
266
267 message_port_pub(pmt::mp("tch_bursts"), d_bursts[tch_burst_nr][jj]);
268 }
269
270 // useless
271 // d_bursts_stolen[tch_burst_nr] = false;
272 }
273 }
274 }
275
276}
277}
278} /* namespace gsm */
279} /* namespace gr */