- Cleaned up style in the PRACH class:

* Removed magic numbers.
* Reduced indentation of statements with early exists.
* Removed elses after a return statement.
* Trimmed unnecessary include files.
* Default initialized members in the class.
master
faluco 4 years ago committed by faluco
parent b892da0294
commit 5acc1c9a73

@ -22,41 +22,29 @@
#ifndef SRSUE_PRACH_H #ifndef SRSUE_PRACH_H
#define SRSUE_PRACH_H #define SRSUE_PRACH_H
#include <bitset>
#include <string.h>
#include "srslte/common/log.h" #include "srslte/common/log.h"
#include "srslte/interfaces/ue_interfaces.h" #include "srslte/interfaces/ue_interfaces.h"
#include "srslte/radio/radio.h" #include "srslte/radio/radio.h"
#include "srslte/srslte.h" #include "srslte/srslte.h"
#include <bitset>
namespace srsue { namespace srsue {
class prach class prach
{ {
public: public:
prach() prach() = default;
{ ~prach() { stop(); }
bzero(&prach_obj, sizeof(srslte_prach_t));
bzero(&cell, sizeof(srslte_cell_t));
bzero(&cfo_h, sizeof(srslte_cfo_t));
transmitted_tti = 0;
target_power_dbm = 0;
mem_initiated = false;
cell_initiated = false;
signal_buffer = NULL;
}
~prach();
void init(uint32_t max_prb, srslte::log* log_h); void init(uint32_t max_prb, srslte::log* log_h);
void stop(); void stop();
bool set_cell(srslte_cell_t cell, srslte_prach_cfg_t prach_cfg); bool set_cell(srslte_cell_t cell, srslte_prach_cfg_t prach_cfg);
bool prepare_to_send(uint32_t preamble_idx, int allowed_subframe = -1, float target_power_dbm = -1); bool prepare_to_send(uint32_t preamble_idx, int allowed_subframe = -1, float target_power_dbm = -1);
bool is_ready_to_send(uint32_t current_tti); bool is_ready_to_send(uint32_t current_tti);
bool is_pending(); bool is_pending() const;
cf_t* generate(float cfo, uint32_t* nof_sf, float* target_power = NULL); cf_t* generate(float cfo, uint32_t* nof_sf, float* target_power = NULL);
phy_interface_mac_lte::prach_info_t get_info(); phy_interface_mac_lte::prach_info_t get_info() const;
private: private:
bool generate_buffer(uint32_t f_idx); bool generate_buffer(uint32_t f_idx);
@ -72,23 +60,25 @@ private:
} }
private: private:
const static int MAX_LEN_SF = 3; static constexpr unsigned MAX_LEN_SF = 3;
static constexpr unsigned max_fs = 12;
static constexpr unsigned max_preambles = 64;
srslte::log* log_h; srslte::log* log_h = nullptr;
int preamble_idx; srslte_prach_t prach_obj = {};
int allowed_subframe; srslte_cell_t cell = {};
bool mem_initiated; srslte_cfo_t cfo_h = {};
bool cell_initiated; srslte_prach_cfg_t cfg = {};
uint32_t len; std::array<std::array<cf_t*, max_preambles>, max_fs> buffer = {};
cf_t* buffer[12][64]; cf_t* signal_buffer = nullptr;
std::bitset<12 * 64> buffer_bitmask; int preamble_idx = 0;
srslte_prach_t prach_obj; uint32_t len = 0;
int transmitted_tti; int allowed_subframe = 0;
srslte_cell_t cell; int transmitted_tti = 0;
cf_t* signal_buffer; float target_power_dbm = 0;
srslte_cfo_t cfo_h; bool mem_initiated = false;
float target_power_dbm; bool cell_initiated = false;
srslte_prach_cfg_t cfg = {}; std::bitset<max_fs * max_preambles> buffer_bitmask;
}; };
} // namespace srsue } // namespace srsue

