Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2013 by Andreas Eversberg <jolly@eversberg.eu> |
| 3 | * (C) 2015 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program 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 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program 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 along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <unistd.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | |
| 28 | #include <osmocom/core/bits.h> |
| 29 | #include <osmocom/core/utils.h> |
| 30 | |
| 31 | #include <osmocom/coding/gsm0503_coding.h> |
| 32 | |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 33 | #define DUMP_U_AT(b, x, u) do { \ |
| 34 | printf("%s %02x %02x ", osmo_ubit_dump(b + x, 57), b[57 + x], b[58 + x]); \ |
| 35 | printf("%s\n", osmo_ubit_dump(b + 59 + x, 57)); \ |
| 36 | if (u <= x) \ |
| 37 | return; \ |
| 38 | } while(0) |
| 39 | |
| 40 | #define DUMP_S_AT(b, x, u) do { \ |
| 41 | printf("%s %02x %02x ", osmo_hexdump(b + x, 57), b[57 + x], b[58 + x]); \ |
| 42 | printf("%s\n", osmo_hexdump(b + 59 + x, 57)); \ |
| 43 | if (u <= x) \ |
| 44 | return; \ |
| 45 | } while(0) |
| 46 | |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 47 | /* Similar to OSMO_ASSERT, but does not panic() */ |
| 48 | #define CHECK_RC_OR_RET(exp, action) \ |
| 49 | if (!(exp)) { \ |
| 50 | printf("%s(%s): assert %s failed\n", __func__, action, #exp); \ |
| 51 | return; \ |
| 52 | } |
| 53 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 54 | #ifdef DEBUG |
| 55 | #define printd(fmt, args...) printf(fmt, ##args) |
| 56 | #else |
| 57 | #define printd(fmt, args...) |
| 58 | #endif |
| 59 | |
Pau Espin Pedrol | 41911d0 | 2018-02-01 12:10:33 +0100 | [diff] [blame] | 60 | static inline void dump_ubits(ubit_t *bursts_u, unsigned until) |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 61 | { |
| 62 | printf("U-Bits:\n"); |
| 63 | DUMP_U_AT(bursts_u, 0, until); |
| 64 | DUMP_U_AT(bursts_u, 116, until); |
| 65 | DUMP_U_AT(bursts_u, 232, until); |
| 66 | DUMP_U_AT(bursts_u, 348, until); |
| 67 | DUMP_U_AT(bursts_u, 464, until); |
| 68 | DUMP_U_AT(bursts_u, 580, until); |
| 69 | DUMP_U_AT(bursts_u, 696, until); |
| 70 | DUMP_U_AT(bursts_u, 812, until); |
| 71 | } |
| 72 | |
Pau Espin Pedrol | 41911d0 | 2018-02-01 12:10:33 +0100 | [diff] [blame] | 73 | static inline void dump_sbits(uint8_t *bursts_s, unsigned until) |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 74 | { |
| 75 | printf("S-Bits:\n"); |
| 76 | DUMP_S_AT(bursts_s, 0, until); |
| 77 | DUMP_S_AT(bursts_s, 116, until); |
| 78 | DUMP_S_AT(bursts_s, 232, until); |
| 79 | DUMP_S_AT(bursts_s, 348, until); |
| 80 | DUMP_S_AT(bursts_s, 464, until); |
| 81 | DUMP_S_AT(bursts_s, 580, until); |
| 82 | DUMP_S_AT(bursts_s, 696, until); |
| 83 | DUMP_S_AT(bursts_s, 812, until); |
| 84 | } |
| 85 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 86 | static void test_xcch(uint8_t *l2) |
| 87 | { |
| 88 | uint8_t result[23]; |
| 89 | ubit_t bursts_u[116 * 4]; |
| 90 | sbit_t bursts_s[116 * 4]; |
| 91 | int n_errors, n_bits_total; |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 92 | int rc; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 93 | |
| 94 | /* Encode L2 message */ |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 95 | printf("Encoding: %s\n", osmo_hexdump(l2, 23)); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 96 | rc = gsm0503_xcch_encode(bursts_u, l2); |
| 97 | CHECK_RC_OR_RET(rc == 0, "encoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 98 | |
| 99 | /* Prepare soft-bits */ |
Max | 29d489f | 2017-11-28 14:54:32 +0100 | [diff] [blame] | 100 | osmo_ubit2sbit(bursts_s, bursts_u, 116 * 4); |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 101 | dump_ubits(bursts_u, 348); |
| 102 | dump_sbits((uint8_t *)bursts_s, 348); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 103 | |
| 104 | /* Destroy some bits */ |
| 105 | memset(bursts_s, 0, 30); |
| 106 | memset(bursts_s + 116, 0, 30); |
| 107 | |
| 108 | /* Decode, correcting errors */ |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 109 | rc = gsm0503_xcch_decode(result, bursts_s, &n_errors, &n_bits_total); |
| 110 | CHECK_RC_OR_RET(rc == 0, "decoding"); |
| 111 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 112 | printf("Decoded: %s\n", osmo_hexdump(result, 23)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 113 | printf("xcch_decode: n_errors=%d n_bits_total=%d ber=%.2f\n", |
| 114 | n_errors, n_bits_total, (float) n_errors / n_bits_total); |
| 115 | |
Max | 33dbecb | 2017-11-28 19:49:05 +0100 | [diff] [blame] | 116 | OSMO_ASSERT(n_bits_total == 456); |
| 117 | OSMO_ASSERT(!memcmp(l2, result, 23)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 118 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 119 | printf("\n"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void test_rach(uint8_t bsic, uint8_t ra) |
| 123 | { |
Max | a478590 | 2019-03-05 11:30:39 +0100 | [diff] [blame] | 124 | int rc; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 125 | uint8_t result; |
| 126 | ubit_t bursts_u[36]; |
| 127 | sbit_t bursts_s[36]; |
| 128 | |
| 129 | /* Encode L2 message */ |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 130 | printd("Encoding: %02x\n", ra); |
Max | a478590 | 2019-03-05 11:30:39 +0100 | [diff] [blame] | 131 | rc = gsm0503_rach_ext_encode(bursts_u, ra, bsic, false); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 132 | CHECK_RC_OR_RET(rc == 0, "encoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 133 | |
| 134 | /* Prepare soft-bits */ |
Max | 29d489f | 2017-11-28 14:54:32 +0100 | [diff] [blame] | 135 | osmo_ubit2sbit(bursts_s, bursts_u, 36); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 136 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 137 | printd("U-Bits: %s\n", osmo_ubit_dump(bursts_u, 36)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 138 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 139 | printd("S-Bits: %s\n", osmo_hexdump((uint8_t *)bursts_s, 36)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 140 | |
| 141 | /* Destroy some bits */ |
| 142 | memset(bursts_s + 6, 0, 8); |
| 143 | |
| 144 | /* Decode, correcting errors */ |
Max | a478590 | 2019-03-05 11:30:39 +0100 | [diff] [blame] | 145 | rc = gsm0503_rach_decode_ber(&result, bursts_s, bsic, NULL, NULL); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 146 | CHECK_RC_OR_RET(rc == 0, "decoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 147 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 148 | printd("Decoded: %02x\n", result); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 149 | if (ra != result) |
| 150 | printf("FAIL [RACH]: encoded %u != %u decoded\n", ra, result); |
| 151 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 152 | printd("\n"); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void test_rach_ext(uint8_t bsic, uint16_t ra) |
| 156 | { |
Max | a478590 | 2019-03-05 11:30:39 +0100 | [diff] [blame] | 157 | int rc; |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 158 | uint16_t result = 3000; /* Max ext. RA is 2^11 = 2048 */ |
| 159 | ubit_t bursts_u[36]; |
| 160 | sbit_t bursts_s[36]; |
| 161 | |
| 162 | /* Encode L2 message */ |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 163 | printd("Encoding: %02x\n", ra); |
Max | a478590 | 2019-03-05 11:30:39 +0100 | [diff] [blame] | 164 | rc = gsm0503_rach_ext_encode(bursts_u, ra, bsic, true); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 165 | CHECK_RC_OR_RET(rc == 0, "encoding"); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 166 | |
| 167 | /* Prepare soft-bits */ |
| 168 | osmo_ubit2sbit(bursts_s, bursts_u, 36); |
| 169 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 170 | printd("U-Bits: %s\n", osmo_ubit_dump(bursts_u, 36)); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 171 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 172 | printd("S-Bits: %s\n", osmo_hexdump((uint8_t *)bursts_s, 36)); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 173 | |
| 174 | /* Destroy some bits */ |
| 175 | memset(bursts_s + 9, 0, 8); |
| 176 | |
| 177 | /* Decode, correcting errors */ |
Max | a478590 | 2019-03-05 11:30:39 +0100 | [diff] [blame] | 178 | rc = gsm0503_rach_ext_decode_ber(&result, bursts_s, bsic, NULL, NULL); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 179 | CHECK_RC_OR_RET(rc == 0, "decoding"); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 180 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 181 | printd("Decoded: %02x\n", result); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 182 | if (ra != result) |
| 183 | printf("FAIL [RACH ext]: encoded %u != %u decoded\n", ra, result); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 184 | |
Vadim Yanitskiy | 0f4919e | 2020-03-30 18:19:17 +0700 | [diff] [blame] | 185 | printd("\n"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 186 | } |
| 187 | |
Vadim Yanitskiy | 41ce675 | 2020-03-31 19:15:28 +0700 | [diff] [blame] | 188 | static void test_rach_11bit_sample(uint8_t bsic, const sbit_t *payload) |
| 189 | { |
| 190 | int n_errors, n_bits_total; |
| 191 | uint16_t ra11; |
| 192 | int rc; |
| 193 | |
| 194 | /* Decode, correcting errors */ |
| 195 | rc = gsm0503_rach_ext_decode_ber(&ra11, payload, bsic, &n_errors, &n_bits_total); |
| 196 | if (rc) { |
| 197 | printf("%s(): decoding failed (rc=%d)\n", __func__, rc); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | printf("Decoded RA11: 0x%03x\n", ra11); |
| 202 | } |
| 203 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 204 | static void test_sch(uint8_t *info) |
| 205 | { |
| 206 | uint8_t result[4]; |
| 207 | ubit_t bursts_u[78]; |
| 208 | sbit_t bursts_s[78]; |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 209 | int rc; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 210 | |
| 211 | /* Zero bits 25 and above */ |
| 212 | info[3] &= 1; |
| 213 | result[3] = 0; |
| 214 | |
| 215 | /* Encode L2 message */ |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 216 | printf("Encoding: %s\n", osmo_hexdump(info, 4)); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 217 | rc = gsm0503_sch_encode(bursts_u, info); |
| 218 | CHECK_RC_OR_RET(rc == 0, "encoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 219 | |
| 220 | /* Prepare soft-bits */ |
Max | 29d489f | 2017-11-28 14:54:32 +0100 | [diff] [blame] | 221 | osmo_ubit2sbit(bursts_s, bursts_u, 78); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 222 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 223 | printf("U-Bits: %s\n", osmo_ubit_dump(bursts_u, 78)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 224 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 225 | printf("S-Bits: %s\n", osmo_hexdump((uint8_t *)bursts_s, 78)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 226 | |
| 227 | /* Destroy some bits */ |
| 228 | memset(bursts_s + 6, 0, 10); |
| 229 | |
| 230 | /* Decode, correcting errors */ |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 231 | rc = gsm0503_sch_decode(result, bursts_s); |
| 232 | CHECK_RC_OR_RET(rc == 0, "decoding"); |
| 233 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 234 | printf("Decoded: %s\n", osmo_hexdump(result, 4)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 235 | |
Max | 33dbecb | 2017-11-28 19:49:05 +0100 | [diff] [blame] | 236 | OSMO_ASSERT(!memcmp(info, result, 4)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 237 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 238 | printf("\n"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static void test_fr(uint8_t *speech, int len) |
| 242 | { |
| 243 | uint8_t result[33]; |
| 244 | ubit_t bursts_u[116 * 8]; |
| 245 | sbit_t bursts_s[116 * 8]; |
| 246 | int n_errors, n_bits_total; |
| 247 | int rc; |
| 248 | |
| 249 | memset(bursts_u, 0x23, sizeof(bursts_u)); |
| 250 | memset(bursts_s, 0, sizeof(bursts_s)); |
| 251 | |
| 252 | /* Encode L2 message */ |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 253 | printf("Encoding: %s\n", osmo_hexdump(speech, len)); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 254 | rc = gsm0503_tch_fr_encode(bursts_u, speech, len, 1); |
| 255 | CHECK_RC_OR_RET(rc == 0, "encoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 256 | |
| 257 | /* Prepare soft-bits */ |
Max | 29d489f | 2017-11-28 14:54:32 +0100 | [diff] [blame] | 258 | osmo_ubit2sbit(bursts_s, bursts_u, 116 * 8); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 259 | |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 260 | dump_ubits(bursts_u, 812); |
| 261 | dump_sbits((uint8_t *)bursts_s, 812); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 262 | |
| 263 | /* Destroy some bits */ |
| 264 | memset(bursts_s + 6, 0, 20); |
| 265 | |
| 266 | /* Decode, correcting errors */ |
| 267 | rc = gsm0503_tch_fr_decode(result, bursts_s, 1, len == 31, |
| 268 | &n_errors, &n_bits_total); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 269 | CHECK_RC_OR_RET(rc == len, "decoding"); |
| 270 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 271 | printf("Decoded: %s\n", osmo_hexdump(result, len)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 272 | printf("tch_fr_decode: n_errors=%d n_bits_total=%d ber=%.2f\n", |
| 273 | n_errors, n_bits_total, (float)n_errors/n_bits_total); |
| 274 | |
Max | 33dbecb | 2017-11-28 19:49:05 +0100 | [diff] [blame] | 275 | OSMO_ASSERT(!memcmp(speech, result, len)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 276 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 277 | printf("\n"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void test_hr(uint8_t *speech, int len) |
| 281 | { |
| 282 | uint8_t result[23]; |
| 283 | ubit_t bursts_u[116 * 6]; |
| 284 | sbit_t bursts_s[116 * 6]; |
| 285 | int n_errors, n_bits_total; |
| 286 | int rc; |
| 287 | |
| 288 | memset(bursts_u, 0x23, sizeof(bursts_u)); |
| 289 | memset(bursts_s, 0, sizeof(bursts_s)); |
| 290 | |
| 291 | /* Encode L2 message */ |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 292 | printf("Encoding: %s\n", osmo_hexdump(speech, len)); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 293 | rc = gsm0503_tch_hr_encode(bursts_u, speech, len); |
| 294 | CHECK_RC_OR_RET(rc == 0, "encoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 295 | |
| 296 | /* Prepare soft-bits */ |
Max | 29d489f | 2017-11-28 14:54:32 +0100 | [diff] [blame] | 297 | osmo_ubit2sbit(bursts_s, bursts_u, 116 * 6); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 298 | |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 299 | dump_ubits(bursts_u, 580); |
| 300 | dump_sbits((uint8_t *)bursts_s, 580); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 301 | |
| 302 | /* Destroy some bits */ |
| 303 | memset(bursts_s + 6, 0, 20); |
| 304 | |
| 305 | /* Decode, correcting errors */ |
| 306 | rc = gsm0503_tch_hr_decode(result, bursts_s, 0, |
| 307 | &n_errors, &n_bits_total); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 308 | CHECK_RC_OR_RET(rc == len, "decoding"); |
| 309 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 310 | printf("Decoded: %s\n", osmo_hexdump(result, len)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 311 | printf("tch_hr_decode: n_errors=%d n_bits_total=%d ber=%.2f\n", |
| 312 | n_errors, n_bits_total, (float)n_errors/n_bits_total); |
| 313 | |
Max | 33dbecb | 2017-11-28 19:49:05 +0100 | [diff] [blame] | 314 | OSMO_ASSERT(!memcmp(speech, result, len)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 315 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 316 | printf("\n"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static void test_pdtch(uint8_t *l2, int len) |
| 320 | { |
| 321 | uint8_t result[len]; |
| 322 | ubit_t bursts_u[116 * 4]; |
| 323 | sbit_t bursts_s[116 * 4]; |
| 324 | int n_errors, n_bits_total; |
| 325 | int rc; |
| 326 | |
| 327 | /* Zero the not coded tail bits */ |
| 328 | switch (len) { |
| 329 | case 34: |
| 330 | case 54: |
| 331 | l2[len - 1] &= 0x7f; |
| 332 | result[len - 1] &= 0x7f; |
| 333 | break; |
| 334 | case 40: |
| 335 | l2[len - 1] &= 0x07; |
| 336 | result[len - 1] &= 0x07; |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | /* Encode L2 message */ |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 341 | printf("Encoding: %s\n", osmo_hexdump(l2, len)); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 342 | rc = gsm0503_pdtch_encode(bursts_u, l2, len); |
| 343 | CHECK_RC_OR_RET(rc == GSM0503_GPRS_BURSTS_NBITS, "encoding"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 344 | |
| 345 | /* Prepare soft-bits */ |
Max | 29d489f | 2017-11-28 14:54:32 +0100 | [diff] [blame] | 346 | osmo_ubit2sbit(bursts_s, bursts_u, 116 * 4); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 347 | |
Max | 458a6f5 | 2017-11-28 17:26:54 +0100 | [diff] [blame] | 348 | dump_ubits(bursts_u, 348); |
| 349 | dump_sbits((uint8_t *)bursts_s, 348); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 350 | |
| 351 | /* Decode */ |
| 352 | rc = gsm0503_pdtch_decode(result, bursts_s, NULL, |
| 353 | &n_errors, &n_bits_total); |
Vadim Yanitskiy | ca466cf | 2020-03-30 18:12:19 +0700 | [diff] [blame] | 354 | CHECK_RC_OR_RET(rc == len, "decoding"); |
| 355 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 356 | printf("Decoded: %s\n", osmo_hexdump(result, len)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 357 | printf("pdtch_decode: n_errors=%d n_bits_total=%d ber=%.2f\n", |
| 358 | n_errors, n_bits_total, (float)n_errors/n_bits_total); |
| 359 | |
Max | 33dbecb | 2017-11-28 19:49:05 +0100 | [diff] [blame] | 360 | OSMO_ASSERT(!memcmp(l2, result, len)); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 361 | |
Max | 0176b4a | 2017-11-28 18:04:23 +0100 | [diff] [blame] | 362 | printf("\n"); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | uint8_t test_l2[][23] = { |
| 366 | /* Dummy frame */ |
| 367 | { 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 368 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 369 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
| 370 | /* Random frame */ |
| 371 | { 0xa3, 0xaf, 0x5f, 0xc6, 0x36, 0x43, 0x44, 0xab, |
| 372 | 0xd9, 0x6d, 0x7d, 0x62, 0x24, 0xc9, 0xd2, 0x92, |
| 373 | 0xfa, 0x27, 0x5d, 0x71, 0x7a, 0x59, 0xa8 }, |
| 374 | /* jolly frame */ |
| 375 | { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 376 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 377 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, |
| 378 | }; |
| 379 | |
| 380 | uint8_t test_macblock[][54] = { |
| 381 | /* Random frame */ |
| 382 | { 0xa3, 0xaf, 0x5f, 0xc6, 0x36, 0x43, 0x44, 0xab, |
| 383 | 0xd9, 0x6d, 0x7d, 0x62, 0x24, 0xc9, 0xd2, 0x92, |
| 384 | 0xfa, 0x27, 0x5d, 0x71, 0x7a, 0x59, 0xa8, 0x42, |
| 385 | 0xa3, 0xaf, 0x5f, 0xc6, 0x36, 0x43, 0x44, 0xab, |
| 386 | 0xa3, 0xaf, 0x5f, 0xc6, 0x36, 0x43, 0x44, 0xab, |
| 387 | 0xd9, 0x6d, 0x7d, 0x62, 0x24, 0xc9, 0xd2, 0x92, |
| 388 | 0xfa, 0x27, 0x5d, 0x71, 0x7a, 0xa8 }, |
| 389 | /* jolly frame */ |
| 390 | { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 391 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 392 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, |
| 393 | }; |
| 394 | |
Vadim Yanitskiy | 41ce675 | 2020-03-31 19:15:28 +0700 | [diff] [blame] | 395 | /* 11-bit Access Burst soft-bits (payload only) from an EGPRS capable phone (BSIC 63) */ |
| 396 | static const sbit_t test_rach_11bit[6][36] = { |
| 397 | { 103, 109, -108, -110, 107, 108, -106, -120, -121, |
| 398 | -120, -105, 122, -104, -109, 108, 109, -109, -111, |
| 399 | 107, 111, -105, -119, -121, -104, 122, -120, 121, |
| 400 | -99, -121, -120, -122, -106, 109, 109, -108, -111 }, |
| 401 | |
| 402 | { 103, 109, -109, -109, 106, 107, -106, -121, -121, |
| 403 | -120, -106, 121, -120, 117, -122, 101, 109, -122, |
| 404 | 120, -120, 101, 118, 120, 102, -125, 101, 110, |
| 405 | -120, 121, -101, -121, -118, -121, -106, 108, 121 }, |
| 406 | |
| 407 | { -121, -122, -104, 123, -104, -108, 122, -104, -121, |
| 408 | -121, -102, 124, -105, -110, 107, 109, -108, -109, |
| 409 | 121, -122, 101, 107, -121, 105, 108, -110, -107, |
| 410 | 124, -104, -109, 120, -122, 100, 122, 104, -123 }, |
| 411 | |
| 412 | { -122, -123, -103, 123, -105, -109, 122, -105, -121, |
| 413 | -120, -104, 122, -120, 121, -101, -122, -120, -120, |
| 414 | -119, -105, 120, -106, -108, 123, -104, -113, 105, |
| 415 | 122, 101, -122, 119, -122, 117, -121, 119, -122 }, |
| 416 | |
| 417 | { 105, 110, -109, -109, 107, 108, -108, -120, -120, |
| 418 | -121, -106, 121, -104, -107, 106, 108, -108, -108, |
| 419 | 108, 107, -105, -120, -122, -104, 122, -119, 121, |
| 420 | -103, -122, -118, -120, -106, 108, 108, -110, -111 }, |
| 421 | |
| 422 | { 120, -103, -123, -104, 119, -121, 100, 123, 106, |
| 423 | -109, -107, 121, -122, 118, -121, 103, 108, -122, |
| 424 | 120, -119, 121, -103, -121, -119, -121, -103, 124, |
| 425 | -106, -108, 122, -103, -106, 121, -120, 119, -121 }, |
| 426 | }; |
| 427 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 428 | uint8_t test_speech_fr[33]; |
| 429 | uint8_t test_speech_efr[31]; |
| 430 | uint8_t test_speech_hr[15]; |
| 431 | |
| 432 | int main(int argc, char **argv) |
| 433 | { |
| 434 | int i, len_l2, len_mb; |
| 435 | |
Vadim Yanitskiy | 88c61c3 | 2020-03-30 20:50:49 +0700 | [diff] [blame] | 436 | len_l2 = ARRAY_SIZE(test_l2); |
| 437 | len_mb = ARRAY_SIZE(test_macblock); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 438 | |
| 439 | for (i = 0; i < len_l2; i++) |
| 440 | test_xcch(test_l2[i]); |
| 441 | |
| 442 | for (i = 0; i < 256; i++) { |
| 443 | test_rach(0x3f, i); |
| 444 | test_rach(0x00, i); |
| 445 | test_rach(0x1a, i); |
| 446 | } |
| 447 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 448 | for (i = 0; i < 2048; i++) { |
| 449 | test_rach_ext(0x3f, i); |
| 450 | test_rach_ext(0x00, i); |
| 451 | test_rach_ext(0x1a, i); |
| 452 | } |
| 453 | |
Vadim Yanitskiy | 41ce675 | 2020-03-31 19:15:28 +0700 | [diff] [blame] | 454 | for (i = 0; i < ARRAY_SIZE(test_rach_11bit); i++) |
| 455 | test_rach_11bit_sample(0x3f, test_rach_11bit[i]); |
| 456 | printf("\n"); |
| 457 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 458 | for (i = 0; i < len_l2; i++) |
| 459 | test_sch(test_l2[i]); |
| 460 | |
| 461 | for (i = 0; i < sizeof(test_speech_fr); i++) |
| 462 | test_speech_fr[i] = i; |
| 463 | test_speech_fr[0] = 0xd0; |
| 464 | test_fr(test_speech_fr, sizeof(test_speech_fr)); |
| 465 | |
| 466 | for (i = 0; i < sizeof(test_speech_efr); i++) |
| 467 | test_speech_efr[i] = i; |
| 468 | test_speech_efr[0] = 0xc0; |
| 469 | test_fr(test_speech_efr, sizeof(test_speech_efr)); |
| 470 | |
| 471 | for (i = 0; i < len_l2; i++) |
| 472 | test_fr(test_l2[i], sizeof(test_l2[0])); |
| 473 | |
| 474 | for (i = 0; i < sizeof(test_speech_hr); i++) |
| 475 | test_speech_hr[i] = i * 17; |
| 476 | test_speech_hr[0] = 0x00; |
| 477 | test_hr(test_speech_hr, sizeof(test_speech_hr)); |
| 478 | |
| 479 | for (i = 0; i < len_l2; i++) |
| 480 | test_hr(test_l2[i], sizeof(test_l2[0])); |
| 481 | |
| 482 | for (i = 0; i < len_mb; i++) { |
| 483 | test_pdtch(test_macblock[i], 23); |
| 484 | test_pdtch(test_macblock[i], 34); |
| 485 | test_pdtch(test_macblock[i], 40); |
| 486 | test_pdtch(test_macblock[i], 54); |
| 487 | } |
| 488 | |
| 489 | printf("Success\n"); |
| 490 | |
| 491 | return 0; |
| 492 | } |