blob: 23ec7fd13c0803ca84778f315308b1580d26026a [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 /*
40 * The private constructor
41 */
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 }
56
57 FC_init(&fc_ctx, 40, 184);
58 message_port_register_in(pmt::mp("bursts"));
59 set_msg_handler(pmt::mp("bursts"), boost::bind(&control_channels_decoder_impl::decode, this, _1));
piotrb529a592014-08-04 11:30:43 +020060 message_port_register_out(pmt::mp("msgs"));
piotrfaacc722014-07-20 23:48:32 +020061 }
62
63 /*
64 * Our virtual destructor.
65 */
66 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
piotrb529a592014-08-04 11:30:43 +020076// pmt::pmt_t header_blob = pmt::car(msg);
77// gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_blob);
piotrfaacc722014-07-20 23:48:32 +020078
79 if(d_collected_bursts_num==4)
80 {
81 d_collected_bursts_num=0;
82 //reorganize data
83 for(int ii = 0; ii < 4; ii++)
84 {
85 pmt::pmt_t burst_content = pmt::cdr(d_bursts[ii]);
86 int8_t * burst_bits = (int8_t *)pmt::blob_data(burst_content);
87
88 for(int jj = 0; jj < 57; jj++)
89 {
90 iBLOCK[ii*iBLOCK_SIZE+jj] = burst_bits[jj + 3];
91 iBLOCK[ii*iBLOCK_SIZE+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
92 }
93 }
94 //deinterleave
95 for (int k = 0; k < CONV_SIZE; k++)
96 {
97 conv_data[k] = iBLOCK[interleave_trans[k]];
98 }
99 //convolutional code decode
100 int errors = conv_decode(decoded_data, conv_data);
101 //std::cout << "Errors:" << errors << " " << parity_check(decoded_data) << std::endl;
102 // check parity
103 // If parity check error detected try to fix it.
104
105 if (parity_check(decoded_data))
106 {
107 FC_init(&fc_ctx, 40, 184);
108 unsigned char crc_result[PARITY_OUTPUT_SIZE];
109 if (FC_check_crc(&fc_ctx, decoded_data, crc_result) == 0)
110 {
111 //("error: sacch: parity error (errors=%d fn=%d)\n", errors, ctx->fn);
piotrb529a592014-08-04 11:30:43 +0200112 std::cout << "Uncorrectable errors!" << std::endl;
piotrfaacc722014-07-20 23:48:32 +0200113 errors = -1;
114 } else {
115 //DEBUGF("Successfully corrected parity bits! (errors=%d fn=%d)\n", errors, ctx->fn);
116 //std::cout << "Corrected some errors" << std::endl;
117 memcpy(decoded_data, crc_result, PARITY_OUTPUT_SIZE);
118 errors = 0;
119 }
120 } else {
121 //std::cout << "Everything correct" << std::endl;
122 }
123 //compress bits
124 unsigned char outmsg[27];
125 unsigned char sbuf_len=224;
126 int i, j, c, pos=0;
127 for(i = 0; i < sbuf_len; i += 8) {
128 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
129 c |= (!!decoded_data[i + j]) << j;
130 }
131 outmsg[pos++] = c & 0xff;
132 }
piotrb529a592014-08-04 11:30:43 +0200133
piotrfaacc722014-07-20 23:48:32 +0200134 int jj=0;
135 while (jj < 23){
136 printf(" %02x", outmsg[jj]);//decoded_data[jj]);
137 jj++;
138 }
139 printf("\n");
140 fflush(stdout);
piotrb529a592014-08-04 11:30:43 +0200141
142 //send message with header of the first burst
143 pmt::pmt_t header_blob = pmt::car(d_bursts[0]);
144 pmt::pmt_t msg_binary_blob = pmt::make_blob(outmsg,23);
145 pmt::pmt_t msg_out = pmt::cons(header_blob, msg_binary_blob);
146
147 message_port_pub(pmt::mp("msgs"), msg_out);
piotrfaacc722014-07-20 23:48:32 +0200148 }
149 return;
150 }
151 } /* namespace gsm */
152} /* namespace gr */
153