blob: 069d195fd15042d94567f2f6f3a07ade65207c88 [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>
Harald Welte81486e02017-06-29 15:35:22 +020030#include <sched.h>
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010031#include <vector>
32#include <string>
33#include <sstream>
34#include <iostream>
Thomas Tsou85b179d2013-11-15 21:14:33 -050035
36#include <GSMCommon.h>
37#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050038
Philipp Maier7e07cf22017-03-15 18:09:35 +010039extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010040#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010041#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010042#include <osmocom/core/msgb.h>
Philipp Maier7e07cf22017-03-15 18:09:35 +010043#include "convolve.h"
44#include "convert.h"
45}
46
Thomas Tsou85b179d2013-11-15 21:14:33 -050047/* Samples-per-symbol for downlink path
48 * 4 - Uses precision modulator (more computation, less distortion)
49 * 1 - Uses minimized modulator (less computation, more distortion)
50 *
51 * Other values are invalid. Receive path (uplink) is always
Tom Tsou0fe41a52016-05-03 15:14:45 -070052 * downsampled to 1 sps. Default to 4 sps for all cases.
Thomas Tsou85b179d2013-11-15 21:14:33 -050053 */
Tom Tsou5cd70dc2016-03-06 01:28:40 -080054#define DEFAULT_TX_SPS 4
Thomas Tsou85b179d2013-11-15 21:14:33 -050055
Tom Tsou5cd70dc2016-03-06 01:28:40 -080056/*
57 * Samples-per-symbol for uplink (receiver) path
58 * Do not modify this value. EDGE configures 4 sps automatically on
59 * B200/B210 devices only. Use of 4 sps on the receive path for other
60 * configurations is not supported.
61 */
62#define DEFAULT_RX_SPS 1
63
Tom Tsoude116e92017-03-30 17:00:42 -070064/* Default configuration parameters */
Thomas Tsou85b179d2013-11-15 21:14:33 -050065#define DEFAULT_TRX_PORT 5700
66#define DEFAULT_TRX_IP "127.0.0.1"
Thomas Tsou85b179d2013-11-15 21:14:33 -050067#define DEFAULT_CHANS 1
68
69struct trx_config {
70 std::string log_level;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +020071 std::string local_addr;
72 std::string remote_addr;
Thomas Tsou85b179d2013-11-15 21:14:33 -050073 std::string dev_args;
74 unsigned port;
Tom Tsou5cd70dc2016-03-06 01:28:40 -080075 unsigned tx_sps;
76 unsigned rx_sps;
Thomas Tsou85b179d2013-11-15 21:14:33 -050077 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070078 unsigned rtsc;
Alexander Chemeris37c52c72016-03-25 18:28:34 +030079 unsigned rach_delay;
Thomas Tsou85b179d2013-11-15 21:14:33 -050080 bool extref;
Tom Tsou2f3e60b2016-07-17 19:29:08 -070081 bool gpsref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040082 Transceiver::FillerType filler;
Tom Tsou76764272016-06-24 14:25:39 -070083 bool mcbts;
Thomas Tsou8e17df72014-03-06 14:16:11 -050084 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040085 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040086 bool swap_channels;
Tom Tsoub0aefcb2016-03-06 03:44:34 -080087 bool edge;
Harald Welte81486e02017-06-29 15:35:22 +020088 int sched_rr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010089 std::vector<std::string> rx_paths;
90 std::vector<std::string> tx_paths;
Thomas Tsou85b179d2013-11-15 21:14:33 -050091};
92
Thomas Tsou85b179d2013-11-15 21:14:33 -050093volatile bool gshutdown = false;
94
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010095static void *tall_trx_ctx;
96
Thomas Tsou85b179d2013-11-15 21:14:33 -050097/* Setup configuration values
98 * Don't query the existence of the Log.Level because it's a
99 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500100 * table will crash or will have already crashed. Everything else we
101 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500102 * are empty.
103 */
104bool trx_setup_config(struct trx_config *config)
105{
Tom Tsou76764272016-06-24 14:25:39 -0700106 std::string refstr, fillstr, divstr, mcstr, edgestr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100107 std::vector<std::string>::const_iterator si;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500108
Tom Tsou60317342017-03-30 19:36:41 -0700109 if (config->mcbts && config->chans > 5) {
Tom Tsou76764272016-06-24 14:25:39 -0700110 std::cout << "Unsupported number of channels" << std::endl;
111 return false;
112 }
113
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800114 edgestr = config->edge ? "Enabled" : "Disabled";
Tom Tsou76764272016-06-24 14:25:39 -0700115 mcstr = config->mcbts ? "Enabled" : "Disabled";
116
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700117 if (config->extref)
118 refstr = "External";
119 else if (config->gpsref)
120 refstr = "GPS";
121 else
122 refstr = "Internal";
123
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400124 switch (config->filler) {
125 case Transceiver::FILLER_DUMMY:
126 fillstr = "Dummy bursts";
127 break;
128 case Transceiver::FILLER_ZERO:
129 fillstr = "Disabled";
130 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800131 case Transceiver::FILLER_NORM_RAND:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400132 fillstr = "Normal busrts with random payload";
133 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800134 case Transceiver::FILLER_EDGE_RAND:
135 fillstr = "EDGE busrts with random payload";
136 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300137 case Transceiver::FILLER_ACCESS_RAND:
138 fillstr = "Access busrts with random payload";
139 break;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400140 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500141
142 std::ostringstream ost("");
143 ost << "Config Settings" << std::endl;
144 ost << " Log Level............... " << config->log_level << std::endl;
145 ost << " Device args............. " << config->dev_args << std::endl;
146 ost << " TRX Base Port........... " << config->port << std::endl;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200147 ost << " TRX Address............. " << config->local_addr << std::endl;
148 ost << " GSM Core Address........." << config->remote_addr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500149 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800150 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Alexander Chemeris1ab5e7f2016-04-20 08:44:55 +0300151 ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800152 ost << " EDGE support............ " << edgestr << std::endl;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700153 ost << " Reference............... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500154 ost << " C0 Filler Table......... " << fillstr << std::endl;
Tom Tsou76764272016-06-24 14:25:39 -0700155 ost << " Multi-Carrier........... " << mcstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500156 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400157 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400158 ost << " Swap channels........... " << config->swap_channels << std::endl;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100159 ost << " Tx Antennas.............";
160 for (si = config->tx_paths.begin(); si != config->tx_paths.end(); ++si)
161 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
162 ost << std::endl;
163 ost << " Rx Antennas.............";
164 for (si = config->rx_paths.begin(); si != config->rx_paths.end(); ++si)
165 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
166 ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500167
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100168 std::cout << ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500169 return true;
170}
171
172/* Create radio interface
173 * The interface consists of sample rate changes, frequency shifts,
174 * channel multiplexing, and other conversions. The transceiver core
175 * accepts input vectors sampled at multiples of the GSM symbol rate.
176 * The radio interface connects the main transceiver with the device
177 * object, which may be operating some other rate.
178 */
179RadioInterface *makeRadioInterface(struct trx_config *config,
180 RadioDevice *usrp, int type)
181{
182 RadioInterface *radio = NULL;
183
184 switch (type) {
185 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800186 radio = new RadioInterface(usrp, config->tx_sps,
187 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500188 break;
189 case RadioDevice::RESAMP_64M:
190 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800191 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700192 config->rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500193 break;
Tom Tsou76764272016-06-24 14:25:39 -0700194 case RadioDevice::MULTI_ARFCN:
195 radio = new RadioInterfaceMulti(usrp, config->tx_sps,
196 config->rx_sps, config->chans);
197 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500198 default:
199 LOG(ALERT) << "Unsupported radio interface configuration";
200 return NULL;
201 }
202
203 if (!radio->init(type)) {
204 LOG(ALERT) << "Failed to initialize radio interface";
205 return NULL;
206 }
207
208 return radio;
209}
210
211/* Create transceiver core
212 * The multi-threaded modem core operates at multiples of the GSM rate of
213 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
214 * and decoding schemes. Also included are the socket interfaces for
215 * connecting to the upper layer stack.
216 */
217Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
218{
219 Transceiver *trx;
220 VectorFIFO *fifo;
221
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200222 trx = new Transceiver(config->port, config->local_addr.c_str(),
223 config->remote_addr.c_str(), config->tx_sps,
224 config->rx_sps, config->chans, GSM::Time(3,0),
225 radio, config->rssi_offset);
Tom Tsou64464e62016-07-01 03:46:46 -0700226 if (!trx->init(config->filler, config->rtsc,
227 config->rach_delay, config->edge)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500228 LOG(ALERT) << "Failed to initialize transceiver";
229 delete trx;
230 return NULL;
231 }
232
233 for (size_t i = 0; i < config->chans; i++) {
234 fifo = radio->receiveFIFO(i);
235 if (fifo && trx->receiveFIFO(fifo, i))
236 continue;
237
238 LOG(ALERT) << "Could not attach FIFO to channel " << i;
239 delete trx;
240 return NULL;
241 }
242
243 return trx;
244}
245
246static void sig_handler(int signo)
247{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100248 fprintf(stdout, "signal %d received\n", signo);
249 switch (signo) {
250 case SIGINT:
251 case SIGTERM:
252 fprintf(stdout, "shutting down\n");
253 gshutdown = true;
254 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100255 case SIGABRT:
256 case SIGUSR1:
257 talloc_report(tall_trx_ctx, stderr);
258 talloc_report_full(tall_trx_ctx, stderr);
259 break;
260 case SIGUSR2:
261 talloc_report_full(tall_trx_ctx, stderr);
262 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100263 default:
264 break;
265 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500266}
267
268static void setup_signal_handlers()
269{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100270 /* Handle keyboard interrupt SIGINT */
271 signal(SIGINT, &sig_handler);
272 signal(SIGTERM, &sig_handler);
273 signal(SIGABRT, &sig_handler);
274 signal(SIGUSR1, &sig_handler);
275 signal(SIGUSR2, &sig_handler);
276 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500277}
278
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100279
280static std::vector<std::string> comma_delimited_to_vector(char* opt) {
281 std::string str = std::string(opt);
282 std::vector<std::string> result;
283 std::stringstream ss(str);
284
285 while( ss.good() )
286 {
287 std::string substr;
288 getline(ss, substr, ',');
289 result.push_back(substr);
290 }
291 return result;
292}
293
Thomas Tsou85b179d2013-11-15 21:14:33 -0500294static void print_help()
295{
296 fprintf(stdout, "Options:\n"
297 " -h This text\n"
298 " -a UHD device args\n"
299 " -l Logging level (%s)\n"
300 " -i IP address of GSM core\n"
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200301 " -j IP address of osmo-trx\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500302 " -p Base port number\n"
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800303 " -e Enable EDGE receiver\n"
Tom Tsou76764272016-06-24 14:25:39 -0700304 " -m Enable multi-ARFCN transceiver (default=disabled)\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500305 " -x Enable external 10 MHz reference\n"
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700306 " -g Enable GPSDO reference\n"
Tom Tsou2e4ed102016-06-27 15:39:16 -0700307 " -s Tx samples-per-symbol (1 or 4)\n"
308 " -b Rx samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500309 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500310 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700311 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300312 " -r Random Normal Burst test mode with TSC\n"
313 " -A Random Access Burst test mode with delay\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400314 " -R RSSI to dBm offset in dB (default=0)\n"
Harald Welte81486e02017-06-29 15:35:22 +0200315 " -S Swap channels (UmTRX only)\n"
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100316 " -t SCHED_RR real-time priority (1..32)\n"
317 " -y comma-delimited list of Tx paths (num elements matches -c)\n"
318 " -z comma-delimited list of Rx paths (num elements matches -c)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500319 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
320}
321
322static void handle_options(int argc, char **argv, struct trx_config *config)
323{
324 int option;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100325 bool tx_path_set = false, rx_path_set = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500326
Tom Tsoude116e92017-03-30 17:00:42 -0700327 config->log_level = "NOTICE";
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200328 config->local_addr = DEFAULT_TRX_IP;
329 config->remote_addr = DEFAULT_TRX_IP;
Tom Tsoude116e92017-03-30 17:00:42 -0700330 config->port = DEFAULT_TRX_PORT;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800331 config->tx_sps = DEFAULT_TX_SPS;
332 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700333 config->chans = DEFAULT_CHANS;
334 config->rtsc = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300335 config->rach_delay = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500336 config->extref = false;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700337 config->gpsref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700338 config->filler = Transceiver::FILLER_ZERO;
Tom Tsou76764272016-06-24 14:25:39 -0700339 config->mcbts = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500340 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400341 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400342 config->swap_channels = false;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800343 config->edge = false;
Harald Welte81486e02017-06-29 15:35:22 +0200344 config->sched_rr = -1;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100345 config->tx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
346 config->rx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
Thomas Tsou85b179d2013-11-15 21:14:33 -0500347
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100348 while ((option = getopt(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500349 switch (option) {
350 case 'h':
351 print_help();
352 exit(0);
353 break;
354 case 'a':
355 config->dev_args = optarg;
356 break;
357 case 'l':
358 config->log_level = optarg;
359 break;
360 case 'i':
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200361 config->remote_addr = optarg;
362 break;
363 case 'j':
364 config->local_addr = optarg;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500365 break;
366 case 'p':
367 config->port = atoi(optarg);
368 break;
369 case 'c':
370 config->chans = atoi(optarg);
371 break;
Tom Tsou76764272016-06-24 14:25:39 -0700372 case 'm':
373 config->mcbts = true;
374 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500375 case 'x':
376 config->extref = true;
377 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700378 case 'g':
379 config->gpsref = true;
380 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500381 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700382 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500383 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500384 case 'o':
385 config->offset = atof(optarg);
386 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500387 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800388 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700389 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700390 case 'b':
391 config->rx_sps = atoi(optarg);
392 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700393 case 'r':
394 config->rtsc = atoi(optarg);
Tom Tsouaf717b22016-03-06 22:19:15 -0800395 config->filler = Transceiver::FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500396 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300397 case 'A':
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300398 config->rach_delay = atoi(optarg);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300399 config->filler = Transceiver::FILLER_ACCESS_RAND;
400 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400401 case 'R':
402 config->rssi_offset = atof(optarg);
403 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400404 case 'S':
405 config->swap_channels = true;
406 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800407 case 'e':
408 config->edge = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800409 break;
Harald Welte81486e02017-06-29 15:35:22 +0200410 case 't':
411 config->sched_rr = atoi(optarg);
412 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100413 case 'y':
414 config->tx_paths = comma_delimited_to_vector(optarg);
415 tx_path_set = true;
416 break;
417 case 'z':
418 config->rx_paths = comma_delimited_to_vector(optarg);
419 rx_path_set = true;
420 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500421 default:
422 print_help();
423 exit(0);
424 }
425 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700426
Tom Tsou76764272016-06-24 14:25:39 -0700427 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
428 if ((config->edge) || (config->mcbts)) {
Tom Tsou2e4ed102016-06-27 15:39:16 -0700429 config->tx_sps = 4;
430 config->rx_sps = 4;
431 }
432
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700433 if (config->gpsref && config->extref) {
434 printf("External and GPSDO references unavailable at the same time\n\n");
435 goto bad_config;
436 }
437
Tom Tsouaf717b22016-03-06 22:19:15 -0800438 if (config->edge && (config->filler == Transceiver::FILLER_NORM_RAND))
439 config->filler = Transceiver::FILLER_EDGE_RAND;
440
Tom Tsou76764272016-06-24 14:25:39 -0700441 if ((config->tx_sps != 1) && (config->tx_sps != 4) &&
442 (config->rx_sps != 1) && (config->rx_sps != 4)) {
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800443 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700444 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700445 }
446
447 if (config->rtsc > 7) {
448 printf("Invalid training sequence %i\n\n", config->rtsc);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700449 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700450 }
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300451
452 if (config->rach_delay > 68) {
453 printf("RACH delay is too big %i\n\n", config->rach_delay);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700454 goto bad_config;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300455 }
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700456
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100457 if (!tx_path_set) {
458 config->tx_paths = std::vector<std::string>(config->chans, "");
459 } else if (config->tx_paths.size() != config->chans) {
460 printf("Num of channels and num of Tx Antennas doesn't match\n\n");
461 goto bad_config;
462 }
463 if (!rx_path_set) {
464 config->rx_paths = std::vector<std::string>(config->chans, "");
465 } else if (config->rx_paths.size() != config->chans) {
466 printf("Num of channels and num of Rx Antennas doesn't match\n\n");
467 goto bad_config;
468 }
469
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700470 return;
471
472bad_config:
473 print_help();
474 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500475}
476
Harald Welte81486e02017-06-29 15:35:22 +0200477static int set_sched_rr(int prio)
478{
479 struct sched_param param;
480 int rc;
481 memset(&param, 0, sizeof(param));
482 param.sched_priority = prio;
483 printf("Setting SCHED_RR priority(%d)\n", param.sched_priority);
484 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
485 if (rc != 0) {
486 std::cerr << "Config: Setting SCHED_RR failed" << std::endl;
487 return -1;
488 }
489 return 0;
490}
491
Thomas Tsou85b179d2013-11-15 21:14:33 -0500492int main(int argc, char *argv[])
493{
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700494 int type, chans, ref;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500495 RadioDevice *usrp;
496 RadioInterface *radio = NULL;
497 Transceiver *trx = NULL;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700498 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500499 struct trx_config config;
500
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100501 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
502 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100503 setup_signal_handlers();
504
Philipp Maiere51a8f02017-03-16 12:09:34 +0100505#ifdef HAVE_SSE3
506 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300507#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100508 if (__builtin_cpu_supports("sse3"))
509 printf(" and supported by CPU\n");
510 else
511 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300512#else
513 printf(", but runtime SIMD detection disabled\n");
514#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100515#endif
516
517#ifdef HAVE_SSE4_1
518 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300519#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100520 if (__builtin_cpu_supports("sse4.1"))
521 printf(" and supported by CPU\n");
522 else
523 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300524#else
525 printf(", but runtime SIMD detection disabled\n");
526#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100527#endif
528
Philipp Maier7e07cf22017-03-15 18:09:35 +0100529 convolve_init();
530 convert_init();
531
Thomas Tsou85b179d2013-11-15 21:14:33 -0500532 handle_options(argc, argv, &config);
533
Harald Welte81486e02017-06-29 15:35:22 +0200534 if (config.sched_rr != -1) {
535 if (set_sched_rr(config.sched_rr) < 0)
536 return EXIT_FAILURE;
537 }
538
Thomas Tsou85b179d2013-11-15 21:14:33 -0500539 /* Check database sanity */
540 if (!trx_setup_config(&config)) {
541 std::cerr << "Config: Database failure - exiting" << std::endl;
542 return EXIT_FAILURE;
543 }
544
Pau Espin Pedrol11d50d92018-02-20 17:52:28 +0100545 gLogInit(config.log_level.c_str());
Thomas Tsou85b179d2013-11-15 21:14:33 -0500546
547 srandom(time(NULL));
548
549 /* Create the low level device object */
Tom Tsou76764272016-06-24 14:25:39 -0700550 if (config.mcbts)
551 iface = RadioDevice::MULTI_ARFCN;
552
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700553 if (config.extref)
554 ref = RadioDevice::REF_EXTERNAL;
555 else if (config.gpsref)
556 ref = RadioDevice::REF_GPS;
557 else
558 ref = RadioDevice::REF_INTERNAL;
559
Tom Tsou05c6feb2016-06-22 16:09:44 -0700560 usrp = RadioDevice::make(config.tx_sps, config.rx_sps, iface,
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100561 config.chans, config.offset, config.tx_paths, config.rx_paths);
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700562 type = usrp->open(config.dev_args, ref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500563 if (type < 0) {
564 LOG(ALERT) << "Failed to create radio device" << std::endl;
565 goto shutdown;
566 }
567
568 /* Setup the appropriate device interface */
569 radio = makeRadioInterface(&config, usrp, type);
570 if (!radio)
571 goto shutdown;
572
573 /* Create the transceiver core */
574 trx = makeTransceiver(&config, radio);
575 if (!trx)
576 goto shutdown;
577
Thomas Tsou85b179d2013-11-15 21:14:33 -0500578 chans = trx->numChans();
579 std::cout << "-- Transceiver active with "
580 << chans << " channel(s)" << std::endl;
581
582 while (!gshutdown)
583 sleep(1);
584
585shutdown:
586 std::cout << "Shutting down transceiver..." << std::endl;
587
588 delete trx;
589 delete radio;
590 delete usrp;
591
592 return 0;
593}