blob: c8fd9a854c112773cf3864e15b79b1645bb319e0 [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
40 * downsampled to 1 sps. Default to 4 sps for all cases except for
41 * ARM and non-SIMD enabled architectures.
42 */
43#if defined(HAVE_NEON) || !defined(HAVE_SSE3)
Tom Tsou5cd70dc2016-03-06 01:28:40 -080044#define DEFAULT_TX_SPS 1
Thomas Tsou85b179d2013-11-15 21:14:33 -050045#else
Tom Tsou5cd70dc2016-03-06 01:28:40 -080046#define DEFAULT_TX_SPS 4
Thomas Tsou85b179d2013-11-15 21:14:33 -050047#endif
48
Tom Tsou5cd70dc2016-03-06 01:28:40 -080049/*
50 * Samples-per-symbol for uplink (receiver) path
51 * Do not modify this value. EDGE configures 4 sps automatically on
52 * B200/B210 devices only. Use of 4 sps on the receive path for other
53 * configurations is not supported.
54 */
55#define DEFAULT_RX_SPS 1
56
Thomas Tsou85b179d2013-11-15 21:14:33 -050057/* Default configuration parameters
58 * Note that these values are only used if the particular key does not
59 * exist in the configuration database. IP port and address values will
60 * typically be overwritten by the OpenBTS.db values. Other values will
61 * not be in the database by default.
62 */
63#define DEFAULT_TRX_PORT 5700
64#define DEFAULT_TRX_IP "127.0.0.1"
65#define DEFAULT_EXTREF false
66#define DEFAULT_DIVERSITY false
67#define DEFAULT_CHANS 1
68
69struct trx_config {
70 std::string log_level;
71 std::string addr;
72 std::string dev_args;
73 unsigned port;
Tom Tsou5cd70dc2016-03-06 01:28:40 -080074 unsigned tx_sps;
75 unsigned rx_sps;
Thomas Tsou85b179d2013-11-15 21:14:33 -050076 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070077 unsigned rtsc;
Thomas Tsou85b179d2013-11-15 21:14:33 -050078 bool extref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040079 Transceiver::FillerType filler;
Thomas Tsou85b179d2013-11-15 21:14:33 -050080 bool diversity;
Thomas Tsou8e17df72014-03-06 14:16:11 -050081 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040082 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040083 bool swap_channels;
Thomas Tsou85b179d2013-11-15 21:14:33 -050084};
85
Thomas Tsou4de70be2013-11-17 18:54:52 -050086ConfigurationTable gConfig;
Thomas Tsou85b179d2013-11-15 21:14:33 -050087
88volatile bool gshutdown = false;
89
90/* Run sanity check on configuration table
91 * The global table constructor cannot provide notification in the
Thomas Tsou4de70be2013-11-17 18:54:52 -050092 * event of failure. Make sure that we can access the database,
Thomas Tsou85b179d2013-11-15 21:14:33 -050093 * write to it, and that it contains the bare minimum required keys.
94 */
Thomas Tsou4de70be2013-11-17 18:54:52 -050095bool testConfig()
Thomas Tsou85b179d2013-11-15 21:14:33 -050096{
Thomas Tsou4de70be2013-11-17 18:54:52 -050097 int val = 9999;
Thomas Tsou85b179d2013-11-15 21:14:33 -050098 std::string test = "asldfkjsaldkf";
99 const char *key = "Log.Level";
100
Thomas Tsou85b179d2013-11-15 21:14:33 -0500101 /* Attempt to query */
102 try {
103 gConfig.getStr(key);
104 } catch (...) {
105 std::cerr << std::endl;
106 std::cerr << "Config: Failed query required key " << key
107 << std::endl;
108 return false;
109 }
110
Thomas Tsou4de70be2013-11-17 18:54:52 -0500111 /* Attempt to set a test value in the global config */
112 if (!gConfig.set(test, val)) {
113 std::cerr << std::endl;
114 std::cerr << "Config: Failed to set test key" << std::endl;
115 return false;
116 } else {
117 gConfig.remove(test);
118 }
119
Thomas Tsou85b179d2013-11-15 21:14:33 -0500120 return true;
121}
122
Thomas Tsou4de70be2013-11-17 18:54:52 -0500123
Thomas Tsou85b179d2013-11-15 21:14:33 -0500124/* Setup configuration values
125 * Don't query the existence of the Log.Level because it's a
126 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500127 * table will crash or will have already crashed. Everything else we
128 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500129 * are empty.
130 */
131bool trx_setup_config(struct trx_config *config)
132{
Thomas Tsou15d743e2014-01-25 02:34:03 -0500133 std::string refstr, fillstr, divstr;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500134
Thomas Tsou4de70be2013-11-17 18:54:52 -0500135 if (!testConfig())
Thomas Tsou85b179d2013-11-15 21:14:33 -0500136 return false;
137
138 if (config->log_level == "")
139 config->log_level = gConfig.getStr("Log.Level");
140
141 if (!config->port) {
142 if (gConfig.defines("TRX.Port"))
143 config->port = gConfig.getNum("TRX.Port");
144 else
145 config->port = DEFAULT_TRX_PORT;
146 }
147
148 if (config->addr == "") {
149 if (gConfig.defines("TRX.IP"))
150 config->addr = gConfig.getStr("TRX.IP");
151 else
152 config->addr = DEFAULT_TRX_IP;
153 }
154
155 if (!config->extref) {
156 if (gConfig.defines("TRX.Reference"))
157 config->extref = gConfig.getNum("TRX.Reference");
158 else
159 config->extref = DEFAULT_EXTREF;
160 }
161
162 if (!config->diversity) {
163 if (gConfig.defines("TRX.Diversity"))
164 config->diversity = gConfig.getNum("TRX.Diversity");
165 else
166 config->diversity = DEFAULT_DIVERSITY;
167 }
168
Thomas Tsou85b179d2013-11-15 21:14:33 -0500169 /* Diversity only supported on 2 channels */
170 if (config->diversity)
171 config->chans = 2;
172
173 refstr = config->extref ? "Enabled" : "Disabled";
174 divstr = config->diversity ? "Enabled" : "Disabled";
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400175 switch (config->filler) {
176 case Transceiver::FILLER_DUMMY:
177 fillstr = "Dummy bursts";
178 break;
179 case Transceiver::FILLER_ZERO:
180 fillstr = "Disabled";
181 break;
182 case Transceiver::FILLER_RAND:
183 fillstr = "Normal busrts with random payload";
184 break;
185 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500186
187 std::ostringstream ost("");
188 ost << "Config Settings" << std::endl;
189 ost << " Log Level............... " << config->log_level << std::endl;
190 ost << " Device args............. " << config->dev_args << std::endl;
191 ost << " TRX Base Port........... " << config->port << std::endl;
192 ost << " TRX Address............. " << config->addr << std::endl;
193 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800194 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500195 ost << " External Reference...... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500196 ost << " C0 Filler Table......... " << fillstr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500197 ost << " Diversity............... " << divstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500198 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400199 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400200 ost << " Swap channels........... " << config->swap_channels << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500201 std::cout << ost << std::endl;
202
203 return true;
204}
205
206/* Create radio interface
207 * The interface consists of sample rate changes, frequency shifts,
208 * channel multiplexing, and other conversions. The transceiver core
209 * accepts input vectors sampled at multiples of the GSM symbol rate.
210 * The radio interface connects the main transceiver with the device
211 * object, which may be operating some other rate.
212 */
213RadioInterface *makeRadioInterface(struct trx_config *config,
214 RadioDevice *usrp, int type)
215{
216 RadioInterface *radio = NULL;
217
218 switch (type) {
219 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800220 radio = new RadioInterface(usrp, config->tx_sps,
221 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500222 break;
223 case RadioDevice::RESAMP_64M:
224 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800225 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
226 config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500227 break;
228 case RadioDevice::DIVERSITY:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800229 radio = new RadioInterfaceDiversity(usrp, config->tx_sps,
230 config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500231 break;
232 default:
233 LOG(ALERT) << "Unsupported radio interface configuration";
234 return NULL;
235 }
236
237 if (!radio->init(type)) {
238 LOG(ALERT) << "Failed to initialize radio interface";
239 return NULL;
240 }
241
242 return radio;
243}
244
245/* Create transceiver core
246 * The multi-threaded modem core operates at multiples of the GSM rate of
247 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
248 * and decoding schemes. Also included are the socket interfaces for
249 * connecting to the upper layer stack.
250 */
251Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
252{
253 Transceiver *trx;
254 VectorFIFO *fifo;
255
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800256 trx = new Transceiver(config->port, config->addr.c_str(),
257 config->tx_sps, config->rx_sps, config->chans,
258 GSM::Time(3,0), radio, config->rssi_offset);
Tom Tsou64ad7122015-05-19 18:26:31 -0700259 if (!trx->init(config->filler, config->rtsc)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500260 LOG(ALERT) << "Failed to initialize transceiver";
261 delete trx;
262 return NULL;
263 }
264
265 for (size_t i = 0; i < config->chans; i++) {
266 fifo = radio->receiveFIFO(i);
267 if (fifo && trx->receiveFIFO(fifo, i))
268 continue;
269
270 LOG(ALERT) << "Could not attach FIFO to channel " << i;
271 delete trx;
272 return NULL;
273 }
274
275 return trx;
276}
277
278static void sig_handler(int signo)
279{
280 fprintf(stdout, "Received shutdown signal");
281 gshutdown = true;
282}
283
284static void setup_signal_handlers()
285{
286 if (signal(SIGINT, sig_handler) == SIG_ERR) {
287 fprintf(stderr, "Failed to install SIGINT signal handler\n");
288 exit(EXIT_FAILURE);
289 }
290 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
291 fprintf(stderr, "Couldn't install SIGTERM signal handler\n");
292 exit( EXIT_FAILURE);
293 }
294}
295
296static void print_help()
297{
298 fprintf(stdout, "Options:\n"
299 " -h This text\n"
300 " -a UHD device args\n"
301 " -l Logging level (%s)\n"
302 " -i IP address of GSM core\n"
303 " -p Base port number\n"
304 " -d Enable dual channel diversity receiver\n"
305 " -x Enable external 10 MHz reference\n"
306 " -s Samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500307 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500308 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700309 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemerise8905a02015-06-03 23:47:56 -0400310 " -r Random burst test mode with TSC\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400311 " -R RSSI to dBm offset in dB (default=0)\n"
312 " -S Swap channels (UmTRX only)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500313 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
314}
315
316static void handle_options(int argc, char **argv, struct trx_config *config)
317{
318 int option;
319
320 config->port = 0;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800321 config->tx_sps = DEFAULT_TX_SPS;
322 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700323 config->chans = DEFAULT_CHANS;
324 config->rtsc = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500325 config->extref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700326 config->filler = Transceiver::FILLER_ZERO;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500327 config->diversity = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500328 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400329 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400330 config->swap_channels = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500331
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400332 while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:R:S")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500333 switch (option) {
334 case 'h':
335 print_help();
336 exit(0);
337 break;
338 case 'a':
339 config->dev_args = optarg;
340 break;
341 case 'l':
342 config->log_level = optarg;
343 break;
344 case 'i':
345 config->addr = optarg;
346 break;
347 case 'p':
348 config->port = atoi(optarg);
349 break;
350 case 'c':
351 config->chans = atoi(optarg);
352 break;
353 case 'd':
354 config->diversity = true;
355 break;
356 case 'x':
357 config->extref = true;
358 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500359 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700360 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500361 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500362 case 'o':
363 config->offset = atof(optarg);
364 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500365 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800366 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700367 break;
368 case 'r':
369 config->rtsc = atoi(optarg);
370 config->filler = Transceiver::FILLER_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500371 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400372 case 'R':
373 config->rssi_offset = atof(optarg);
374 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400375 case 'S':
376 config->swap_channels = true;
377 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500378 default:
379 print_help();
380 exit(0);
381 }
382 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700383
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800384 if ((config->tx_sps != 1) && (config->tx_sps != 4)) {
385 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou64ad7122015-05-19 18:26:31 -0700386 print_help();
387 exit(0);
388 }
389
390 if (config->rtsc > 7) {
391 printf("Invalid training sequence %i\n\n", config->rtsc);
392 print_help();
393 exit(0);
394 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500395}
396
397int main(int argc, char *argv[])
398{
399 int type, chans;
400 RadioDevice *usrp;
401 RadioInterface *radio = NULL;
402 Transceiver *trx = NULL;
403 struct trx_config config;
404
405 handle_options(argc, argv, &config);
406
407 setup_signal_handlers();
408
409 /* Check database sanity */
410 if (!trx_setup_config(&config)) {
411 std::cerr << "Config: Database failure - exiting" << std::endl;
412 return EXIT_FAILURE;
413 }
414
415 gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7);
416
417 srandom(time(NULL));
418
419 /* Create the low level device object */
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800420 usrp = RadioDevice::make(config.tx_sps, config.rx_sps, config.chans,
Thomas Tsou8e17df72014-03-06 14:16:11 -0500421 config.diversity, config.offset);
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400422 type = usrp->open(config.dev_args, config.extref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500423 if (type < 0) {
424 LOG(ALERT) << "Failed to create radio device" << std::endl;
425 goto shutdown;
426 }
427
428 /* Setup the appropriate device interface */
429 radio = makeRadioInterface(&config, usrp, type);
430 if (!radio)
431 goto shutdown;
432
433 /* Create the transceiver core */
434 trx = makeTransceiver(&config, radio);
435 if (!trx)
436 goto shutdown;
437
Thomas Tsou85b179d2013-11-15 21:14:33 -0500438 chans = trx->numChans();
439 std::cout << "-- Transceiver active with "
440 << chans << " channel(s)" << std::endl;
441
442 while (!gshutdown)
443 sleep(1);
444
445shutdown:
446 std::cout << "Shutting down transceiver..." << std::endl;
447
448 delete trx;
449 delete radio;
450 delete usrp;
451
452 return 0;
453}