-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarrierrow5g.cpp
More file actions
673 lines (582 loc) · 26.9 KB
/
Copy pathcarrierrow5g.cpp
File metadata and controls
673 lines (582 loc) · 26.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include "carrierrow5g.h"
#include "ui_carrierrow5g.h"
#include "appconfig.h"
#include <cmath>
#include <QJsonArray>
#include <QComboBox>
/**
* @brief This namespace contains all the core logic for calculating the 5G NR theoretical throughput.
* The calculations are based on the formulas and parameters outlined in 3GPP TS 38.306.
*/
namespace {
/**
* @brief Scaling factor to convert the final result from bits per second (bps) to Megabits per second (Mbps).
*/
const double base = 1e-6;
/**
* @brief Number of subcarriers per Physical Resource Block (PRB). A standard value in 5G NR.
*/
const int scprb = 12;
/**
* @brief Number of OFDM symbols per slot for Normal Cyclic Prefix (CP). A standard value.
*/
const int symslot = 14;
/**
* @brief Duration of a standard 5G radio frame in milliseconds.
*/
const int frametime = 10; // in milliseconds
/**
* @brief Maximum code rate (R_max), as defined in 3GPP TS 38.306.
* It's a dimensionless factor representing the upper limit for data bits
* relative to the total transmitted bits (which includes redundancy for error correction).
*/
const double rMax = 948.0 / 1024.0;
/**
* @brief Maps Subcarrier Spacing (SCS) in kHz to its corresponding numerology index 'μ' (mu),
* as per 3GPP TS 38.211, Table 4.2-1.
*/
const QMap<int, int> numerologyMap = {{15, 0}, {30, 1}, {60, 2}, {120, 3}};
/**
* @brief Estimated overhead percentage for control channel signaling (e.g., PDCCH, PUCCH), which reduces
* the available resources for user data throughput.
* Key '1' corresponds to Frequency Range 1 (FR1, < 7.125 GHz).
* Key '2' corresponds to Frequency Range 2 (FR2, mmWave).
* Values are pairs of {Downlink Overhead, Uplink Overhead}.
*/
const QMap<int, QPair<double, double>> nrFreqOverhead = {{1, {0.14, 0.08}}, {2, {0.18, 0.10}}};
/**
* @brief Retrieves the numerology index 'μ' (mu) for a given Subcarrier Spacing (SCS) value in kHz.
* @param scs Subcarrier Spacing in kHz.
* @return The corresponding numerology index 'μ'.
*/
int numerology(int scs) { return numerologyMap.value(scs, 0); }
/**
* @brief Calculates the duration of a single OFDM symbol in seconds.
* @note The function name 'scsSlotDuration' is slightly misleading; it returns the symbol duration, not the slot duration.
* The calculation is `(1ms / 2^μ) / 14_symbols_per_slot`, where μ is the numerology index.
* @param scsNum The numerology index 'μ'.
* @return The duration of one OFDM symbol in seconds.
*/
double scsSlotDuration(int scsNum) { return 1e-3 / (symslot * std::pow(2.0, scsNum)); }
/**
* @brief A utility function to round a double-precision number to 4 decimal places for cleaner intermediate values.
* @param n The number to round.
* @return The rounded number.
*/
double reduce(double n) { return qRound(n * 10000) / 10000.0; }
// --- Core Calculation Functions ---
/**
* @brief Calculates the theoretical peak data rate for a single carrier, for both downlink and uplink,
* before considering TDD patterns or overhead.
* This is the core calculation based on the formula from 3GPP TS 38.306, Annex A.
* @param info A struct containing all user-selected parameters for the carrier.
* @param band The JSON object containing configuration data for the selected band.
* @return A pair of doubles representing {downlink throughput, uplink throughput} in Mbps, before overhead and TDD scaling.
*/
QPair<double, double> common(const Carrier5GInfo& info, const QJsonObject& band)
{
double sFactor = info.sfactor;
int scs = info.scs;
if(scs == 0) return {0.0, 0.0};
int num = numerology(scs); // Get numerology index μ
double cNum = scsSlotDuration(num); // Get OFDM symbol duration (T_s)
const auto& nrRb = AppConfig::instance().nrRbData;
QString freqRangeStr = QString::number(band["freqrange"].toInt());
// This lambda function calculates the throughput for a specific direction (downlink or uplink).
auto calcDir = [&](const QString& dir) {
int layers = (dir == "dl") ? info.dlLayers : info.ulLayers;
int coding = (dir == "dl") ? info.dlModulation : info.ulModulation;
int bw = (dir == "ul" && band["type"] == "FDD") ? info.ulBandwidth : info.dlBandwidth;
if (layers == 0 || coding == 0 || sFactor == 0 || scs == 0 || bw == 0) return 0.0;
// N_PRB: Number of Physical Resource Blocks for the given bandwidth and SCS.
int rbCount = nrRb[freqRangeStr].toObject()[QString::number(bw)].toObject()[QString::number(scs)].toInt(0);
if (rbCount == 0) return 0.0;
// Total number of subcarriers = N_PRB * N_sc_RB (where N_sc_RB is 12).
double totalSc = rbCount * scprb;
/**
* The throughput formula is derived from 3GPP TS 38.306, Annex A:
* Data Rate (bps) = Σ (V_layers * Q_m * f * R_max * (N_PRB * N_sc_RB / T_s) * OH)
*
* V_layers: Number of MIMO layers ('layers').
* Q_m: Modulation order, bits per symbol (e.g., 8 for 256QAM) ('coding').
* f: Scaling factor ('sFactor').
* R_max: Maximum code rate ('rMax').
* N_PRB: Number of PRBs ('rbCount').
* N_sc_RB: Subcarriers per PRB ('scprb', which is 12).
* T_s: OFDM symbol duration in seconds ('cNum').
*
* The 'base' (1e-6) is applied at the end to convert bps to Mbps.
* TDD duty cycle and overhead (OH) are applied in subsequent functions.
*/
return base * layers * coding * sFactor * rMax * (totalSc / cNum);
};
return {calcDir("dl"), calcDir("ul")};
}
/**
* @brief Applies a reduction factor to the calculated throughput to account for control channel overhead.
* The overhead percentage depends on the frequency range (FR1 vs. FR2).
* @param band The JSON object for the selected band.
* @param throughput A pair containing the pre-overhead DL and UL throughput.
* @return A pair of doubles representing {downlink throughput, uplink throughput} after applying overhead.
*/
QPair<double, double> calcOverhead(const QJsonObject& band, QPair<double, double> throughput)
{
int freqRange = band["freqrange"].toInt();
if (!nrFreqOverhead.contains(freqRange)) return throughput;
const auto& oh = nrFreqOverhead[freqRange];
return {throughput.first * (1 - oh.first), throughput.second * (1 - oh.second)};
}
/**
* @brief Parses user-provided TDD pattern strings into a structured format.
* A TDD pattern consists of its periodicity and the number of DL/UL slots and symbols within that period.
* This function supports up to two distinct patterns which can be combined.
* @param info A struct containing the user-input strings for TDD patterns.
* @return A vector of TddSlotPattern structs.
*/
QVector<TddSlotPattern> getSlotFormat(const Carrier5GInfo& info)
{
QVector<TddSlotPattern> patterns;
auto processPattern = [&](const QString& p, const QString& s, const QString& sy){
bool p_ok, s1_ok, s2_ok, sy1_ok, sy2_ok;
double period = p.toDouble(&p_ok);
auto ranura = s.split('/'); // 'ranura' is Spanish for slot
auto symbs = sy.split('/');
if(p_ok && period >= 0 && period <= 10 && ranura.size() == 2 && symbs.size() == 2) {
int dlSlots = ranura[0].trimmed().toInt(&s1_ok);
int ulSlots = ranura[1].trimmed().toInt(&s2_ok);
int dlSymbs = symbs[0].trimmed().toInt(&sy1_ok);
int ulSymbs = symbs[1].trimmed().toInt(&sy2_ok);
if(s1_ok && s2_ok && sy1_ok && sy2_ok) {
patterns.append({period, dlSlots, ulSlots, dlSymbs, ulSymbs});
}
}
};
if(!info.tddPattern1Period.isEmpty()) processPattern(info.tddPattern1Period, info.tddPattern1CustomSlot, info.tddPattern1CustomSymb);
if(!info.tddPattern2Period.isEmpty()) processPattern(info.tddPattern2Period, info.tddPattern2CustomSlot, info.tddPattern2CustomSymb);
return patterns;
}
/**
* @brief Calculates the percentage of time allocated to Downlink (D), Uplink (U), and Flexible (F) symbols
* within a 10ms frame, based on the provided TDD slot patterns.
* @param patterns A vector of parsed TDD patterns.
* @param info A struct containing carrier info, used here to get the SCS.
* @return A map containing the calculated percentages for Downlink ('D'), Uplink ('U'), and Flexible ('F') symbols.
*/
QMap<QString, double> calcSlotPercent(const QVector<TddSlotPattern>& patterns, const Carrier5GInfo& info)
{
int num = numerology(info.scs);
double slotDur = scsSlotDuration(num); // Note: this is actually symbol duration.
// Total symbols in a 10ms frame for the given numerology.
double frameSymbols = frametime * 1e-3 / slotDur;
double dlSymbols = 0, ulSymbols = 0, totalPeriodicity = 0;
// Sum the total number of DL and UL symbols and the total periodicity from all defined patterns.
for (const auto& p : patterns) {
dlSymbols += (p.nrofDownlinkSlots * symslot) + p.nrofDownlinkSymbols;
ulSymbols += (p.nrofUplinkSlots * symslot) + p.nrofUplinkSymbols;
totalPeriodicity += p.periodicity;
}
// If the combined periodicity of patterns (e.g., 5ms + 5ms) does not equal the 10ms frame time,
// scale the symbol counts proportionally to represent the full 10ms duration.
if (totalPeriodicity > 0 && totalPeriodicity != frametime) {
double multiplier = frametime / totalPeriodicity;
dlSymbols *= multiplier;
ulSymbols *= multiplier;
}
// Flexible symbols are the remainder of the frame.
double flSymbols = frameSymbols - (dlSymbols + ulSymbols);
// Return the percentage of time for each symbol type.
return {{"D", dlSymbols / frameSymbols}, {"U", ulSymbols / frameSymbols}, {"F", flSymbols / frameSymbols}};
}
}
CarrierRow5G::CarrierRow5G(int carrierId, bool isPrimary, QWidget *parent) :
QWidget(parent),
ui(new Ui::CarrierRow5G),
m_carrierId(carrierId),
m_isPrimary(isPrimary)
{
ui->setupUi(this);
setupInitialState();
connectSignals();
populateBands();
updateCarrierStructure({}); // Initial hidden state
}
CarrierRow5G::~CarrierRow5G()
{
delete ui;
}
int CarrierRow5G::id() const
{
return m_carrierId;
}
void CarrierRow5G::setCarrierIndex(int index)
{
m_carrierIndex = index;
updateTitle(QString("Carrier #%1 - Select a band").arg(m_carrierIndex));
}
void CarrierRow5G::updateTitle(const QString &html)
{
ui->headerButton->setText(html);
}
Calc5GResult CarrierRow5G::calculate()
{
Calc5GResult result;
// Check if the frequency band is selected
if (m_info.band == "0") {
result.error = QString("Please select a band for Carrier #%1").arg(m_carrierIndex);
updateTitle(QString("Carrier #%1 - Select a band").arg(m_carrierIndex));
return result;
}
// Get the frequency band setting and calculate it based on the type
const auto& band = AppConfig::instance().nrBandData[m_info.band].toObject();
QString type = band["type"].toString();
QPair<double, double> res = {0.0, 0.0};
if (type == "FDD") {
res = calcOverhead(band, common(m_info, band));
} else if (type == "TDD") {
auto patterns = getSlotFormat(m_info);
if (patterns.isEmpty()) {
result.error = QString("Invalid TDD slot pattern for Carrier #%1").arg(m_carrierIndex);
return result;
}
// Verify the validity of TDD parameters
bool validationFailed = false;
for (const auto& p : patterns) {
if (p.periodicity <= 0 || p.nrofDownlinkSlots < 0 || p.nrofUplinkSlots < 0 ||
p.nrofDownlinkSymbols < 0 || p.nrofUplinkSymbols < 0)
{
result.error = QString("TDD values must be positive for Carrier #%1 (Periodicity > 0)").arg(m_carrierIndex);
validationFailed = true;
break;
}
}
if (validationFailed) {
return result;
}
// Calculate TDD throughput
auto common_res = common(m_info, band);
auto final_res = calcOverhead(band, common_res);
auto perc = calcSlotPercent(patterns, m_info);
double dlPc = perc["D"];
double ulPc = perc["U"];
if (m_info.tddFlexData) {
dlPc += perc["F"];
ulPc += perc["F"];
}
res = {final_res.first * dlPc, final_res.second * ulPc};
} else if (type == "SDL" || type == "SUL") {
auto common_res = common(m_info, band);
auto final_res = calcOverhead(band, common_res);
if (type == "SDL") res = {final_res.first, 0.0};
else res = {0.0, final_res.second}; // SUL should use uplink conf for calculation
}
// Store the final calculation result in the result object
result.downlink = reduce(res.first);
result.uplink = reduce(res.second);
// Update individual row title
const auto& nrRb = AppConfig::instance().nrRbData;
QString freqRangeStr = QString::number(band["freqrange"].toInt());
int rbDl = nrRb[freqRangeStr].toObject()[QString::number(m_info.dlBandwidth)].toObject()[QString::number(m_info.scs)].toInt(0);
int rbUl = nrRb[freqRangeStr].toObject()[QString::number(m_info.ulBandwidth)].toObject()[QString::number(m_info.scs)].toInt(0);
QString dlText = result.downlink > 0 ? QString("%1Mbps ↓").arg(round2(result.downlink)) : "";
QString ulText = result.uplink > 0 ? QString("%1Mbps ↑").arg(round2(result.uplink)) : "";
QStringList speedsParts;
if (!dlText.isEmpty()) speedsParts << dlText;
if (!ulText.isEmpty()) speedsParts << ulText;
QString speeds = speedsParts.join(" && "); // Button text needs '&&' escape to be distinguished from shortcut key
if (speeds.isEmpty()) speeds = "No Data Throughput";
QString bwText = QString("%1MHz (%2 RBs)").arg(m_info.dlBandwidth).arg(rbDl);
if (type == "FDD" && m_info.dlBandwidth != m_info.ulBandwidth) {
bwText += QString(" ↓ && %1MHz (%2 RBs) ↑").arg(m_info.ulBandwidth).arg(rbUl);
}
QString bandTxt = QString("Band n%1: %2MHz, ").arg(m_info.band).arg(band["frequency"].toString());
QJsonArray range = band["range"].toArray();
bandTxt += (range.size() == 2)
? QString("Uplink: %1MHz, Downlink: %2MHz").arg(range[0].toString()).arg(range[1].toString())
: QString("Range: %1MHz").arg(range[0].toString());
updateTitle(QString("Carrier #%1: %2, %3\n%4").arg(m_carrierIndex).arg(bwText).arg(speeds).arg(bandTxt));
return result;
}
Carrier5GInfo CarrierRow5G::getCarrierInfo() const
{
return m_info;
}
void CarrierRow5G::setupInitialState()
{
ui->removeButton->setVisible(!m_isPrimary);
ui->uplinkAggButton->setVisible(!m_isPrimary);
ui->contentWidget->setVisible(m_isPrimary);
setCarrierIndex(m_carrierId + 1); // Default index
reset5GSelectors();
// Default TDD values
ui->tddPattern1PeriodLineEdit->setText(m_info.tddPattern1Period);
ui->tddPattern1SlotLineEdit->setText(m_info.tddPattern1CustomSlot);
ui->tddPattern1SymbLineEdit->setText(m_info.tddPattern1CustomSymb);
}
void CarrierRow5G::connectSignals()
{
connect(ui->headerButton, &QPushButton::clicked, this, &CarrierRow5G::onHeaderClicked);
connect(ui->removeButton, &QPushButton::clicked, this, &CarrierRow5G::onRemoveClicked);
connect(ui->uplinkAggButton, &QPushButton::clicked, this, &CarrierRow5G::onUplinkAggClicked);
connect(ui->flexSymbolButton, &QPushButton::clicked, this, &CarrierRow5G::onFlexSymbolClicked);
// Data change signals
QList<QComboBox*> comboBoxes = this->findChildren<QComboBox*>();
for(auto cb : comboBoxes) {
connect(cb, QOverload<int>::of(&QComboBox::activated), this, &CarrierRow5G::onStateChanged);
}
QList<QLineEdit*> lineEdits = this->findChildren<QLineEdit*>();
for(auto le : lineEdits) {
connect(le, &QLineEdit::editingFinished, this, &CarrierRow5G::onStateChanged);
}
}
void CarrierRow5G::onHeaderClicked()
{
ui->contentWidget->setVisible(!ui->contentWidget->isVisible());
}
void CarrierRow5G::onRemoveClicked()
{
emit removeClicked(m_carrierId);
}
void CarrierRow5G::onUplinkAggClicked()
{
m_info.uplinkAggregation = !m_info.uplinkAggregation;
ui->uplinkAggButton->setText(m_info.uplinkAggregation ? "Deaggregate Uplink" : "Aggregate Uplink");
emit dataChanged();
}
void CarrierRow5G::onFlexSymbolClicked()
{
m_info.tddFlexData = !m_info.tddFlexData;
ui->flexSymbolButton->setText(m_info.tddFlexData ? "Data" : "Guard Period");
emit dataChanged();
}
void CarrierRow5G::updateStateFromUI()
{
m_info.band = ui->bandComboBox->currentData().toString();
m_info.scs = ui->scsComboBox->currentData().toInt();
m_info.dlBandwidth = ui->dlBandwidthComboBox->currentData().toInt();
m_info.ulBandwidth = ui->ulBandwidthComboBox->currentData().toInt();
m_info.sfactor = ui->sfactorComboBox->currentData().toDouble();
m_info.dlLayers = ui->dlLayersComboBox->currentData().toInt();
m_info.ulLayers = ui->ulLayersComboBox->currentData().toInt();
m_info.dlModulation = ui->dlModComboBox->currentData().toInt();
m_info.ulModulation = ui->ulModComboBox->currentData().toInt();
m_info.tddPattern1Period = ui->tddPattern1PeriodLineEdit->text();
m_info.tddPattern1CustomSlot = ui->tddPattern1SlotLineEdit->text();
m_info.tddPattern1CustomSymb = ui->tddPattern1SymbLineEdit->text();
m_info.tddPattern2Period = ui->tddPattern2PeriodLineEdit->text();
m_info.tddPattern2CustomSlot = ui->tddPattern2SlotLineEdit->text();
m_info.tddPattern2CustomSymb = ui->tddPattern2SymbLineEdit->text();
}
void CarrierRow5G::onStateChanged()
{
QString oldBand = m_info.band;
int oldScs = m_info.scs;
updateStateFromUI();
if (m_info.band != oldBand) {
// Before resetting for a new band, save any state information that needs to be preserved.
auto [was_ua, was_tfd, band] = std::make_tuple(
m_info.uplinkAggregation, m_info.tddFlexData, m_info.band);
// Reset
m_info = Carrier5GInfo();
// Restore frequency band and its saved state
m_info.band = band;
m_info.uplinkAggregation = was_ua;
m_info.tddFlexData = was_tfd;
reset5GSelectors();
const auto& bandData = AppConfig::instance().nrBandData[m_info.band].toObject();
updateCarrierStructure(bandData);
if (!bandData.isEmpty()) {
setCarrierDefaults(bandData);
populateAllSelectors(bandData);
updateAllInputsFromState();
}
} else if (m_info.scs != oldScs) {
const auto& bandData = AppConfig::instance().nrBandData[m_info.band].toObject();
populateBandwidth(bandData);
// After populating, our selection might have been reset if the old BW is not available for the new SCS.
// So we re-read the UI state.
updateStateFromUI();
}
emit dataChanged();
}
void CarrierRow5G::populateBands()
{
ui->bandComboBox->blockSignals(true);
ui->bandComboBox->clear();
ui->bandComboBox->addItem("Select NR-Band...", "0");
const auto& nrBands = AppConfig::instance().nrBandData;
QList<int> bandKeys;
for(auto it = nrBands.constBegin(); it != nrBands.constEnd(); ++it) {
bandKeys.append(it.key().toInt());
}
std::sort(bandKeys.begin(), bandKeys.end());
for (int key : bandKeys) {
QString bandNum = QString::number(key);
QJsonObject data = nrBands[bandNum].toObject();
if (data.contains("frequency")) {
QString text = QString("n%1 | %2 %3MHz")
.arg(bandNum)
.arg(data["type"].toString())
.arg(data["frequency"].toString());
ui->bandComboBox->addItem(text, bandNum);
}
}
ui->bandComboBox->blockSignals(false);
}
void CarrierRow5G::reset5GSelectors()
{
// Clear all selectors except band
QList<QComboBox*> selectors = {ui->scsComboBox, ui->dlBandwidthComboBox, ui->ulBandwidthComboBox, ui->sfactorComboBox,
ui->dlLayersComboBox, ui->ulLayersComboBox, ui->dlModComboBox, ui->ulModComboBox};
for (auto* cb : selectors) {
cb->blockSignals(true);
cb->clear();
cb->addItem("Select...");
cb->blockSignals(false);
}
}
void CarrierRow5G::setCarrierDefaults(const QJsonObject &bandData)
{
const auto& scsbw_dl = bandData["scsbw"].toObject()["dl"].toObject();
// Default SCS
QList<int> scsKeys;
for(auto it = scsbw_dl.constBegin(); it != scsbw_dl.constEnd(); ++it) {
if(!it.value().toArray().isEmpty()) scsKeys.append(it.key().toInt());
}
std::sort(scsKeys.begin(), scsKeys.end());
if (m_info.scs == 0) {
m_info.scs = scsKeys.contains(30) ? 30 : (scsKeys.isEmpty() ? 0 : scsKeys.first());
}
// Default Bandwidth
QJsonArray bwOptions = scsbw_dl[QString::number(m_info.scs)].toArray();
QList<int> bws;
for(const auto& val : bwOptions) bws.append(val.toInt());
if (m_info.dlBandwidth == 0) {
m_info.dlBandwidth = bws.contains(20) ? 20 : (bws.isEmpty() ? 0 : bws.first());
}
if (bandData["type"].toString() == "FDD" && m_info.ulBandwidth == 0) {
m_info.ulBandwidth = m_info.dlBandwidth;
}
}
void CarrierRow5G::updateCarrierStructure(const QJsonObject& bandData)
{
QString type = bandData["type"].toString();
bool isFdd = (type == "FDD");
bool isTdd = (type == "TDD");
bool isSdl = (type == "SDL");
bool isSul = (type == "SUL");
ui->ulBandwidthLabel->setVisible(isFdd);
ui->ulBandwidthComboBox->setVisible(isFdd);
ui->dlBandwidthLabel->setText(isFdd ? "Downlink Bandwidth" : "Bandwidth");
// Preset case (FDD/TDD), both visible
bool showDlSelectors = true;
bool showUlSelectors = true;
if (isSdl) {
// SDL only has downlink settings, hides uplink settings
showUlSelectors = false;
} else if (isSul) {
// Vice versa for SUL
showDlSelectors = false;
}
// Apply visibility settings
ui->dlLayersLabel->setVisible(showDlSelectors);
ui->dlLayersComboBox->setVisible(showDlSelectors);
ui->dlModLabel->setVisible(showDlSelectors);
ui->dlModComboBox->setVisible(showDlSelectors);
ui->ulLayersLabel->setVisible(showUlSelectors);
ui->ulLayersComboBox->setVisible(showUlSelectors);
ui->ulModLabel->setVisible(showUlSelectors);
ui->ulModComboBox->setVisible(showUlSelectors);
QList<QWidget*> tddWidgets = {
ui->tddPattern1Group, ui->tddPattern2Group,
ui->flexSymbolLabel, ui->flexSymbolButton
};
for(auto w : tddWidgets) {
w->setVisible(isTdd);
}
}
void CarrierRow5G::updateAllInputsFromState()
{
auto findAndSet = [&](QComboBox* cb, const QVariant& val) {
int index = cb->findData(val);
if (index != -1) {
cb->setCurrentIndex(index);
}
};
findAndSet(ui->scsComboBox, m_info.scs);
findAndSet(ui->dlBandwidthComboBox, m_info.dlBandwidth);
findAndSet(ui->ulBandwidthComboBox, m_info.ulBandwidth);
findAndSet(ui->sfactorComboBox, m_info.sfactor);
findAndSet(ui->dlLayersComboBox, m_info.dlLayers);
findAndSet(ui->ulLayersComboBox, m_info.ulLayers);
findAndSet(ui->dlModComboBox, m_info.dlModulation);
findAndSet(ui->ulModComboBox, m_info.ulModulation);
}
void CarrierRow5G::populateAllSelectors(const QJsonObject &bandData)
{
// Static selectors
const QVariantMap dlLayers = {{"1",1},{"2",2},{"4",4},{"8",8}};
const QVariantMap ulLayers = {{"1",1},{"2",2},{"4",4}};
const QVariantMap mod = {{"BPSK",1},{"QPSK",2},{"16QAM",4},{"64QAM",6},{"256QAM",8}};
const QVariantMap sfactor = {{"0.4",0.4},{"0.5",0.5},{"0.65",0.65},{"0.75",0.75},{"0.8",0.8},{"1.0",1.0}};
populateScs(bandData);
populateBandwidth(bandData);
populateGenericSelector(ui->dlLayersComboBox, dlLayers, m_info.dlLayers);
populateGenericSelector(ui->ulLayersComboBox, ulLayers, m_info.ulLayers);
populateGenericSelector(ui->dlModComboBox, mod, m_info.dlModulation);
populateGenericSelector(ui->ulModComboBox, mod, m_info.ulModulation);
populateGenericSelector(ui->sfactorComboBox, sfactor, m_info.sfactor);
}
void CarrierRow5G::populateScs(const QJsonObject &bandData)
{
const auto& scsbw_dl = bandData["scsbw"].toObject()["dl"].toObject();
QVariantMap scsOptions;
QList<int> scsKeys;
for(auto it = scsbw_dl.constBegin(); it != scsbw_dl.constEnd(); ++it) {
if(!it.value().toArray().isEmpty()) scsKeys.append(it.key().toInt());
}
std::sort(scsKeys.begin(), scsKeys.end());
for(int key : scsKeys) {
scsOptions[QString::number(key)] = key;
}
populateGenericSelector(ui->scsComboBox, scsOptions, m_info.scs, "KHz", true);
}
void CarrierRow5G::populateBandwidth(const QJsonObject &bandData)
{
const auto& scsbw = bandData["scsbw"].toObject();
const auto& dlScsBwMap = scsbw["dl"].toObject();
QJsonArray dlBwArray = dlScsBwMap[QString::number(m_info.scs)].toArray();
QVariantMap dlBwOptions;
for(const auto& val : dlBwArray) dlBwOptions[QString::number(val.toInt())] = val.toInt();
populateGenericSelector(ui->dlBandwidthComboBox, dlBwOptions, m_info.dlBandwidth, "MHz", true);
if (bandData["type"].toString() == "FDD") {
const auto& ulScsBwMap = scsbw.contains("ul") ? scsbw["ul"].toObject() : dlScsBwMap;
QJsonArray ulBwArray = ulScsBwMap[QString::number(m_info.scs)].toArray();
QVariantMap ulBwOptions;
for(const auto& val : ulBwArray) ulBwOptions[QString::number(val.toInt())] = val.toInt();
populateGenericSelector(ui->ulBandwidthComboBox, ulBwOptions, m_info.ulBandwidth, "MHz", true);
}
}
void CarrierRow5G::populateGenericSelector(QComboBox *comboBox, const QVariantMap &data, const QVariant &selectedValue, const QString &unit, bool valueAsText)
{
comboBox->blockSignals(true);
comboBox->clear();
if (data.isEmpty()) {
comboBox->addItem("N/A");
comboBox->setEnabled(false);
} else {
comboBox->addItem("Select...", QVariant(0));
for(auto it = data.constBegin(); it != data.constEnd(); ++it) {
QString text = valueAsText ? it.key() : it.key();
comboBox->addItem(text + " " + unit, it.value());
}
comboBox->setEnabled(true);
}
int index = comboBox->findData(selectedValue);
if(index != -1) {
comboBox->setCurrentIndex(index);
} else if (comboBox->count() > 1 && selectedValue != 0) {
// If the previously selected value is not available, default to the first valid option.
comboBox->setCurrentIndex(1);
}
comboBox->blockSignals(false);
}