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.
136 lines
3.1 KiB
C
136 lines
3.1 KiB
C
11 years ago
|
/**
|
||
|
*
|
||
|
* \section COPYRIGHT
|
||
|
*
|
||
9 years ago
|
* Copyright 2013-2015 Software Radio Systems Limited
|
||
11 years ago
|
*
|
||
|
* \section LICENSE
|
||
11 years ago
|
*
|
||
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
|
|
||
11 years ago
|
#include <sys/time.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <strings.h>
|
||
|
|
||
10 years ago
|
#include "srslte/io/binsource.h"
|
||
|
#include "srslte/utils/bit.h"
|
||
11 years ago
|
|
||
|
#define DIV(a,b) ((a-1)/b+1)
|
||
|
|
||
|
|
||
|
/* Internal functions */
|
||
10 years ago
|
static int gen_seq_buff(srslte_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.
|
||
|
*/
|
||
10 years ago
|
void srslte_binsource_init(srslte_binsource_t* q) {
|
||
|
bzero((void*) q,sizeof(srslte_binsource_t));
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Destroys binsource object
|
||
|
*/
|
||
10 years ago
|
void srslte_binsource_free(srslte_binsource_t* q) {
|
||
11 years ago
|
if (q->seq_buff) {
|
||
|
free(q->seq_buff);
|
||
|
}
|
||
10 years ago
|
bzero(q, sizeof(srslte_binsource_t));
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets a new seed
|
||
|
*/
|
||
10 years ago
|
void srslte_binsource_seed_set(srslte_binsource_t* q, uint32_t seed) {
|
||
11 years ago
|
q->seed = seed;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets local time as seed.
|
||
|
*/
|
||
10 years ago
|
void srslte_binsource_seed_time(srslte_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
|
||
|
*/
|
||
10 years ago
|
int srslte_binsource_cache_gen(srslte_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
|
}
|
||
|
|
||
10 years ago
|
static int int_2_bits(uint32_t* src, uint8_t* dst, int nbits) {
|
||
11 years ago
|
int n;
|
||
|
n=nbits/32;
|
||
|
for (int i=0;i<n;i++) {
|
||
9 years ago
|
srslte_bit_unpack(src[i],&dst,32);
|
||
11 years ago
|
}
|
||
9 years ago
|
srslte_bit_unpack(src[n],&dst,nbits-n*32);
|
||
11 years ago
|
return n;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Copies the next random bits to the buffer bits from the array generated by srslte_binsource_cache_gen
|
||
11 years ago
|
*/
|
||
10 years ago
|
void srslte_binsource_cache_cpy(srslte_binsource_t* q, uint8_t *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.
|
||
10 years ago
|
* Overwrites the bits generated using srslte_binsource_cache_gen.
|
||
11 years ago
|
*/
|
||
10 years ago
|
int srslte_binsource_generate(srslte_binsource_t* q, uint8_t *bits, int nbits) {
|
||
11 years ago
|
|
||
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
|
}
|
||
|
|
||
|
|
||
|
|