Added PRACH example. Added timed send/recv functions to cuhd

This commit is contained in:
ismagom 2015-02-24 11:19:42 +01:00
parent 31a4edffae
commit 83e3250c29
22 changed files with 746 additions and 43 deletions

View File

@ -71,27 +71,41 @@ LIBLTE_API int cuhd_recv(void *h,
bool blocking);
LIBLTE_API int cuhd_recv_timed(void *h,
void *data,
uint32_t nsamples,
bool blocking,
time_t *secs,
double *frac_secs);
void *data,
uint32_t nsamples,
bool blocking,
time_t *secs,
double *frac_secs);
LIBLTE_API double cuhd_set_tx_srate(void *h,
double freq);
LIBLTE_API void cuhd_set_tx_antenna(void *h,
char *name);
LIBLTE_API double cuhd_set_tx_gain(void *h,
double gain);
LIBLTE_API double cuhd_set_tx_freq(void *h,
double freq);
LIBLTE_API double cuhd_set_tx_freq_offset(void *h,
double freq,
double off);
LIBLTE_API int cuhd_send(void *h,
void *data,
uint32_t nsamples,
bool blocking);
LIBLTE_API int cuhd_send_timed(void *h,
void *data,
int nsamples,
int blocking,
time_t secs,
double frac_secs);
#ifdef __cplusplus
}
#endif

View File

@ -214,11 +214,12 @@ int cuhd_recv(void *h, void *data, uint32_t nsamples, bool blocking)
}
int cuhd_recv_timed(void *h,
void *data,
uint32_t nsamples,
int blocking,
time_t *secs,
double *frac_secs) {
void *data,
uint32_t nsamples,
bool blocking,
time_t *secs,
double *frac_secs)
{
cuhd_handler* handler = static_cast<cuhd_handler*>(h);
uhd::rx_metadata_t md;
*secs = -1;
@ -247,7 +248,11 @@ int cuhd_recv_timed(void *h,
}
}
void cuhd_set_tx_antenna(void *h, char *name)
{
cuhd_handler *handler = static_cast < cuhd_handler * >(h);
handler->usrp->set_tx_antenna(name, 0);
}
double cuhd_set_tx_gain(void *h, double gain)
{
cuhd_handler *handler = static_cast < cuhd_handler * >(h);
@ -269,6 +274,13 @@ double cuhd_set_tx_freq(void *h, double freq)
return handler->usrp->get_tx_freq();
}
double cuhd_set_tx_freq_offset(void *h, double freq, double off) {
cuhd_handler* handler = static_cast<cuhd_handler*>(h);
handler->usrp->set_tx_freq(uhd::tune_request_t(freq, off));
return handler->usrp->get_tx_freq();
}
int cuhd_send(void *h, void *data, uint32_t nsamples, bool blocking)
{
cuhd_handler *handler = static_cast < cuhd_handler * >(h);
@ -288,3 +300,31 @@ int cuhd_send(void *h, void *data, uint32_t nsamples, bool blocking)
return handler->tx_stream->send(data, nsamples, md, 0.0);
}
}
int cuhd_send_timed(void *h,
void *data,
int nsamples,
int blocking,
time_t secs,
double frac_secs) {
cuhd_handler* handler = static_cast<cuhd_handler*>(h);
uhd::tx_metadata_t md;
md.has_time_spec = true;
md.time_spec = uhd::time_spec_t(secs, frac_secs);
if (blocking) {
int n=0,p;
complex_t *data_c = (complex_t*) data;
do {
p=handler->tx_stream->send(&data_c[n], nsamples-n, md);
md.has_time_spec = false;
if (p == -1) {
return -1;
}
n+=p;
} while(n<nsamples);
return nsamples;
} else {
return handler->tx_stream->send(data, nsamples, md, 0.0);
}
}

View File

@ -83,6 +83,9 @@ IF(${CUHD_FIND} GREATER -1)
add_executable(cell_search cell_search.c cuhd_utils.c)
target_link_libraries(cell_search lte_rrc lte_phy cuhd )
add_executable(prach_ue prach_ue.c cuhd_utils.c)
target_link_libraries(prach_ue lte_rrc lte_phy cuhd)
add_executable(cell_measurement cell_measurement.c cuhd_utils.c)
target_link_libraries(cell_measurement cuhd lte_rrc lte_phy)

View File

