blob: d56b5c6c768b901bbf11b12c79384ed1565e6312 [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;
Thomas Tsou85b179d2013-11-15 21:14:33 -050073 bool extref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040074 Transceiver::FillerType filler;
Thomas Tsou85b179d2013-11-15 21:14:33 -050075 bool diversity;
Thomas Tsou8e17df72014-03-06 14:16:11 -050076 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040077 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040078 bool swap_channels;
Tom Tsoub0aefcb2016-03-06 03:44:34 -080079 bool edge;
Thomas Tsou85b179d2013-11-15 21:14:33 -050080};
81
Thomas Tsou4de70be2013-11-17 18:54:52 -050082ConfigurationTable gConfig;
Thomas Tsou85b179d2013-11-15 21:14:33 -050083
84volatile bool gshutdown = false;
85
86/* Run sanity check on configuration table
87 * The global table constructor cannot provide notification in the
Thomas Tsou4de70be2013-11-17 18:54:52 -050088 * event of failure. Make sure that we can access the database,
Thomas Tsou85b179d2013-11-15 21:14:33 -050089 * write to it, and that it contains the bare minimum required keys.
90 */
Thomas Tsou4de70be2013-11-17 18:54:52 -050091bool testConfig()
Thomas Tsou85b179d2013-11-15 21:14:33 -050092{
Thomas Tsou4de70be2013-11-17 18:54:52 -050093 int val = 9999;
Thomas Tsou85b179d2013-11-15 21:14:33 -050094 std::string test = "asldfkjsaldkf";
95 const char *key = "Log.Level";
96
Thomas Tsou85b179d2013-11-15 21:14:33 -050097 /* Attempt to query */
98 try {
99 gConfig.getStr(key);
100 } catch (...) {
101 std::cerr << std::endl;
102 std::cerr << "Config: Failed query required key " << key
103 << std::endl;
104 return false;
105 }
106
Thomas Tsou4de70be2013-11-17 18:54:52 -0500107 /* Attempt to set a test value in the global config */
108 if (!gConfig.set(test, val)) {
109 std::cerr << std::endl;
110 std::cerr << "Config: Failed to set test key" << std::endl;
111 return false;
112 } else {
113 gConfig.remove(test);
114 }
115
Thomas Tsou85b179d2013-11-15 21:14:33 -0500116 return true;
117}
118
Thomas Tsou4de70be2013-11-17 18:54:52 -0500119
Thomas Tsou85b179d2013-11-15 21:14:33 -0500120/* Setup configuration values
121 * Don't query the existence of the Log.Level because it's a
122 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500123 * table will crash or will have already crashed. Everything else we
124 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500125 * are empty.
126 */
127bool trx_setup_config(struct trx_config *config)
128{
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800129 std::string refstr, fillstr, divstr, edgestr;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500130
Thomas Tsou4de70be2013-11-17 18:54:52 -0500131 if (!testConfig())
Thomas Tsou85b179d2013-11-15 21:14:33 -0500132 return false;
133
134 if (config->log_level == "")
135 config->log_level = gConfig.getStr("Log.Level");
136
137 if (!config->port) {
138 if (gConfig.defines("TRX.Port"))
139 config->port = gConfig.getNum("TRX.Port");
140 else
141 config->port = DEFAULT_TRX_PORT;
142 }
143
144 if (config->addr == "") {
145 if (gConfig.defines("TRX.IP"))
146 config->addr = gConfig.getStr("TRX.IP");
147 else
148 config->addr = DEFAULT_TRX_IP;
149 }
150
151 if (!config->extref) {
152 if (gConfig.defines("TRX.Reference"))
153 config->extref = gConfig.getNum("TRX.Reference");
154 else
155 config->extref = DEFAULT_EXTREF;
156 }
157
158 if (!config->diversity) {
159 if (gConfig.defines("TRX.Diversity"))
160 config->diversity = gConfig.getNum("TRX.Diversity");
161 else
162 config->diversity = DEFAULT_DIVERSITY;
163 }
164
Thomas Tsou85b179d2013-11-15 21:14:33 -0500165 /* Diversity only supported on 2 channels */
166 if (config->diversity)
167 config->chans = 2;
168
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800169 edgestr = config->edge ? "Enabled" : "Disabled";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500170 refstr = config->extref ? "Enabled" : "Disabled";
171 divstr = config->diversity ? "Enabled" : "Disabled";
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400172 switch (config->filler) {
173 case Transceiver::FILLER_DUMMY:
174 fillstr = "Dummy bursts";
175 break;
176 case Transceiver::FILLER_ZERO:
177 fillstr = "Disabled";
178 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800179 case Transceiver::FILLER_NORM_RAND:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400180 fillstr = "Normal busrts with random payload";
181 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800182 case Transceiver::FILLER_EDGE_RAND:
183 fillstr = "EDGE busrts with random payload";
184 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300185 case Transceiver::FILLER_ACCESS_RAND:
186 fillstr = "Access busrts with random payload";
187 break;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400188 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500189
190 std::ostringstream ost("");
191 ost << "Config Settings" << std::endl;
192 ost << " Log Level............... " << config->log_level << std::endl;
193 ost << " Device args............. " << config->dev_args << std::endl;
194 ost << " TRX Base Port........... " << config->port << std::endl;
195 ost << " TRX Address............. " << config->addr << std::endl;
196 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800197 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Alexander Chemeris1ab5e7f2016-04-20 08:44:55 +0300198 ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800199 ost << " EDGE support............ " << edgestr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500200 ost << " External Reference...... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500201 ost << " C0 Filler Table......... " << fillstr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500202 ost << " Diversity............... " << divstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500203 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400204 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400205 ost << " Swap channels........... " << config->swap_channels << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500206 std::cout << ost << std::endl;
207
208 return true;
209}
210
211/* Create radio interface
212 * The interface consists of sample rate changes, frequency shifts,
213 * channel multiplexing, and other conversions. The transceiver core
214 * accepts input vectors sampled at multiples of the GSM symbol rate.
215 * The radio interface connects the main transceiver with the device
216 * object, which may be operating some other rate.
217 */
218RadioInterface *makeRadioInterface(struct trx_config *config,
219 RadioDevice *usrp, int type)
220{
221 RadioInterface *radio = NULL;
222
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800223 if ((config->rx_sps != 1) && (type != RadioDevice::NORMAL)) {
224 LOG(ALERT) << "Unsupported radio interface configuration";
225 }
226
Thomas Tsou85b179d2013-11-15 21:14:33 -0500227 switch (type) {
228 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800229 radio = new RadioInterface(usrp, config->tx_sps,
230 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500231 break;
232 case RadioDevice::RESAMP_64M:
233 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800234 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
235 config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500236 break;
237 case RadioDevice::DIVERSITY:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800238 radio = new RadioInterfaceDiversity(usrp, config->tx_sps,
239 config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500240 break;
241 default:
242 LOG(ALERT) << "Unsupported radio interface configuration";
243 return NULL;
244 }
245
246 if (!radio->init(type)) {
247 LOG(ALERT) << "Failed to initialize radio interface";
248 return NULL;
249 }
250
251 return radio;
252}
253
254/* Create transceiver core
255 * The multi-threaded modem core operates at multiples of the GSM rate of
256 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
257 * and decoding schemes. Also included are the socket interfaces for
258 * connecting to the upper layer stack.
259 */
260Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
261{
262 Transceiver *trx;
263 VectorFIFO *fifo;
264
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800265 trx = new Transceiver(config->port, config->addr.c_str(),
266 config->tx_sps, config->rx_sps, config->chans,
267 GSM::Time(3,0), radio, config->rssi_offset);
Tom Tsou64ad7122015-05-19 18:26:31 -0700268 if (!trx->init(config->filler, config->rtsc)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500269 LOG(ALERT) << "Failed to initialize transceiver";
270 delete trx;
271 return NULL;
272 }
273
274 for (size_t i = 0; i < config->chans; i++) {
275 fifo = radio->receiveFIFO(i);
276 if (fifo && trx->receiveFIFO(fifo, i))
277 continue;
278
279 LOG(ALERT) << "Could not attach FIFO to channel " << i;
280 delete trx;
281 return NULL;
282 }
283
284 return trx;
285}
286
287static void sig_handler(int signo)
288{
289 fprintf(stdout, "Received shutdown signal");
290 gshutdown = true;
291}
292
293static void setup_signal_handlers()
294{
295 if (signal(SIGINT, sig_handler) == SIG_ERR) {
296 fprintf(stderr, "Failed to install SIGINT signal handler\n");
297 exit(EXIT_FAILURE);
298 }
299 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
300 fprintf(stderr, "Couldn't install SIGTERM signal handler\n");
301 exit( EXIT_FAILURE);
302 }
303}
304
305static void print_help()
306{
307 fprintf(stdout, "Options:\n"
308 " -h This text\n"
309 " -a UHD device args\n"
310 " -l Logging level (%s)\n"
311 " -i IP address of GSM core\n"
312 " -p Base port number\n"
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800313 " -e Enable EDGE receiver\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500314 " -d Enable dual channel diversity receiver\n"
315 " -x Enable external 10 MHz reference\n"
316 " -s Samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500317 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500318 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700319 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemerise8905a02015-06-03 23:47:56 -0400320 " -r Random burst test mode with TSC\n"
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300321 " -A Random burst test mode with Access Bursts\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400322 " -R RSSI to dBm offset in dB (default=0)\n"
323 " -S Swap channels (UmTRX only)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500324 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
325}
326
327static void handle_options(int argc, char **argv, struct trx_config *config)
328{
329 int option;
330
331 config->port = 0;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800332 config->tx_sps = DEFAULT_TX_SPS;
333 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700334 config->chans = DEFAULT_CHANS;
335 config->rtsc = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500336 config->extref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700337 config->filler = Transceiver::FILLER_ZERO;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500338 config->diversity = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500339 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400340 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400341 config->swap_channels = false;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800342 config->edge = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500343
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300344 while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:AR:Se")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500345 switch (option) {
346 case 'h':
347 print_help();
348 exit(0);
349 break;
350 case 'a':
351 config->dev_args = optarg;
352 break;
353 case 'l':
354 config->log_level = optarg;
355 break;
356 case 'i':
357 config->addr = optarg;
358 break;
359 case 'p':
360 config->port = atoi(optarg);
361 break;
362 case 'c':
363 config->chans = atoi(optarg);
364 break;
365 case 'd':
366 config->diversity = true;
367 break;
368 case 'x':
369 config->extref = true;
370 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500371 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700372 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500373 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500374 case 'o':
375 config->offset = atof(optarg);
376 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500377 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800378 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700379 break;
380 case 'r':
381 config->rtsc = atoi(optarg);
Tom Tsouaf717b22016-03-06 22:19:15 -0800382 config->filler = Transceiver::FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500383 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300384 case 'A':
385 config->filler = Transceiver::FILLER_ACCESS_RAND;
386 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400387 case 'R':
388 config->rssi_offset = atof(optarg);
389 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400390 case 'S':
391 config->swap_channels = true;
392 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800393 case 'e':
394 config->edge = true;
395 config->rx_sps = 4;
396 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500397 default:
398 print_help();
399 exit(0);
400 }
401 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700402
Tom Tsouaf717b22016-03-06 22:19:15 -0800403 if (config->edge && (config->filler == Transceiver::FILLER_NORM_RAND))
404 config->filler = Transceiver::FILLER_EDGE_RAND;
405
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800406 if ((config->tx_sps != 1) && (config->tx_sps != 4)) {
407 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou64ad7122015-05-19 18:26:31 -0700408 print_help();
409 exit(0);
410 }
411
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800412 if (config->edge && (config->tx_sps != 4)) {
413 printf("EDGE only supported at 4 samples per symbol\n\n");
414 print_help();
415 exit(0);
416 }
417
Tom Tsou64ad7122015-05-19 18:26:31 -0700418 if (config->rtsc > 7) {
419 printf("Invalid training sequence %i\n\n", config->rtsc);
420 print_help();
421 exit(0);
422 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500423}
424
425int main(int argc, char *argv[])
426{
427 int type, chans;
428 RadioDevice *usrp;
429 RadioInterface *radio = NULL;
430 Transceiver *trx = NULL;
431 struct trx_config config;
432
433 handle_options(argc, argv, &config);
434
435 setup_signal_handlers();
436
437 /* Check database sanity */
438 if (!trx_setup_config(&config)) {
439 std::cerr << "Config: Database failure - exiting" << std::endl;
440 return EXIT_FAILURE;
441 }
442
443 gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7);
444
445 srandom(time(NULL));
446
447 /* Create the low level device object */
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800448 usrp = RadioDevice::make(config.tx_sps, config.rx_sps, config.chans,
Thomas Tsou8e17df72014-03-06 14:16:11 -0500449 config.diversity, config.offset);
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400450 type = usrp->open(config.dev_args, config.extref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500451 if (type < 0) {
452 LOG(ALERT) << "Failed to create radio device" << std::endl;
453 goto shutdown;
454 }
455
456 /* Setup the appropriate device interface */
457 radio = makeRadioInterface(&config, usrp, type);
458 if (!radio)
459 goto shutdown;
460
461 /* Create the transceiver core */
462 trx = makeTransceiver(&config, radio);
463 if (!trx)
464 goto shutdown;
465
Thomas Tsou85b179d2013-11-15 21:14:33 -0500466 chans = trx->numChans();
467 std::cout << "-- Transceiver active with "
468 << chans << " channel(s)" << std::endl;
469
470 while (!gshutdown)
471 sleep(1);
472
473shutdown:
474 std::cout << "Shutting down transceiver..." << std::endl;
475
476 delete trx;
477 delete radio;
478 delete usrp;
479
480 return 0;
481}