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 | |
| 441 | ///////////////////// |
| 442 | // DECODE |
| 443 | ///////////////////// |
| 444 | |
| 445 | /* DECODE DOWNLINK */ |
| 446 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 447 | RlcmacDlDataBlock dec__RlcmacDlDataBlock(const OCTETSTRING& stream) |
| 448 | { |
| 449 | RlcmacDlDataBlock ret_val; |
| 450 | TTCN_Buffer ttcn_buffer(stream); |
| 451 | int num_llc_blocks = 0; |
| 452 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 453 | ret_val.cs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 454 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 455 | /* use automatic/generated decoder for header */ |
| 456 | ret_val.mac__hdr().decode(DlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 457 | |
| 458 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 459 | if (ret_val.mac__hdr().hdr__ext().e() == false) { |
| 460 | /* extension octet follows, i.e. optional Llc length octets */ |
| 461 | while (1) { |
| 462 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 463 | LlcBlock lb; |
Harald Welte | 439e546 | 2018-03-08 23:21:17 +0100 | [diff] [blame] | 464 | lb.hdr()().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 465 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 466 | |
| 467 | /* if E == '1'B, we can proceed further */ |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 468 | if (lb.hdr()().e() == true) |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* RLC blocks at end */ |
| 474 | if (ret_val.mac__hdr().hdr__ext().e() == true) { |
| 475 | LlcBlock lb; |
| 476 | unsigned int length = ttcn_buffer.get_read_len(); |
| 477 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 478 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 479 | * data block */ |
| 480 | lb.payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 481 | ttcn_buffer.increase_pos(length); |
| 482 | ret_val.blocks()[0] = lb; |
| 483 | } else { |
| 484 | if (ret_val.blocks().is_bound()) { |
| 485 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 486 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 487 | if (length > ttcn_buffer.get_read_len()) |
| 488 | length = ttcn_buffer.get_read_len(); |
| 489 | ret_val.blocks()[i].payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 490 | ttcn_buffer.increase_pos(length); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return ret_val; |
| 496 | } |
| 497 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 498 | static |
| 499 | EgprsDlMacDataHeader dec__EgprsDlMacDataHeader_type1(const OCTETSTRING& stream) |
| 500 | { |
| 501 | EgprsDlMacDataHeader ret_val; |
| 502 | |
| 503 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 504 | |
| 505 | return ret_val; |
| 506 | } |
| 507 | |
| 508 | static |
| 509 | EgprsDlMacDataHeader dec__EgprsDlMacDataHeader_type2(const OCTETSTRING& stream) |
| 510 | { |
| 511 | EgprsDlMacDataHeader ret_val; |
| 512 | |
| 513 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 514 | |
| 515 | return ret_val; |
| 516 | } |
| 517 | |
| 518 | static |
| 519 | EgprsDlMacDataHeader dec__EgprsDlMacDataHeader_type3(const OCTETSTRING& stream) |
| 520 | { |
| 521 | TTCN_Buffer ttcn_buffer(stream); |
| 522 | EgprsDlMacDataHeader ret_val; |
| 523 | const struct gprs_rlc_dl_header_egprs_3 *egprs3; |
| 524 | uint8_t tmp; |
| 525 | |
| 526 | egprs3 = static_cast<const struct gprs_rlc_dl_header_egprs_3 *> |
| 527 | ((const void *)ttcn_buffer.get_data()); |
| 528 | |
| 529 | ret_val.header__type() = EgprsHeaderType::RLCMAC__HDR__TYPE__3; |
| 530 | ret_val.tfi() = egprs3->tfi_lo << 1 | egprs3->tfi_hi << 0; |
| 531 | ret_val.rrbp() = egprs3->rrbp; |
| 532 | tmp = egprs3->es_p; |
| 533 | ret_val.esp() = BITSTRING(2, &tmp); |
| 534 | ret_val.usf() = egprs3->usf; |
| 535 | ret_val.bsn1() = egprs3->bsn1_lo << 10 | egprs3->bsn1_mid << 2 | egprs3->bsn1_hi; |
| 536 | ret_val.bsn2__offset() = 0; /*TODO: mark optional and not set ? */ |
| 537 | ret_val.pr() = egprs3->pr; |
| 538 | ret_val.spb() = egprs3->spb; |
| 539 | ret_val.cps() = egprs3->cps; |
| 540 | |
| 541 | ttcn_buffer.increase_pos(sizeof(*egprs3)); |
| 542 | return ret_val; |
| 543 | } |
| 544 | |
| 545 | static |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 546 | RlcmacDlEgprsDataBlock dec__RlcmacDlEgprsDataBlock(const OCTETSTRING& stream) |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 547 | { |
| 548 | RlcmacDlEgprsDataBlock ret_val; |
| 549 | TTCN_Buffer ttcn_buffer(stream); |
| 550 | TTCN_Buffer aligned_buffer; |
| 551 | int num_llc_blocks = 0; |
| 552 | unsigned int data_block_bits, data_block_offsets[2]; |
| 553 | unsigned int num_calls; |
| 554 | const uint8_t *ti_e; |
| 555 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 556 | ret_val.mcs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 557 | switch (ret_val.mcs()) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 558 | case CodingScheme::MCS__0: |
| 559 | case CodingScheme::MCS__1: |
| 560 | case CodingScheme::MCS__2: |
| 561 | case CodingScheme::MCS__3: |
| 562 | case CodingScheme::MCS__4: |
| 563 | ret_val.mac__hdr() = dec__EgprsDlMacDataHeader_type3(stream); |
| 564 | break; |
| 565 | case CodingScheme::MCS__5: |
| 566 | case CodingScheme::MCS__6: |
| 567 | ret_val.mac__hdr() = dec__EgprsDlMacDataHeader_type2(stream); |
| 568 | break; |
| 569 | case CodingScheme::MCS__7: |
| 570 | case CodingScheme::MCS__8: |
| 571 | case CodingScheme::MCS__9: |
| 572 | ret_val.mac__hdr() = dec__EgprsDlMacDataHeader_type1(stream); |
| 573 | break; |
| 574 | } |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 575 | 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] | 576 | &num_calls, &data_block_bits, data_block_offsets); |
| 577 | get_egprs_data_block(ttcn_buffer, data_block_offsets[0], data_block_bits, aligned_buffer); |
| 578 | |
| 579 | ti_e = aligned_buffer.get_read_data(); |
| 580 | ret_val.fbi() = *ti_e & 0x02 ? true : false; |
| 581 | ret_val.e() = *ti_e & 0x01 ? true : false; |
| 582 | aligned_buffer.increase_pos(1); |
| 583 | |
| 584 | /* optional extension octets, containing LI+E of Llc blocks */ |
| 585 | if (ret_val.e() == false) { |
| 586 | /* extension octet follows, i.e. optional Llc length octets */ |
| 587 | while (1) { |
| 588 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 589 | EgprsLlcBlock lb; |
| 590 | lb.hdr()().decode(EgprsLlcBlockHdr_descr_, aligned_buffer, TTCN_EncDec::CT_RAW); |
| 591 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 592 | |
| 593 | /* if E == '1'B, we can proceed further */ |
| 594 | if (lb.hdr()().e() == true) |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* RLC blocks at end */ |
| 600 | if (ret_val.blocks().is_bound()) { |
| 601 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 602 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
| 603 | if (length > aligned_buffer.get_read_len()) |
| 604 | length = aligned_buffer.get_read_len(); |
| 605 | ret_val.blocks()[i].payload() = OCTETSTRING(length, aligned_buffer.get_read_data()); |
| 606 | aligned_buffer.increase_pos(length); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | return ret_val; |
| 611 | } |
| 612 | |
| 613 | RlcmacDlBlock dec__RlcmacDlBlock(const OCTETSTRING& stream) |
| 614 | { |
| 615 | RlcmacDlBlock ret_val; |
| 616 | size_t stream_len = stream.lengthof(); |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 617 | 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] | 618 | unsigned char pt; |
| 619 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 620 | switch (cs_mcs) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 621 | case CodingScheme::CS__1: |
| 622 | case CodingScheme::CS__2: |
| 623 | case CodingScheme::CS__3: |
| 624 | case CodingScheme::CS__4: |
| 625 | pt = stream[0].get_octet() >> 6; |
| 626 | if (pt == MacPayloadType::MAC__PT__RLC__DATA) |
| 627 | ret_val.data() = dec__RlcmacDlDataBlock(stream); |
| 628 | else |
| 629 | ret_val.ctrl() = dec__RlcmacDlCtrlBlock(stream); |
| 630 | break; |
| 631 | case CodingScheme::MCS__0: |
| 632 | case CodingScheme::MCS__1: |
| 633 | case CodingScheme::MCS__2: |
| 634 | case CodingScheme::MCS__3: |
| 635 | case CodingScheme::MCS__4: |
| 636 | case CodingScheme::MCS__5: |
| 637 | case CodingScheme::MCS__6: |
| 638 | case CodingScheme::MCS__7: |
| 639 | case CodingScheme::MCS__8: |
| 640 | case CodingScheme::MCS__9: |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 641 | ret_val.data__egprs() = dec__RlcmacDlEgprsDataBlock(stream); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 642 | break; |
| 643 | } |
| 644 | return ret_val; |
| 645 | } |
| 646 | |
| 647 | /* DECODE UPLINK */ |
| 648 | |
| 649 | RlcmacUlDataBlock dec__RlcmacUlDataBlock(const OCTETSTRING& stream) |
| 650 | { |
| 651 | RlcmacUlDataBlock ret_val; |
| 652 | TTCN_Buffer ttcn_buffer(stream); |
| 653 | int num_llc_blocks = 0; |
| 654 | |
| 655 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 656 | TTCN_Logger::log_event_str("==================================\n" |
| 657 | "dec_RlcmacUlDataBlock(): Stream before decoding: "); |
| 658 | stream.log(); |
| 659 | TTCN_Logger::end_event(); |
| 660 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 661 | ret_val.cs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 662 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 663 | /* use automatic/generated decoder for header */ |
| 664 | ret_val.mac__hdr().decode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 665 | |
| 666 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 667 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream after decoding hdr: "); |
| 668 | ttcn_buffer.log(); |
| 669 | TTCN_Logger::end_event(); |
| 670 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 671 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val after decoding hdr: "); |
| 672 | ret_val.log(); |
| 673 | TTCN_Logger::end_event(); |
| 674 | |
| 675 | /* Manually decoder remainder of ttcn_buffer, containing optional header octets, |
| 676 | * optional tlli, optional pfi and LLC Blocks */ |
| 677 | |
| 678 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 679 | if (ret_val.mac__hdr().e() == false) { |
| 680 | /* extension octet follows, i.e. optional Llc length octets */ |
| 681 | while (1) { |
| 682 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 683 | LlcBlock lb; |
| 684 | lb.hdr()().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 685 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 686 | |
| 687 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 688 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream after decoding ExtOct: "); |
| 689 | ttcn_buffer.log(); |
| 690 | TTCN_Logger::end_event(); |
| 691 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 692 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val after decoding ExtOct: "); |
| 693 | ret_val.log(); |
| 694 | TTCN_Logger::end_event(); |
| 695 | |
| 696 | /* if E == '1'B, we can proceed further */ |
| 697 | if (lb.hdr()().e() == true) |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* parse optional TLLI */ |
| 703 | if (ret_val.mac__hdr().tlli__ind()) { |
| 704 | ret_val.tlli() = OCTETSTRING(4, ttcn_buffer.get_read_data()); |
| 705 | ttcn_buffer.increase_pos(4); |
Pau Espin Pedrol | a2c5b6e | 2020-04-30 19:59:13 +0200 | [diff] [blame] | 706 | } else { |
| 707 | ret_val.tlli() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 708 | } |
| 709 | /* parse optional PFI */ |
| 710 | if (ret_val.mac__hdr().pfi__ind()) { |
| 711 | 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] | 712 | } else { |
| 713 | ret_val.pfi() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* RLC blocks at end */ |
| 717 | if (ret_val.mac__hdr().e() == true) { |
| 718 | LlcBlock lb; |
| 719 | unsigned int length = ttcn_buffer.get_read_len(); |
| 720 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 721 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 722 | * data block */ |
| 723 | lb.payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 724 | ttcn_buffer.increase_pos(length); |
| 725 | ret_val.blocks()[0] = lb; |
| 726 | } else { |
| 727 | if (ret_val.blocks().is_bound()) { |
| 728 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 729 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
| 730 | if (length > ttcn_buffer.get_read_len()) |
| 731 | length = ttcn_buffer.get_read_len(); |
| 732 | ret_val.blocks()[i].payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 733 | ttcn_buffer.increase_pos(length); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 739 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream before return: "); |
| 740 | ttcn_buffer.log(); |
| 741 | TTCN_Logger::end_event(); |
| 742 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 743 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val before return: "); |
| 744 | ret_val.log(); |
| 745 | TTCN_Logger::end_event(); |
| 746 | |
| 747 | return ret_val; |
| 748 | } |
| 749 | |
| 750 | static |
| 751 | EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type1(const OCTETSTRING& stream) |
| 752 | { |
| 753 | EgprsUlMacDataHeader ret_val; |
| 754 | |
| 755 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 756 | |
| 757 | return ret_val; |
| 758 | } |
| 759 | |
| 760 | static |
| 761 | EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type2(const OCTETSTRING& stream) |
| 762 | { |
| 763 | EgprsUlMacDataHeader ret_val; |
| 764 | |
| 765 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 766 | |
| 767 | return ret_val; |
| 768 | } |
| 769 | |
| 770 | static |
| 771 | EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type3(const OCTETSTRING& stream) |
| 772 | { |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 773 | TTCN_Buffer ttcn_buffer(stream); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 774 | EgprsUlMacDataHeader ret_val; |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 775 | const struct gprs_rlc_ul_header_egprs_3 *egprs3; |
| 776 | uint8_t tmp; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 777 | |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 778 | egprs3 = static_cast<const struct gprs_rlc_ul_header_egprs_3 *> |
| 779 | ((const void *)ttcn_buffer.get_data()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 780 | |
Pau Espin Pedrol | 0b8e22b | 2020-04-30 20:10:17 +0200 | [diff] [blame] | 781 | ret_val.header__type() = EgprsHeaderType::RLCMAC__HDR__TYPE__3; |
| 782 | ret_val.tfi() = egprs3->tfi_lo << 2 | egprs3->tfi_hi << 0; |
| 783 | ret_val.countdown() = egprs3->cv; |
| 784 | tmp = egprs3->si; |
| 785 | ret_val.foi__si() = BITSTRING(1, &tmp); |
| 786 | tmp = egprs3->r; |
| 787 | ret_val.r__ri() = BITSTRING(1, &tmp); |
| 788 | ret_val.bsn1() = egprs3->bsn1_lo << 5 | egprs3->bsn1_hi << 0; |
| 789 | ret_val.cps() = egprs3->cps_lo << 2 | egprs3->cps_hi << 0; |
| 790 | ret_val.pfi__ind() = egprs3->pi; |
| 791 | tmp = egprs3->rsb; |
| 792 | ret_val.rsb() = BITSTRING(1, &tmp); |
| 793 | tmp = egprs3->spb; |
| 794 | ret_val.spb() = BITSTRING(2, &tmp); |
| 795 | |
| 796 | ttcn_buffer.increase_pos(sizeof(*egprs3)); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 797 | return ret_val; |
| 798 | } |
| 799 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 800 | RlcmacUlEgprsDataBlock dec__RlcmacUlEgprsDataBlock(const OCTETSTRING& stream) |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 801 | { |
| 802 | RlcmacUlEgprsDataBlock ret_val; |
| 803 | TTCN_Buffer ttcn_buffer(stream); |
| 804 | TTCN_Buffer aligned_buffer; |
| 805 | int num_llc_blocks = 0; |
| 806 | unsigned int data_block_bits, data_block_offsets[2]; |
| 807 | unsigned int num_calls; |
| 808 | const uint8_t *ti_e; |
| 809 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 810 | ret_val.mcs() = payload_len_2_coding_scheme(stream.lengthof()); |
| 811 | switch (ret_val.mcs()) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 812 | case CodingScheme::MCS__1: |
| 813 | case CodingScheme::MCS__2: |
| 814 | case CodingScheme::MCS__3: |
| 815 | case CodingScheme::MCS__4: |
| 816 | ret_val.mac__hdr() = dec__EgprsUlMacDataHeader_type3(stream); |
| 817 | break; |
| 818 | case CodingScheme::MCS__5: |
| 819 | case CodingScheme::MCS__6: |
| 820 | ret_val.mac__hdr() = dec__EgprsUlMacDataHeader_type2(stream); |
| 821 | break; |
| 822 | case CodingScheme::MCS__7: |
| 823 | case CodingScheme::MCS__8: |
| 824 | case CodingScheme::MCS__9: |
| 825 | ret_val.mac__hdr() = dec__EgprsUlMacDataHeader_type1(stream); |
| 826 | break; |
| 827 | } |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 828 | 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] | 829 | &num_calls, &data_block_bits, data_block_offsets); |
| 830 | get_egprs_data_block(ttcn_buffer, data_block_offsets[0], data_block_bits, aligned_buffer); |
| 831 | |
| 832 | ti_e = aligned_buffer.get_read_data(); |
| 833 | ret_val.tlli__ind() = *ti_e & 0x02 ? true : false; |
| 834 | ret_val.e() = *ti_e & 0x01 ? true : false; |
| 835 | aligned_buffer.increase_pos(1); |
| 836 | |
| 837 | /* Manually decoder remainder of aligned_buffer, containing optional header octets, |
| 838 | * optional tlli, optional pfi and LLC Blocks */ |
| 839 | |
| 840 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 841 | if (ret_val.e() == false) { |
| 842 | /* extension octet follows, i.e. optional Llc length octets */ |
| 843 | while (1) { |
| 844 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 845 | EgprsLlcBlock lb; |
| 846 | lb.hdr()().decode(EgprsLlcBlockHdr_descr_, aligned_buffer, TTCN_EncDec::CT_RAW); |
| 847 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 848 | |
| 849 | /* if E == '1'B, we can proceed further */ |
| 850 | if (lb.hdr()().e() == true) |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* parse optional TLLI */ |
| 856 | if (ret_val.tlli__ind()) { |
| 857 | ret_val.tlli() = OCTETSTRING(4, aligned_buffer.get_read_data()); |
| 858 | aligned_buffer.increase_pos(4); |
Pau Espin Pedrol | a2c5b6e | 2020-04-30 19:59:13 +0200 | [diff] [blame] | 859 | } else { |
| 860 | ret_val.tlli() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 861 | } |
| 862 | /* parse optional PFI */ |
| 863 | if (ret_val.mac__hdr().pfi__ind()) { |
Pau Espin Pedrol | 331f525 | 2020-04-30 20:05:29 +0200 | [diff] [blame] | 864 | 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] | 865 | } else { |
| 866 | ret_val.pfi() = OMIT_VALUE; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* RLC blocks at end */ |
| 870 | if (ret_val.e() == true) { |
| 871 | EgprsLlcBlock lb; |
| 872 | unsigned int length = aligned_buffer.get_read_len(); |
| 873 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 874 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 875 | * data block */ |
| 876 | lb.payload() = OCTETSTRING(length, aligned_buffer.get_read_data()); |
| 877 | aligned_buffer.increase_pos(length); |
| 878 | ret_val.blocks()[0] = lb; |
| 879 | } else { |
| 880 | if (ret_val.blocks().is_bound()) { |
| 881 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 882 | unsigned int length = ret_val.blocks()[i].hdr()().length__ind(); |
| 883 | if (length > aligned_buffer.get_read_len()) |
| 884 | length = aligned_buffer.get_read_len(); |
| 885 | ret_val.blocks()[i].payload() = OCTETSTRING(length, aligned_buffer.get_read_data()); |
| 886 | aligned_buffer.increase_pos(length); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | return ret_val; |
| 892 | } |
| 893 | |
| 894 | RlcmacUlBlock dec__RlcmacUlBlock(const OCTETSTRING& stream) |
| 895 | { |
| 896 | RlcmacUlBlock ret_val; |
| 897 | size_t stream_len = stream.lengthof(); |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 898 | 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] | 899 | unsigned char pt; |
| 900 | |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 901 | switch (cs_mcs) { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 902 | case CodingScheme::CS__1: |
| 903 | case CodingScheme::CS__2: |
| 904 | case CodingScheme::CS__3: |
| 905 | case CodingScheme::CS__4: |
| 906 | pt = stream[0].get_octet() >> 6; |
| 907 | if (pt == MacPayloadType::MAC__PT__RLC__DATA) |
| 908 | ret_val.data() = dec__RlcmacUlDataBlock(stream); |
| 909 | else |
| 910 | ret_val.ctrl() = dec__RlcmacUlCtrlBlock(stream); |
| 911 | break; |
| 912 | case CodingScheme::MCS__1: |
| 913 | case CodingScheme::MCS__2: |
| 914 | case CodingScheme::MCS__3: |
| 915 | case CodingScheme::MCS__4: |
| 916 | case CodingScheme::MCS__5: |
| 917 | case CodingScheme::MCS__6: |
| 918 | case CodingScheme::MCS__7: |
| 919 | case CodingScheme::MCS__8: |
| 920 | case CodingScheme::MCS__9: |
Pau Espin Pedrol | 5abfded | 2020-11-03 17:30:44 +0100 | [diff] [blame] | 921 | ret_val.data__egprs() = dec__RlcmacUlEgprsDataBlock(stream); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 922 | break; |
| 923 | } |
| 924 | |
| 925 | return ret_val; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | ///////////////////// |
| 930 | // ENCODE |
| 931 | ///////////////////// |
| 932 | |
| 933 | /* ENCODE DOWNLINK */ |
| 934 | |
| 935 | OCTETSTRING enc__RlcmacDlDataBlock(const RlcmacDlDataBlock& si) |
| 936 | { |
| 937 | RlcmacDlDataBlock in = si; |
| 938 | OCTETSTRING ret_val; |
| 939 | TTCN_Buffer ttcn_buffer; |
| 940 | int i; |
| 941 | |
| 942 | /* Fix 'e' bit of initial header based on following blocks */ |
| 943 | if (!in.blocks().is_bound() || |
| 944 | (in.blocks().size_of() == 1 && !in.blocks()[0].hdr().is_bound())) |
| 945 | in.mac__hdr().hdr__ext().e() = true; |
| 946 | else |
| 947 | in.mac__hdr().hdr__ext().e() = false; |
| 948 | |
| 949 | /* use automatic/generated decoder for header */ |
| 950 | in.mac__hdr().encode(DlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 951 | |
| 952 | /* Add LI octets, if any */ |
| 953 | if (in.blocks().is_bound() && |
| 954 | (in.blocks().size_of() != 1 || in.blocks()[0].hdr().is_bound())) { |
| 955 | /* first write LI octets */ |
| 956 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 957 | /* fix the 'E' bit in case it is not clear */ |
| 958 | if (i < in.blocks().size_of()-1) |
| 959 | in.blocks()[i].hdr()().e() = false; |
| 960 | else |
| 961 | in.blocks()[i].hdr()().e() = true; |
| 962 | in.blocks()[i].hdr()().encode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 963 | } |
| 964 | } |
| 965 | if (in.blocks().is_bound()) { |
| 966 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 967 | if (!in.blocks()[i].is_bound()) |
| 968 | continue; |
| 969 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | ttcn_buffer.get_string(ret_val); |
| 974 | return ret_val; |
| 975 | } |
| 976 | |
| 977 | static |
| 978 | void enc__RlcmacDlEgprsDataHeader_type1(const EgprsDlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 979 | { |
| 980 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 981 | } |
| 982 | |
| 983 | static |
| 984 | void enc__RlcmacDlEgprsDataHeader_type2(const EgprsDlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 985 | { |
| 986 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 987 | } |
| 988 | |
| 989 | static |
| 990 | void enc__RlcmacDlEgprsDataHeader_type3(const EgprsDlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 991 | { |
| 992 | fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__); |
| 993 | } |
| 994 | |
| 995 | OCTETSTRING enc__RlcmacDlEgprsDataBlock(const RlcmacDlEgprsDataBlock& si) |
| 996 | { |
| 997 | RlcmacDlEgprsDataBlock in = si; |
| 998 | OCTETSTRING ret_val; |
| 999 | TTCN_Buffer ttcn_buffer; |
| 1000 | int i; |
| 1001 | |
| 1002 | /* Fix 'e' bit of initial header based on following blocks */ |
| 1003 | if (!in.blocks().is_bound() || |
| 1004 | (in.blocks().size_of() == 1 && !in.blocks()[0].hdr().is_bound())) |
| 1005 | in.e() = true; |
| 1006 | else |
| 1007 | in.e() = false; |
| 1008 | |
| 1009 | switch (in.mac__hdr().header__type()) { |
| 1010 | case EgprsHeaderType::RLCMAC__HDR__TYPE__1: |
| 1011 | enc__RlcmacDlEgprsDataHeader_type1(si.mac__hdr(), ttcn_buffer); |
| 1012 | break; |
| 1013 | case EgprsHeaderType::RLCMAC__HDR__TYPE__2: |
| 1014 | enc__RlcmacDlEgprsDataHeader_type2(si.mac__hdr(), ttcn_buffer); |
| 1015 | break; |
| 1016 | case EgprsHeaderType::RLCMAC__HDR__TYPE__3: |
| 1017 | enc__RlcmacDlEgprsDataHeader_type3(si.mac__hdr(), ttcn_buffer); |
| 1018 | default: |
| 1019 | break; /* TODO: error */ |
| 1020 | } |
| 1021 | |
| 1022 | /* Add LI octets, if any */ |
| 1023 | if (in.blocks().is_bound() && |
| 1024 | (in.blocks().size_of() != 1 || in.blocks()[0].hdr().is_bound())) { |
| 1025 | /* first write LI octets */ |
| 1026 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1027 | /* fix the 'E' bit in case it is not clear */ |
| 1028 | if (i < in.blocks().size_of()-1) |
| 1029 | in.blocks()[i].hdr()().e() = false; |
| 1030 | else |
| 1031 | in.blocks()[i].hdr()().e() = true; |
| 1032 | in.blocks()[i].hdr()().encode(EgprsLlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 1033 | } |
| 1034 | } |
| 1035 | if (in.blocks().is_bound()) { |
| 1036 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1037 | if (!in.blocks()[i].is_bound()) |
| 1038 | continue; |
| 1039 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | ttcn_buffer.get_string(ret_val); |
| 1044 | return ret_val; |
| 1045 | } |
| 1046 | |
| 1047 | OCTETSTRING enc__RlcmacDlBlock(const RlcmacDlBlock& si) |
| 1048 | { |
| 1049 | if (si.ischosen(RlcmacDlBlock::ALT_data__egprs)) |
| 1050 | return enc__RlcmacDlEgprsDataBlock(si.data__egprs()); |
| 1051 | else if (si.ischosen(RlcmacDlBlock::ALT_data)) |
| 1052 | return enc__RlcmacDlDataBlock(si.data()); |
| 1053 | else |
| 1054 | return enc__RlcmacDlCtrlBlock(si.ctrl()); |
| 1055 | } |
| 1056 | |
| 1057 | /* ENCODE UPLINK */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1058 | |
| 1059 | OCTETSTRING enc__RlcmacUlDataBlock(const RlcmacUlDataBlock& si) |
| 1060 | { |
| 1061 | RlcmacUlDataBlock in = si; |
| 1062 | OCTETSTRING ret_val; |
| 1063 | TTCN_Buffer ttcn_buffer; |
| 1064 | int i; |
| 1065 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1066 | if (!in.blocks().is_bound()) { |
Pau Espin Pedrol | ecaeb89 | 2020-04-30 19:58:15 +0200 | [diff] [blame] | 1067 | /* we don't have any blocks: Add length value (zero) */ |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1068 | in.mac__hdr().e() = false; /* E=0: extension octet follows */ |
| 1069 | } else if (in.blocks().size_of() == 1 && in.blocks()[0].hdr() == OMIT_VALUE) { |
| 1070 | /* If there's only a single block, and that block has no HDR value defined, */ |
| 1071 | in.mac__hdr().e() = true; /* E=0: extension octet follows */ |
| 1072 | } else { |
| 1073 | /* Length value */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1074 | in.mac__hdr().e() = false; |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1075 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1076 | |
| 1077 | /* Fix other presence indications */ |
Harald Welte | 2072ab6 | 2017-07-31 18:33:35 +0200 | [diff] [blame] | 1078 | in.mac__hdr().tlli__ind() = in.tlli().is_bound() && in.tlli() != OMIT_VALUE; |
| 1079 | 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] | 1080 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1081 | /* use automatic/generated encoder for header */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1082 | in.mac__hdr().encode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 1083 | |
Harald Welte | 060e27a | 2018-03-03 20:38:19 +0100 | [diff] [blame] | 1084 | if (in.mac__hdr().e() == false) { |
| 1085 | /* Add LI octets, if any */ |
| 1086 | if (!in.blocks().is_bound()) { |
| 1087 | ttcn_buffer.put_c(0x01); /* M=0, E=1 LEN=0 */ |
| 1088 | } else { |
| 1089 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1090 | #if 0 |
| 1091 | /* check for penultimate block */ |
| 1092 | if (i == in.blocks().size_of()-2) { |
| 1093 | /* if last block has no header, no more LI */ |
| 1094 | if (in.blocks()[i+1].hdr() == OMIT_VALUE) { |
| 1095 | in.blocks()[i].hdr()().more() = true; |
| 1096 | } else { |
| 1097 | /* header present, we have to encode LI */ |
| 1098 | in.blocks()[i].hdr()().more() = false; |
| 1099 | in.blocks()[i].hdr()().length__ind() = |
| 1100 | in.blocks()[i+1].payload().lengthof(); |
| 1101 | } |
| 1102 | } else if (i < in.blocks().size_of()-2) { |
| 1103 | /* one of the first blocks, before the penultimate or last */ |
| 1104 | in.blocks()[i].hdr()().e() = false; /* LI present */ |
| 1105 | /* re-compute length */ |
| 1106 | in.blocks()[i].hdr()().length__ind() = |
| 1107 | in.blocks()[i+1].payload().lengthof(); |
| 1108 | } |
| 1109 | /* Encode LI octet if E=0 */ |
| 1110 | } |
| 1111 | #endif |
| 1112 | if (in.blocks()[i].hdr() != OMIT_VALUE) { |
| 1113 | in.blocks()[i].hdr()().encode(LlcBlockHdr_descr_, ttcn_buffer, |
| 1114 | TTCN_EncDec::CT_RAW); |
| 1115 | } |
| 1116 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | if (in.mac__hdr().tlli__ind()) { |
Harald Welte | acc93ab | 2018-03-02 21:39:09 +0100 | [diff] [blame] | 1121 | ttcn_buffer.put_string(in.tlli()); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | if (in.mac__hdr().pfi__ind()) { |
| 1125 | in.pfi().encode(RlcmacUlDataBlock_pfi_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 1126 | } |
| 1127 | |
| 1128 | if (in.blocks().is_bound()) { |
| 1129 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1130 | if (!in.blocks()[i].is_bound()) |
| 1131 | continue; |
| 1132 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | ttcn_buffer.get_string(ret_val); |
| 1137 | return ret_val; |
| 1138 | } |
| 1139 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1140 | static |
| 1141 | void enc__RlcmacUlEgprsDataHeader_type1(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1142 | { |
Pau Espin Pedrol | 081b168 | 2020-11-06 17:15:52 +0100 | [diff] [blame] | 1143 | struct gprs_rlc_ul_header_egprs_1 egprs1; |
| 1144 | |
| 1145 | egprs1.r = bs2uint8(si.r__ri()); |
| 1146 | egprs1.si = bs2uint8(si.foi__si()); |
| 1147 | egprs1.cv = si.countdown(); |
| 1148 | egprs1.tfi_hi = si.tfi() >> 0; |
| 1149 | egprs1.tfi_lo = si.tfi() >> 2; |
| 1150 | egprs1.bsn1_hi = si.bsn1() >> 0; |
| 1151 | egprs1.bsn1_lo = si.bsn1() >> 5; |
| 1152 | egprs1.bsn2_hi = si.bsn2__offset() >> 0; |
| 1153 | egprs1.bsn2_lo = si.bsn2__offset() >> 2; |
| 1154 | egprs1.cps = si.cps(); |
| 1155 | egprs1.rsb = bs2uint8(si.rsb()); |
| 1156 | egprs1.pi = si.pfi__ind(); |
| 1157 | egprs1.spare_hi = 0; |
| 1158 | egprs1.spare_lo = 0; |
| 1159 | egprs1.dummy = 0; |
| 1160 | |
| 1161 | ttcn_buffer.put_s(sizeof(egprs1), (const unsigned char *)&egprs1); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1162 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1163 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1164 | static |
| 1165 | void enc__RlcmacUlEgprsDataHeader_type2(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1166 | { |
Pau Espin Pedrol | 081b168 | 2020-11-06 17:15:52 +0100 | [diff] [blame] | 1167 | struct gprs_rlc_ul_header_egprs_2 egprs2; |
| 1168 | |
| 1169 | egprs2.r = bs2uint8(si.r__ri()); |
| 1170 | egprs2.si = bs2uint8(si.foi__si()); |
| 1171 | egprs2.cv = si.countdown(); |
| 1172 | egprs2.tfi_hi = si.tfi() >> 0; |
| 1173 | egprs2.tfi_lo = si.tfi() >> 2; |
| 1174 | egprs2.bsn1_hi = si.bsn1() >> 0; |
| 1175 | egprs2.bsn1_lo = si.bsn1() >> 5; |
| 1176 | egprs2.cps_hi = si.cps() >> 0; |
| 1177 | egprs2.cps_lo = si.cps() >> 2; |
| 1178 | egprs2.rsb = bs2uint8(si.rsb()); |
| 1179 | egprs2.pi = si.pfi__ind(); |
| 1180 | egprs2.spare_hi = 0; |
| 1181 | egprs2.spare_lo = 0; |
| 1182 | egprs2.dummy = 0; |
| 1183 | |
| 1184 | ttcn_buffer.put_s(sizeof(egprs2), (const unsigned char *)&egprs2); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1185 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1186 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1187 | static |
| 1188 | void enc__RlcmacUlEgprsDataHeader_type3(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer) |
| 1189 | { |
| 1190 | struct gprs_rlc_ul_header_egprs_3 egprs3; |
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 | egprs3.r = bs2uint8(si.r__ri()); |
| 1193 | egprs3.si = bs2uint8(si.foi__si()); |
| 1194 | egprs3.cv = si.countdown(); |
| 1195 | egprs3.tfi_hi = si.tfi() >> 0; |
Pau Espin Pedrol | d3da797 | 2020-04-30 20:06:52 +0200 | [diff] [blame] | 1196 | egprs3.tfi_lo = si.tfi() >> 2; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1197 | egprs3.bsn1_hi = si.bsn1() >> 0; |
| 1198 | egprs3.bsn1_lo = si.bsn1() >> 5; |
| 1199 | egprs3.cps_hi = si.cps() >> 0; |
| 1200 | egprs3.cps_lo = si.cps() >> 2; |
| 1201 | egprs3.spb = bs2uint8(si.spb()); |
Pau Espin Pedrol | d3da797 | 2020-04-30 20:06:52 +0200 | [diff] [blame] | 1202 | egprs3.rsb = bs2uint8(si.rsb()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1203 | egprs3.pi = si.pfi__ind(); |
| 1204 | egprs3.spare = 0; |
| 1205 | egprs3.dummy = 0; |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1206 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1207 | ttcn_buffer.put_s(sizeof(egprs3), (const unsigned char *)&egprs3); |
| 1208 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1209 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1210 | OCTETSTRING enc__RlcmacUlEgprsDataBlock(const RlcmacUlEgprsDataBlock& si) |
| 1211 | { |
| 1212 | RlcmacUlEgprsDataBlock in = si; |
| 1213 | OCTETSTRING ret_val; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1214 | TTCN_Buffer ttcn_buffer, aligned_buffer; |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1215 | int i; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1216 | unsigned int data_block_bits, data_block_offsets[2]; |
| 1217 | unsigned int num_calls; |
| 1218 | CodingScheme mcs; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1219 | |
| 1220 | mcs = RLCMAC__Templates::f__rlcmac__cps__htype__to__mcs(in.mac__hdr().cps(), in.mac__hdr().header__type()); |
| 1221 | //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] | 1222 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1223 | if (!in.blocks().is_bound()) { |
| 1224 | /* we don't have nay blocks: Add length value (zero) */ |
| 1225 | in.e() = false; /* E=0: extension octet follows */ |
| 1226 | } else if (in.blocks().size_of() == 1 && in.blocks()[0].hdr() == OMIT_VALUE) { |
| 1227 | /* If there's only a single block, and that block has no HDR value defined, */ |
| 1228 | in.e() = true; /* E=0: extension octet follows */ |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1229 | } else { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1230 | /* Length value */ |
| 1231 | in.e() = false; |
| 1232 | } |
| 1233 | |
| 1234 | /* Fix other presence indications */ |
| 1235 | in.tlli__ind() = in.tlli().is_bound() && in.tlli() != OMIT_VALUE; |
| 1236 | in.mac__hdr().pfi__ind() = in.pfi().is_bound() && in.pfi() != OMIT_VALUE; |
| 1237 | |
| 1238 | switch (in.mac__hdr().header__type()) { |
| 1239 | case EgprsHeaderType::RLCMAC__HDR__TYPE__1: |
Pau Espin Pedrol | 82d22bc | 2020-04-30 20:09:12 +0200 | [diff] [blame] | 1240 | enc__RlcmacUlEgprsDataHeader_type1(in.mac__hdr(), ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1241 | break; |
| 1242 | case EgprsHeaderType::RLCMAC__HDR__TYPE__2: |
Pau Espin Pedrol | 82d22bc | 2020-04-30 20:09:12 +0200 | [diff] [blame] | 1243 | enc__RlcmacUlEgprsDataHeader_type2(in.mac__hdr(), ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1244 | break; |
| 1245 | case EgprsHeaderType::RLCMAC__HDR__TYPE__3: |
Pau Espin Pedrol | 82d22bc | 2020-04-30 20:09:12 +0200 | [diff] [blame] | 1246 | enc__RlcmacUlEgprsDataHeader_type3(in.mac__hdr(), ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1247 | default: |
| 1248 | break; /* TODO: error */ |
| 1249 | } |
| 1250 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1251 | /* Put first TI + E byte */ |
Pau Espin Pedrol | 563bcd6 | 2020-11-06 19:51:23 +0100 | [diff] [blame] | 1252 | 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] | 1253 | //printbuffer("After encoding first byte", aligned_buffer); |
| 1254 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1255 | if (in.e() == false) { |
| 1256 | /* Add LI octets, if any */ |
| 1257 | if (!in.blocks().is_bound()) { |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1258 | aligned_buffer.put_c(0x01); /* M=0, E=1 LEN=0 */ |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1259 | } else { |
| 1260 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1261 | #if 0 |
| 1262 | /* check for penultimate block */ |
| 1263 | if (i == in.blocks().size_of()-2) { |
| 1264 | /* if last block has no header, no more LI */ |
| 1265 | if (in.blocks()[i+1].hdr() == OMIT_VALUE) { |
| 1266 | in.blocks()[i].hdr()().more() = true; |
| 1267 | } else { |
| 1268 | /* header present, we have to encode LI */ |
| 1269 | in.blocks()[i].hdr()().more() = false; |
| 1270 | in.blocks()[i].hdr()().length__ind() = |
| 1271 | in.blocks()[i+1].payload().lengthof(); |
| 1272 | } |
| 1273 | } else if (i < in.blocks().size_of()-2) { |
| 1274 | /* one of the first blocks, before the penultimate or last */ |
| 1275 | in.blocks()[i].hdr()().e() = false; /* LI present */ |
| 1276 | /* re-compute length */ |
| 1277 | in.blocks()[i].hdr()().length__ind() = |
| 1278 | in.blocks()[i+1].payload().lengthof(); |
| 1279 | } |
| 1280 | /* Encode LI octet if E=0 */ |
| 1281 | } |
| 1282 | #endif |
| 1283 | if (in.blocks()[i].hdr() != OMIT_VALUE) { |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1284 | in.blocks()[i].hdr()().encode(EgprsLlcBlockHdr_descr_, aligned_buffer, |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1285 | TTCN_EncDec::CT_RAW); |
| 1286 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1291 | |
| 1292 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1293 | if (in.tlli__ind()) { |
Pau Espin Pedrol | a05b2bc | 2020-11-06 20:38:21 +0100 | [diff] [blame] | 1294 | /* The TLLI is encoded in little endian for EGPRS (see |
| 1295 | * TS 44.060, figure 10.3a.2.1, note 2) */ |
| 1296 | OCTETSTRING tlli = in.tlli(); |
| 1297 | aligned_buffer.put_c(tlli[3].get_octet()); |
| 1298 | aligned_buffer.put_c(tlli[2].get_octet()); |
| 1299 | aligned_buffer.put_c(tlli[1].get_octet()); |
| 1300 | aligned_buffer.put_c(tlli[0].get_octet()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1301 | } |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1302 | |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1303 | if (in.mac__hdr().pfi__ind()) { |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1304 | 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] | 1305 | } |
| 1306 | |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1307 | //printbuffer("Before encoding EgprsLlc payload", aligned_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1308 | if (in.blocks().is_bound()) { |
| 1309 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 1310 | if (!in.blocks()[i].is_bound()) |
| 1311 | continue; |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1312 | aligned_buffer.put_string(in.blocks()[i].payload()); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1313 | } |
| 1314 | } |
Pau Espin Pedrol | 2456dad | 2020-04-30 20:22:38 +0200 | [diff] [blame] | 1315 | //printbuffer("After encoding EgprsLlc payload", aligned_buffer); |
| 1316 | |
| 1317 | setup_rlc_mac_priv(mcs, in.mac__hdr().header__type(), true, |
| 1318 | &num_calls, &data_block_bits, data_block_offsets); |
| 1319 | //printbuffer("before merging data block", ttcn_buffer); |
| 1320 | put_egprs_data_block(aligned_buffer, data_block_offsets[0], data_block_bits, ttcn_buffer); |
| 1321 | //printbuffer("after merging data block", ttcn_buffer); |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1322 | |
| 1323 | ttcn_buffer.get_string(ret_val); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1324 | return ret_val; |
| 1325 | } |
| 1326 | |
Harald Welte | 78a1af6 | 2017-07-31 17:33:56 +0200 | [diff] [blame] | 1327 | OCTETSTRING enc__RlcmacUlBlock(const RlcmacUlBlock& si) |
| 1328 | { |
Pau Espin Pedrol | 372af7a | 2020-04-27 17:32:01 +0200 | [diff] [blame] | 1329 | if (si.ischosen(RlcmacUlBlock::ALT_data__egprs)) |
| 1330 | return enc__RlcmacUlEgprsDataBlock(si.data__egprs()); |
| 1331 | else if (si.ischosen(RlcmacUlBlock::ALT_data)) |
Harald Welte | 78a1af6 | 2017-07-31 17:33:56 +0200 | [diff] [blame] | 1332 | return enc__RlcmacUlDataBlock(si.data()); |
| 1333 | else |
| 1334 | return enc__RlcmacUlCtrlBlock(si.ctrl()); |
| 1335 | } |
| 1336 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1337 | } // namespace |