-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.cc
490 lines (441 loc) · 19.2 KB
/
main.cc
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
/*
* Copyright 2020-2021 Telecom Paris
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <uhd/utils/thread.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/exception.hpp>
#include <uhd/types/tune_request.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <fstream>
#include <chrono>
#include <libconfig.h++>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include "../lib/rf/rf.h"
#include "../lib/rf/usrp_b200/usrp_b200.h"
#include "../lib/rf/usrp_x300/usrp_x300.h"
#ifdef INCLUDE_N210_OPT
#include "../lib/rf/usrp_usrp2/usrp_usrp2.h"
#endif
#include "../lib/variables/common_variables/common_variables.h"
#include "phy/phy.h"
#include "../lib/phy/libphy/libphy.h"
#include "../lib/asn1c/nr_rrc/SIB1.h"
#include "../lib/utils/common_utils/common_utils.h"
using namespace std;
/*
* Define structure to store found cells
*/
typedef struct found_cell_ {
free5GRAN::band band_obj;
phy phy_obj;
double gain;
int gscn, scs;
bool dci_found;
} found_cell;
void search_cell_with_defined_params(double frequency, double ssb_period, const string &rf_address, free5GRAN::band band, int input_gain);
void scan_bands(vector<free5GRAN::band> BANDS, double ssb_period, const string &rf_address, int input_gain);
void init_logging(const string &level);
int main(int argc, char *argv[]) {
libconfig::Config cfg;
try
{
cfg.readFile(argv[1]);
cout << "Using configuration file $PWD/" << argv[1] << endl;
}
catch(const libconfig::FileIOException &fioex)
{
try
{
string path = "/root/.config/free5GRAN/config/free5GRAN.cfg";
cfg.readFile(path.c_str());
cout << "Using configuration file " << path << endl;
}
catch(const libconfig::FileIOException &fioex)
{
cerr << "Please provide a config file!" << endl;
return(EXIT_FAILURE);
}
}
catch(const libconfig::ParseException &pex)
{
cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << endl;
return(EXIT_FAILURE);
}
try
{
string level;
if (!cfg.lookupValue("logging", level)){
level = "info";
}
init_logging(level);
BOOST_LOG_TRIVIAL(info) << "Initializing free5GRAN";
string func = cfg.lookup("function");
string rf_address;
const libconfig::Setting& root = cfg.getRoot();
if (!root.lookupValue("rf_address", rf_address)) {
rf_address = "";
}
int gain = cfg.lookup("gain");
if (func == "CELL_SEARCH"){
cout << "## CELL SEARCH ##" << endl;
BOOST_LOG_TRIVIAL(info) << "CELL SEARCH";
const libconfig::Setting& cell_info = root["cell_info"];
double ssb_period;
int band;
free5GRAN::band band_obj;
if (!root.lookupValue("ssb_period", ssb_period)){
// Default ssb period to 20 ms
ssb_period = 0.02;
}if (cell_info.lookupValue("band", band) ){
bool found_band = false;
for (int j = 0; j < free5GRAN::NUM_SUPPORTED_BANDS; j ++){
if (free5GRAN::AVAILABLE_BANDS[j].number == band){
band_obj = free5GRAN::AVAILABLE_BANDS[j];
found_band = true;
BOOST_LOG_TRIVIAL(trace) << "Band n" + to_string(band);
break;
}
}
if (found_band) {
double frequency;
int gscn;
if (cell_info.lookupValue("ssb_frequency", frequency) || cell_info.lookupValue("gscn", gscn)){
if (!cell_info.lookupValue("ssb_frequency", frequency)){
frequency = free5GRAN::phy::signal_processing::compute_freq_from_gscn(gscn);
}
search_cell_with_defined_params(frequency, ssb_period, rf_address,band_obj, gain);
}
else {
cerr << "Missing parameters (frequency or GSCN) in config file" << endl;
}
}else {
cout << "BAND not supported" << endl;
}
}
else {
cerr << "Missing parameters (band) in config file" << endl;
}
}else if (func == "BAND_SCAN"){
BOOST_LOG_TRIVIAL(info) << "BAND SCANNING";
cout << "## BAND SCAN ##" << endl;
const libconfig::Setting& bands = root["bands"];
int count = bands.getLength();
vector<free5GRAN::band> band_array;
BOOST_LOG_TRIVIAL(trace) << "Adding bands to scan: ";
for (int i = 0; i < count; i ++){
int band_id = bands[i];
for (int j = 0; j < free5GRAN::NUM_SUPPORTED_BANDS; j ++){
if (free5GRAN::AVAILABLE_BANDS[j].number == band_id){
band_array.push_back(free5GRAN::AVAILABLE_BANDS[j]);
BOOST_LOG_TRIVIAL(trace) << "Band n" + to_string(band_id);
break;
}
}
}
double ssb_period;
if (!root.lookupValue("ssb_period", ssb_period)){
// Default ssb period to 20 ms
ssb_period = 0.02;
}
scan_bands(band_array, ssb_period, rf_address, gain);
}
}
catch(const libconfig::SettingNotFoundException &nfex)
{
cerr << "Missing function, RF device address of gain in config file" << endl;
}
return EXIT_SUCCESS;
}
void scan_bands(vector<free5GRAN::band> BANDS, double ssb_period, const string &rf_address, int input_gain){
/*
* Cell default parameters
*/
double freq(1000e6);
double rate(3.84e6);
double gain(input_gain);
double bw(3.84e6);
/*
* Find USRP device with input parameters parameters
*/
free5GRAN::rf_device chosen_device;
free5GRAN::utils::common_utils::select_rf_device(chosen_device, rf_address);
if (chosen_device.type.empty()){
cout << "Cannot find USRP device ! Exiting..." << endl;
return;
}else {
cout << "Using USRP " << chosen_device.type << " device" << endl;
}
/*
* Create RF device depending on RF type.
*/
free5GRAN::rf *rf_device;
if (chosen_device.type == "b200"){
rf_device = new free5GRAN::usrp_b200(rate, freq, gain, bw, chosen_device);
} else if (chosen_device.type == "x300"){
rf_device = new free5GRAN::usrp_x300(rate, freq, gain, bw, chosen_device);
}
#ifdef INCLUDE_N210_OPT
else if (chosen_device.type == "usrp2"){
rf_device = new free5GRAN::usrp_usrp2(rate, freq, gain, bw, chosen_device);
}
#endif
else {
cout << "Unsupported RF device" << endl;
return;
}
free5GRAN::band current_band;
// Search cell with 15kHz SCS
vector<found_cell> found_cells;
cout << "\n";
auto start = chrono::high_resolution_clock::now();
float received_power;
/*
* Loop over each supported band
*/
for (int i = 0; i < BANDS.size() ; i ++){
current_band = BANDS[i];
BOOST_LOG_TRIVIAL(info) << "###########################################";
BOOST_LOG_TRIVIAL(info) << "Scanning band n"+ to_string(current_band.number);
/*
* Search a cell by scanning different possible GSCN
*/
for (int gscn = current_band.min_gscn; gscn <= current_band.max_gscn; gscn ++){
/*
* Reconfigure RF device and instanciate a phy object
*/
freq = free5GRAN::phy::signal_processing::compute_freq_from_gscn(gscn);
BOOST_LOG_TRIVIAL(info) << "Scanning gscn= "+ to_string(gscn) + " and freq= " + to_string(freq/1e6) + " MHz";
free5GRAN::bandwidth_info band_info;
if (freq < 3000e6){
band_info = free5GRAN::BANDWIDTH_15_KHZ;
}else {
band_info = free5GRAN::BANDWIDTH_30_KHZ;
}
BOOST_LOG_TRIVIAL(trace) << "Bandwidth informations are: ";
BOOST_LOG_TRIVIAL(trace) << "SCS: " + to_string(band_info.scs);
int fft_size_pss = 128;
rf_device -> setCenterFrequency(freq);
rf_device -> setSampleRate(fft_size_pss * band_info.scs);
rf_device -> setGain(100);
phy phy_layer(rf_device, ssb_period, fft_size_pss, band_info.scs, current_band);
cout << "\r## Searching in band n" + to_string(current_band.number) + " - " + to_string((((float) gscn - (float) current_band.min_gscn)/((float) current_band.max_gscn - (float) current_band.min_gscn)) * 100.0) + "% (found " + to_string(found_cells.size()) + " cells)";
/*
* If a PSS is correlate with a good peak value
*/
if (phy_layer.cell_synchronization(received_power)==0){
/*
* Power down-ramping to avoid saturation
*/
BOOST_LOG_TRIVIAL(trace) << "Received power: " + to_string(received_power) + " dB (Gain= "+ to_string(rf_device -> getGain()) +" dB)";
while (received_power > -2 && rf_device -> getGain() > 0){
rf_device -> setGain(rf_device -> getGain()-10);
phy_layer.cell_synchronization(received_power);
BOOST_LOG_TRIVIAL(trace) << "Received power: " + to_string(received_power) + " dB (Gain= "+ to_string(rf_device -> getGain()) +" dB)";
}
BOOST_LOG_TRIVIAL(trace) << "Final received power: " + to_string(received_power) + " dB (Final gain= "+ to_string(rf_device -> getGain()) +" dB)";
/*
* If the re-computed PCI is equal to the initially computed one
*/
rf_device -> setSampleRate(30.72e6);
int new_fft_size;
new_fft_size = (int) (30.72e6/band_info.scs);
phy_layer.reconfigure(new_fft_size);
if(phy_layer.extract_pbch() == 0){
//phy_layer.print_cell_info();
bool dci_found;
phy_layer.search_pdcch(dci_found);
if (dci_found){
phy_layer.extract_pdsch();
}
found_cell new_cell;
new_cell.band_obj = current_band;
new_cell.scs = current_band.scs;
new_cell.gscn = gscn;
new_cell.phy_obj = phy_layer;
new_cell.gain = rf_device -> getGain();
new_cell.dci_found = dci_found;
found_cells.push_back(new_cell);
}
}
}
}
for (int i = 0; i < found_cells.size(); i ++){
cout << "\n";
cout << "#######################################################################" << endl;
cout << "########################## CELL " + to_string(i) + " ##########################" << endl;
cout << "#######################################################################" << endl;
cout << "# GSCN: " + to_string(found_cells[i].gscn) + " / Frequency: " + to_string(free5GRAN::phy::signal_processing::compute_freq_from_gscn(found_cells[i].gscn)/1e6) + " MHz (band n" + to_string(found_cells[i].band_obj.number) + ")" << endl;
cout << "# Gain: " + to_string(found_cells[i].gain) + " dB"<< endl;
found_cells[i].phy_obj.print_cell_info();
if (found_cells[i].dci_found || found_cells[i].phy_obj.getSIB1RV() == 1 || found_cells[i].phy_obj.getSIB1RV() == 2){
found_cells[i].phy_obj.print_dci_info();
try {
found_cells[i].phy_obj.print_sib1();
} catch (const std::exception& e) { cout << "NO SIB1" << endl; }
}else {
cout << "###### DCI" << endl;
cout << "# DCI not found, not CRC validated, or unsupported RV";
cout << "\n";
}
cout << "#######################################################################" << endl;
}
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
cout << "Executed in "+ to_string(duration.count()/1e6) + " seconds" << endl;
}
void search_cell_with_defined_params(double frequency, double ssb_period, const string &rf_address, free5GRAN::band band, int input_gain){
/*
* Cell parameters
*/
double gain(input_gain);
free5GRAN::bandwidth_info band_info;
if (frequency < 3000e6){
band_info = free5GRAN::BANDWIDTH_15_KHZ;
}else {
band_info = free5GRAN::BANDWIDTH_30_KHZ;
}
BOOST_LOG_TRIVIAL(trace) << "Bandwidth informations are: ";
BOOST_LOG_TRIVIAL(trace) << "SCS: " + to_string(band_info.scs);
/*
* Instanciate a rf layer instance to provide exchanges with USRP device
*/
int fft_size_pss = 128;
/*
* Find USRP device with input parameters parameters
*/
free5GRAN::rf_device chosen_device;
free5GRAN::utils::common_utils::select_rf_device(chosen_device, rf_address);
if (chosen_device.type.empty()){
cout << "Cannot find USRP device ! Exiting..." << endl;
return;
}else {
cout << "Using USRP " << chosen_device.type << " device" << endl;
}
/*
* Create RF device depending on RF type.
*/
free5GRAN::rf *rf_device;
if (chosen_device.type == "b200"){
rf_device = new free5GRAN::usrp_b200(fft_size_pss * band_info.scs, frequency, gain, fft_size_pss * band_info.scs, chosen_device);
} else if (chosen_device.type == "x300"){
rf_device = new free5GRAN::usrp_x300(fft_size_pss * band_info.scs, frequency, gain, fft_size_pss * band_info.scs, chosen_device);
}
#ifdef INCLUDE_N210_OPT
else if (chosen_device.type == "usrp2"){
rf_device = new free5GRAN::usrp_usrp2(fft_size_pss * band_info.scs, frequency, gain, fft_size_pss * band_info.scs, chosen_device);
}
#endif
else {
cout << "Unsupported RF device" << endl;
return;
}
//FIX BAND SELECTION
phy phy_layer(rf_device, ssb_period, fft_size_pss, band_info.scs, band);
cout << "\n";
cout << "########################## Searching cell ##########################" << endl;
cout << "\n";
cout << "###### RADIO" << endl;
cout << "# Frequency: " + to_string(frequency/1e6) + " MHz" << endl;
cout << "# SCS: " + to_string(band_info.scs/1e3) + " kHz" << endl;
cout << "\n";
BOOST_LOG_TRIVIAL(info) << "Searching cell with frequency = " + to_string(frequency/1e6) + " MHz and SCS = " + to_string(band_info.scs/1e3) + " kHz";
float received_power;
if (phy_layer.cell_synchronization(received_power) == 0){
/*
* Power down-ramping to avoid saturation
*/
cout << "###### Power ramping" << endl;
cout << "Received power: "+ to_string(received_power) + " dB" << endl;
BOOST_LOG_TRIVIAL(trace) << "Received power: " + to_string(received_power) + " dB (Gain= "+ to_string(rf_device -> getGain()) +" dB)";
while (received_power > -2 && rf_device -> getGain() > 0){
rf_device -> setGain(rf_device -> getGain()-10);
phy_layer.cell_synchronization(received_power);
cout << "New gain: "+ to_string(rf_device -> getGain()) + " dB" << endl;
cout << "Received power: "+ to_string(received_power) + " dB" << endl;
BOOST_LOG_TRIVIAL(trace) << "Received power: " + to_string(received_power) + " dB (Gain= "+ to_string(rf_device -> getGain()) +" dB)";
}
cout << "\n";
BOOST_LOG_TRIVIAL(trace) << "Final received power: " + to_string(received_power) + " dB (Final gain= "+ to_string(rf_device -> getGain()) +" dB)";
rf_device -> setSampleRate(30.72e6);
int new_fft_size;
new_fft_size = (int) (rf_device -> getSampleRate()/band_info.scs);
phy_layer.reconfigure(new_fft_size);
if(phy_layer.extract_pbch() == 0){
phy_layer.print_cell_info();
bool dci_found;
phy_layer.search_pdcch(dci_found);
phy_layer.print_dci_info();
if (dci_found){
phy_layer.extract_pdsch();
phy_layer.print_sib1();
}
cout << "####################################################################" << endl;
}else {
cout << "########################## Cell not confirmed ##########################" << endl;
cout << "####################################################################" << endl;
}
}else {
cout << "########################## Cell not found ##########################" << endl;
cout << "####################################################################" << endl;
}
cout << "####################################################################" << endl;
}
void init_logging(const string &level)
{
boost::log::register_simple_formatter_factory<boost::log::trivial::severity_level, char>("Severity");
boost::log::add_file_log
(
boost::log::keywords::file_name = "/var/log/free5GRAN/free5GRAN.log",
boost::log::keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%"
);
if (level == "trace"){
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::trace
);
}else if (level == "debug"){
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::debug
);
}else if (level == "info"){
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::info
);
}else if (level == "warning"){
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::warning
);
}else if (level == "error"){
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::error
);
}else {
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::fatal
);
}
boost::log::add_common_attributes();
}