blob: 3135dc602e9883f987f07dbf212e2666b8d97783 [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
35#define CONFIGDB "/etc/OpenBTS/OpenBTS.db"
36
37/* Samples-per-symbol for downlink path
38 * 4 - Uses precision modulator (more computation, less distortion)
39 * 1 - Uses minimized modulator (less computation, more distortion)
40 *
41 * Other values are invalid. Receive path (uplink) is always
42 * downsampled to 1 sps. Default to 4 sps for all cases except for
43 * ARM and non-SIMD enabled architectures.
44 */
45#if defined(HAVE_NEON) || !defined(HAVE_SSE3)
46#define DEFAULT_SPS 1
47#else
48#define DEFAULT_SPS 4
49#endif
50
51/* Default configuration parameters
52 * Note that these values are only used if the particular key does not
53 * exist in the configuration database. IP port and address values will
54 * typically be overwritten by the OpenBTS.db values. Other values will
55 * not be in the database by default.
56 */
57#define DEFAULT_TRX_PORT 5700
58#define DEFAULT_TRX_IP "127.0.0.1"
59#define DEFAULT_EXTREF false
60#define DEFAULT_DIVERSITY false
61#define DEFAULT_CHANS 1
62
63struct trx_config {
64 std::string log_level;
65 std::string addr;
66 std::string dev_args;
67 unsigned port;
68 unsigned sps;
69 unsigned chans;
70 bool extref;
71 bool diversity;
72};
73
74ConfigurationTable gConfig(CONFIGDB);
75
76volatile bool gshutdown = false;
77
78/* Run sanity check on configuration table
79 * The global table constructor cannot provide notification in the
80 * event of failure. Make sure that we can open the database file,
81 * write to it, and that it contains the bare minimum required keys.
82 */
83bool testConfig(const char *filename)
84{
85 int rc, val = 9999;
86 sqlite3 *db;
87 std::string test = "asldfkjsaldkf";
88 const char *key = "Log.Level";
89
90 /* Try to open the database */
91 rc = sqlite3_open(filename, &db);
92 if (rc || !db) {
93 std::cerr << std::endl;
94 std::cerr << "Config: Database could not be opened - "
95 << "check that database exists with read access"
96 << std::endl;
97 return false;
98 } else {
99 sqlite3_close(db);
100 }
101
102 /* 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 - "
106 << "permission to write to database directory?"
107 << std::endl;
108 return false;
109 } else {
110 gConfig.remove(test);
111 }
112
113 /* Attempt to query */
114 try {
115 gConfig.getStr(key);
116 } catch (...) {
117 std::cerr << std::endl;
118 std::cerr << "Config: Failed query required key " << key
119 << std::endl;
120 return false;
121 }
122
123 return true;
124}
125
126/* Setup configuration values
127 * Don't query the existence of the Log.Level because it's a
128 * mandatory value. That is, if it doesn't exist, the configuration
129 * table will crash or will have already crashed. So we check for
130 * it in the initial database sanity check. Everything else we can
131 * survive without and use default values if the database entries
132 * are empty.
133 */
134bool trx_setup_config(struct trx_config *config)
135{
136 std::string refstr, divstr;
137
138 if (!testConfig(CONFIGDB))
139 return false;
140
141 if (config->log_level == "")
142 config->log_level = gConfig.getStr("Log.Level");
143
144 if (!config->port) {
145 if (gConfig.defines("TRX.Port"))
146 config->port = gConfig.getNum("TRX.Port");
147 else
148 config->port = DEFAULT_TRX_PORT;
149 }
150
151 if (config->addr == "") {
152 if (gConfig.defines("TRX.IP"))
153 config->addr = gConfig.getStr("TRX.IP");
154 else
155 config->addr = DEFAULT_TRX_IP;
156 }
157
158 if (!config->extref) {
159 if (gConfig.defines("TRX.Reference"))
160 config->extref = gConfig.getNum("TRX.Reference");
161 else
162 config->extref = DEFAULT_EXTREF;
163 }
164
165 if (!config->diversity) {
166 if (gConfig.defines("TRX.Diversity"))
167 config->diversity = gConfig.getNum("TRX.Diversity");
168 else
169 config->diversity = DEFAULT_DIVERSITY;
170 }
171
172 if (!config->sps)
173 config->sps = DEFAULT_SPS;
174
175 if (!config->chans)
176 config->chans = DEFAULT_CHANS;
177
178 /* Diversity only supported on 2 channels */
179 if (config->diversity)
180 config->chans = 2;
181
182 refstr = config->extref ? "Enabled" : "Disabled";
183 divstr = config->diversity ? "Enabled" : "Disabled";
184
185 std::ostringstream ost("");
186 ost << "Config Settings" << std::endl;
187 ost << " Log Level............... " << config->log_level << std::endl;
188 ost << " Device args............. " << config->dev_args << std::endl;
189 ost << " TRX Base Port........... " << config->port << std::endl;
190 ost << " TRX Address............. " << config->addr << std::endl;
191 ost << " Channels................ " << config->chans << std::endl;
192 ost << " Samples-per-Symbol...... " << config->sps << std::endl;
193 ost << " External Reference...... " << refstr << std::endl;
194 ost << " Diversity............... " << divstr << std::endl;
195 std::cout << ost << std::endl;
196
197 return true;
198}
199
200/* Create radio interface
201 * The interface consists of sample rate changes, frequency shifts,
202 * channel multiplexing, and other conversions. The transceiver core
203 * accepts input vectors sampled at multiples of the GSM symbol rate.
204 * The radio interface connects the main transceiver with the device
205 * object, which may be operating some other rate.
206 */
207RadioInterface *makeRadioInterface(struct trx_config *config,
208 RadioDevice *usrp, int type)
209{
210 RadioInterface *radio = NULL;
211
212 switch (type) {
213 case RadioDevice::NORMAL:
214 radio = new RadioInterface(usrp, config->sps, config->chans);
215 break;
216 case RadioDevice::RESAMP_64M:
217 case RadioDevice::RESAMP_100M:
218 radio = new RadioInterfaceResamp(usrp,
219 config->sps, config->chans);
220 break;
221 case RadioDevice::DIVERSITY:
222 radio = new RadioInterfaceDiversity(usrp,
223 config->sps, config->chans);
224 break;
225 default:
226 LOG(ALERT) << "Unsupported radio interface configuration";
227 return NULL;
228 }
229
230 if (!radio->init(type)) {
231 LOG(ALERT) << "Failed to initialize radio interface";
232 return NULL;
233 }
234
235 return radio;
236}
237
238/* Create transceiver core
239 * The multi-threaded modem core operates at multiples of the GSM rate of
240 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
241 * and decoding schemes. Also included are the socket interfaces for
242 * connecting to the upper layer stack.
243 */
244Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
245{
246 Transceiver *trx;
247 VectorFIFO *fifo;
248
249 trx = new Transceiver(config->port, config->addr.c_str(), config->sps,
250 config->chans, GSM::Time(3,0), radio);
251 if (!trx->init()) {
252 LOG(ALERT) << "Failed to initialize transceiver";
253 delete trx;
254 return NULL;
255 }
256
257 for (size_t i = 0; i < config->chans; i++) {
258 fifo = radio->receiveFIFO(i);
259 if (fifo && trx->receiveFIFO(fifo, i))
260 continue;
261
262 LOG(ALERT) << "Could not attach FIFO to channel " << i;
263 delete trx;
264 return NULL;
265 }
266
267 return trx;
268}
269
270static void sig_handler(int signo)
271{
272 fprintf(stdout, "Received shutdown signal");
273 gshutdown = true;
274}
275
276static void setup_signal_handlers()
277{
278 if (signal(SIGINT, sig_handler) == SIG_ERR) {
279 fprintf(stderr, "Failed to install SIGINT signal handler\n");
280 exit(EXIT_FAILURE);
281 }
282 if (signal(SIGTERM, sig_handler) == SIG_ERR) {
283 fprintf(stderr, "Couldn't install SIGTERM signal handler\n");
284 exit( EXIT_FAILURE);
285 }
286}
287
288static void print_help()
289{
290 fprintf(stdout, "Options:\n"
291 " -h This text\n"
292 " -a UHD device args\n"
293 " -l Logging level (%s)\n"
294 " -i IP address of GSM core\n"
295 " -p Base port number\n"
296 " -d Enable dual channel diversity receiver\n"
297 " -x Enable external 10 MHz reference\n"
298 " -s Samples-per-symbol (1 or 4)\n"
299 " -c Number of ARFCN channels (default=1)\n",
300 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
301}
302
303static void handle_options(int argc, char **argv, struct trx_config *config)
304{
305 int option;
306
307 config->port = 0;
308 config->sps = 0;
309 config->chans = 0;
310 config->extref = false;
311 config->diversity = false;
312
313 while ((option = getopt(argc, argv, "ha:l:i:p:c:dxs:")) != -1) {
314 switch (option) {
315 case 'h':
316 print_help();
317 exit(0);
318 break;
319 case 'a':
320 config->dev_args = optarg;
321 break;
322 case 'l':
323 config->log_level = optarg;
324 break;
325 case 'i':
326 config->addr = optarg;
327 break;
328 case 'p':
329 config->port = atoi(optarg);
330 break;
331 case 'c':
332 config->chans = atoi(optarg);
333 break;
334 case 'd':
335 config->diversity = true;
336 break;
337 case 'x':
338 config->extref = true;
339 break;
340 case 's':
341 config->sps = atoi(optarg);
342 if ((config->sps != 1) && (config->sps != 4)) {
343 printf("Unsupported samples-per-symbol\n\n");
344 print_help();
345 exit(0);
346 }
347 break;
348 default:
349 print_help();
350 exit(0);
351 }
352 }
353}
354
355int main(int argc, char *argv[])
356{
357 int type, chans;
358 RadioDevice *usrp;
359 RadioInterface *radio = NULL;
360 Transceiver *trx = NULL;
361 struct trx_config config;
362
363 handle_options(argc, argv, &config);
364
365 setup_signal_handlers();
366
367 /* Check database sanity */
368 if (!trx_setup_config(&config)) {
369 std::cerr << "Config: Database failure - exiting" << std::endl;
370 return EXIT_FAILURE;
371 }
372
373 gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7);
374
375 srandom(time(NULL));
376
377 /* Create the low level device object */
378 usrp = RadioDevice::make(config.sps, config.chans, config.diversity);
379 type = usrp->open(config.dev_args, config.extref);
380 if (type < 0) {
381 LOG(ALERT) << "Failed to create radio device" << std::endl;
382 goto shutdown;
383 }
384
385 /* Setup the appropriate device interface */
386 radio = makeRadioInterface(&config, usrp, type);
387 if (!radio)
388 goto shutdown;
389
390 /* Create the transceiver core */
391 trx = makeTransceiver(&config, radio);
392 if (!trx)
393 goto shutdown;
394
395 trx->start();
396
397 chans = trx->numChans();
398 std::cout << "-- Transceiver active with "
399 << chans << " channel(s)" << std::endl;
400
401 while (!gshutdown)
402 sleep(1);
403
404shutdown:
405 std::cout << "Shutting down transceiver..." << std::endl;
406
407 delete trx;
408 delete radio;
409 delete usrp;
410
411 return 0;
412}