blob: 0c81b2a77fda7d5e2ca1358553b9645c21bb62fe [file] [log] [blame]
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +02001/* decoding
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20#include <decoding.h>
21#include <rlc.h>
22#include <gprs_debug.h>
23
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010024extern "C" {
25#include <osmocom/core/utils.h>
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +010026#include <osmocom/core/bitcomp.h>
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010027}
28
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +020029#include <arpa/inet.h>
30
31#include <errno.h>
32#include <string.h>
33
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010034#define LENGTH_TO_END 255
35/*
36 * \returns num extensions fields (num frames == offset) on success,
37 * -errno otherwise.
38 */
39static int parse_extensions_egprs(const uint8_t *data, unsigned int data_len,
40 unsigned int *offs,
41 bool is_last_block,
42 Decoding::RlcData *chunks, unsigned int chunks_size)
43{
44 const struct rlc_li_field_egprs *li;
45 uint8_t e;
46 unsigned int num_chunks = 0;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010047
48 e = 0;
49 while (!e) {
50 if (*offs > data_len) {
51 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
52 "but no more data\n");
53 return -EINVAL;
54 }
55
56 /* get new E */
57 li = (struct rlc_li_field_egprs *)&data[*offs];
58 e = li->e;
59 *offs += 1;
60
61 if (!chunks)
62 continue;
63
64 if (num_chunks == chunks_size) {
65 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
66 "but no more chunks possible\n");
67 return -ENOSPC;
68 }
69 if (li->li == 0 && num_chunks == 0 && li->e == 0) {
70 /* TS 44.060, table 10.4.14a.1, row 2a */
71 chunks[num_chunks].length = 0;
72 chunks[num_chunks].is_complete = true;
73 } else if (li->li == 0 && num_chunks == 0 && li->e == 1) {
74 /* TS 44.060, table 10.4.14a.1, row 4 */
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010075 chunks[num_chunks].length = LENGTH_TO_END;
76 chunks[num_chunks].is_complete = is_last_block;
77 } else if (li->li == 127 && li->e == 1) {
78 /* TS 44.060, table 10.4.14a.1, row 3 & 5 */
79 /* only filling bytes left */
80 break;
81 } else if (li->li > 0) {
82 /* TS 44.060, table 10.4.14a.1, row 1 & 2b */
83 chunks[num_chunks].length = li->li;
84 chunks[num_chunks].is_complete = true;
85 } else {
86 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI contains "
87 "invalid extension octet: LI=%d, E=%d, count=%d\n",
88 li->li, li->e, num_chunks);
89 return -EINVAL;
90 }
91
92 num_chunks += 1;
93
94 if (e == 1) {
95 /* There is space after the last chunk, add a final one */
96 if (num_chunks == chunks_size) {
97 LOGP(DRLCMACUL, LOGL_NOTICE,
98 "UL DATA LI possibly extended, "
99 "but no more chunks possible\n");
100 return -ENOSPC;
101 }
102
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100103 chunks[num_chunks].length = LENGTH_TO_END;
104 chunks[num_chunks].is_complete = is_last_block;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100105 num_chunks += 1;
106 }
107 }
108
109 return num_chunks;
110}
111
112static int parse_extensions_gprs(const uint8_t *data, unsigned int data_len,
113 unsigned int *offs,
114 bool is_last_block,
115 Decoding::RlcData *chunks, unsigned int chunks_size)
116{
117 const struct rlc_li_field *li;
118 uint8_t m, e;
119 unsigned int num_chunks = 0;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100120
121 e = 0;
122 while (!e) {
123 if (*offs > data_len) {
124 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
125 "but no more data\n");
126 return -EINVAL;
127 }
128
129 /* get new E */
130 li = (const struct rlc_li_field *)&data[*offs];
131 e = li->e;
132 m = li->m;
133 *offs += 1;
134
135 if (li->li == 0) {
136 /* TS 44.060, 10.4.14, par 6 */
137 e = 1;
138 m = 0;
139 }
140
141 /* TS 44.060, table 10.4.13.1 */
142 if (m == 0 && e == 0) {
143 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA "
144 "ignored, because M='0' and E='0'.\n");
145 return 0;
146 }
147
148 if (!chunks)
149 continue;
150
151 if (num_chunks == chunks_size) {
152 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
153 "but no more chunks possible\n");
154 return -ENOSPC;
155 }
156
157 if (li->li == 0)
158 /* e is 1 here */
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100159 chunks[num_chunks].length = LENGTH_TO_END;
160 else
161 chunks[num_chunks].length = li->li;
162
163 chunks[num_chunks].is_complete = li->li || is_last_block;
164
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100165 num_chunks += 1;
166
167 if (e == 1 && m == 1) {
168 if (num_chunks == chunks_size) {
169 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
170 "but no more chunks possible\n");
171 return -ENOSPC;
172 }
173 /* TS 44.060, 10.4.13.1, row 4 */
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100174 chunks[num_chunks].length = LENGTH_TO_END;
175 chunks[num_chunks].is_complete = is_last_block;
176 num_chunks += 1;
177 }
178 }
179
180 return num_chunks;
181}
182
183int Decoding::rlc_data_from_ul_data(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100184 const struct gprs_rlc_data_block_info *rdbi, GprsCodingScheme cs,
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100185 const uint8_t *data, RlcData *chunks, unsigned int chunks_size,
186 uint32_t *tlli)
187{
188 uint8_t e;
189 unsigned int data_len = rdbi->data_len;
Jacob Erlbeckead5e472016-01-08 10:14:50 +0100190 int num_chunks = 0, i;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100191 unsigned int offs = 0;
192 bool is_last_block = (rdbi->cv == 0);
193
194 if (!chunks)
195 chunks_size = 0;
196
197 e = rdbi->e;
198 if (e) {
199 if (chunks_size > 0) {
200 chunks[num_chunks].offset = offs;
201 chunks[num_chunks].length = LENGTH_TO_END;
202 chunks[num_chunks].is_complete = is_last_block;
203 num_chunks += 1;
204 } else if (chunks) {
205 LOGP(DRLCMACUL, LOGL_NOTICE, "No extension, "
206 "but no more chunks possible\n");
207 return -ENOSPC;
208 }
209 } else if (cs.isEgprs()) {
210 /* if E is not set (LI follows), EGPRS */
211 num_chunks = parse_extensions_egprs(data, data_len, &offs,
212 is_last_block,
213 chunks, chunks_size);
214 } else {
215 /* if E is not set (LI follows), GPRS */
216 num_chunks = parse_extensions_gprs(data, data_len, &offs,
217 is_last_block,
218 chunks, chunks_size);
219 }
220
Jacob Erlbeckead5e472016-01-08 10:14:50 +0100221 if (num_chunks < 0)
222 return num_chunks;
223
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100224 /* TLLI */
225 if (rdbi->ti) {
226 uint32_t tlli_enc;
227 if (offs + 4 > data_len) {
228 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of block "
229 "border\n");
230 return -EINVAL;
231 }
232
233 memcpy(&tlli_enc, data + offs, sizeof(tlli_enc));
234 if (cs.isGprs())
235 /* The TLLI is encoded in big endian for GPRS (see
236 * TS 44.060, figure 10.2.2.1, note) */
237 *tlli = be32toh(tlli_enc);
238 else
239 /* The TLLI is encoded in little endian for EGPRS (see
240 * TS 44.060, figure 10.3a.2.1, note 2) */
241 *tlli = le32toh(tlli_enc);
242
243 offs += sizeof(tlli_enc);
244 } else {
245 *tlli = 0;
246 }
247
248 /* PFI */
249 if (rdbi->pi) {
250 LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, "
251 "please disable in SYSTEM INFORMATION\n");
252 return -ENOTSUP;
253
254 /* TODO: Skip all extensions with E=0 (see TS 44.060, 10.4.11 */
255 }
256
Jacob Erlbeckead5e472016-01-08 10:14:50 +0100257 if (chunks_size == 0)
258 return num_chunks;
259
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100260 /* LLC */
261 for (i = 0; i < num_chunks; i++) {
262 chunks[i].offset = offs;
263 if (chunks[i].length == LENGTH_TO_END) {
264 if (offs == data_len) {
265 /* There is no place for an additional chunk,
266 * so drop it (this may happen with EGPRS since
267 * there is no M flag. */
268 num_chunks -= 1;
269 break;;
270 }
271 chunks[i].length = data_len - offs;
272 }
273 offs += chunks[i].length;
274 if (offs > data_len) {
275 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA out of block "
276 "border, chunk idx: %d, size: %d\n",
277 i, chunks[i].length);
278 return -EINVAL;
279 }
280 }
281
282 return num_chunks;
283}
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200284
Holger Hans Peter Freytherfcbc7022013-10-26 17:38:37 +0200285uint8_t Decoding::get_ms_class_by_capability(MS_Radio_Access_capability_t *cap)
286{
287 int i;
288
289 for (i = 0; i < cap->Count_MS_RA_capability_value; i++) {
290 if (!cap->MS_RA_capability_value[i].u.Content.Exist_Multislot_capability)
291 continue;
292 if (!cap->MS_RA_capability_value[i].u.Content.Multislot_capability.Exist_GPRS_multislot_class)
293 continue;
294 return cap->MS_RA_capability_value[i].u.Content.Multislot_capability.GPRS_multislot_class;
295 }
296
297 return 0;
298}
299
Jacob Erlbeckc3c58042015-09-28 17:55:32 +0200300uint8_t Decoding::get_egprs_ms_class_by_capability(MS_Radio_Access_capability_t *cap)
301{
302 int i;
303
304 for (i = 0; i < cap->Count_MS_RA_capability_value; i++) {
305 if (!cap->MS_RA_capability_value[i].u.Content.Exist_Multislot_capability)
306 continue;
307 if (!cap->MS_RA_capability_value[i].u.Content.Multislot_capability.Exist_EGPRS_multislot_class)
308 continue;
309 return cap->MS_RA_capability_value[i].u.Content.Multislot_capability.EGPRS_multislot_class;
310 }
311
312 return 0;
313}
314
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100315/**
316 * show_rbb needs to be an array with 65 elements
Daniel Willmann5e94cd42013-12-11 20:11:16 +0100317 * The index of the array is the bit position in the rbb
318 * (show_rbb[63] relates to BSN ssn-1)
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100319 */
320void Decoding::extract_rbb(const uint8_t *rbb, char *show_rbb)
321{
Daniel Willmann5e94cd42013-12-11 20:11:16 +0100322 for (int i = 0; i < 64; i++) {
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100323 uint8_t bit;
324
Daniel Willmann5e94cd42013-12-11 20:11:16 +0100325 bit = !!(rbb[i/8] & (1<<(7-i%8)));
Daniel Willmann6f7cb2c2013-12-11 14:25:20 +0100326 show_rbb[i] = bit ? 'R' : 'I';
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100327 }
328
329 show_rbb[64] = '\0';
330}
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100331
Jacob Erlbeckf2f24b02016-02-05 13:16:24 +0100332void Decoding::extract_rbb(const struct bitvec *rbb, char *show_rbb)
333{
334 unsigned int i;
335 for (i = 0; i < rbb->cur_bit; i++) {
336 uint8_t bit;
337 bit = bitvec_get_bit_pos(rbb, i);
338 show_rbb[i] = bit == 1 ? 'R' : 'I';
339 }
340
341 show_rbb[i] = '\0';
342}
343
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100344int Decoding::rlc_parse_ul_data_header(struct gprs_rlc_data_info *rlc,
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100345 const uint8_t *data, GprsCodingScheme cs)
346{
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100347 unsigned int cur_bit = 0;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100348 switch(cs.headerTypeData()) {
Bhargava Abhyankare44383b2016-03-22 18:42:30 +0530349 case GprsCodingScheme::HEADER_GPRS_DATA :
350 cur_bit = rlc_parse_ul_data_header_gprs(rlc, data, cs);
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100351 break;
Bhargava Abhyankare44383b2016-03-22 18:42:30 +0530352 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_3 :
353 cur_bit = rlc_parse_ul_data_header_egprs_type_3(rlc, data, cs);
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100354 break;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100355 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_1:
356 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_2:
357 /* TODO: Support both header types */
358 /* fall through */
359 default:
360 LOGP(DRLCMACDL, LOGL_ERROR,
361 "Decoding of uplink %s data blocks not yet supported.\n",
362 cs.name());
363 return -ENOTSUP;
364 };
365
366 return cur_bit;
367}
368
Bhargava Abhyankare44383b2016-03-22 18:42:30 +0530369int Decoding::rlc_parse_ul_data_header_egprs_type_3(
370 struct gprs_rlc_data_info *rlc,
371 const uint8_t *data,
372 const GprsCodingScheme &cs)
373{
374 int punct, punct2, with_padding, cps;
375 unsigned int e_ti_header, offs, cur_bit = 0;
376 const struct gprs_rlc_ul_header_egprs_3 *egprs3;
377
378 egprs3 = static_cast < struct gprs_rlc_ul_header_egprs_3 * >
379 ((void *)data);
380
381 cps = (egprs3->cps_a << 0) | (egprs3->cps_b << 2);
382 gprs_rlc_mcs_cps_decode(cps, cs, &punct, &punct2, &with_padding);
383 gprs_rlc_data_info_init_ul(rlc, cs, with_padding);
384
385 rlc->r = egprs3->r;
386 rlc->si = egprs3->si;
387 rlc->tfi = (egprs3->tfi_a << 0) | (egprs3->tfi_b << 2);
388 rlc->cps = cps;
389 rlc->rsb = egprs3->rsb;
390
391 rlc->num_data_blocks = 1;
392 rlc->block_info[0].cv = egprs3->cv;
393 rlc->block_info[0].pi = egprs3->pi;
394 rlc->block_info[0].spb = egprs3->spb;
395 rlc->block_info[0].bsn =
396 (egprs3->bsn1_a << 0) | (egprs3->bsn1_b << 5);
397
398 cur_bit += rlc->data_offs_bits[0] - 2;
399 offs = rlc->data_offs_bits[0] / 8;
400 OSMO_ASSERT(rlc->data_offs_bits[0] % 8 == 1);
401 e_ti_header = (data[offs-1] + (data[offs] << 8)) >> 7;
402 rlc->block_info[0].e = !!(e_ti_header & 0x01);
403 rlc->block_info[0].ti = !!(e_ti_header & 0x02);
404 cur_bit += 2;
405 /* skip data area */
406 cur_bit += cs.maxDataBlockBytes() * 8;
407
408 return cur_bit;
409}
410
411int Decoding::rlc_parse_ul_data_header_gprs(struct gprs_rlc_data_info *rlc,
412 const uint8_t *data, const GprsCodingScheme &cs)
413{
414 const struct rlc_ul_header *gprs;
415 unsigned int cur_bit = 0;
416
417 gprs = static_cast < struct rlc_ul_header * >
418 ((void *)data);
419
420 gprs_rlc_data_info_init_ul(rlc, cs, false);
421
422 rlc->r = gprs->r;
423 rlc->si = gprs->si;
424 rlc->tfi = gprs->tfi;
425 rlc->cps = 0;
426 rlc->rsb = 0;
427 rlc->num_data_blocks = 1;
428 rlc->block_info[0].cv = gprs->cv;
429 rlc->block_info[0].pi = gprs->pi;
430 rlc->block_info[0].bsn = gprs->bsn;
431 rlc->block_info[0].e = gprs->e;
432 rlc->block_info[0].ti = gprs->ti;
433 rlc->block_info[0].spb = 0;
434 cur_bit += rlc->data_offs_bits[0];
435 /* skip data area */
436 cur_bit += cs.maxDataBlockBytes() * 8;
437
438 return cur_bit;
439}
440
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100441/**
442 * \brief Copy LSB bitstream RLC data block to byte aligned buffer.
443 *
444 * Note that the bitstream is encoded in LSB first order, so the two octets
445 * 654321xx xxxxxx87 contain the octet 87654321 starting at bit position 3
446 * (LSB has bit position 1). This is a different order than the one used by
447 * CSN.1.
448 *
449 * \param data_block_idx The block index, 0..1 for header type 1, 0 otherwise
450 * \param src A pointer to the start of the RLC block (incl. the header)
451 * \param buffer A data area of a least the size of the RLC block
452 * \returns the number of bytes copied
453 */
454unsigned int Decoding::rlc_copy_to_aligned_buffer(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100455 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100456 unsigned int data_block_idx,
457 const uint8_t *src, uint8_t *buffer)
458{
459 unsigned int hdr_bytes;
460 unsigned int extra_bits;
461 unsigned int i;
462
463 uint8_t c, last_c;
464 uint8_t *dst;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100465 const struct gprs_rlc_data_block_info *rdbi;
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100466
467 OSMO_ASSERT(data_block_idx < rlc->num_data_blocks);
468 rdbi = &rlc->block_info[data_block_idx];
469
470 hdr_bytes = rlc->data_offs_bits[data_block_idx] >> 3;
471 extra_bits = (rlc->data_offs_bits[data_block_idx] & 7);
472
473 if (extra_bits == 0) {
474 /* It is aligned already */
475 memmove(buffer, src + hdr_bytes, rdbi->data_len);
476 return rdbi->data_len;
477 }
478
479 dst = buffer;
480 src = src + hdr_bytes;
481 last_c = *(src++);
482
483 for (i = 0; i < rdbi->data_len; i++) {
484 c = src[i];
485 *(dst++) = (last_c >> extra_bits) | (c << (8 - extra_bits));
486 last_c = c;
487 }
488
489 return rdbi->data_len;
490}
491
492/**
493 * \brief Get a pointer to byte aligned RLC data.
494 *
495 * Since the RLC data may not be byte aligned to the RLC block data such that a
496 * single RLC data byte is spread over two RLC block bytes, this function
497 * eventually uses the provided buffer as data storage.
498 *
499 * \param src A pointer to the start of the RLC block (incl. the header)
500 * \param buffer A data area of a least the size of the RLC block
501 * \returns A pointer to the RLC data start within src if it is aligned, and
502 * buffer otherwise.
503 */
504const uint8_t *Decoding::rlc_get_data_aligned(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100505 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100506 unsigned int data_block_idx,
507 const uint8_t *src, uint8_t *buffer)
508{
509 unsigned int hdr_bytes;
510 unsigned int extra_bits;
511
512 OSMO_ASSERT(data_block_idx < ARRAY_SIZE(rlc->data_offs_bits));
513
514 hdr_bytes = rlc->data_offs_bits[data_block_idx] >> 3;
515 extra_bits = (rlc->data_offs_bits[data_block_idx] & 7);
516
517 if (extra_bits == 0)
518 /* It is aligned already, return a pointer that refers to the
519 * original data. */
520 return src + hdr_bytes;
521
522 Decoding::rlc_copy_to_aligned_buffer(rlc, data_block_idx, src, buffer);
523 return buffer;
524}
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100525
526static int handle_final_ack(bitvec *bits, int *bsn_begin, int *bsn_end,
527 gprs_rlc_dl_window *window)
528{
529 int num_blocks, i;
530
531 num_blocks = window->mod_sns(window->v_s() - window->v_a());
532 for (i = 0; i < num_blocks; i++)
533 bitvec_set_bit(bits, ONE);
534
535 *bsn_begin = window->v_a();
536 *bsn_end = window->mod_sns(*bsn_begin + num_blocks);
537 return num_blocks;
538}
539
540int Decoding::decode_egprs_acknack_bits(const EGPRS_AckNack_Desc_t *desc,
541 bitvec *bits, int *bsn_begin, int *bsn_end, gprs_rlc_dl_window *window)
542{
543 int urbb_len = desc->URBB_LENGTH;
544 int crbb_len = 0;
545 int num_blocks = 0;
546 struct bitvec urbb;
547 int i;
548 bool have_bitmap;
549 int implicitly_acked_blocks;
550 int ssn = desc->STARTING_SEQUENCE_NUMBER;
Jacob Erlbeck2bef4db2016-01-20 14:33:11 +0100551 int rc;
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100552
553 if (desc->FINAL_ACK_INDICATION)
554 return handle_final_ack(bits, bsn_begin, bsn_end, window);
555
556 if (desc->Exist_CRBB)
557 crbb_len = desc->CRBB_LENGTH;
558
559 have_bitmap = (urbb_len + crbb_len) > 0;
560
561 /*
562 * bow & bitmap present:
563 * V(A)-> [ 11111...11111 0 SSN-> BBBBB...BBBBB ] (SSN+Nbits) .... V(S)
564 * bow & not bitmap present:
565 * V(A)-> [ 11111...11111 ] . SSN .... V(S)
566 * not bow & bitmap present:
567 * V(A)-> ... [ 0 SSN-> BBBBB...BBBBB ](SSN+N) .... V(S)
568 * not bow & not bitmap present:
569 * V(A)-> ... [] . SSN .... V(S)
570 */
571
572 if (desc->BEGINNING_OF_WINDOW) {
573 implicitly_acked_blocks = window->mod_sns(ssn - 1 - window->v_a());
574
575 for (i = 0; i < implicitly_acked_blocks; i++)
576 bitvec_set_bit(bits, ONE);
577
578 num_blocks += implicitly_acked_blocks;
579 }
580
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100581 if (!have_bitmap)
582 goto aborted;
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100583
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100584 /* next bit refers to V(Q) and thus is always zero (and not
585 * transmitted) */
586 bitvec_set_bit(bits, ZERO);
587 num_blocks += 1;
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100588
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100589 if (crbb_len > 0) {
590 int old_len = bits->cur_bit;
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100591 struct bitvec crbb;
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100592
593 crbb.data = (uint8_t *)desc->CRBB;
594 crbb.data_len = sizeof(desc->CRBB);
595 crbb.cur_bit = desc->CRBB_LENGTH;
596
597 rc = osmo_t4_decode(&crbb, desc->CRBB_STARTING_COLOR_CODE,
598 bits);
599
600 if (rc < 0) {
601 LOGP(DRLCMACUL, LOGL_NOTICE,
602 "Failed to decode CRBB: "
603 "length %d, data '%s'\n",
604 desc->CRBB_LENGTH,
605 osmo_hexdump(crbb.data, crbb.data_len));
606 /* We don't know the SSN offset for the URBB,
607 * return what we have so far and assume the
608 * bitmap has stopped here */
609 goto aborted;
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100610 }
611
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100612 LOGP(DRLCMACDL, LOGL_DEBUG,
613 "CRBB len: %d, decoded len: %d, cc: %d, crbb: '%s'\n",
614 desc->CRBB_LENGTH, bits->cur_bit - old_len,
615 desc->CRBB_STARTING_COLOR_CODE,
616 osmo_hexdump(
617 desc->CRBB, (desc->CRBB_LENGTH + 7)/8)
618 );
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100619
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100620 num_blocks += (bits->cur_bit - old_len);
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100621 }
622
Jacob Erlbeck3fdcb2b2016-01-18 16:51:59 +0100623 urbb.cur_bit = 0;
624 urbb.data = (uint8_t *)desc->URBB;
625 urbb.data_len = sizeof(desc->URBB);
626
627 for (i = urbb_len; i > 0; i--) {
628 /*
629 * Set bit at the appropriate position (see 3GPP TS
630 * 44.060 12.3.1)
631 */
632 int is_ack = bitvec_get_bit_pos(&urbb, i-1);
633 bitvec_set_bit(bits, is_ack == 1 ? ONE : ZERO);
634 }
635 num_blocks += urbb_len;
636
637aborted:
Jacob Erlbeckae9c5752016-01-13 13:55:44 +0100638 *bsn_begin = window->v_a();
639 *bsn_end = window->mod_sns(*bsn_begin + num_blocks);
640
641 return num_blocks;
642}
Jacob Erlbeck192bf332016-02-05 13:10:29 +0100643
644int Decoding::decode_gprs_acknack_bits(const Ack_Nack_Description_t *desc,
645 bitvec *bits, int *bsn_begin, int *bsn_end, gprs_rlc_dl_window *window)
646{
647 int urbb_len = RLC_GPRS_WS;
648 int num_blocks;
649 struct bitvec urbb;
650
651 if (desc->FINAL_ACK_INDICATION)
652 return handle_final_ack(bits, bsn_begin, bsn_end, window);
653
654 *bsn_begin = window->v_a();
655 *bsn_end = desc->STARTING_SEQUENCE_NUMBER;
656
657 num_blocks = window->mod_sns(*bsn_end - *bsn_begin);
658
659 if (num_blocks < 0 || num_blocks > urbb_len) {
660 *bsn_end = *bsn_begin;
661 LOGP(DRLCMACUL, LOGL_NOTICE,
662 "Invalid GPRS Ack/Nack window %d:%d (length %d)\n",
663 *bsn_begin, *bsn_end, num_blocks);
664 return -EINVAL;
665 }
666
667 urbb.cur_bit = 0;
668 urbb.data = (uint8_t *)desc->RECEIVED_BLOCK_BITMAP;
669 urbb.data_len = sizeof(desc->RECEIVED_BLOCK_BITMAP);
670
671 /*
672 * TS 44.060, 12.3:
673 * BSN = (SSN - bit_number) modulo 128, for bit_number = 1 to 64.
674 * The BSN values represented range from (SSN - 1) mod 128 to (SSN - 64) mod 128.
675 *
676 * We are only interested in the range from V(A) to SSN-1 which is
677 * num_blocks large. The RBB is laid out as
678 * [SSN-1] [SSN-2] ... [V(A)] ... [SSN-64]
679 * so we want to start with [V(A)] and go backwards until we reach
680 * [SSN-1] to get the needed BSNs in an increasing order. Note that
681 * the bit numbers are counted from the end of the buffer.
682 */
683 for (int i = num_blocks; i > 0; i--) {
684 int is_ack = bitvec_get_bit_pos(&urbb, urbb_len - i);
685 bitvec_set_bit(bits, is_ack == 1 ? ONE : ZERO);
686 }
687
688 return num_blocks;
689}