Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <endian.h> |
| 3 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 4 | #include "RLCMAC_Types.hh" |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 5 | #include "RLCMAC_Templates.hh" |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 6 | #include "GSM_Types.hh" |
| 7 | /* Decoding of TS 44.060 GPRS RLC/MAC blocks, portions requiring manual functions |
| 8 | * beyond what TITAN RAW coder can handle internally. |
| 9 | * |
| 10 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 11 | * All rights reserved. |
| 12 | * |
| 13 | * Released under the terms of GNU General Public License, Version 2 or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * SPDX-License-Identifier: GPL-2.0-or-later |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | namespace RLCMAC__Types { |
| 20 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 21 | ///////////////////// |
| 22 | // INTENRAL HELPERS |
| 23 | ///////////////////// |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 25 | /* TS 04.60 10.3a.4.1.1 */ |
| 26 | struct gprs_rlc_ul_header_egprs_1 { |
| 27 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 28 | uint8_t r:1, |
| 29 | si:1, |
| 30 | cv:4, |
| 31 | tfi_hi:2; |
| 32 | uint8_t tfi_lo:3, |
| 33 | bsn1_hi:5; |
| 34 | uint8_t bsn1_lo:6, |
| 35 | bsn2_hi:2; |
| 36 | uint8_t bsn2_lo:8; |
| 37 | uint8_t cps:5, |
| 38 | rsb:1, |
| 39 | pi:1, |
| 40 | spare_hi:1; |
| 41 | uint8_t spare_lo:6, |
| 42 | dummy:2; |
| 43 | #else |
| 44 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 45 | uint8_t tfi_hi:2, cv:4, si:1, r:1; |
| 46 | uint8_t bsn1_hi:5, tfi_lo:3; |
| 47 | uint8_t bsn2_hi:2, bsn1_lo:6; |
| 48 | uint8_t bsn2_lo:8; |
| 49 | uint8_t spare_hi:1, pi:1, rsb:1, cps:5; |
| 50 | uint8_t dummy:2, spare_lo:6; |
| 51 | #endif |
| 52 | } __attribute__ ((packed)); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 53 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 54 | /* TS 04.60 10.3a.4.2.1 */ |
| 55 | struct gprs_rlc_ul_header_egprs_2 { |
| 56 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 57 | uint8_t r:1, |
| 58 | si:1, |
| 59 | cv:4, |
| 60 | tfi_hi:2; |
| 61 | uint8_t tfi_lo:3, |
| 62 | bsn1_hi:5; |
| 63 | uint8_t bsn1_lo:6, |
| 64 | cps_hi:2; |
| 65 | uint8_t cps_lo:1, |
| 66 | rsb:1, |
| 67 | pi:1, |
| 68 | spare_hi:5; |
| 69 | uint8_t spare_lo:5, |
| 70 | dummy:3; |
| 71 | #else |
| 72 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 73 | uint8_t tfi_hi:2, cv:4, si:1, r:1; |
| 74 | uint8_t bsn1_hi:5, tfi_lo:3; |
| 75 | uint8_t cps_hi:2, bsn1_lo:6; |
| 76 | uint8_t spare_hi:5, pi:1, rsb:1, cps_lo:1; |
| 77 | uint8_t dummy:3, spare_lo:5; |
| 78 | #endif |
| 79 | } __attribute__ ((packed)); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 80 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 81 | /* TS 04.60 10.3a.4.3.1 */ |
| 82 | struct gprs_rlc_ul_header_egprs_3 { |
| 83 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 84 | uint8_t r:1, |
| 85 | si:1, |
| 86 | cv:4, |
| 87 | tfi_hi:2; |
| 88 | uint8_t tfi_lo:3, |
| 89 | bsn1_hi:5; |
| 90 | uint8_t bsn1_lo:6, |
| 91 | cps_hi:2; |
| 92 | uint8_t cps_lo:2, |
| 93 | spb:2, |
| 94 | rsb:1, |
| 95 | pi:1, |
| 96 | spare:1, |
| 97 | dummy:1; |
| 98 | #else |
| 99 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 100 | uint8_t tfi_hi:2, cv:4, si:1, r:1; |
| 101 | uint8_t bsn1_hi:5, tfi_lo:3; |
| 102 | uint8_t cps_hi:2, bsn1_lo:6; |
| 103 | uint8_t dummy:1, spare:1, pi:1, rsb:1, spb:2, cps_lo:2; |
| 104 | #endif |
| 105 | } __attribute__ ((packed)); |
| 106 | |
| 107 | struct gprs_rlc_dl_header_egprs_1 { |
| 108 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 109 | uint8_t usf:3, |
| 110 | es_p:2, |
| 111 | rrbp:2, |
| 112 | tfi_hi:1; |
| 113 | uint8_t tfi_lo:4, |
| 114 | pr:2, |
| 115 | bsn1_hi:2; |
| 116 | uint8_t bsn1_mid:8; |
| 117 | uint8_t bsn1_lo:1, |
| 118 | bsn2_hi:7; |
| 119 | uint8_t bsn2_lo:3, |
| 120 | cps:5; |
| 121 | #else |
| 122 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 123 | uint8_t tfi_hi:1, rrbp:2, es_p:2, usf:3; |
| 124 | uint8_t bsn1_hi:2, pr:2, tfi_lo:4; |
| 125 | uint8_t bsn1_mid:8; |
| 126 | uint8_t bsn2_hi:7, bsn1_lo:1; |
| 127 | uint8_t cps:5, bsn2_lo:3; |
| 128 | #endif |
| 129 | } __attribute__ ((packed)); |
| 130 | |
| 131 | struct gprs_rlc_dl_header_egprs_2 { |
| 132 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 133 | uint8_t usf:3, |
| 134 | es_p:2, |
| 135 | rrbp:2, |
| 136 | tfi_hi:1; |
| 137 | uint8_t tfi_lo:4, |
| 138 | pr:2, |
| 139 | bsn1_hi:2; |
| 140 | uint8_t bsn1_mid:8; |
| 141 | uint8_t bsn1_lo:1, |
| 142 | cps:3, |
| 143 | dummy:4; |
| 144 | #else |
| 145 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 146 | uint8_t tfi_hi:1, rrbp:2, es_p:2, usf:3; |
| 147 | uint8_t bsn1_hi:2, pr:2, tfi_lo:4; |
| 148 | uint8_t bsn1_mid:8; |
| 149 | uint8_t dummy:4, cps:3, bsn1_lo:1; |
| 150 | #endif |
| 151 | } __attribute__ ((packed)); |
| 152 | |
| 153 | struct gprs_rlc_dl_header_egprs_3 { |
| 154 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 155 | uint8_t usf:3, |
| 156 | es_p:2, |
| 157 | rrbp:2, |
| 158 | tfi_hi:1; |
| 159 | uint8_t tfi_lo:4, |
| 160 | pr:2, |
| 161 | bsn1_hi:2; |
| 162 | uint8_t bsn1_mid:8; |
| 163 | uint8_t bsn1_lo:1, |
| 164 | cps:4, |
| 165 | spb:2, |
| 166 | dummy:1; |
| 167 | #else |
| 168 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 169 | uint8_t tfi_hi:1, rrbp:2, es_p:2, usf:3; |
| 170 | uint8_t bsn1_hi:2, pr:2, tfi_lo:4; |
| 171 | uint8_t bsn1_mid:8; |
| 172 | uint8_t dummy:1, spb:2, cps:4, bsn1_lo:1; |
| 173 | #endif |
| 174 | } __attribute__ ((packed)); |
| 175 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 176 | /* |
| 177 | static const char hex_chars[] = "0123456789abcdef"; |
| 178 | void printbuffer(const char* ptr, TTCN_Buffer& buf) { |
| 179 | int len = buf.get_len(); |
| 180 | const unsigned char* cbuf = buf.get_data(); |
| 181 | |
| 182 | fprintf(stderr, "printbuffer %s (len=%d): [", ptr, len); |
| 183 | |
| 184 | for (int i = 0; i < len; i++) { |
| 185 | fprintf(stderr, " %c%c", hex_chars[cbuf[i] >> 4], hex_chars[cbuf[i] & 0xf]); |
| 186 | } |
| 187 | |
| 188 | fprintf(stderr, " ]\n"); |
| 189 | } |
| 190 | */ |
| 191 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 192 | static CodingScheme::enum_type payload_len_2_coding_scheme(size_t payload_len) { |
| 193 | switch (payload_len) { |
| 194 | case 23: |
| 195 | return CodingScheme::CS__1; |
| 196 | case 34: |
| 197 | return CodingScheme::CS__2; |
| 198 | case 40: |
| 199 | return CodingScheme::CS__3; |
| 200 | case 54: |
| 201 | return CodingScheme::CS__4; |
| 202 | case 27: |
| 203 | return CodingScheme::MCS__1; |
| 204 | case 33: |
| 205 | return CodingScheme::MCS__2; |
| 206 | case 42: |
| 207 | return CodingScheme::MCS__3; |
| 208 | case 49: |
| 209 | return CodingScheme::MCS__4; |
| 210 | case 61: |
| 211 | return CodingScheme::MCS__5; |
| 212 | case 79: |
| 213 | return CodingScheme::MCS__6; |
| 214 | case 119: |
| 215 | return CodingScheme::MCS__7; |
| 216 | case 142: |
| 217 | return CodingScheme::MCS__8; |
| 218 | case 155: |
| 219 | return CodingScheme::MCS__9; |
| 220 | default: |
| 221 | return CodingScheme::CS__1; |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 222 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 225 | static unsigned int coding_scheme_2_data_block_len(CodingScheme::enum_type mcs) { |
| 226 | switch (mcs) { |
| 227 | case CodingScheme::MCS__0: |
| 228 | return 0; |
| 229 | case CodingScheme::MCS__1: |
| 230 | return 22; |
| 231 | case CodingScheme::MCS__2: |
| 232 | return 28; |
| 233 | case CodingScheme::MCS__3: |
| 234 | return 37; |
| 235 | case CodingScheme::MCS__4: |
| 236 | return 44; |
| 237 | case CodingScheme::MCS__5: |
| 238 | return 56; |
| 239 | case CodingScheme::MCS__6: |
| 240 | return 74; |
| 241 | case CodingScheme::MCS__7: |
| 242 | return 56; |
| 243 | case CodingScheme::MCS__8: |
| 244 | return 68; |
| 245 | case CodingScheme::MCS__9: |
| 246 | return 74; |
| 247 | default: |
| 248 | return 22; /* MCS1*/ |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static uint8_t bs2uint8(const BITSTRING& bs) |
| 253 | { |
| 254 | int len = bs.lengthof(); |
| 255 | int i; |
| 256 | uint8_t res = 0; |
| 257 | for (i = 0; i < len; i++) { |
| 258 | res = res << 1; |
| 259 | res |= (bs[i].get_bit() ? 1 : 0); |
| 260 | } |
| 261 | return res; |
| 262 | } |
| 263 | |
| 264 | /* determine the number of rlc data blocks and their size / offsets */ |
| 265 | static void |
| 266 | setup_rlc_mac_priv(CodingScheme::enum_type mcs, EgprsHeaderType::enum_type hdrtype, boolean is_uplink, |
| 267 | unsigned int *n_calls, unsigned int *data_block_bits, unsigned int *data_block_offsets) |
| 268 | { |
| 269 | unsigned int nc, dbl = 0, dbo[2] = {0,0}; |
| 270 | |
| 271 | dbl = coding_scheme_2_data_block_len(mcs); |
| 272 | |
| 273 | switch (hdrtype) { |
| 274 | case EgprsHeaderType::RLCMAC__HDR__TYPE__1: |
| 275 | nc = 3; |
| 276 | dbo[0] = is_uplink ? 5*8 + 6 : 5*8 + 0; |
| 277 | dbo[1] = dbo[0] + dbl * 8 + 2; |
| 278 | break; |
| 279 | case EgprsHeaderType::RLCMAC__HDR__TYPE__2: |
| 280 | nc = 2; |
| 281 | dbo[0] = is_uplink ? 4*8 + 5 : 3*8 + 4; |
| 282 | break; |
| 283 | case EgprsHeaderType::RLCMAC__HDR__TYPE__3: |
| 284 | nc = 2; |
| 285 | dbo[0] = 3*8 + 7; |
| 286 | break; |
| 287 | default: |
| 288 | nc = 1; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | *n_calls = nc; |
| 293 | *data_block_bits = dbl * 8 + 2; |
| 294 | data_block_offsets[0] = dbo[0]; |
| 295 | data_block_offsets[1] = dbo[1]; |
| 296 | } |
| 297 | |
| 298 | /* bit-shift the entire 'src' of length 'length_bytes' by 'offset_bits' |
Pau Espin Pedrol | ecaeb89 | 2020-04-30 19:58:15 +0200 | [diff] [blame] | 299 | * and store the result to caller-allocated 'buffer'. The shifting is |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 300 | * done lsb-first. */ |
| 301 | static void clone_aligned_buffer_lsbf(unsigned int offset_bits, unsigned int length_bytes, |
| 302 | const uint8_t *src, uint8_t *buffer) |
| 303 | { |
| 304 | unsigned int hdr_bytes; |
| 305 | unsigned int extra_bits; |
| 306 | unsigned int i; |
| 307 | |
| 308 | uint8_t c, last_c; |
| 309 | uint8_t *dst; |
| 310 | |
| 311 | hdr_bytes = offset_bits / 8; |
| 312 | extra_bits = offset_bits % 8; |
| 313 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 314 | //fprintf(stderr, "RLMAC: clone: hdr_bytes=%u extra_bits=%u (length_bytes=%u)\n", hdr_bytes, extra_bits, length_bytes); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 315 | |
| 316 | if (extra_bits == 0) { |
| 317 | /* It is aligned already */ |
| 318 | memcpy(buffer, src + hdr_bytes, length_bytes); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | dst = buffer; |
| 323 | src = src + hdr_bytes; |
| 324 | last_c = *(src++); |
| 325 | |
| 326 | for (i = 0; i < length_bytes; i++) { |
| 327 | c = src[i]; |
| 328 | *(dst++) = (last_c >> extra_bits) | (c << (8 - extra_bits)); |
| 329 | last_c = c; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* obtain an (aligned) EGPRS data block with given bit-offset and |
| 334 | * bit-length from the parent buffer */ |
| 335 | static void get_egprs_data_block(const TTCN_Buffer& orig_ttcn_buffer, unsigned int offset_bits, |
| 336 | unsigned int length_bits, TTCN_Buffer& dst_ttcn_buffer) |
| 337 | { |
| 338 | const unsigned int initial_spare_bits = 6; |
| 339 | unsigned char *aligned_buf = NULL; |
| 340 | size_t min_src_length_bytes = (offset_bits + length_bits + 7) / 8; |
| 341 | size_t length_bytes = (initial_spare_bits + length_bits + 7) / 8; |
| 342 | size_t accepted_len = length_bytes; |
| 343 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 344 | //fprintf(stderr, "RLMAC: trying to allocate %u bytes (orig is %zu bytes long with read pos %zu)\n", length_bytes, orig_ttcn_buffer.get_len(), orig_ttcn_buffer.get_pos()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 345 | dst_ttcn_buffer.get_end(aligned_buf, accepted_len); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 346 | if (accepted_len < length_bytes) { |
| 347 | fprintf(stderr, "RLMAC: ERROR! asked for %zu bytes but got %zu\n", length_bytes, accepted_len); |
| 348 | } |
| 349 | |
| 350 | /* Copy the data out of the tvb to an aligned buffer */ |
| 351 | clone_aligned_buffer_lsbf( |
| 352 | offset_bits - initial_spare_bits, length_bytes, |
| 353 | orig_ttcn_buffer.get_data(), |
| 354 | aligned_buf); |
| 355 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 356 | /* clear spare bits and move block header bits to the right */ |
| 357 | aligned_buf[0] = aligned_buf[0] >> initial_spare_bits; |
| 358 | |
| 359 | dst_ttcn_buffer.increase_length(length_bytes); |
| 360 | } |
| 361 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 362 | /* bit-shift the entire 'src' of length 'length_bytes' |
| 363 | * and store the result to caller-allocated 'buffer' by 'offset_bits'. The shifting is |
| 364 | * done lsb-first. */ |
| 365 | static void clone_unaligned_buffer_lsbf(unsigned int offset_bits, unsigned int length_bytes, |
| 366 | const uint8_t *src, uint8_t *buffer) |
| 367 | { |
| 368 | unsigned int hdr_bytes; |
| 369 | unsigned int extra_bits; |
| 370 | unsigned int i; |
| 371 | |
| 372 | uint8_t c, last_hdr_c, last_c; |
| 373 | uint8_t *dst; |
| 374 | |
| 375 | hdr_bytes = offset_bits / 8; |
| 376 | extra_bits = offset_bits % 8; |
| 377 | |
| 378 | //fprintf(stderr, "RLMAC: clone: hdr_bytes=%u extra_bits=%u (length_bytes=%u)\n", hdr_bytes, extra_bits, length_bytes); |
| 379 | |
| 380 | if (extra_bits == 0) { |
| 381 | /* It is aligned already */ |
| 382 | memcpy(buffer, src + hdr_bytes, length_bytes); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | /* Copy first header+data byte, it's not handled correctly by loop */ |
| 387 | dst = buffer + hdr_bytes; |
| 388 | last_hdr_c = *dst; |
| 389 | last_c = *dst << (8 - extra_bits); |
| 390 | |
| 391 | for (i = 0; i < length_bytes; i++) { |
| 392 | c = src[i]; |
| 393 | *(dst++) = (last_c >> (8 - extra_bits)) | (c << extra_bits); |
| 394 | last_c = c; |
| 395 | } |
| 396 | /* overwrite the lower extra_bits */ |
| 397 | *dst = (*dst & (0xff << extra_bits)) | (last_c >> (8 - extra_bits)); |
| 398 | |
| 399 | /* Copy back first header+data byte */ |
| 400 | dst = buffer + hdr_bytes; |
| 401 | *(dst++) = last_hdr_c | (src[0] << (8 - extra_bits)); |
| 402 | *dst |= (src[0] >> (extra_bits)) & (0xff >> (8 - extra_bits)); |
| 403 | } |
| 404 | |
| 405 | /* put an (aligned) EGPRS data block with given bit-offset and |
| 406 | * bit-length into parent buffer */ |
| 407 | static void put_egprs_data_block(const TTCN_Buffer& aligned_data_block_buffer, unsigned int offset_bits, |
| 408 | unsigned int length_bits, TTCN_Buffer& dst_ttcn_buffer) |
| 409 | { |
| 410 | const unsigned int initial_spare_bits = 6; |
| 411 | unsigned char *unaligned_buf = NULL; |
| 412 | char tmpbuf[120]; |
| 413 | int tmplen = dst_ttcn_buffer.get_len(); |
| 414 | //size_t max_length_bytes = (initial_spare_bits + length_bits + 7) / 8; |
| 415 | size_t length_bytes = tmplen + aligned_data_block_buffer.get_len(); |
| 416 | size_t accepted_len = length_bytes; |
| 417 | |
| 418 | //fprintf(stderr, "RLMAC: trying to allocate %u bytes\n", length_bytes); |
| 419 | |
| 420 | /* API .get_end() is the only one I could find to access writeable |
| 421 | memory in the buffer. It points to the end. Hence, we first copy |
| 422 | (readonly) data to tmpbuf and later clear() so that .get_end() |
| 423 | provides us with a pointer to the start of the buffer. */ |
| 424 | memcpy(tmpbuf, dst_ttcn_buffer.get_data(), tmplen); |
| 425 | dst_ttcn_buffer.clear(); |
| 426 | dst_ttcn_buffer.get_end(unaligned_buf, accepted_len); |
| 427 | if (accepted_len < tmplen) { |
| 428 | fprintf(stderr, "RLMAC: ERROR! asked for %zu bytes but got %zu\n", length_bytes, accepted_len); |
| 429 | } |
| 430 | memcpy(unaligned_buf, tmpbuf, tmplen); |
| 431 | |
| 432 | /* Copy the data out of the tvb to an aligned buffer */ |
| 433 | clone_unaligned_buffer_lsbf( |
| 434 | offset_bits - initial_spare_bits, length_bytes, |
| 435 | aligned_data_block_buffer.get_data(), |
| 436 | unaligned_buf); |
| 437 | |
| 438 | dst_ttcn_buffer.increase_length(length_bytes); |
| 439 | } |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 440 | |
Pau Espin Pedrol | cb00c52 | 2020-11-06 19:52:05 +0100 | [diff] [blame^] | 441 | /* Append padding bytes and spare bits at the end of ttcn_buffer, based on requested CS */ |
| 442 | static void encode_trailing_padding_spb(TTCN_Buffer& ttcn_buffer, CodingScheme cs) |
| 443 | { |
| 444 | uint8_t buf[256]; /* enough to fit any RLCMAC buffer*/ |
| 445 | uint32_t blk_len = RLCMAC__Templates::f__rlcmac__cs__mcs2block__len(cs); |
| 446 | uint32_t blk_len_no_spb = RLCMAC__Templates::f__rlcmac__cs__mcs2block__len__no__spare__bits(cs); |
| 447 | uint32_t data_len = ttcn_buffer.get_len(); |
| 448 | |
| 449 | if (data_len > blk_len_no_spb) { |
| 450 | fprintf(stderr, "Buffer too large for requested CS! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 451 | // TODO: throw exception? |
| 452 | } |
| 453 | |
| 454 | for (int i = 0; i < blk_len_no_spb - data_len; i++) |
| 455 | buf[i] = 0x2b; /* Padding bits if needed */ |
| 456 | for (int i = blk_len_no_spb - data_len; i < blk_len - data_len; i++) |
| 457 | buf[i] = 0x00; /* Spare bits if needed */ |
| 458 | |
| 459 | const OCTETSTRING& pad_octstr = OCTETSTRING(blk_len - data_len, buf); |
| 460 | ttcn_buffer.put_string(pad_octstr); |
| 461 | } |
| 462 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 463 | ///////////////////// |
| 464 | // DECODE |
| 465 | ///////////////////// |
| 466 | |
| 467 | /* DECODE DOWNLINK */ |
| 468 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 469 | RlcmacDlDataBlock dec__RlcmacDlDataBlock(const OCTETSTRING& stream) |
| 470 | { |
| 471 | RlcmacDlDataBlock ret_val; |
| 472 | TTCN_Buffer ttcn_buffer(stream); |
| 473 | int num_llc_blocks = 0; |
| 474 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 475 | ret_val.cs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 476 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 477 | /* use automatic/generated decoder for header */ |
| 478 | ret_val.mac__hdr().decode(DlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 479 | |
| 480 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 481 | if (ret_val.mac__hdr().hdr__ext().e() == false) { |
| 482 | /* extension octet follows, i.e. optional Llc length octets */ |
| 483 | while (1) { |
| 484 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 485 | LlcBlock lb; |
Harald Welte | 439e546 | 2018-03-08 23:21:17 +0100 | [diff] [blame] | 486 | lb.hdr()().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 487 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 488 | |
| 489 | /* if E == '1'B, we can proceed further */ |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 490 | if (lb.hdr()().e() == true) |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* RLC blocks at end */ |
| 496 | if (ret_val.mac__hdr().hdr__ext().e() == true) { |
| 497 | LlcBlock lb; |
| 498 | unsigned int length = ttcn_buffer.get_read_len(); |
| 499 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 500 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 501 | * data block */ |
| 502 | lb.payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 503 | ttcn_buffer.increase_pos(length); |
| 504 | ret_val.blocks()[0] = lb; |
| 505 | } else { |
| 506 | if (ret_val.blocks().is_bound()) { |
| 507 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 508 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 509 | if (length > ttcn_buffer.get_read_len()) |
| 510 | length = ttcn_buffer.get_read_len(); |
| 511 | ret_val.blocks()[i].payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 512 | ttcn_buffer.increase_pos(length); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return ret_val; |
| 518 | } |
| 519 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 520 | static |
| 521 | EgprsDlMacDataHeader dec__EgprsDlMacDataHeader_type1(const OCTETSTRING& stream) |
| 522 | { |
| 523 | EgprsDlMacDataHeader ret_val; |
| 524 | |
| 525 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 526 | |
| 527 | return ret_val; |
| 528 | } |
| 529 | |
| 530 | static |
| 531 | EgprsDlMacDataHeader dec__EgprsDlMacDataHeader_type2(const OCTETSTRING& stream) |
| 532 | { |
| 533 | EgprsDlMacDataHeader ret_val; |
| 534 | |
| 535 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 536 | |
| 537 | return ret_val; |
| 538 | } |
| 539 | |
| 540 | static |
| 541 | EgprsDlMacDataHeader dec__EgprsDlMacDataHeader_type3(const OCTETSTRING& stream) |
| 542 | { |
| 543 | TTCN_Buffer ttcn_buffer(stream); |
| 544 | EgprsDlMacDataHeader ret_val; |
| 545 | const struct gprs_rlc_dl_header_egprs_3 *egprs3; |
| 546 | uint8_t tmp; |
| 547 | |
| 548 | egprs3 = static_cast<const struct gprs_rlc_dl_header_egprs_3 *> |
| 549 | ((const void *)ttcn_buffer.get_data()); |
| 550 | |
| 551 | ret_val.header__type() = EgprsHeaderType::RLCMAC__HDR__TYPE__3; |
| 552 | ret_val.tfi() = egprs3->tfi_lo << 1 | egprs3->tfi_hi << 0; |
| 553 | ret_val.rrbp() = egprs3->rrbp; |
| 554 | tmp = egprs3->es_p; |
| 555 | ret_val.esp() = BITSTRING(2, &tmp); |
| 556 | ret_val.usf() = egprs3->usf; |
| 557 | ret_val.bsn1() = egprs3->bsn1_lo << 10 | egprs3->bsn1_mid << 2 | egprs3->bsn1_hi; |
| 558 | ret_val.bsn2__offset() = 0; /*TODO: mark optional and not set ? */ |
| 559 | ret_val.pr() = egprs3->pr; |
| 560 | ret_val.spb() = egprs3->spb; |
| 561 | ret_val.cps() = egprs3->cps; |
| 562 | |
| 563 | ttcn_buffer.increase_pos(sizeof(*egprs3)); |
| 564 | return ret_val; |
| 565 | } |
| 566 | |
| 567 | static |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 568 | RlcmacDlEgprsDataBlock dec__RlcmacDlEgprsDataBlock(const OCTETSTRING& stream) |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 569 | { |
| 570 | RlcmacDlEgprsDataBlock ret_val; |
| 571 | TTCN_Buffer ttcn_buffer(stream); |
| 572 | TTCN_Buffer aligned_buffer; |
| 573 | int num_llc_blocks = 0; |
| 574 | unsigned int data_block_bits, data_block_offsets[2]; |
| 575 | unsigned int num_calls; |
| 576 | const uint8_t *ti_e; |
| 577 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 578 | ret_val.mcs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 579 | switch (ret_val.mcs()) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 580 | case CodingScheme::MCS__0: |
| 581 | case CodingScheme::MCS__1: |
| 582 | case CodingScheme::MCS__2: |
| 583 | case CodingScheme::MCS__3: |
| 584 | case CodingScheme::MCS__4: |
| 585 | ret_val.mac__hdr() = dec__EgprsDlMacDataHeader_type3(stream); |
| 586 | break; |
| 587 | case CodingScheme::MCS__5: |
| 588 | case CodingScheme::MCS__6: |
| 589 | ret_val.mac__hdr() = dec__EgprsDlMacDataHeader_type2(stream); |
| 590 | break; |
| 591 | case CodingScheme::MCS__7: |
| 592 | case CodingScheme::MCS__8: |
| 593 | case CodingScheme::MCS__9: |
| 594 | ret_val.mac__hdr() = dec__EgprsDlMacDataHeader_type1(stream); |
| 595 | break; |
| 596 | } |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 597 | setup_rlc_mac_priv(ret_val.mcs(), ret_val.mac__hdr().header__type(), false, |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 598 | &num_calls, &data_block_bits, data_block_offsets); |
| 599 | get_egprs_data_block(ttcn_buffer, data_block_offsets[0], data_block_bits, aligned_buffer); |
| 600 | |
| 601 | ti_e = aligned_buffer.get_read_data(); |
| 602 | ret_val.fbi() = *ti_e & 0x02 ? true : false; |
| 603 | ret_val.e() = *ti_e & 0x01 ? true : false; |
| 604 | aligned_buffer.increase_pos(1); |
| 605 | |
| 606 | /* optional extension octets, containing LI+E of Llc blocks */ |
| 607 | if (ret_val.e() == false) { |
| 608 | /* extension octet follows, i.e. optional Llc length octets */ |
| 609 | while (1) { |
| 610 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 611 | EgprsLlcBlock lb; |
| 612 | lb.hdr()().decode(EgprsLlcBlockHdr_descr_, aligned_buffer, TTCN_EncDec::CT_RAW); |
| 613 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 614 | |
| 615 | /* if E == '1'B, we can proceed further */ |
| 616 | if (lb.hdr()().e() == true) |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /* RLC blocks at end */ |
| 622 | if (ret_val.blocks().is_bound()) { |
| 623 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 624 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
| 625 | if (length > aligned_buffer.get_read_len()) |
| 626 | length = aligned_buffer.get_read_len(); |
| 627 | ret_val.blocks()[i].payload() = OCTETSTRING(length, aligned_buffer.get_read_data()); |
| 628 | aligned_buffer.increase_pos(length); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return ret_val; |
| 633 | } |
| 634 | |
| 635 | RlcmacDlBlock dec__RlcmacDlBlock(const OCTETSTRING& stream) |
| 636 | { |
| 637 | RlcmacDlBlock ret_val; |
| 638 | size_t stream_len = stream.lengthof(); |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 639 | CodingScheme::enum_type cs_mcs = payload_len_2_coding_scheme(stream_len); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 640 | unsigned char pt; |
| 641 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 642 | switch (cs_mcs) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 643 | case CodingScheme::CS__1: |
| 644 | case CodingScheme::CS__2: |
| 645 | case CodingScheme::CS__3: |
| 646 | case CodingScheme::CS__4: |
| 647 | pt = stream[0].get_octet() >> 6; |
| 648 | if (pt == MacPayloadType::MAC__PT__RLC__DATA) |
| 649 | ret_val.data() = dec__RlcmacDlDataBlock(stream); |
| 650 | else |
| 651 | ret_val.ctrl() = dec__RlcmacDlCtrlBlock(stream); |
| 652 | break; |
| 653 | case CodingScheme::MCS__0: |
| 654 | case CodingScheme::MCS__1: |
| 655 | case CodingScheme::MCS__2: |
| 656 | case CodingScheme::MCS__3: |
| 657 | case CodingScheme::MCS__4: |
| 658 | case CodingScheme::MCS__5: |
| 659 | case CodingScheme::MCS__6: |
| 660 | case CodingScheme::MCS__7: |
| 661 | case CodingScheme::MCS__8: |
| 662 | case CodingScheme::MCS__9: |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 663 | ret_val.data__egprs() = dec__RlcmacDlEgprsDataBlock(stream); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 664 | break; |
| 665 | } |
| 666 | return ret_val; |
| 667 | } |
| 668 | |
| 669 | /* DECODE UPLINK */ |
| 670 | |
| 671 | RlcmacUlDataBlock dec__RlcmacUlDataBlock(const OCTETSTRING& stream) |
| 672 | { |
| 673 | RlcmacUlDataBlock ret_val; |
| 674 | TTCN_Buffer ttcn_buffer(stream); |
| 675 | int num_llc_blocks = 0; |
| 676 | |
| 677 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 678 | TTCN_Logger::log_event_str("==================================\n" |
| 679 | "dec_RlcmacUlDataBlock(): Stream before decoding: "); |
| 680 | stream.log(); |
| 681 | TTCN_Logger::end_event(); |
| 682 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 683 | ret_val.cs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 684 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 685 | /* use automatic/generated decoder for header */ |
| 686 | ret_val.mac__hdr().decode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 687 | |
| 688 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 689 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream after decoding hdr: "); |
| 690 | ttcn_buffer.log(); |
| 691 | TTCN_Logger::end_event(); |
| 692 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 693 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val after decoding hdr: "); |
| 694 | ret_val.log(); |
| 695 | TTCN_Logger::end_event(); |
| 696 | |
| 697 | /* Manually decoder remainder of ttcn_buffer, containing optional header octets, |
| 698 | * optional tlli, optional pfi and LLC Blocks */ |
| 699 | |
| 700 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 701 | if (ret_val.mac__hdr().e() == false) { |
| 702 | /* extension octet follows, i.e. optional Llc length octets */ |
| 703 | while (1) { |
| 704 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 705 | LlcBlock lb; |
| 706 | lb.hdr()().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 707 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 708 | |
| 709 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 710 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream after decoding ExtOct: "); |
| 711 | ttcn_buffer.log(); |
| 712 | TTCN_Logger::end_event(); |
| 713 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 714 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val after decoding ExtOct: "); |
| 715 | ret_val.log(); |
| 716 | TTCN_Logger::end_event(); |
| 717 | |
| 718 | /* if E == '1'B, we can proceed further */ |
| 719 | if (lb.hdr()().e() == true) |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /* parse optional TLLI */ |
| 725 | if (ret_val.mac__hdr().tlli__ind()) { |
| 726 | ret_val.tlli() = OCTETSTRING(4, ttcn_buffer.get_read_data()); |
| 727 | ttcn_buffer.increase_pos(4); |
Pau Espin Pedrol | a2c5b6e | 2020-04-30 19:59:13 +0200 | [diff] [blame] | 728 | } else { |
| 729 | ret_val.tlli() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 730 | } |
| 731 | /* parse optional PFI */ |
| 732 | if (ret_val.mac__hdr().pfi__ind()) { |
| 733 | ret_val.pfi().decode(RlcmacUlDataBlock_pfi_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
Pau Espin Pedrol | a2c5b6e | 2020-04-30 19:59:13 +0200 | [diff] [blame] | 734 | } else { |
| 735 | ret_val.pfi() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* RLC blocks at end */ |
| 739 | if (ret_val.mac__hdr().e() == true) { |
| 740 | LlcBlock lb; |
| 741 | unsigned int length = ttcn_buffer.get_read_len(); |
| 742 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 743 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 744 | * data block */ |
| 745 | lb.payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 746 | ttcn_buffer.increase_pos(length); |
| 747 | ret_val.blocks()[0] = lb; |
| 748 | } else { |
| 749 | if (ret_val.blocks().is_bound()) { |
| 750 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 751 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
| 752 | if (length > ttcn_buffer.get_read_len()) |
| 753 | length = ttcn_buffer.get_read_len(); |
| 754 | ret_val.blocks()[i].payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 755 | ttcn_buffer.increase_pos(length); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 761 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream before return: "); |
| 762 | ttcn_buffer.log(); |
| 763 | TTCN_Logger::end_event(); |
| 764 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 765 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val before return: "); |
| 766 | ret_val.log(); |
| 767 | TTCN_Logger::end_event(); |
| 768 | |
| 769 | return ret_val; |
| 770 | } |
| 771 | |
| 772 | static |
| 773 | EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type1(const OCTETSTRING& stream) |
| 774 | { |
| 775 | EgprsUlMacDataHeader ret_val; |
| 776 | |
| 777 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 778 | |
| 779 | return ret_val; |
| 780 | } |
| 781 | |
| 782 | static |
| 783 | EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type2(const OCTETSTRING& stream) |
| 784 | { |
| 785 | EgprsUlMacDataHeader ret_val; |
| 786 | |
| 787 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 788 | |
| 789 | return ret_val; |
| 790 | } |
| 791 | |
| 792 | static |
| 793 | EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type3(const OCTETSTRING& stream) |
| 794 | { |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 795 | TTCN_Buffer ttcn_buffer(stream); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 796 | EgprsUlMacDataHeader ret_val; |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 797 | const struct gprs_rlc_ul_header_egprs_3 *egprs3; |
| 798 | uint8_t tmp; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 799 | |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 800 | egprs3 = static_cast<const struct gprs_rlc_ul_header_egprs_3 *> |
| 801 | ((const void *)ttcn_buffer.get_data()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 802 | |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 803 | ret_val.header__type() = EgprsHeaderType::RLCMAC__HDR__TYPE__3; |
| 804 | ret_val.tfi() = egprs3->tfi_lo << 2 | egprs3->tfi_hi << 0; |
| 805 | ret_val.countdown() = egprs3->cv; |
| 806 | tmp = egprs3->si; |
| 807 | ret_val.foi__si() = BITSTRING(1, &tmp); |
| 808 | tmp = egprs3->r; |
| 809 | ret_val.r__ri() = BITSTRING(1, &tmp); |
| 810 | ret_val.bsn1() = egprs3->bsn1_lo << 5 | egprs3->bsn1_hi << 0; |
| 811 | ret_val.cps() = egprs3->cps_lo << 2 | egprs3->cps_hi << 0; |
| 812 | ret_val.pfi__ind() = egprs3->pi; |
| 813 | tmp = egprs3->rsb; |
| 814 | ret_val.rsb() = BITSTRING(1, &tmp); |
| 815 | tmp = egprs3->spb; |
| 816 | ret_val.spb() = BITSTRING(2, &tmp); |
| 817 | |
| 818 | ttcn_buffer.increase_pos(sizeof(*egprs3)); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 819 | return ret_val; |
| 820 | } |
| 821 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 822 | RlcmacUlEgprsDataBlock dec__RlcmacUlEgprsDataBlock(const OCTETSTRING& stream) |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 823 | { |
| 824 | RlcmacUlEgprsDataBlock ret_val; |
| 825 | TTCN_Buffer ttcn_buffer(stream); |
| 826 | TTCN_Buffer aligned_buffer; |
| 827 | int num_llc_blocks = 0; |
| 828 | unsigned int data_block_bits, data_block_offsets[2]; |
| 829 | unsigned int num_calls; |
| 830 | const uint8_t *ti_e; |
| 831 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 832 | ret_val.mcs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 833 | switch (ret_val.mcs()) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 834 | case CodingScheme::MCS__1: |
| 835 | case CodingScheme::MCS__2: |
| 836 | case CodingScheme::MCS__3: |
| 837 | case CodingScheme::MCS__4: |
| 838 | ret_val.mac__hdr() = dec__EgprsUlMacDataHeader_type3(stream); |
| 839 | break; |
| 840 | case CodingScheme::MCS__5: |
| 841 | case CodingScheme::MCS__6: |
| 842 | ret_val.mac__hdr() = dec__EgprsUlMacDataHeader_type2(stream); |
| 843 | break; |
| 844 | case CodingScheme::MCS__7: |
| 845 | case CodingScheme::MCS__8: |
| 846 | case CodingScheme::MCS__9: |
| 847 | ret_val.mac__hdr() = dec__EgprsUlMacDataHeader_type1(stream); |
| 848 | break; |
| 849 | } |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 850 | setup_rlc_mac_priv(ret_val.mcs(), ret_val.mac__hdr().header__type(), true, |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 851 | &num_calls, &data_block_bits, data_block_offsets); |
| 852 | get_egprs_data_block(ttcn_buffer, data_block_offsets[0], data_block_bits, aligned_buffer); |
| 853 | |
| 854 | ti_e = aligned_buffer.get_read_data(); |
| 855 | ret_val.tlli__ind() = *ti_e & 0x02 ? true : false; |
| 856 | ret_val.e() = *ti_e & 0x01 ? true : false; |
| 857 | aligned_buffer.increase_pos(1); |
| 858 | |
| 859 | /* Manually decoder remainder of aligned_buffer, containing optional header octets, |
| 860 | * optional tlli, optional pfi and LLC Blocks */ |
| 861 | |
| 862 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 863 | if (ret_val.e() == false) { |
| 864 | /* extension octet follows, i.e. optional Llc length octets */ |
| 865 | while (1) { |
| 866 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 867 | EgprsLlcBlock lb; |
| 868 | lb.hdr()().decode(EgprsLlcBlockHdr_descr_, aligned_buffer, TTCN_EncDec::CT_RAW); |
| 869 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 870 | |
| 871 | /* if E == '1'B, we can proceed further */ |
| 872 | if (lb.hdr()().e() == true) |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /* parse optional TLLI */ |
| 878 | if (ret_val.tlli__ind()) { |
| 879 | ret_val.tlli() = OCTETSTRING(4, aligned_buffer.get_read_data()); |
| 880 | aligned_buffer.increase_pos(4); |
Pau Espin Pedrol | a2c5b6e | 2020-04-30 19:59:13 +0200 | [diff] [blame] | 881 | } else { |
| 882 | ret_val.tlli() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 883 | } |
| 884 | /* parse optional PFI */ |
| 885 | if (ret_val.mac__hdr().pfi__ind()) { |
Pau Espin Pedrol | 331f525 | 2020-04-30 20:05:29 +0200 | [diff] [blame] | 886 | ret_val.pfi().decode(RlcmacUlEgprsDataBlock_pfi_descr_, aligned_buffer, TTCN_EncDec::CT_RAW); |
Pau Espin Pedrol | a2c5b6e | 2020-04-30 19:59:13 +0200 | [diff] [blame] | 887 | } else { |
| 888 | ret_val.pfi() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* RLC blocks at end */ |
| 892 | if (ret_val.e() == true) { |
| 893 | EgprsLlcBlock lb; |
| 894 | unsigned int length = aligned_buffer.get_read_len(); |
| 895 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 896 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 897 | * data block */ |
| 898 | lb.payload() = OCTETSTRING(length, aligned_buffer.get_read_data()); |
| 899 | aligned_buffer.increase_pos(length); |
| 900 | ret_val.blocks()[0] = lb; |
| 901 | } else { |
| 902 | if (ret_val.blocks().is_bound()) { |
| 903 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 904 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
| 905 | if (length > aligned_buffer.get_read_len()) |
| 906 | length = aligned_buffer.get_read_len(); |
| 907 | ret_val.blocks()[i].payload() = OCTETSTRING(length, aligned_buffer.get_read_data()); |
| 908 | aligned_buffer.increase_pos(length); |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return ret_val; |
| 914 | } |
| 915 | |
| 916 | RlcmacUlBlock dec__RlcmacUlBlock(const OCTETSTRING& stream) |
| 917 | { |
| 918 | RlcmacUlBlock ret_val; |
| 919 | size_t stream_len = stream.lengthof(); |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 920 | CodingScheme::enum_type cs_mcs = payload_len_2_coding_scheme(stream_len); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 921 | unsigned char pt; |
| 922 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 923 | switch (cs_mcs) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 924 | case CodingScheme::CS__1: |
| 925 | case CodingScheme::CS__2: |
| 926 | case CodingScheme::CS__3: |
| 927 | case CodingScheme::CS__4: |
| 928 | pt = stream[0].get_octet() >> 6; |
| 929 | if (pt == MacPayloadType::MAC__PT__RLC__DATA) |
| 930 | ret_val.data() = dec__RlcmacUlDataBlock(stream); |
| 931 | else |
| 932 | ret_val.ctrl() = dec__RlcmacUlCtrlBlock(stream); |
| 933 | break; |
| 934 | case CodingScheme::MCS__1: |
| 935 | case CodingScheme::MCS__2: |
| 936 | case CodingScheme::MCS__3: |
| 937 | case CodingScheme::MCS__4: |
| 938 | case CodingScheme::MCS__5: |
| 939 | case CodingScheme::MCS__6: |
| 940 | case CodingScheme::MCS__7: |
| 941 | case CodingScheme::MCS__8: |
| 942 | case CodingScheme::MCS__9: |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 943 | ret_val.data__egprs() = dec__RlcmacUlEgprsDataBlock(stream); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | |
| 947 | return ret_val; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | ///////////////////// |
| 952 | // ENCODE |
| 953 | ///////////////////// |
| 954 | |
| 955 | /* ENCODE DOWNLINK */ |
| 956 | |
| 957 | OCTETSTRING enc__RlcmacDlDataBlock(const RlcmacDlDataBlock& si) |
| 958 | { |
| 959 | RlcmacDlDataBlock in = si; |
| 960 | OCTETSTRING ret_val; |
| 961 | TTCN_Buffer ttcn_buffer; |
| 962 | int i; |
| 963 | |
| 964 | /* Fix 'e' bit of initial header based on following blocks */ |
| 965 | if (!in.blocks().is_bound() || |
| 966 | (in.blocks().size_of() == 1 && !in.blocks()[0].hdr().is_bound())) |
| 967 | in.mac__hdr().hdr__ext().e() = true; |
| 968 | else |
| 969 | in.mac__hdr().hdr__ext().e() = false; |
| 970 | |
| 971 | /* use automatic/generated decoder for header */ |
| 972 | in.mac__hdr().encode(DlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 973 | |
| 974 | /* Add LI octets, if any */ |
| 975 | if (in.blocks().is_bound() && |
| 976 | (in.blocks().size_of() != 1 || in.blocks()[0].hdr().is_bound())) { |
| 977 | /* first write LI octets */ |
| 978 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 979 | /* fix the 'E' bit in case it is not clear */ |
| 980 | if (i < in.blocks().size_of()-1) |
| 981 | in.blocks()[i].hdr()().e() = false; |
| 982 | else |
| 983 | in.blocks()[i].hdr()().e() = true; |
| 984 | in.blocks()[i].hdr()().encode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 985 | } |
| 986 | } |
| 987 | if (in.blocks().is_bound()) { |
| 988 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 989 | if (!in.blocks()[i].is_bound()) |
| 990 | continue; |
| 991 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 992 | } |
| 993 | } |
| 994 | |
Pau Espin Pedrol | cb00c52 | 2020-11-06 19:52:05 +0100 | [diff] [blame^] | 995 | encode_trailing_padding_spb(ttcn_buffer, in.cs()); |
| 996 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 997 | ttcn_buffer.get_string(ret_val); |
| 998 | return ret_val; |
| 999 | } |
| 1000 | |
| 1001 | static |
| 1002 | void enc__RlcmacDlEgprsDataHeader_type1(const EgprsDlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1003 | { |
| 1004 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 1005 | } |
| 1006 | |
| 1007 | static |
| 1008 | void enc__RlcmacDlEgprsDataHeader_type2(const EgprsDlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1009 | { |
| 1010 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 1011 | } |
| 1012 | |
| 1013 | static |
| 1014 | void enc__RlcmacDlEgprsDataHeader_type3(const EgprsDlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1015 | { |
| 1016 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 1017 | } |
| 1018 | |
| 1019 | OCTETSTRING enc__RlcmacDlEgprsDataBlock(const RlcmacDlEgprsDataBlock& si) |
| 1020 | { |
| 1021 | RlcmacDlEgprsDataBlock in = si; |
| 1022 | OCTETSTRING ret_val; |
| 1023 | TTCN_Buffer ttcn_buffer; |
| 1024 | int i; |
| 1025 | |
| 1026 | /* Fix 'e' bit of initial header based on following blocks */ |
| 1027 | if (!in.blocks().is_bound() || |
| 1028 | (in.blocks().size_of() == 1 && !in.blocks()[0].hdr().is_bound())) |
| 1029 | in.e() = true; |
| 1030 | else |
| 1031 | in.e() = false; |
| 1032 | |
| 1033 | switch (in.mac__hdr().header__type()) { |
| 1034 | case EgprsHeaderType::RLCMAC__HDR__TYPE__1: |
| 1035 | enc__RlcmacDlEgprsDataHeader_type1(si.mac__hdr(), ttcn_buffer); |
| 1036 | break; |
| 1037 | case EgprsHeaderType::RLCMAC__HDR__TYPE__2: |
| 1038 | enc__RlcmacDlEgprsDataHeader_type2(si.mac__hdr(), ttcn_buffer); |
| 1039 | break; |
| 1040 | case EgprsHeaderType::RLCMAC__HDR__TYPE__3: |
| 1041 | enc__RlcmacDlEgprsDataHeader_type3(si.mac__hdr(), ttcn_buffer); |
| 1042 | default: |
| 1043 | break; /* TODO: error */ |
| 1044 | } |
| 1045 | |
| 1046 | /* Add LI octets, if any */ |
| 1047 | if (in.blocks().is_bound() && |
| 1048 | (in.blocks().size_of() != 1 || in.blocks()[0].hdr().is_bound())) { |
| 1049 | /* first write LI octets */ |
| 1050 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1051 | /* fix the 'E' bit in case it is not clear */ |
| 1052 | if (i < in.blocks().size_of()-1) |
| 1053 | in.blocks()[i].hdr()().e() = false; |
| 1054 | else |
| 1055 | in.blocks()[i].hdr()().e() = true; |
| 1056 | in.blocks()[i].hdr()().encode(EgprsLlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 1057 | } |
| 1058 | } |
| 1059 | if (in.blocks().is_bound()) { |
| 1060 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1061 | if (!in.blocks()[i].is_bound()) |
| 1062 | continue; |
| 1063 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 1064 | } |
| 1065 | } |
| 1066 | |
Pau Espin Pedrol | cb00c52 | 2020-11-06 19:52:05 +0100 | [diff] [blame^] | 1067 | encode_trailing_padding_spb(ttcn_buffer, in.mcs()); |
| 1068 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1069 | ttcn_buffer.get_string(ret_val); |
| 1070 | return ret_val; |
| 1071 | } |
| 1072 | |
| 1073 | OCTETSTRING enc__RlcmacDlBlock(const RlcmacDlBlock& si) |
| 1074 | { |
| 1075 | if (si.ischosen(RlcmacDlBlock::ALT_data__egprs)) |
| 1076 | return enc__RlcmacDlEgprsDataBlock(si.data__egprs()); |
| 1077 | else if (si.ischosen(RlcmacDlBlock::ALT_data)) |
| 1078 | return enc__RlcmacDlDataBlock(si.data()); |
| 1079 | else |
| 1080 | return enc__RlcmacDlCtrlBlock(si.ctrl()); |
| 1081 | } |
| 1082 | |
| 1083 | /* ENCODE UPLINK */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1084 | |
| 1085 | OCTETSTRING enc__RlcmacUlDataBlock(const RlcmacUlDataBlock& si) |
| 1086 | { |
| 1087 | RlcmacUlDataBlock in = si; |
| 1088 | OCTETSTRING ret_val; |
| 1089 | TTCN_Buffer ttcn_buffer; |
| 1090 | int i; |
| 1091 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1092 | if (!in.blocks().is_bound()) { |
Pau Espin Pedrol | ecaeb89 | 2020-04-30 19:58:15 +0200 | [diff] [blame] | 1093 | /* we don't have any blocks: Add length value (zero) */ |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1094 | in.mac__hdr().e() = false; /* E=0: extension octet follows */ |
| 1095 | } else if (in.blocks().size_of() == 1 && in.blocks()[0].hdr() == OMIT_VALUE) { |
| 1096 | /* If there's only a single block, and that block has no HDR value defined, */ |
| 1097 | in.mac__hdr().e() = true; /* E=0: extension octet follows */ |
| 1098 | } else { |
| 1099 | /* Length value */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1100 | in.mac__hdr().e() = false; |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1101 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1102 | |
| 1103 | /* Fix other presence indications */ |
Harald Welte | 2072ab6 | 2017-07-31 18:33:35 +0200 | [diff] [blame] | 1104 | in.mac__hdr().tlli__ind() = in.tlli().is_bound() && in.tlli() != OMIT_VALUE; |
| 1105 | in.mac__hdr().pfi__ind() = in.pfi().is_bound() && in.pfi() != OMIT_VALUE; |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1106 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1107 | /* use automatic/generated encoder for header */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1108 | in.mac__hdr().encode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 1109 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1110 | if (in.mac__hdr().e() == false) { |
| 1111 | /* Add LI octets, if any */ |
| 1112 | if (!in.blocks().is_bound()) { |
| 1113 | ttcn_buffer.put_c(0x01); /* M=0, E=1 LEN=0 */ |
| 1114 | } else { |
| 1115 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1116 | #if 0 |
| 1117 | /* check for penultimate block */ |
| 1118 | if (i == in.blocks().size_of()-2) { |
| 1119 | /* if last block has no header, no more LI */ |
| 1120 | if (in.blocks()[i+1].hdr() == OMIT_VALUE) { |
| 1121 | in.blocks()[i].hdr()().more() = true; |
| 1122 | } else { |
| 1123 | /* header present, we have to encode LI */ |
| 1124 | in.blocks()[i].hdr()().more() = false; |
| 1125 | in.blocks()[i].hdr()().length__ind() = |
| 1126 | in.blocks()[i+1].payload().lengthof(); |
| 1127 | } |
| 1128 | } else if (i < in.blocks().size_of()-2) { |
| 1129 | /* one of the first blocks, before the penultimate or last */ |
| 1130 | in.blocks()[i].hdr()().e() = false; /* LI present */ |
| 1131 | /* re-compute length */ |
| 1132 | in.blocks()[i].hdr()().length__ind() = |
| 1133 | in.blocks()[i+1].payload().lengthof(); |
| 1134 | } |
| 1135 | /* Encode LI octet if E=0 */ |
| 1136 | } |
| 1137 | #endif |
| 1138 | if (in.blocks()[i].hdr() != OMIT_VALUE) { |
| 1139 | in.blocks()[i].hdr()().encode(LlcBlockHdr_descr_, ttcn_buffer, |
| 1140 | TTCN_EncDec::CT_RAW); |
| 1141 | } |
| 1142 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | if (in.mac__hdr().tlli__ind()) { |
Harald Welte | acc93ab | 2018-03-02 21:39:09 +0100 | [diff] [blame] | 1147 | ttcn_buffer.put_string(in.tlli()); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | if (in.mac__hdr().pfi__ind()) { |
| 1151 | in.pfi().encode(RlcmacUlDataBlock_pfi_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 1152 | } |
| 1153 | |
| 1154 | if (in.blocks().is_bound()) { |
| 1155 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1156 | if (!in.blocks()[i].is_bound()) |
| 1157 | continue; |
| 1158 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 1159 | } |
| 1160 | } |
| 1161 | |
Pau Espin Pedrol | cb00c52 | 2020-11-06 19:52:05 +0100 | [diff] [blame^] | 1162 | encode_trailing_padding_spb(ttcn_buffer, in.cs()); |
| 1163 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1164 | ttcn_buffer.get_string(ret_val); |
| 1165 | return ret_val; |
| 1166 | } |
| 1167 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1168 | static |
| 1169 | void enc__RlcmacUlEgprsDataHeader_type1(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1170 | { |
Pau Espin Pedrol | 081b168 | 2020-11-06 17:15:52 +0100 | [diff] [blame] | 1171 | struct gprs_rlc_ul_header_egprs_1 egprs1; |
| 1172 | |
| 1173 | egprs1.r = bs2uint8(si.r__ri()); |
| 1174 | egprs1.si = bs2uint8(si.foi__si()); |
| 1175 | egprs1.cv = si.countdown(); |
| 1176 | egprs1.tfi_hi = si.tfi() >> 0; |
| 1177 | egprs1.tfi_lo = si.tfi() >> 2; |
| 1178 | egprs1.bsn1_hi = si.bsn1() >> 0; |
| 1179 | egprs1.bsn1_lo = si.bsn1() >> 5; |
| 1180 | egprs1.bsn2_hi = si.bsn2__offset() >> 0; |
| 1181 | egprs1.bsn2_lo = si.bsn2__offset() >> 2; |
| 1182 | egprs1.cps = si.cps(); |
| 1183 | egprs1.rsb = bs2uint8(si.rsb()); |
| 1184 | egprs1.pi = si.pfi__ind(); |
| 1185 | egprs1.spare_hi = 0; |
| 1186 | egprs1.spare_lo = 0; |
| 1187 | egprs1.dummy = 0; |
| 1188 | |
| 1189 | ttcn_buffer.put_s(sizeof(egprs1), (const unsigned char *)&egprs1); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1190 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1191 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1192 | static |
| 1193 | void enc__RlcmacUlEgprsDataHeader_type2(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1194 | { |
Pau Espin Pedrol | 081b168 | 2020-11-06 17:15:52 +0100 | [diff] [blame] | 1195 | struct gprs_rlc_ul_header_egprs_2 egprs2; |
| 1196 | |
| 1197 | egprs2.r = bs2uint8(si.r__ri()); |
| 1198 | egprs2.si = bs2uint8(si.foi__si()); |
| 1199 | egprs2.cv = si.countdown(); |
| 1200 | egprs2.tfi_hi = si.tfi() >> 0; |
| 1201 | egprs2.tfi_lo = si.tfi() >> 2; |
| 1202 | egprs2.bsn1_hi = si.bsn1() >> 0; |
| 1203 | egprs2.bsn1_lo = si.bsn1() >> 5; |
| 1204 | egprs2.cps_hi = si.cps() >> 0; |
| 1205 | egprs2.cps_lo = si.cps() >> 2; |
| 1206 | egprs2.rsb = bs2uint8(si.rsb()); |
| 1207 | egprs2.pi = si.pfi__ind(); |
| 1208 | egprs2.spare_hi = 0; |
| 1209 | egprs2.spare_lo = 0; |
| 1210 | egprs2.dummy = 0; |
| 1211 | |
| 1212 | ttcn_buffer.put_s(sizeof(egprs2), (const unsigned char *)&egprs2); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1213 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1214 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1215 | static |
| 1216 | void enc__RlcmacUlEgprsDataHeader_type3(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1217 | { |
| 1218 | struct gprs_rlc_ul_header_egprs_3 egprs3; |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1219 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1220 | egprs3.r = bs2uint8(si.r__ri()); |
| 1221 | egprs3.si = bs2uint8(si.foi__si()); |
| 1222 | egprs3.cv = si.countdown(); |
| 1223 | egprs3.tfi_hi = si.tfi() >> 0; |
Pau Espin Pedrol | d3da797 | 2020-04-30 20:06:52 +0200 | [diff] [blame] | 1224 | egprs3.tfi_lo = si.tfi() >> 2; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1225 | egprs3.bsn1_hi = si.bsn1() >> 0; |
| 1226 | egprs3.bsn1_lo = si.bsn1() >> 5; |
| 1227 | egprs3.cps_hi = si.cps() >> 0; |
| 1228 | egprs3.cps_lo = si.cps() >> 2; |
| 1229 | egprs3.spb = bs2uint8(si.spb()); |
Pau Espin Pedrol | d3da797 | 2020-04-30 20:06:52 +0200 | [diff] [blame] | 1230 | egprs3.rsb = bs2uint8(si.rsb()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1231 | egprs3.pi = si.pfi__ind(); |
| 1232 | egprs3.spare = 0; |
| 1233 | egprs3.dummy = 0; |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1234 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1235 | ttcn_buffer.put_s(sizeof(egprs3), (const unsigned char *)&egprs3); |
| 1236 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1237 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1238 | OCTETSTRING enc__RlcmacUlEgprsDataBlock(const RlcmacUlEgprsDataBlock& si) |
| 1239 | { |
| 1240 | RlcmacUlEgprsDataBlock in = si; |
| 1241 | OCTETSTRING ret_val; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1242 | TTCN_Buffer ttcn_buffer, aligned_buffer; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1243 | int i; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1244 | unsigned int data_block_bits, data_block_offsets[2]; |
| 1245 | unsigned int num_calls; |
| 1246 | CodingScheme mcs; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1247 | |
| 1248 | mcs = RLCMAC__Templates::f__rlcmac__cps__htype__to__mcs(in.mac__hdr().cps(), in.mac__hdr().header__type()); |
| 1249 | //fprintf(stderr, "RLCMAC: infered MCS %s (%d)\n", mcs.enum_to_str(static_cast<CodingScheme::enum_type>(mcs.as_int())), mcs.as_int()); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1250 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1251 | if (!in.blocks().is_bound()) { |
| 1252 | /* we don't have nay blocks: Add length value (zero) */ |
| 1253 | in.e() = false; /* E=0: extension octet follows */ |
| 1254 | } else if (in.blocks().size_of() == 1 && in.blocks()[0].hdr() == OMIT_VALUE) { |
| 1255 | /* If there's only a single block, and that block has no HDR value defined, */ |
| 1256 | in.e() = true; /* E=0: extension octet follows */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1257 | } else { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1258 | /* Length value */ |
| 1259 | in.e() = false; |
| 1260 | } |
| 1261 | |
| 1262 | /* Fix other presence indications */ |
| 1263 | in.tlli__ind() = in.tlli().is_bound() && in.tlli() != OMIT_VALUE; |
| 1264 | in.mac__hdr().pfi__ind() = in.pfi().is_bound() && in.pfi() != OMIT_VALUE; |
| 1265 | |
| 1266 | switch (in.mac__hdr().header__type()) { |
| 1267 | case EgprsHeaderType::RLCMAC__HDR__TYPE__1: |
Pau Espin Pedrol | 82d22bc | 2020-04-30 20:09:12 +0200 | [diff] [blame] | 1268 | enc__RlcmacUlEgprsDataHeader_type1(in.mac__hdr(), ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1269 | break; |
| 1270 | case EgprsHeaderType::RLCMAC__HDR__TYPE__2: |
Pau Espin Pedrol | 82d22bc | 2020-04-30 20:09:12 +0200 | [diff] [blame] | 1271 | enc__RlcmacUlEgprsDataHeader_type2(in.mac__hdr(), ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1272 | break; |
| 1273 | case EgprsHeaderType::RLCMAC__HDR__TYPE__3: |
Pau Espin Pedrol | 82d22bc | 2020-04-30 20:09:12 +0200 | [diff] [blame] | 1274 | enc__RlcmacUlEgprsDataHeader_type3(in.mac__hdr(), ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1275 | default: |
| 1276 | break; /* TODO: error */ |
| 1277 | } |
| 1278 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1279 | /* Put first TI + E byte */ |
Pau Espin Pedrol | 563bcd6 | 2020-11-06 19:51:23 +0100 | [diff] [blame] | 1280 | aligned_buffer.put_c((in.tlli__ind() & 0x01) << 1 | (in.e() & 0x01) << 0); |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1281 | //printbuffer("After encoding first byte", aligned_buffer); |
| 1282 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1283 | if (in.e() == false) { |
| 1284 | /* Add LI octets, if any */ |
| 1285 | if (!in.blocks().is_bound()) { |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1286 | aligned_buffer.put_c(0x01); /* M=0, E=1 LEN=0 */ |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1287 | } else { |
| 1288 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1289 | #if 0 |
| 1290 | /* check for penultimate block */ |
| 1291 | if (i == in.blocks().size_of()-2) { |
| 1292 | /* if last block has no header, no more LI */ |
| 1293 | if (in.blocks()[i+1].hdr() == OMIT_VALUE) { |
| 1294 | in.blocks()[i].hdr()().more() = true; |
| 1295 | } else { |
| 1296 | /* header present, we have to encode LI */ |
| 1297 | in.blocks()[i].hdr()().more() = false; |
| 1298 | in.blocks()[i].hdr()().length__ind() = |
| 1299 | in.blocks()[i+1].payload().lengthof(); |
| 1300 | } |
| 1301 | } else if (i < in.blocks().size_of()-2) { |
| 1302 | /* one of the first blocks, before the penultimate or last */ |
| 1303 | in.blocks()[i].hdr()().e() = false; /* LI present */ |
| 1304 | /* re-compute length */ |
| 1305 | in.blocks()[i].hdr()().length__ind() = |
| 1306 | in.blocks()[i+1].payload().lengthof(); |
| 1307 | } |
| 1308 | /* Encode LI octet if E=0 */ |
| 1309 | } |
| 1310 | #endif |
| 1311 | if (in.blocks()[i].hdr() != OMIT_VALUE) { |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1312 | in.blocks()[i].hdr()().encode(EgprsLlcBlockHdr_descr_, aligned_buffer, |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1313 | TTCN_EncDec::CT_RAW); |
| 1314 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1319 | |
| 1320 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1321 | if (in.tlli__ind()) { |
Pau Espin Pedrol | a05b2bc | 2020-11-06 20:38:21 +0100 | [diff] [blame] | 1322 | /* The TLLI is encoded in little endian for EGPRS (see |
| 1323 | * TS 44.060, figure 10.3a.2.1, note 2) */ |
| 1324 | OCTETSTRING tlli = in.tlli(); |
| 1325 | aligned_buffer.put_c(tlli[3].get_octet()); |
| 1326 | aligned_buffer.put_c(tlli[2].get_octet()); |
| 1327 | aligned_buffer.put_c(tlli[1].get_octet()); |
| 1328 | aligned_buffer.put_c(tlli[0].get_octet()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1329 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1330 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1331 | if (in.mac__hdr().pfi__ind()) { |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1332 | in.pfi().encode(RlcmacUlEgprsDataBlock_pfi_descr_, aligned_buffer, TTCN_EncDec::CT_RAW); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1335 | //printbuffer("Before encoding EgprsLlc payload", aligned_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1336 | if (in.blocks().is_bound()) { |
| 1337 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1338 | if (!in.blocks()[i].is_bound()) |
| 1339 | continue; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1340 | aligned_buffer.put_string(in.blocks()[i].payload()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1341 | } |
| 1342 | } |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1343 | //printbuffer("After encoding EgprsLlc payload", aligned_buffer); |
| 1344 | |
| 1345 | setup_rlc_mac_priv(mcs, in.mac__hdr().header__type(), true, |
| 1346 | &num_calls, &data_block_bits, data_block_offsets); |
| 1347 | //printbuffer("before merging data block", ttcn_buffer); |
| 1348 | put_egprs_data_block(aligned_buffer, data_block_offsets[0], data_block_bits, ttcn_buffer); |
| 1349 | //printbuffer("after merging data block", ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1350 | |
Pau Espin Pedrol | cb00c52 | 2020-11-06 19:52:05 +0100 | [diff] [blame^] | 1351 | encode_trailing_padding_spb(ttcn_buffer, in.mcs()); |
| 1352 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1353 | ttcn_buffer.get_string(ret_val); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1354 | return ret_val; |
| 1355 | } |
| 1356 | |
Harald Welte | 78a1af6 | 2017-07-31 17:33:56 +0200 | [diff] [blame] | 1357 | OCTETSTRING enc__RlcmacUlBlock(const RlcmacUlBlock& si) |
| 1358 | { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1359 | if (si.ischosen(RlcmacUlBlock::ALT_data__egprs)) |
| 1360 | return enc__RlcmacUlEgprsDataBlock(si.data__egprs()); |
| 1361 | else if (si.ischosen(RlcmacUlBlock::ALT_data)) |
Harald Welte | 78a1af6 | 2017-07-31 17:33:56 +0200 | [diff] [blame] | 1362 | return enc__RlcmacUlDataBlock(si.data()); |
| 1363 | else |
| 1364 | return enc__RlcmacUlCtrlBlock(si.ctrl()); |
| 1365 | } |
| 1366 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1367 | } // namespace |