blob: b8cf96861afe66a9015fb3b9d169541595d3fbb2 [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"
Oliver Smith8d9a05c2018-12-12 16:03:38 +010025#include "Utils.h"
Thomas Tsou85b179d2013-11-15 21:14:33 -050026
27#include <time.h>
28#include <signal.h>
29#include <stdlib.h>
30#include <unistd.h>
Oliver Smitha439fed2018-10-23 13:12:17 +020031#include <getopt.h>
Harald Welte81486e02017-06-29 15:35:22 +020032#include <sched.h>
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010033#include <vector>
34#include <string>
35#include <sstream>
36#include <iostream>
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010037#include <sys/signalfd.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050038
39#include <GSMCommon.h>
40#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050041
Philipp Maier7e07cf22017-03-15 18:09:35 +010042extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010043#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010044#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010045#include <osmocom/core/msgb.h>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010046#include <osmocom/core/stats.h>
47#include <osmocom/vty/logging.h>
48#include <osmocom/vty/ports.h>
49#include <osmocom/vty/misc.h>
50#include <osmocom/vty/telnet_interface.h>
51#include <osmocom/ctrl/control_vty.h>
52#include <osmocom/ctrl/ports.h>
53#include <osmocom/ctrl/control_if.h>
54#include <osmocom/vty/stats.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020055#include <osmocom/vty/command.h>
56
Philipp Maier7e07cf22017-03-15 18:09:35 +010057#include "convolve.h"
58#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010059#include "trx_vty.h"
60#include "debug.h"
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020061#include "osmo_signal.h"
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +020062#include "trx_rate_ctr.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010063}
64
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010065#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050066
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010067#define charp2str(a) ((a) ? std::string(a) : std::string(""))
68
69static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050070
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010071struct osmo_fd signal_ofd;
Thomas Tsou85b179d2013-11-15 21:14:33 -050072volatile bool gshutdown = false;
73
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010074static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010075static struct trx_ctx *g_trx_ctx;
76static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010077
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010078static RadioDevice *usrp;
79static RadioInterface *radio;
80static Transceiver *transceiver;
81
Thomas Tsou85b179d2013-11-15 21:14:33 -050082/* Create radio interface
83 * The interface consists of sample rate changes, frequency shifts,
84 * channel multiplexing, and other conversions. The transceiver core
85 * accepts input vectors sampled at multiples of the GSM symbol rate.
86 * The radio interface connects the main transceiver with the device
87 * object, which may be operating some other rate.
88 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010089RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050090 RadioDevice *usrp, int type)
91{
92 RadioInterface *radio = NULL;
93
94 switch (type) {
95 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010096 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
97 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -050098 break;
99 case RadioDevice::RESAMP_64M:
100 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100101 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
102 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500103 break;
Tom Tsou76764272016-06-24 14:25:39 -0700104 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100105 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
106 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700107 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500108 default:
109 LOG(ALERT) << "Unsupported radio interface configuration";
110 return NULL;
111 }
112
113 if (!radio->init(type)) {
114 LOG(ALERT) << "Failed to initialize radio interface";
115 return NULL;
116 }
117
118 return radio;
119}
120
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200121/* Callback function to be called every time we receive a signal from TRANSC */
122static int transc_sig_cb(unsigned int subsys, unsigned int signal,
123 void *handler_data, void *signal_data)
124{
125 switch (signal) {
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200126 case S_MAIN_STOP_REQUIRED:
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200127 gshutdown = true;
128 break;
129 default:
130 break;
131 }
132 return 0;
133}
134
Thomas Tsou85b179d2013-11-15 21:14:33 -0500135/* Create transceiver core
136 * The multi-threaded modem core operates at multiples of the GSM rate of
137 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
138 * and decoding schemes. Also included are the socket interfaces for
139 * connecting to the upper layer stack.
140 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100141int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500142{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500143 VectorFIFO *fifo;
144
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100145 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
146 trx->cfg.remote_addr, trx->cfg.tx_sps,
147 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
Eric Wildac0487e2019-06-17 13:02:44 +0200148 radio, trx->cfg.rssi_offset, trx->cfg.stack_size);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100149 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200150 trx->cfg.rach_delay, trx->cfg.egprs, trx->cfg.ext_rach)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500151 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100152 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500153 }
154
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100155 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500156 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100157 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500158 continue;
159
160 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100161 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500162 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100163 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500164}
165
166static void sig_handler(int signo)
167{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100168
169 if (gshutdown)
170 /* We are in the middle of shutdown process, avoid any kind of extra
171 action like printing */
172 return;
173
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100174 fprintf(stdout, "signal %d received\n", signo);
175 switch (signo) {
176 case SIGINT:
177 case SIGTERM:
178 fprintf(stdout, "shutting down\n");
179 gshutdown = true;
180 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100181 case SIGABRT:
182 case SIGUSR1:
183 talloc_report(tall_trx_ctx, stderr);
184 talloc_report_full(tall_trx_ctx, stderr);
185 break;
186 case SIGUSR2:
187 talloc_report_full(tall_trx_ctx, stderr);
188 break;
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100189 case SIGHUP:
190 log_targets_reopen();
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100191 default:
192 break;
193 }
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100194
195}
196
197static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
198{
199 struct signalfd_siginfo fdsi;
200 ssize_t s;
201
202 s = read(ofd->fd, &fdsi, sizeof(struct signalfd_siginfo));
203 if (s < 0) {
204 LOG(FATAL) << "Failed to read from signalfd ("<< ofd->fd << "): " << errno;
205 gshutdown = true;
206 return 0;
207 }
208 sig_handler(fdsi.ssi_signo);
209 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500210}
211
212static void setup_signal_handlers()
213{
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100214 sigset_t set;
215 int sfd;
216
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100217 signal(SIGABRT, &sig_handler);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100218 osmo_init_ignore_signals();
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100219
220 /* Other threads created by this thread (main) will inherit a copy of the
221 signal mask. */
222 sigemptyset(&set);
223 sigaddset(&set, SIGINT);
224 sigaddset(&set, SIGTERM);
225 sigaddset(&set, SIGUSR1);
226 sigaddset(&set, SIGUSR2);
227 sigaddset(&set, SIGHUP);
228 if (pthread_sigmask(SIG_BLOCK, &set, NULL)) {
229 fprintf(stderr, "pthread_sigmask() failed.\n");
230 exit(EXIT_FAILURE);
231 }
232
233 if ((sfd = signalfd(-1, &set, 0)) == -1) {
234 fprintf(stderr, "signalfd() failed (%d).\n", errno);
235 exit(EXIT_FAILURE);
236 }
237
Harald Welte71df4252019-04-16 17:17:40 +0200238 osmo_fd_setup(&signal_ofd, sfd, BSC_FD_READ, signalfd_callback, NULL, 0);
Pau Espin Pedrol5e6f3e02019-04-02 11:45:04 +0200239 if (osmo_fd_register(&signal_ofd) < 0) {
240 fprintf(stderr, "osmo_fd_register() failed.\n");
241 exit(EXIT_FAILURE);
242 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500243}
244
245static void print_help()
246{
247 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200248 " -h, --help This text\n"
249 " -C, --config Filename The config file to use\n"
250 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100251 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500252}
253
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100254static void print_deprecated(char opt)
255{
256 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
257 << " Please use VTY cfg option instead."
Ruben Undheim252564b2018-07-20 22:03:39 +0200258 << " All cmd line options are already being overridden by VTY options if set.";
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100259}
260
261static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500262{
263 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100264 unsigned int i;
265 std::vector<std::string> rx_paths, tx_paths;
266 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200267 static struct option long_options[] = {
268 {"help", 0, 0, 'h'},
269 {"config", 1, 0, 'C'},
270 {"version", 0, 0, 'V'},
271 {NULL, 0, 0, 0}
272 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500273
Oliver Smitha439fed2018-10-23 13:12:17 +0200274 while ((option = getopt_long(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:V", long_options,
275 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500276 switch (option) {
277 case 'h':
278 print_help();
279 exit(0);
280 break;
281 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100282 print_deprecated(option);
283 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500284 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100285 case 'l':
286 print_deprecated(option);
287 log_set_log_level(osmo_stderr_target, atoi(optarg));
288 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500289 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100290 print_deprecated(option);
291 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200292 break;
293 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100294 print_deprecated(option);
295 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500296 break;
297 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100298 print_deprecated(option);
299 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500300 break;
301 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100302 print_deprecated(option);
303 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500304 break;
Tom Tsou76764272016-06-24 14:25:39 -0700305 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100306 print_deprecated(option);
307 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700308 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500309 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100310 print_deprecated(option);
311 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500312 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700313 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100314 print_deprecated(option);
315 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700316 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500317 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100318 print_deprecated(option);
319 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500320 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500321 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100322 print_deprecated(option);
323 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500324 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500325 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100326 print_deprecated(option);
327 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700328 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700329 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100330 print_deprecated(option);
331 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700332 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700333 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100334 print_deprecated(option);
335 trx->cfg.rtsc_set = true;
336 trx->cfg.rtsc = atoi(optarg);
337 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
338 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500339 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300340 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100341 print_deprecated(option);
342 trx->cfg.rach_delay_set = true;
343 trx->cfg.rach_delay = atoi(optarg);
344 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300345 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400346 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100347 print_deprecated(option);
348 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400349 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400350 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100351 print_deprecated(option);
352 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400353 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800354 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100355 print_deprecated(option);
356 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800357 break;
Harald Welte81486e02017-06-29 15:35:22 +0200358 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100359 print_deprecated(option);
360 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200361 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100362 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100363 print_deprecated(option);
364 tx_paths = comma_delimited_to_vector(optarg);
365 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100366 break;
367 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100368 print_deprecated(option);
369 rx_paths = comma_delimited_to_vector(optarg);
370 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100371 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100372 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100373 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100374 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200375 case 'V':
376 print_version(1);
377 exit(0);
378 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500379 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100380 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500381 }
382 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700383
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100384 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700385
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100386 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
387 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700388 goto bad_config;
389 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100390 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
391 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
392 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700393 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700394 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100395 for (i = 0; i < trx->cfg.num_chans; i++) {
396 trx->cfg.chans[i].trx = trx;
397 trx->cfg.chans[i].idx = i;
398 if (tx_paths_set)
399 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
400 if (rx_paths_set)
401 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100402 }
403
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700404 return;
405
406bad_config:
407 print_help();
408 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500409}
410
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100411int trx_validate_config(struct trx_ctx *trx)
412{
Tom Tsoud2800452019-04-01 07:55:48 +0700413 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > TRX_MCHAN_MAX) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100414 LOG(ERROR) << "Unsupported number of channels";
415 return -1;
416 }
417
418 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
419 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200420 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100421 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
422 return -1;
423 }
424
425 return 0;
426}
427
428static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200429{
430 struct sched_param param;
431 int rc;
432 memset(&param, 0, sizeof(param));
433 param.sched_priority = prio;
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100434 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority;
Harald Welte81486e02017-06-29 15:35:22 +0200435 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
436 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100437 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200438 return -1;
439 }
440 return 0;
441}
442
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100443static void print_config(struct trx_ctx *trx)
444{
445 unsigned int i;
446 std::ostringstream ost("");
447
448 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100449 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100450 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
451 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
452 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200453 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100454 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
455 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
456 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
457 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200458 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100459 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
460 ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl;
461 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
462 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
463 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
464 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
465 ost << " Tx Antennas.............";
466 for (i = 0; i < trx->cfg.num_chans; i++) {
467 std::string p = charp2str(trx->cfg.chans[i].tx_path);
468 ost << " '" << ((p != "") ? p : "<default>") << "'";
469 }
470 ost << std::endl;
471 ost << " Rx Antennas.............";
472 for (i = 0; i < trx->cfg.num_chans; i++) {
473 std::string p = charp2str(trx->cfg.chans[i].rx_path);
474 ost << " '" << ((p != "") ? p : "<default>") << "'";
475 }
476 ost << std::endl;
477
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100478 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100479}
480
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100481static void trx_stop()
482{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100483 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100484
485 delete transceiver;
486 delete radio;
487 delete usrp;
488}
489
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100490static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500491{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100492 int type, chans;
493 unsigned int i;
494 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700495 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100496
497 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100498 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100499 iface = RadioDevice::MULTI_ARFCN;
500
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100501 /* Generate vector of rx/tx_path: */
502 for (i = 0; i < trx->cfg.num_chans; i++) {
503 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
504 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
505 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100506
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100507 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
508 trx->cfg.num_chans, trx->cfg.offset,
509 tx_paths, rx_paths);
510 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100511 if (type < 0) {
512 LOG(ALERT) << "Failed to create radio device" << std::endl;
513 goto shutdown;
514 }
515
516 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100517 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100518 if (!radio)
519 goto shutdown;
520
521 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100522 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100523 goto shutdown;
524
525 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100526 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100527 << chans << " channel(s)" << std::endl;
528
529 return 0;
530
531shutdown:
532 trx_stop();
533 return -1;
534}
535
536int main(int argc, char *argv[])
537{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100538 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500539
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100540 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
541 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100542 g_vty_info.tall_ctx = tall_trx_ctx;
543
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100544 setup_signal_handlers();
545
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100546 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100547
Philipp Maiere51a8f02017-03-16 12:09:34 +0100548#ifdef HAVE_SSE3
549 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300550#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100551 if (__builtin_cpu_supports("sse3"))
552 printf(" and supported by CPU\n");
553 else
554 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300555#else
556 printf(", but runtime SIMD detection disabled\n");
557#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100558#endif
559
560#ifdef HAVE_SSE4_1
561 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300562#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100563 if (__builtin_cpu_supports("sse4.1"))
564 printf(" and supported by CPU\n");
565 else
566 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300567#else
568 printf(", but runtime SIMD detection disabled\n");
569#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100570#endif
571
Philipp Maier7e07cf22017-03-15 18:09:35 +0100572 convolve_init();
573 convert_init();
574
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200575 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100576 osmo_stats_init(tall_trx_ctx);
577 vty_init(&g_vty_info);
578 ctrl_vty_init(tall_trx_ctx);
579 trx_vty_init(g_trx_ctx);
580
581 logging_vty_add_cmds();
582 osmo_talloc_vty_add_cmds();
583 osmo_stats_vty_add_cmds();
584
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100585 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500586
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100587 rate_ctr_init(tall_trx_ctx);
588
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100589 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100590 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100591 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100592 exit(2);
593 }
594
595 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
596 if (rc < 0)
597 exit(1);
598
599 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
600 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100601 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100602 exit(1);
603 }
604
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100605 /* Backward compatibility: Hack to have 1 channel allocated by default.
606 * Can be Dropped once we * get rid of "-c" cmdline param */
607 if (g_trx_ctx->cfg.num_chans == 0) {
608 g_trx_ctx->cfg.num_chans = 1;
609 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
610 g_trx_ctx->cfg.chans[0].idx = 0;
611 LOG(ERROR) << "No explicit channel config found. Make sure you" \
612 " configure channels in VTY config. Using 1 channel as default," \
613 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200614 }
615
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100616 print_config(g_trx_ctx);
617
618 if (trx_validate_config(g_trx_ctx) < 0) {
619 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500620 return EXIT_FAILURE;
621 }
622
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100623 if (g_trx_ctx->cfg.sched_rr) {
624 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
625 return EXIT_FAILURE;
626 }
627
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200628 osmo_signal_register_handler(SS_MAIN, transc_sig_cb, NULL);
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200629 trx_rate_ctr_init(tall_trx_ctx, g_trx_ctx);
630
Thomas Tsou85b179d2013-11-15 21:14:33 -0500631 srandom(time(NULL));
632
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100633 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100634 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500635
636 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100637 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500638
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100639 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500640
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100641 osmo_fd_unregister(&signal_ofd);
642 osmo_fd_close(&signal_ofd);
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200643 osmo_signal_unregister_handler(SS_MAIN, transc_sig_cb, NULL);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500644 return 0;
645}