srsLTE/lib/examples/usrp_capture_sync.c

216 lines
5.9 KiB
C
Raw Normal View History

2019-04-26 12:27:38 -07:00
/*
* Copyright 2013-2019 Software Radio Systems Limited
2015-04-05 12:03:01 -07:00
*
2019-04-26 12:27:38 -07:00
* This file is part of srsLTE.
2015-04-05 12:03:01 -07:00
*
* srsLTE is free software: you can redistribute it and/or modify
2015-05-08 08:05:40 -07:00
* it under the terms of the GNU Affero General Public License as
2015-04-05 12:03:01 -07:00
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE 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
2015-05-08 08:05:40 -07:00
* GNU Affero General Public License for more details.
2015-04-05 12:03:01 -07:00
*
2015-05-08 08:05:40 -07:00
* A copy of the GNU Affero General Public License can be found in
2015-04-05 12:03:01 -07:00
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include <math.h>
2015-04-05 12:03:01 -07:00
#include <signal.h>
#include <stdio.h>
2015-04-05 12:03:01 -07:00
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
2015-04-05 12:03:01 -07:00
#include <stdbool.h>
2017-05-18 00:48:24 -07:00
#include "srslte/phy/rf/rf.h"
#include "srslte/srslte.h"
2015-04-05 12:03:01 -07:00
static bool keep_running = true;
char* output_file_name = NULL;
char* rf_args = "";
float rf_gain = 60.0, rf_freq = -1.0;
int nof_prb = 6;
int nof_subframes = -1;
int N_id_2 = -1;
uint32_t nof_rx_antennas = 1;
2015-04-05 12:03:01 -07:00
void int_handler(int dummy)
{
2015-04-05 12:03:01 -07:00
keep_running = false;
}
void usage(char* prog)
{
2015-04-05 12:03:01 -07:00
printf("Usage: %s [agrnv] -l N_id_2 -f rx_frequency_hz -o output_file\n", prog);
printf("\t-a RF args [Default %s]\n", rf_args);
printf("\t-g RF Gain [Default %.2f dB]\n", rf_gain);
2015-04-05 12:03:01 -07:00
printf("\t-p nof_prb [Default %d]\n", nof_prb);
printf("\t-n nof_subframes [Default %d]\n", nof_subframes);
2017-09-05 06:26:36 -07:00
printf("\t-A nof_rx_antennas [Default %d]\n", nof_rx_antennas);
printf("\t-v verbose\n");
2015-04-05 12:03:01 -07:00
}
void parse_args(int argc, char** argv)
{
2015-04-05 12:03:01 -07:00
int opt;
2017-09-05 06:26:36 -07:00
while ((opt = getopt(argc, argv, "agpnvfolA")) != -1) {
2015-04-05 12:03:01 -07:00
switch (opt) {
case 'o':
output_file_name = argv[optind];
break;
case 'a':
rf_args = argv[optind];
break;
case 'g':
rf_gain = strtof(argv[optind], NULL);
break;
case 'p':
nof_prb = (int)strtol(argv[optind], NULL, 10);
break;
case 'f':
rf_freq = strtof(argv[optind], NULL);
break;
case 'n':
nof_subframes = (int)strtol(argv[optind], NULL, 10);
break;
case 'l':
N_id_2 = (int)strtol(argv[optind], NULL, 10);
break;
case 'A':
nof_rx_antennas = (uint32_t)strtol(argv[optind], NULL, 10);
break;
case 'v':
srslte_verbose++;
break;
default:
usage(argv[0]);
exit(-1);
2015-04-05 12:03:01 -07:00
}
}
if (&rf_freq < 0 || N_id_2 == -1 || output_file_name == NULL) {
2015-04-05 12:03:01 -07:00
usage(argv[0]);
exit(-1);
}
}
int srslte_rf_recv_wrapper(void* h, cf_t* data_[SRSLTE_MAX_PORTS], uint32_t nsamples, srslte_timestamp_t* t)
{
2015-04-05 12:03:01 -07:00
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
void* ptr[SRSLTE_MAX_PORTS];
for (int i = 0; i < SRSLTE_MAX_PORTS; i++) {
ptr[i] = data_[i];
}
return srslte_rf_recv_with_time_multi(h, ptr, nsamples, true, NULL, NULL);
2015-04-05 12:03:01 -07:00
}
int main(int argc, char** argv)
{
2019-04-23 01:53:11 -07:00
cf_t* buffer[SRSLTE_MAX_PORTS] = {NULL};
int n;
srslte_rf_t rf;
2015-04-05 12:03:01 -07:00
srslte_filesink_t sink;
srslte_ue_sync_t ue_sync;
srslte_cell_t cell;
2015-04-05 12:03:01 -07:00
signal(SIGINT, int_handler);
parse_args(argc, argv);
2015-04-05 12:03:01 -07:00
srslte_filesink_init(&sink, output_file_name, SRSLTE_COMPLEX_FLOAT_BIN);
printf("Opening RF device...\n");
2017-09-05 06:26:36 -07:00
if (srslte_rf_open_multi(&rf, rf_args, nof_rx_antennas)) {
2019-04-23 01:53:11 -07:00
ERROR("Error opening rf\n");
2015-04-05 12:03:01 -07:00
exit(-1);
}
uint32_t max_num_samples = 3 * SRSLTE_SF_LEN_MAX;
2019-04-23 01:53:11 -07:00
for (int i = 0; i < nof_rx_antennas; i++) {
buffer[i] = srslte_vec_cf_malloc(max_num_samples);
2017-09-05 06:26:36 -07:00
}
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGINT);
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
2019-04-23 01:53:11 -07:00
printf("Set RX freq: %.6f MHz\n", srslte_rf_set_rx_freq(&rf, nof_rx_antennas, rf_freq) / 1000000);
2015-12-16 09:02:25 -08:00
printf("Set RX gain: %.1f dB\n", srslte_rf_set_rx_gain(&rf, rf_gain));
int srate = srslte_sampling_freq_hz(nof_prb);
if (srate != -1) {
printf("Setting sampling rate %.2f MHz\n", (float)srate / 1000000);
float srate_rf = srslte_rf_set_rx_srate(&rf, (double)srate);
if (srate_rf != srate) {
ERROR("Could not set sampling rate\n");
exit(-1);
}
} else {
ERROR("Invalid number of PRB %d\n", nof_prb);
exit(-1);
}
2017-11-23 10:46:34 -08:00
srslte_rf_start_rx_stream(&rf, false);
2015-04-05 12:03:01 -07:00
cell.cp = SRSLTE_CP_NORM;
cell.id = N_id_2;
cell.nof_prb = nof_prb;
cell.nof_ports = 1;
if (srslte_ue_sync_init_multi(
&ue_sync, cell.nof_prb, cell.id == 1000, srslte_rf_recv_wrapper, nof_rx_antennas, (void*)&rf)) {
2015-04-05 12:03:01 -07:00
fprintf(stderr, "Error initiating ue_sync\n");
exit(-1);
2015-04-05 12:03:01 -07:00
}
if (srslte_ue_sync_set_cell(&ue_sync, cell)) {
2019-04-23 01:53:11 -07:00
ERROR("Error initiating ue_sync\n");
exit(-1);
}
uint32_t subframe_count = 0;
bool start_capture = false;
bool stop_capture = false;
while ((subframe_count < nof_subframes || nof_subframes == -1) && !stop_capture) {
n = srslte_ue_sync_zerocopy(&ue_sync, buffer, max_num_samples);
2015-04-05 12:03:01 -07:00
if (n < 0) {
2019-04-23 01:53:11 -07:00
ERROR("Error receiving samples\n");
2015-04-05 12:03:01 -07:00
exit(-1);
}
if (n == 1) {
if (!start_capture) {
if (srslte_ue_sync_get_sfidx(&ue_sync) == 9) {
start_capture = true;
}
} else {
printf("Writing to file %6d subframes...\r", subframe_count);
srslte_filesink_write_multi(&sink, (void**)buffer, SRSLTE_SF_LEN_PRB(nof_prb), nof_rx_antennas);
subframe_count++;
}
}
if (!keep_running) {
if (!start_capture || (start_capture && srslte_ue_sync_get_sfidx(&ue_sync) == 9)) {
stop_capture = true;
2015-04-05 12:03:01 -07:00
}
}
}
2015-04-05 12:03:01 -07:00
srslte_filesink_free(&sink);
2015-12-16 09:02:25 -08:00
srslte_rf_close(&rf);
2015-04-05 12:03:01 -07:00
srslte_ue_sync_free(&ue_sync);
2019-04-23 01:53:11 -07:00
for (int i = 0; i < nof_rx_antennas; i++) {
2017-09-05 06:26:36 -07:00
if (buffer[i]) {
free(buffer[i]);
}
}
printf("\nOk - wrote %d subframes\n", subframe_count);
2015-04-05 12:03:01 -07:00
exit(0);
}