2014-12-11 15:04:21 -08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* \section COPYRIGHT
|
|
|
|
*
|
2015-03-18 05:31:13 -07:00
|
|
|
* Copyright 2013-2014 The srsLTE Developers. See the
|
2014-12-11 15:04:21 -08:00
|
|
|
* COPYRIGHT file at the top-level directory of this distribution.
|
|
|
|
*
|
|
|
|
* \section LICENSE
|
|
|
|
*
|
2015-03-18 05:31:13 -07:00
|
|
|
* This file is part of the srsLTE library.
|
2014-12-11 15:04:21 -08:00
|
|
|
*
|
2015-03-18 05:31:13 -07:00
|
|
|
* srsLTE is free software: you can redistribute it and/or modify
|
2014-12-11 15:04:21 -08:00
|
|
|
* 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.
|
|
|
|
*
|
2015-03-18 05:31:13 -07:00
|
|
|
* srsLTE is distributed in the hope that it will be useful,
|
2014-12-11 15:04:21 -08:00
|
|
|
* 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>
|
|
|
|
|
2015-03-18 05:31:13 -07:00
|
|
|
#include "srslte/srslte.h"
|
2014-12-11 15:04:21 -08:00
|
|
|
#include "cuhd_utils.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef DISABLE_UHD
|
2015-03-18 05:16:28 -07:00
|
|
|
#include "srslte/cuhd/cuhd.h"
|
2014-12-11 15:04:21 -08:00
|
|
|
|
2015-03-18 08:05:38 -07:00
|
|
|
int cuhd_recv_wrapper_cs(void *h, void *data, uint32_t nsamples, srslte_timestamp_t *t) {
|
2014-12-11 15:04:21 -08:00
|
|
|
DEBUG(" ---- Receive %d samples ---- \n", nsamples);
|
|
|
|
return cuhd_recv(h, data, nsamples, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function is simply a wrapper to the ue_cell_search module for cuhd devices
|
|
|
|
* Return 1 if the MIB is decoded, 0 if not or -1 on error.
|
|
|
|
*/
|
2015-03-18 05:41:50 -07:00
|
|
|
int cuhd_mib_decoder(void *uhd, uint32_t max_nof_frames, srslte_cell_t *cell) {
|
2015-03-18 05:31:13 -07:00
|
|
|
int ret = SRSLTE_ERROR;
|
2015-03-18 11:14:24 -07:00
|
|
|
srslte_ue_mib_sync_t ue_mib;
|
2015-04-08 11:47:52 -07:00
|
|
|
uint8_t bch_payload[SRSLTE_BCH_PAYLOAD_LEN];
|
2014-12-11 15:04:21 -08:00
|
|
|
|
2015-03-18 11:14:24 -07:00
|
|
|
if (srslte_ue_mib_sync_init(&ue_mib, cell->id, cell->cp, cuhd_recv_wrapper_cs, uhd)) {
|
|
|
|
fprintf(stderr, "Error initiating srslte_ue_mib_sync\n");
|
2014-12-11 15:04:21 -08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:14:24 -07:00
|
|
|
int srate = srslte_sampling_freq_hz(SRSLTE_UE_MIB_NOF_PRB);
|
2014-12-11 15:04:21 -08:00
|
|
|
INFO("Setting sampling frequency %.2f MHz for PSS search\n", (float) srate/1000000);
|
|
|
|
cuhd_set_rx_srate(uhd, (float) srate);
|
|
|
|
|
|
|
|
INFO("Starting receiver...\n", 0);
|
|
|
|
cuhd_start_rx_stream(uhd);
|
|
|
|
|
|
|
|
/* Find and decody MIB */
|
2015-03-18 11:14:24 -07:00
|
|
|
ret = srslte_ue_mib_sync_decode(&ue_mib, max_nof_frames, bch_payload, &cell->nof_ports, NULL);
|
2014-12-11 15:04:21 -08:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error decoding MIB\n");
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
if (ret == 1) {
|
2015-03-19 09:38:52 -07:00
|
|
|
srslte_pbch_mib_unpack(bch_payload, cell, NULL);
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
clean_exit:
|
|
|
|
|
|
|
|
cuhd_stop_rx_stream(uhd);
|
2015-03-18 11:14:24 -07:00
|
|
|
srslte_ue_mib_sync_free(&ue_mib);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This function is simply a wrapper to the ue_cell_search module for cuhd devices
|
|
|
|
*/
|
|
|
|
int cuhd_cell_search(void *uhd, cell_search_cfg_t *config,
|
2015-03-18 05:41:50 -07:00
|
|
|
int force_N_id_2, srslte_cell_t *cell)
|
2014-12-11 15:04:21 -08:00
|
|
|
{
|
2015-03-18 05:31:13 -07:00
|
|
|
int ret = SRSLTE_ERROR;
|
2015-03-18 11:14:24 -07:00
|
|
|
srslte_ue_cellsearch_t cs;
|
|
|
|
srslte_ue_cellsearch_result_t found_cells[3];
|
2014-12-11 15:04:21 -08:00
|
|
|
|
2015-03-18 11:14:24 -07:00
|
|
|
bzero(found_cells, 3*sizeof(srslte_ue_cellsearch_result_t));
|
2014-12-11 15:04:21 -08:00
|
|
|
|
2015-03-18 11:14:24 -07:00
|
|
|
if (srslte_ue_cellsearch_init(&cs, cuhd_recv_wrapper_cs, uhd)) {
|
2014-12-11 15:04:21 -08:00
|
|
|
fprintf(stderr, "Error initiating UE cell detect\n");
|
2015-03-18 05:31:13 -07:00
|
|
|
return SRSLTE_ERROR;
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
|
2014-12-15 05:43:54 -08:00
|
|
|
if (config->max_frames_pss) {
|
2015-03-18 11:14:24 -07:00
|
|
|
srslte_ue_cellsearch_set_nof_frames_to_scan(&cs, config->max_frames_pss);
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
if (config->threshold) {
|
2015-03-18 11:14:24 -07:00
|
|
|
srslte_ue_cellsearch_set_threshold(&cs, config->threshold);
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
|
2015-03-18 11:14:24 -07:00
|
|
|
INFO("Setting sampling frequency %.2f MHz for PSS search\n", SRSLTE_CS_SAMP_FREQ/1000000);
|
|
|
|
cuhd_set_rx_srate(uhd, SRSLTE_CS_SAMP_FREQ);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
INFO("Starting receiver...\n", 0);
|
|
|
|
cuhd_start_rx_stream(uhd);
|
|
|
|
|
|
|
|
/* Find a cell in the given N_id_2 or go through the 3 of them to find the strongest */
|
|
|
|
uint32_t max_peak_cell = 0;
|
|
|
|
if (force_N_id_2 >= 0) {
|
2015-03-18 11:14:24 -07:00
|
|
|
ret = srslte_ue_cellsearch_scan_N_id_2(&cs, force_N_id_2, &found_cells[force_N_id_2]);
|
2014-12-11 15:04:21 -08:00
|
|
|
max_peak_cell = force_N_id_2;
|
|
|
|
} else {
|
2015-03-18 11:14:24 -07:00
|
|
|
ret = srslte_ue_cellsearch_scan(&cs, found_cells, &max_peak_cell);
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error searching cell\n");
|
2015-03-18 05:31:13 -07:00
|
|
|
return SRSLTE_ERROR;
|
2014-12-11 15:04:21 -08:00
|
|
|
} else if (ret == 0) {
|
|
|
|
fprintf(stderr, "Could not find any cell in this frequency\n");
|
2015-03-18 05:31:13 -07:00
|
|
|
return SRSLTE_SUCCESS;
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save result
|
|
|
|
if (cell) {
|
|
|
|
cell->id = found_cells[max_peak_cell].cell_id;
|
|
|
|
cell->cp = found_cells[max_peak_cell].cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
cuhd_stop_rx_stream(uhd);
|
2015-03-18 11:14:24 -07:00
|
|
|
srslte_ue_cellsearch_free(&cs);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Finds a cell and decodes MIB from the PBCH.
|
|
|
|
* Returns 1 if the cell is found and MIB is decoded successfully.
|
|
|
|
* 0 if no cell was found or MIB could not be decoded,
|
|
|
|
* -1 on error
|
|
|
|
*/
|
2015-03-18 05:41:50 -07:00
|
|
|
int cuhd_search_and_decode_mib(void *uhd, cell_search_cfg_t *config, int force_N_id_2, srslte_cell_t *cell)
|
2014-12-11 15:04:21 -08:00
|
|
|
{
|
2015-03-18 05:31:13 -07:00
|
|
|
int ret = SRSLTE_ERROR;
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
printf("Searching for cell...\n");
|
|
|
|
ret = cuhd_cell_search(uhd, config, force_N_id_2, cell);
|
|
|
|
if (ret > 0) {
|
|
|
|
printf("Decoding PBCH for cell %d (N_id_2=%d)\n", cell->id, cell->id%3);
|
2014-12-15 05:43:54 -08:00
|
|
|
ret = cuhd_mib_decoder(uhd, config->max_frames_pbch, cell);
|
2014-12-11 15:04:21 -08:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Could not decode PBCH from CELL ID %d\n", cell->id);
|
2015-03-18 05:31:13 -07:00
|
|
|
return SRSLTE_ERROR;
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|