blob: 24cee305fab9cfcd15fe2da548dc6da90c193579 [file] [log] [blame]
Piotr Krysik61696ed2016-10-02 18:51:43 +02001/* -*- c++ -*- */
2/*
3 * @file
4 * @author Piotr Krysik <ptrkrysik@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
piotr437f5462014-02-04 17:57:25 +010023#include <string.h>
24#include "gsm_constants.h"
25
Piotr Krysik70c25a12017-01-03 08:01:23 +010026#include "osmocom/coding/gsm0503_coding.h"
Piotr Krysik61696ed2016-10-02 18:51:43 +020027#include <osmocom/core/utils.h>
piotr6d152d92014-02-21 00:02:44 +010028
Piotr Krysik61696ed2016-10-02 18:51:43 +020029static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count)
piotr437f5462014-02-04 17:57:25 +010030{
Piotr Krysik61696ed2016-10-02 18:51:43 +020031 int i;
piotr437f5462014-02-04 17:57:25 +010032
Piotr Krysik61696ed2016-10-02 18:51:43 +020033 for (i = 0; i < count; i++) {
34 if (*ubits == 0x23) {
35 ubits++;
36 sbits++;
37 continue;
38 }
39 if ((*ubits++) & 1)
40 *sbits++ = -127;
41 else
42 *sbits++ = 127;
piotr437f5462014-02-04 17:57:25 +010043 }
44
Piotr Krysik61696ed2016-10-02 18:51:43 +020045 return count;
piotr437f5462014-02-04 17:57:25 +010046}
47
piotr437f5462014-02-04 17:57:25 +010048int decode_sch(const unsigned char *buf, int * t1_o, int * t2_o, int * t3_o, int * ncc_o, int * bcc_o)
49{
50
Piotr Krysik61696ed2016-10-02 18:51:43 +020051 int t1, t2, t3p, t3, ncc, bcc;
52
53 uint8_t result[4];
54 ubit_t bursts_u[SCH_DATA_LEN*2];
55 sbit_t bursts_s[SCH_DATA_LEN*2];
piotr437f5462014-02-04 17:57:25 +010056
57 // extract encoded data from synchronization burst
58 /* buf, 39 bit */
59 /* buf + 39 + 64 = 103, 39 */
Piotr Krysik61696ed2016-10-02 18:51:43 +020060 memcpy(bursts_u, buf, SCH_DATA_LEN);
61 memcpy(bursts_u + SCH_DATA_LEN, buf + SCH_DATA_LEN + N_SYNC_BITS, SCH_DATA_LEN);
piotr437f5462014-02-04 17:57:25 +010062
Piotr Krysik61696ed2016-10-02 18:51:43 +020063 ubits2sbits(bursts_u, bursts_s, SCH_DATA_LEN*2);
64 if(gsm0503_sch_decode(result, bursts_s)==-1){
piotr437f5462014-02-04 17:57:25 +010065 return 1;
66 }
Piotr Krysik61696ed2016-10-02 18:51:43 +020067
piotr437f5462014-02-04 17:57:25 +010068 // Synchronization channel information, 44.018 page 171. (V7.2.0)
Piotr Krysik61696ed2016-10-02 18:51:43 +020069 uint8_t decoded_data[25];
70 osmo_pbit2ubit_ext(decoded_data, 0, result, 0, 25, 1);
piotr437f5462014-02-04 17:57:25 +010071 ncc =
72 (decoded_data[ 7] << 2) |
73 (decoded_data[ 6] << 1) |
74 (decoded_data[ 5] << 0);
75 bcc =
76 (decoded_data[ 4] << 2) |
77 (decoded_data[ 3] << 1) |
78 (decoded_data[ 2] << 0);
79 t1 =
80 (decoded_data[ 1] << 10) |
81 (decoded_data[ 0] << 9) |
82 (decoded_data[15] << 8) |
83 (decoded_data[14] << 7) |
84 (decoded_data[13] << 6) |
85 (decoded_data[12] << 5) |
86 (decoded_data[11] << 4) |
87 (decoded_data[10] << 3) |
88 (decoded_data[ 9] << 2) |
89 (decoded_data[ 8] << 1) |
90 (decoded_data[23] << 0);
91 t2 =
92 (decoded_data[22] << 4) |
93 (decoded_data[21] << 3) |
94 (decoded_data[20] << 2) |
95 (decoded_data[19] << 1) |
96 (decoded_data[18] << 0);
97 t3p =
98 (decoded_data[17] << 2) |
99 (decoded_data[16] << 1) |
100 (decoded_data[24] << 0);
101
102 t3 = 10 * t3p + 1;
103
104 // modulo arithmetic t3 - t2 mod 26
105// tt = ((t3 + 26) - t2) % 26;
106
107// fn = (51 * 26 * t1) + (51 * tt) + t3;
108
109 /*
110 * BSIC: Base Station Identification Code
111 * BCC: Base station Color Code
112 * NCC: Network Color Code
113 *
114 * FN: Frame Number
115 */
116
117// printf("bsic: %x (bcc: %u; ncc: %u)\tFN: %u\n", bsic, bsic & 7,
118// (bsic >> 3) & 7, fn);
119
120// if (fn_o)
121// *fn_o = fn;
122// if (bsic_o)
123 if (t1_o && t2_o && t3_o && ncc_o && bcc_o) {
124 *t1_o = t1;
125 *t2_o = t2;
126 *t3_o = t3;
127 *bcc_o = bcc;
128 *ncc_o = ncc;
129 }
130
131 return 0;
132}