blob: 0141810f0c37fdd5dd3afff0ff9914e484d1f342 [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>
Thomas Tsou85b179d2013-11-15 21:14:33 -050037
38#include <GSMCommon.h>
39#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050040
Philipp Maier7e07cf22017-03-15 18:09:35 +010041extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010042#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010043#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010044#include <osmocom/core/msgb.h>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010045#include <osmocom/core/stats.h>
46#include <osmocom/vty/logging.h>
47#include <osmocom/vty/ports.h>
48#include <osmocom/vty/misc.h>
49#include <osmocom/vty/telnet_interface.h>
50#include <osmocom/ctrl/control_vty.h>
51#include <osmocom/ctrl/ports.h>
52#include <osmocom/ctrl/control_if.h>
53#include <osmocom/vty/stats.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020054#include <osmocom/vty/command.h>
55
Philipp Maier7e07cf22017-03-15 18:09:35 +010056#include "convolve.h"
57#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010058#include "trx_vty.h"
59#include "debug.h"
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020060#include "osmo_signal.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010061}
62
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010063#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050064
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010065#define charp2str(a) ((a) ? std::string(a) : std::string(""))
66
67static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050068
Thomas Tsou85b179d2013-11-15 21:14:33 -050069volatile bool gshutdown = false;
70
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010071static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010072static struct trx_ctx *g_trx_ctx;
73static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010074
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010075static RadioDevice *usrp;
76static RadioInterface *radio;
77static Transceiver *transceiver;
78
Thomas Tsou85b179d2013-11-15 21:14:33 -050079/* Create radio interface
80 * The interface consists of sample rate changes, frequency shifts,
81 * channel multiplexing, and other conversions. The transceiver core
82 * accepts input vectors sampled at multiples of the GSM symbol rate.
83 * The radio interface connects the main transceiver with the device
84 * object, which may be operating some other rate.
85 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010086RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050087 RadioDevice *usrp, int type)
88{
89 RadioInterface *radio = NULL;
90
91 switch (type) {
92 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010093 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
94 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -050095 break;
96 case RadioDevice::RESAMP_64M:
97 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010098 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
99 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500100 break;
Tom Tsou76764272016-06-24 14:25:39 -0700101 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100102 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
103 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700104 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500105 default:
106 LOG(ALERT) << "Unsupported radio interface configuration";
107 return NULL;
108 }
109
110 if (!radio->init(type)) {
111 LOG(ALERT) << "Failed to initialize radio interface";
112 return NULL;
113 }
114
115 return radio;
116}
117
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200118/* Callback function to be called every time we receive a signal from TRANSC */
119static int transc_sig_cb(unsigned int subsys, unsigned int signal,
120 void *handler_data, void *signal_data)
121{
122 switch (signal) {
123 case S_TRANSC_STOP_REQUIRED:
124 gshutdown = true;
125 break;
126 default:
127 break;
128 }
129 return 0;
130}
131
Thomas Tsou85b179d2013-11-15 21:14:33 -0500132/* Create transceiver core
133 * The multi-threaded modem core operates at multiples of the GSM rate of
134 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
135 * and decoding schemes. Also included are the socket interfaces for
136 * connecting to the upper layer stack.
137 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100138int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500139{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500140 VectorFIFO *fifo;
141
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100142 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
143 trx->cfg.remote_addr, trx->cfg.tx_sps,
144 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
145 radio, trx->cfg.rssi_offset);
146 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200147 trx->cfg.rach_delay, trx->cfg.egprs, trx->cfg.ext_rach)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500148 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100149 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500150 }
151
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200152 transceiver->setSignalHandler(transc_sig_cb);
153
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100154 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500155 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100156 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500157 continue;
158
159 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100160 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500161 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100162 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500163}
164
165static void sig_handler(int signo)
166{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100167
168 if (gshutdown)
169 /* We are in the middle of shutdown process, avoid any kind of extra
170 action like printing */
171 return;
172
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100173 fprintf(stdout, "signal %d received\n", signo);
174 switch (signo) {
175 case SIGINT:
176 case SIGTERM:
177 fprintf(stdout, "shutting down\n");
178 gshutdown = true;
179 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100180 case SIGABRT:
181 case SIGUSR1:
182 talloc_report(tall_trx_ctx, stderr);
183 talloc_report_full(tall_trx_ctx, stderr);
184 break;
185 case SIGUSR2:
186 talloc_report_full(tall_trx_ctx, stderr);
187 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100188 default:
189 break;
190 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500191}
192
193static void setup_signal_handlers()
194{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100195 /* Handle keyboard interrupt SIGINT */
196 signal(SIGINT, &sig_handler);
197 signal(SIGTERM, &sig_handler);
198 signal(SIGABRT, &sig_handler);
199 signal(SIGUSR1, &sig_handler);
200 signal(SIGUSR2, &sig_handler);
201 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500202}
203
204static void print_help()
205{
206 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200207 " -h, --help This text\n"
208 " -C, --config Filename The config file to use\n"
209 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100210 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500211}
212
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100213static void print_deprecated(char opt)
214{
215 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
216 << " Please use VTY cfg option instead."
217 << " All cmd line options are already being overriden by VTY options if set.";
218}
219
220static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500221{
222 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100223 unsigned int i;
224 std::vector<std::string> rx_paths, tx_paths;
225 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200226 static struct option long_options[] = {
227 {"help", 0, 0, 'h'},
228 {"config", 1, 0, 'C'},
229 {"version", 0, 0, 'V'},
230 {NULL, 0, 0, 0}
231 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500232
Oliver Smitha439fed2018-10-23 13:12:17 +0200233 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,
234 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500235 switch (option) {
236 case 'h':
237 print_help();
238 exit(0);
239 break;
240 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100241 print_deprecated(option);
242 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500243 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100244 case 'l':
245 print_deprecated(option);
246 log_set_log_level(osmo_stderr_target, atoi(optarg));
247 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500248 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100249 print_deprecated(option);
250 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200251 break;
252 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100253 print_deprecated(option);
254 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500255 break;
256 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100257 print_deprecated(option);
258 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500259 break;
260 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100261 print_deprecated(option);
262 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500263 break;
Tom Tsou76764272016-06-24 14:25:39 -0700264 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100265 print_deprecated(option);
266 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700267 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500268 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100269 print_deprecated(option);
270 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500271 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700272 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100273 print_deprecated(option);
274 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700275 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500276 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100277 print_deprecated(option);
278 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500279 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500280 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100281 print_deprecated(option);
282 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500283 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500284 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100285 print_deprecated(option);
286 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700287 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700288 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100289 print_deprecated(option);
290 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700291 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700292 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100293 print_deprecated(option);
294 trx->cfg.rtsc_set = true;
295 trx->cfg.rtsc = atoi(optarg);
296 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
297 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500298 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300299 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100300 print_deprecated(option);
301 trx->cfg.rach_delay_set = true;
302 trx->cfg.rach_delay = atoi(optarg);
303 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300304 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400305 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100306 print_deprecated(option);
307 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400308 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400309 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100310 print_deprecated(option);
311 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400312 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800313 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100314 print_deprecated(option);
315 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800316 break;
Harald Welte81486e02017-06-29 15:35:22 +0200317 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100318 print_deprecated(option);
319 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200320 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100321 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100322 print_deprecated(option);
323 tx_paths = comma_delimited_to_vector(optarg);
324 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100325 break;
326 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100327 print_deprecated(option);
328 rx_paths = comma_delimited_to_vector(optarg);
329 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100330 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100331 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100332 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100333 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200334 case 'V':
335 print_version(1);
336 exit(0);
337 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500338 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100339 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500340 }
341 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700342
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100343 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700344
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100345 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
346 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700347 goto bad_config;
348 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100349 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
350 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
351 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700352 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700353 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100354 for (i = 0; i < trx->cfg.num_chans; i++) {
355 trx->cfg.chans[i].trx = trx;
356 trx->cfg.chans[i].idx = i;
357 if (tx_paths_set)
358 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
359 if (rx_paths_set)
360 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100361 }
362
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700363 return;
364
365bad_config:
366 print_help();
367 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500368}
369
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100370int trx_validate_config(struct trx_ctx *trx)
371{
372 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > 5) {
373 LOG(ERROR) << "Unsupported number of channels";
374 return -1;
375 }
376
377 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
378 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200379 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100380 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
381 return -1;
382 }
383
384 return 0;
385}
386
387static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200388{
389 struct sched_param param;
390 int rc;
391 memset(&param, 0, sizeof(param));
392 param.sched_priority = prio;
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100393 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority;
Harald Welte81486e02017-06-29 15:35:22 +0200394 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
395 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100396 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200397 return -1;
398 }
399 return 0;
400}
401
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100402static void print_config(struct trx_ctx *trx)
403{
404 unsigned int i;
405 std::ostringstream ost("");
406
407 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100408 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100409 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
410 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
411 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200412 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100413 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
414 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
415 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
416 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200417 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100418 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
419 ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl;
420 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
421 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
422 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
423 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
424 ost << " Tx Antennas.............";
425 for (i = 0; i < trx->cfg.num_chans; i++) {
426 std::string p = charp2str(trx->cfg.chans[i].tx_path);
427 ost << " '" << ((p != "") ? p : "<default>") << "'";
428 }
429 ost << std::endl;
430 ost << " Rx Antennas.............";
431 for (i = 0; i < trx->cfg.num_chans; i++) {
432 std::string p = charp2str(trx->cfg.chans[i].rx_path);
433 ost << " '" << ((p != "") ? p : "<default>") << "'";
434 }
435 ost << std::endl;
436
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100437 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100438}
439
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100440static void trx_stop()
441{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100442 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100443
444 delete transceiver;
445 delete radio;
446 delete usrp;
447}
448
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100449static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500450{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100451 int type, chans;
452 unsigned int i;
453 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700454 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100455
456 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100457 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100458 iface = RadioDevice::MULTI_ARFCN;
459
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100460 /* Generate vector of rx/tx_path: */
461 for (i = 0; i < trx->cfg.num_chans; i++) {
462 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
463 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
464 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100465
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100466 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
467 trx->cfg.num_chans, trx->cfg.offset,
468 tx_paths, rx_paths);
469 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100470 if (type < 0) {
471 LOG(ALERT) << "Failed to create radio device" << std::endl;
472 goto shutdown;
473 }
474
475 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100476 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100477 if (!radio)
478 goto shutdown;
479
480 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100481 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100482 goto shutdown;
483
484 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100485 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100486 << chans << " channel(s)" << std::endl;
487
488 return 0;
489
490shutdown:
491 trx_stop();
492 return -1;
493}
494
495int main(int argc, char *argv[])
496{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100497 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500498
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100499 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
500 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100501 g_vty_info.tall_ctx = tall_trx_ctx;
502
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100503 setup_signal_handlers();
504
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100505 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100506
Philipp Maiere51a8f02017-03-16 12:09:34 +0100507#ifdef HAVE_SSE3
508 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300509#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100510 if (__builtin_cpu_supports("sse3"))
511 printf(" and supported by CPU\n");
512 else
513 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300514#else
515 printf(", but runtime SIMD detection disabled\n");
516#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100517#endif
518
519#ifdef HAVE_SSE4_1
520 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300521#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100522 if (__builtin_cpu_supports("sse4.1"))
523 printf(" and supported by CPU\n");
524 else
525 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300526#else
527 printf(", but runtime SIMD detection disabled\n");
528#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100529#endif
530
Philipp Maier7e07cf22017-03-15 18:09:35 +0100531 convolve_init();
532 convert_init();
533
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200534 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100535 osmo_stats_init(tall_trx_ctx);
536 vty_init(&g_vty_info);
537 ctrl_vty_init(tall_trx_ctx);
538 trx_vty_init(g_trx_ctx);
539
540 logging_vty_add_cmds();
541 osmo_talloc_vty_add_cmds();
542 osmo_stats_vty_add_cmds();
543
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100544 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500545
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100546 rate_ctr_init(tall_trx_ctx);
547
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100548 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100549 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100550 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100551 exit(2);
552 }
553
554 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
555 if (rc < 0)
556 exit(1);
557
558 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
559 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100560 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100561 exit(1);
562 }
563
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100564 /* Backward compatibility: Hack to have 1 channel allocated by default.
565 * Can be Dropped once we * get rid of "-c" cmdline param */
566 if (g_trx_ctx->cfg.num_chans == 0) {
567 g_trx_ctx->cfg.num_chans = 1;
568 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
569 g_trx_ctx->cfg.chans[0].idx = 0;
570 LOG(ERROR) << "No explicit channel config found. Make sure you" \
571 " configure channels in VTY config. Using 1 channel as default," \
572 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200573 }
574
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100575 print_config(g_trx_ctx);
576
577 if (trx_validate_config(g_trx_ctx) < 0) {
578 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500579 return EXIT_FAILURE;
580 }
581
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100582 if (g_trx_ctx->cfg.sched_rr) {
583 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
584 return EXIT_FAILURE;
585 }
586
Thomas Tsou85b179d2013-11-15 21:14:33 -0500587 srandom(time(NULL));
588
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100589 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100590 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500591
592 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100593 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500594
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100595 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500596
597 return 0;
598}