Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 4 | * @author (C) 2014 by Piotr Krysik <ptrkrysik@gmail.com> |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 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 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 23 | #include <string.h> |
Piotr Krysik | d7efc05 | 2017-11-07 19:33:22 +0100 | [diff] [blame] | 24 | #include <grgsm/gsm_constants.h> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 25 | |
Piotr Krysik | b24beeb | 2018-02-28 10:11:08 +0100 | [diff] [blame] | 26 | #include <stdbool.h> |
Piotr Krysik | 15219ab | 2018-02-27 08:44:56 +0100 | [diff] [blame] | 27 | #include <osmocom/coding/gsm0503_coding.h> |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 28 | #include <osmocom/core/utils.h> |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 29 | |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 30 | static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 31 | { |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 32 | int i; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 33 | |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 34 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 46 | return count; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 47 | } |
| 48 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 49 | int decode_sch(const unsigned char *buf, int * t1_o, int * t2_o, int * t3_o, int * ncc_o, int * bcc_o) |
| 50 | { |
| 51 | |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 52 | 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]; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 57 | |
| 58 | // extract encoded data from synchronization burst |
| 59 | /* buf, 39 bit */ |
| 60 | /* buf + 39 + 64 = 103, 39 */ |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 61 | memcpy(bursts_u, buf, SCH_DATA_LEN); |
| 62 | memcpy(bursts_u + SCH_DATA_LEN, buf + SCH_DATA_LEN + N_SYNC_BITS, SCH_DATA_LEN); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 63 | |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 64 | ubits2sbits(bursts_u, bursts_s, SCH_DATA_LEN*2); |
| 65 | if(gsm0503_sch_decode(result, bursts_s)==-1){ |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 66 | return 1; |
| 67 | } |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 68 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 69 | // Synchronization channel information, 44.018 page 171. (V7.2.0) |
Piotr Krysik | 61696ed | 2016-10-02 18:51:43 +0200 | [diff] [blame] | 70 | uint8_t decoded_data[25]; |
| 71 | osmo_pbit2ubit_ext(decoded_data, 0, result, 0, 25, 1); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 72 | 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 | } |