mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.4 KiB
C
191 lines
4.4 KiB
C
11 years ago
|
/**
|
||
11 years ago
|
*
|
||
11 years ago
|
* \section COPYRIGHT
|
||
11 years ago
|
*
|
||
9 years ago
|
* Copyright 2013-2015 Software Radio Systems Limited
|
||
11 years ago
|
*
|
||
|
* \section LICENSE
|
||
|
*
|
||
10 years ago
|
* This file is part of the srsLTE library.
|
||
11 years ago
|
*
|
||
10 years ago
|
* srsLTE is free software: you can redistribute it and/or modify
|
||
10 years ago
|
* it under the terms of the GNU Affero General Public License as
|
||
11 years ago
|
* published by the Free Software Foundation, either version 3 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
10 years ago
|
* srsLTE is distributed in the hope that it will be useful,
|
||
11 years ago
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
* GNU Affero General Public License for more details.
|
||
11 years ago
|
*
|
||
10 years ago
|
* A copy of the GNU Affero General Public License can be found in
|
||
11 years ago
|
* the LICENSE file in the top-level directory of this distribution
|
||
|
* and at http://www.gnu.org/licenses/.
|
||
|
*
|
||
11 years ago
|
*/
|
||
|
|
||
11 years ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
11 years ago
|
#include <string.h>
|
||
|
|
||
10 years ago
|
#include "srslte/utils/bit.h"
|
||
10 years ago
|
#include "srslte/fec/crc.h"
|
||
11 years ago
|
|
||
10 years ago
|
void gen_crc_table(srslte_crc_t *h) {
|
||
11 years ago
|
|
||
11 years ago
|
int i, j, ord = (h->order - 8);
|
||
9 years ago
|
uint64_t bit, crc;
|
||
11 years ago
|
|
||
|
for (i = 0; i < 256; i++) {
|
||
9 years ago
|
crc = ((uint64_t) i) << ord;
|
||
11 years ago
|
for (j = 0; j < 8; j++) {
|
||
|
bit = crc & h->crchighbit;
|
||
|
crc <<= 1;
|
||
|
if (bit)
|
||
|
crc ^= h->polynom;
|
||
|
}
|
||
|
h->table[i] = crc & h->crcmask;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
uint64_t crctable(srslte_crc_t *h, uint8_t byte) {
|
||
11 years ago
|
|
||
11 years ago
|
// Polynom order 8, 16, 24 or 32 only.
|
||
|
int ord = h->order - 8;
|
||
9 years ago
|
uint64_t crc = h->crcinit;
|
||
11 years ago
|
|
||
11 years ago
|
crc = (crc << 8) ^ h->table[((crc >> (ord)) & 0xff) ^ byte];
|
||
|
h->crcinit = crc;
|
||
|
return (crc & h->crcmask);
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
uint64_t reversecrcbit(uint32_t crc, int nbits, srslte_crc_t *h) {
|
||
11 years ago
|
|
||
9 years ago
|
uint64_t m, rmask = 0x1;
|
||
11 years ago
|
|
||
11 years ago
|
for (m = 0; m < nbits; m++) {
|
||
|
if ((rmask & crc) == 0x01)
|
||
|
crc = (crc ^ h->polynom) >> 1;
|
||
|
else
|
||
|
crc = crc >> 1;
|
||
|
}
|
||
|
return (crc & h->crcmask);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
int srslte_crc_set_init(srslte_crc_t *crc_par, uint64_t crc_init_value) {
|
||
11 years ago
|
|
||
10 years ago
|
crc_par->crcinit = crc_init_value;
|
||
|
if (crc_par->crcinit != (crc_par->crcinit & crc_par->crcmask)) {
|
||
|
printf("ERROR, invalid crcinit in crc_set_init().\n");
|
||
11 years ago
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int srslte_crc_init(srslte_crc_t *h, uint32_t crc_poly, int crc_order) {
|
||
11 years ago
|
|
||
11 years ago
|
// Set crc working default parameters
|
||
10 years ago
|
h->polynom = crc_poly;
|
||
|
h->order = crc_order;
|
||
11 years ago
|
h->crcinit = 0x00000000;
|
||
11 years ago
|
|
||
11 years ago
|
// Compute bit masks for whole CRC and CRC high bit
|
||
9 years ago
|
h->crcmask = ((((uint64_t) 1 << (h->order - 1)) - 1) << 1) | 1;
|
||
|
h->crchighbit = (uint64_t) 1 << (h->order - 1);
|
||
11 years ago
|
|
||
11 years ago
|
// check parameters
|
||
|
if (h->order % 8 != 0) {
|
||
|
fprintf(stderr, "ERROR, invalid order=%d, it must be 8, 16, 24 or 32.\n",
|
||
|
h->order);
|
||
|
return -1;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
if (srslte_crc_set_init(h, h->crcinit)) {
|
||
11 years ago
|
fprintf(stderr, "Error setting CRC init word\n");
|
||
|
return -1;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// generate lookup table
|
||
10 years ago
|
gen_crc_table(h);
|
||
11 years ago
|
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
uint32_t srslte_crc_checksum(srslte_crc_t *h, uint8_t *data, int len) {
|
||
11 years ago
|
int i, k, len8, res8, a = 0;
|
||
10 years ago
|
uint32_t crc = 0;
|
||
|
uint8_t *pter;
|
||
11 years ago
|
|
||
10 years ago
|
srslte_crc_set_init(h, 0);
|
||
11 years ago
|
|
||
|
// Pack bits into bytes
|
||
|
len8 = (len >> 3);
|
||
|
res8 = (len - (len8 << 3));
|
||
|
if (res8 > 0) {
|
||
|
a = 1;
|
||
|
}
|
||
|
|
||
|
// Calculate CRC
|
||
|
for (i = 0; i < len8 + a; i++) {
|
||
10 years ago
|
pter = (uint8_t *) (data + 8 * i);
|
||
9 years ago
|
uint8_t byte;
|
||
11 years ago
|
if (i == len8) {
|
||
9 years ago
|
byte = 0x00;
|
||
11 years ago
|
for (k = 0; k < res8; k++) {
|
||
9 years ago
|
byte |= ((uint8_t) *(pter + k)) << (7 - k);
|
||
11 years ago
|
}
|
||
|
} else {
|
||
9 years ago
|
byte = (uint8_t) (srslte_bit_pack(&pter, 8) & 0xFF);
|
||
11 years ago
|
}
|
||
9 years ago
|
crc = crctable(h, byte);
|
||
11 years ago
|
}
|
||
|
|
||
|
// Reverse CRC res8 positions
|
||
|
if (a == 1) {
|
||
|
crc = reversecrcbit(crc, 8 - res8, h);
|
||
|
}
|
||
|
|
||
|
//Return CRC value
|
||
|
return crc;
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
// len is multiple of 8
|
||
|
uint32_t srslte_crc_checksum_byte(srslte_crc_t *h, uint8_t *data, int len) {
|
||
|
int i;
|
||
|
uint32_t crc = 0;
|
||
|
|
||
|
srslte_crc_set_init(h, 0);
|
||
|
|
||
|
// Calculate CRC
|
||
|
for (i = 0; i < len/8; i++) {
|
||
|
crc = crctable(h, data[i]);
|
||
|
}
|
||
|
|
||
|
return crc;
|
||
|
|
||
|
}
|
||
|
|
||
|
uint32_t srslte_crc_attach_byte(srslte_crc_t *h, uint8_t *data, int len) {
|
||
|
uint32_t checksum = srslte_crc_checksum_byte(h, data, len);
|
||
|
|
||
|
// Add CRC
|
||
|
for (int i=0;i<h->order/8;i++) {
|
||
|
data[len/8+(h->order/8-i-1)] = (checksum&(0xff<<(8*i)))>>(8*i);
|
||
|
}
|
||
|
return checksum;
|
||
|
}
|
||
|
|
||
10 years ago
|
/** Appends crc_order checksum bits to the buffer data.
|
||
|
* The buffer data must be len + crc_order bytes
|
||
11 years ago
|
*/
|
||
9 years ago
|
uint32_t srslte_crc_attach(srslte_crc_t *h, uint8_t *data, int len) {
|
||
10 years ago
|
uint32_t checksum = srslte_crc_checksum(h, data, len);
|
||
11 years ago
|
|
||
11 years ago
|
// Add CRC
|
||
10 years ago
|
uint8_t *ptr = &data[len];
|
||
9 years ago
|
srslte_bit_unpack(checksum, &ptr, h->order);
|
||
9 years ago
|
return checksum;
|
||
11 years ago
|
}
|
||
|
|