2020-11-26 01:25:22 -08:00
|
|
|
/**
|
2022-04-29 00:28:44 -07:00
|
|
|
* Copyright 2013-2022 Software Radio Systems Limited
|
2020-05-05 10:02:36 -07:00
|
|
|
*
|
2021-04-22 01:59:40 -07:00
|
|
|
* This file is part of srsRAN.
|
2020-05-05 10:02:36 -07:00
|
|
|
*
|
2021-04-22 01:59:40 -07:00
|
|
|
* srsRAN is free software: you can redistribute it and/or modify
|
2021-03-28 14:12:42 -07:00
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
2021-04-22 01:59:40 -07:00
|
|
|
* srsRAN is distributed in the hope that it will be useful,
|
2021-03-28 14:12:42 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2020-05-05 10:02:36 -07:00
|
|
|
*
|
2021-03-28 14:12:42 -07:00
|
|
|
* A copy of the GNU Affero 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/.
|
2020-05-05 10:02:36 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "srsue/hdr/phy/search.h"
|
2021-03-19 03:45:56 -07:00
|
|
|
#include "srsran/common/standard_streams.h"
|
2020-05-05 10:02:36 -07:00
|
|
|
|
|
|
|
#define Error(fmt, ...) \
|
2021-03-19 03:45:56 -07:00
|
|
|
if (SRSRAN_DEBUG_ENABLED) \
|
2021-01-28 08:17:43 -08:00
|
|
|
logger.error(fmt, ##__VA_ARGS__)
|
2020-05-05 10:02:36 -07:00
|
|
|
#define Warning(fmt, ...) \
|
2021-03-19 03:45:56 -07:00
|
|
|
if (SRSRAN_DEBUG_ENABLED) \
|
2021-01-28 08:17:43 -08:00
|
|
|
logger.warning(fmt, ##__VA_ARGS__)
|
2020-05-05 10:02:36 -07:00
|
|
|
#define Info(fmt, ...) \
|
2021-03-19 03:45:56 -07:00
|
|
|
if (SRSRAN_DEBUG_ENABLED) \
|
2021-01-28 08:17:43 -08:00
|
|
|
logger.info(fmt, ##__VA_ARGS__)
|
2020-05-05 10:02:36 -07:00
|
|
|
#define Debug(fmt, ...) \
|
2021-03-19 03:45:56 -07:00
|
|
|
if (SRSRAN_DEBUG_ENABLED) \
|
2021-01-28 08:17:43 -08:00
|
|
|
logger.debug(fmt, ##__VA_ARGS__)
|
2020-05-05 10:02:36 -07:00
|
|
|
|
|
|
|
namespace srsue {
|
|
|
|
|
|
|
|
static int
|
2021-03-19 03:45:56 -07:00
|
|
|
radio_recv_callback(void* obj, cf_t* data[SRSRAN_MAX_CHANNELS], uint32_t nsamples, srsran_timestamp_t* rx_time)
|
2020-05-05 10:02:36 -07:00
|
|
|
{
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran::rf_buffer_t x(data, nsamples);
|
2020-05-19 01:19:36 -07:00
|
|
|
return ((search_callback*)obj)->radio_recv_fnc(x, rx_time);
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
static SRSRAN_AGC_CALLBACK(callback_set_rx_gain)
|
2020-05-05 10:02:36 -07:00
|
|
|
{
|
|
|
|
((search_callback*)h)->set_rx_gain(gain_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
search::~search()
|
|
|
|
{
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_ue_mib_sync_free(&ue_mib_sync);
|
|
|
|
srsran_ue_cellsearch_free(&cs);
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
2021-11-15 21:54:23 -08:00
|
|
|
void search::init(srsran::rf_buffer_t& buffer_, uint32_t nof_rx_channels, search_callback* parent, int force_N_id_2_, int force_N_id_1_)
|
2020-05-05 10:02:36 -07:00
|
|
|
{
|
2021-01-28 08:17:43 -08:00
|
|
|
p = parent;
|
2020-05-05 10:02:36 -07:00
|
|
|
|
|
|
|
buffer = buffer_;
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
if (srsran_ue_cellsearch_init_multi(&cs, 8, radio_recv_callback, nof_rx_channels, parent)) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Error("SYNC: Initiating UE cell search");
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_ue_cellsearch_set_nof_valid_frames(&cs, 4);
|
2020-05-05 10:02:36 -07:00
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
if (srsran_ue_mib_sync_init_multi(&ue_mib_sync, radio_recv_callback, nof_rx_channels, parent)) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Error("SYNC: Initiating UE MIB synchronization");
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set options defined in expert section
|
|
|
|
p->set_ue_sync_opts(&cs.ue_sync, 0);
|
|
|
|
|
2021-04-07 13:06:24 -07:00
|
|
|
force_N_id_2 = force_N_id_2_;
|
2021-11-15 21:54:23 -08:00
|
|
|
force_N_id_1 = force_N_id_1_;
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
2021-05-16 09:18:45 -07:00
|
|
|
void search::set_cp_en(bool enable)
|
|
|
|
{
|
|
|
|
srsran_set_detect_cp(&cs, enable);
|
|
|
|
}
|
|
|
|
|
2020-05-05 10:02:36 -07:00
|
|
|
void search::reset()
|
|
|
|
{
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_ue_sync_reset(&ue_mib_sync.ue_sync);
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
float search::get_last_cfo()
|
|
|
|
{
|
2021-03-19 03:45:56 -07:00
|
|
|
return srsran_ue_sync_get_cfo(&ue_mib_sync.ue_sync);
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void search::set_agc_enable(bool enable)
|
|
|
|
{
|
|
|
|
if (enable) {
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_rf_info_t* rf_info = p->get_radio()->get_info();
|
|
|
|
srsran_ue_sync_start_agc(&ue_mib_sync.ue_sync,
|
2020-05-05 10:02:36 -07:00
|
|
|
callback_set_rx_gain,
|
|
|
|
rf_info->min_rx_gain,
|
|
|
|
rf_info->max_rx_gain,
|
|
|
|
p->get_radio()->get_rx_gain());
|
|
|
|
} else {
|
2021-01-28 08:17:43 -08:00
|
|
|
ERROR("Error stop AGC not implemented");
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
search::ret_code search::run(srsran_cell_t* cell_, std::array<uint8_t, SRSRAN_BCH_PAYLOAD_LEN>& bch_payload)
|
2020-05-05 10:02:36 -07:00
|
|
|
{
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_cell_t new_cell = {};
|
2020-05-05 10:02:36 -07:00
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_ue_cellsearch_result_t found_cells[3];
|
2020-05-05 10:02:36 -07:00
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
bzero(found_cells, 3 * sizeof(srsran_ue_cellsearch_result_t));
|
2020-05-05 10:02:36 -07: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;
|
2021-03-19 03:45:56 -07:00
|
|
|
int ret = SRSRAN_ERROR;
|
2020-05-05 10:02:36 -07:00
|
|
|
|
2021-01-28 08:17:43 -08:00
|
|
|
Info("SYNC: Searching for cell...");
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran::console(".");
|
2020-05-05 10:02:36 -07:00
|
|
|
|
2021-11-30 02:24:26 -08:00
|
|
|
if (force_N_id_2 >= 0 && force_N_id_2 < SRSRAN_NOF_NID_2) {
|
2021-03-19 03:45:56 -07:00
|
|
|
ret = srsran_ue_cellsearch_scan_N_id_2(&cs, force_N_id_2, &found_cells[force_N_id_2]);
|
2020-05-05 10:02:36 -07:00
|
|
|
max_peak_cell = force_N_id_2;
|
|
|
|
} else {
|
2021-03-19 03:45:56 -07:00
|
|
|
ret = srsran_ue_cellsearch_scan(&cs, found_cells, &max_peak_cell);
|
2020-05-05 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Error("SYNC: Error decoding MIB: Error searching PSS");
|
2020-05-05 10:02:36 -07:00
|
|
|
return ERROR;
|
|
|
|
} else if (ret == 0) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Info("SYNC: Could not find any cell in this frequency");
|
2020-05-05 10:02:36 -07:00
|
|
|
return CELL_NOT_FOUND;
|
|
|
|
}
|
2021-11-15 21:54:23 -08:00
|
|
|
|
2021-11-30 02:24:26 -08:00
|
|
|
// In case of forced N_id_1 discard any results with different values
|
2021-11-15 21:54:23 -08:00
|
|
|
if (force_N_id_1 >= 0 && force_N_id_1 < SRSRAN_NOF_NID_1) {
|
2021-11-30 02:24:26 -08:00
|
|
|
/* Note that srsran_ue_cellsearch_scan_N_id_2 only finds the strongest cell for a given N_id_2/PSS within the search
|
|
|
|
* window. A cell with the desired SSS can be occluded by other cells with the same PSS, if their PSS is stronger and
|
|
|
|
* within the same search window.
|
|
|
|
*/
|
2021-11-15 21:54:23 -08:00
|
|
|
bool N_id_1_found = false;
|
2021-11-30 02:24:26 -08:00
|
|
|
if (force_N_id_2 >= 0 && force_N_id_2 < SRSRAN_NOF_NID_2) {
|
|
|
|
// N_id_2 (PSS) was forced, so there is only one search result to check
|
|
|
|
if (found_cells[max_peak_cell].cell_id / SRSRAN_NOF_NID_2 == (uint32_t)force_N_id_1) {
|
2021-11-15 21:54:23 -08:00
|
|
|
N_id_1_found = true;
|
2021-11-30 02:24:26 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Go through the results for all N_id_2 (PSS); pick strongest with matching N_id_1 (SSS)
|
|
|
|
float max_peak_value = -1.0;
|
|
|
|
for (uint32_t N_id_2 = 0; N_id_2 < SRSRAN_NOF_NID_2; N_id_2++) {
|
|
|
|
if (found_cells[N_id_2].cell_id / SRSRAN_NOF_NID_2 == (uint32_t)force_N_id_1) {
|
|
|
|
if (found_cells[N_id_2].peak > max_peak_value) {
|
|
|
|
N_id_1_found = true;
|
|
|
|
max_peak_value = found_cells[N_id_2].peak;
|
|
|
|
max_peak_cell = N_id_2;
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 21:54:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!N_id_1_found) {
|
2021-11-30 02:24:26 -08:00
|
|
|
Info("SYNC: Could not find any cell with preselected SSS (force_N_id_1=%d)", force_N_id_1);
|
2021-11-15 21:54:23 -08:00
|
|
|
return CELL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 10:02:36 -07:00
|
|
|
// Save result
|
|
|
|
new_cell.id = found_cells[max_peak_cell].cell_id;
|
|
|
|
new_cell.cp = found_cells[max_peak_cell].cp;
|
|
|
|
new_cell.frame_type = found_cells[max_peak_cell].frame_type;
|
|
|
|
float cfo = found_cells[max_peak_cell].cfo;
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran::console("\n");
|
2021-01-28 08:17:43 -08:00
|
|
|
Info("SYNC: PSS/SSS detected: Mode=%s, PCI=%d, CFO=%.1f KHz, CP=%s",
|
2020-05-05 10:02:36 -07:00
|
|
|
new_cell.frame_type ? "TDD" : "FDD",
|
|
|
|
new_cell.id,
|
|
|
|
cfo / 1000,
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_cp_string(new_cell.cp));
|
2020-05-05 10:02:36 -07:00
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
if (srsran_ue_mib_sync_set_cell(&ue_mib_sync, new_cell)) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Error("SYNC: Setting UE MIB cell");
|
2020-05-05 10:02:36 -07:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set options defined in expert section
|
|
|
|
p->set_ue_sync_opts(&ue_mib_sync.ue_sync, cfo);
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_ue_sync_reset(&ue_mib_sync.ue_sync);
|
2020-05-05 10:02:36 -07:00
|
|
|
|
|
|
|
/* Find and decode MIB */
|
|
|
|
int sfn_offset;
|
2021-03-19 03:45:56 -07:00
|
|
|
ret = srsran_ue_mib_sync_decode(&ue_mib_sync, 40, bch_payload.data(), &new_cell.nof_ports, &sfn_offset);
|
2020-05-05 10:02:36 -07:00
|
|
|
if (ret == 1) {
|
2021-03-19 03:45:56 -07:00
|
|
|
srsran_pbch_mib_unpack(bch_payload.data(), &new_cell, NULL);
|
2020-05-05 10:02:36 -07:00
|
|
|
// pack MIB and store inplace for PCAP dump
|
2021-03-19 03:45:56 -07:00
|
|
|
std::array<uint8_t, SRSRAN_BCH_PAYLOAD_LEN / 8> mib_packed;
|
|
|
|
srsran_bit_pack_vector(bch_payload.data(), mib_packed.data(), SRSRAN_BCH_PAYLOAD_LEN);
|
2020-05-05 10:02:36 -07:00
|
|
|
std::copy(std::begin(mib_packed), std::end(mib_packed), std::begin(bch_payload));
|
|
|
|
|
|
|
|
fprintf(stdout,
|
2021-05-16 09:18:45 -07:00
|
|
|
"Found Cell: Mode=%s, PCI=%d, PRB=%d, Ports=%d, CP=%s, CFO=%.1f KHz\n",
|
2020-05-05 10:02:36 -07:00
|
|
|
new_cell.frame_type ? "TDD" : "FDD",
|
|
|
|
new_cell.id,
|
|
|
|
new_cell.nof_prb,
|
|
|
|
new_cell.nof_ports,
|
2021-05-21 01:29:41 -07:00
|
|
|
new_cell.cp ? "Extended" : "Normal",
|
2020-05-05 10:02:36 -07:00
|
|
|
cfo / 1000);
|
|
|
|
|
2021-01-28 08:17:43 -08:00
|
|
|
Info("SYNC: MIB Decoded: Mode=%s, PCI=%d, PRB=%d, Ports=%d, CFO=%.1f KHz",
|
2020-05-05 10:02:36 -07:00
|
|
|
new_cell.frame_type ? "TDD" : "FDD",
|
|
|
|
new_cell.id,
|
|
|
|
new_cell.nof_prb,
|
|
|
|
new_cell.nof_ports,
|
|
|
|
cfo / 1000);
|
|
|
|
|
2021-03-19 03:45:56 -07:00
|
|
|
if (!srsran_cell_isvalid(&new_cell)) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Error("SYNC: Detected invalid cell.");
|
2020-05-05 10:02:36 -07:00
|
|
|
return CELL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save cell pointer
|
|
|
|
if (cell_) {
|
|
|
|
*cell_ = new_cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CELL_FOUND;
|
|
|
|
} else if (ret == 0) {
|
2021-01-28 08:17:43 -08:00
|
|
|
Warning("SYNC: Found PSS but could not decode PBCH");
|
2020-05-05 10:02:36 -07:00
|
|
|
return CELL_NOT_FOUND;
|
|
|
|
} else {
|
2021-01-28 08:17:43 -08:00
|
|
|
Error("SYNC: Receiving MIB");
|
2020-05-05 10:02:36 -07:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 02:24:26 -08:00
|
|
|
}; // namespace srsue
|