blob: 404796f98d1e3f98652c5060c83d2fc08c30fd28 [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
ptrkrysik617ba032014-11-21 10:11:05 +010029#define DATA_BYTES 23
30
piotrfaacc722014-07-20 23:48:32 +020031namespace gr {
32 namespace gsm {
33
34 control_channels_decoder::sptr
35 control_channels_decoder::make()
36 {
37 return gnuradio::get_initial_sptr
38 (new control_channels_decoder_impl());
39 }
40
41 /*
ptrkrysik7f61c642014-10-30 08:57:27 +010042 * Constructor
piotrfaacc722014-07-20 23:48:32 +020043 */
44 control_channels_decoder_impl::control_channels_decoder_impl()
45 : gr::block("control_channels_decoder",
46 gr::io_signature::make(0, 0, 0),
47 gr::io_signature::make(0, 0, 0)),
48 d_collected_bursts_num(0)
49 {
50 //initialize de/interleaver
51 int j, k, B;
52 for (k = 0; k < CONV_SIZE; k++)
53 {
54 B = k % 4;
55 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
56 interleave_trans[k] = B * 114 + j; //114=57 + 57
57 }
ptrkrysik7f61c642014-10-30 08:57:27 +010058
59 //initialize decoder
piotrfaacc722014-07-20 23:48:32 +020060 FC_init(&fc_ctx, 40, 184);
ptrkrysik7f61c642014-10-30 08:57:27 +010061
62 //setup input/output ports
piotrfaacc722014-07-20 23:48:32 +020063 message_port_register_in(pmt::mp("bursts"));
64 set_msg_handler(pmt::mp("bursts"), boost::bind(&control_channels_decoder_impl::decode, this, _1));
piotrb529a592014-08-04 11:30:43 +020065 message_port_register_out(pmt::mp("msgs"));
piotrfaacc722014-07-20 23:48:32 +020066 }
67
piotrfaacc722014-07-20 23:48:32 +020068 control_channels_decoder_impl::~control_channels_decoder_impl()
69 {
70 }
71
72 void control_channels_decoder_impl::decode(pmt::pmt_t msg)
73 {
74 unsigned char iBLOCK[BLOCKS*iBLOCK_SIZE], hl, hn, conv_data[CONV_SIZE], decoded_data[PARITY_OUTPUT_SIZE];
75 d_bursts[d_collected_bursts_num] = msg;
76 d_collected_bursts_num++;
77 //get convecutive bursts
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 {
ptrkrysik617ba032014-11-21 10:11:05 +010085 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
86 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
piotrfaacc722014-07-20 23:48:32 +020087
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);
piotrab663c82014-08-06 14:14:15 +0200112 //std::cout << "Uncorrectable errors!" << std::endl;
piotrfaacc722014-07-20 23:48:32 +0200113 errors = -1;
ptrkrysik18df2f32014-11-19 11:33:08 +0100114 return;
piotrfaacc722014-07-20 23:48:32 +0200115 } else {
116 //DEBUGF("Successfully corrected parity bits! (errors=%d fn=%d)\n", errors, ctx->fn);
117 //std::cout << "Corrected some errors" << std::endl;
118 memcpy(decoded_data, crc_result, PARITY_OUTPUT_SIZE);
119 errors = 0;
120 }
121 } else {
122 //std::cout << "Everything correct" << std::endl;
123 }
124 //compress bits
125 unsigned char outmsg[27];
126 unsigned char sbuf_len=224;
127 int i, j, c, pos=0;
128 for(i = 0; i < sbuf_len; i += 8) {
piotrab663c82014-08-06 14:14:15 +0200129 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
130 c |= (!!decoded_data[i + j]) << j;
131 }
132 outmsg[pos++] = c & 0xff;
133 }
piotrb529a592014-08-04 11:30:43 +0200134
135 //send message with header of the first burst
ptrkrysik617ba032014-11-21 10:11:05 +0100136 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
137 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
ptrkrysik7f61c642014-10-30 08:57:27 +0100138 header->type = GSMTAP_TYPE_UM;
ptrkrysik7f61c642014-10-30 08:57:27 +0100139
ptrkrysik617ba032014-11-21 10:11:05 +0100140 int8_t * header_content = (int8_t *)header;
141
142 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
143 memcpy(header_plus_data, header_content, sizeof(gsmtap_hdr));
144 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
145
146 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
ptrkrysik7f61c642014-10-30 08:57:27 +0100147 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
148
piotrb529a592014-08-04 11:30:43 +0200149 message_port_pub(pmt::mp("msgs"), msg_out);
piotrfaacc722014-07-20 23:48:32 +0200150 }
151 return;
152 }
153 } /* namespace gsm */
154} /* namespace gr */
155