2014-12-11 15:04:21 -08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* \section COPYRIGHT
|
|
|
|
*
|
2015-11-13 04:22:33 -08:00
|
|
|
* Copyright 2013-2015 Software Radio Systems Limited
|
2014-12-11 15:04:21 -08:00
|
|
|
*
|
|
|
|
* \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
|
2015-07-15 09:40:36 -07:00
|
|
|
* it under the terms of the GNU Affero General Public License as
|
2014-12-11 15:04:21 -08:00
|
|
|
* 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
|
2015-07-15 09:40:36 -07:00
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-11 15:04:21 -08:00
|
|
|
*
|
2015-07-15 09:40:36 -07:00
|
|
|
* A copy of the GNU Affero General Public License can be found in
|
2014-12-11 15:04:21 -08:00
|
|
|
* 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
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
#include "srslte/rf/rf.h"
|
|
|
|
#include "srslte/rf/rf_utils.h"
|
2015-05-08 03:33:33 -07:00
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
int rf_rssi_scan(void *uhd, float *freqs, float *rssi, int nof_bands, double fs, int nsamp) {
|
2015-05-08 03:33:33 -07:00
|
|
|
int i, j;
|
|
|
|
int ret = -1;
|
|
|
|
_Complex float *buffer;
|
|
|
|
double f;
|
|
|
|
|
|
|
|
buffer = calloc(nsamp, sizeof(_Complex float));
|
|
|
|
if (!buffer) {
|
|
|
|
goto free_and_exit;
|
|
|
|
}
|
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_set_rx_gain(uhd, 20.0);
|
|
|
|
rf_set_rx_srate(uhd, fs);
|
2015-05-08 03:33:33 -07:00
|
|
|
|
|
|
|
for (i=0;i<nof_bands;i++) {
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_stop_rx_stream(uhd);
|
2015-05-08 03:33:33 -07:00
|
|
|
|
|
|
|
f = (double) freqs[i];
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_set_rx_freq(uhd, f);
|
|
|
|
rf_rx_wait_lo_locked(uhd);
|
2015-05-08 03:33:33 -07:00
|
|
|
usleep(10000);
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_start_rx_stream(uhd);
|
2015-05-08 03:33:33 -07:00
|
|
|
|
|
|
|
/* discard first samples */
|
|
|
|
for (j=0;j<2;j++) {
|
2015-12-07 09:06:56 -08:00
|
|
|
if (rf_recv(uhd, buffer, nsamp, 1) != nsamp) {
|
2015-05-08 03:33:33 -07:00
|
|
|
goto free_and_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rssi[i] = srslte_vec_avg_power_cf(buffer, nsamp);
|
|
|
|
printf("[%3d]: Freq %4.1f Mhz - RSSI: %3.2f dBm\r", i, f/1000000, 10*log10f(rssi[i]) + 30); fflush(stdout);
|
|
|
|
if (SRSLTE_VERBOSE_ISINFO()) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_stop_rx_stream(uhd);
|
2015-05-08 03:33:33 -07:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
free_and_exit:
|
|
|
|
free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-11 15:04:21 -08:00
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
int rf_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);
|
2015-12-07 09:06:56 -08:00
|
|
|
return rf_recv(h, data, nsamples, 1);
|
2014-12-11 15:04:21 -08:00
|
|
|
}
|
|
|
|
|
2015-12-10 02:11:57 -08:00
|
|
|
double rf_set_rx_gain_th_wrapper(void *h, double f) {
|
|
|
|
return rf_set_rx_gain_th((rf_t*) h, f);
|
|
|
|
}
|
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
/** This function is simply a wrapper to the ue_cell_search module for rf devices
|
2014-12-11 15:04:21 -08:00
|
|
|
* Return 1 if the MIB is decoded, 0 if not or -1 on error.
|
|
|
|
*/
|
2015-12-07 09:06:56 -08:00
|
|
|
int rf_mib_decoder(void *uhd, cell_search_cfg_t *config, 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-12-07 09:06:56 -08:00
|
|
|
if (srslte_ue_mib_sync_init(&ue_mib, cell->id, cell->cp, rf_recv_wrapper_cs, uhd)) {
|
2015-03-18 11:14:24 -07:00
|
|
|
fprintf(stderr, "Error initiating srslte_ue_mib_sync\n");
|
2014-12-11 15:04:21 -08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
2015-04-21 12:05:51 -07:00
|
|
|
if (config->init_agc > 0) {
|
2015-12-10 02:11:57 -08:00
|
|
|
srslte_ue_sync_start_agc(&ue_mib.ue_sync, rf_set_rx_gain_th_wrapper, config->init_agc);
|
2015-04-21 02:09:08 -07:00
|
|
|
}
|
2015-04-20 00:25:29 -07:00
|
|
|
|
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);
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_set_rx_srate(uhd, (float) srate);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
INFO("Starting receiver...\n", 0);
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_start_rx_stream(uhd);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
/* Find and decody MIB */
|
2015-04-21 02:09:08 -07:00
|
|
|
ret = srslte_ue_mib_sync_decode(&ue_mib, config->max_frames_pss, 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
|
|
|
}
|
|
|
|
|
2015-04-21 12:05:51 -07:00
|
|
|
// Save AGC value
|
|
|
|
if (config->init_agc > 0) {
|
|
|
|
config->init_agc = srslte_agc_get_gain(&ue_mib.ue_sync.agc);
|
|
|
|
}
|
|
|
|
|
2014-12-11 15:04:21 -08:00
|
|
|
clean_exit:
|
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_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;
|
|
|
|
}
|
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
/** This function is simply a wrapper to the ue_cell_search module for rf devices
|
2014-12-11 15:04:21 -08:00
|
|
|
*/
|
2015-12-07 09:06:56 -08:00
|
|
|
int rf_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-12-07 09:06:56 -08:00
|
|
|
if (srslte_ue_cellsearch_init(&cs, rf_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-04-21 12:05:51 -07:00
|
|
|
if (config->init_agc > 0) {
|
2015-12-10 02:11:57 -08:00
|
|
|
srslte_ue_sync_start_agc(&cs.ue_sync, rf_set_rx_gain_th_wrapper, config->init_agc);
|
2015-04-21 02:09:08 -07:00
|
|
|
}
|
|
|
|
|
2015-03-18 11:14:24 -07:00
|
|
|
INFO("Setting sampling frequency %.2f MHz for PSS search\n", SRSLTE_CS_SAMP_FREQ/1000000);
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_set_rx_srate(uhd, SRSLTE_CS_SAMP_FREQ);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
INFO("Starting receiver...\n", 0);
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_start_rx_stream(uhd);
|
2014-12-11 15:04:21 -08:00
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2015-04-21 12:05:51 -07:00
|
|
|
for (int i=0;i<3;i++) {
|
|
|
|
if (i == max_peak_cell) {
|
|
|
|
printf("*");
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("Found Cell_id: %3d CP: %s, DetectRatio=%2.0f%% PSR=%.2f, Power=%.1f dBm\n",
|
|
|
|
found_cells[i].cell_id, srslte_cp_string(found_cells[i].cp),
|
|
|
|
found_cells[i].mode*100,
|
|
|
|
found_cells[i].psr, 20*log10(found_cells[i].peak*1000));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-21 12:05:51 -07:00
|
|
|
// Save AGC value for MIB decoding
|
|
|
|
if (config->init_agc > 0) {
|
|
|
|
config->init_agc = srslte_agc_get_gain(&cs.ue_sync.agc);
|
|
|
|
}
|
|
|
|
|
2015-12-07 09:06:56 -08:00
|
|
|
rf_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-12-07 09:06:56 -08:00
|
|
|
int rf_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");
|
2015-12-07 09:06:56 -08:00
|
|
|
ret = rf_cell_search(uhd, config, force_N_id_2, cell);
|
2014-12-11 15:04:21 -08:00
|
|
|
if (ret > 0) {
|
|
|
|
printf("Decoding PBCH for cell %d (N_id_2=%d)\n", cell->id, cell->id%3);
|
2015-12-07 09:06:56 -08:00
|
|
|
ret = rf_mib_decoder(uhd, config, 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;
|
|
|
|
}
|
|
|
|
|