blob: 0e63020d7585b60feb61bb3c5a05b3410fb531d9 [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,
147 trx->cfg.rach_delay, trx->cfg.egprs)) {
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 Pedrolab22f4c2018-02-21 20:15:18 +0100167 fprintf(stdout, "signal %d received\n", signo);
168 switch (signo) {
169 case SIGINT:
170 case SIGTERM:
171 fprintf(stdout, "shutting down\n");
172 gshutdown = true;
173 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100174 case SIGABRT:
175 case SIGUSR1:
176 talloc_report(tall_trx_ctx, stderr);
177 talloc_report_full(tall_trx_ctx, stderr);
178 break;
179 case SIGUSR2:
180 talloc_report_full(tall_trx_ctx, stderr);
181 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100182 default:
183 break;
184 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500185}
186
187static void setup_signal_handlers()
188{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100189 /* Handle keyboard interrupt SIGINT */
190 signal(SIGINT, &sig_handler);
191 signal(SIGTERM, &sig_handler);
192 signal(SIGABRT, &sig_handler);
193 signal(SIGUSR1, &sig_handler);
194 signal(SIGUSR2, &sig_handler);
195 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500196}
197
198static void print_help()
199{
200 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200201 " -h, --help This text\n"
202 " -C, --config Filename The config file to use\n"
203 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100204 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500205}
206
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100207static void print_deprecated(char opt)
208{
209 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
210 << " Please use VTY cfg option instead."
211 << " All cmd line options are already being overriden by VTY options if set.";
212}
213
214static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500215{
216 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100217 unsigned int i;
218 std::vector<std::string> rx_paths, tx_paths;
219 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200220 static struct option long_options[] = {
221 {"help", 0, 0, 'h'},
222 {"config", 1, 0, 'C'},
223 {"version", 0, 0, 'V'},
224 {NULL, 0, 0, 0}
225 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500226
Oliver Smitha439fed2018-10-23 13:12:17 +0200227 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,
228 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500229 switch (option) {
230 case 'h':
231 print_help();
232 exit(0);
233 break;
234 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100235 print_deprecated(option);
236 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500237 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100238 case 'l':
239 print_deprecated(option);
240 log_set_log_level(osmo_stderr_target, atoi(optarg));
241 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500242 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100243 print_deprecated(option);
244 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200245 break;
246 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100247 print_deprecated(option);
248 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500249 break;
250 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100251 print_deprecated(option);
252 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500253 break;
254 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100255 print_deprecated(option);
256 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500257 break;
Tom Tsou76764272016-06-24 14:25:39 -0700258 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100259 print_deprecated(option);
260 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700261 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500262 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100263 print_deprecated(option);
264 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500265 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700266 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100267 print_deprecated(option);
268 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700269 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500270 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100271 print_deprecated(option);
272 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500273 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500274 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100275 print_deprecated(option);
276 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500277 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500278 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100279 print_deprecated(option);
280 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700281 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700282 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100283 print_deprecated(option);
284 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700285 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700286 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100287 print_deprecated(option);
288 trx->cfg.rtsc_set = true;
289 trx->cfg.rtsc = atoi(optarg);
290 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
291 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500292 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300293 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100294 print_deprecated(option);
295 trx->cfg.rach_delay_set = true;
296 trx->cfg.rach_delay = atoi(optarg);
297 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300298 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400299 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100300 print_deprecated(option);
301 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400302 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400303 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100304 print_deprecated(option);
305 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400306 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800307 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100308 print_deprecated(option);
309 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800310 break;
Harald Welte81486e02017-06-29 15:35:22 +0200311 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100312 print_deprecated(option);
313 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200314 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100315 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100316 print_deprecated(option);
317 tx_paths = comma_delimited_to_vector(optarg);
318 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100319 break;
320 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100321 print_deprecated(option);
322 rx_paths = comma_delimited_to_vector(optarg);
323 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100324 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100325 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100326 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100327 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200328 case 'V':
329 print_version(1);
330 exit(0);
331 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500332 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100333 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500334 }
335 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700336
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100337 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700338
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100339 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
340 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700341 goto bad_config;
342 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100343 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
344 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
345 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700346 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700347 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100348 for (i = 0; i < trx->cfg.num_chans; i++) {
349 trx->cfg.chans[i].trx = trx;
350 trx->cfg.chans[i].idx = i;
351 if (tx_paths_set)
352 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
353 if (rx_paths_set)
354 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100355 }
356
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700357 return;
358
359bad_config:
360 print_help();
361 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500362}
363
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100364int trx_validate_config(struct trx_ctx *trx)
365{
366 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > 5) {
367 LOG(ERROR) << "Unsupported number of channels";
368 return -1;
369 }
370
371 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
372 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200373 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100374 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
375 return -1;
376 }
377
378 return 0;
379}
380
381static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200382{
383 struct sched_param param;
384 int rc;
385 memset(&param, 0, sizeof(param));
386 param.sched_priority = prio;
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100387 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority;
Harald Welte81486e02017-06-29 15:35:22 +0200388 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
389 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100390 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200391 return -1;
392 }
393 return 0;
394}
395
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100396static void print_config(struct trx_ctx *trx)
397{
398 unsigned int i;
399 std::ostringstream ost("");
400
401 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100402 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100403 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
404 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
405 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200406 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100407 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
408 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
409 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
410 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
411 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
412 ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl;
413 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
414 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
415 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
416 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
417 ost << " Tx Antennas.............";
418 for (i = 0; i < trx->cfg.num_chans; i++) {
419 std::string p = charp2str(trx->cfg.chans[i].tx_path);
420 ost << " '" << ((p != "") ? p : "<default>") << "'";
421 }
422 ost << std::endl;
423 ost << " Rx Antennas.............";
424 for (i = 0; i < trx->cfg.num_chans; i++) {
425 std::string p = charp2str(trx->cfg.chans[i].rx_path);
426 ost << " '" << ((p != "") ? p : "<default>") << "'";
427 }
428 ost << std::endl;
429
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100430 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100431}
432
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100433static void trx_stop()
434{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100435 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100436
437 delete transceiver;
438 delete radio;
439 delete usrp;
440}
441
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100442static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500443{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100444 int type, chans;
445 unsigned int i;
446 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700447 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100448
449 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100450 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100451 iface = RadioDevice::MULTI_ARFCN;
452
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100453 /* Generate vector of rx/tx_path: */
454 for (i = 0; i < trx->cfg.num_chans; i++) {
455 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
456 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
457 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100458
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100459 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
460 trx->cfg.num_chans, trx->cfg.offset,
461 tx_paths, rx_paths);
462 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100463 if (type < 0) {
464 LOG(ALERT) << "Failed to create radio device" << std::endl;
465 goto shutdown;
466 }
467
468 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100469 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100470 if (!radio)
471 goto shutdown;
472
473 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100474 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100475 goto shutdown;
476
477 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100478 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100479 << chans << " channel(s)" << std::endl;
480
481 return 0;
482
483shutdown:
484 trx_stop();
485 return -1;
486}
487
488int main(int argc, char *argv[])
489{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100490 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500491
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100492 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
493 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100494 g_vty_info.tall_ctx = tall_trx_ctx;
495
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100496 setup_signal_handlers();
497
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100498 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100499
Philipp Maiere51a8f02017-03-16 12:09:34 +0100500#ifdef HAVE_SSE3
501 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300502#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100503 if (__builtin_cpu_supports("sse3"))
504 printf(" and supported by CPU\n");
505 else
506 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300507#else
508 printf(", but runtime SIMD detection disabled\n");
509#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100510#endif
511
512#ifdef HAVE_SSE4_1
513 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300514#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100515 if (__builtin_cpu_supports("sse4.1"))
516 printf(" and supported by CPU\n");
517 else
518 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300519#else
520 printf(", but runtime SIMD detection disabled\n");
521#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100522#endif
523
Philipp Maier7e07cf22017-03-15 18:09:35 +0100524 convolve_init();
525 convert_init();
526
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200527 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100528 osmo_stats_init(tall_trx_ctx);
529 vty_init(&g_vty_info);
530 ctrl_vty_init(tall_trx_ctx);
531 trx_vty_init(g_trx_ctx);
532
533 logging_vty_add_cmds();
534 osmo_talloc_vty_add_cmds();
535 osmo_stats_vty_add_cmds();
536
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100537 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500538
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100539 rate_ctr_init(tall_trx_ctx);
540
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100541 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100542 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100543 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100544 exit(2);
545 }
546
547 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
548 if (rc < 0)
549 exit(1);
550
551 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
552 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100553 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100554 exit(1);
555 }
556
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100557 /* Backward compatibility: Hack to have 1 channel allocated by default.
558 * Can be Dropped once we * get rid of "-c" cmdline param */
559 if (g_trx_ctx->cfg.num_chans == 0) {
560 g_trx_ctx->cfg.num_chans = 1;
561 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
562 g_trx_ctx->cfg.chans[0].idx = 0;
563 LOG(ERROR) << "No explicit channel config found. Make sure you" \
564 " configure channels in VTY config. Using 1 channel as default," \
565 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200566 }
567
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100568 print_config(g_trx_ctx);
569
570 if (trx_validate_config(g_trx_ctx) < 0) {
571 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500572 return EXIT_FAILURE;
573 }
574
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100575 if (g_trx_ctx->cfg.sched_rr) {
576 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
577 return EXIT_FAILURE;
578 }
579
Thomas Tsou85b179d2013-11-15 21:14:33 -0500580 srandom(time(NULL));
581
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100582 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100583 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500584
585 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100586 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500587
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100588 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500589
590 return 0;
591}