Holger Hans Peter Freyther | d11290b | 2013-10-26 17:32:04 +0200 | [diff] [blame] | 1 | /* 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 Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 24 | extern "C" { |
| 25 | #include <osmocom/core/utils.h> |
| 26 | } |
| 27 | |
Holger Hans Peter Freyther | d11290b | 2013-10-26 17:32:04 +0200 | [diff] [blame] | 28 | #include <arpa/inet.h> |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
| 32 | |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 33 | #define LENGTH_TO_END 255 |
| 34 | /* |
| 35 | * \returns num extensions fields (num frames == offset) on success, |
| 36 | * -errno otherwise. |
| 37 | */ |
| 38 | static int parse_extensions_egprs(const uint8_t *data, unsigned int data_len, |
| 39 | unsigned int *offs, |
| 40 | bool is_last_block, |
| 41 | Decoding::RlcData *chunks, unsigned int chunks_size) |
| 42 | { |
| 43 | const struct rlc_li_field_egprs *li; |
| 44 | uint8_t e; |
| 45 | unsigned int num_chunks = 0; |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 46 | |
| 47 | e = 0; |
| 48 | while (!e) { |
| 49 | if (*offs > data_len) { |
| 50 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, " |
| 51 | "but no more data\n"); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | /* get new E */ |
| 56 | li = (struct rlc_li_field_egprs *)&data[*offs]; |
| 57 | e = li->e; |
| 58 | *offs += 1; |
| 59 | |
| 60 | if (!chunks) |
| 61 | continue; |
| 62 | |
| 63 | if (num_chunks == chunks_size) { |
| 64 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, " |
| 65 | "but no more chunks possible\n"); |
| 66 | return -ENOSPC; |
| 67 | } |
| 68 | if (li->li == 0 && num_chunks == 0 && li->e == 0) { |
| 69 | /* TS 44.060, table 10.4.14a.1, row 2a */ |
| 70 | chunks[num_chunks].length = 0; |
| 71 | chunks[num_chunks].is_complete = true; |
| 72 | } else if (li->li == 0 && num_chunks == 0 && li->e == 1) { |
| 73 | /* TS 44.060, table 10.4.14a.1, row 4 */ |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 74 | chunks[num_chunks].length = LENGTH_TO_END; |
| 75 | chunks[num_chunks].is_complete = is_last_block; |
| 76 | } else if (li->li == 127 && li->e == 1) { |
| 77 | /* TS 44.060, table 10.4.14a.1, row 3 & 5 */ |
| 78 | /* only filling bytes left */ |
| 79 | break; |
| 80 | } else if (li->li > 0) { |
| 81 | /* TS 44.060, table 10.4.14a.1, row 1 & 2b */ |
| 82 | chunks[num_chunks].length = li->li; |
| 83 | chunks[num_chunks].is_complete = true; |
| 84 | } else { |
| 85 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI contains " |
| 86 | "invalid extension octet: LI=%d, E=%d, count=%d\n", |
| 87 | li->li, li->e, num_chunks); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | num_chunks += 1; |
| 92 | |
| 93 | if (e == 1) { |
| 94 | /* There is space after the last chunk, add a final one */ |
| 95 | if (num_chunks == chunks_size) { |
| 96 | LOGP(DRLCMACUL, LOGL_NOTICE, |
| 97 | "UL DATA LI possibly extended, " |
| 98 | "but no more chunks possible\n"); |
| 99 | return -ENOSPC; |
| 100 | } |
| 101 | |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 102 | chunks[num_chunks].length = LENGTH_TO_END; |
| 103 | chunks[num_chunks].is_complete = is_last_block; |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 104 | num_chunks += 1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return num_chunks; |
| 109 | } |
| 110 | |
| 111 | static int parse_extensions_gprs(const uint8_t *data, unsigned int data_len, |
| 112 | unsigned int *offs, |
| 113 | bool is_last_block, |
| 114 | Decoding::RlcData *chunks, unsigned int chunks_size) |
| 115 | { |
| 116 | const struct rlc_li_field *li; |
| 117 | uint8_t m, e; |
| 118 | unsigned int num_chunks = 0; |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 119 | |
| 120 | e = 0; |
| 121 | while (!e) { |
| 122 | if (*offs > data_len) { |
| 123 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, " |
| 124 | "but no more data\n"); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | /* get new E */ |
| 129 | li = (const struct rlc_li_field *)&data[*offs]; |
| 130 | e = li->e; |
| 131 | m = li->m; |
| 132 | *offs += 1; |
| 133 | |
| 134 | if (li->li == 0) { |
| 135 | /* TS 44.060, 10.4.14, par 6 */ |
| 136 | e = 1; |
| 137 | m = 0; |
| 138 | } |
| 139 | |
| 140 | /* TS 44.060, table 10.4.13.1 */ |
| 141 | if (m == 0 && e == 0) { |
| 142 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA " |
| 143 | "ignored, because M='0' and E='0'.\n"); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | if (!chunks) |
| 148 | continue; |
| 149 | |
| 150 | if (num_chunks == chunks_size) { |
| 151 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, " |
| 152 | "but no more chunks possible\n"); |
| 153 | return -ENOSPC; |
| 154 | } |
| 155 | |
| 156 | if (li->li == 0) |
| 157 | /* e is 1 here */ |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 158 | chunks[num_chunks].length = LENGTH_TO_END; |
| 159 | else |
| 160 | chunks[num_chunks].length = li->li; |
| 161 | |
| 162 | chunks[num_chunks].is_complete = li->li || is_last_block; |
| 163 | |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 164 | num_chunks += 1; |
| 165 | |
| 166 | if (e == 1 && m == 1) { |
| 167 | if (num_chunks == chunks_size) { |
| 168 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, " |
| 169 | "but no more chunks possible\n"); |
| 170 | return -ENOSPC; |
| 171 | } |
| 172 | /* TS 44.060, 10.4.13.1, row 4 */ |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 173 | chunks[num_chunks].length = LENGTH_TO_END; |
| 174 | chunks[num_chunks].is_complete = is_last_block; |
| 175 | num_chunks += 1; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return num_chunks; |
| 180 | } |
| 181 | |
| 182 | int Decoding::rlc_data_from_ul_data( |
| 183 | const struct gprs_rlc_ul_data_block_info *rdbi, GprsCodingScheme cs, |
| 184 | const uint8_t *data, RlcData *chunks, unsigned int chunks_size, |
| 185 | uint32_t *tlli) |
| 186 | { |
| 187 | uint8_t e; |
| 188 | unsigned int data_len = rdbi->data_len; |
Jacob Erlbeck | ead5e47 | 2016-01-08 10:14:50 +0100 | [diff] [blame] | 189 | int num_chunks = 0, i; |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 190 | unsigned int offs = 0; |
| 191 | bool is_last_block = (rdbi->cv == 0); |
| 192 | |
| 193 | if (!chunks) |
| 194 | chunks_size = 0; |
| 195 | |
| 196 | e = rdbi->e; |
| 197 | if (e) { |
| 198 | if (chunks_size > 0) { |
| 199 | chunks[num_chunks].offset = offs; |
| 200 | chunks[num_chunks].length = LENGTH_TO_END; |
| 201 | chunks[num_chunks].is_complete = is_last_block; |
| 202 | num_chunks += 1; |
| 203 | } else if (chunks) { |
| 204 | LOGP(DRLCMACUL, LOGL_NOTICE, "No extension, " |
| 205 | "but no more chunks possible\n"); |
| 206 | return -ENOSPC; |
| 207 | } |
| 208 | } else if (cs.isEgprs()) { |
| 209 | /* if E is not set (LI follows), EGPRS */ |
| 210 | num_chunks = parse_extensions_egprs(data, data_len, &offs, |
| 211 | is_last_block, |
| 212 | chunks, chunks_size); |
| 213 | } else { |
| 214 | /* if E is not set (LI follows), GPRS */ |
| 215 | num_chunks = parse_extensions_gprs(data, data_len, &offs, |
| 216 | is_last_block, |
| 217 | chunks, chunks_size); |
| 218 | } |
| 219 | |
Jacob Erlbeck | ead5e47 | 2016-01-08 10:14:50 +0100 | [diff] [blame] | 220 | if (num_chunks < 0) |
| 221 | return num_chunks; |
| 222 | |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 223 | /* TLLI */ |
| 224 | if (rdbi->ti) { |
| 225 | uint32_t tlli_enc; |
| 226 | if (offs + 4 > data_len) { |
| 227 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of block " |
| 228 | "border\n"); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | memcpy(&tlli_enc, data + offs, sizeof(tlli_enc)); |
| 233 | if (cs.isGprs()) |
| 234 | /* The TLLI is encoded in big endian for GPRS (see |
| 235 | * TS 44.060, figure 10.2.2.1, note) */ |
| 236 | *tlli = be32toh(tlli_enc); |
| 237 | else |
| 238 | /* The TLLI is encoded in little endian for EGPRS (see |
| 239 | * TS 44.060, figure 10.3a.2.1, note 2) */ |
| 240 | *tlli = le32toh(tlli_enc); |
| 241 | |
| 242 | offs += sizeof(tlli_enc); |
| 243 | } else { |
| 244 | *tlli = 0; |
| 245 | } |
| 246 | |
| 247 | /* PFI */ |
| 248 | if (rdbi->pi) { |
| 249 | LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, " |
| 250 | "please disable in SYSTEM INFORMATION\n"); |
| 251 | return -ENOTSUP; |
| 252 | |
| 253 | /* TODO: Skip all extensions with E=0 (see TS 44.060, 10.4.11 */ |
| 254 | } |
| 255 | |
Jacob Erlbeck | ead5e47 | 2016-01-08 10:14:50 +0100 | [diff] [blame] | 256 | if (chunks_size == 0) |
| 257 | return num_chunks; |
| 258 | |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 259 | /* LLC */ |
| 260 | for (i = 0; i < num_chunks; i++) { |
| 261 | chunks[i].offset = offs; |
| 262 | if (chunks[i].length == LENGTH_TO_END) { |
| 263 | if (offs == data_len) { |
| 264 | /* There is no place for an additional chunk, |
| 265 | * so drop it (this may happen with EGPRS since |
| 266 | * there is no M flag. */ |
| 267 | num_chunks -= 1; |
| 268 | break;; |
| 269 | } |
| 270 | chunks[i].length = data_len - offs; |
| 271 | } |
| 272 | offs += chunks[i].length; |
| 273 | if (offs > data_len) { |
| 274 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA out of block " |
| 275 | "border, chunk idx: %d, size: %d\n", |
| 276 | i, chunks[i].length); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return num_chunks; |
| 282 | } |
Holger Hans Peter Freyther | d11290b | 2013-10-26 17:32:04 +0200 | [diff] [blame] | 283 | |
| 284 | int Decoding::tlli_from_ul_data(const uint8_t *data, uint8_t len, |
| 285 | uint32_t *tlli) |
| 286 | { |
| 287 | struct rlc_ul_header *rh = (struct rlc_ul_header *)data; |
| 288 | struct rlc_li_field *li; |
| 289 | uint8_t e; |
| 290 | uint32_t _tlli; |
| 291 | |
| 292 | if (!rh->ti) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | data += 3; |
| 296 | len -= 3; |
| 297 | e = rh->e; |
| 298 | /* if E is not set (LI follows) */ |
| 299 | while (!e) { |
| 300 | if (!len) { |
| 301 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, " |
| 302 | "but no more data\n"); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | /* get new E */ |
| 306 | li = (struct rlc_li_field *)data; |
| 307 | if (li->e == 0) /* if LI==0, E is interpreted as '1' */ |
| 308 | e = 1; |
| 309 | else |
| 310 | e = li->e; |
| 311 | data++; |
| 312 | len--; |
| 313 | } |
| 314 | if (len < 4) { |
| 315 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of frame " |
| 316 | "border\n"); |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | memcpy(&_tlli, data, 4); |
| 320 | *tlli = ntohl(_tlli); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Holger Hans Peter Freyther | fcbc702 | 2013-10-26 17:38:37 +0200 | [diff] [blame] | 325 | uint8_t Decoding::get_ms_class_by_capability(MS_Radio_Access_capability_t *cap) |
| 326 | { |
| 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < cap->Count_MS_RA_capability_value; i++) { |
| 330 | if (!cap->MS_RA_capability_value[i].u.Content.Exist_Multislot_capability) |
| 331 | continue; |
| 332 | if (!cap->MS_RA_capability_value[i].u.Content.Multislot_capability.Exist_GPRS_multislot_class) |
| 333 | continue; |
| 334 | return cap->MS_RA_capability_value[i].u.Content.Multislot_capability.GPRS_multislot_class; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Jacob Erlbeck | c3c5804 | 2015-09-28 17:55:32 +0200 | [diff] [blame] | 340 | uint8_t Decoding::get_egprs_ms_class_by_capability(MS_Radio_Access_capability_t *cap) |
| 341 | { |
| 342 | int i; |
| 343 | |
| 344 | for (i = 0; i < cap->Count_MS_RA_capability_value; i++) { |
| 345 | if (!cap->MS_RA_capability_value[i].u.Content.Exist_Multislot_capability) |
| 346 | continue; |
| 347 | if (!cap->MS_RA_capability_value[i].u.Content.Multislot_capability.Exist_EGPRS_multislot_class) |
| 348 | continue; |
| 349 | return cap->MS_RA_capability_value[i].u.Content.Multislot_capability.EGPRS_multislot_class; |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 355 | /** |
| 356 | * show_rbb needs to be an array with 65 elements |
Daniel Willmann | 5e94cd4 | 2013-12-11 20:11:16 +0100 | [diff] [blame] | 357 | * The index of the array is the bit position in the rbb |
| 358 | * (show_rbb[63] relates to BSN ssn-1) |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 359 | */ |
| 360 | void Decoding::extract_rbb(const uint8_t *rbb, char *show_rbb) |
| 361 | { |
Daniel Willmann | 5e94cd4 | 2013-12-11 20:11:16 +0100 | [diff] [blame] | 362 | for (int i = 0; i < 64; i++) { |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 363 | uint8_t bit; |
| 364 | |
Daniel Willmann | 5e94cd4 | 2013-12-11 20:11:16 +0100 | [diff] [blame] | 365 | bit = !!(rbb[i/8] & (1<<(7-i%8))); |
Daniel Willmann | 6f7cb2c | 2013-12-11 14:25:20 +0100 | [diff] [blame] | 366 | show_rbb[i] = bit ? 'R' : 'I'; |
Holger Hans Peter Freyther | df6b4f5 | 2013-11-24 17:05:48 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | show_rbb[64] = '\0'; |
| 370 | } |
Jacob Erlbeck | 4abc686 | 2015-12-08 15:14:05 +0100 | [diff] [blame] | 371 | |
| 372 | int Decoding::rlc_parse_ul_data_header(struct gprs_rlc_ul_header_egprs *rlc, |
| 373 | const uint8_t *data, GprsCodingScheme cs) |
| 374 | { |
| 375 | const struct gprs_rlc_ul_header_egprs_3 *egprs3; |
| 376 | const struct rlc_ul_header *gprs; |
| 377 | unsigned int e_ti_header; |
| 378 | unsigned int cur_bit = 0; |
| 379 | unsigned int data_len = 0; |
| 380 | |
| 381 | rlc->cs = cs; |
| 382 | |
| 383 | data_len = cs.maxDataBlockBytes(); |
| 384 | |
| 385 | switch(cs.headerTypeData()) { |
| 386 | case GprsCodingScheme::HEADER_GPRS_DATA: |
| 387 | gprs = static_cast<struct rlc_ul_header *> |
| 388 | ((void *)data); |
| 389 | rlc->r = gprs->r; |
| 390 | rlc->si = gprs->si; |
| 391 | rlc->tfi = gprs->tfi; |
| 392 | rlc->cps = 0; |
| 393 | rlc->rsb = 0; |
| 394 | |
| 395 | rlc->num_data_blocks = 1; |
| 396 | rlc->block_info[0].cv = gprs->cv; |
| 397 | rlc->block_info[0].pi = gprs->pi; |
| 398 | rlc->block_info[0].bsn = gprs->bsn; |
| 399 | rlc->block_info[0].e = gprs->e; |
| 400 | rlc->block_info[0].ti = gprs->ti; |
| 401 | rlc->block_info[0].spb = 0; |
| 402 | |
| 403 | cur_bit += 3 * 8; |
| 404 | |
| 405 | rlc->data_offs_bits[0] = cur_bit; |
| 406 | rlc->block_info[0].data_len = data_len; |
| 407 | cur_bit += data_len * 8; |
| 408 | |
| 409 | break; |
| 410 | case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_3: |
| 411 | egprs3 = static_cast<struct gprs_rlc_ul_header_egprs_3 *> |
| 412 | ((void *)data); |
| 413 | rlc->r = egprs3->r; |
| 414 | rlc->si = egprs3->si; |
| 415 | rlc->tfi = (egprs3->tfi_a << 0) | (egprs3->tfi_b << 2); |
| 416 | rlc->cps = (egprs3->cps_a << 0) | (egprs3->cps_b << 2); |
| 417 | rlc->rsb = egprs3->rsb; |
| 418 | |
| 419 | rlc->num_data_blocks = 1; |
| 420 | rlc->block_info[0].cv = egprs3->cv; |
| 421 | rlc->block_info[0].pi = egprs3->pi; |
| 422 | rlc->block_info[0].spb = egprs3->spb; |
| 423 | rlc->block_info[0].bsn = |
| 424 | (egprs3->bsn1_a << 0) | (egprs3->bsn1_b << 5); |
| 425 | |
| 426 | cur_bit += 3 * 8 + 7; |
| 427 | |
| 428 | e_ti_header = (data[3] + (data[4] << 8)) >> 7; |
| 429 | rlc->block_info[0].e = !!(e_ti_header & 0x01); |
| 430 | rlc->block_info[0].ti = !!(e_ti_header & 0x02); |
| 431 | cur_bit += 2; |
| 432 | |
| 433 | rlc->data_offs_bits[0] = cur_bit; |
| 434 | rlc->block_info[0].data_len = data_len; |
| 435 | cur_bit += data_len * 8; |
| 436 | |
| 437 | break; |
| 438 | |
| 439 | case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_1: |
| 440 | case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_2: |
| 441 | /* TODO: Support both header types */ |
| 442 | /* fall through */ |
| 443 | default: |
| 444 | LOGP(DRLCMACDL, LOGL_ERROR, |
| 445 | "Decoding of uplink %s data blocks not yet supported.\n", |
| 446 | cs.name()); |
| 447 | return -ENOTSUP; |
| 448 | }; |
| 449 | |
| 450 | return cur_bit; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * \brief Copy LSB bitstream RLC data block to byte aligned buffer. |
| 455 | * |
| 456 | * Note that the bitstream is encoded in LSB first order, so the two octets |
| 457 | * 654321xx xxxxxx87 contain the octet 87654321 starting at bit position 3 |
| 458 | * (LSB has bit position 1). This is a different order than the one used by |
| 459 | * CSN.1. |
| 460 | * |
| 461 | * \param data_block_idx The block index, 0..1 for header type 1, 0 otherwise |
| 462 | * \param src A pointer to the start of the RLC block (incl. the header) |
| 463 | * \param buffer A data area of a least the size of the RLC block |
| 464 | * \returns the number of bytes copied |
| 465 | */ |
| 466 | unsigned int Decoding::rlc_copy_to_aligned_buffer( |
| 467 | const struct gprs_rlc_ul_header_egprs *rlc, |
| 468 | unsigned int data_block_idx, |
| 469 | const uint8_t *src, uint8_t *buffer) |
| 470 | { |
| 471 | unsigned int hdr_bytes; |
| 472 | unsigned int extra_bits; |
| 473 | unsigned int i; |
| 474 | |
| 475 | uint8_t c, last_c; |
| 476 | uint8_t *dst; |
| 477 | const struct gprs_rlc_ul_data_block_info *rdbi; |
| 478 | |
| 479 | OSMO_ASSERT(data_block_idx < rlc->num_data_blocks); |
| 480 | rdbi = &rlc->block_info[data_block_idx]; |
| 481 | |
| 482 | hdr_bytes = rlc->data_offs_bits[data_block_idx] >> 3; |
| 483 | extra_bits = (rlc->data_offs_bits[data_block_idx] & 7); |
| 484 | |
| 485 | if (extra_bits == 0) { |
| 486 | /* It is aligned already */ |
| 487 | memmove(buffer, src + hdr_bytes, rdbi->data_len); |
| 488 | return rdbi->data_len; |
| 489 | } |
| 490 | |
| 491 | dst = buffer; |
| 492 | src = src + hdr_bytes; |
| 493 | last_c = *(src++); |
| 494 | |
| 495 | for (i = 0; i < rdbi->data_len; i++) { |
| 496 | c = src[i]; |
| 497 | *(dst++) = (last_c >> extra_bits) | (c << (8 - extra_bits)); |
| 498 | last_c = c; |
| 499 | } |
| 500 | |
| 501 | return rdbi->data_len; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * \brief Get a pointer to byte aligned RLC data. |
| 506 | * |
| 507 | * Since the RLC data may not be byte aligned to the RLC block data such that a |
| 508 | * single RLC data byte is spread over two RLC block bytes, this function |
| 509 | * eventually uses the provided buffer as data storage. |
| 510 | * |
| 511 | * \param src A pointer to the start of the RLC block (incl. the header) |
| 512 | * \param buffer A data area of a least the size of the RLC block |
| 513 | * \returns A pointer to the RLC data start within src if it is aligned, and |
| 514 | * buffer otherwise. |
| 515 | */ |
| 516 | const uint8_t *Decoding::rlc_get_data_aligned( |
| 517 | const struct gprs_rlc_ul_header_egprs *rlc, |
| 518 | unsigned int data_block_idx, |
| 519 | const uint8_t *src, uint8_t *buffer) |
| 520 | { |
| 521 | unsigned int hdr_bytes; |
| 522 | unsigned int extra_bits; |
| 523 | |
| 524 | OSMO_ASSERT(data_block_idx < ARRAY_SIZE(rlc->data_offs_bits)); |
| 525 | |
| 526 | hdr_bytes = rlc->data_offs_bits[data_block_idx] >> 3; |
| 527 | extra_bits = (rlc->data_offs_bits[data_block_idx] & 7); |
| 528 | |
| 529 | if (extra_bits == 0) |
| 530 | /* It is aligned already, return a pointer that refers to the |
| 531 | * original data. */ |
| 532 | return src + hdr_bytes; |
| 533 | |
| 534 | Decoding::rlc_copy_to_aligned_buffer(rlc, data_block_idx, src, buffer); |
| 535 | return buffer; |
| 536 | } |