blob: 7b9fd7c37b214bb515b78d9bb1702853ec2e4ddc [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)
44#define DEFAULT_SPS 1
45#else
46#define DEFAULT_SPS 4
47#endif
48
49/* Default configuration parameters
50 * Note that these values are only used if the particular key does not
51 * exist in the configuration database. IP port and address values will
52 * typically be overwritten by the OpenBTS.db values. Other values will
53 * not be in the database by default.
54 */
55#define DEFAULT_TRX_PORT 5700
56#define DEFAULT_TRX_IP "127.0.0.1"
57#define DEFAULT_EXTREF false
58#define DEFAULT_DIVERSITY false
59#define DEFAULT_CHANS 1
60
61struct trx_config {
62 std::string log_level;
63 std::string addr;
64 std::string dev_args;
65 unsigned port;
66 unsigned sps;
67 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070068 unsigned rtsc;
Thomas Tsou85b179d2013-11-15 21:14:33 -050069 bool extref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040070 Transceiver::FillerType filler;
Thomas Tsou85b179d2013-11-15 21:14:33 -050071 bool diversity;
Thomas Tsou8e17df72014-03-06 14:16:11 -050072 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040073 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040074 bool swap_channels;
Thomas Tsou85b179d2013-11-15 21:14:33 -050075};
76
Thomas Tsou4de70be2013-11-17 18:54:52 -050077ConfigurationTable gConfig;
Thomas Tsou85b179d2013-11-15 21:14:33 -050078
79volatile bool gshutdown = false;
80
81/* Run sanity check on configuration table
82 * The global table constructor cannot provide notification in the
Thomas Tsou4de70be2013-11-17 18:54:52 -050083 * event of failure. Make sure that we can access the database,
Thomas Tsou85b179d2013-11-15 21:14:33 -050084 * write to it, and that it contains the bare minimum required keys.
85 */
Thomas Tsou4de70be2013-11-17 18:54:52 -050086bool testConfig()
Thomas Tsou85b179d2013-11-15 21:14:33 -050087{
Thomas Tsou4de70be2013-11-17 18:54:52 -050088 int val = 9999;
Thomas Tsou85b179d2013-11-15 21:14:33 -050089 std::string test = "asldfkjsaldkf";
90 const char *key = "Log.Level";
91
Thomas Tsou85b179d2013-11-15 21:14:33 -050092 /* Attempt to query */
93 try {
94 gConfig.getStr(key);
95 } catch (...) {
96 std::cerr << std::endl;
97 std::cerr << "Config: Failed query required key " << key
98 << std::endl;
99 return false;
100 }
101
Thomas Tsou4de70be2013-11-17 18:54:52 -0500102 /* Attempt to set a test value in the global config */
103 if (!gConfig.set(test, val)) {
104 std::cerr << std::endl;
105 std::cerr << "Config: Failed to set test key" << std::endl;
106 return false;
107 } else {
108 gConfig.remove(test);
109 }
110
Thomas Tsou85b179d2013-11-15 21:14:33 -0500111 return true;
112}
113
Thomas Tsou4de70be2013-11-17 18:54:52 -0500114
Thomas Tsou85b179d2013-11-15 21:14:33 -0500115/* Setup configuration values
116 * Don't query the existence of the Log.Level because it's a
117 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500118 * table will crash or will have already crashed. Everything else we
119 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500120 * are empty.
121 */
122bool trx_setup_config(struct trx_config *config)
123{
Thomas Tsou15d743e2014-01-25 02:34:03 -0500124 std::string refstr, fillstr, divstr;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500125
Thomas Tsou4de70be2013-11-17 18:54:52 -0500126 if (!testConfig())
Thomas Tsou85b179d2013-11-15 21:14:33 -0500127 return false;
128
129 if (config->log_level == "")
130 config->log_level = gConfig.getStr("Log.Level");
131
132 if (!config->port) {
133 if (gConfig.defines("TRX.Port"))
134 config->port = gConfig.getNum("TRX.Port");
135 else
136 config->port = DEFAULT_TRX_PORT;
137 }
138
139 if (config->addr == "") {
140 if (gConfig.defines("TRX.IP"))
141 config->addr = gConfig.getStr("TRX.IP");
142 else
143 config->addr = DEFAULT_TRX_IP;
144 }
145
146 if (!config->extref) {
147 if (gConfig.defines("TRX.Reference"))
148 config->extref = gConfig.getNum("TRX.Reference");
149 else
150 config->extref = DEFAULT_EXTREF;
151 }
152
153 if (!config->diversity) {
154 if (gConfig.defines("TRX.Diversity"))
155 config->diversity = gConfig.getNum("TRX.Diversity");
156 else
157 config->diversity = DEFAULT_DIVERSITY;
158 }
159
Thomas Tsou85b179d2013-11-15 21:14:33 -0500160 /* Diversity only supported on 2 channels */
161 if (config->diversity)
162 config->chans = 2;
163
164 refstr = config->extref ? "Enabled" : "Disabled";
165 divstr = config->diversity ? "Enabled" : "Disabled";
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400166 switch (config->filler) {
167 case Transceiver::FILLER_DUMMY:
168 fillstr = "Dummy bursts";
169 break;
170 case Transceiver::FILLER_ZERO:
171 fillstr = "Disabled";
172 break;
173 case Transceiver::FILLER_RAND:
174 fillstr = "Normal busrts with random payload";
175 break;
176 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500177
178 std::ostringstream ost("");
179 ost << "Config Settings" << std::endl;
180 ost << " Log Level............... " << config->log_level << std::endl;
181 ost << " Device args............. " << config->dev_args << std::endl;
182 ost << " TRX Base Port........... " << config->port << std::endl;
183 ost << " TRX Address............. " << config->addr << std::endl;
184 ost << " Channels................ " << config->chans << std::endl;
185 ost << " Samples-per-Symbol...... " << config->sps << std::endl;
186 ost << " External Reference...... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500187 ost << " C0 Filler Table......... " << fillstr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500188 ost << " Diversity............... " << divstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500189 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400190 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400191 ost << " Swap channels........... " << config->swap_channels << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500192 std::cout << ost << std::endl;
193
194 return true;
195}
196
197/* Create radio interface
198 * The interface consists of sample rate changes, frequency shifts,
199 * channel multiplexing, and other conversions. The transceiver core
200 * accepts input vectors sampled at multiples of the GSM symbol rate.
201 * The radio interface connects the main transceiver with the device
202 * object, which may be operating some other rate.
203 */
204RadioInterface *makeRadioInterface(struct trx_config *config,
205 RadioDevice *usrp, int type)
206{
207 RadioInterface *radio = NULL;
208
209 switch (type) {
210 case RadioDevice::NORMAL:
211 radio = new RadioInterface(usrp, config->sps, config->chans);
212 break;
213 case RadioDevice::RESAMP_64M:
214 case RadioDevice::RESAMP_100M:
215 radio = new RadioInterfaceResamp(usrp,
216 config->sps, config->chans);
217 break;
218 case RadioDevice::DIVERSITY:
219 radio = new RadioInterfaceDiversity(usrp,
220 config->sps, config->chans);
221 break;
222 default:
223 LOG(ALERT) << "Unsupported radio interface configuration";
224 return NULL;
225 }
226
227 if (!radio->init(type)) {
228 LOG(ALERT) << "Failed to initialize radio interface";
229 return NULL;
230 }
231
232 return radio;
233}
234
235/* Create transceiver core
236 * The multi-threaded modem core operates at multiples of the GSM rate of
237 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
238 * and decoding schemes. Also included are the socket interfaces for
239 * connecting to the upper layer stack.
240 */
241Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
242{
243 Transceiver *trx;
244 VectorFIFO *fifo;
245
246 trx = new Transceiver(config->port, config->addr.c_str(), config->sps,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400247 config->chans, GSM::Time(3,0), radio, config->rssi_offset);
Tom Tsou64ad7122015-05-19 18:26:31 -0700248 if (!trx->init(config->filler, config->rtsc)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500249 LOG(ALERT) << "Failed to initialize transceiver";
250 delete trx;
251 return NULL;
252 }
253
254 for (size_t i = 0; i < config->chans; i++) {
255 fifo = radio->receiveFIFO(i);
256 if (fifo && trx->receiveFIFO(fifo, i))
257 continue;
258
259 LOG(ALERT) << "Could not attach FIFO to channel " << i;
260 delete trx;
261 return NULL;
262 }
263
264 return trx;
265}
266
267static void sig_handler(int signo)
268{
269 fprintf(stdout, "Received shutdown signal");
270 gshutdown = true;
271}
272
273static void setup_signal_handlers()
274{
275 if (signal(SIGINT, sig_handler) == SIG_ERR) {
276 fprintf(stderr, "Failed to install SIGINT signal handler\n");
277 exit(EXIT_FAILURE);
278 }
279 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
280 fprintf(stderr, "Couldn't install SIGTERM signal handler\n");
281 exit( EXIT_FAILURE);
282 }
283}
284
285static void print_help()
286{
287 fprintf(stdout, "Options:\n"
288 " -h This text\n"
289 " -a UHD device args\n"
290 " -l Logging level (%s)\n"
291 " -i IP address of GSM core\n"
292 " -p Base port number\n"
293 " -d Enable dual channel diversity receiver\n"
294 " -x Enable external 10 MHz reference\n"
295 " -s Samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500296 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500297 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700298 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemerise8905a02015-06-03 23:47:56 -0400299 " -r Random burst test mode with TSC\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400300 " -R RSSI to dBm offset in dB (default=0)\n"
301 " -S Swap channels (UmTRX only)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500302 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
303}
304
305static void handle_options(int argc, char **argv, struct trx_config *config)
306{
307 int option;
308
309 config->port = 0;
Tom Tsou64ad7122015-05-19 18:26:31 -0700310 config->sps = DEFAULT_SPS;
311 config->chans = DEFAULT_CHANS;
312 config->rtsc = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500313 config->extref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700314 config->filler = Transceiver::FILLER_ZERO;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500315 config->diversity = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500316 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400317 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400318 config->swap_channels = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500319
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400320 while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:R:S")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500321 switch (option) {
322 case 'h':
323 print_help();
324 exit(0);
325 break;
326 case 'a':
327 config->dev_args = optarg;
328 break;
329 case 'l':
330 config->log_level = optarg;
331 break;
332 case 'i':
333 config->addr = optarg;
334 break;
335 case 'p':
336 config->port = atoi(optarg);
337 break;
338 case 'c':
339 config->chans = atoi(optarg);
340 break;
341 case 'd':
342 config->diversity = true;
343 break;
344 case 'x':
345 config->extref = true;
346 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500347 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700348 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500349 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500350 case 'o':
351 config->offset = atof(optarg);
352 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500353 case 's':
354 config->sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700355 break;
356 case 'r':
357 config->rtsc = atoi(optarg);
358 config->filler = Transceiver::FILLER_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500359 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400360 case 'R':
361 config->rssi_offset = atof(optarg);
362 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400363 case 'S':
364 config->swap_channels = true;
365 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500366 default:
367 print_help();
368 exit(0);
369 }
370 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700371
372 if ((config->sps != 1) && (config->sps != 4)) {
373 printf("Unsupported samples-per-symbol %i\n\n", config->sps);
374 print_help();
375 exit(0);
376 }
377
378 if (config->rtsc > 7) {
379 printf("Invalid training sequence %i\n\n", config->rtsc);
380 print_help();
381 exit(0);
382 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500383}
384
385int main(int argc, char *argv[])
386{
387 int type, chans;
388 RadioDevice *usrp;
389 RadioInterface *radio = NULL;
390 Transceiver *trx = NULL;
391 struct trx_config config;
392
393 handle_options(argc, argv, &config);
394
395 setup_signal_handlers();
396
397 /* Check database sanity */
398 if (!trx_setup_config(&config)) {
399 std::cerr << "Config: Database failure - exiting" << std::endl;
400 return EXIT_FAILURE;
401 }
402
403 gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7);
404
405 srandom(time(NULL));
406
407 /* Create the low level device object */
Thomas Tsou8e17df72014-03-06 14:16:11 -0500408 usrp = RadioDevice::make(config.sps, config.chans,
409 config.diversity, config.offset);
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400410 type = usrp->open(config.dev_args, config.extref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500411 if (type < 0) {
412 LOG(ALERT) << "Failed to create radio device" << std::endl;
413 goto shutdown;
414 }
415
416 /* Setup the appropriate device interface */
417 radio = makeRadioInterface(&config, usrp, type);
418 if (!radio)
419 goto shutdown;
420
421 /* Create the transceiver core */
422 trx = makeTransceiver(&config, radio);
423 if (!trx)
424 goto shutdown;
425
Thomas Tsou85b179d2013-11-15 21:14:33 -0500426 chans = trx->numChans();
427 std::cout << "-- Transceiver active with "
428 << chans << " channel(s)" << std::endl;
429
430 while (!gshutdown)
431 sleep(1);
432
433shutdown:
434 std::cout << "Shutting down transceiver..." << std::endl;
435
436 delete trx;
437 delete radio;
438 delete usrp;
439
440 return 0;
441}