diff --git a/lib/src/phy/sync/refsignal_dl_sync.c b/lib/src/phy/sync/refsignal_dl_sync.c index c9503182e..628ff8863 100644 --- a/lib/src/phy/sync/refsignal_dl_sync.c +++ b/lib/src/phy/sync/refsignal_dl_sync.c @@ -30,19 +30,19 @@ * Constants * -------------- * These constants have been optimized for passing scell_search_test for a number of scenarios. - * - 6 PRB. 6 cells distributed uniformly 10ms delay + * - 6 PRB. 6 cells distributed uniformly 10ms delay. Brut-forced search. * srsue/test/phy/scell_search_test --duration=5 --cell.nof_prb=6 --active_cell_list=all - * --simulation_cell_list=1,2,3,4,5,6 --channel_period_s=30 --channel.hst.fd=750 --channel.delay_max=1000 + * --simulation_cell_list=1,2,3,4,5,6 --channel_period_s=30 --channel.hst.fd=750 --channel.delay_max=10000 * - * - 6 PRB. 6 cells distributed uniformly 10ms delay + * - 6 PRB. 6 cells distributed uniformly 10ms delay. With PCI set list. * srsue/test/phy/scell_search_test --duration=30 --cell.nof_prb=6 --active_cell_list=2,3,4,5,6 * --simulation_cell_list=1,2,3,4,5,6 --channel_period_s=30 --channel.hst.fd=750 --channel.delay_max=10000 * - * - 6 PRB. 6 cell PSS, SSS overlapped: + * - 6 PRB. 6 cell PSS, SSS overlapped. Brut-forced search. * srsue/test/phy/scell_search_test --duration=30 --cell.nof_prb=6 --active_cell_list=all * --simulation_cell_list=1,2,3,4,5,6 --channel_period_s=30 --channel.hst.fd=750 --channel.delay_max=0 * - * - 100 PRB, 6 cell, distributed around 1ms + * - 100 PRB, 6 cell, distributed around 1ms. With PCI set list. * srsue/test/phy/scell_search_test --duration=30 --cell.nof_prb=100 --active_cell_list=2,3,4,5,6 * --simulation_cell_list=1,2,3,4,5,6 --channel_period_s=30 --channel.hst.fd=750 --channel.delay_max=1000 */ diff --git a/srsue/hdr/phy/scell/intra_measure.h b/srsue/hdr/phy/scell/intra_measure.h index 1b92423c3..9284738c7 100644 --- a/srsue/hdr/phy/scell/intra_measure.h +++ b/srsue/hdr/phy/scell/intra_measure.h @@ -34,45 +34,163 @@ namespace scell { // Class to perform intra-frequency measurements class intra_measure : public thread { + /* + * The intra-cell measurment has 5 different states: + * - idle: it has been initiated and it is waiting to get configured to start capturing samples. From any state + * except quit can transition to idle. + * - wait: waits for at least intra_freq_meas_period_ms since last receive start and goes to receive. + * - receive: captures base-band samples for intra_freq_meas_len_ms and goes to measure. + * - measure: enables the inner thread to start the measuring function and goes to wait. + * - quit: stops the inner thread and quits. Transition from any state measure state. + * + * FSM abstraction: + * + * +------+ set_cells_to_meas +------+ intra_freq_meas_period_ms +---------+ + * | Idle | --------------------->| Wait |------------------------------>| Receive | + * +------+ +------+ +---------+ + * ^ ^ | stop +------+ + * | | | ----->| Quit | + * init +---------+ intra_freq_meas_len_ms | +------+ + * meas_stop | Measure |<----------------------------------+ + * +---------+ + */ public: + /** + * Constructor + */ intra_measure(); + + /** + * Destructor + */ ~intra_measure(); - void init(phy_common* common, rrc_interface_phy_lte* rrc, srslte::log* log_h); - void stop(); - void set_primary_cell(uint32_t earfcn, srslte_cell_t cell); - void set_cells_to_meas(const std::set& pci); - void meas_stop(); - void write(uint32_t tti, cf_t* data, uint32_t nsamples); + + /** + * Initiation function, necessary to configure main parameters + * @param common SRSUE phy_common instance pointer for providing intra_freq_meas_len_ms and intra_freq_meas_period_ms + * @param rrc SRSUE PHY->RRC interface for supplying the RRC with the measurements + * @param log_h Physical layer Logging filter pointer + */ + void init(phy_common* common, rrc_interface_phy_lte* rrc, srslte::log* log_h); + + /** + * Stops the operation of this component + */ + void stop(); + + /** + * Sets the primmary cell, configures the cell bandwidth and sampling rate + * @param earfcn Frequency the component is receiving base-band from. Used only for reporting the EARFCN to the RRC + * @param cell Actual cell configuration + */ + void set_primary_cell(uint32_t earfcn, srslte_cell_t cell); + + /** + * Sets the PCI list of the cells this components needs to measure and starts the FSM for measuring + * @param pci is the list of PCIs to measure + */ + void set_cells_to_meas(const std::set& pci); + + /** + * Stops the measurment FSM, setting the inner state to idle. + */ + void meas_stop(); + + /** + * Inputs the baseband IQ samples into the component, internal state dictates whether it will be written or not. + * @param tti The current physical layer TTI, used for calculating the buffer write + * @param data buffer with baseband IQ samples + * @param nsamples number of samples to write + */ + void write(uint32_t tti, cf_t* data, uint32_t nsamples); + + /** + * Get EARFCN of this component + * @return EARFCN + */ uint32_t get_earfcn() { return current_earfcn; }; - void wait_meas() + + /** + * Synchronous wait mechanism, used for testing purposes, it waits for the inner thread to return a measurement. + */ + void wait_meas() { // Only used by scell_search_test meas_sync.wait(); } private: + class internal_state ///< Internal state class, provides thread safe state management + { + public: + typedef enum { + idle = 0, ///< Initial state, internal thread runs, it does not capture data + wait, ///< Wait for the period time to pass + receive, ///< Accumulate samples in ring buffer + measure, ///< Module is busy measuring + quit ///< Quit thread, no transitions are allowed + } state_t; + + private: + state_t state = idle; + std::mutex mutex; + std::condition_variable cvar; + + public: + /** + * Get the internal state + * @return protected state + */ + state_t get_state() { return state; } + + /** + * Transitions to a different state, all transitions are allowed except from quit + * @param new_state + */ + void set_state(state_t new_state) + { + std::unique_lock lock(mutex); + // Do not allow transition from quit + if (state != quit) { + state = new_state; + } + + // Notifies to the inner thread about the change of state + cvar.notify_all(); + } + + /** + * Waits for a state transition change, used for blocking the inner thread + */ + void wait_change() + { + std::unique_lock lock(mutex); + cvar.wait(lock); + } + }; + + internal_state state; + void run_thread() override; const static int INTRA_FREQ_MEAS_PRIO = DEFAULT_PRIORITY + 5; - scell_recv scell = {}; - rrc_interface_phy_lte* rrc = nullptr; - srslte::log* log_h = nullptr; - phy_common* common = nullptr; - uint32_t current_earfcn = 0; - uint32_t current_sflen = 0; - srslte_cell_t serving_cell = {}; - std::set active_pci = {}; - std::mutex active_pci_mutex = {}; - - srslte::tti_sync_cv tti_sync; - srslte::tti_sync_cv meas_sync; // Only used by scell_search_test + scell_recv scell = {}; + rrc_interface_phy_lte* rrc = nullptr; + srslte::log* log_h = nullptr; + uint32_t current_earfcn = 0; + uint32_t current_sflen = 0; + srslte_cell_t serving_cell = {}; + std::set active_pci = {}; + std::mutex active_pci_mutex = {}; + uint32_t last_measure_tti = 0; + uint32_t intra_freq_meas_len_ms = 20; + uint32_t intra_freq_meas_period_ms = 200; + uint32_t rx_gain_offset_db = 0; + srslte::tti_sync_cv meas_sync; // Only used by scell_search_test cf_t* search_buffer = nullptr; - bool running = false; - bool receive_enabled = false; - bool receiving = false; - uint32_t receive_cnt = 0; - srslte_ringbuffer_t ring_buffer = {}; + uint32_t receive_cnt = 0; + srslte_ringbuffer_t ring_buffer = {}; srslte_refsignal_dl_sync_t refsignal_dl_sync = {}; }; diff --git a/srsue/src/phy/scell/intra_measure.cc b/srsue/src/phy/scell/intra_measure.cc index fdf2c68f8..2876117e7 100644 --- a/srsue/src/phy/scell/intra_measure.cc +++ b/srsue/src/phy/scell/intra_measure.cc @@ -45,52 +45,52 @@ intra_measure::~intra_measure() free(search_buffer); } -void intra_measure::init(phy_common* common_, rrc_interface_phy_lte* rrc_, srslte::log* log_h_) +void intra_measure::init(phy_common* common, rrc_interface_phy_lte* rrc_, srslte::log* log_h_) { - this->rrc = rrc_; - this->log_h = log_h_; - this->common = common_; - receive_enabled = false; + rrc = rrc_; + log_h = log_h_; + + if (common) { + intra_freq_meas_len_ms = common->args->intra_freq_meas_len_ms; + intra_freq_meas_period_ms = common->args->intra_freq_meas_period_ms; + rx_gain_offset_db = common->args->rx_gain_offset; + } // Initialise Reference signal measurement srslte_refsignal_dl_sync_init(&refsignal_dl_sync); // Start scell - scell.init(log_h, common->args->intra_freq_meas_len_ms); + scell.init(log_h, intra_freq_meas_len_ms); - search_buffer = - (cf_t*)srslte_vec_malloc(common->args->intra_freq_meas_len_ms * SRSLTE_SF_LEN_PRB(SRSLTE_MAX_PRB) * sizeof(cf_t)); + search_buffer = srslte_vec_cf_malloc(intra_freq_meas_len_ms * SRSLTE_SF_LEN_PRB(SRSLTE_MAX_PRB)); - if (srslte_ringbuffer_init( - &ring_buffer, sizeof(cf_t) * common->args->intra_freq_meas_len_ms * 2 * SRSLTE_SF_LEN_PRB(SRSLTE_MAX_PRB))) { + if (srslte_ringbuffer_init(&ring_buffer, sizeof(cf_t) * intra_freq_meas_len_ms * SRSLTE_SF_LEN_PRB(SRSLTE_MAX_PRB))) { return; } - running = true; + state.set_state(internal_state::idle); start(INTRA_FREQ_MEAS_PRIO); } void intra_measure::stop() { - running = false; + state.set_state(internal_state::quit); srslte_ringbuffer_stop(&ring_buffer); - tti_sync.increase(); wait_thread_finish(); srslte_refsignal_dl_sync_free(&refsignal_dl_sync); } void intra_measure::set_primary_cell(uint32_t earfcn, srslte_cell_t cell) { - this->current_earfcn = earfcn; - current_sflen = (uint32_t)SRSLTE_SF_LEN_PRB(cell.nof_prb); - this->serving_cell = cell; + current_earfcn = earfcn; + current_sflen = (uint32_t)SRSLTE_SF_LEN_PRB(cell.nof_prb); + serving_cell = cell; } void intra_measure::meas_stop() { - receive_enabled = false; - receiving = false; - receive_cnt = 0; + state.set_state(internal_state::idle); + receive_cnt = 0; srslte_ringbuffer_reset(&ring_buffer); if (log_h) { log_h->info("INTRA: Disabled neighbour cell search for EARFCN %d\n", get_earfcn()); @@ -102,30 +102,41 @@ void intra_measure::set_cells_to_meas(const std::set& pci) active_pci_mutex.lock(); active_pci = pci; active_pci_mutex.unlock(); - receive_enabled = true; + state.set_state(internal_state::receive); log_h->info("INTRA: Received list of %lu neighbour cells to measure in EARFCN %d.\n", pci.size(), current_earfcn); } void intra_measure::write(uint32_t tti, cf_t* data, uint32_t nsamples) { - if (receive_enabled) { - if ((tti % common->args->intra_freq_meas_period_ms) == 0) { - receiving = true; - receive_cnt = 0; - srslte_ringbuffer_reset(&ring_buffer); - } - if (receiving) { + uint32_t elapsed_tti = ((tti + 10240) - last_measure_tti) % 10240; + + switch (state.get_state()) { + + case internal_state::idle: + case internal_state::measure: + case internal_state::quit: + // Do nothing + break; + case internal_state::wait: + if (elapsed_tti >= intra_freq_meas_period_ms) { + state.set_state(internal_state::receive); + receive_cnt = 0; + last_measure_tti = tti; + srslte_ringbuffer_reset(&ring_buffer); + } + break; + case internal_state::receive: if (srslte_ringbuffer_write(&ring_buffer, data, nsamples * sizeof(cf_t)) < (int)(nsamples * sizeof(cf_t))) { - Warning("Error writting to ringbuffer\n"); - receiving = false; + Warning("Error writing to ringbuffer\n"); + state.set_state(internal_state::idle); } else { receive_cnt++; - if (receive_cnt == common->args->intra_freq_meas_len_ms) { - tti_sync.increase(); - receiving = false; + if (receive_cnt == intra_freq_meas_len_ms) { + // Buffer ready for measuring, start + state.set_state(internal_state::measure); } } - } + break; } } @@ -133,32 +144,31 @@ void intra_measure::run_thread() { std::set cells_to_measure = {}; - while (running) { - if (running) { - tti_sync.wait(); - } - - if (running) { - + while (state.get_state() != internal_state::quit) { + if (state.get_state() == internal_state::measure) { + // Start measuring active_pci_mutex.lock(); cells_to_measure = active_pci; active_pci_mutex.unlock(); // Read data from buffer and find cells in it - srslte_ringbuffer_read( - &ring_buffer, search_buffer, common->args->intra_freq_meas_len_ms * current_sflen * sizeof(cf_t)); + srslte_ringbuffer_read(&ring_buffer, search_buffer, intra_freq_meas_len_ms * current_sflen * sizeof(cf_t)); + + // Go to receive before finishing, so new samples can be enqueued before the thread finishes + if (state.get_state() == internal_state::measure) { + // Prevents transition to wait if state has changed while reading the ring-buffer + state.set_state(internal_state::wait); + } // Detect new cells using PSS/SSS - std::set detected_cells = - scell.find_cells(search_buffer, serving_cell, common->args->intra_freq_meas_len_ms); + std::set detected_cells = scell.find_cells(search_buffer, serving_cell, intra_freq_meas_len_ms); // Add detected cells to the list of cells to measure for (auto& c : detected_cells) { cells_to_measure.insert(c); } - receiving = false; - + // Initialise empty neighbour cell list std::vector neighbour_cells = {}; // Use Cell Reference signal to measure cells in the time domain for all known active PCI @@ -171,14 +181,13 @@ void intra_measure::run_thread() cell.id = id; srslte_refsignal_dl_sync_set_cell(&refsignal_dl_sync, cell); - srslte_refsignal_dl_sync_run( - &refsignal_dl_sync, search_buffer, common->args->intra_freq_meas_len_ms * current_sflen); + srslte_refsignal_dl_sync_run(&refsignal_dl_sync, search_buffer, intra_freq_meas_len_ms * current_sflen); if (refsignal_dl_sync.found) { rrc_interface_phy_lte::phy_meas_t m = {}; m.pci = cell.id; m.earfcn = current_earfcn; - m.rsrp = refsignal_dl_sync.rsrp_dBfs - common->rx_gain_offset; + m.rsrp = refsignal_dl_sync.rsrp_dBfs - rx_gain_offset_db; m.rsrq = refsignal_dl_sync.rsrq_dB; neighbour_cells.push_back(m); @@ -199,6 +208,9 @@ void intra_measure::run_thread() } meas_sync.increase(); + } else if (state.get_state() != internal_state::quit) { + // Wait for changing state + state.wait_change(); } } } diff --git a/srsue/test/phy/scell_search_test.cc b/srsue/test/phy/scell_search_test.cc index 65e5c418b..5270d6490 100644 --- a/srsue/test/phy/scell_search_test.cc +++ b/srsue/test/phy/scell_search_test.cc @@ -324,6 +324,7 @@ int parse_args(int argc, char** argv) common.add_options() ("duration", bpo::value(&duration_execution_s)->default_value(60), "Duration of the execution in seconds") ("cell.nof_prb", bpo::value(&cell_base.nof_prb)->default_value(100), "Cell Number of PRB") + ("cell.nof_ports", bpo::value(&cell_base.nof_ports)->default_value(1), "Cell Number of Tx ports") ("intra_meas_log_level", bpo::value(&intra_meas_log_level)->default_value("none"), "Intra measurement log level (none, warning, info, debug)") ("intra_freq_meas_len_ms", bpo::value(&phy_args.intra_freq_meas_len_ms)->default_value(20), "Intra measurement measurement length") ("intra_freq_meas_period_ms", bpo::value(&phy_args.intra_freq_meas_period_ms)->default_value(200), "Intra measurement measurement period") @@ -450,6 +451,7 @@ int main(int argc, char** argv) phy_args.cfo_loop_pss_conv = DEFAULT_PSS_STABLE_TIMEOUT; phy_args.snr_estim_alg = "refs"; phy_args.snr_ema_coeff = 0.1f; + phy_args.rx_gain_offset = rx_gain + 62.0f; // Set phy-lib logging level srslte_verbose = phy_lib_log_level; @@ -490,7 +492,7 @@ int main(int argc, char** argv) logger.set_level(intra_meas_log_level); intra_measure.init(&common, &rrc, &logger); - intra_measure.set_primary_cell(serving_cell_id, cell_base); + intra_measure.set_primary_cell(SRSLTE_MAX(earfcn_dl, 0), cell_base); if (earfcn_dl >= 0) { // Create radio log @@ -636,17 +638,19 @@ int main(int argc, char** argv) enb->work(&sf_cfg_dl, nullptr, nullptr, nullptr, nullptr, baseband_buffer, ts); } } + // If it is time for a measurement, wait previous to finish + if (sf_idx > phy_args.intra_freq_meas_period_ms) { + if (sf_idx % phy_args.intra_freq_meas_period_ms == 0) { + intra_measure.wait_meas(); + } + } } + // Increase Time counter srslte_timestamp_add(&ts, 0, 0.001f); - if (sf_idx > phy_args.intra_freq_meas_period_ms) { - if (sf_idx % phy_args.intra_freq_meas_period_ms == 0) { - intra_measure.wait_meas(); - } - } - - intra_measure.write(sf_idx, baseband_buffer, SRSLTE_SF_LEN_PRB(cell_base.nof_prb)); + // Give data to intra measure component + intra_measure.write(sf_idx % 10240, baseband_buffer, SRSLTE_SF_LEN_PRB(cell_base.nof_prb)); if (sf_idx % 1000 == 0) { printf("Done %.1f%%\n", (double)sf_idx * 100.0 / ((double)duration_execution_s * 1000.0)); } @@ -657,6 +661,10 @@ int main(int argc, char** argv) ret = rrc.print_stats() ? SRSLTE_SUCCESS : SRSLTE_ERROR; + if (radio) { + radio->stop(); + } + if (baseband_buffer) { free(baseband_buffer); } @@ -673,7 +681,7 @@ int main(int argc, char** argv) } } - if (ret) { + if (ret && radio == nullptr) { printf("Error\n"); } else { printf("Ok\n");