blob: 2b69da404f140776ba34200092c74822654f0c2f [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>
Oliver Smitha439fed2018-10-23 13:12:17 +020030#include <getopt.h>
Harald Welte81486e02017-06-29 15:35:22 +020031#include <sched.h>
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010032#include <vector>
33#include <string>
34#include <sstream>
35#include <iostream>
Thomas Tsou85b179d2013-11-15 21:14:33 -050036
37#include <GSMCommon.h>
38#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050039
Philipp Maier7e07cf22017-03-15 18:09:35 +010040extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010041#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010042#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010043#include <osmocom/core/msgb.h>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010044#include <osmocom/core/stats.h>
45#include <osmocom/vty/logging.h>
46#include <osmocom/vty/ports.h>
47#include <osmocom/vty/misc.h>
48#include <osmocom/vty/telnet_interface.h>
49#include <osmocom/ctrl/control_vty.h>
50#include <osmocom/ctrl/ports.h>
51#include <osmocom/ctrl/control_if.h>
52#include <osmocom/vty/stats.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020053#include <osmocom/vty/command.h>
54
Philipp Maier7e07cf22017-03-15 18:09:35 +010055#include "convolve.h"
56#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010057#include "trx_vty.h"
58#include "debug.h"
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020059#include "osmo_signal.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010060}
61
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010062#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050063
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010064#define charp2str(a) ((a) ? std::string(a) : std::string(""))
65
66static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050067
Thomas Tsou85b179d2013-11-15 21:14:33 -050068volatile bool gshutdown = false;
69
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010070static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010071static struct trx_ctx *g_trx_ctx;
72static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010073
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010074static RadioDevice *usrp;
75static RadioInterface *radio;
76static Transceiver *transceiver;
77
Thomas Tsou85b179d2013-11-15 21:14:33 -050078/* Create radio interface
79 * The interface consists of sample rate changes, frequency shifts,
80 * channel multiplexing, and other conversions. The transceiver core
81 * accepts input vectors sampled at multiples of the GSM symbol rate.
82 * The radio interface connects the main transceiver with the device
83 * object, which may be operating some other rate.
84 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010085RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050086 RadioDevice *usrp, int type)
87{
88 RadioInterface *radio = NULL;
89
90 switch (type) {
91 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010092 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
93 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -050094 break;
95 case RadioDevice::RESAMP_64M:
96 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010097 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
98 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -050099 break;
Tom Tsou76764272016-06-24 14:25:39 -0700100 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100101 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
102 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700103 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500104 default:
105 LOG(ALERT) << "Unsupported radio interface configuration";
106 return NULL;
107 }
108
109 if (!radio->init(type)) {
110 LOG(ALERT) << "Failed to initialize radio interface";
111 return NULL;
112 }
113
114 return radio;
115}
116
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200117/* Callback function to be called every time we receive a signal from TRANSC */
118static int transc_sig_cb(unsigned int subsys, unsigned int signal,
119 void *handler_data, void *signal_data)
120{
121 switch (signal) {
122 case S_TRANSC_STOP_REQUIRED:
123 gshutdown = true;
124 break;
125 default:
126 break;
127 }
128 return 0;
129}
130
Thomas Tsou85b179d2013-11-15 21:14:33 -0500131/* Create transceiver core
132 * The multi-threaded modem core operates at multiples of the GSM rate of
133 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
134 * and decoding schemes. Also included are the socket interfaces for
135 * connecting to the upper layer stack.
136 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100137int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500138{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500139 VectorFIFO *fifo;
140
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100141 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
142 trx->cfg.remote_addr, trx->cfg.tx_sps,
143 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
144 radio, trx->cfg.rssi_offset);
145 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
146 trx->cfg.rach_delay, trx->cfg.egprs)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500147 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100148 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500149 }
150
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200151 transceiver->setSignalHandler(transc_sig_cb);
152
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100153 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500154 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100155 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500156 continue;
157
158 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100159 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500160 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100161 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500162}
163
164static void sig_handler(int signo)
165{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100166 fprintf(stdout, "signal %d received\n", signo);
167 switch (signo) {
168 case SIGINT:
169 case SIGTERM:
170 fprintf(stdout, "shutting down\n");
171 gshutdown = true;
172 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100173 case SIGABRT:
174 case SIGUSR1:
175 talloc_report(tall_trx_ctx, stderr);
176 talloc_report_full(tall_trx_ctx, stderr);
177 break;
178 case SIGUSR2:
179 talloc_report_full(tall_trx_ctx, stderr);
180 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100181 default:
182 break;
183 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500184}
185
186static void setup_signal_handlers()
187{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100188 /* Handle keyboard interrupt SIGINT */
189 signal(SIGINT, &sig_handler);
190 signal(SIGTERM, &sig_handler);
191 signal(SIGABRT, &sig_handler);
192 signal(SIGUSR1, &sig_handler);
193 signal(SIGUSR2, &sig_handler);
194 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500195}
196
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100197static std::vector<std::string> comma_delimited_to_vector(char* opt)
198{
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100199 std::string str = std::string(opt);
200 std::vector<std::string> result;
201 std::stringstream ss(str);
202
203 while( ss.good() )
204 {
205 std::string substr;
206 getline(ss, substr, ',');
207 result.push_back(substr);
208 }
209 return result;
210}
211
Thomas Tsou85b179d2013-11-15 21:14:33 -0500212static void print_help()
213{
214 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200215 " -h, --help This text\n"
216 " -C, --config Filename The config file to use\n"
217 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100218 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500219}
220
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100221static void print_deprecated(char opt)
222{
223 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
224 << " Please use VTY cfg option instead."
225 << " All cmd line options are already being overriden by VTY options if set.";
226}
227
228static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500229{
230 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100231 unsigned int i;
232 std::vector<std::string> rx_paths, tx_paths;
233 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200234 static struct option long_options[] = {
235 {"help", 0, 0, 'h'},
236 {"config", 1, 0, 'C'},
237 {"version", 0, 0, 'V'},
238 {NULL, 0, 0, 0}
239 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500240
Oliver Smitha439fed2018-10-23 13:12:17 +0200241 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,
242 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500243 switch (option) {
244 case 'h':
245 print_help();
246 exit(0);
247 break;
248 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100249 print_deprecated(option);
250 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500251 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100252 case 'l':
253 print_deprecated(option);
254 log_set_log_level(osmo_stderr_target, atoi(optarg));
255 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500256 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100257 print_deprecated(option);
258 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200259 break;
260 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100261 print_deprecated(option);
262 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500263 break;
264 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100265 print_deprecated(option);
266 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500267 break;
268 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100269 print_deprecated(option);
270 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500271 break;
Tom Tsou76764272016-06-24 14:25:39 -0700272 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100273 print_deprecated(option);
274 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700275 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500276 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100277 print_deprecated(option);
278 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500279 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700280 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100281 print_deprecated(option);
282 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700283 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500284 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100285 print_deprecated(option);
286 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500287 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500288 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100289 print_deprecated(option);
290 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500291 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500292 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100293 print_deprecated(option);
294 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700295 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700296 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100297 print_deprecated(option);
298 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700299 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700300 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100301 print_deprecated(option);
302 trx->cfg.rtsc_set = true;
303 trx->cfg.rtsc = atoi(optarg);
304 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
305 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500306 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300307 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100308 print_deprecated(option);
309 trx->cfg.rach_delay_set = true;
310 trx->cfg.rach_delay = atoi(optarg);
311 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300312 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400313 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100314 print_deprecated(option);
315 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400316 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400317 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100318 print_deprecated(option);
319 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400320 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800321 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100322 print_deprecated(option);
323 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800324 break;
Harald Welte81486e02017-06-29 15:35:22 +0200325 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100326 print_deprecated(option);
327 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200328 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100329 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100330 print_deprecated(option);
331 tx_paths = comma_delimited_to_vector(optarg);
332 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100333 break;
334 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100335 print_deprecated(option);
336 rx_paths = comma_delimited_to_vector(optarg);
337 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100338 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100339 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100340 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100341 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200342 case 'V':
343 print_version(1);
344 exit(0);
345 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500346 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100347 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500348 }
349 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700350
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100351 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700352
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100353 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
354 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700355 goto bad_config;
356 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100357 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
358 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
359 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700360 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700361 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100362 for (i = 0; i < trx->cfg.num_chans; i++) {
363 trx->cfg.chans[i].trx = trx;
364 trx->cfg.chans[i].idx = i;
365 if (tx_paths_set)
366 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
367 if (rx_paths_set)
368 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100369 }
370
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700371 return;
372
373bad_config:
374 print_help();
375 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500376}
377
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100378int trx_validate_config(struct trx_ctx *trx)
379{
380 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > 5) {
381 LOG(ERROR) << "Unsupported number of channels";
382 return -1;
383 }
384
385 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
386 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200387 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100388 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
389 return -1;
390 }
391
392 return 0;
393}
394
395static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200396{
397 struct sched_param param;
398 int rc;
399 memset(&param, 0, sizeof(param));
400 param.sched_priority = prio;
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100401 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority;
Harald Welte81486e02017-06-29 15:35:22 +0200402 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
403 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100404 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200405 return -1;
406 }
407 return 0;
408}
409
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100410static void print_config(struct trx_ctx *trx)
411{
412 unsigned int i;
413 std::ostringstream ost("");
414
415 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100416 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100417 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
418 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
419 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200420 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100421 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
422 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
423 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
424 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
425 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
426 ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl;
427 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
428 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
429 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
430 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
431 ost << " Tx Antennas.............";
432 for (i = 0; i < trx->cfg.num_chans; i++) {
433 std::string p = charp2str(trx->cfg.chans[i].tx_path);
434 ost << " '" << ((p != "") ? p : "<default>") << "'";
435 }
436 ost << std::endl;
437 ost << " Rx Antennas.............";
438 for (i = 0; i < trx->cfg.num_chans; i++) {
439 std::string p = charp2str(trx->cfg.chans[i].rx_path);
440 ost << " '" << ((p != "") ? p : "<default>") << "'";
441 }
442 ost << std::endl;
443
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100444 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100445}
446
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100447static void trx_stop()
448{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100449 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100450
451 delete transceiver;
452 delete radio;
453 delete usrp;
454}
455
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100456static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500457{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100458 int type, chans;
459 unsigned int i;
460 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700461 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100462
463 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100464 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100465 iface = RadioDevice::MULTI_ARFCN;
466
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100467 /* Generate vector of rx/tx_path: */
468 for (i = 0; i < trx->cfg.num_chans; i++) {
469 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
470 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
471 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100472
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100473 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
474 trx->cfg.num_chans, trx->cfg.offset,
475 tx_paths, rx_paths);
476 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100477 if (type < 0) {
478 LOG(ALERT) << "Failed to create radio device" << std::endl;
479 goto shutdown;
480 }
481
482 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100483 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100484 if (!radio)
485 goto shutdown;
486
487 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100488 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100489 goto shutdown;
490
491 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100492 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100493 << chans << " channel(s)" << std::endl;
494
495 return 0;
496
497shutdown:
498 trx_stop();
499 return -1;
500}
501
502int main(int argc, char *argv[])
503{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100504 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500505
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100506 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
507 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100508 g_vty_info.tall_ctx = tall_trx_ctx;
509
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100510 setup_signal_handlers();
511
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100512 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100513
Philipp Maiere51a8f02017-03-16 12:09:34 +0100514#ifdef HAVE_SSE3
515 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300516#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100517 if (__builtin_cpu_supports("sse3"))
518 printf(" and supported by CPU\n");
519 else
520 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300521#else
522 printf(", but runtime SIMD detection disabled\n");
523#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100524#endif
525
526#ifdef HAVE_SSE4_1
527 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300528#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100529 if (__builtin_cpu_supports("sse4.1"))
530 printf(" and supported by CPU\n");
531 else
532 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300533#else
534 printf(", but runtime SIMD detection disabled\n");
535#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100536#endif
537
Philipp Maier7e07cf22017-03-15 18:09:35 +0100538 convolve_init();
539 convert_init();
540
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200541 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100542 osmo_stats_init(tall_trx_ctx);
543 vty_init(&g_vty_info);
544 ctrl_vty_init(tall_trx_ctx);
545 trx_vty_init(g_trx_ctx);
546
547 logging_vty_add_cmds();
548 osmo_talloc_vty_add_cmds();
549 osmo_stats_vty_add_cmds();
550
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100551 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500552
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100553 rate_ctr_init(tall_trx_ctx);
554
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100555 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100556 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100557 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100558 exit(2);
559 }
560
561 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
562 if (rc < 0)
563 exit(1);
564
565 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
566 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100567 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100568 exit(1);
569 }
570
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100571 /* Backward compatibility: Hack to have 1 channel allocated by default.
572 * Can be Dropped once we * get rid of "-c" cmdline param */
573 if (g_trx_ctx->cfg.num_chans == 0) {
574 g_trx_ctx->cfg.num_chans = 1;
575 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
576 g_trx_ctx->cfg.chans[0].idx = 0;
577 LOG(ERROR) << "No explicit channel config found. Make sure you" \
578 " configure channels in VTY config. Using 1 channel as default," \
579 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200580 }
581
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100582 print_config(g_trx_ctx);
583
584 if (trx_validate_config(g_trx_ctx) < 0) {
585 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500586 return EXIT_FAILURE;
587 }
588
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100589 if (g_trx_ctx->cfg.sched_rr) {
590 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
591 return EXIT_FAILURE;
592 }
593
Thomas Tsou85b179d2013-11-15 21:14:33 -0500594 srandom(time(NULL));
595
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100596 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100597 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500598
599 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100600 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500601
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100602 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500603
604 return 0;
605}