blob: 7bc025f1ff628a61d1158ce90badb31af23cc154 [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
Thomas Tsou85b179d2013-11-15 21:14:33 -050058/* Samples-per-symbol for downlink path
59 * 4 - Uses precision modulator (more computation, less distortion)
60 * 1 - Uses minimized modulator (less computation, more distortion)
61 *
62 * Other values are invalid. Receive path (uplink) is always
Tom Tsou0fe41a52016-05-03 15:14:45 -070063 * downsampled to 1 sps. Default to 4 sps for all cases.
Thomas Tsou85b179d2013-11-15 21:14:33 -050064 */
Tom Tsou5cd70dc2016-03-06 01:28:40 -080065#define DEFAULT_TX_SPS 4
Thomas Tsou85b179d2013-11-15 21:14:33 -050066
Tom Tsou5cd70dc2016-03-06 01:28:40 -080067/*
68 * Samples-per-symbol for uplink (receiver) path
69 * Do not modify this value. EDGE configures 4 sps automatically on
70 * B200/B210 devices only. Use of 4 sps on the receive path for other
71 * configurations is not supported.
72 */
73#define DEFAULT_RX_SPS 1
74
Tom Tsoude116e92017-03-30 17:00:42 -070075/* Default configuration parameters */
Thomas Tsou85b179d2013-11-15 21:14:33 -050076#define DEFAULT_TRX_PORT 5700
77#define DEFAULT_TRX_IP "127.0.0.1"
Thomas Tsou85b179d2013-11-15 21:14:33 -050078#define DEFAULT_CHANS 1
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010079#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050080
81struct trx_config {
Pau Espin Pedrol8c800952017-08-16 16:53:23 +020082 std::string local_addr;
83 std::string remote_addr;
Thomas Tsou85b179d2013-11-15 21:14:33 -050084 std::string dev_args;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010085 char* config_file;
Thomas Tsou85b179d2013-11-15 21:14:33 -050086 unsigned port;
Tom Tsou5cd70dc2016-03-06 01:28:40 -080087 unsigned tx_sps;
88 unsigned rx_sps;
Thomas Tsou85b179d2013-11-15 21:14:33 -050089 unsigned chans;
Tom Tsou64ad7122015-05-19 18:26:31 -070090 unsigned rtsc;
Alexander Chemeris37c52c72016-03-25 18:28:34 +030091 unsigned rach_delay;
Thomas Tsou85b179d2013-11-15 21:14:33 -050092 bool extref;
Tom Tsou2f3e60b2016-07-17 19:29:08 -070093 bool gpsref;
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010094 FillerType filler;
Tom Tsou76764272016-06-24 14:25:39 -070095 bool mcbts;
Thomas Tsou8e17df72014-03-06 14:16:11 -050096 double offset;
Alexander Chemerise8905a02015-06-03 23:47:56 -040097 double rssi_offset;
Alexander Chemeris50747dc2015-06-07 01:07:45 -040098 bool swap_channels;
Tom Tsoub0aefcb2016-03-06 03:44:34 -080099 bool edge;
Harald Welte81486e02017-06-29 15:35:22 +0200100 int sched_rr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100101 std::vector<std::string> rx_paths;
102 std::vector<std::string> tx_paths;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500103};
104
Thomas Tsou85b179d2013-11-15 21:14:33 -0500105volatile bool gshutdown = false;
106
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100107static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100108static struct trx_ctx *g_trx_ctx;
109static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100110
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100111static RadioDevice *usrp;
112static RadioInterface *radio;
113static Transceiver *transceiver;
114
Thomas Tsou85b179d2013-11-15 21:14:33 -0500115/* Setup configuration values
116 * Don't query the existence of the Log.Level because it's a
117 * mandatory value. That is, if it doesn't exist, the configuration
Thomas Tsou4de70be2013-11-17 18:54:52 -0500118 * table will crash or will have already crashed. Everything else we
119 * can survive without and use default values if the database entries
Thomas Tsou85b179d2013-11-15 21:14:33 -0500120 * are empty.
121 */
122bool trx_setup_config(struct trx_config *config)
123{
Tom Tsou76764272016-06-24 14:25:39 -0700124 std::string refstr, fillstr, divstr, mcstr, edgestr;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100125 std::vector<std::string>::const_iterator si;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500126
Tom Tsou60317342017-03-30 19:36:41 -0700127 if (config->mcbts && config->chans > 5) {
Tom Tsou76764272016-06-24 14:25:39 -0700128 std::cout << "Unsupported number of channels" << std::endl;
129 return false;
130 }
131
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800132 edgestr = config->edge ? "Enabled" : "Disabled";
Tom Tsou76764272016-06-24 14:25:39 -0700133 mcstr = config->mcbts ? "Enabled" : "Disabled";
134
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700135 if (config->extref)
136 refstr = "External";
137 else if (config->gpsref)
138 refstr = "GPS";
139 else
140 refstr = "Internal";
141
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400142 switch (config->filler) {
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100143 case FILLER_DUMMY:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400144 fillstr = "Dummy bursts";
145 break;
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100146 case FILLER_ZERO:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400147 fillstr = "Disabled";
148 break;
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100149 case FILLER_NORM_RAND:
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400150 fillstr = "Normal busrts with random payload";
151 break;
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100152 case FILLER_EDGE_RAND:
Tom Tsouaf717b22016-03-06 22:19:15 -0800153 fillstr = "EDGE busrts with random payload";
154 break;
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100155 case FILLER_ACCESS_RAND:
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300156 fillstr = "Access busrts with random payload";
157 break;
Alexander Chemerisf5fd5782015-05-24 18:56:51 -0400158 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500159
160 std::ostringstream ost("");
161 ost << "Config Settings" << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500162 ost << " Device args............. " << config->dev_args << std::endl;
163 ost << " TRX Base Port........... " << config->port << std::endl;
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200164 ost << " TRX Address............. " << config->local_addr << std::endl;
165 ost << " GSM Core Address........." << config->remote_addr << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500166 ost << " Channels................ " << config->chans << std::endl;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800167 ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
Alexander Chemeris1ab5e7f2016-04-20 08:44:55 +0300168 ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800169 ost << " EDGE support............ " << edgestr << std::endl;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700170 ost << " Reference............... " << refstr << std::endl;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500171 ost << " C0 Filler Table......... " << fillstr << std::endl;
Tom Tsou76764272016-06-24 14:25:39 -0700172 ost << " Multi-Carrier........... " << mcstr << std::endl;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500173 ost << " Tuning offset........... " << config->offset << std::endl;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400174 ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400175 ost << " Swap channels........... " << config->swap_channels << std::endl;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100176 ost << " Tx Antennas.............";
177 for (si = config->tx_paths.begin(); si != config->tx_paths.end(); ++si)
178 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
179 ost << std::endl;
180 ost << " Rx Antennas.............";
181 for (si = config->rx_paths.begin(); si != config->rx_paths.end(); ++si)
182 ost << " '" << ((*si != "") ? *si : "<default>") << "'";
183 ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500184
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100185 std::cout << ost << std::endl;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500186 return true;
187}
188
189/* Create radio interface
190 * The interface consists of sample rate changes, frequency shifts,
191 * channel multiplexing, and other conversions. The transceiver core
192 * accepts input vectors sampled at multiples of the GSM symbol rate.
193 * The radio interface connects the main transceiver with the device
194 * object, which may be operating some other rate.
195 */
196RadioInterface *makeRadioInterface(struct trx_config *config,
197 RadioDevice *usrp, int type)
198{
199 RadioInterface *radio = NULL;
200
201 switch (type) {
202 case RadioDevice::NORMAL:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800203 radio = new RadioInterface(usrp, config->tx_sps,
204 config->rx_sps, config->chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500205 break;
206 case RadioDevice::RESAMP_64M:
207 case RadioDevice::RESAMP_100M:
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800208 radio = new RadioInterfaceResamp(usrp, config->tx_sps,
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700209 config->rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500210 break;
Tom Tsou76764272016-06-24 14:25:39 -0700211 case RadioDevice::MULTI_ARFCN:
212 radio = new RadioInterfaceMulti(usrp, config->tx_sps,
213 config->rx_sps, config->chans);
214 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500215 default:
216 LOG(ALERT) << "Unsupported radio interface configuration";
217 return NULL;
218 }
219
220 if (!radio->init(type)) {
221 LOG(ALERT) << "Failed to initialize radio interface";
222 return NULL;
223 }
224
225 return radio;
226}
227
228/* Create transceiver core
229 * The multi-threaded modem core operates at multiples of the GSM rate of
230 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
231 * and decoding schemes. Also included are the socket interfaces for
232 * connecting to the upper layer stack.
233 */
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100234int makeTransceiver(struct trx_config *config, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500235{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500236 VectorFIFO *fifo;
237
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100238 transceiver = new Transceiver(config->port, config->local_addr.c_str(),
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200239 config->remote_addr.c_str(), config->tx_sps,
240 config->rx_sps, config->chans, GSM::Time(3,0),
241 radio, config->rssi_offset);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100242 if (!transceiver->init(config->filler, config->rtsc,
Tom Tsou64464e62016-07-01 03:46:46 -0700243 config->rach_delay, config->edge)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500244 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100245 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500246 }
247
248 for (size_t i = 0; i < config->chans; i++) {
249 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100250 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500251 continue;
252
253 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100254 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500255 }
256
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100257 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500258}
259
260static void sig_handler(int signo)
261{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100262 fprintf(stdout, "signal %d received\n", signo);
263 switch (signo) {
264 case SIGINT:
265 case SIGTERM:
266 fprintf(stdout, "shutting down\n");
267 gshutdown = true;
268 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100269 case SIGABRT:
270 case SIGUSR1:
271 talloc_report(tall_trx_ctx, stderr);
272 talloc_report_full(tall_trx_ctx, stderr);
273 break;
274 case SIGUSR2:
275 talloc_report_full(tall_trx_ctx, stderr);
276 break;
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100277 default:
278 break;
279 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500280}
281
282static void setup_signal_handlers()
283{
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100284 /* Handle keyboard interrupt SIGINT */
285 signal(SIGINT, &sig_handler);
286 signal(SIGTERM, &sig_handler);
287 signal(SIGABRT, &sig_handler);
288 signal(SIGUSR1, &sig_handler);
289 signal(SIGUSR2, &sig_handler);
290 osmo_init_ignore_signals();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500291}
292
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100293
294static std::vector<std::string> comma_delimited_to_vector(char* opt) {
295 std::string str = std::string(opt);
296 std::vector<std::string> result;
297 std::stringstream ss(str);
298
299 while( ss.good() )
300 {
301 std::string substr;
302 getline(ss, substr, ',');
303 result.push_back(substr);
304 }
305 return result;
306}
307
Thomas Tsou85b179d2013-11-15 21:14:33 -0500308static void print_help()
309{
310 fprintf(stdout, "Options:\n"
311 " -h This text\n"
312 " -a UHD device args\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500313 " -i IP address of GSM core\n"
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200314 " -j IP address of osmo-trx\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500315 " -p Base port number\n"
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800316 " -e Enable EDGE receiver\n"
Tom Tsou76764272016-06-24 14:25:39 -0700317 " -m Enable multi-ARFCN transceiver (default=disabled)\n"
Thomas Tsou85b179d2013-11-15 21:14:33 -0500318 " -x Enable external 10 MHz reference\n"
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700319 " -g Enable GPSDO reference\n"
Tom Tsou2e4ed102016-06-27 15:39:16 -0700320 " -s Tx samples-per-symbol (1 or 4)\n"
321 " -b Rx samples-per-symbol (1 or 4)\n"
Thomas Tsou15d743e2014-01-25 02:34:03 -0500322 " -c Number of ARFCN channels (default=1)\n"
Thomas Tsou8e17df72014-03-06 14:16:11 -0500323 " -f Enable C0 filler table\n"
Tom Tsou64ad7122015-05-19 18:26:31 -0700324 " -o Set baseband frequency offset (default=auto)\n"
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300325 " -r Random Normal Burst test mode with TSC\n"
326 " -A Random Access Burst test mode with delay\n"
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400327 " -R RSSI to dBm offset in dB (default=0)\n"
Harald Welte81486e02017-06-29 15:35:22 +0200328 " -S Swap channels (UmTRX only)\n"
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100329 " -t SCHED_RR real-time priority (1..32)\n"
330 " -y comma-delimited list of Tx paths (num elements matches -c)\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100331 " -z comma-delimited list of Rx paths (num elements matches -c)\n"
332 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500333}
334
335static void handle_options(int argc, char **argv, struct trx_config *config)
336{
337 int option;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100338 bool tx_path_set = false, rx_path_set = false;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500339
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200340 config->local_addr = DEFAULT_TRX_IP;
341 config->remote_addr = DEFAULT_TRX_IP;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100342 config->config_file = (char *)DEFAULT_CONFIG_FILE;
Tom Tsoude116e92017-03-30 17:00:42 -0700343 config->port = DEFAULT_TRX_PORT;
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800344 config->tx_sps = DEFAULT_TX_SPS;
345 config->rx_sps = DEFAULT_RX_SPS;
Tom Tsou64ad7122015-05-19 18:26:31 -0700346 config->chans = DEFAULT_CHANS;
347 config->rtsc = 0;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300348 config->rach_delay = 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500349 config->extref = false;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700350 config->gpsref = false;
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100351 config->filler = FILLER_ZERO;
Tom Tsou76764272016-06-24 14:25:39 -0700352 config->mcbts = false;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500353 config->offset = 0.0;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400354 config->rssi_offset = 0.0;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400355 config->swap_channels = false;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800356 config->edge = false;
Harald Welte81486e02017-06-29 15:35:22 +0200357 config->sched_rr = -1;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100358 config->tx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
359 config->rx_paths = std::vector<std::string>(DEFAULT_CHANS, "");
Thomas Tsou85b179d2013-11-15 21:14:33 -0500360
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100361 while ((option = getopt(argc, argv, "ha:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:")) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500362 switch (option) {
363 case 'h':
364 print_help();
365 exit(0);
366 break;
367 case 'a':
368 config->dev_args = optarg;
369 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500370 case 'i':
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200371 config->remote_addr = optarg;
372 break;
373 case 'j':
374 config->local_addr = optarg;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500375 break;
376 case 'p':
377 config->port = atoi(optarg);
378 break;
379 case 'c':
380 config->chans = atoi(optarg);
381 break;
Tom Tsou76764272016-06-24 14:25:39 -0700382 case 'm':
383 config->mcbts = true;
384 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500385 case 'x':
386 config->extref = true;
387 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700388 case 'g':
389 config->gpsref = true;
390 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500391 case 'f':
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100392 config->filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500393 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500394 case 'o':
395 config->offset = atof(optarg);
396 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500397 case 's':
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800398 config->tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700399 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700400 case 'b':
401 config->rx_sps = atoi(optarg);
402 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700403 case 'r':
404 config->rtsc = atoi(optarg);
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100405 config->filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500406 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300407 case 'A':
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300408 config->rach_delay = atoi(optarg);
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100409 config->filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300410 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400411 case 'R':
412 config->rssi_offset = atof(optarg);
413 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400414 case 'S':
415 config->swap_channels = true;
416 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800417 case 'e':
418 config->edge = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800419 break;
Harald Welte81486e02017-06-29 15:35:22 +0200420 case 't':
421 config->sched_rr = atoi(optarg);
422 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100423 case 'y':
424 config->tx_paths = comma_delimited_to_vector(optarg);
425 tx_path_set = true;
426 break;
427 case 'z':
428 config->rx_paths = comma_delimited_to_vector(optarg);
429 rx_path_set = true;
430 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100431 case 'C':
432 config->config_file = optarg;
433 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500434 default:
435 print_help();
436 exit(0);
437 }
438 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700439
Tom Tsou76764272016-06-24 14:25:39 -0700440 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
441 if ((config->edge) || (config->mcbts)) {
Tom Tsou2e4ed102016-06-27 15:39:16 -0700442 config->tx_sps = 4;
443 config->rx_sps = 4;
444 }
445
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700446 if (config->gpsref && config->extref) {
447 printf("External and GPSDO references unavailable at the same time\n\n");
448 goto bad_config;
449 }
450
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100451 if (config->edge && (config->filler == FILLER_NORM_RAND))
452 config->filler = FILLER_EDGE_RAND;
Tom Tsouaf717b22016-03-06 22:19:15 -0800453
Tom Tsou76764272016-06-24 14:25:39 -0700454 if ((config->tx_sps != 1) && (config->tx_sps != 4) &&
455 (config->rx_sps != 1) && (config->rx_sps != 4)) {
Tom Tsou5cd70dc2016-03-06 01:28:40 -0800456 printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700457 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700458 }
459
460 if (config->rtsc > 7) {
461 printf("Invalid training sequence %i\n\n", config->rtsc);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700462 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700463 }
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300464
465 if (config->rach_delay > 68) {
466 printf("RACH delay is too big %i\n\n", config->rach_delay);
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700467 goto bad_config;
Alexander Chemeris37c52c72016-03-25 18:28:34 +0300468 }
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700469
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100470 if (!tx_path_set) {
471 config->tx_paths = std::vector<std::string>(config->chans, "");
472 } else if (config->tx_paths.size() != config->chans) {
473 printf("Num of channels and num of Tx Antennas doesn't match\n\n");
474 goto bad_config;
475 }
476 if (!rx_path_set) {
477 config->rx_paths = std::vector<std::string>(config->chans, "");
478 } else if (config->rx_paths.size() != config->chans) {
479 printf("Num of channels and num of Rx Antennas doesn't match\n\n");
480 goto bad_config;
481 }
482
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700483 return;
484
485bad_config:
486 print_help();
487 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500488}
489
Harald Welte81486e02017-06-29 15:35:22 +0200490static int set_sched_rr(int prio)
491{
492 struct sched_param param;
493 int rc;
494 memset(&param, 0, sizeof(param));
495 param.sched_priority = prio;
496 printf("Setting SCHED_RR priority(%d)\n", param.sched_priority);
497 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
498 if (rc != 0) {
499 std::cerr << "Config: Setting SCHED_RR failed" << std::endl;
500 return -1;
501 }
502 return 0;
503}
504
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100505static void trx_stop()
506{
507 std::cout << "Shutting down transceiver..." << std::endl;
508
509 delete transceiver;
510 delete radio;
511 delete usrp;
512}
513
514static int trx_start(struct trx_config *config)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500515{
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700516 int type, chans, ref;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700517 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100518
519 /* Create the low level device object */
520 if (config->mcbts)
521 iface = RadioDevice::MULTI_ARFCN;
522
523 if (config->extref)
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100524 ref = REF_EXTERNAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100525 else if (config->gpsref)
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100526 ref = REF_GPS;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100527 else
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +0100528 ref = REF_INTERNAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100529
530 usrp = RadioDevice::make(config->tx_sps, config->rx_sps, iface,
531 config->chans, config->offset, config->tx_paths, config->rx_paths);
532 type = usrp->open(config->dev_args, ref, config->swap_channels);
533 if (type < 0) {
534 LOG(ALERT) << "Failed to create radio device" << std::endl;
535 goto shutdown;
536 }
537
538 /* Setup the appropriate device interface */
539 radio = makeRadioInterface(config, usrp, type);
540 if (!radio)
541 goto shutdown;
542
543 /* Create the transceiver core */
544 if(makeTransceiver(config, radio) < 0)
545 goto shutdown;
546
547 chans = transceiver->numChans();
548 std::cout << "-- Transceiver active with "
549 << chans << " channel(s)" << std::endl;
550
551 return 0;
552
553shutdown:
554 trx_stop();
555 return -1;
556}
557
558int main(int argc, char *argv[])
559{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500560 struct trx_config config;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100561 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500562
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100563 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
564 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100565 g_vty_info.tall_ctx = tall_trx_ctx;
566
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100567 setup_signal_handlers();
568
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100569 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100570
Philipp Maiere51a8f02017-03-16 12:09:34 +0100571#ifdef HAVE_SSE3
572 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300573#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100574 if (__builtin_cpu_supports("sse3"))
575 printf(" and supported by CPU\n");
576 else
577 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300578#else
579 printf(", but runtime SIMD detection disabled\n");
580#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100581#endif
582
583#ifdef HAVE_SSE4_1
584 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300585#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100586 if (__builtin_cpu_supports("sse4.1"))
587 printf(" and supported by CPU\n");
588 else
589 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300590#else
591 printf(", but runtime SIMD detection disabled\n");
592#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100593#endif
594
Philipp Maier7e07cf22017-03-15 18:09:35 +0100595 convolve_init();
596 convert_init();
597
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100598 osmo_init_logging(&log_info);
599 osmo_stats_init(tall_trx_ctx);
600 vty_init(&g_vty_info);
601 ctrl_vty_init(tall_trx_ctx);
602 trx_vty_init(g_trx_ctx);
603
604 logging_vty_add_cmds();
605 osmo_talloc_vty_add_cmds();
606 osmo_stats_vty_add_cmds();
607
Thomas Tsou85b179d2013-11-15 21:14:33 -0500608 handle_options(argc, argv, &config);
609
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100610 rate_ctr_init(tall_trx_ctx);
611
612 rc = vty_read_config_file(config.config_file, NULL);
613 if (rc < 0) {
614 fprintf(stderr, "Failed to open config file: '%s'\n", config.config_file);
615 exit(2);
616 }
617
618 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
619 if (rc < 0)
620 exit(1);
621
622 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
623 if (!g_ctrlh) {
624 fprintf(stderr, "Failed to create CTRL interface.\n");
625 exit(1);
626 }
627
Harald Welte81486e02017-06-29 15:35:22 +0200628 if (config.sched_rr != -1) {
629 if (set_sched_rr(config.sched_rr) < 0)
630 return EXIT_FAILURE;
631 }
632
Thomas Tsou85b179d2013-11-15 21:14:33 -0500633 /* Check database sanity */
634 if (!trx_setup_config(&config)) {
635 std::cerr << "Config: Database failure - exiting" << std::endl;
636 return EXIT_FAILURE;
637 }
638
Thomas Tsou85b179d2013-11-15 21:14:33 -0500639 srandom(time(NULL));
640
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100641 if(trx_start(&config) < 0)
642 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500643
644 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100645 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500646
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100647 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500648
649 return 0;
650}