blob: 09ba14e24cfa76c7f7ebdbd7dc62524a822cf33f [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>
Harald Welte81486e02017-06-29 15:35:22 +020030#include <sched.h>
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010031#include <vector>
32#include <string>
33#include <sstream>
34#include <iostream>
Thomas Tsou85b179d2013-11-15 21:14:33 -050035
36#include <GSMCommon.h>
37#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050038
Philipp Maier7e07cf22017-03-15 18:09:35 +010039extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010040#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010041#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010042#include <osmocom/core/msgb.h>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010043#include <osmocom/core/stats.h>
44#include <osmocom/vty/logging.h>
45#include <osmocom/vty/ports.h>
46#include <osmocom/vty/misc.h>
47#include <osmocom/vty/telnet_interface.h>
48#include <osmocom/ctrl/control_vty.h>
49#include <osmocom/ctrl/ports.h>
50#include <osmocom/ctrl/control_if.h>
51#include <osmocom/vty/stats.h>
Philipp Maier7e07cf22017-03-15 18:09:35 +010052#include "convolve.h"
53#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010054#include "trx_vty.h"
55#include "debug.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010056}
57
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010058#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050059
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010060#define charp2str(a) ((a) ? std::string(a) : std::string(""))
61
62static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050063
Thomas Tsou85b179d2013-11-15 21:14:33 -050064volatile bool gshutdown = false;
65
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010066static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010067static struct trx_ctx *g_trx_ctx;
68static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010069
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010070static RadioDevice *usrp;
71static RadioInterface *radio;
72static Transceiver *transceiver;
73
Thomas Tsou85b179d2013-11-15 21:14:33 -050074/* Create radio interface
75 * The interface consists of sample rate changes, frequency shifts,
76 * channel multiplexing, and other conversions. The transceiver core
77 * accepts input vectors sampled at multiples of the GSM symbol rate.
78 * The radio interface connects the main transceiver with the device
79 * object, which may be operating some other rate.
80 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010081RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050082 RadioDevice *usrp, int type)
83{
84 RadioInterface *radio = NULL;
85
86 switch (type) {
87 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010088 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
89 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -050090 break;
91 case RadioDevice::RESAMP_64M:
92 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010093 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
94 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -050095 break;
Tom Tsou76764272016-06-24 14:25:39 -070096 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010097 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
98 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -070099 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500100 default:
101 LOG(ALERT) << "Unsupported radio interface configuration";
102 return NULL;
103 }
104
105 if (!radio->init(type)) {
106 LOG(ALERT) << "Failed to initialize radio interface";
107 return NULL;
108 }
109
110 return radio;
111}
112
113/* Create transceiver core
114 * The multi-threaded modem core operates at multiples of the GSM rate of
115 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
116 * and decoding schemes. Also included are the socket interfaces for
117 * connecting to the upper layer stack.
118 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100119int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500120{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500121 VectorFIFO *fifo;
122
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100123 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
124 trx->cfg.remote_addr, trx->cfg.tx_sps,
125 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
126 radio, trx->cfg.rssi_offset);
127 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
128 trx->cfg.rach_delay, trx->cfg.egprs)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500129 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100130 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500131 }
132
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100133 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500134 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100135 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500136 continue;
137
138 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100139 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500140 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100141 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500142}
143
144static void sig_handler(int signo)
145{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100146 fprintf(stdout, "signal %d received\n", signo);
147 switch (signo) {
148 case SIGINT:
149 case SIGTERM:
150 fprintf(stdout, "shutting down\n");
151 gshutdown = true;
152 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100153 case SIGABRT:
154 case SIGUSR1:
155 talloc_report(tall_trx_ctx, stderr);
156 talloc_report_full(tall_trx_ctx, stderr);
157 break;
158 case SIGUSR2:
159 talloc_report_full(tall_trx_ctx, stderr);
160 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100161 default:
162 break;
163 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500164}
165
166static void setup_signal_handlers()
167{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100168 /* Handle keyboard interrupt SIGINT */
169 signal(SIGINT, &sig_handler);
170 signal(SIGTERM, &sig_handler);
171 signal(SIGABRT, &sig_handler);
172 signal(SIGUSR1, &sig_handler);
173 signal(SIGUSR2, &sig_handler);
174 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500175}
176
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100177static std::vector<std::string> comma_delimited_to_vector(char* opt)
178{
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100179 std::string str = std::string(opt);
180 std::vector<std::string> result;
181 std::stringstream ss(str);
182
183 while( ss.good() )
184 {
185 std::string substr;
186 getline(ss, substr, ',');
187 result.push_back(substr);
188 }
189 return result;
190}
191
Thomas Tsou85b179d2013-11-15 21:14:33 -0500192static void print_help()
193{
194 fprintf(stdout, "Options:\n"
195 " -h This text\n"
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100196 " -C Filename The config file to use\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100197 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500198}
199
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100200static void print_deprecated(char opt)
201{
202 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
203 << " Please use VTY cfg option instead."
204 << " All cmd line options are already being overriden by VTY options if set.";
205}
206
207static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500208{
209 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100210 unsigned int i;
211 std::vector<std::string> rx_paths, tx_paths;
212 bool rx_paths_set = false, tx_paths_set = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500213
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100214 while ((option = getopt(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500215 switch (option) {
216 case 'h':
217 print_help();
218 exit(0);
219 break;
220 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100221 print_deprecated(option);
222 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500223 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100224 case 'l':
225 print_deprecated(option);
226 log_set_log_level(osmo_stderr_target, atoi(optarg));
227 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500228 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100229 print_deprecated(option);
230 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200231 break;
232 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100233 print_deprecated(option);
234 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500235 break;
236 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100237 print_deprecated(option);
238 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500239 break;
240 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100241 print_deprecated(option);
242 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500243 break;
Tom Tsou76764272016-06-24 14:25:39 -0700244 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100245 print_deprecated(option);
246 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700247 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500248 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100249 print_deprecated(option);
250 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500251 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700252 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100253 print_deprecated(option);
254 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700255 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500256 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100257 print_deprecated(option);
258 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500259 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500260 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100261 print_deprecated(option);
262 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500263 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500264 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100265 print_deprecated(option);
266 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700267 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700268 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100269 print_deprecated(option);
270 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700271 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700272 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100273 print_deprecated(option);
274 trx->cfg.rtsc_set = true;
275 trx->cfg.rtsc = atoi(optarg);
276 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
277 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500278 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300279 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100280 print_deprecated(option);
281 trx->cfg.rach_delay_set = true;
282 trx->cfg.rach_delay = atoi(optarg);
283 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300284 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400285 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100286 print_deprecated(option);
287 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400288 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400289 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100290 print_deprecated(option);
291 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400292 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800293 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100294 print_deprecated(option);
295 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800296 break;
Harald Welte81486e02017-06-29 15:35:22 +0200297 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100298 print_deprecated(option);
299 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200300 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100301 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100302 print_deprecated(option);
303 tx_paths = comma_delimited_to_vector(optarg);
304 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100305 break;
306 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100307 print_deprecated(option);
308 rx_paths = comma_delimited_to_vector(optarg);
309 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100310 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100311 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100312 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100313 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500314 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100315 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500316 }
317 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700318
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100319 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700320
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100321 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
322 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700323 goto bad_config;
324 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100325 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
326 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
327 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700328 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700329 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100330 for (i = 0; i < trx->cfg.num_chans; i++) {
331 trx->cfg.chans[i].trx = trx;
332 trx->cfg.chans[i].idx = i;
333 if (tx_paths_set)
334 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
335 if (rx_paths_set)
336 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100337 }
338
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700339 return;
340
341bad_config:
342 print_help();
343 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500344}
345
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100346int trx_validate_config(struct trx_ctx *trx)
347{
348 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > 5) {
349 LOG(ERROR) << "Unsupported number of channels";
350 return -1;
351 }
352
353 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
354 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
355 (trx->cfg.tx_sps!=4 || trx->cfg.tx_sps!=4)) {
356 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
357 return -1;
358 }
359
360 return 0;
361}
362
363static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200364{
365 struct sched_param param;
366 int rc;
367 memset(&param, 0, sizeof(param));
368 param.sched_priority = prio;
369 printf("Setting SCHED_RR priority(%d)\n", param.sched_priority);
370 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
371 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100372 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200373 return -1;
374 }
375 return 0;
376}
377
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100378static void print_config(struct trx_ctx *trx)
379{
380 unsigned int i;
381 std::ostringstream ost("");
382
383 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100384 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100385 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
386 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
387 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200388 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100389 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
390 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
391 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
392 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
393 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
394 ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl;
395 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
396 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
397 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
398 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
399 ost << " Tx Antennas.............";
400 for (i = 0; i < trx->cfg.num_chans; i++) {
401 std::string p = charp2str(trx->cfg.chans[i].tx_path);
402 ost << " '" << ((p != "") ? p : "<default>") << "'";
403 }
404 ost << std::endl;
405 ost << " Rx Antennas.............";
406 for (i = 0; i < trx->cfg.num_chans; i++) {
407 std::string p = charp2str(trx->cfg.chans[i].rx_path);
408 ost << " '" << ((p != "") ? p : "<default>") << "'";
409 }
410 ost << std::endl;
411
412 std::cout << ost << std::endl;
413}
414
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100415static void trx_stop()
416{
417 std::cout << "Shutting down transceiver..." << std::endl;
418
419 delete transceiver;
420 delete radio;
421 delete usrp;
422}
423
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100424static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500425{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100426 int type, chans;
427 unsigned int i;
428 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700429 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100430
431 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100432 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100433 iface = RadioDevice::MULTI_ARFCN;
434
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100435 /* Generate vector of rx/tx_path: */
436 for (i = 0; i < trx->cfg.num_chans; i++) {
437 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
438 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
439 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100440
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100441 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
442 trx->cfg.num_chans, trx->cfg.offset,
443 tx_paths, rx_paths);
444 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100445 if (type < 0) {
446 LOG(ALERT) << "Failed to create radio device" << std::endl;
447 goto shutdown;
448 }
449
450 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100451 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100452 if (!radio)
453 goto shutdown;
454
455 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100456 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100457 goto shutdown;
458
459 chans = transceiver->numChans();
460 std::cout << "-- Transceiver active with "
461 << chans << " channel(s)" << std::endl;
462
463 return 0;
464
465shutdown:
466 trx_stop();
467 return -1;
468}
469
470int main(int argc, char *argv[])
471{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100472 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500473
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100474 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
475 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100476 g_vty_info.tall_ctx = tall_trx_ctx;
477
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100478 setup_signal_handlers();
479
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100480 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100481
Philipp Maiere51a8f02017-03-16 12:09:34 +0100482#ifdef HAVE_SSE3
483 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300484#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100485 if (__builtin_cpu_supports("sse3"))
486 printf(" and supported by CPU\n");
487 else
488 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300489#else
490 printf(", but runtime SIMD detection disabled\n");
491#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100492#endif
493
494#ifdef HAVE_SSE4_1
495 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300496#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100497 if (__builtin_cpu_supports("sse4.1"))
498 printf(" and supported by CPU\n");
499 else
500 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300501#else
502 printf(", but runtime SIMD detection disabled\n");
503#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100504#endif
505
Philipp Maier7e07cf22017-03-15 18:09:35 +0100506 convolve_init();
507 convert_init();
508
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200509 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100510 osmo_stats_init(tall_trx_ctx);
511 vty_init(&g_vty_info);
512 ctrl_vty_init(tall_trx_ctx);
513 trx_vty_init(g_trx_ctx);
514
515 logging_vty_add_cmds();
516 osmo_talloc_vty_add_cmds();
517 osmo_stats_vty_add_cmds();
518
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100519 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500520
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100521 rate_ctr_init(tall_trx_ctx);
522
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100523 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100524 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100525 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100526 exit(2);
527 }
528
529 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
530 if (rc < 0)
531 exit(1);
532
533 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
534 if (!g_ctrlh) {
535 fprintf(stderr, "Failed to create CTRL interface.\n");
536 exit(1);
537 }
538
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100539 /* Backward compatibility: Hack to have 1 channel allocated by default.
540 * Can be Dropped once we * get rid of "-c" cmdline param */
541 if (g_trx_ctx->cfg.num_chans == 0) {
542 g_trx_ctx->cfg.num_chans = 1;
543 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
544 g_trx_ctx->cfg.chans[0].idx = 0;
545 LOG(ERROR) << "No explicit channel config found. Make sure you" \
546 " configure channels in VTY config. Using 1 channel as default," \
547 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200548 }
549
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100550 print_config(g_trx_ctx);
551
552 if (trx_validate_config(g_trx_ctx) < 0) {
553 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500554 return EXIT_FAILURE;
555 }
556
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100557 if (g_trx_ctx->cfg.sched_rr) {
558 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
559 return EXIT_FAILURE;
560 }
561
Thomas Tsou85b179d2013-11-15 21:14:33 -0500562 srandom(time(NULL));
563
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100564 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100565 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500566
567 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100568 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500569
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100570 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500571
572 return 0;
573}