@ -19,15 +19,10 @@
* *
*/ */
#include <pthread.h> #include "srsue/hdr/phy/prach.h"
#include <string.h>
#include <strings.h>
#include "srslte/common/log.h" #include "srslte/common/log.h"
#include "srslte/interfaces/ue_interfaces.h" #include "srslte/interfaces/ue_interfaces.h"
#include "srslte/srslte.h" #include "srslte/srslte.h"
#include "srsue/hdr/phy/phy.h"
#include "srsue/hdr/phy/prach.h"
#define Error(fmt, ...) \ #define Error(fmt, ...) \
if (SRSLTE_DEBUG_ENABLED) \ if (SRSLTE_DEBUG_ENABLED) \
@ -42,100 +37,100 @@
if (SRSLTE_DEBUG_ENABLED) \ if (SRSLTE_DEBUG_ENABLED) \
log_h->debug(fmt, ##__VA_ARGS__) log_h->debug(fmt, ##__VA_ARGS__)
namespace srsue { using namespace srsue;
prach::~prach()
{
stop();
}
void prach::init(uint32_t max_prb, srslte::log* log_h_) void prach::init(uint32_t max_prb, srslte::log* log_h_)
{ {
log_h = log_h_; log_h = log_h_;
for (int i = 0; i < 64; i++) { for (auto& i : buffer) {
for (int f = 0; f < 12; f++) { for (auto& j : i) {
buffer[f][i] = srslte_vec_cf_malloc(SRSLTE_PRACH_MAX_LEN); j = srslte_vec_cf_malloc(SRSLTE_PRACH_MAX_LEN);
if (!buffer[f][i]) { if (!j) {
perror("malloc"); perror("malloc");
return; return;
} }
} }
} }
if (srslte_cfo_init(&cfo_h, SRSLTE_PRACH_MAX_LEN)) { if (srslte_cfo_init(&cfo_h, SRSLTE_PRACH_MAX_LEN)) {
ERROR("PRACH: Error initiating CFO\n"); ERROR("PRACH: Error initiating CFO\n");
return; return;
} }
srslte_cfo_set_tol(&cfo_h, 0); srslte_cfo_set_tol(&cfo_h, 0);
signal_buffer = srslte_vec_cf_malloc(MAX_LEN_SF * 30720U); signal_buffer = srslte_vec_cf_malloc(MAX_LEN_SF * 30720U);
if (!signal_buffer) { if (!signal_buffer) {
perror("malloc"); perror("malloc");
return; return;
} }
if (srslte_prach_init(&prach_obj, srslte_symbol_sz(max_prb))) { if (srslte_prach_init(&prach_obj, srslte_symbol_sz(max_prb))) {
Error("Initiating PRACH library\n"); Error("Initiating PRACH library\n");
return; return;
} }
mem_initiated = true; mem_initiated = true;
} }
void prach::stop() void prach::stop()
{ {
if (mem_initiated) { if (!mem_initiated) {
for (int i = 0; i < 64; i++) { return;
for (int f = 0; f < 12; f++) { }
if (buffer[f][i]) {
free(buffer[f][i]); for (auto& i : buffer) {
} for (auto& j : i) {
} free(j);
}
if (signal_buffer) {
free(signal_buffer);
} }
srslte_cfo_free(&cfo_h);
srslte_prach_free(&prach_obj);
mem_initiated = false;
} }
free(signal_buffer);
srslte_cfo_free(&cfo_h);
srslte_prach_free(&prach_obj);
mem_initiated = false;
} }
bool prach::set_cell(srslte_cell_t cell_, srslte_prach_cfg_t prach_cfg) bool prach::set_cell(srslte_cell_t cell_, srslte_prach_cfg_t prach_cfg)
{ {
if (mem_initiated) { if (!mem_initiated) {
// TODO: Check if other PRACH parameters changed ERROR("PRACH: Error must call init() first\n");
if (cell.id != cell_.id || !cell_initiated) { return false;
cell = cell_; }
cfg = prach_cfg;
preamble_idx = -1;
if (6 + prach_cfg.freq_offset > cell.nof_prb) {
log_h->console(
"Error no space for PRACH: frequency offset=%d, N_rb_ul=%d\n", prach_cfg.freq_offset, cell.nof_prb);
log_h->error(
"Error no space for PRACH: frequency offset=%d, N_rb_ul=%d\n", prach_cfg.freq_offset, cell.nof_prb);
return false;
}
Info("PRACH: configIdx=%d, rootSequence=%d, zeroCorrelationConfig=%d, freqOffset=%d\n", // TODO: Check if other PRACH parameters changed
prach_cfg.config_idx, if (cell.id == cell_.id && cell_initiated) {
prach_cfg.root_seq_idx, return true;
prach_cfg.zero_corr_zone, }
prach_cfg.freq_offset);
if (srslte_prach_set_cfg(&prach_obj, &prach_cfg, cell.nof_prb)) { cell = cell_;
Error("Initiating PRACH library\n"); cfg = prach_cfg;
return false; preamble_idx = -1;
}
buffer_bitmask.reset(); if (6 + prach_cfg.freq_offset > cell.nof_prb) {
len = prach_obj.N_seq + prach_obj.N_cp; log_h->console("Error no space for PRACH: frequency offset=%d, N_rb_ul=%d\n", prach_cfg.freq_offset, cell.nof_prb);
transmitted_tti = -1; log_h->error("Error no space for PRACH: frequency offset=%d, N_rb_ul=%d\n", prach_cfg.freq_offset, cell.nof_prb);
cell_initiated = true; return false;
} }
return true;
} else { Info("PRACH: configIdx=%d, rootSequence=%d, zeroCorrelationConfig=%d, freqOffset=%d\n",
ERROR("PRACH: Error must call init() first\n"); prach_cfg.config_idx,
prach_cfg.root_seq_idx,
prach_cfg.zero_corr_zone,
prach_cfg.freq_offset);
if (srslte_prach_set_cfg(&prach_obj, &prach_cfg, cell.nof_prb)) {
Error("Initiating PRACH library\n");
return false; return false;
} }
buffer_bitmask.reset();
len = prach_obj.N_seq + prach_obj.N_cp;
transmitted_tti = -1;
cell_initiated = true;
return true;
} }
bool prach::generate_buffer(uint32_t f_idx) bool prach::generate_buffer(uint32_t f_idx)
@ -161,26 +156,26 @@ bool prach::generate_buffer(uint32_t f_idx)
bool prach::prepare_to_send(uint32_t preamble_idx_, int allowed_subframe_, float target_power_dbm_) bool prach::prepare_to_send(uint32_t preamble_idx_, int allowed_subframe_, float target_power_dbm_)
{ {
if (cell_initiated && preamble_idx_ < 64) { if (cell_initiated && preamble_idx_ < max_preambles) {
preamble_idx = preamble_idx_; preamble_idx = preamble_idx_;
target_power_dbm = target_power_dbm_; target_power_dbm = target_power_dbm_;
allowed_subframe = allowed_subframe_; allowed_subframe = allowed_subframe_;
transmitted_tti = -1; transmitted_tti = -1;
Debug("PRACH: prepare to send preamble %d\n", preamble_idx); Debug("PRACH: prepare to send preamble %d\n", preamble_idx);
return true; return true;
}
if (!cell_initiated) {
Error("PRACH: Cell not configured\n");
} else { } else {
if (!cell_initiated) { Error("PRACH: Invalid preamble %d\n", preamble_idx_);
Error("PRACH: Cell not configured\n");
} else {
Error("PRACH: Invalid preamble %d\n", preamble_idx_);
}
return false;
} }
return false;
} }
bool prach::is_pending() bool prach::is_pending() const
{ {
return cell_initiated && preamble_idx >= 0 && preamble_idx < 64; return cell_initiated && preamble_idx >= 0 && unsigned(preamble_idx) < max_preambles;
} }
bool prach::is_ready_to_send(uint32_t current_tti_) bool prach::is_ready_to_send(uint32_t current_tti_)
@ -197,7 +192,7 @@ bool prach::is_ready_to_send(uint32_t current_tti_)
return false; return false;
} }
phy_interface_mac_lte::prach_info_t prach::get_info() phy_interface_mac_lte::prach_info_t prach::get_info() const
{ {
phy_interface_mac_lte::prach_info_t info = {}; phy_interface_mac_lte::prach_info_t info = {};
@ -217,61 +212,58 @@ phy_interface_mac_lte::prach_info_t prach::get_info()
cf_t* prach::generate(float cfo, uint32_t* nof_sf, float* target_power) cf_t* prach::generate(float cfo, uint32_t* nof_sf, float* target_power)
{ {
if (cell_initiated && preamble_idx >= 0 && nof_sf && preamble_idx < 64 && srslte_cell_isvalid(&cell) && if (!cell_initiated || preamble_idx < 0 || !nof_sf || unsigned(preamble_idx) >= max_preambles ||
len < MAX_LEN_SF * 30720 && len > 0) { !srslte_cell_isvalid(&cell) || len >= MAX_LEN_SF * 30720 || len == 0) {
Error("PRACH: Invalid parameters: cell_initiated=%d, preamble_idx=%d, cell.nof_prb=%d, len=%d\n",
uint32_t f_idx = 0; cell_initiated,
if (cell.frame_type == SRSLTE_TDD) { preamble_idx,
f_idx = prach_obj.current_prach_idx; cell.nof_prb,
// For format4, choose odd or even position len);
if (prach_obj.config_idx >= 48) { return nullptr;
f_idx += 6; }
}
if (f_idx >= 12) {
Error("PRACH Buffer: Invalid f_idx=%d\n", f_idx);
f_idx = 0;
}
}
if (!generate_buffer(f_idx)) { uint32_t f_idx = 0;
return NULL; if (cell.frame_type == SRSLTE_TDD) {
f_idx = prach_obj.current_prach_idx;
// For format4, choose odd or even position
if (prach_obj.config_idx >= 48) {
f_idx += 6;
} }
if (f_idx >= max_fs) {
if (!is_buffer_generated(f_idx, preamble_idx)) { Error("PRACH Buffer: Invalid f_idx=%d\n", f_idx);
Error("PRACH Buffer not generated: f_idx=%d preamble_idx=%d\n", f_idx, preamble_idx); f_idx = 0;
return NULL;
} }
}
// Correct CFO before transmission if (!generate_buffer(f_idx)) {
srslte_cfo_correct(&cfo_h, buffer[f_idx][preamble_idx], signal_buffer, cfo / srslte_symbol_sz(cell.nof_prb)); return nullptr;
}
// pad guard symbols with zeros if (!is_buffer_generated(f_idx, preamble_idx)) {
uint32_t nsf = (len - 1) / SRSLTE_SF_LEN_PRB(cell.nof_prb) + 1; Error("PRACH Buffer not generated: f_idx=%d preamble_idx=%d\n", f_idx, preamble_idx);
srslte_vec_cf_zero(&signal_buffer[len], (nsf * SRSLTE_SF_LEN_PRB(cell.nof_prb) - len)); return nullptr;
}
*nof_sf = nsf; // Correct CFO before transmission
srslte_cfo_correct(&cfo_h, buffer[f_idx][preamble_idx], signal_buffer, cfo / srslte_symbol_sz(cell.nof_prb));
if (target_power) { // pad guard symbols with zeros
*target_power = target_power_dbm; uint32_t nsf = (len - 1) / SRSLTE_SF_LEN_PRB(cell.nof_prb) + 1;
} srslte_vec_cf_zero(&signal_buffer[len], (nsf * SRSLTE_SF_LEN_PRB(cell.nof_prb) - len));
Info("PRACH: Transmitted preamble=%d, tti_tx=%d, CFO=%.2f KHz, nof_sf=%d, target_power=%.1f dBm\n", *nof_sf = nsf;
preamble_idx,
transmitted_tti,
cfo * 15,
nsf,
target_power_dbm);
preamble_idx = -1;
return signal_buffer; if (target_power) {
} else { *target_power = target_power_dbm;
Error("PRACH: Invalid parameters: cell_initiated=%d, preamble_idx=%d, cell.nof_prb=%d, len=%d\n",
cell_initiated,
preamble_idx,
cell.nof_prb,
len);
return NULL;
} }
}
} // namespace srsue Info("PRACH: Transmitted preamble=%d, tti_tx=%d, CFO=%.2f KHz, nof_sf=%d, target_power=%.1f dBm\n",
preamble_idx,
transmitted_tti,
cfo * 15,
nsf,
target_power_dbm);
preamble_idx = -1;
return signal_buffer;
}

Loading…
Cancel
Save