@ -120,7 +120,7 @@ int parse_args(prog_args_t *args, int argc, char **argv) {
/* TODO: Do something with the output data */
uint8_t data[10000], data_unpacked[1000];
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples) {
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples, timestamp_t *q) {
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
return cuhd_recv(h, data, nsamples, 1);
}
@ -196,7 +196,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error initiating ue_sync\n");
return -1;
}
if (ue_dl_init(&ue_dl, cell, 1234)) {
if (ue_dl_init(&ue_dl, cell)) {
fprintf(stderr, "Error initiating UE downlink processing module\n");
return -1;
}

View File

@ -111,7 +111,7 @@ void parse_args(int argc, char **argv) {
}
}
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples) {
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples, timestamp_t *t) {
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
return cuhd_recv(h, data, nsamples, 1);
}

View File

@ -43,7 +43,7 @@
#ifndef DISABLE_UHD
#include "liblte/cuhd/cuhd.h"
int cuhd_recv_wrapper_cs(void *h, void *data, uint32_t nsamples) {
int cuhd_recv_wrapper_cs(void *h, void *data, uint32_t nsamples, timestamp_t *t) {
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
return cuhd_recv(h, data, nsamples, 1);
}

View File

@ -107,7 +107,7 @@ void args_default(prog_args_t *args) {
void usage(prog_args_t *args, char *prog) {
printf("Usage: %s [agildnruv] -f rx_frequency (in Hz) | -i input_file\n", prog);
#ifndef DISABLE_GRAPHICS
#ifndef DISABLE_UHD
printf("\t-a UHD args [Default %s]\n", args->uhd_args);
printf("\t-g UHD RX gain [Default %.2f dB]\n", args->uhd_gain);
#else
@ -202,7 +202,7 @@ void sig_int_handler(int signo)
}
#ifndef DISABLE_UHD
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples) {
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples, timestamp_t *t) {
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
return cuhd_recv(h, data, nsamples, 1);
}
@ -320,10 +320,12 @@ int main(int argc, char **argv) {
#endif
}
if (ue_dl_init(&ue_dl, cell, prog_args.rnti==SIRNTI?1:prog_args.rnti)) { // This is the User RNTI
if (ue_dl_init(&ue_dl, cell)) { // This is the User RNTI
fprintf(stderr, "Error initiating UE downlink processing module\n");
exit(-1);
}
ue_dl_set_user_rnti(&ue_dl, prog_args.rnti==SIRNTI?1:prog_args.rnti);
/* Configure downlink receiver for the SI-RNTI since will be the only one we'll use */
ue_dl_set_rnti(&ue_dl, prog_args.rnti);

370
lte/examples/prach_ue.c Normal file
View File

@ -0,0 +1,370 @@
/**
*
* \section COPYRIGHT
*
* Copyright 2013-2014 The libLTE Developers. See the
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
* This file is part of the libLTE library.
*
* 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,
* 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.
*
* 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/.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include "liblte/rrc/rrc.h"
#include "liblte/phy/phy.h"
#include "liblte/cuhd/cuhd.h"
#include "cuhd_utils.h"
cell_search_cfg_t cell_detect_config = {
5000,
100, // nof_frames_total
16.0 // threshold
};
#define B210_DEFAULT_GAIN 40.0
#define B210_DEFAULT_GAIN_CORREC 110.0 // Gain of the Rx chain when the gain is set to 40
float gain_offset = B210_DEFAULT_GAIN_CORREC;
double pss_time_offset = (6/14)*10e-3;
double prach_time_offset = 4*10e-3; //Subframe 4
/**********************************************************************
* Program arguments processing
***********************************************************************/
typedef struct {
int nof_subframes;
int force_N_id_2;
uint16_t rnti;
uint32_t file_nof_prb;
char *uhd_args;
float uhd_rx_freq;
float uhd_tx_freq;
float uhd_tx_freq_offset;
float uhd_gain;
int net_port;
char *net_address;
int net_port_signal;
char *net_address_signal;
}prog_args_t;
void args_default(prog_args_t *args) {
args->nof_subframes = -1;
args->rnti = SIRNTI;
args->force_N_id_2 = -1; // Pick the best
args->file_nof_prb = 6;
args->uhd_args = "";
args->uhd_rx_freq = 2112500000.0;
args->uhd_tx_freq = 1922500000.0;
args->uhd_tx_freq_offset = 8000000.0;
args->uhd_gain = 60.0;
args->net_port = -1;
args->net_address = "127.0.0.1";
args->net_port_signal = -1;
args->net_address_signal = "127.0.0.1";
}
void usage(prog_args_t *args, char *prog) {
printf("Usage: %s [agfFrlnv]\n", prog);
printf("\t-a UHD args [Default %s]\n", args->uhd_args);
printf("\t-g UHD TX/RX gain [Default %.2f dB]\n", args->uhd_gain);
printf("\t-f UHD RX freq [Default %.1f MHz]\n", args->uhd_rx_freq/1000000);
printf("\t-F UHD TX freq [Default %.1f MHz]\n", args->uhd_tx_freq/1000000);
printf("\t-r RNTI [Default 0x%x]\n",args->rnti);
printf("\t-l Force N_id_2 [Default best]\n");
printf("\t-n nof_subframes [Default %d]\n", args->nof_subframes);
printf("\t-v [set verbose to debug, default none]\n");
}
void parse_args(prog_args_t *args, int argc, char **argv) {
int opt;
args_default(args);
while ((opt = getopt(argc, argv, "agfFrlnv")) != -1) {
switch (opt) {
case 'a':
args->uhd_args = argv[optind];
break;
case 'g':
args->uhd_gain = atof(argv[optind]);
break;
case 'f':
args->uhd_rx_freq = atof(argv[optind]);
break;
case 'F':
args->uhd_tx_freq = atof(argv[optind]);
break;
case 'n':
args->nof_subframes = atoi(argv[optind]);
break;
case 'r':
args->rnti = atoi(argv[optind]);
break;
case 'l':
args->force_N_id_2 = atoi(argv[optind]);
break;
case 'v':
verbose++;
break;
default:
usage(args, argv[0]);
exit(-1);
}
}
if (args->uhd_tx_freq < 0 && args->uhd_rx_freq < 0) {
usage(args, argv[0]);
exit(-1);
}
}
/**********************************************************************/
/* TODO: Do something with the output data */
uint8_t data[20000], data_packed[20000];
bool go_exit = false;
void sig_int_handler(int signo)
{
if (signo == SIGINT) {
go_exit = true;
}
}
int cuhd_recv_wrapper_timed(void *h, void *data, uint32_t nsamples, timestamp_t *uhd_time) {
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
return cuhd_recv_timed(h, data, nsamples, 1, &uhd_time->full_secs, &uhd_time->frac_secs);
}
extern float mean_exec_time;
enum receiver_state { DECODE_MIB, SEND_PRACH} state;
#define NOF_PRACH_SEQUENCES 52
ue_dl_t ue_dl;
ue_sync_t ue_sync;
prach_t prach;
cf_t *prach_buffers[NOF_PRACH_SEQUENCES];
int prach_buffer_len;
prog_args_t prog_args;
uint32_t sfn = 0; // system frame number
cf_t *sf_buffer = NULL;
int generate_prach_sequences(){
for(int i=0;i<NOF_PRACH_SEQUENCES;i++){
if(prach_gen(&prach, i, 2, prach_buffers[i])){
fprintf(stderr, "Error generating prach sequence\n");
return -1;
}
}
return 0;
}
int main(int argc, char **argv) {
int ret;
lte_cell_t cell;
int64_t sf_cnt;
ue_mib_t ue_mib;
void *uhd;
int n;
uint8_t bch_payload[BCH_PAYLOAD_LEN], bch_payload_unpacked[BCH_PAYLOAD_LEN];
uint32_t sfn_offset;
parse_args(&prog_args, argc, argv);
printf("Opening UHD device...\n");
if (cuhd_open(prog_args.uhd_args, &uhd)) {
fprintf(stderr, "Error opening uhd\n");
exit(-1);
}
/* Set receiver gain */
cuhd_set_rx_gain(uhd, prog_args.uhd_gain);
cuhd_set_tx_gain(uhd, prog_args.uhd_gain);
cuhd_set_tx_antenna(uhd, "TX/RX");
/* set receiver frequency */
cuhd_set_rx_freq(uhd, (double) prog_args.uhd_rx_freq);
cuhd_rx_wait_lo_locked(uhd);
printf("Tunning RX receiver to %.3f MHz\n", (double ) prog_args.uhd_rx_freq/1000000);
cuhd_set_tx_freq(uhd, prog_args.uhd_tx_freq);
cuhd_set_tx_freq_offset(uhd, prog_args.uhd_tx_freq, prog_args.uhd_tx_freq_offset);
printf("Tunning TX receiver to %.3f MHz\n", (double ) prog_args.uhd_tx_freq/1000000);
ret = cuhd_search_and_decode_mib(uhd, &cell_detect_config, prog_args.force_N_id_2, &cell);
if (ret < 0) {
fprintf(stderr, "Error searching for cell\n");
exit(-1);
} else if (ret == 0) {
printf("Cell not found\n");
exit(0);
}
/* set sampling frequency */
int srate = lte_sampling_freq_hz(cell.nof_prb);
if (srate != -1) {
cuhd_set_rx_srate(uhd, (double) srate);
cuhd_set_tx_srate(uhd, (double) srate);
} else {
fprintf(stderr, "Invalid number of PRB %d\n", cell.nof_prb);
return LIBLTE_ERROR;
}
INFO("Stopping UHD and flushing buffer...\r",0);
cuhd_stop_rx_stream(uhd);
cuhd_flush_buffer(uhd);
if (ue_mib_init(&ue_mib, cell)) {
fprintf(stderr, "Error initaiting UE MIB decoder\n");
exit(-1);
}
if (prach_init(&prach, lte_symbol_sz(cell.nof_prb), 0, 0, false, 1)) {
fprintf(stderr, "Error initializing PRACH\n");
return -1;
}
prach_buffer_len = prach.N_seq + prach.N_cp;
for(int i=0;i<NOF_PRACH_SEQUENCES;i++){
prach_buffers[i] = (cf_t*)malloc(prach_buffer_len*sizeof(cf_t));
if(!prach_buffers[i]) {
perror("maloc");
return -1;
}
}
generate_prach_sequences();
state = DECODE_MIB;
if (ue_sync_init(&ue_sync, cell, cuhd_recv_wrapper_timed, uhd)) {
fprintf(stderr, "Error initiating ue_sync\n");
exit(-1);
}
if (ue_dl_init(&ue_dl, cell)) { // This is the User RNTI
fprintf(stderr, "Error initiating UE downlink processing module\n");
exit(-1);
}
ue_dl_set_user_rnti(&ue_dl, prog_args.rnti==SIRNTI?1:prog_args.rnti);
/* Configure downlink receiver for the SI-RNTI since will be the only one we'll use */
ue_dl_set_rnti(&ue_dl, prog_args.rnti);
/* Initialize subframe counter */
sf_cnt = 0;
// Register Ctrl+C handler
signal(SIGINT, sig_int_handler);
cuhd_start_rx_stream(uhd);
timestamp_t uhd_time;
timestamp_t next_frame_time;
timestamp_t next_prach_time;
/* Main loop */
while (!go_exit && (sf_cnt < prog_args.nof_subframes || prog_args.nof_subframes == -1)) {
ret = ue_sync_get_buffer(&ue_sync, &sf_buffer);
if (ret < 0) {
fprintf(stderr, "Error calling ue_sync_work()\n");
}
/* ue_sync_get_buffer returns 1 if successfully read 1 aligned subframe */
if (ret == 1) {
switch (state) {
case DECODE_MIB:
if (ue_sync_get_sfidx(&ue_sync) == 0) {
pbch_decode_reset(&ue_mib.pbch);
n = ue_mib_decode(&ue_mib, sf_buffer, bch_payload_unpacked, NULL, &sfn_offset);
if (n < 0) {
fprintf(stderr, "Error decoding UE MIB\n");
exit(-1);
} else if (n == MIB_FOUND) {
bit_unpack_vector(bch_payload_unpacked, bch_payload, BCH_PAYLOAD_LEN);
bcch_bch_unpack(bch_payload, BCH_PAYLOAD_LEN, &cell, &sfn);
printf("Decoded MIB. SFN: %d, offset: %d\n", sfn, sfn_offset);
sfn = (sfn + sfn_offset)%1024;
state = SEND_PRACH;
}
}
break;
case SEND_PRACH:
ue_sync_get_last_timestamp(&ue_sync, &uhd_time);
timestamp_init(&next_frame_time, uhd_time.full_secs, uhd_time.frac_secs);
printf("Last frame time = %.6f\n", timestamp_real(&next_frame_time));
//Tx PRACH every 10 lte dl frames
printf("TX PRACH\n");
timestamp_copy(&next_prach_time, &next_frame_time);
timestamp_add(&next_prach_time, 0, prach_time_offset);
cuhd_send_timed(uhd, prach_buffers[7], prach_buffer_len, 0,
next_prach_time.full_secs, next_prach_time.frac_secs);
break;
}
if (ue_sync_get_sfidx(&ue_sync) == 9) {
sfn++;
if (sfn == 1024) {
sfn = 0;
}
}
} else if (ret == 0) {
printf("Finding PSS... Peak: %8.1f, FrameCnt: %d, State: %d\r",
sync_get_peak_value(&ue_sync.sfind),
ue_sync.frame_total_cnt, ue_sync.state);
}
sf_cnt++;
} // Main loop
ue_dl_free(&ue_dl);
ue_sync_free(&ue_sync);
ue_mib_free(&ue_mib);
cuhd_close(uhd);
printf("\nBye\n");
exit(0);
}

View File

@ -0,0 +1,53 @@
/**
*
* \section COPYRIGHT
*
* Copyright 2013-2014 The libLTE Developers. See the
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
* This file is part of the libLTE library.
*
* 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,
* 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.
*
* 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/.
*
*/
#ifndef TIMESTAMP_
#define TIMESTAMP_
#include <time.h>
#include "liblte/config.h"
/*!
* A simple timestamp struct with separate variables for full and frac seconds.
*
* Separate variables are used to avoid loss of precision in our frac seconds.
* Only positive timestamps are supported.
*/
typedef struct LIBLTE_API{
time_t full_secs;
double frac_secs;
}timestamp_t;
LIBLTE_API int timestamp_init(timestamp_t *t, time_t full_secs, double frac_secs);
LIBLTE_API int timestamp_copy(timestamp_t *dest, timestamp_t *src);
LIBLTE_API int timestamp_add(timestamp_t *t, time_t full_secs, double frac_secs);
LIBLTE_API int timestamp_sub(timestamp_t *t, time_t full_secs, double frac_secs);
LIBLTE_API double timestamp_real(timestamp_t *t);
#endif // TIMESTAMP_

View File

@ -99,4 +99,17 @@ LIBLTE_API int pbch_encode(pbch_t *q,
LIBLTE_API void pbch_decode_reset(pbch_t *q);
LIBLTE_API void pbch_mib_unpack(uint8_t *msg,
lte_cell_t *cell,
uint32_t *sfn);
LIBLTE_API void pbch_mib_pack(lte_cell_t *cell,
uint32_t sfn,
uint8_t *msg);
LIBLTE_API void pbch_mib_fprint(FILE *stream,
lte_cell_t *cell,
uint32_t sfn,
uint32_t cell_id);
#endif // PBCH_

View File

@ -48,6 +48,8 @@
#include "liblte/phy/utils/pack.h"
#include "liblte/phy/utils/vector.h"
#include "liblte/phy/common/timestamp.h"
#include "liblte/phy/common/sequence.h"
#include "liblte/phy/common/phy_common.h"
#include "liblte/phy/common/fft.h"

View File

@ -86,12 +86,12 @@ typedef struct LIBLTE_API {
LIBLTE_API int ue_cell_search_init(ue_cell_search_t *q,
int (recv_callback)(void*, void*, uint32_t),
int (recv_callback)(void*, void*, uint32_t,timestamp_t*),
void *stream_handler);
LIBLTE_API int ue_cell_search_init_max(ue_cell_search_t *q,
uint32_t max_frames_total,
int (recv_callback)(void*, void*, uint32_t),
int (recv_callback)(void*, void*, uint32_t,timestamp_t*),
void *stream_handler);
LIBLTE_API void ue_cell_search_free(ue_cell_search_t *q);

View File

@ -81,8 +81,7 @@ typedef struct LIBLTE_API {
/* This function shall be called just after the initial synchronization */
LIBLTE_API int ue_dl_init(ue_dl_t *q,
lte_cell_t cell,
uint16_t user_rnti);
lte_cell_t cell);
LIBLTE_API void ue_dl_free(ue_dl_t *q);
@ -102,4 +101,7 @@ LIBLTE_API void ue_dl_reset(ue_dl_t *q);
LIBLTE_API void ue_dl_set_rnti(ue_dl_t *q,
uint16_t rnti);
LIBLTE_API void ue_dl_set_user_rnti(ue_dl_t *q,
uint16_t user_rnti);
#endif

View File

@ -104,7 +104,7 @@ typedef struct {
LIBLTE_API int ue_mib_sync_init(ue_mib_sync_t *q,
uint32_t cell_id,
lte_cp_t cp,
int (recv_callback)(void*, void*, uint32_t),
int (recv_callback)(void*, void*, uint32_t, timestamp_t *),
void *stream_handler);
LIBLTE_API void ue_mib_sync_free(ue_mib_sync_t *q);

View File

@ -36,6 +36,7 @@
#include "liblte/phy/ch_estimation/chest_dl.h"
#include "liblte/phy/phch/pbch.h"
#include "liblte/phy/common/fft.h"
#include "liblte/phy/common/timestamp.h"
#include "liblte/phy/io/filesource.h"
/**************************************************************
@ -64,8 +65,9 @@ typedef struct LIBLTE_API {
sync_t strack;
void *stream;
int (*recv_callback)(void*, void*, uint32_t);
int (*recv_callback)(void*, void*, uint32_t, timestamp_t*);
timestamp_t last_timestamp;
filesource_t file_source;
bool file_mode;
@ -105,7 +107,7 @@ typedef struct LIBLTE_API {
LIBLTE_API int ue_sync_init(ue_sync_t *q,
lte_cell_t cell,
int (recv_callback)(void*, void*, uint32_t),
int (recv_callback)(void*, void*, uint32_t, timestamp_t*),
void *stream_handler);
LIBLTE_API int ue_sync_init_file(ue_sync_t *q,
@ -135,6 +137,8 @@ LIBLTE_API float ue_sync_get_cfo(ue_sync_t *q);
LIBLTE_API float ue_sync_get_sfo(ue_sync_t *q);
LIBLTE_API void ue_sync_get_last_timestamp(ue_sync_t *q,
timestamp_t *timestamp);

View File

@ -29,6 +29,10 @@
#ifndef VECTOR_
#define VECTOR_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdint.h>
#include "liblte/config.h"
@ -137,4 +141,8 @@ LIBLTE_API void vec_abs_square_cf(cf_t *x, float *abs_square, uint32_t len);
/* argument of each vector element */
LIBLTE_API void vec_arg_cf(cf_t *x, float *arg, uint32_t len);
#ifdef __cplusplus
}
#endif
#endif // VECTOR_

View File

@ -0,0 +1,82 @@
/**
*
* \section COPYRIGHT
*
* Copyright 2013-2014 The libLTE Developers. See the
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
* This file is part of the libLTE library.
*
* 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,
* 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.
*
* 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/.
*
*/
#include "liblte/phy/common/timestamp.h"
#include "math.h"
int timestamp_init(timestamp_t *t, time_t full_secs, double frac_secs){
int ret = LIBLTE_ERROR;
if(t != NULL && frac_secs >= 0.0){
t->full_secs = full_secs;
t->frac_secs = frac_secs;
ret = LIBLTE_SUCCESS;
}
return ret;
}
int timestamp_copy(timestamp_t *dest, timestamp_t *src){
int ret = LIBLTE_ERROR;
if(dest != NULL && src != NULL){
dest->full_secs = src->full_secs;
dest->frac_secs = src->frac_secs;
ret = LIBLTE_SUCCESS;
}
return ret;
}
int timestamp_add(timestamp_t *t, time_t full_secs, double frac_secs){
int ret = LIBLTE_ERROR;
if(t != NULL && frac_secs >= 0.0){
t->frac_secs += frac_secs;
t->full_secs += full_secs;
double r = floor(t->frac_secs);
t->full_secs += r;
t->frac_secs -= r;
ret = LIBLTE_SUCCESS;
}
return ret;
}
int timestamp_sub(timestamp_t *t, time_t full_secs, double frac_secs){
int ret = LIBLTE_ERROR;
if(t != NULL && frac_secs >= 0.0){
t->frac_secs -= frac_secs;
t->full_secs -= full_secs;
if(t->frac_secs < 0){
t->frac_secs = 1-t->frac_secs;
t->full_secs--;
}
if(t->full_secs < 0)
return LIBLTE_ERROR;
ret = LIBLTE_SUCCESS;
}
return ret;
}
double timestamp_real(timestamp_t *t){
return t->frac_secs + t->full_secs;
}

View File

@ -241,6 +241,116 @@ void pbch_free(pbch_t *q) {
}
/** Unpacks MIB from PBCH message.
* msg buffer must be 24 byte length at least
*/
void pbch_mib_unpack(uint8_t *msg, lte_cell_t *cell, uint32_t *sfn) {
int bw, phich_res;
bw = bit_unpack(&msg, 3);
switch (bw) {
case 0:
cell->nof_prb = 6;
break;
case 1:
cell->nof_prb = 15;
break;
default:
cell->nof_prb = (bw - 1) * 25;
break;
}
if (*msg) {
cell->phich_length = PHICH_EXT;
} else {
cell->phich_length = PHICH_NORM;
}
msg++;
phich_res = bit_unpack(&msg, 2);
switch (phich_res) {
case 0:
cell->phich_resources = R_1_6;
break;
case 1:
cell->phich_resources = R_1_2;
break;
case 2:
cell->phich_resources = R_1;
break;
case 3:
cell->phich_resources = R_2;
break;
}
if (sfn) {
*sfn = bit_unpack(&msg, 8) << 2;
}
}
/** Unpacks MIB from PBCH message.
* msg buffer must be 24 byte length at least
*/
void pbch_mib_pack(lte_cell_t *cell, uint32_t sfn, uint8_t *msg) {
int bw, phich_res = 0;
bzero(msg, 24);
if (cell->nof_prb <= 6) {
bw = 0;
} else if (cell->nof_prb <= 15) {
bw = 1;
} else {
bw = 1 + cell->nof_prb / 25;
}
bit_pack(bw, &msg, 3);
*msg = cell->phich_length == PHICH_EXT;
msg++;
switch (cell->phich_resources) {
case R_1_6:
phich_res = 0;
break;
case R_1_2:
phich_res = 1;
break;
case R_1:
phich_res = 2;
break;
case R_2:
phich_res = 3;
break;
}
bit_pack(phich_res, &msg, 2);
bit_pack(sfn >> 2, &msg, 8);
}
void pbch_mib_fprint(FILE *stream, lte_cell_t *cell, uint32_t sfn, uint32_t cell_id) {
printf(" - Cell ID: %d\n", cell_id);
printf(" - Nof ports: %d\n", cell->nof_ports);
printf(" - PRB: %d\n", cell->nof_prb);
printf(" - PHICH Length: %s\n",
cell->phich_length == PHICH_EXT ? "Extended" : "Normal");
printf(" - PHICH Resources: ");
switch (cell->phich_resources) {
case R_1_6:
printf("1/6");
break;
case R_1_2:
printf("1/2");
break;
case R_1:
printf("1");
break;
case R_2:
printf("2");
break;
}
printf("\n");
printf(" - SFN: %d\n", sfn);
}
void pbch_decode_reset(pbch_t *q) {
q->frame_idx = 0;
}

View File

@ -39,13 +39,13 @@
float tmp_pss_corr[32*10000];
float tmp_sss_corr[31*10000];
int ue_cell_search_init(ue_cell_search_t * q, int (recv_callback)(void*, void*, uint32_t), void *stream_handler)
int ue_cell_search_init(ue_cell_search_t * q, int (recv_callback)(void*, void*, uint32_t,timestamp_t*), void *stream_handler)
{
return ue_cell_search_init_max(q, CS_DEFAULT_MAXFRAMES_TOTAL, recv_callback, stream_handler);
}
int ue_cell_search_init_max(ue_cell_search_t * q, uint32_t max_frames,
int (recv_callback)(void*, void*, uint32_t), void *stream_handler)
int (recv_callback)(void*, void*, uint32_t,timestamp_t*), void *stream_handler)
{
int ret = LIBLTE_ERROR_INVALID_INPUTS;
@ -270,9 +270,6 @@ int ue_cell_search_scan_N_id_2(ue_cell_search_t * q, uint32_t N_id_2, ue_cell_se
ret = 1; // A cell has been found.
if (found_cell) {
get_cell(q, nof_detected_frames, found_cell);
printf("Found CELL PHYID: %d, CP: %s, PSR: %.1f, Absolute Peak: %.1f dBm, Reliability: %.0f \%\n",
found_cell->cell_id, lte_cp_string(found_cell->cp),
found_cell->psr, 10*log10(found_cell->peak*1000), 100*found_cell->mode);
}
} else {
ret = 0; // A cell was not found.

View File

@ -39,8 +39,7 @@
#define MAX_CANDIDATES 64
int ue_dl_init(ue_dl_t *q,
lte_cell_t cell,
uint16_t user_rnti)
lte_cell_t cell)
{
int ret = LIBLTE_ERROR_INVALID_INPUTS;
@ -52,7 +51,7 @@ int ue_dl_init(ue_dl_t *q,
bzero(q, sizeof(ue_dl_t));
q->cell = cell;
q->user_rnti = user_rnti;
q->user_rnti = 0;
q->pkt_errors = 0;
q->pkts_total = 0;
@ -144,6 +143,10 @@ void ue_dl_free(ue_dl_t *q) {
}
}
void ue_dl_set_user_rnti(ue_dl_t *q, uint16_t user_rnti) {
q->user_rnti = user_rnti;
}
void ue_dl_set_rnti(ue_dl_t *q, uint16_t rnti) {
q->current_rnti = rnti;
pdsch_set_rnti(&q->pdsch, rnti);
@ -217,10 +220,6 @@ int ue_dl_decode_sib(ue_dl_t *q, cf_t *input, uint8_t *data, uint32_t sf_idx, ui
nof_locations = pdcch_ue_locations(&q->pdcch, locations, MAX_CANDIDATES, sf_idx, cfi, q->current_rnti);
formats = ue_formats;
nof_formats = nof_ue_formats;
if (q->current_rnti == 1234) {
nof_locations = 1;
nof_formats = 1;
}
}
/* Extract all PDCCH symbols and get LLRs */

View File

@ -159,7 +159,7 @@ int ue_mib_decode(ue_mib_t * q, cf_t *input,
int ue_mib_sync_init(ue_mib_sync_t *q,
uint32_t cell_id,
lte_cp_t cp,
int (recv_callback)(void*, void*, uint32_t),
int (recv_callback)(void*, void*, uint32_t, timestamp_t*),
void *stream_handler)
{
lte_cell_t cell;

View File

@ -82,7 +82,7 @@ clean_exit:
int ue_sync_init(ue_sync_t *q,
lte_cell_t cell,
int (recv_callback)(void*, void*, uint32_t),
int (recv_callback)(void*, void*, uint32_t,timestamp_t*),
void *stream_handler)
{
int ret = LIBLTE_ERROR_INVALID_INPUTS;
@ -201,6 +201,10 @@ void ue_sync_free(ue_sync_t *q) {
bzero(q, sizeof(ue_sync_t));
}
void ue_sync_get_last_timestamp(ue_sync_t *q, timestamp_t *timestamp) {
memcpy(timestamp, &q->last_timestamp, sizeof(timestamp_t));
}
uint32_t ue_sync_peak_idx(ue_sync_t *q) {
return q->peak_idx;
}
@ -250,7 +254,7 @@ static int find_peak_ok(ue_sync_t *q) {
if (q->frame_find_cnt >= q->nof_avg_find_frames || q->peak_idx < 2*q->fft_size) {
INFO("Realigning frame, reading %d samples\n", q->peak_idx+q->sf_len/2);
/* Receive the rest of the subframe so that we are subframe aligned*/
if (q->recv_callback(q->stream, q->input_buffer, q->peak_idx+q->sf_len/2) < 0) {
if (q->recv_callback(q->stream, q->input_buffer, q->peak_idx+q->sf_len/2, &q->last_timestamp) < 0) {
return LIBLTE_ERROR;
}
@ -297,7 +301,7 @@ static int track_peak_ok(ue_sync_t *q, uint32_t track_idx) {
discard the offseted samples to align next frame */
if (q->time_offset > 0 && q->time_offset < MAX_TIME_OFFSET) {
INFO("\nPositive time offset %d samples. Mean time offset %f.\n", q->time_offset, q->mean_time_offset);
if (q->recv_callback(q->stream, dummy, (uint32_t) q->time_offset) < 0) {
if (q->recv_callback(q->stream, dummy, (uint32_t) q->time_offset, &q->last_timestamp) < 0) {
fprintf(stderr, "Error receiving from USRP\n");
return LIBLTE_ERROR;
}
@ -337,7 +341,7 @@ static int receive_samples(ue_sync_t *q) {
}
/* Get N subframes from the USRP getting more samples and keeping the previous samples, if any */
if (q->recv_callback(q->stream, &q->input_buffer[q->time_offset], q->frame_len - q->time_offset) < 0) {
if (q->recv_callback(q->stream, &q->input_buffer[q->time_offset], q->frame_len - q->time_offset, &q->last_timestamp) < 0) {
return LIBLTE_ERROR;
}