blob: f1ba5ba38e9e2db8ac9b917265d946370a82bb34 [file] [log] [blame]
Vasil Velichkov7f259fd2018-05-06 02:13:36 +03001/* -*- c++ -*- */
2/*
3 * @file
4 * @author (C) 2018 by Vasil Velichkov <vvvelichkov@gmail.com>
5 * @section LICENSE
6 *
7 * Gr-gsm is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * Gr-gsm is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with gr-gsm; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <grgsm/gsmtap.h>
Vasil Velichkov2f0c0962019-01-24 18:25:44 +020028#include <grgsm/endian.h>
Vasil Velichkov7f259fd2018-05-06 02:13:36 +030029#include "tch_h_decoder_impl.h"
30
31extern "C" {
32#include "osmocom/gsm/protocol/gsm_04_08.h"
33#include "osmocom/coding/gsm0503_coding.h"
34}
35
36namespace gr {
37 namespace gsm {
38
39 static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count)
40 {
41 int i;
42
43 for (i = 0; i < count; i++) {
44 if (*ubits == 0x23) {
45 ubits++;
46 sbits++;
47 continue;
48 }
49 if ((*ubits++) & 1)
50 *sbits++ = -127;
51 else
52 *sbits++ = 127;
53 }
54
55 return count;
56 }
57
58
59 tch_h_decoder::sptr
60 tch_h_decoder::make(unsigned int sub_channel, std::string multi_rate, bool boundary_check)
61 {
62 return gnuradio::get_initial_sptr
63 (new tch_h_decoder_impl(sub_channel, multi_rate, boundary_check));
64 }
65
66 /*
67 * Constructor
68 */
69 tch_h_decoder_impl::tch_h_decoder_impl(unsigned int sub_channel, std::string multi_rate, bool boundary_check)
70 : gr::block("tch_h_decoder",
71 gr::io_signature::make(0, 0, 0),
72 gr::io_signature::make(0, 0, 0)),
73 d_collected_bursts_num(0),
74 d_tch_mode(TCH_HS),
75 d_sub_channel(sub_channel),
76 d_boundary_check(boundary_check),
77 d_boundary_decode(false),
78 d_header_sent(false),
79 d_ft(0),
80 d_cmr(0)
81 {
82 //setup input/output ports
83 message_port_register_in(pmt::mp("bursts"));
Vadim Yanitskiya1169202021-05-03 19:00:43 +020084 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_h_decoder_impl::decode, this, boost::placeholders::_1));
Vasil Velichkov7f259fd2018-05-06 02:13:36 +030085 message_port_register_out(pmt::mp("msgs"));
86 message_port_register_out(pmt::mp("voice"));
87
88 if(multi_rate.length())
89 {
90 std::cout<<"multi_rate configuration: "<<multi_rate<<std::endl;
91 if (multi_rate.length() < 4 || multi_rate.length() % 2)
92 {
93 throw std::invalid_argument("Invalid multi_rate hexstring");
94 }
95
96 std::vector<uint8_t> binary;
97 for (std::string::const_iterator it = multi_rate.begin();
98 it != multi_rate.end(); it += 2)
99 {
100 std::string byte(it, it + 2);
101 char* end = NULL;
102 errno = 0;
103 uint8_t b = strtoul(byte.c_str(), &end, 16);
104 if (errno != 0 || *end != '\0')
105 {
106 throw std::invalid_argument("Invalid multi_rate hexstring");
107 }
108 binary.push_back(b);
109 }
110
111 if (binary.size() < 2) {
112 throw std::invalid_argument("The multi_rate is too short");
113 }
114
115 //GSM A-I/F DTAP - Assignment Command
116 // Protocol Discriminator: Radio Resources Management messages (6)
117 // DTAP Radio Resources Management Message Type: Assignment Command (0x2e)
118 // Channel Description 2 - Description of the First Channel, after time
119 // Power Command
120 // Channel Mode - Mode of the First Channel(Channel Set 1)
121 // MultiRate configuration
122 // Element ID: 0x03
123 // Length: 4
124 // 001. .... = Multirate speech version: Adaptive Multirate speech version 1 (1)
125 // ...0 .... = NSCB: Noise Suppression Control Bit: Noise Suppression can be used (default) (0)
126 // .... 1... = ICMI: Initial Codec Mode Indicator: The initial codec mode is defined by the Start Mode field (1)
127 // .... ..00 = Start Mode: 0
128 // 0... .... = 12,2 kbit/s codec rate: is not part of the subset
129 // .0.. .... = 10,2 kbit/s codec rate: is not part of the subset
130 // ..0. .... = 7,95 kbit/s codec rate: is not part of the subset
131 // ...1 .... = 7,40 kbit/s codec rate: is part of the subset
132 // .... 0... = 6,70 kbit/s codec rate: is not part of the subset
133 // .... .0.. = 5,90 kbit/s codec rate: is not part of the subset
134 // .... ..0. = 5,15 kbit/s codec rate: is not part of the subset
135 // .... ...1 = 4,75 kbit/s codec rate: is part of the subset
136 // ..01 1010 = AMR Threshold: 13.0 dB (26)
137 // 0100 .... = AMR Hysteresis: 2.0 dB (4)
138
139 const uint8_t first = binary[0];
140 uint8_t multirate_speech_ver = (first >> 5) & 0x07;
141 if (multirate_speech_ver == 1)
142 {
143 d_tch_mode = TCH_AFS4_75;
144 }
145 else if (multirate_speech_ver == 2)
146 {
147 throw std::invalid_argument("Adaptive Multirate speech version 2 is not supported");
148 }
149 else
150 {
151 throw std::invalid_argument("Multirate speech version");
152 }
153
154 bool ncsb = (first >> 4) & 0x01;
155 bool icmi = (first >> 3) & 0x01;
156 uint8_t start = first & 0x03;
157
158 const uint8_t codecs = binary[1];
159 for (int i = 0; i < 8; i++)
160 {
161 if ((codecs >> i) & 1)
162 {
163 d_multi_rate_codes.push_back(i);
164 }
165 }
166
167 std::cout<<"Enabled AMR Codecs:"<<std::endl;
168 for(std::vector<uint8_t>::const_iterator it = d_multi_rate_codes.begin();
169 it != d_multi_rate_codes.end();
170 it ++)
171 {
172 switch(*it)
173 {
174 case 0:
175 std::cout<<"4,75 kbit/s codec rate: is part of the subset"<<std::endl;
176 break;
177 case 1:
178 std::cout<<"5,15 kbit/s codec rate: is part of the subset"<<std::endl;
179 break;
180 case 2:
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300181 std::cout<<"5,90 kbit/s codec rate: is part of the subset"<<std::endl;
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300182 break;
183 case 3:
184 std::cout<<"6,70 kbit/s codec rate: is part of the subset"<<std::endl;
185 break;
186 case 4:
187 std::cout<<"7,40 kbit/s codec rate: is part of the subset"<<std::endl;
188 break;
189 case 5:
190 std::cout<<"7,95 kbit/s codec rate: is part of the subset"<<std::endl;
191 break;
192 case 6:
193 std::cout<<"12,2 kbit/s codec rate: is part of the subset"<<std::endl;
194 }
195 }
196 if (d_multi_rate_codes.size() > 4) {
197 throw std::invalid_argument("More then 4 multirate codes");
198 }
199 }
200 }
201
202 tch_h_decoder_impl::~tch_h_decoder_impl()
203 {
204 }
205
206 void tch_h_decoder_impl::decode(pmt::pmt_t msg)
207 {
208 d_bursts[d_collected_bursts_num++] = msg;
209 if (d_collected_bursts_num <= 7)
210 {
211 return;
212 }
213
214 gsmtap_hdr* header = (gsmtap_hdr*)(pmt::blob_data(pmt::cdr(msg)));
215 uint32_t frame_nr = be32toh(header->frame_number);
216 bool uplink_burst = (be16toh(header->arfcn) & 0x4000) ? true : false;
217
218 //TODO: Check in 3gpp specs which frames could contains facch/h frames
219 //and replace this ugly formula with table
220 int fn_is_odd = (((frame_nr - (uplink_burst ? 10 : 15)) % 26) >> 2) & 1;
221
222 ubit_t bursts_u[116 * 6] = {0}; //facch/h is 6 bursts
223
224 //reorganize data
225 for (int ii = 0; ii < 8; ii++)
226 {
227 //skip the 4th and 5th bursts
228 if (ii == 4 || ii == 5) continue;
229
230 int8_t* burst_bits = (int8_t*)(pmt::blob_data(pmt::cdr(d_bursts[ii])))+sizeof(gsmtap_hdr);
231
232 //copy 6th and 7th burst to 4th and 5th position
233 int n = ii < 6 ? ii : ii - 2;
234
235 memcpy(&bursts_u[n*116], &burst_bits[3],58);
236 memcpy(&bursts_u[n*116+58], &burst_bits[3+57+1+26],58);
237 }
238
239 //Convert to sbits
240 sbit_t bursts_s[116 * 6] = {0};
241 ubits2sbits(bursts_u, bursts_s, 116 * 6);
242
243 //Prepare burst for the next iteration by shifting them by 4
244 for (int ii = 0; ii < 4; ii++) {
245 d_bursts[ii] = d_bursts[ii + 4];
246 }
247 d_collected_bursts_num = 4;
248
249 uint8_t frameBuffer[64];
250 int frameLength = -1;
251 int n_errors, n_bits_total;
252
253 if (d_tch_mode == TCH_HS)
254 {
255 frameLength = gsm0503_tch_hr_decode(frameBuffer, bursts_s, fn_is_odd, &n_errors, &n_bits_total);
256 }
257 else
258 {
259 frameLength = gsm0503_tch_ahs_decode(frameBuffer, bursts_s, fn_is_odd,
260 fn_is_odd, //int codec_mode_req,
261 &d_multi_rate_codes.front(), d_multi_rate_codes.size(),
262 &d_ft,
263 &d_cmr,
264 &n_errors, &n_bits_total);
265 }
266
267 if (frameLength < 12)
268 {
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300269 #if 0
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300270 if (!d_boundary_check || d_boundary_decode) {
271 std::cerr<<"Error! frame_nr:"<<frame_nr<<" mod26:"<<frame_nr%26
272 <<" fn_is_odd:"<<fn_is_odd<<" length:"<<frameLength<<std::endl;
273 }
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300274 #endif
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300275 return;
276 }
277 else if (frameLength == GSM_MACBLOCK_LEN) //FACCH/H
278 {
279 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
280 gsmtap_hdr* header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
281 int8_t header_plus_data[sizeof(gsmtap_hdr)+frameLength];
282 memcpy(header_plus_data, header, sizeof(gsmtap_hdr));
283 memcpy(header_plus_data+sizeof(gsmtap_hdr), frameBuffer, frameLength);
284 ((gsmtap_hdr*)header_plus_data)->type = GSMTAP_TYPE_UM;
285
286 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data, frameLength + sizeof(gsmtap_hdr));
287 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
288
289 message_port_pub(pmt::mp("msgs"), msg_out);
290
291 // if d_boundary_check is enabled, we set d_boundary_decode to true, when a
292 // "Connect" or "Connect Acknowledge" message is received, and
293 // we set d_boundary_decode back to false, when "Release" message is received
294 if (d_boundary_check)
295 {
296 // check if this is a call control message
297 if ((frameBuffer[3] & 0x0f) == 0x03)
298 {
Vasil Velichkov06321a32018-05-15 21:36:14 +0300299 // Alerting
300 if ((frameBuffer[4] & 0x3f) == 0x01)
301 {
302 if ((frameBuffer[5] == 0x1e) && //element id
303 (frameBuffer[6] == 2) && //length
304 ((frameBuffer[8] & 0x7f) == 0x08))
305 {
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300306 std::cout << "(CC) Alerting with In-band information" << std::endl;
Vasil Velichkov06321a32018-05-15 21:36:14 +0300307 //.000 1000 = Progress description: In-band information or appropriate pattern now available (8)
308 d_boundary_decode = true;
309 }
310 }
311 // Progress
312 else if ((frameBuffer[4] & 0x3f) == 0x03)
313 {
314 if ((frameBuffer[5] == 2) && //length
315 (frameBuffer[7] & 0x7f) == 0x08)
316 {
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300317 std::cout << "(CC) Progress with In-band information" << std::endl;
Vasil Velichkov06321a32018-05-15 21:36:14 +0300318 //.000 1000 = Progress description: In-band information or appropriate pattern now available (8)
319 d_boundary_decode = true;
320 }
321 }
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300322 // Connect specified in GSM 04.08, 9.3.5
Vasil Velichkov06321a32018-05-15 21:36:14 +0300323 else if ((frameBuffer[4] & 0x3f) == 0x07)
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300324 {
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300325 std::cout << "(CC) Connect" << std::endl;
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300326 d_boundary_decode = true;
327 }
328 // Connect Acknowledge specified in GSM 04.08, 9.3.6
329 else if ((frameBuffer[4] & 0x3f) == 0x0f)
330 {
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300331 std::cout << "(CC) Connect Acknowledge" << std::endl;
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300332 d_boundary_decode = true;
333 }
334 // Release specified in GSM 04.08, 9.3.18
335 else if ((frameBuffer[4] & 0x3f) == 0x2d)
336 {
Vasil Velichkov61a3ca62019-04-14 21:31:20 +0300337 std::cout << "(CC) Release" << std::endl;
Vasil Velichkov7f259fd2018-05-06 02:13:36 +0300338 d_boundary_decode = false;
339 }
340 }
341 }
342 return;
343 }
344
345 if (!d_header_sent && d_tch_mode != TCH_HS)
346 {
347 const unsigned char amr_nb_magic[7] = "#!AMR\n";
348 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(amr_nb_magic, 6)));
349 d_header_sent = true;
350 }
351
352 if (!n_errors && (!d_boundary_check || d_boundary_decode))
353 {
354 //std::cerr<<"Voice frame_nr:"<<frame_nr<<" mod26:"<<frame_nr%26<<" is_odd:"<<fn_is_odd
355 // <<" type:"<<(uint32_t)d_ft<<" cmr:"<<(uint32_t)d_cmr
356 // <<" errors:"<<n_errors<<std::endl;
357
358 if (d_tch_mode != TCH_HS)
359 {
360 //Move one byte to make space for the header
361 memmove(frameBuffer + 1, frameBuffer, frameLength);
362 //Add the AMR header
363 switch(frameLength)
364 {
365 case 12: frameBuffer[0] = (0 << 3); break; //TCH/AHS4.75
366 case 13: frameBuffer[0] = (1 << 3); break; //TCH/AHS5.15
367 case 15: frameBuffer[0] = (2 << 3); break; //TCH/AHS5.9
368 case 17: frameBuffer[0] = (3 << 3); break; //TCH/AHS6.7
369 case 19: frameBuffer[0] = (4 << 3); break; //TCH/AHS7.4
370 case 20: frameBuffer[0] = (5 << 3); break; //TCH/AHS7.95
371 default: std::cerr<<"Unexpected voice frame length:"<<frameLength<<std::endl; return;
372 }
373 frameLength += 1;
374 }
375
376 //std::ostringstream out;
377 //out << "voice frame: ";
378 //for (int i = 0; i < frameLength; i++)
379 // out << " " << (std::hex) << std::setw(2) << std::setfill('0') << (uint32_t)*(frameBuffer + i);
380 //std::cerr << out.str() << std::endl;
381 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer, frameLength)));
382 }
383 }
384 } /* namespace gsm */
385} /* namespace gr */
386