blob: a5ac83c1a7499aba35629b511a3cd4bc34aaa69e [file] [log] [blame]
piotrfaacc722014-07-20 23:48:32 +02001/* -*- c++ -*- */
2/*
3 * Copyright 2014 <+YOU OR YOUR COMPANY+>.
4 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <gnuradio/io_signature.h>
26#include <gsm/gsmtap.h>
27#include "control_channels_decoder_impl.h"
28
29namespace gr {
30 namespace gsm {
31
32 control_channels_decoder::sptr
33 control_channels_decoder::make()
34 {
35 return gnuradio::get_initial_sptr
36 (new control_channels_decoder_impl());
37 }
38
39 /*
ptrkrysik7f61c642014-10-30 08:57:27 +010040 * Constructor
piotrfaacc722014-07-20 23:48:32 +020041 */
42 control_channels_decoder_impl::control_channels_decoder_impl()
43 : gr::block("control_channels_decoder",
44 gr::io_signature::make(0, 0, 0),
45 gr::io_signature::make(0, 0, 0)),
46 d_collected_bursts_num(0)
47 {
48 //initialize de/interleaver
49 int j, k, B;
50 for (k = 0; k < CONV_SIZE; k++)
51 {
52 B = k % 4;
53 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
54 interleave_trans[k] = B * 114 + j; //114=57 + 57
55 }
ptrkrysik7f61c642014-10-30 08:57:27 +010056
57 //initialize decoder
piotrfaacc722014-07-20 23:48:32 +020058 FC_init(&fc_ctx, 40, 184);
ptrkrysik7f61c642014-10-30 08:57:27 +010059
60 //setup input/output ports
piotrfaacc722014-07-20 23:48:32 +020061 message_port_register_in(pmt::mp("bursts"));
62 set_msg_handler(pmt::mp("bursts"), boost::bind(&control_channels_decoder_impl::decode, this, _1));
piotrb529a592014-08-04 11:30:43 +020063 message_port_register_out(pmt::mp("msgs"));
piotrfaacc722014-07-20 23:48:32 +020064 }
65
piotrfaacc722014-07-20 23:48:32 +020066 control_channels_decoder_impl::~control_channels_decoder_impl()
67 {
68 }
69
70 void control_channels_decoder_impl::decode(pmt::pmt_t msg)
71 {
72 unsigned char iBLOCK[BLOCKS*iBLOCK_SIZE], hl, hn, conv_data[CONV_SIZE], decoded_data[PARITY_OUTPUT_SIZE];
73 d_bursts[d_collected_bursts_num] = msg;
74 d_collected_bursts_num++;
75 //get convecutive bursts
piotrfaacc722014-07-20 23:48:32 +020076
77 if(d_collected_bursts_num==4)
78 {
79 d_collected_bursts_num=0;
80 //reorganize data
81 for(int ii = 0; ii < 4; ii++)
82 {
83 pmt::pmt_t burst_content = pmt::cdr(d_bursts[ii]);
84 int8_t * burst_bits = (int8_t *)pmt::blob_data(burst_content);
85
86 for(int jj = 0; jj < 57; jj++)
87 {
88 iBLOCK[ii*iBLOCK_SIZE+jj] = burst_bits[jj + 3];
89 iBLOCK[ii*iBLOCK_SIZE+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
90 }
91 }
92 //deinterleave
93 for (int k = 0; k < CONV_SIZE; k++)
94 {
95 conv_data[k] = iBLOCK[interleave_trans[k]];
96 }
97 //convolutional code decode
98 int errors = conv_decode(decoded_data, conv_data);
99 //std::cout << "Errors:" << errors << " " << parity_check(decoded_data) << std::endl;
100 // check parity
101 // If parity check error detected try to fix it.
102
103 if (parity_check(decoded_data))
104 {
105 FC_init(&fc_ctx, 40, 184);
106 unsigned char crc_result[PARITY_OUTPUT_SIZE];
107 if (FC_check_crc(&fc_ctx, decoded_data, crc_result) == 0)
108 {
109 //("error: sacch: parity error (errors=%d fn=%d)\n", errors, ctx->fn);
piotrab663c82014-08-06 14:14:15 +0200110 //std::cout << "Uncorrectable errors!" << std::endl;
piotrfaacc722014-07-20 23:48:32 +0200111 errors = -1;
ptrkrysik18df2f32014-11-19 11:33:08 +0100112 return;
piotrfaacc722014-07-20 23:48:32 +0200113 } else {
114 //DEBUGF("Successfully corrected parity bits! (errors=%d fn=%d)\n", errors, ctx->fn);
115 //std::cout << "Corrected some errors" << std::endl;
116 memcpy(decoded_data, crc_result, PARITY_OUTPUT_SIZE);
117 errors = 0;
118 }
119 } else {
120 //std::cout << "Everything correct" << std::endl;
121 }
122 //compress bits
123 unsigned char outmsg[27];
124 unsigned char sbuf_len=224;
125 int i, j, c, pos=0;
126 for(i = 0; i < sbuf_len; i += 8) {
piotrab663c82014-08-06 14:14:15 +0200127 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
128 c |= (!!decoded_data[i + j]) << j;
129 }
130 outmsg[pos++] = c & 0xff;
131 }
piotrb529a592014-08-04 11:30:43 +0200132
133 //send message with header of the first burst
134 pmt::pmt_t header_blob = pmt::car(d_bursts[0]);
ptrkrysik7f61c642014-10-30 08:57:27 +0100135 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_blob);
136 header->type = GSMTAP_TYPE_UM;
ptrkrysik7f61c642014-10-30 08:57:27 +0100137 int8_t * header_content = (int8_t *)pmt::blob_data(header_blob);
138
139 int8_t header_plus_data[16+23];
140 memcpy(header_plus_data, header_content, 16);
141 memcpy(header_plus_data+16, outmsg, 23);
142
143 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,23+16);
144 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
145
piotrb529a592014-08-04 11:30:43 +0200146 message_port_pub(pmt::mp("msgs"), msg_out);
piotrfaacc722014-07-20 23:48:32 +0200147 }
148 return;
149 }
150 } /* namespace gsm */
151} /* namespace gr */
152