blob: 6b7806475ea7561ecf7136926c6645153ff550fc [file] [log] [blame]
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001#include <stdint.h>
2#include <endian.h>
3
Harald Welte43e060a2017-07-30 22:38:03 +02004#include "RLCMAC_Types.hh"
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02005#include "RLCMAC_Templates.hh"
Harald Welte43e060a2017-07-30 22:38:03 +02006#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 Welte34b5a952019-05-27 11:54:11 +020011 * 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 Welte43e060a2017-07-30 22:38:03 +020017 */
18
19namespace RLCMAC__Types {
20
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +020021/////////////////////
22// INTENRAL HELPERS
23/////////////////////
Harald Welte43e060a2017-07-30 22:38:03 +020024
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +020025/* TS 04.60 10.3a.4.1.1 */
26struct 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 Welte43e060a2017-07-30 22:38:03 +020053
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +020054/* TS 04.60 10.3a.4.2.1 */
55struct 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 Welte43e060a2017-07-30 22:38:03 +020080
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +020081/* TS 04.60 10.3a.4.3.1 */
82struct 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
107struct 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
131struct 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
153struct 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 Pedrol2456dad2020-04-30 20:22:38 +0200176/*
177static const char hex_chars[] = "0123456789abcdef";
178void 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 Pedrol372af7a2020-04-27 17:32:01 +0200192static 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 Welte43e060a2017-07-30 22:38:03 +0200222 }
Harald Welte43e060a2017-07-30 22:38:03 +0200223}
224
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200225static 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
252static 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 */
265static void
266setup_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 Pedrolecaeb892020-04-30 19:58:15 +0200299 * and store the result to caller-allocated 'buffer'. The shifting is
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200300 * done lsb-first. */
301static 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 Pedrol2456dad2020-04-30 20:22:38 +0200314 //fprintf(stderr, "RLMAC: clone: hdr_bytes=%u extra_bits=%u (length_bytes=%u)\n", hdr_bytes, extra_bits, length_bytes);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200315
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 */
335static 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 Pedrol2456dad2020-04-30 20:22:38 +0200344 //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 Pedrol372af7a2020-04-27 17:32:01 +0200345 dst_ttcn_buffer.get_end(aligned_buf, accepted_len);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200346 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 Pedrol372af7a2020-04-27 17:32:01 +0200356 /* 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 Pedrol2456dad2020-04-30 20:22:38 +0200362/* 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. */
365static 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 */
407static 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 Pedrol372af7a2020-04-27 17:32:01 +0200440
441/////////////////////
442// DECODE
443/////////////////////
444
445/* DECODE DOWNLINK */
446
Harald Welte43e060a2017-07-30 22:38:03 +0200447RlcmacDlDataBlock 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 Pedrol5abfded2020-11-03 17:30:44 +0100453 ret_val.cs() = payload_len_2_coding_scheme(stream.lengthof());
454
Harald Welte43e060a2017-07-30 22:38:03 +0200455 /* 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 Welte439e5462018-03-08 23:21:17 +0100464 lb.hdr()().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW);
Harald Welte43e060a2017-07-30 22:38:03 +0200465 ret_val.blocks()[num_llc_blocks++] = lb;
466
467 /* if E == '1'B, we can proceed further */
Harald Welte060e27a2018-03-03 20:38:19 +0100468 if (lb.hdr()().e() == true)
Harald Welte43e060a2017-07-30 22:38:03 +0200469 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 Welte060e27a2018-03-03 20:38:19 +0100486 unsigned int length = ret_val.blocks()[i].hdr()().length__ind();
Harald Welte43e060a2017-07-30 22:38:03 +0200487 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 Pedrol372af7a2020-04-27 17:32:01 +0200498static
499EgprsDlMacDataHeader 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
508static
509EgprsDlMacDataHeader 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
518static
519EgprsDlMacDataHeader 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
545static
Pau Espin Pedrol5abfded2020-11-03 17:30:44 +0100546RlcmacDlEgprsDataBlock dec__RlcmacDlEgprsDataBlock(const OCTETSTRING& stream)
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200547{
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 Pedrol5abfded2020-11-03 17:30:44 +0100556 ret_val.mcs() = payload_len_2_coding_scheme(stream.lengthof());
557 switch (ret_val.mcs()) {
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200558 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 Pedrol5abfded2020-11-03 17:30:44 +0100575 setup_rlc_mac_priv(ret_val.mcs(), ret_val.mac__hdr().header__type(), false,
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200576 &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
613RlcmacDlBlock dec__RlcmacDlBlock(const OCTETSTRING& stream)
614{
615 RlcmacDlBlock ret_val;
616 size_t stream_len = stream.lengthof();
Pau Espin Pedrol5abfded2020-11-03 17:30:44 +0100617 CodingScheme::enum_type cs_mcs = payload_len_2_coding_scheme(stream_len);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200618 unsigned char pt;
619
Pau Espin Pedrol5abfded2020-11-03 17:30:44 +0100620 switch (cs_mcs) {
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200621 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 Pedrol5abfded2020-11-03 17:30:44 +0100641 ret_val.data__egprs() = dec__RlcmacDlEgprsDataBlock(stream);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200642 break;
643 }
644 return ret_val;
645}
646
647/* DECODE UPLINK */
648
649RlcmacUlDataBlock 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 Pedrol5abfded2020-11-03 17:30:44 +0100661 ret_val.cs() = payload_len_2_coding_scheme(stream.lengthof());
662
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200663 /* 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 Pedrola2c5b6e2020-04-30 19:59:13 +0200706 } else {
707 ret_val.tlli() = OMIT_VALUE;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200708 }
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 Pedrola2c5b6e2020-04-30 19:59:13 +0200712 } else {
713 ret_val.pfi() = OMIT_VALUE;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200714 }
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
750static
751EgprsUlMacDataHeader 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
760static
761EgprsUlMacDataHeader 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
770static
771EgprsUlMacDataHeader dec__EgprsUlMacDataHeader_type3(const OCTETSTRING& stream)
772{
Pau Espin Pedrol0b8e22b2020-04-30 20:10:17 +0200773 TTCN_Buffer ttcn_buffer(stream);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200774 EgprsUlMacDataHeader ret_val;
Pau Espin Pedrol0b8e22b2020-04-30 20:10:17 +0200775 const struct gprs_rlc_ul_header_egprs_3 *egprs3;
776 uint8_t tmp;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200777
Pau Espin Pedrol0b8e22b2020-04-30 20:10:17 +0200778 egprs3 = static_cast<const struct gprs_rlc_ul_header_egprs_3 *>
779 ((const void *)ttcn_buffer.get_data());
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200780
Pau Espin Pedrol0b8e22b2020-04-30 20:10:17 +0200781 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 Pedrol372af7a2020-04-27 17:32:01 +0200797 return ret_val;
798}
799
Pau Espin Pedrol5abfded2020-11-03 17:30:44 +0100800RlcmacUlEgprsDataBlock dec__RlcmacUlEgprsDataBlock(const OCTETSTRING& stream)
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200801{
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 Pedrol5abfded2020-11-03 17:30:44 +0100810 ret_val.mcs() = payload_len_2_coding_scheme(stream.lengthof());
811 switch (ret_val.mcs()) {
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200812 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 Pedrol5abfded2020-11-03 17:30:44 +0100828 setup_rlc_mac_priv(ret_val.mcs(), ret_val.mac__hdr().header__type(), true,
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200829 &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 Pedrola2c5b6e2020-04-30 19:59:13 +0200859 } else {
860 ret_val.tlli() = OMIT_VALUE;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200861 }
862 /* parse optional PFI */
863 if (ret_val.mac__hdr().pfi__ind()) {
Pau Espin Pedrol331f5252020-04-30 20:05:29 +0200864 ret_val.pfi().decode(RlcmacUlEgprsDataBlock_pfi_descr_, aligned_buffer, TTCN_EncDec::CT_RAW);
Pau Espin Pedrola2c5b6e2020-04-30 19:59:13 +0200865 } else {
866 ret_val.pfi() = OMIT_VALUE;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200867 }
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
894RlcmacUlBlock dec__RlcmacUlBlock(const OCTETSTRING& stream)
895{
896 RlcmacUlBlock ret_val;
897 size_t stream_len = stream.lengthof();
Pau Espin Pedrol5abfded2020-11-03 17:30:44 +0100898 CodingScheme::enum_type cs_mcs = payload_len_2_coding_scheme(stream_len);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200899 unsigned char pt;
900
Pau Espin Pedrol5abfded2020-11-03 17:30:44 +0100901 switch (cs_mcs) {
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200902 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 Pedrol5abfded2020-11-03 17:30:44 +0100921 ret_val.data__egprs() = dec__RlcmacUlEgprsDataBlock(stream);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +0200922 break;
923 }
924
925 return ret_val;
926}
927
928
929/////////////////////
930// ENCODE
931/////////////////////
932
933/* ENCODE DOWNLINK */
934
935OCTETSTRING 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
977static
978void 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
983static
984void 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
989static
990void 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
995OCTETSTRING 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
1047OCTETSTRING 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 Welte43e060a2017-07-30 22:38:03 +02001058
1059OCTETSTRING enc__RlcmacUlDataBlock(const RlcmacUlDataBlock& si)
1060{
1061 RlcmacUlDataBlock in = si;
1062 OCTETSTRING ret_val;
1063 TTCN_Buffer ttcn_buffer;
1064 int i;
1065
Harald Welte060e27a2018-03-03 20:38:19 +01001066 if (!in.blocks().is_bound()) {
Pau Espin Pedrolecaeb892020-04-30 19:58:15 +02001067 /* we don't have any blocks: Add length value (zero) */
Harald Welte060e27a2018-03-03 20:38:19 +01001068 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 Welte43e060a2017-07-30 22:38:03 +02001074 in.mac__hdr().e() = false;
Harald Welte060e27a2018-03-03 20:38:19 +01001075 }
Harald Welte43e060a2017-07-30 22:38:03 +02001076
1077 /* Fix other presence indications */
Harald Welte2072ab62017-07-31 18:33:35 +02001078 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 Welte43e060a2017-07-30 22:38:03 +02001080
Harald Welte060e27a2018-03-03 20:38:19 +01001081 /* use automatic/generated encoder for header */
Harald Welte43e060a2017-07-30 22:38:03 +02001082 in.mac__hdr().encode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW);
1083
Harald Welte060e27a2018-03-03 20:38:19 +01001084 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 Welte43e060a2017-07-30 22:38:03 +02001117 }
1118 }
1119
1120 if (in.mac__hdr().tlli__ind()) {
Harald Welteacc93ab2018-03-02 21:39:09 +01001121 ttcn_buffer.put_string(in.tlli());
Harald Welte43e060a2017-07-30 22:38:03 +02001122 }
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 Pedrol372af7a2020-04-27 17:32:01 +02001140static
1141void enc__RlcmacUlEgprsDataHeader_type1(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer)
Harald Welte43e060a2017-07-30 22:38:03 +02001142{
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001143 fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__);
1144}
Harald Welte43e060a2017-07-30 22:38:03 +02001145
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001146static
1147void enc__RlcmacUlEgprsDataHeader_type2(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer)
1148{
1149 fprintf(stderr, "FIXME: Not implemented! %s (%s:%u)\n", __func__, __FILE__, __LINE__);
1150}
Harald Welte43e060a2017-07-30 22:38:03 +02001151
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001152static
1153void enc__RlcmacUlEgprsDataHeader_type3(const EgprsUlMacDataHeader& si, TTCN_Buffer& ttcn_buffer)
1154{
1155 struct gprs_rlc_ul_header_egprs_3 egprs3;
Harald Welte43e060a2017-07-30 22:38:03 +02001156
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001157 egprs3.r = bs2uint8(si.r__ri());
1158 egprs3.si = bs2uint8(si.foi__si());
1159 egprs3.cv = si.countdown();
1160 egprs3.tfi_hi = si.tfi() >> 0;
Pau Espin Pedrold3da7972020-04-30 20:06:52 +02001161 egprs3.tfi_lo = si.tfi() >> 2;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001162 egprs3.bsn1_hi = si.bsn1() >> 0;
1163 egprs3.bsn1_lo = si.bsn1() >> 5;
1164 egprs3.cps_hi = si.cps() >> 0;
1165 egprs3.cps_lo = si.cps() >> 2;
1166 egprs3.spb = bs2uint8(si.spb());
Pau Espin Pedrold3da7972020-04-30 20:06:52 +02001167 egprs3.rsb = bs2uint8(si.rsb());
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001168 egprs3.pi = si.pfi__ind();
1169 egprs3.spare = 0;
1170 egprs3.dummy = 0;
Harald Welte43e060a2017-07-30 22:38:03 +02001171
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001172 ttcn_buffer.put_s(sizeof(egprs3), (const unsigned char *)&egprs3);
1173}
Harald Welte43e060a2017-07-30 22:38:03 +02001174
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001175OCTETSTRING enc__RlcmacUlEgprsDataBlock(const RlcmacUlEgprsDataBlock& si)
1176{
1177 RlcmacUlEgprsDataBlock in = si;
1178 OCTETSTRING ret_val;
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001179 TTCN_Buffer ttcn_buffer, aligned_buffer;
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001180 int i;
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001181 unsigned int data_block_bits, data_block_offsets[2];
1182 unsigned int num_calls;
1183 CodingScheme mcs;
1184 boolean tlli_ind, e;
1185
1186 mcs = RLCMAC__Templates::f__rlcmac__cps__htype__to__mcs(in.mac__hdr().cps(), in.mac__hdr().header__type());
1187 //fprintf(stderr, "RLCMAC: infered MCS %s (%d)\n", mcs.enum_to_str(static_cast<CodingScheme::enum_type>(mcs.as_int())), mcs.as_int());
Harald Welte43e060a2017-07-30 22:38:03 +02001188
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001189 if (!in.blocks().is_bound()) {
1190 /* we don't have nay blocks: Add length value (zero) */
1191 in.e() = false; /* E=0: extension octet follows */
1192 } else if (in.blocks().size_of() == 1 && in.blocks()[0].hdr() == OMIT_VALUE) {
1193 /* If there's only a single block, and that block has no HDR value defined, */
1194 in.e() = true; /* E=0: extension octet follows */
Harald Welte43e060a2017-07-30 22:38:03 +02001195 } else {
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001196 /* Length value */
1197 in.e() = false;
1198 }
1199
1200 /* Fix other presence indications */
1201 in.tlli__ind() = in.tlli().is_bound() && in.tlli() != OMIT_VALUE;
1202 in.mac__hdr().pfi__ind() = in.pfi().is_bound() && in.pfi() != OMIT_VALUE;
1203
1204 switch (in.mac__hdr().header__type()) {
1205 case EgprsHeaderType::RLCMAC__HDR__TYPE__1:
Pau Espin Pedrol82d22bc2020-04-30 20:09:12 +02001206 enc__RlcmacUlEgprsDataHeader_type1(in.mac__hdr(), ttcn_buffer);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001207 break;
1208 case EgprsHeaderType::RLCMAC__HDR__TYPE__2:
Pau Espin Pedrol82d22bc2020-04-30 20:09:12 +02001209 enc__RlcmacUlEgprsDataHeader_type2(in.mac__hdr(), ttcn_buffer);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001210 break;
1211 case EgprsHeaderType::RLCMAC__HDR__TYPE__3:
Pau Espin Pedrol82d22bc2020-04-30 20:09:12 +02001212 enc__RlcmacUlEgprsDataHeader_type3(in.mac__hdr(), ttcn_buffer);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001213 default:
1214 break; /* TODO: error */
1215 }
1216
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001217 /* Put first TI + E byte */
1218 aligned_buffer.put_c(tlli_ind << 1 | e << 0); /* M=0, E=1 LEN=0 */
1219 //printbuffer("After encoding first byte", aligned_buffer);
1220
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001221 if (in.e() == false) {
1222 /* Add LI octets, if any */
1223 if (!in.blocks().is_bound()) {
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001224 aligned_buffer.put_c(0x01); /* M=0, E=1 LEN=0 */
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001225 } else {
1226 for (i = 0; i < in.blocks().size_of(); i++) {
1227#if 0
1228 /* check for penultimate block */
1229 if (i == in.blocks().size_of()-2) {
1230 /* if last block has no header, no more LI */
1231 if (in.blocks()[i+1].hdr() == OMIT_VALUE) {
1232 in.blocks()[i].hdr()().more() = true;
1233 } else {
1234 /* header present, we have to encode LI */
1235 in.blocks()[i].hdr()().more() = false;
1236 in.blocks()[i].hdr()().length__ind() =
1237 in.blocks()[i+1].payload().lengthof();
1238 }
1239 } else if (i < in.blocks().size_of()-2) {
1240 /* one of the first blocks, before the penultimate or last */
1241 in.blocks()[i].hdr()().e() = false; /* LI present */
1242 /* re-compute length */
1243 in.blocks()[i].hdr()().length__ind() =
1244 in.blocks()[i+1].payload().lengthof();
1245 }
1246 /* Encode LI octet if E=0 */
1247 }
1248#endif
1249 if (in.blocks()[i].hdr() != OMIT_VALUE) {
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001250 in.blocks()[i].hdr()().encode(EgprsLlcBlockHdr_descr_, aligned_buffer,
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001251 TTCN_EncDec::CT_RAW);
1252 }
Harald Welte43e060a2017-07-30 22:38:03 +02001253 }
1254 }
1255 }
1256
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001257
1258
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001259 if (in.tlli__ind()) {
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001260 aligned_buffer.put_string(in.tlli());
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001261 }
Harald Welte43e060a2017-07-30 22:38:03 +02001262
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001263 if (in.mac__hdr().pfi__ind()) {
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001264 in.pfi().encode(RlcmacUlEgprsDataBlock_pfi_descr_, aligned_buffer, TTCN_EncDec::CT_RAW);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001265 }
1266
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001267 //printbuffer("Before encoding EgprsLlc payload", aligned_buffer);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001268 if (in.blocks().is_bound()) {
1269 for (i = 0; i < in.blocks().size_of(); i++) {
1270 if (!in.blocks()[i].is_bound())
1271 continue;
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001272 aligned_buffer.put_string(in.blocks()[i].payload());
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001273 }
1274 }
Pau Espin Pedrol2456dad2020-04-30 20:22:38 +02001275 //printbuffer("After encoding EgprsLlc payload", aligned_buffer);
1276
1277 setup_rlc_mac_priv(mcs, in.mac__hdr().header__type(), true,
1278 &num_calls, &data_block_bits, data_block_offsets);
1279 //printbuffer("before merging data block", ttcn_buffer);
1280 put_egprs_data_block(aligned_buffer, data_block_offsets[0], data_block_bits, ttcn_buffer);
1281 //printbuffer("after merging data block", ttcn_buffer);
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001282
1283 ttcn_buffer.get_string(ret_val);
Harald Welte43e060a2017-07-30 22:38:03 +02001284 return ret_val;
1285}
1286
Harald Welte78a1af62017-07-31 17:33:56 +02001287OCTETSTRING enc__RlcmacUlBlock(const RlcmacUlBlock& si)
1288{
Pau Espin Pedrol372af7a2020-04-27 17:32:01 +02001289 if (si.ischosen(RlcmacUlBlock::ALT_data__egprs))
1290 return enc__RlcmacUlEgprsDataBlock(si.data__egprs());
1291 else if (si.ischosen(RlcmacUlBlock::ALT_data))
Harald Welte78a1af62017-07-31 17:33:56 +02001292 return enc__RlcmacUlDataBlock(si.data());
1293 else
1294 return enc__RlcmacUlCtrlBlock(si.ctrl());
1295}
1296
Harald Welte43e060a2017-07-30 22:38:03 +02001297} // namespace