blob: 8e2088d7ec91ebb2b91f274dd04c8e0215e9c285 [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 {
82 std::string log_level;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +020083 std::string local_addr;
84 std::string remote_addr;
Thomas Tsou85b179d2013-11-15 21:14:33 -050085 std::string dev_args;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010086 char* config_file;
Thomas Tsou85b179d2013-11-15 21:14:33 -050087 unsigned port;
Tom Tsou5cd70dc2016-03-06 01:28:40 -080088 unsigned tx_sps;
89 unsigned rx_sps;
Thomas Tsou85b179d2013-11-15 21:14:33 -050090 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070091 unsigned rtsc;
Alexander Chemeris37c52c72016-03-25 18:28:34 +030092 unsigned rach_delay;
Thomas Tsou85b179d2013-11-15 21:14:33 -050093 bool extref;
Tom Tsou2f3e60b2016-07-17 19:29:08 -070094 bool gpsref;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -040095 Transceiver::FillerType filler;
Tom Tsou76764272016-06-24 14:25:39 -070096 bool mcbts;
Thomas Tsou8e17df72014-03-06 14:16:11 -050097 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040098 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040099 bool swap_channels;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800100 bool edge;
Harald Welte81486e02017-06-29 15:35:22 +0200101 int sched_rr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100102 std::vector<std::string> rx_paths;
103 std::vector<std::string> tx_paths;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500104};
105
Thomas Tsou85b179d2013-11-15 21:14:33 -0500106volatile bool gshutdown = false;
107
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100108static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100109static struct trx_ctx *g_trx_ctx;
110static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100111
Thomas Tsou85b179d2013-11-15 21:14:33 -0500112/* Setup configuration values
113 * Don't query the existence of the Log.Level because it's a
114 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500115 * table will crash or will have already crashed. Everything else we
116 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500117 * are empty.
118 */
119bool trx_setup_config(struct trx_config *config)
120{
Tom Tsou76764272016-06-24 14:25:39 -0700121 std::string refstr, fillstr, divstr, mcstr, edgestr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100122 std::vector<std::string>::const_iterator si;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500123
Tom Tsou60317342017-03-30 19:36:41 -0700124 if (config->mcbts && config->chans > 5) {
Tom Tsou76764272016-06-24 14:25:39 -0700125 std::cout << "Unsupported number of channels" << std::endl;
126 return false;
127 }
128
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800129 edgestr = config->edge ? "Enabled" : "Disabled";
Tom Tsou76764272016-06-24 14:25:39 -0700130 mcstr = config->mcbts ? "Enabled" : "Disabled";
131
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700132 if (config->extref)
133 refstr = "External";
134 else if (config->gpsref)
135 refstr = "GPS";
136 else
137 refstr = "Internal";
138
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400139 switch (config->filler) {
140 case Transceiver::FILLER_DUMMY:
141 fillstr = "Dummy bursts";
142 break;
143 case Transceiver::FILLER_ZERO:
144 fillstr = "Disabled";
145 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800146 case Transceiver::FILLER_NORM_RAND:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400147 fillstr = "Normal busrts with random payload";
148 break;
Tom Tsouaf717b22016-03-06 22:19:15 -0800149 case Transceiver::FILLER_EDGE_RAND:
150 fillstr = "EDGE busrts with random payload";
151 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300152 case Transceiver::FILLER_ACCESS_RAND:
153 fillstr = "Access busrts with random payload";
154 break;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400155 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500156
157 std::ostringstream ost("");
158 ost << "Config Settings" << std::endl;
159 ost << " Log Level............... " << config->log_level << std::endl;
160 ost << " Device args............. " << config->dev_args << std::endl;
161 ost << " TRX Base Port........... " << config->port << std::endl;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200162 ost << " TRX Address............. " << config->local_addr << std::endl;
163 ost << " GSM Core Address........." << config->remote_addr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500164 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800165 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Alexander Chemeris1ab5e7f2016-04-20 08:44:55 +0300166 ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800167 ost << " EDGE support............ " << edgestr << std::endl;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700168 ost << " Reference............... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500169 ost << " C0 Filler Table......... " << fillstr << std::endl;
Tom Tsou76764272016-06-24 14:25:39 -0700170 ost << " Multi-Carrier........... " << mcstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500171 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400172 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400173 ost << " Swap channels........... " << config->swap_channels << std::endl;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100174 ost << " Tx Antennas.............";
175 for (si = config->tx_paths.begin(); si != config->tx_paths.end(); ++si)
176 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
177 ost << std::endl;
178 ost << " Rx Antennas.............";
179 for (si = config->rx_paths.begin(); si != config->rx_paths.end(); ++si)
180 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
181 ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500182
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100183 std::cout << ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500184 return true;
185}
186
187/* Create radio interface
188 * The interface consists of sample rate changes, frequency shifts,
189 * channel multiplexing, and other conversions. The transceiver core
190 * accepts input vectors sampled at multiples of the GSM symbol rate.
191 * The radio interface connects the main transceiver with the device
192 * object, which may be operating some other rate.
193 */
194RadioInterface *makeRadioInterface(struct trx_config *config,
195 RadioDevice *usrp, int type)
196{
197 RadioInterface *radio = NULL;
198
199 switch (type) {
200 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800201 radio = new RadioInterface(usrp, config->tx_sps,
202 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500203 break;
204 case RadioDevice::RESAMP_64M:
205 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800206 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700207 config->rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500208 break;
Tom Tsou76764272016-06-24 14:25:39 -0700209 case RadioDevice::MULTI_ARFCN:
210 radio = new RadioInterfaceMulti(usrp, config->tx_sps,
211 config->rx_sps, config->chans);
212 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500213 default:
214 LOG(ALERT) << "Unsupported radio interface configuration";
215 return NULL;
216 }
217
218 if (!radio->init(type)) {
219 LOG(ALERT) << "Failed to initialize radio interface";
220 return NULL;
221 }
222
223 return radio;
224}
225
226/* Create transceiver core
227 * The multi-threaded modem core operates at multiples of the GSM rate of
228 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
229 * and decoding schemes. Also included are the socket interfaces for
230 * connecting to the upper layer stack.
231 */
232Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio)
233{
234 Transceiver *trx;
235 VectorFIFO *fifo;
236
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200237 trx = new Transceiver(config->port, config->local_addr.c_str(),
238 config->remote_addr.c_str(), config->tx_sps,
239 config->rx_sps, config->chans, GSM::Time(3,0),
240 radio, config->rssi_offset);
Tom Tsou64464e62016-07-01 03:46:46 -0700241 if (!trx->init(config->filler, config->rtsc,
242 config->rach_delay, config->edge)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500243 LOG(ALERT) << "Failed to initialize transceiver";
244 delete trx;
245 return NULL;
246 }
247
248 for (size_t i = 0; i < config->chans; i++) {
249 fifo = radio->receiveFIFO(i);
250 if (fifo && trx->receiveFIFO(fifo, i))
251 continue;
252
253 LOG(ALERT) << "Could not attach FIFO to channel " << i;
254 delete trx;
255 return NULL;
256 }
257
258 return trx;
259}
260
261static void sig_handler(int signo)
262{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100263 fprintf(stdout, "signal %d received\n", signo);
264 switch (signo) {
265 case SIGINT:
266 case SIGTERM:
267 fprintf(stdout, "shutting down\n");
268 gshutdown = true;
269 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100270 case SIGABRT:
271 case SIGUSR1:
272 talloc_report(tall_trx_ctx, stderr);
273 talloc_report_full(tall_trx_ctx, stderr);
274 break;
275 case SIGUSR2:
276 talloc_report_full(tall_trx_ctx, stderr);
277 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100278 default:
279 break;
280 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500281}
282
283static void setup_signal_handlers()
284{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100285 /* Handle keyboard interrupt SIGINT */
286 signal(SIGINT, &sig_handler);
287 signal(SIGTERM, &sig_handler);
288 signal(SIGABRT, &sig_handler);
289 signal(SIGUSR1, &sig_handler);
290 signal(SIGUSR2, &sig_handler);
291 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500292}
293
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100294
295static std::vector<std::string> comma_delimited_to_vector(char* opt) {
296 std::string str = std::string(opt);
297 std::vector<std::string> result;
298 std::stringstream ss(str);
299
300 while( ss.good() )
301 {
302 std::string substr;
303 getline(ss, substr, ',');
304 result.push_back(substr);
305 }
306 return result;
307}
308
Thomas Tsou85b179d2013-11-15 21:14:33 -0500309static void print_help()
310{
311 fprintf(stdout, "Options:\n"
312 " -h This text\n"
313 " -a UHD device args\n"
314 " -l Logging level (%s)\n"
315 " -i IP address of GSM core\n"
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200316 " -j IP address of osmo-trx\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500317 " -p Base port number\n"
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800318 " -e Enable EDGE receiver\n"
Tom Tsou76764272016-06-24 14:25:39 -0700319 " -m Enable multi-ARFCN transceiver (default=disabled)\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500320 " -x Enable external 10 MHz reference\n"
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700321 " -g Enable GPSDO reference\n"
Tom Tsou2e4ed102016-06-27 15:39:16 -0700322 " -s Tx samples-per-symbol (1 or 4)\n"
323 " -b Rx samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500324 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500325 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700326 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300327 " -r Random Normal Burst test mode with TSC\n"
328 " -A Random Access Burst test mode with delay\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400329 " -R RSSI to dBm offset in dB (default=0)\n"
Harald Welte81486e02017-06-29 15:35:22 +0200330 " -S Swap channels (UmTRX only)\n"
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100331 " -t SCHED_RR real-time priority (1..32)\n"
332 " -y comma-delimited list of Tx paths (num elements matches -c)\n"
333 " -z comma-delimited list of Rx paths (num elements matches -c)\n",
Thomas Tsou85b179d2013-11-15 21:14:33 -0500334 "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
335}
336
337static void handle_options(int argc, char **argv, struct trx_config *config)
338{
339 int option;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100340 bool tx_path_set = false, rx_path_set = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500341
Tom Tsoude116e92017-03-30 17:00:42 -0700342 config->log_level = "NOTICE";
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200343 config->local_addr = DEFAULT_TRX_IP;
344 config->remote_addr = DEFAULT_TRX_IP;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100345 config->config_file = (char *)DEFAULT_CONFIG_FILE;
Tom Tsoude116e92017-03-30 17:00:42 -0700346 config->port = DEFAULT_TRX_PORT;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800347 config->tx_sps = DEFAULT_TX_SPS;
348 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700349 config->chans = DEFAULT_CHANS;
350 config->rtsc = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300351 config->rach_delay = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500352 config->extref = false;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700353 config->gpsref = false;
Tom Tsou64ad7122015-05-19 18:26:31 -0700354 config->filler = Transceiver::FILLER_ZERO;
Tom Tsou76764272016-06-24 14:25:39 -0700355 config->mcbts = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500356 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400357 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400358 config->swap_channels = false;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800359 config->edge = false;
Harald Welte81486e02017-06-29 15:35:22 +0200360 config->sched_rr = -1;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100361 config->tx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
362 config->rx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
Thomas Tsou85b179d2013-11-15 21:14:33 -0500363
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100364 while ((option = getopt(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500365 switch (option) {
366 case 'h':
367 print_help();
368 exit(0);
369 break;
370 case 'a':
371 config->dev_args = optarg;
372 break;
373 case 'l':
374 config->log_level = optarg;
375 break;
376 case 'i':
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200377 config->remote_addr = optarg;
378 break;
379 case 'j':
380 config->local_addr = optarg;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500381 break;
382 case 'p':
383 config->port = atoi(optarg);
384 break;
385 case 'c':
386 config->chans = atoi(optarg);
387 break;
Tom Tsou76764272016-06-24 14:25:39 -0700388 case 'm':
389 config->mcbts = true;
390 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500391 case 'x':
392 config->extref = true;
393 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700394 case 'g':
395 config->gpsref = true;
396 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500397 case 'f':
Tom Tsou64ad7122015-05-19 18:26:31 -0700398 config->filler = Transceiver::FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500399 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500400 case 'o':
401 config->offset = atof(optarg);
402 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500403 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800404 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700405 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700406 case 'b':
407 config->rx_sps = atoi(optarg);
408 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700409 case 'r':
410 config->rtsc = atoi(optarg);
Tom Tsouaf717b22016-03-06 22:19:15 -0800411 config->filler = Transceiver::FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500412 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300413 case 'A':
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300414 config->rach_delay = atoi(optarg);
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300415 config->filler = Transceiver::FILLER_ACCESS_RAND;
416 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400417 case 'R':
418 config->rssi_offset = atof(optarg);
419 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400420 case 'S':
421 config->swap_channels = true;
422 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800423 case 'e':
424 config->edge = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800425 break;
Harald Welte81486e02017-06-29 15:35:22 +0200426 case 't':
427 config->sched_rr = atoi(optarg);
428 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100429 case 'y':
430 config->tx_paths = comma_delimited_to_vector(optarg);
431 tx_path_set = true;
432 break;
433 case 'z':
434 config->rx_paths = comma_delimited_to_vector(optarg);
435 rx_path_set = true;
436 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100437 case 'C':
438 config->config_file = optarg;
439 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500440 default:
441 print_help();
442 exit(0);
443 }
444 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700445
Tom Tsou76764272016-06-24 14:25:39 -0700446 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
447 if ((config->edge) || (config->mcbts)) {
Tom Tsou2e4ed102016-06-27 15:39:16 -0700448 config->tx_sps = 4;
449 config->rx_sps = 4;
450 }
451
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700452 if (config->gpsref && config->extref) {
453 printf("External and GPSDO references unavailable at the same time\n\n");
454 goto bad_config;
455 }
456
Tom Tsouaf717b22016-03-06 22:19:15 -0800457 if (config->edge && (config->filler == Transceiver::FILLER_NORM_RAND))
458 config->filler = Transceiver::FILLER_EDGE_RAND;
459
Tom Tsou76764272016-06-24 14:25:39 -0700460 if ((config->tx_sps != 1) && (config->tx_sps != 4) &&
461 (config->rx_sps != 1) && (config->rx_sps != 4)) {
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800462 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700463 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700464 }
465
466 if (config->rtsc > 7) {
467 printf("Invalid training sequence %i\n\n", config->rtsc);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700468 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700469 }
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300470
471 if (config->rach_delay > 68) {
472 printf("RACH delay is too big %i\n\n", config->rach_delay);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700473 goto bad_config;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300474 }
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700475
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100476 if (!tx_path_set) {
477 config->tx_paths = std::vector<std::string>(config->chans, "");
478 } else if (config->tx_paths.size() != config->chans) {
479 printf("Num of channels and num of Tx Antennas doesn't match\n\n");
480 goto bad_config;
481 }
482 if (!rx_path_set) {
483 config->rx_paths = std::vector<std::string>(config->chans, "");
484 } else if (config->rx_paths.size() != config->chans) {
485 printf("Num of channels and num of Rx Antennas doesn't match\n\n");
486 goto bad_config;
487 }
488
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700489 return;
490
491bad_config:
492 print_help();
493 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500494}
495
Harald Welte81486e02017-06-29 15:35:22 +0200496static int set_sched_rr(int prio)
497{
498 struct sched_param param;
499 int rc;
500 memset(&param, 0, sizeof(param));
501 param.sched_priority = prio;
502 printf("Setting SCHED_RR priority(%d)\n", param.sched_priority);
503 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
504 if (rc != 0) {
505 std::cerr << "Config: Setting SCHED_RR failed" << std::endl;
506 return -1;
507 }
508 return 0;
509}
510
Thomas Tsou85b179d2013-11-15 21:14:33 -0500511int main(int argc, char *argv[])
512{
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700513 int type, chans, ref;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500514 RadioDevice *usrp;
515 RadioInterface *radio = NULL;
516 Transceiver *trx = NULL;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700517 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500518 struct trx_config config;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100519 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500520
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100521 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
522 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100523 g_vty_info.tall_ctx = tall_trx_ctx;
524
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100525 setup_signal_handlers();
526
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100527 g_trx_ctx = talloc_zero(tall_trx_ctx, struct trx_ctx);
528
Philipp Maiere51a8f02017-03-16 12:09:34 +0100529#ifdef HAVE_SSE3
530 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300531#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100532 if (__builtin_cpu_supports("sse3"))
533 printf(" and supported by CPU\n");
534 else
535 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300536#else
537 printf(", but runtime SIMD detection disabled\n");
538#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100539#endif
540
541#ifdef HAVE_SSE4_1
542 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300543#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100544 if (__builtin_cpu_supports("sse4.1"))
545 printf(" and supported by CPU\n");
546 else
547 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300548#else
549 printf(", but runtime SIMD detection disabled\n");
550#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100551#endif
552
Philipp Maier7e07cf22017-03-15 18:09:35 +0100553 convolve_init();
554 convert_init();
555
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100556 osmo_init_logging(&log_info);
557 osmo_stats_init(tall_trx_ctx);
558 vty_init(&g_vty_info);
559 ctrl_vty_init(tall_trx_ctx);
560 trx_vty_init(g_trx_ctx);
561
562 logging_vty_add_cmds();
563 osmo_talloc_vty_add_cmds();
564 osmo_stats_vty_add_cmds();
565
Thomas Tsou85b179d2013-11-15 21:14:33 -0500566 handle_options(argc, argv, &config);
567
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100568 rate_ctr_init(tall_trx_ctx);
569
570 rc = vty_read_config_file(config.config_file, NULL);
571 if (rc < 0) {
572 fprintf(stderr, "Failed to open config file: '%s'\n", config.config_file);
573 exit(2);
574 }
575
576 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
577 if (rc < 0)
578 exit(1);
579
580 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
581 if (!g_ctrlh) {
582 fprintf(stderr, "Failed to create CTRL interface.\n");
583 exit(1);
584 }
585
Harald Welte81486e02017-06-29 15:35:22 +0200586 if (config.sched_rr != -1) {
587 if (set_sched_rr(config.sched_rr) < 0)
588 return EXIT_FAILURE;
589 }
590
Thomas Tsou85b179d2013-11-15 21:14:33 -0500591 /* Check database sanity */
592 if (!trx_setup_config(&config)) {
593 std::cerr << "Config: Database failure - exiting" << std::endl;
594 return EXIT_FAILURE;
595 }
596
Pau Espin Pedrol11d50d92018-02-20 17:52:28 +0100597 gLogInit(config.log_level.c_str());
Thomas Tsou85b179d2013-11-15 21:14:33 -0500598
599 srandom(time(NULL));
600
601 /* Create the low level device object */
Tom Tsou76764272016-06-24 14:25:39 -0700602 if (config.mcbts)
603 iface = RadioDevice::MULTI_ARFCN;
604
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700605 if (config.extref)
606 ref = RadioDevice::REF_EXTERNAL;
607 else if (config.gpsref)
608 ref = RadioDevice::REF_GPS;
609 else
610 ref = RadioDevice::REF_INTERNAL;
611
Tom Tsou05c6feb2016-06-22 16:09:44 -0700612 usrp = RadioDevice::make(config.tx_sps, config.rx_sps, iface,
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100613 config.chans, config.offset, config.tx_paths, config.rx_paths);
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700614 type = usrp->open(config.dev_args, ref, config.swap_channels);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500615 if (type < 0) {
616 LOG(ALERT) << "Failed to create radio device" << std::endl;
617 goto shutdown;
618 }
619
620 /* Setup the appropriate device interface */
621 radio = makeRadioInterface(&config, usrp, type);
622 if (!radio)
623 goto shutdown;
624
625 /* Create the transceiver core */
626 trx = makeTransceiver(&config, radio);
627 if (!trx)
628 goto shutdown;
629
Thomas Tsou85b179d2013-11-15 21:14:33 -0500630 chans = trx->numChans();
631 std::cout << "-- Transceiver active with "
632 << chans << " channel(s)" << std::endl;
633
634 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100635 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500636
637shutdown:
638 std::cout << "Shutting down transceiver..." << std::endl;
639
640 delete trx;
641 delete radio;
642 delete usrp;
643
644 return 0;
645}