blob: 88b9de093b4030417a810fc3d8e070e44cd93765 [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"
Philipp Maier7e07cf22017-03-15 18:09:35 +010062}
63
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010064#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050065
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010066#define charp2str(a) ((a) ? std::string(a) : std::string(""))
67
68static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050069
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010070struct osmo_fd signal_ofd;
Thomas Tsou85b179d2013-11-15 21:14:33 -050071volatile bool gshutdown = false;
72
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010073static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010074static struct trx_ctx *g_trx_ctx;
75static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010076
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010077static RadioDevice *usrp;
78static RadioInterface *radio;
79static Transceiver *transceiver;
80
Thomas Tsou85b179d2013-11-15 21:14:33 -050081/* Create radio interface
82 * The interface consists of sample rate changes, frequency shifts,
83 * channel multiplexing, and other conversions. The transceiver core
84 * accepts input vectors sampled at multiples of the GSM symbol rate.
85 * The radio interface connects the main transceiver with the device
86 * object, which may be operating some other rate.
87 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010088RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050089 RadioDevice *usrp, int type)
90{
91 RadioInterface *radio = NULL;
92
93 switch (type) {
94 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010095 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
96 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -050097 break;
98 case RadioDevice::RESAMP_64M:
99 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100100 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
101 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500102 break;
Tom Tsou76764272016-06-24 14:25:39 -0700103 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100104 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
105 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700106 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500107 default:
108 LOG(ALERT) << "Unsupported radio interface configuration";
109 return NULL;
110 }
111
112 if (!radio->init(type)) {
113 LOG(ALERT) << "Failed to initialize radio interface";
114 return NULL;
115 }
116
117 return radio;
118}
119
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200120/* Callback function to be called every time we receive a signal from TRANSC */
121static int transc_sig_cb(unsigned int subsys, unsigned int signal,
122 void *handler_data, void *signal_data)
123{
124 switch (signal) {
125 case S_TRANSC_STOP_REQUIRED:
126 gshutdown = true;
127 break;
128 default:
129 break;
130 }
131 return 0;
132}
133
Thomas Tsou85b179d2013-11-15 21:14:33 -0500134/* Create transceiver core
135 * The multi-threaded modem core operates at multiples of the GSM rate of
136 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
137 * and decoding schemes. Also included are the socket interfaces for
138 * connecting to the upper layer stack.
139 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100140int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500141{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500142 VectorFIFO *fifo;
143
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100144 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
145 trx->cfg.remote_addr, trx->cfg.tx_sps,
146 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
147 radio, trx->cfg.rssi_offset);
148 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200149 trx->cfg.rach_delay, trx->cfg.egprs, trx->cfg.ext_rach)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500150 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100151 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500152 }
153
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200154 transceiver->setSignalHandler(transc_sig_cb);
155
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100156 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500157 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100158 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500159 continue;
160
161 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100162 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500163 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100164 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500165}
166
167static void sig_handler(int signo)
168{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100169
170 if (gshutdown)
171 /* We are in the middle of shutdown process, avoid any kind of extra
172 action like printing */
173 return;
174
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100175 fprintf(stdout, "signal %d received\n", signo);
176 switch (signo) {
177 case SIGINT:
178 case SIGTERM:
179 fprintf(stdout, "shutting down\n");
180 gshutdown = true;
181 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100182 case SIGABRT:
183 case SIGUSR1:
184 talloc_report(tall_trx_ctx, stderr);
185 talloc_report_full(tall_trx_ctx, stderr);
186 break;
187 case SIGUSR2:
188 talloc_report_full(tall_trx_ctx, stderr);
189 break;
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100190 case SIGHUP:
191 log_targets_reopen();
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100192 default:
193 break;
194 }
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100195
196}
197
198static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
199{
200 struct signalfd_siginfo fdsi;
201 ssize_t s;
202
203 s = read(ofd->fd, &fdsi, sizeof(struct signalfd_siginfo));
204 if (s < 0) {
205 LOG(FATAL) << "Failed to read from signalfd ("<< ofd->fd << "): " << errno;
206 gshutdown = true;
207 return 0;
208 }
209 sig_handler(fdsi.ssi_signo);
210 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500211}
212
213static void setup_signal_handlers()
214{
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100215 sigset_t set;
216 int sfd;
217
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100218 signal(SIGABRT, &sig_handler);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100219 osmo_init_ignore_signals();
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100220
221 /* Other threads created by this thread (main) will inherit a copy of the
222 signal mask. */
223 sigemptyset(&set);
224 sigaddset(&set, SIGINT);
225 sigaddset(&set, SIGTERM);
226 sigaddset(&set, SIGUSR1);
227 sigaddset(&set, SIGUSR2);
228 sigaddset(&set, SIGHUP);
229 if (pthread_sigmask(SIG_BLOCK, &set, NULL)) {
230 fprintf(stderr, "pthread_sigmask() failed.\n");
231 exit(EXIT_FAILURE);
232 }
233
234 if ((sfd = signalfd(-1, &set, 0)) == -1) {
235 fprintf(stderr, "signalfd() failed (%d).\n", errno);
236 exit(EXIT_FAILURE);
237 }
238
239 osmo_fd_setup(&signal_ofd, sfd, OSMO_FD_READ, signalfd_callback, NULL, 0);
240 osmo_fd_register(&signal_ofd);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500241}
242
243static void print_help()
244{
245 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200246 " -h, --help This text\n"
247 " -C, --config Filename The config file to use\n"
248 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100249 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500250}
251
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100252static void print_deprecated(char opt)
253{
254 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
255 << " Please use VTY cfg option instead."
256 << " All cmd line options are already being overriden by VTY options if set.";
257}
258
259static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500260{
261 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100262 unsigned int i;
263 std::vector<std::string> rx_paths, tx_paths;
264 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200265 static struct option long_options[] = {
266 {"help", 0, 0, 'h'},
267 {"config", 1, 0, 'C'},
268 {"version", 0, 0, 'V'},
269 {NULL, 0, 0, 0}
270 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500271
Oliver Smitha439fed2018-10-23 13:12:17 +0200272 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,
273 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500274 switch (option) {
275 case 'h':
276 print_help();
277 exit(0);
278 break;
279 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100280 print_deprecated(option);
281 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500282 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100283 case 'l':
284 print_deprecated(option);
285 log_set_log_level(osmo_stderr_target, atoi(optarg));
286 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500287 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100288 print_deprecated(option);
289 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200290 break;
291 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100292 print_deprecated(option);
293 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500294 break;
295 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100296 print_deprecated(option);
297 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500298 break;
299 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100300 print_deprecated(option);
301 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500302 break;
Tom Tsou76764272016-06-24 14:25:39 -0700303 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100304 print_deprecated(option);
305 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700306 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500307 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100308 print_deprecated(option);
309 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500310 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700311 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100312 print_deprecated(option);
313 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700314 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500315 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100316 print_deprecated(option);
317 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500318 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500319 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100320 print_deprecated(option);
321 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500322 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500323 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100324 print_deprecated(option);
325 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700326 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700327 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100328 print_deprecated(option);
329 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700330 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700331 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100332 print_deprecated(option);
333 trx->cfg.rtsc_set = true;
334 trx->cfg.rtsc = atoi(optarg);
335 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
336 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500337 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300338 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100339 print_deprecated(option);
340 trx->cfg.rach_delay_set = true;
341 trx->cfg.rach_delay = atoi(optarg);
342 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300343 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400344 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100345 print_deprecated(option);
346 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400347 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400348 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100349 print_deprecated(option);
350 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400351 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800352 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100353 print_deprecated(option);
354 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800355 break;
Harald Welte81486e02017-06-29 15:35:22 +0200356 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100357 print_deprecated(option);
358 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200359 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100360 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100361 print_deprecated(option);
362 tx_paths = comma_delimited_to_vector(optarg);
363 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100364 break;
365 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100366 print_deprecated(option);
367 rx_paths = comma_delimited_to_vector(optarg);
368 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100369 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100370 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100371 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100372 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200373 case 'V':
374 print_version(1);
375 exit(0);
376 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500377 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100378 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500379 }
380 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700381
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100382 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700383
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100384 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
385 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700386 goto bad_config;
387 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100388 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
389 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
390 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700391 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700392 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100393 for (i = 0; i < trx->cfg.num_chans; i++) {
394 trx->cfg.chans[i].trx = trx;
395 trx->cfg.chans[i].idx = i;
396 if (tx_paths_set)
397 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
398 if (rx_paths_set)
399 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100400 }
401
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700402 return;
403
404bad_config:
405 print_help();
406 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500407}
408
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100409int trx_validate_config(struct trx_ctx *trx)
410{
411 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > 5) {
412 LOG(ERROR) << "Unsupported number of channels";
413 return -1;
414 }
415
416 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
417 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200418 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100419 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
420 return -1;
421 }
422
423 return 0;
424}
425
426static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200427{
428 struct sched_param param;
429 int rc;
430 memset(&param, 0, sizeof(param));
431 param.sched_priority = prio;
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100432 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority;
Harald Welte81486e02017-06-29 15:35:22 +0200433 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
434 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100435 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200436 return -1;
437 }
438 return 0;
439}
440
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100441static void print_config(struct trx_ctx *trx)
442{
443 unsigned int i;
444 std::ostringstream ost("");
445
446 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100447 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100448 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
449 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
450 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200451 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100452 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
453 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
454 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
455 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200456 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100457 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
458 ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl;
459 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
460 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
461 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
462 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
463 ost << " Tx Antennas.............";
464 for (i = 0; i < trx->cfg.num_chans; i++) {
465 std::string p = charp2str(trx->cfg.chans[i].tx_path);
466 ost << " '" << ((p != "") ? p : "<default>") << "'";
467 }
468 ost << std::endl;
469 ost << " Rx Antennas.............";
470 for (i = 0; i < trx->cfg.num_chans; i++) {
471 std::string p = charp2str(trx->cfg.chans[i].rx_path);
472 ost << " '" << ((p != "") ? p : "<default>") << "'";
473 }
474 ost << std::endl;
475
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100476 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100477}
478
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100479static void trx_stop()
480{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100481 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100482
483 delete transceiver;
484 delete radio;
485 delete usrp;
486}
487
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100488static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500489{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100490 int type, chans;
491 unsigned int i;
492 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700493 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100494
495 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100496 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100497 iface = RadioDevice::MULTI_ARFCN;
498
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100499 /* Generate vector of rx/tx_path: */
500 for (i = 0; i < trx->cfg.num_chans; i++) {
501 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
502 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
503 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100504
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100505 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
506 trx->cfg.num_chans, trx->cfg.offset,
507 tx_paths, rx_paths);
508 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100509 if (type < 0) {
510 LOG(ALERT) << "Failed to create radio device" << std::endl;
511 goto shutdown;
512 }
513
514 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100515 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100516 if (!radio)
517 goto shutdown;
518
519 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100520 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100521 goto shutdown;
522
523 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100524 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100525 << chans << " channel(s)" << std::endl;
526
527 return 0;
528
529shutdown:
530 trx_stop();
531 return -1;
532}
533
534int main(int argc, char *argv[])
535{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100536 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500537
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100538 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
539 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100540 g_vty_info.tall_ctx = tall_trx_ctx;
541
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100542 setup_signal_handlers();
543
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100544 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100545
Philipp Maiere51a8f02017-03-16 12:09:34 +0100546#ifdef HAVE_SSE3
547 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300548#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100549 if (__builtin_cpu_supports("sse3"))
550 printf(" and supported by CPU\n");
551 else
552 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300553#else
554 printf(", but runtime SIMD detection disabled\n");
555#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100556#endif
557
558#ifdef HAVE_SSE4_1
559 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300560#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100561 if (__builtin_cpu_supports("sse4.1"))
562 printf(" and supported by CPU\n");
563 else
564 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300565#else
566 printf(", but runtime SIMD detection disabled\n");
567#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100568#endif
569
Philipp Maier7e07cf22017-03-15 18:09:35 +0100570 convolve_init();
571 convert_init();
572
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200573 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100574 osmo_stats_init(tall_trx_ctx);
575 vty_init(&g_vty_info);
576 ctrl_vty_init(tall_trx_ctx);
577 trx_vty_init(g_trx_ctx);
578
579 logging_vty_add_cmds();
580 osmo_talloc_vty_add_cmds();
581 osmo_stats_vty_add_cmds();
582
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100583 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500584
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100585 rate_ctr_init(tall_trx_ctx);
586
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100587 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100588 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100589 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100590 exit(2);
591 }
592
593 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
594 if (rc < 0)
595 exit(1);
596
597 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
598 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100599 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100600 exit(1);
601 }
602
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100603 /* Backward compatibility: Hack to have 1 channel allocated by default.
604 * Can be Dropped once we * get rid of "-c" cmdline param */
605 if (g_trx_ctx->cfg.num_chans == 0) {
606 g_trx_ctx->cfg.num_chans = 1;
607 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
608 g_trx_ctx->cfg.chans[0].idx = 0;
609 LOG(ERROR) << "No explicit channel config found. Make sure you" \
610 " configure channels in VTY config. Using 1 channel as default," \
611 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200612 }
613
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100614 print_config(g_trx_ctx);
615
616 if (trx_validate_config(g_trx_ctx) < 0) {
617 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500618 return EXIT_FAILURE;
619 }
620
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100621 if (g_trx_ctx->cfg.sched_rr) {
622 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
623 return EXIT_FAILURE;
624 }
625
Thomas Tsou85b179d2013-11-15 21:14:33 -0500626 srandom(time(NULL));
627
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100628 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100629 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500630
631 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100632 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500633
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100634 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500635
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100636 osmo_fd_unregister(&signal_ofd);
637 osmo_fd_close(&signal_ofd);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500638 return 0;
639}