blob: 5f4e2867c98def385618cbbcbd02f0b8c86ae78d [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>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010043#include <osmocom/core/stats.h>
44#include <osmocom/vty/logging.h>
45#include <osmocom/vty/ports.h>
46#include <osmocom/vty/misc.h>
47#include <osmocom/vty/telnet_interface.h>
48#include <osmocom/ctrl/control_vty.h>
49#include <osmocom/ctrl/ports.h>
50#include <osmocom/ctrl/control_if.h>
51#include <osmocom/vty/stats.h>
Philipp Maier7e07cf22017-03-15 18:09:35 +010052#include "convolve.h"
53#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010054#include "trx_vty.h"
55#include "debug.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010056}
57
Thomas Tsou85b179d2013-11-15 21:14:33 -050058/* Samples-per-symbol for downlink path
59 * 4 - Uses precision modulator (more computation, less distortion)
60 * 1 - Uses minimized modulator (less computation, more distortion)
61 *
62 * Other values are invalid. Receive path (uplink) is always
Tom Tsou0fe41a52016-05-03 15:14:45 -070063 * downsampled to 1 sps. Default to 4 sps for all cases.
Thomas Tsou85b179d2013-11-15 21:14:33 -050064 */
Tom Tsou5cd70dc2016-03-06 01:28:40 -080065#define DEFAULT_TX_SPS 4
Thomas Tsou85b179d2013-11-15 21:14:33 -050066
Tom Tsou5cd70dc2016-03-06 01:28:40 -080067/*
68 * Samples-per-symbol for uplink (receiver) path
69 * Do not modify this value. EDGE configures 4 sps automatically on
70 * B200/B210 devices only. Use of 4 sps on the receive path for other
71 * configurations is not supported.
72 */
73#define DEFAULT_RX_SPS 1
74
Tom Tsoude116e92017-03-30 17:00:42 -070075/* Default configuration parameters */
Thomas Tsou85b179d2013-11-15 21:14:33 -050076#define DEFAULT_TRX_PORT 5700
77#define DEFAULT_TRX_IP "127.0.0.1"
Thomas Tsou85b179d2013-11-15 21:14:33 -050078#define DEFAULT_CHANS 1
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010079#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050080
81struct trx_config {
Pau Espin Pedrol8c800952017-08-16 16:53:23 +020082 std::string local_addr;
83 std::string remote_addr;
Thomas Tsou85b179d2013-11-15 21:14:33 -050084 std::string dev_args;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010085 char* config_file;
Thomas Tsou85b179d2013-11-15 21:14:33 -050086 unsigned port;
Tom Tsou5cd70dc2016-03-06 01:28:40 -080087 unsigned tx_sps;
88 unsigned rx_sps;
Thomas Tsou85b179d2013-11-15 21:14:33 -050089 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070090 unsigned rtsc;
Alexander Chemeris37c52c72016-03-25 18:28:34 +030091 unsigned rach_delay;
Thomas Tsou85b179d2013-11-15 21:14:33 -050092 bool extref;
Tom Tsou2f3e60b2016-07-17 19:29:08 -070093 bool gpsref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040094 Transceiver::FillerType filler;
Tom Tsou76764272016-06-24 14:25:39 -070095 bool mcbts;
Thomas Tsou8e17df72014-03-06 14:16:11 -050096 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040097 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040098 bool swap_channels;
Tom Tsoub0aefcb2016-03-06 03:44:34 -080099 bool edge;
Harald Welte81486e02017-06-29 15:35:22 +0200100 int sched_rr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100101 std::vector<std::string> rx_paths;
102 std::vector<std::string> tx_paths;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500103};
104
Thomas Tsou85b179d2013-11-15 21:14:33 -0500105volatile bool gshutdown = false;
106
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100107static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100108static struct trx_ctx *g_trx_ctx;
109static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100110
Thomas Tsou85b179d2013-11-15 21:14:33 -0500111/* Setup configuration values
112 * Don't query the existence of the Log.Level because it's a
113 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500114 * table will crash or will have already crashed. Everything else we
115 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500116 * are empty.
117 */
118bool trx_setup_config(struct trx_config *config)
119{
Tom Tsou76764272016-06-24 14:25:39 -0700120 std::string refstr, fillstr, divstr, mcstr, edgestr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100121 std::vector<std::string>::const_iterator si;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500122
Tom Tsou60317342017-03-30 19:36:41 -0700123 if (config->mcbts && config->chans > 5) {
Tom Tsou76764272016-06-24 14:25:39 -0700124 std::cout << "Unsupported number of channels" << std::endl;
125 return false;
126 }
127
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800128 edgestr = config->edge ? "Enabled" : "Disabled";
Tom Tsou76764272016-06-24 14:25:39 -0700129 mcstr = config->mcbts ? "Enabled" : "Disabled";
130
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700131 if (config->extref)
132 refstr = "External";
133 else if (config->gpsref)
134 refstr = "GPS";
135 else
136 refstr = "Internal";
137
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400138 switch (config->filler) {
139 case Transceiver::FILLER_DUMMY:
140 fillstr = "Dummy bursts";
141 break;
142 case Transceiver::FILLER_ZERO:
143 fillstr = "Disabled";
144 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800145 case Transceiver::FILLER_NORM_RAND:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400146 fillstr = "Normal busrts with random payload";
147 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800148 case Transceiver::FILLER_EDGE_RAND:
149 fillstr = "EDGE busrts with random payload";
150 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300151 case Transceiver::FILLER_ACCESS_RAND:
152 fillstr = "Access busrts with random payload";
153 break;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400154 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500155
156 std::ostringstream ost("");
157 ost << "Config Settings" << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500158 ost << " Device args............. " << config->dev_args << std::endl;
159 ost << " TRX Base Port........... " << config->port << std::endl;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200160 ost << " TRX Address............. " << config->local_addr << std::endl;
161 ost << " GSM Core Address........." << config->remote_addr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500162 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800163 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Alexander Chemeris1ab5e7f2016-04-20 08:44:55 +0300164 ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800165 ost << " EDGE support............ " << edgestr << std::endl;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700166 ost << " Reference............... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500167 ost << " C0 Filler Table......... " << fillstr << std::endl;
Tom Tsou76764272016-06-24 14:25:39 -0700168 ost << " Multi-Carrier........... " << mcstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500169 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400170 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400171 ost << " Swap channels........... " << config->swap_channels << std::endl;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100172 ost << " Tx Antennas.............";
173 for (si = config->tx_paths.begin(); si != config->tx_paths.end(); ++si)
174 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
175 ost << std::endl;
176 ost << " Rx Antennas.............";
177 for (si = config->rx_paths.begin(); si != config->rx_paths.end(); ++si)
178 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
179 ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500180
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100181 std::cout << ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500182 return true;
183}
184
185/* Create radio interface
186 * The interface consists of sample rate changes, frequency shifts,
187 * channel multiplexing, and other conversions. The transceiver core
188 * accepts input vectors sampled at multiples of the GSM symbol rate.
189 * The radio interface connects the main transceiver with the device
190 * object, which may be operating some other rate.
191 */
192RadioInterface *makeRadioInterface(struct trx_config *config,
193 RadioDevice *usrp, int type)
194{
195 RadioInterface *radio = NULL;
196
197 switch (type) {
198 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800199 radio = new RadioInterface(usrp, config->tx_sps,
200 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500201 break;
202 case RadioDevice::RESAMP_64M:
203 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800204 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700205 config->rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500206 break;
Tom Tsou76764272016-06-24 14:25:39 -0700207 case RadioDevice::MULTI_ARFCN:
208 radio = new RadioInterfaceMulti(usrp, config->tx_sps,
209 config->rx_sps, config->chans);
210 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500211 default:
212 LOG(ALERT) << "Unsupported radio interface configuration";
213 return NULL;
214 }
215
216 if (!radio->init(type)) {
217 LOG(ALERT) << "Failed to initialize radio interface";
218 return NULL;
219 }
220
221 return radio;
222}
223
224/* Create transceiver core
225 * The multi-threaded modem core operates at multiples of the GSM rate of
226 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
227 * and decoding schemes. Also included are the socket interfaces for
228 * connecting to the upper layer stack.
229 */
230Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
231{
232 Transceiver *trx;
233 VectorFIFO *fifo;
234
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200235 trx = new Transceiver(config->port, config->local_addr.c_str(),
236 config->remote_addr.c_str(), config->tx_sps,
237 config->rx_sps, config->chans, GSM::Time(3,0),
238 radio, config->rssi_offset);
Tom Tsou64464e62016-07-01 03:46:46 -0700239 if (!trx->init(config->filler, config->rtsc,
240 config->rach_delay, config->edge)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500241 LOG(ALERT) << "Failed to initialize transceiver";
242 delete trx;
243 return NULL;
244 }
245
246 for (size_t i = 0; i < config->chans; i++) {
247 fifo = radio->receiveFIFO(i);
248 if (fifo && trx->receiveFIFO(fifo, i))
249 continue;
250
251 LOG(ALERT) << "Could not attach FIFO to channel " << i;
252 delete trx;
253 return NULL;
254 }
255
256 return trx;
257}
258
259static void sig_handler(int signo)
260{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100261 fprintf(stdout, "signal %d received\n", signo);
262 switch (signo) {
263 case SIGINT:
264 case SIGTERM:
265 fprintf(stdout, "shutting down\n");
266 gshutdown = true;
267 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100268 case SIGABRT:
269 case SIGUSR1:
270 talloc_report(tall_trx_ctx, stderr);
271 talloc_report_full(tall_trx_ctx, stderr);
272 break;
273 case SIGUSR2:
274 talloc_report_full(tall_trx_ctx, stderr);
275 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100276 default:
277 break;
278 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500279}
280
281static void setup_signal_handlers()
282{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100283 /* Handle keyboard interrupt SIGINT */
284 signal(SIGINT, &sig_handler);
285 signal(SIGTERM, &sig_handler);
286 signal(SIGABRT, &sig_handler);
287 signal(SIGUSR1, &sig_handler);
288 signal(SIGUSR2, &sig_handler);
289 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500290}
291
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100292
293static std::vector<std::string> comma_delimited_to_vector(char* opt) {
294 std::string str = std::string(opt);
295 std::vector<std::string> result;
296 std::stringstream ss(str);
297
298 while( ss.good() )
299 {
300 std::string substr;
301 getline(ss, substr, ',');
302 result.push_back(substr);
303 }
304 return result;
305}
306
Thomas Tsou85b179d2013-11-15 21:14:33 -0500307static void print_help()
308{
309 fprintf(stdout, "Options:\n"
310 " -h This text\n"
311 " -a UHD device args\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500312 " -i IP address of GSM core\n"
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200313 " -j IP address of osmo-trx\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500314 " -p Base port number\n"
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800315 " -e Enable EDGE receiver\n"
Tom Tsou76764272016-06-24 14:25:39 -0700316 " -m Enable multi-ARFCN transceiver (default=disabled)\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500317 " -x Enable external 10 MHz reference\n"
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700318 " -g Enable GPSDO reference\n"
Tom Tsou2e4ed102016-06-27 15:39:16 -0700319 " -s Tx samples-per-symbol (1 or 4)\n"
320 " -b Rx samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500321 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500322 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700323 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300324 " -r Random Normal Burst test mode with TSC\n"
325 " -A Random Access Burst test mode with delay\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400326 " -R RSSI to dBm offset in dB (default=0)\n"
Harald Welte81486e02017-06-29 15:35:22 +0200327 " -S Swap channels (UmTRX only)\n"
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100328 " -t SCHED_RR real-time priority (1..32)\n"
329 " -y comma-delimited list of Tx paths (num elements matches -c)\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100330 " -z comma-delimited list of Rx paths (num elements matches -c)\n"
331 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500332}
333
334static void handle_options(int argc, char **argv, struct trx_config *config)
335{
336 int option;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100337 bool tx_path_set = false, rx_path_set = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500338
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200339 config->local_addr = DEFAULT_TRX_IP;
340 config->remote_addr = DEFAULT_TRX_IP;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100341 config->config_file = (char *)DEFAULT_CONFIG_FILE;
Tom Tsoude116e92017-03-30 17:00:42 -0700342 config->port = DEFAULT_TRX_PORT;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800343 config->tx_sps = DEFAULT_TX_SPS;
344 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700345 config->chans = DEFAULT_CHANS;
346 config->rtsc = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300347 config->rach_delay = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500348 config->extref = false;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700349 config->gpsref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700350 config->filler = Transceiver::FILLER_ZERO;
Tom Tsou76764272016-06-24 14:25:39 -0700351 config->mcbts = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500352 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400353 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400354 config->swap_channels = false;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800355 config->edge = false;
Harald Welte81486e02017-06-29 15:35:22 +0200356 config->sched_rr = -1;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100357 config->tx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
358 config->rx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
Thomas Tsou85b179d2013-11-15 21:14:33 -0500359
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100360 while ((option = getopt(argc, argv, "ha:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500361 switch (option) {
362 case 'h':
363 print_help();
364 exit(0);
365 break;
366 case 'a':
367 config->dev_args = optarg;
368 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500369 case 'i':
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200370 config->remote_addr = optarg;
371 break;
372 case 'j':
373 config->local_addr = optarg;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500374 break;
375 case 'p':
376 config->port = atoi(optarg);
377 break;
378 case 'c':
379 config->chans = atoi(optarg);
380 break;
Tom Tsou76764272016-06-24 14:25:39 -0700381 case 'm':
382 config->mcbts = true;
383 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500384 case 'x':
385 config->extref = true;
386 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700387 case 'g':
388 config->gpsref = true;
389 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500390 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700391 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500392 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500393 case 'o':
394 config->offset = atof(optarg);
395 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500396 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800397 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700398 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700399 case 'b':
400 config->rx_sps = atoi(optarg);
401 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700402 case 'r':
403 config->rtsc = atoi(optarg);
Tom Tsouaf717b22016-03-06 22:19:15 -0800404 config->filler = Transceiver::FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500405 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300406 case 'A':
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300407 config->rach_delay = atoi(optarg);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300408 config->filler = Transceiver::FILLER_ACCESS_RAND;
409 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400410 case 'R':
411 config->rssi_offset = atof(optarg);
412 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400413 case 'S':
414 config->swap_channels = true;
415 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800416 case 'e':
417 config->edge = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800418 break;
Harald Welte81486e02017-06-29 15:35:22 +0200419 case 't':
420 config->sched_rr = atoi(optarg);
421 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100422 case 'y':
423 config->tx_paths = comma_delimited_to_vector(optarg);
424 tx_path_set = true;
425 break;
426 case 'z':
427 config->rx_paths = comma_delimited_to_vector(optarg);
428 rx_path_set = true;
429 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100430 case 'C':
431 config->config_file = optarg;
432 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500433 default:
434 print_help();
435 exit(0);
436 }
437 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700438
Tom Tsou76764272016-06-24 14:25:39 -0700439 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
440 if ((config->edge) || (config->mcbts)) {
Tom Tsou2e4ed102016-06-27 15:39:16 -0700441 config->tx_sps = 4;
442 config->rx_sps = 4;
443 }
444
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700445 if (config->gpsref && config->extref) {
446 printf("External and GPSDO references unavailable at the same time\n\n");
447 goto bad_config;
448 }
449
Tom Tsouaf717b22016-03-06 22:19:15 -0800450 if (config->edge && (config->filler == Transceiver::FILLER_NORM_RAND))
451 config->filler = Transceiver::FILLER_EDGE_RAND;
452
Tom Tsou76764272016-06-24 14:25:39 -0700453 if ((config->tx_sps != 1) && (config->tx_sps != 4) &&
454 (config->rx_sps != 1) && (config->rx_sps != 4)) {
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800455 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700456 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700457 }
458
459 if (config->rtsc > 7) {
460 printf("Invalid training sequence %i\n\n", config->rtsc);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700461 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700462 }
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300463
464 if (config->rach_delay > 68) {
465 printf("RACH delay is too big %i\n\n", config->rach_delay);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700466 goto bad_config;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300467 }
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700468
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100469 if (!tx_path_set) {
470 config->tx_paths = std::vector<std::string>(config->chans, "");
471 } else if (config->tx_paths.size() != config->chans) {
472 printf("Num of channels and num of Tx Antennas doesn't match\n\n");
473 goto bad_config;
474 }
475 if (!rx_path_set) {
476 config->rx_paths = std::vector<std::string>(config->chans, "");
477 } else if (config->rx_paths.size() != config->chans) {
478 printf("Num of channels and num of Rx Antennas doesn't match\n\n");
479 goto bad_config;
480 }
481
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700482 return;
483
484bad_config:
485 print_help();
486 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500487}
488
Harald Welte81486e02017-06-29 15:35:22 +0200489static int set_sched_rr(int prio)
490{
491 struct sched_param param;
492 int rc;
493 memset(&param, 0, sizeof(param));
494 param.sched_priority = prio;
495 printf("Setting SCHED_RR priority(%d)\n", param.sched_priority);
496 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
497 if (rc != 0) {
498 std::cerr << "Config: Setting SCHED_RR failed" << std::endl;
499 return -1;
500 }
501 return 0;
502}
503
Thomas Tsou85b179d2013-11-15 21:14:33 -0500504int main(int argc, char *argv[])
505{
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700506 int type, chans, ref;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500507 RadioDevice *usrp;
508 RadioInterface *radio = NULL;
509 Transceiver *trx = NULL;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700510 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500511 struct trx_config config;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100512 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500513
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100514 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
515 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100516 g_vty_info.tall_ctx = tall_trx_ctx;
517
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100518 setup_signal_handlers();
519
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100520 g_trx_ctx = talloc_zero(tall_trx_ctx, struct trx_ctx);
521
Philipp Maiere51a8f02017-03-16 12:09:34 +0100522#ifdef HAVE_SSE3
523 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300524#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100525 if (__builtin_cpu_supports("sse3"))
526 printf(" and supported by CPU\n");
527 else
528 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300529#else
530 printf(", but runtime SIMD detection disabled\n");
531#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100532#endif
533
534#ifdef HAVE_SSE4_1
535 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300536#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100537 if (__builtin_cpu_supports("sse4.1"))
538 printf(" and supported by CPU\n");
539 else
540 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300541#else
542 printf(", but runtime SIMD detection disabled\n");
543#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100544#endif
545
Philipp Maier7e07cf22017-03-15 18:09:35 +0100546 convolve_init();
547 convert_init();
548
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100549 osmo_init_logging(&log_info);
550 osmo_stats_init(tall_trx_ctx);
551 vty_init(&g_vty_info);
552 ctrl_vty_init(tall_trx_ctx);
553 trx_vty_init(g_trx_ctx);
554
555 logging_vty_add_cmds();
556 osmo_talloc_vty_add_cmds();
557 osmo_stats_vty_add_cmds();
558
Thomas Tsou85b179d2013-11-15 21:14:33 -0500559 handle_options(argc, argv, &config);
560
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100561 rate_ctr_init(tall_trx_ctx);
562
563 rc = vty_read_config_file(config.config_file, NULL);
564 if (rc < 0) {
565 fprintf(stderr, "Failed to open config file: '%s'\n", config.config_file);
566 exit(2);
567 }
568
569 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
570 if (rc < 0)
571 exit(1);
572
573 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
574 if (!g_ctrlh) {
575 fprintf(stderr, "Failed to create CTRL interface.\n");
576 exit(1);
577 }
578
Harald Welte81486e02017-06-29 15:35:22 +0200579 if (config.sched_rr != -1) {
580 if (set_sched_rr(config.sched_rr) < 0)
581 return EXIT_FAILURE;
582 }
583
Thomas Tsou85b179d2013-11-15 21:14:33 -0500584 /* Check database sanity */
585 if (!trx_setup_config(&config)) {
586 std::cerr << "Config: Database failure - exiting" << std::endl;
587 return EXIT_FAILURE;
588 }
589
Thomas Tsou85b179d2013-11-15 21:14:33 -0500590 srandom(time(NULL));
591
592 /* Create the low level device object */
Tom Tsou76764272016-06-24 14:25:39 -0700593 if (config.mcbts)
594 iface = RadioDevice::MULTI_ARFCN;
595
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700596 if (config.extref)
597 ref = RadioDevice::REF_EXTERNAL;
598 else if (config.gpsref)
599 ref = RadioDevice::REF_GPS;
600 else
601 ref = RadioDevice::REF_INTERNAL;
602
Tom Tsou05c6feb2016-06-22 16:09:44 -0700603 usrp = RadioDevice::make(config.tx_sps, config.rx_sps, iface,
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100604 config.chans, config.offset, config.tx_paths, config.rx_paths);
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700605 type = usrp->open(config.dev_args, ref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500606 if (type < 0) {
607 LOG(ALERT) << "Failed to create radio device" << std::endl;
608 goto shutdown;
609 }
610
611 /* Setup the appropriate device interface */
612 radio = makeRadioInterface(&config, usrp, type);
613 if (!radio)
614 goto shutdown;
615
616 /* Create the transceiver core */
617 trx = makeTransceiver(&config, radio);
618 if (!trx)
619 goto shutdown;
620
Thomas Tsou85b179d2013-11-15 21:14:33 -0500621 chans = trx->numChans();
622 std::cout << "-- Transceiver active with "
623 << chans << " channel(s)" << std::endl;
624
625 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100626 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500627
628shutdown:
629 std::cout << "Shutting down transceiver..." << std::endl;
630
631 delete trx;
632 delete radio;
633 delete usrp;
634
635 return 0;
636}