blob: fff82ea1370d0bae43bdf688d0c715856a869bd3 [file] [log] [blame]
Piotr Krysik61696ed2016-10-02 18:51:43 +02001/* -*- c++ -*- */
2/*
3 * @file
Piotr Krysika6268a52017-08-23 16:02:19 +02004 * @author (C) 2014 by Piotr Krysik <ptrkrysik@gmail.com>
Piotr Krysik61696ed2016-10-02 18:51:43 +02005 * @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>
Piotr Krysikd7efc052017-11-07 19:33:22 +010024#include <grgsm/gsm_constants.h>
piotr437f5462014-02-04 17:57:25 +010025
Piotr Krysikb24beeb2018-02-28 10:11:08 +010026#include <stdbool.h>
Piotr Krysik15219ab2018-02-27 08:44:56 +010027#include <osmocom/coding/gsm0503_coding.h>
Piotr Krysik61696ed2016-10-02 18:51:43 +020028#include <osmocom/core/utils.h>
piotr6d152d92014-02-21 00:02:44 +010029
Piotr Krysik61696ed2016-10-02 18:51:43 +020030static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count)
piotr437f5462014-02-04 17:57:25 +010031{
Piotr Krysik61696ed2016-10-02 18:51:43 +020032 int i;
piotr437f5462014-02-04 17:57:25 +010033
Piotr Krysik61696ed2016-10-02 18:51:43 +020034 for (i = 0; i < count; i++) {
35 if (*ubits == 0x23) {
36 ubits++;
37 sbits++;
38 continue;
39 }
40 if ((*ubits++) & 1)
41 *sbits++ = -127;
42 else
43 *sbits++ = 127;
piotr437f5462014-02-04 17:57:25 +010044 }
45
Piotr Krysik61696ed2016-10-02 18:51:43 +020046 return count;
piotr437f5462014-02-04 17:57:25 +010047}
48
piotr437f5462014-02-04 17:57:25 +010049int decode_sch(const unsigned char *buf, int * t1_o, int * t2_o, int * t3_o, int * ncc_o, int * bcc_o)
50{
51
Piotr Krysik61696ed2016-10-02 18:51:43 +020052 int t1, t2, t3p, t3, ncc, bcc;
53
54 uint8_t result[4];
55 ubit_t bursts_u[SCH_DATA_LEN*2];
56 sbit_t bursts_s[SCH_DATA_LEN*2];
piotr437f5462014-02-04 17:57:25 +010057
58 // extract encoded data from synchronization burst
59 /* buf, 39 bit */
60 /* buf + 39 + 64 = 103, 39 */
Piotr Krysik61696ed2016-10-02 18:51:43 +020061 memcpy(bursts_u, buf, SCH_DATA_LEN);
62 memcpy(bursts_u + SCH_DATA_LEN, buf + SCH_DATA_LEN + N_SYNC_BITS, SCH_DATA_LEN);
piotr437f5462014-02-04 17:57:25 +010063
Piotr Krysik61696ed2016-10-02 18:51:43 +020064 ubits2sbits(bursts_u, bursts_s, SCH_DATA_LEN*2);
65 if(gsm0503_sch_decode(result, bursts_s)==-1){
piotr437f5462014-02-04 17:57:25 +010066 return 1;
67 }
Piotr Krysik61696ed2016-10-02 18:51:43 +020068
piotr437f5462014-02-04 17:57:25 +010069 // Synchronization channel information, 44.018 page 171. (V7.2.0)
Piotr Krysik61696ed2016-10-02 18:51:43 +020070 uint8_t decoded_data[25];
71 osmo_pbit2ubit_ext(decoded_data, 0, result, 0, 25, 1);
piotr437f5462014-02-04 17:57:25 +010072 ncc =
73 (decoded_data[ 7] << 2) |
74 (decoded_data[ 6] << 1) |
75 (decoded_data[ 5] << 0);
76 bcc =
77 (decoded_data[ 4] << 2) |
78 (decoded_data[ 3] << 1) |
79 (decoded_data[ 2] << 0);
80 t1 =
81 (decoded_data[ 1] << 10) |
82 (decoded_data[ 0] << 9) |
83 (decoded_data[15] << 8) |
84 (decoded_data[14] << 7) |
85 (decoded_data[13] << 6) |
86 (decoded_data[12] << 5) |
87 (decoded_data[11] << 4) |
88 (decoded_data[10] << 3) |
89 (decoded_data[ 9] << 2) |
90 (decoded_data[ 8] << 1) |
91 (decoded_data[23] << 0);
92 t2 =
93 (decoded_data[22] << 4) |
94 (decoded_data[21] << 3) |
95 (decoded_data[20] << 2) |
96 (decoded_data[19] << 1) |
97 (decoded_data[18] << 0);
98 t3p =
99 (decoded_data[17] << 2) |
100 (decoded_data[16] << 1) |
101 (decoded_data[24] << 0);
102
103 t3 = 10 * t3p + 1;
104
105 // modulo arithmetic t3 - t2 mod 26
106// tt = ((t3 + 26) - t2) % 26;
107
108// fn = (51 * 26 * t1) + (51 * tt) + t3;
109
110 /*
111 * BSIC: Base Station Identification Code
112 * BCC: Base station Color Code
113 * NCC: Network Color Code
114 *
115 * FN: Frame Number
116 */
117
118// printf("bsic: %x (bcc: %u; ncc: %u)\tFN: %u\n", bsic, bsic & 7,
119// (bsic >> 3) & 7, fn);
120
121// if (fn_o)
122// *fn_o = fn;
123// if (bsic_o)
124 if (t1_o && t2_o && t3_o && ncc_o && bcc_o) {
125 *t1_o = t1;
126 *t2_o = t2;
127 *t3_o = t3;
128 *bcc_o = bcc;
129 *ncc_o = ncc;
130 }
131
132 return 0;
133}