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.
181 lines
3.8 KiB
C
181 lines
3.8 KiB
C
11 years ago
|
/**
|
||
|
*
|
||
|
* \section COPYRIGHT
|
||
|
*
|
||
|
* Copyright 2013-2014 The libLTE Developers. See the
|
||
|
* COPYRIGHT file at the top-level directory of this distribution.
|
||
|
*
|
||
|
* \section LICENSE
|
||
11 years ago
|
*
|
||
11 years ago
|
* This file is part of the libLTE library.
|
||
11 years ago
|
*
|
||
11 years ago
|
* libLTE is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Lesser General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
|
* libLTE 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
|
||
|
* GNU Lesser General Public License for more details.
|
||
|
*
|
||
11 years ago
|
* A copy of the GNU Lesser General Public License can be found in
|
||
|
* the LICENSE file in the top-level directory of this distribution
|
||
|
* and at http://www.gnu.org/licenses/.
|
||
|
*
|
||
11 years ago
|
*/
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
#include <sys/time.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <strings.h>
|
||
|
|
||
11 years ago
|
#include "liblte/phy/io/binsource.h"
|
||
|
#include "liblte/phy/utils/bit.h"
|
||
11 years ago
|
|
||
|
#define DIV(a,b) ((a-1)/b+1)
|
||
|
|
||
|
|
||
|
/* Internal functions */
|
||
|
static int gen_seq_buff(binsource_t* q, int nwords) {
|
||
11 years ago
|
if (q->seq_buff_nwords != nwords) {
|
||
|
free(q->seq_buff);
|
||
|
q->seq_buff_nwords = 0;
|
||
|
}
|
||
|
if (!q->seq_buff_nwords) {
|
||
|
q->seq_buff = malloc(nwords*sizeof(uint32_t));
|
||
|
if (!q->seq_buff) {
|
||
|
return -1;
|
||
|
}
|
||
|
q->seq_buff_nwords = nwords;
|
||
|
}
|
||
|
for (int i=0;i<q->seq_buff_nwords;i++) {
|
||
|
q->seq_buff[i] = rand_r(&q->seed);
|
||
|
}
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
|
/* Low-level API */
|
||
|
|
||
|
/**
|
||
|
* Initializes the binsource object.
|
||
|
*/
|
||
|
void binsource_init(binsource_t* q) {
|
||
11 years ago
|
bzero((void*) q,sizeof(binsource_t));
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Destroys binsource object
|
||
|
*/
|
||
|
void binsource_free(binsource_t* q) {
|
||
11 years ago
|
if (q->seq_buff) {
|
||
|
free(q->seq_buff);
|
||
|
}
|
||
|
bzero(q, sizeof(binsource_t));
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets a new seed
|
||
|
*/
|
||
|
void binsource_seed_set(binsource_t* q, unsigned int seed) {
|
||
11 years ago
|
q->seed = seed;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets local time as seed.
|
||
|
*/
|
||
|
void binsource_seed_time(binsource_t *q) {
|
||
11 years ago
|
struct timeval t1;
|
||
|
gettimeofday(&t1, NULL);
|
||
|
q->seed = t1.tv_usec * t1.tv_sec;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Generates a sequence of nbits random bits
|
||
|
*/
|
||
|
int binsource_cache_gen(binsource_t* q, int nbits) {
|
||
11 years ago
|
if (gen_seq_buff(q,DIV(nbits,32))) {
|
||
|
return -1;
|
||
|
}
|
||
|
q->seq_cache_nbits = nbits;
|
||
|
q->seq_cache_rp = 0;
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
|
static int int_2_bits(uint32_t* src, char* dst, int nbits) {
|
||
11 years ago
|
int n;
|
||
|
n=nbits/32;
|
||
|
for (int i=0;i<n;i++) {
|
||
|
bit_pack(src[i],&dst,32);
|
||
|
}
|
||
|
bit_pack(src[n],&dst,nbits-n*32);
|
||
|
return n;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Copies the next random bits to the buffer bits from the array generated by binsource_cache_gen
|
||
|
*/
|
||
|
void binsource_cache_cpy(binsource_t* q, char *bits, int nbits) {
|
||
11 years ago
|
q->seq_cache_rp += int_2_bits(&q->seq_buff[q->seq_cache_rp],bits,nbits);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Stores in the bits buffer a sequence of nbits pseudo-random bits.
|
||
|
* Overwrites the bits generated using binsource_cache_gen.
|
||
|
*/
|
||
|
int binsource_generate(binsource_t* q, char *bits, int nbits) {
|
||
|
|
||
11 years ago
|
if (gen_seq_buff(q,DIV(nbits,32))) {
|
||
|
return -1;
|
||
|
}
|
||
|
int_2_bits(q->seq_buff,bits,nbits);
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* High-Level API */
|
||
|
int binsource_initialize(binsource_hl* hl) {
|
||
|
|
||
11 years ago
|
binsource_init(&hl->obj);
|
||
|
if (hl->init.seed) {
|
||
|
binsource_seed_set(&hl->obj,hl->init.seed);
|
||
|
} else {
|
||
|
binsource_seed_time(&hl->obj);
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
if (hl->init.cache_seq_nbits) {
|
||
|
if (binsource_cache_gen(&hl->obj,hl->init.cache_seq_nbits)) {
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
|
int binsource_work(binsource_hl* hl) {
|
||
11 years ago
|
int ret = -1;
|
||
|
|
||
|
if (hl->init.cache_seq_nbits) {
|
||
|
binsource_cache_cpy(&hl->obj,hl->output,hl->ctrl_in.nbits);
|
||
|
ret = 0;
|
||
|
} else {
|
||
|
ret = binsource_generate(&hl->obj,hl->output,hl->ctrl_in.nbits);
|
||
|
}
|
||
|
if (!ret) {
|
||
|
hl->out_len = hl->ctrl_in.nbits;
|
||
|
} else {
|
||
|
hl->out_len = 0;
|
||
|
}
|
||
|
return ret;
|
||
11 years ago
|
}
|
||
|
|
||
|
int binsource_stop(binsource_hl* hl) {
|
||
11 years ago
|
binsource_free(&hl->obj);
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|