blob: 048b9f80e76b9c269ad542caca54c2536de10f65 [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;
Thomas Tsou85b179d2013-11-15 21:14:33 -050074};
75
Thomas Tsou4de70be2013-11-17 18:54:52 -050076ConfigurationTable gConfig;
Thomas Tsou85b179d2013-11-15 21:14:33 -050077
78volatile bool gshutdown = false;
79
80/* Run sanity check on configuration table
81 * The global table constructor cannot provide notification in the
Thomas Tsou4de70be2013-11-17 18:54:52 -050082 * event of failure. Make sure that we can access the database,
Thomas Tsou85b179d2013-11-15 21:14:33 -050083 * write to it, and that it contains the bare minimum required keys.
84 */
Thomas Tsou4de70be2013-11-17 18:54:52 -050085bool testConfig()
Thomas Tsou85b179d2013-11-15 21:14:33 -050086{
Thomas Tsou4de70be2013-11-17 18:54:52 -050087 int val = 9999;
Thomas Tsou85b179d2013-11-15 21:14:33 -050088 std::string test = "asldfkjsaldkf";
89 const char *key = "Log.Level";
90
Thomas Tsou85b179d2013-11-15 21:14:33 -050091 /* Attempt to query */
92 try {
93 gConfig.getStr(key);
94 } catch (...) {
95 std::cerr << std::endl;
96 std::cerr << "Config: Failed query required key " << key
97 << std::endl;
98 return false;
99 }
100
Thomas Tsou4de70be2013-11-17 18:54:52 -0500101 /* Attempt to set a test value in the global config */
102 if (!gConfig.set(test, val)) {
103 std::cerr << std::endl;
104 std::cerr << "Config: Failed to set test key" << std::endl;
105 return false;
106 } else {
107 gConfig.remove(test);
108 }
109
Thomas Tsou85b179d2013-11-15 21:14:33 -0500110 return true;
111}
112
Thomas Tsou4de70be2013-11-17 18:54:52 -0500113
Thomas Tsou85b179d2013-11-15 21:14:33 -0500114/* Setup configuration values
115 * Don't query the existence of the Log.Level because it's a
116 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500117 * table will crash or will have already crashed. Everything else we
118 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500119 * are empty.
120 */
121bool trx_setup_config(struct trx_config *config)
122{
Thomas Tsou15d743e2014-01-25 02:34:03 -0500123 std::string refstr, fillstr, divstr;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500124
Thomas Tsou4de70be2013-11-17 18:54:52 -0500125 if (!testConfig())
Thomas Tsou85b179d2013-11-15 21:14:33 -0500126 return false;
127
128 if (config->log_level == "")
129 config->log_level = gConfig.getStr("Log.Level");
130
131 if (!config->port) {
132 if (gConfig.defines("TRX.Port"))
133 config->port = gConfig.getNum("TRX.Port");
134 else
135 config->port = DEFAULT_TRX_PORT;
136 }
137
138 if (config->addr == "") {
139 if (gConfig.defines("TRX.IP"))
140 config->addr = gConfig.getStr("TRX.IP");
141 else
142 config->addr = DEFAULT_TRX_IP;
143 }
144
145 if (!config->extref) {
146 if (gConfig.defines("TRX.Reference"))
147 config->extref = gConfig.getNum("TRX.Reference");
148 else
149 config->extref = DEFAULT_EXTREF;
150 }
151
152 if (!config->diversity) {
153 if (gConfig.defines("TRX.Diversity"))
154 config->diversity = gConfig.getNum("TRX.Diversity");
155 else
156 config->diversity = DEFAULT_DIVERSITY;
157 }
158
Thomas Tsou85b179d2013-11-15 21:14:33 -0500159 /* Diversity only supported on 2 channels */
160 if (config->diversity)
161 config->chans = 2;
162
163 refstr = config->extref ? "Enabled" : "Disabled";
164 divstr = config->diversity ? "Enabled" : "Disabled";
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400165 switch (config->filler) {
166 case Transceiver::FILLER_DUMMY:
167 fillstr = "Dummy bursts";
168 break;
169 case Transceiver::FILLER_ZERO:
170 fillstr = "Disabled";
171 break;
172 case Transceiver::FILLER_RAND:
173 fillstr = "Normal busrts with random payload";
174 break;
175 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500176
177 std::ostringstream ost("");
178 ost << "Config Settings" << std::endl;
179 ost << " Log Level............... " << config->log_level << std::endl;
180 ost << " Device args............. " << config->dev_args << std::endl;
181 ost << " TRX Base Port........... " << config->port << std::endl;
182 ost << " TRX Address............. " << config->addr << std::endl;
183 ost << " Channels................ " << config->chans << std::endl;
184 ost << " Samples-per-Symbol...... " << config->sps << std::endl;
185 ost << " External Reference...... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500186 ost << " C0 Filler Table......... " << fillstr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500187 ost << " Diversity............... " << divstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500188 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400189 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500190 std::cout << ost << std::endl;
191
192 return true;
193}
194
195/* Create radio interface
196 * The interface consists of sample rate changes, frequency shifts,
197 * channel multiplexing, and other conversions. The transceiver core
198 * accepts input vectors sampled at multiples of the GSM symbol rate.
199 * The radio interface connects the main transceiver with the device
200 * object, which may be operating some other rate.
201 */
202RadioInterface *makeRadioInterface(struct trx_config *config,
203 RadioDevice *usrp, int type)
204{
205 RadioInterface *radio = NULL;
206
207 switch (type) {
208 case RadioDevice::NORMAL:
209 radio = new RadioInterface(usrp, config->sps, config->chans);
210 break;
211 case RadioDevice::RESAMP_64M:
212 case RadioDevice::RESAMP_100M:
213 radio = new RadioInterfaceResamp(usrp,
214 config->sps, config->chans);
215 break;
216 case RadioDevice::DIVERSITY:
217 radio = new RadioInterfaceDiversity(usrp,
218 config->sps, config->chans);
219 break;
220 default:
221 LOG(ALERT) << "Unsupported radio interface configuration";
222 return NULL;
223 }
224
225 if (!radio->init(type)) {
226 LOG(ALERT) << "Failed to initialize radio interface";
227 return NULL;
228 }
229
230 return radio;
231}
232
233/* Create transceiver core
234 * The multi-threaded modem core operates at multiples of the GSM rate of
235 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
236 * and decoding schemes. Also included are the socket interfaces for
237 * connecting to the upper layer stack.
238 */
239Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
240{
241 Transceiver *trx;
242 VectorFIFO *fifo;
243
244 trx = new Transceiver(config->port, config->addr.c_str(), config->sps,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400245 config->chans, GSM::Time(3,0), radio, config->rssi_offset);
Tom Tsou64ad7122015-05-19 18:26:31 -0700246 if (!trx->init(config->filler, config->rtsc)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500247 LOG(ALERT) << "Failed to initialize transceiver";
248 delete trx;
249 return NULL;
250 }
251
252 for (size_t i = 0; i < config->chans; i++) {
253 fifo = radio->receiveFIFO(i);
254 if (fifo && trx->receiveFIFO(fifo, i))
255 continue;
256
257 LOG(ALERT) << "Could not attach FIFO to channel " << i;
258 delete trx;
259 return NULL;
260 }
261
262 return trx;
263}
264
265static void sig_handler(int signo)
266{
267 fprintf(stdout, "Received shutdown signal");
268 gshutdown = true;
269}
270
271static void setup_signal_handlers()
272{
273 if (signal(SIGINT, sig_handler) == SIG_ERR) {
274 fprintf(stderr, "Failed to install SIGINT signal handler\n");
275 exit(EXIT_FAILURE);
276 }
277 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
278 fprintf(stderr, "Couldn't install SIGTERM signal handler\n");
279 exit( EXIT_FAILURE);
280 }
281}
282
283static void print_help()
284{
285 fprintf(stdout, "Options:\n"
286 " -h This text\n"
287 " -a UHD device args\n"
288 " -l Logging level (%s)\n"
289 " -i IP address of GSM core\n"
290 " -p Base port number\n"
291 " -d Enable dual channel diversity receiver\n"
292 " -x Enable external 10 MHz reference\n"
293 " -s Samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500294 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500295 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700296 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemerise8905a02015-06-03 23:47:56 -0400297 " -r Random burst test mode with TSC\n"
298 " -R RSSI to dBm offset in dB (default=0)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500299 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
300}
301
302static void handle_options(int argc, char **argv, struct trx_config *config)
303{
304 int option;
305
306 config->port = 0;
Tom Tsou64ad7122015-05-19 18:26:31 -0700307 config->sps = DEFAULT_SPS;
308 config->chans = DEFAULT_CHANS;
309 config->rtsc = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500310 config->extref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700311 config->filler = Transceiver::FILLER_ZERO;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500312 config->diversity = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500313 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400314 config->rssi_offset = 0.0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500315
Alexander Chemerise8905a02015-06-03 23:47:56 -0400316 while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:R:")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500317 switch (option) {
318 case 'h':
319 print_help();
320 exit(0);
321 break;
322 case 'a':
323 config->dev_args = optarg;
324 break;
325 case 'l':
326 config->log_level = optarg;
327 break;
328 case 'i':
329 config->addr = optarg;
330 break;
331 case 'p':
332 config->port = atoi(optarg);
333 break;
334 case 'c':
335 config->chans = atoi(optarg);
336 break;
337 case 'd':
338 config->diversity = true;
339 break;
340 case 'x':
341 config->extref = true;
342 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500343 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700344 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500345 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500346 case 'o':
347 config->offset = atof(optarg);
348 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500349 case 's':
350 config->sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700351 break;
352 case 'r':
353 config->rtsc = atoi(optarg);
354 config->filler = Transceiver::FILLER_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500355 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400356 case 'R':
357 config->rssi_offset = atof(optarg);
358 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500359 default:
360 print_help();
361 exit(0);
362 }
363 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700364
365 if ((config->sps != 1) && (config->sps != 4)) {
366 printf("Unsupported samples-per-symbol %i\n\n", config->sps);
367 print_help();
368 exit(0);
369 }
370
371 if (config->rtsc > 7) {
372 printf("Invalid training sequence %i\n\n", config->rtsc);
373 print_help();
374 exit(0);
375 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500376}
377
378int main(int argc, char *argv[])
379{
380 int type, chans;
381 RadioDevice *usrp;
382 RadioInterface *radio = NULL;
383 Transceiver *trx = NULL;
384 struct trx_config config;
385
386 handle_options(argc, argv, &config);
387
388 setup_signal_handlers();
389
390 /* Check database sanity */
391 if (!trx_setup_config(&config)) {
392 std::cerr << "Config: Database failure - exiting" << std::endl;
393 return EXIT_FAILURE;
394 }
395
396 gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7);
397
398 srandom(time(NULL));
399
400 /* Create the low level device object */
Thomas Tsou8e17df72014-03-06 14:16:11 -0500401 usrp = RadioDevice::make(config.sps, config.chans,
402 config.diversity, config.offset);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500403 type = usrp->open(config.dev_args, config.extref);
404 if (type < 0) {
405 LOG(ALERT) << "Failed to create radio device" << std::endl;
406 goto shutdown;
407 }
408
409 /* Setup the appropriate device interface */
410 radio = makeRadioInterface(&config, usrp, type);
411 if (!radio)
412 goto shutdown;
413
414 /* Create the transceiver core */
415 trx = makeTransceiver(&config, radio);
416 if (!trx)
417 goto shutdown;
418
Thomas Tsou85b179d2013-11-15 21:14:33 -0500419 chans = trx->numChans();
420 std::cout << "-- Transceiver active with "
421 << chans << " channel(s)" << std::endl;
422
423 while (!gshutdown)
424 sleep(1);
425
426shutdown:
427 std::cout << "Shutting down transceiver..." << std::endl;
428
429 delete trx;
430 delete radio;
431 delete usrp;
432
433 return 0;
434}