blob: 241e69c29fd6534b92df0796a291a44c8a0bd385 [file] [log] [blame]
Thomas Tsou85b179d2013-11-15 21:14:33 -05001/*
2 * Copyright (C) 2013 Thomas Tsou <tom@tsou.cc>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "Transceiver.h"
24#include "radioDevice.h"
25
26#include <time.h>
27#include <signal.h>
28#include <stdlib.h>
29#include <unistd.h>
30
31#include <GSMCommon.h>
32#include <Logger.h>
33#include <Configuration.h>
34
Thomas Tsou85b179d2013-11-15 21:14:33 -050035/* Samples-per-symbol for downlink path
36 * 4 - Uses precision modulator (more computation, less distortion)
37 * 1 - Uses minimized modulator (less computation, more distortion)
38 *
39 * Other values are invalid. Receive path (uplink) is always
Tom Tsou0fe41a52016-05-03 15:14:45 -070040 * downsampled to 1 sps. Default to 4 sps for all cases.
Thomas Tsou85b179d2013-11-15 21:14:33 -050041 */
Tom Tsou5cd70dc2016-03-06 01:28:40 -080042#define DEFAULT_TX_SPS 4
Thomas Tsou85b179d2013-11-15 21:14:33 -050043
Tom Tsou5cd70dc2016-03-06 01:28:40 -080044/*
45 * Samples-per-symbol for uplink (receiver) path
46 * Do not modify this value. EDGE configures 4 sps automatically on
47 * B200/B210 devices only. Use of 4 sps on the receive path for other
48 * configurations is not supported.
49 */
50#define DEFAULT_RX_SPS 1
51
Thomas Tsou85b179d2013-11-15 21:14:33 -050052/* Default configuration parameters
53 * Note that these values are only used if the particular key does not
54 * exist in the configuration database. IP port and address values will
55 * typically be overwritten by the OpenBTS.db values. Other values will
56 * not be in the database by default.
57 */
58#define DEFAULT_TRX_PORT 5700
59#define DEFAULT_TRX_IP "127.0.0.1"
60#define DEFAULT_EXTREF false
61#define DEFAULT_DIVERSITY false
62#define DEFAULT_CHANS 1
63
64struct trx_config {
65 std::string log_level;
66 std::string addr;
67 std::string dev_args;
68 unsigned port;
Tom Tsou5cd70dc2016-03-06 01:28:40 -080069 unsigned tx_sps;
70 unsigned rx_sps;
Thomas Tsou85b179d2013-11-15 21:14:33 -050071 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070072 unsigned rtsc;
Alexander Chemeris37c52c72016-03-25 18:28:34 +030073 unsigned rach_delay;
Thomas Tsou85b179d2013-11-15 21:14:33 -050074 bool extref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040075 Transceiver::FillerType filler;
Thomas Tsou85b179d2013-11-15 21:14:33 -050076 bool diversity;
Thomas Tsou8e17df72014-03-06 14:16:11 -050077 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040078 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040079 bool swap_channels;
Tom Tsoub0aefcb2016-03-06 03:44:34 -080080 bool edge;
Thomas Tsou85b179d2013-11-15 21:14:33 -050081};
82
Thomas Tsou4de70be2013-11-17 18:54:52 -050083ConfigurationTable gConfig;
Thomas Tsou85b179d2013-11-15 21:14:33 -050084
85volatile bool gshutdown = false;
86
87/* Run sanity check on configuration table
88 * The global table constructor cannot provide notification in the
Thomas Tsou4de70be2013-11-17 18:54:52 -050089 * event of failure. Make sure that we can access the database,
Thomas Tsou85b179d2013-11-15 21:14:33 -050090 * write to it, and that it contains the bare minimum required keys.
91 */
Thomas Tsou4de70be2013-11-17 18:54:52 -050092bool testConfig()
Thomas Tsou85b179d2013-11-15 21:14:33 -050093{
Thomas Tsou4de70be2013-11-17 18:54:52 -050094 int val = 9999;
Thomas Tsou85b179d2013-11-15 21:14:33 -050095 std::string test = "asldfkjsaldkf";
96 const char *key = "Log.Level";
97
Thomas Tsou85b179d2013-11-15 21:14:33 -050098 /* Attempt to query */
99 try {
100 gConfig.getStr(key);
101 } catch (...) {
102 std::cerr << std::endl;
103 std::cerr << "Config: Failed query required key " << key
104 << std::endl;
105 return false;
106 }
107
Thomas Tsou4de70be2013-11-17 18:54:52 -0500108 /* Attempt to set a test value in the global config */
109 if (!gConfig.set(test, val)) {
110 std::cerr << std::endl;
111 std::cerr << "Config: Failed to set test key" << std::endl;
112 return false;
113 } else {
114 gConfig.remove(test);
115 }
116
Thomas Tsou85b179d2013-11-15 21:14:33 -0500117 return true;
118}
119
Thomas Tsou4de70be2013-11-17 18:54:52 -0500120
Thomas Tsou85b179d2013-11-15 21:14:33 -0500121/* Setup configuration values
122 * Don't query the existence of the Log.Level because it's a
123 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500124 * table will crash or will have already crashed. Everything else we
125 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500126 * are empty.
127 */
128bool trx_setup_config(struct trx_config *config)
129{
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800130 std::string refstr, fillstr, divstr, edgestr;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500131
Thomas Tsou4de70be2013-11-17 18:54:52 -0500132 if (!testConfig())
Thomas Tsou85b179d2013-11-15 21:14:33 -0500133 return false;
134
135 if (config->log_level == "")
136 config->log_level = gConfig.getStr("Log.Level");
137
138 if (!config->port) {
139 if (gConfig.defines("TRX.Port"))
140 config->port = gConfig.getNum("TRX.Port");
141 else
142 config->port = DEFAULT_TRX_PORT;
143 }
144
145 if (config->addr == "") {
146 if (gConfig.defines("TRX.IP"))
147 config->addr = gConfig.getStr("TRX.IP");
148 else
149 config->addr = DEFAULT_TRX_IP;
150 }
151
152 if (!config->extref) {
153 if (gConfig.defines("TRX.Reference"))
154 config->extref = gConfig.getNum("TRX.Reference");
155 else
156 config->extref = DEFAULT_EXTREF;
157 }
158
159 if (!config->diversity) {
160 if (gConfig.defines("TRX.Diversity"))
161 config->diversity = gConfig.getNum("TRX.Diversity");
162 else
163 config->diversity = DEFAULT_DIVERSITY;
164 }
165
Thomas Tsou85b179d2013-11-15 21:14:33 -0500166 /* Diversity only supported on 2 channels */
167 if (config->diversity)
168 config->chans = 2;
169
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800170 edgestr = config->edge ? "Enabled" : "Disabled";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500171 refstr = config->extref ? "Enabled" : "Disabled";
172 divstr = config->diversity ? "Enabled" : "Disabled";
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400173 switch (config->filler) {
174 case Transceiver::FILLER_DUMMY:
175 fillstr = "Dummy bursts";
176 break;
177 case Transceiver::FILLER_ZERO:
178 fillstr = "Disabled";
179 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800180 case Transceiver::FILLER_NORM_RAND:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400181 fillstr = "Normal busrts with random payload";
182 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800183 case Transceiver::FILLER_EDGE_RAND:
184 fillstr = "EDGE busrts with random payload";
185 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300186 case Transceiver::FILLER_ACCESS_RAND:
187 fillstr = "Access busrts with random payload";
188 break;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400189 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500190
191 std::ostringstream ost("");
192 ost << "Config Settings" << std::endl;
193 ost << " Log Level............... " << config->log_level << std::endl;
194 ost << " Device args............. " << config->dev_args << std::endl;
195 ost << " TRX Base Port........... " << config->port << std::endl;
196 ost << " TRX Address............. " << config->addr << std::endl;
197 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800198 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Alexander Chemeris1ab5e7f2016-04-20 08:44:55 +0300199 ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800200 ost << " EDGE support............ " << edgestr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500201 ost << " External Reference...... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500202 ost << " C0 Filler Table......... " << fillstr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500203 ost << " Diversity............... " << divstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500204 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400205 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400206 ost << " Swap channels........... " << config->swap_channels << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500207 std::cout << ost << std::endl;
208
209 return true;
210}
211
212/* Create radio interface
213 * The interface consists of sample rate changes, frequency shifts,
214 * channel multiplexing, and other conversions. The transceiver core
215 * accepts input vectors sampled at multiples of the GSM symbol rate.
216 * The radio interface connects the main transceiver with the device
217 * object, which may be operating some other rate.
218 */
219RadioInterface *makeRadioInterface(struct trx_config *config,
220 RadioDevice *usrp, int type)
221{
222 RadioInterface *radio = NULL;
223
224 switch (type) {
225 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800226 radio = new RadioInterface(usrp, config->tx_sps,
227 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500228 break;
229 case RadioDevice::RESAMP_64M:
230 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800231 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
232 config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500233 break;
234 case RadioDevice::DIVERSITY:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800235 radio = new RadioInterfaceDiversity(usrp, config->tx_sps,
236 config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500237 break;
238 default:
239 LOG(ALERT) << "Unsupported radio interface configuration";
240 return NULL;
241 }
242
243 if (!radio->init(type)) {
244 LOG(ALERT) << "Failed to initialize radio interface";
245 return NULL;
246 }
247
248 return radio;
249}
250
251/* Create transceiver core
252 * The multi-threaded modem core operates at multiples of the GSM rate of
253 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
254 * and decoding schemes. Also included are the socket interfaces for
255 * connecting to the upper layer stack.
256 */
257Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
258{
259 Transceiver *trx;
260 VectorFIFO *fifo;
261
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800262 trx = new Transceiver(config->port, config->addr.c_str(),
263 config->tx_sps, config->rx_sps, config->chans,
264 GSM::Time(3,0), radio, config->rssi_offset);
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300265 if (!trx->init(config->filler, config->rtsc, config->rach_delay)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500266 LOG(ALERT) << "Failed to initialize transceiver";
267 delete trx;
268 return NULL;
269 }
270
271 for (size_t i = 0; i < config->chans; i++) {
272 fifo = radio->receiveFIFO(i);
273 if (fifo && trx->receiveFIFO(fifo, i))
274 continue;
275
276 LOG(ALERT) << "Could not attach FIFO to channel " << i;
277 delete trx;
278 return NULL;
279 }
280
281 return trx;
282}
283
284static void sig_handler(int signo)
285{
286 fprintf(stdout, "Received shutdown signal");
287 gshutdown = true;
288}
289
290static void setup_signal_handlers()
291{
292 if (signal(SIGINT, sig_handler) == SIG_ERR) {
293 fprintf(stderr, "Failed to install SIGINT signal handler\n");
294 exit(EXIT_FAILURE);
295 }
296 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
297 fprintf(stderr, "Couldn't install SIGTERM signal handler\n");
298 exit( EXIT_FAILURE);
299 }
300}
301
302static void print_help()
303{
304 fprintf(stdout, "Options:\n"
305 " -h This text\n"
306 " -a UHD device args\n"
307 " -l Logging level (%s)\n"
308 " -i IP address of GSM core\n"
309 " -p Base port number\n"
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800310 " -e Enable EDGE receiver\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500311 " -d Enable dual channel diversity receiver\n"
312 " -x Enable external 10 MHz reference\n"
Tom Tsou2e4ed102016-06-27 15:39:16 -0700313 " -s Tx samples-per-symbol (1 or 4)\n"
314 " -b Rx samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500315 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500316 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700317 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300318 " -r Random Normal Burst test mode with TSC\n"
319 " -A Random Access Burst test mode with delay\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400320 " -R RSSI to dBm offset in dB (default=0)\n"
321 " -S Swap channels (UmTRX only)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500322 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
323}
324
325static void handle_options(int argc, char **argv, struct trx_config *config)
326{
327 int option;
328
329 config->port = 0;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800330 config->tx_sps = DEFAULT_TX_SPS;
331 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700332 config->chans = DEFAULT_CHANS;
333 config->rtsc = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300334 config->rach_delay = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500335 config->extref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700336 config->filler = Transceiver::FILLER_ZERO;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500337 config->diversity = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500338 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400339 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400340 config->swap_channels = false;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800341 config->edge = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500342
Tom Tsou2e4ed102016-06-27 15:39:16 -0700343 while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:b:r:A:R:Se")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500344 switch (option) {
345 case 'h':
346 print_help();
347 exit(0);
348 break;
349 case 'a':
350 config->dev_args = optarg;
351 break;
352 case 'l':
353 config->log_level = optarg;
354 break;
355 case 'i':
356 config->addr = optarg;
357 break;
358 case 'p':
359 config->port = atoi(optarg);
360 break;
361 case 'c':
362 config->chans = atoi(optarg);
363 break;
364 case 'd':
365 config->diversity = true;
366 break;
367 case 'x':
368 config->extref = true;
369 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500370 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700371 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500372 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500373 case 'o':
374 config->offset = atof(optarg);
375 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500376 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800377 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700378 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700379 case 'b':
380 config->rx_sps = atoi(optarg);
381 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700382 case 'r':
383 config->rtsc = atoi(optarg);
Tom Tsouaf717b22016-03-06 22:19:15 -0800384 config->filler = Transceiver::FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500385 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300386 case 'A':
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300387 config->rach_delay = atoi(optarg);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300388 config->filler = Transceiver::FILLER_ACCESS_RAND;
389 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400390 case 'R':
391 config->rssi_offset = atof(optarg);
392 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400393 case 'S':
394 config->swap_channels = true;
395 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800396 case 'e':
397 config->edge = true;
398 config->rx_sps = 4;
399 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500400 default:
401 print_help();
402 exit(0);
403 }
404 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700405
Tom Tsou2e4ed102016-06-27 15:39:16 -0700406 /* Force 4 SPS for EDGE configurations */
407 if (config->edge) {
408 config->tx_sps = 4;
409 config->rx_sps = 4;
410 }
411
Tom Tsouaf717b22016-03-06 22:19:15 -0800412 if (config->edge && (config->filler == Transceiver::FILLER_NORM_RAND))
413 config->filler = Transceiver::FILLER_EDGE_RAND;
414
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800415 if ((config->tx_sps != 1) && (config->tx_sps != 4)) {
416 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou64ad7122015-05-19 18:26:31 -0700417 print_help();
418 exit(0);
419 }
420
421 if (config->rtsc > 7) {
422 printf("Invalid training sequence %i\n\n", config->rtsc);
423 print_help();
424 exit(0);
425 }
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300426
427 if (config->rach_delay > 68) {
428 printf("RACH delay is too big %i\n\n", config->rach_delay);
429 print_help();
430 exit(0);
431 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500432}
433
434int main(int argc, char *argv[])
435{
436 int type, chans;
437 RadioDevice *usrp;
438 RadioInterface *radio = NULL;
439 Transceiver *trx = NULL;
440 struct trx_config config;
441
442 handle_options(argc, argv, &config);
443
444 setup_signal_handlers();
445
446 /* Check database sanity */
447 if (!trx_setup_config(&config)) {
448 std::cerr << "Config: Database failure - exiting" << std::endl;
449 return EXIT_FAILURE;
450 }
451
452 gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7);
453
454 srandom(time(NULL));
455
456 /* Create the low level device object */
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800457 usrp = RadioDevice::make(config.tx_sps, config.rx_sps, config.chans,
Thomas Tsou8e17df72014-03-06 14:16:11 -0500458 config.diversity, config.offset);
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400459 type = usrp->open(config.dev_args, config.extref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500460 if (type < 0) {
461 LOG(ALERT) << "Failed to create radio device" << std::endl;
462 goto shutdown;
463 }
464
465 /* Setup the appropriate device interface */
466 radio = makeRadioInterface(&config, usrp, type);
467 if (!radio)
468 goto shutdown;
469
470 /* Create the transceiver core */
471 trx = makeTransceiver(&config, radio);
472 if (!trx)
473 goto shutdown;
474
Thomas Tsou85b179d2013-11-15 21:14:33 -0500475 chans = trx->numChans();
476 std::cout << "-- Transceiver active with "
477 << chans << " channel(s)" << std::endl;
478
479 while (!gshutdown)
480 sleep(1);
481
482shutdown:
483 std::cout << "Shutting down transceiver..." << std::endl;
484
485 delete trx;
486 delete radio;
487 delete usrp;
488
489 return 0;
490}