Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 1 | /* |
| 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 Welte | 81486e0 | 2017-06-29 15:35:22 +0200 | [diff] [blame] | 30 | #include <sched.h> |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 31 | #include <vector> |
| 32 | #include <string> |
| 33 | #include <sstream> |
| 34 | #include <iostream> |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 35 | |
| 36 | #include <GSMCommon.h> |
| 37 | #include <Logger.h> |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 38 | |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 39 | extern "C" { |
Pau Espin Pedrol | 3a3b220 | 2018-02-21 20:15:47 +0100 | [diff] [blame] | 40 | #include <osmocom/core/talloc.h> |
Pau Espin Pedrol | ab22f4c | 2018-02-21 20:15:18 +0100 | [diff] [blame] | 41 | #include <osmocom/core/application.h> |
Pau Espin Pedrol | 3a3b220 | 2018-02-21 20:15:47 +0100 | [diff] [blame] | 42 | #include <osmocom/core/msgb.h> |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 43 | #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> |
Pau Espin Pedrol | 16e7e20 | 2018-06-15 15:19:21 +0200 | [diff] [blame] | 52 | #include <osmocom/vty/command.h> |
| 53 | |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 54 | #include "convolve.h" |
| 55 | #include "convert.h" |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 56 | #include "trx_vty.h" |
| 57 | #include "debug.h" |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 60 | #define DEFAULT_CONFIG_FILE "osmo-trx.cfg" |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 61 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 62 | #define charp2str(a) ((a) ? std::string(a) : std::string("")) |
| 63 | |
| 64 | static char* config_file = (char*)DEFAULT_CONFIG_FILE; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 65 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 66 | volatile bool gshutdown = false; |
| 67 | |
Pau Espin Pedrol | 3a3b220 | 2018-02-21 20:15:47 +0100 | [diff] [blame] | 68 | static void *tall_trx_ctx; |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 69 | static struct trx_ctx *g_trx_ctx; |
| 70 | static struct ctrl_handle *g_ctrlh; |
Pau Espin Pedrol | 3a3b220 | 2018-02-21 20:15:47 +0100 | [diff] [blame] | 71 | |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 72 | static RadioDevice *usrp; |
| 73 | static RadioInterface *radio; |
| 74 | static Transceiver *transceiver; |
| 75 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 76 | /* Create radio interface |
| 77 | * The interface consists of sample rate changes, frequency shifts, |
| 78 | * channel multiplexing, and other conversions. The transceiver core |
| 79 | * accepts input vectors sampled at multiples of the GSM symbol rate. |
| 80 | * The radio interface connects the main transceiver with the device |
| 81 | * object, which may be operating some other rate. |
| 82 | */ |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 83 | RadioInterface *makeRadioInterface(struct trx_ctx *trx, |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 84 | RadioDevice *usrp, int type) |
| 85 | { |
| 86 | RadioInterface *radio = NULL; |
| 87 | |
| 88 | switch (type) { |
| 89 | case RadioDevice::NORMAL: |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 90 | radio = new RadioInterface(usrp, trx->cfg.tx_sps, |
| 91 | trx->cfg.rx_sps, trx->cfg.num_chans); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 92 | break; |
| 93 | case RadioDevice::RESAMP_64M: |
| 94 | case RadioDevice::RESAMP_100M: |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 95 | radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps, |
| 96 | trx->cfg.rx_sps); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 97 | break; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 98 | case RadioDevice::MULTI_ARFCN: |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 99 | radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps, |
| 100 | trx->cfg.rx_sps, trx->cfg.num_chans); |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 101 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 102 | default: |
| 103 | LOG(ALERT) << "Unsupported radio interface configuration"; |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | if (!radio->init(type)) { |
| 108 | LOG(ALERT) << "Failed to initialize radio interface"; |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | return radio; |
| 113 | } |
| 114 | |
| 115 | /* Create transceiver core |
| 116 | * The multi-threaded modem core operates at multiples of the GSM rate of |
| 117 | * 270.8333 ksps and consists of GSM specific modulation, demodulation, |
| 118 | * and decoding schemes. Also included are the socket interfaces for |
| 119 | * connecting to the upper layer stack. |
| 120 | */ |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 121 | int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio) |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 122 | { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 123 | VectorFIFO *fifo; |
| 124 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 125 | transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr, |
| 126 | trx->cfg.remote_addr, trx->cfg.tx_sps, |
| 127 | trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0), |
| 128 | radio, trx->cfg.rssi_offset); |
| 129 | if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc, |
| 130 | trx->cfg.rach_delay, trx->cfg.egprs)) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 131 | LOG(ALERT) << "Failed to initialize transceiver"; |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 132 | return -1; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 135 | for (size_t i = 0; i < trx->cfg.num_chans; i++) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 136 | fifo = radio->receiveFIFO(i); |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 137 | if (fifo && transceiver->receiveFIFO(fifo, i)) |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 138 | continue; |
| 139 | |
| 140 | LOG(ALERT) << "Could not attach FIFO to channel " << i; |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 141 | return -1; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 142 | } |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 143 | return 0; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void sig_handler(int signo) |
| 147 | { |
Pau Espin Pedrol | ab22f4c | 2018-02-21 20:15:18 +0100 | [diff] [blame] | 148 | fprintf(stdout, "signal %d received\n", signo); |
| 149 | switch (signo) { |
| 150 | case SIGINT: |
| 151 | case SIGTERM: |
| 152 | fprintf(stdout, "shutting down\n"); |
| 153 | gshutdown = true; |
| 154 | break; |
Pau Espin Pedrol | 3a3b220 | 2018-02-21 20:15:47 +0100 | [diff] [blame] | 155 | case SIGABRT: |
| 156 | case SIGUSR1: |
| 157 | talloc_report(tall_trx_ctx, stderr); |
| 158 | talloc_report_full(tall_trx_ctx, stderr); |
| 159 | break; |
| 160 | case SIGUSR2: |
| 161 | talloc_report_full(tall_trx_ctx, stderr); |
| 162 | break; |
Pau Espin Pedrol | ab22f4c | 2018-02-21 20:15:18 +0100 | [diff] [blame] | 163 | default: |
| 164 | break; |
| 165 | } |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void setup_signal_handlers() |
| 169 | { |
Pau Espin Pedrol | ab22f4c | 2018-02-21 20:15:18 +0100 | [diff] [blame] | 170 | /* Handle keyboard interrupt SIGINT */ |
| 171 | signal(SIGINT, &sig_handler); |
| 172 | signal(SIGTERM, &sig_handler); |
| 173 | signal(SIGABRT, &sig_handler); |
| 174 | signal(SIGUSR1, &sig_handler); |
| 175 | signal(SIGUSR2, &sig_handler); |
| 176 | osmo_init_ignore_signals(); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 179 | static std::vector<std::string> comma_delimited_to_vector(char* opt) |
| 180 | { |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 181 | std::string str = std::string(opt); |
| 182 | std::vector<std::string> result; |
| 183 | std::stringstream ss(str); |
| 184 | |
| 185 | while( ss.good() ) |
| 186 | { |
| 187 | std::string substr; |
| 188 | getline(ss, substr, ','); |
| 189 | result.push_back(substr); |
| 190 | } |
| 191 | return result; |
| 192 | } |
| 193 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 194 | static void print_help() |
| 195 | { |
| 196 | fprintf(stdout, "Options:\n" |
| 197 | " -h This text\n" |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 198 | " -C Filename The config file to use\n" |
Pau Espin Pedrol | 16e7e20 | 2018-06-15 15:19:21 +0200 | [diff] [blame] | 199 | " -V Print the version of OsmoTRX\n" |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 200 | ); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 203 | static void print_deprecated(char opt) |
| 204 | { |
| 205 | LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed." |
| 206 | << " Please use VTY cfg option instead." |
| 207 | << " All cmd line options are already being overriden by VTY options if set."; |
| 208 | } |
| 209 | |
| 210 | static void handle_options(int argc, char **argv, struct trx_ctx* trx) |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 211 | { |
| 212 | int option; |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 213 | unsigned int i; |
| 214 | std::vector<std::string> rx_paths, tx_paths; |
| 215 | bool rx_paths_set = false, tx_paths_set = false; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 216 | |
Pau Espin Pedrol | 16e7e20 | 2018-06-15 15:19:21 +0200 | [diff] [blame] | 217 | while ((option = getopt(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:V")) != -1) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 218 | switch (option) { |
| 219 | case 'h': |
| 220 | print_help(); |
| 221 | exit(0); |
| 222 | break; |
| 223 | case 'a': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 224 | print_deprecated(option); |
| 225 | osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 226 | break; |
Pau Espin Pedrol | 8dffadb | 2018-03-06 18:38:22 +0100 | [diff] [blame] | 227 | case 'l': |
| 228 | print_deprecated(option); |
| 229 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 230 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 231 | case 'i': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 232 | print_deprecated(option); |
| 233 | osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg); |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 234 | break; |
| 235 | case 'j': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 236 | print_deprecated(option); |
| 237 | osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 238 | break; |
| 239 | case 'p': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 240 | print_deprecated(option); |
| 241 | trx->cfg.base_port = atoi(optarg); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 242 | break; |
| 243 | case 'c': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 244 | print_deprecated(option); |
| 245 | trx->cfg.num_chans = atoi(optarg); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 246 | break; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 247 | case 'm': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 248 | print_deprecated(option); |
| 249 | trx->cfg.multi_arfcn = true; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 250 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 251 | case 'x': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 252 | print_deprecated(option); |
| 253 | trx->cfg.clock_ref = REF_EXTERNAL; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 254 | break; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 255 | case 'g': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 256 | print_deprecated(option); |
| 257 | trx->cfg.clock_ref = REF_GPS; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 258 | break; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 259 | case 'f': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 260 | print_deprecated(option); |
| 261 | trx->cfg.filler = FILLER_DUMMY; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 262 | break; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 263 | case 'o': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 264 | print_deprecated(option); |
| 265 | trx->cfg.offset = atof(optarg); |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 266 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 267 | case 's': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 268 | print_deprecated(option); |
| 269 | trx->cfg.tx_sps = atoi(optarg); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 270 | break; |
Tom Tsou | 2e4ed10 | 2016-06-27 15:39:16 -0700 | [diff] [blame] | 271 | case 'b': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 272 | print_deprecated(option); |
| 273 | trx->cfg.rx_sps = atoi(optarg); |
Tom Tsou | 2e4ed10 | 2016-06-27 15:39:16 -0700 | [diff] [blame] | 274 | break; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 275 | case 'r': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 276 | print_deprecated(option); |
| 277 | trx->cfg.rtsc_set = true; |
| 278 | trx->cfg.rtsc = atoi(optarg); |
| 279 | if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */ |
| 280 | trx->cfg.filler = FILLER_NORM_RAND; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 281 | break; |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 282 | case 'A': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 283 | print_deprecated(option); |
| 284 | trx->cfg.rach_delay_set = true; |
| 285 | trx->cfg.rach_delay = atoi(optarg); |
| 286 | trx->cfg.filler = FILLER_ACCESS_RAND; |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 287 | break; |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 288 | case 'R': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 289 | print_deprecated(option); |
| 290 | trx->cfg.rssi_offset = atof(optarg); |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 291 | break; |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 292 | case 'S': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 293 | print_deprecated(option); |
| 294 | trx->cfg.swap_channels = true; |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 295 | break; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 296 | case 'e': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 297 | print_deprecated(option); |
| 298 | trx->cfg.egprs = true; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 299 | break; |
Harald Welte | 81486e0 | 2017-06-29 15:35:22 +0200 | [diff] [blame] | 300 | case 't': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 301 | print_deprecated(option); |
| 302 | trx->cfg.sched_rr = atoi(optarg); |
Harald Welte | 81486e0 | 2017-06-29 15:35:22 +0200 | [diff] [blame] | 303 | break; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 304 | case 'y': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 305 | print_deprecated(option); |
| 306 | tx_paths = comma_delimited_to_vector(optarg); |
| 307 | tx_paths_set = true; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 308 | break; |
| 309 | case 'z': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 310 | print_deprecated(option); |
| 311 | rx_paths = comma_delimited_to_vector(optarg); |
| 312 | rx_paths_set = true; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 313 | break; |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 314 | case 'C': |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 315 | config_file = optarg; |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 316 | break; |
Pau Espin Pedrol | 16e7e20 | 2018-06-15 15:19:21 +0200 | [diff] [blame] | 317 | case 'V': |
| 318 | print_version(1); |
| 319 | exit(0); |
| 320 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 321 | default: |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 322 | goto bad_config; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 323 | } |
| 324 | } |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 325 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 326 | /* Cmd line option specific validation & setup */ |
Tom Tsou | 2e4ed10 | 2016-06-27 15:39:16 -0700 | [diff] [blame] | 327 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 328 | if (trx->cfg.num_chans > TRX_CHAN_MAX) { |
| 329 | LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX; |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 330 | goto bad_config; |
| 331 | } |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 332 | if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) || |
| 333 | (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) { |
| 334 | LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match"; |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 335 | goto bad_config; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 336 | } |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 337 | for (i = 0; i < trx->cfg.num_chans; i++) { |
| 338 | trx->cfg.chans[i].trx = trx; |
| 339 | trx->cfg.chans[i].idx = i; |
| 340 | if (tx_paths_set) |
| 341 | osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str()); |
| 342 | if (rx_paths_set) |
| 343 | osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str()); |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 346 | return; |
| 347 | |
| 348 | bad_config: |
| 349 | print_help(); |
| 350 | exit(0); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 353 | int trx_validate_config(struct trx_ctx *trx) |
| 354 | { |
| 355 | if (trx->cfg.multi_arfcn && trx->cfg.num_chans > 5) { |
| 356 | LOG(ERROR) << "Unsupported number of channels"; |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | /* Force 4 SPS for EDGE or multi-ARFCN configurations */ |
| 361 | if ((trx->cfg.egprs || trx->cfg.multi_arfcn) && |
| 362 | (trx->cfg.tx_sps!=4 || trx->cfg.tx_sps!=4)) { |
| 363 | LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config."; |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int set_sched_rr(unsigned int prio) |
Harald Welte | 81486e0 | 2017-06-29 15:35:22 +0200 | [diff] [blame] | 371 | { |
| 372 | struct sched_param param; |
| 373 | int rc; |
| 374 | memset(¶m, 0, sizeof(param)); |
| 375 | param.sched_priority = prio; |
| 376 | printf("Setting SCHED_RR priority(%d)\n", param.sched_priority); |
| 377 | rc = sched_setscheduler(getpid(), SCHED_RR, ¶m); |
| 378 | if (rc != 0) { |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 379 | LOG(ERROR) << "Config: Setting SCHED_RR failed"; |
Harald Welte | 81486e0 | 2017-06-29 15:35:22 +0200 | [diff] [blame] | 380 | return -1; |
| 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 385 | static void print_config(struct trx_ctx *trx) |
| 386 | { |
| 387 | unsigned int i; |
| 388 | std::ostringstream ost(""); |
| 389 | |
| 390 | ost << "Config Settings" << std::endl; |
Pau Espin Pedrol | 8dffadb | 2018-03-06 18:38:22 +0100 | [diff] [blame] | 391 | ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl; |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 392 | ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl; |
| 393 | ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl; |
| 394 | ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl; |
Harald Welte | fad2e09 | 2018-04-28 21:25:09 +0200 | [diff] [blame] | 395 | ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl; |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 396 | ost << " Channels................ " << trx->cfg.num_chans << std::endl; |
| 397 | ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl; |
| 398 | ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl; |
| 399 | ost << " EDGE support............ " << trx->cfg.egprs << std::endl; |
| 400 | ost << " Reference............... " << trx->cfg.clock_ref << std::endl; |
| 401 | ost << " C0 Filler Table......... " << trx->cfg.filler << std::endl; |
| 402 | ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl; |
| 403 | ost << " Tuning offset........... " << trx->cfg.offset << std::endl; |
| 404 | ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl; |
| 405 | ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl; |
| 406 | ost << " Tx Antennas............."; |
| 407 | for (i = 0; i < trx->cfg.num_chans; i++) { |
| 408 | std::string p = charp2str(trx->cfg.chans[i].tx_path); |
| 409 | ost << " '" << ((p != "") ? p : "<default>") << "'"; |
| 410 | } |
| 411 | ost << std::endl; |
| 412 | ost << " Rx Antennas............."; |
| 413 | for (i = 0; i < trx->cfg.num_chans; i++) { |
| 414 | std::string p = charp2str(trx->cfg.chans[i].rx_path); |
| 415 | ost << " '" << ((p != "") ? p : "<default>") << "'"; |
| 416 | } |
| 417 | ost << std::endl; |
| 418 | |
| 419 | std::cout << ost << std::endl; |
| 420 | } |
| 421 | |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 422 | static void trx_stop() |
| 423 | { |
| 424 | std::cout << "Shutting down transceiver..." << std::endl; |
| 425 | |
| 426 | delete transceiver; |
| 427 | delete radio; |
| 428 | delete usrp; |
| 429 | } |
| 430 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 431 | static int trx_start(struct trx_ctx *trx) |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 432 | { |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 433 | int type, chans; |
| 434 | unsigned int i; |
| 435 | std::vector<std::string> rx_paths, tx_paths; |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 436 | RadioDevice::InterfaceType iface = RadioDevice::NORMAL; |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 437 | |
| 438 | /* Create the low level device object */ |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 439 | if (trx->cfg.multi_arfcn) |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 440 | iface = RadioDevice::MULTI_ARFCN; |
| 441 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 442 | /* Generate vector of rx/tx_path: */ |
| 443 | for (i = 0; i < trx->cfg.num_chans; i++) { |
| 444 | rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path)); |
| 445 | tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path)); |
| 446 | } |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 447 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 448 | usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface, |
| 449 | trx->cfg.num_chans, trx->cfg.offset, |
| 450 | tx_paths, rx_paths); |
| 451 | type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels); |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 452 | if (type < 0) { |
| 453 | LOG(ALERT) << "Failed to create radio device" << std::endl; |
| 454 | goto shutdown; |
| 455 | } |
| 456 | |
| 457 | /* Setup the appropriate device interface */ |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 458 | radio = makeRadioInterface(trx, usrp, type); |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 459 | if (!radio) |
| 460 | goto shutdown; |
| 461 | |
| 462 | /* Create the transceiver core */ |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 463 | if (makeTransceiver(trx, radio) < 0) |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 464 | goto shutdown; |
| 465 | |
| 466 | chans = transceiver->numChans(); |
| 467 | std::cout << "-- Transceiver active with " |
| 468 | << chans << " channel(s)" << std::endl; |
| 469 | |
| 470 | return 0; |
| 471 | |
| 472 | shutdown: |
| 473 | trx_stop(); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | int main(int argc, char *argv[]) |
| 478 | { |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 479 | int rc; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 480 | |
Pau Espin Pedrol | 3a3b220 | 2018-02-21 20:15:47 +0100 | [diff] [blame] | 481 | tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX"); |
| 482 | msgb_talloc_ctx_init(tall_trx_ctx, 0); |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 483 | g_vty_info.tall_ctx = tall_trx_ctx; |
| 484 | |
Pau Espin Pedrol | ab22f4c | 2018-02-21 20:15:18 +0100 | [diff] [blame] | 485 | setup_signal_handlers(); |
| 486 | |
Pau Espin Pedrol | a3ab8c2 | 2018-02-21 15:41:03 +0100 | [diff] [blame] | 487 | g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx); |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 488 | |
Philipp Maier | e51a8f0 | 2017-03-16 12:09:34 +0100 | [diff] [blame] | 489 | #ifdef HAVE_SSE3 |
| 490 | printf("Info: SSE3 support compiled in"); |
Vadim Yanitskiy | 3bd763d | 2017-05-20 01:46:51 +0300 | [diff] [blame] | 491 | #ifdef HAVE___BUILTIN_CPU_SUPPORTS |
Philipp Maier | e51a8f0 | 2017-03-16 12:09:34 +0100 | [diff] [blame] | 492 | if (__builtin_cpu_supports("sse3")) |
| 493 | printf(" and supported by CPU\n"); |
| 494 | else |
| 495 | printf(", but not supported by CPU\n"); |
Vadim Yanitskiy | 3bd763d | 2017-05-20 01:46:51 +0300 | [diff] [blame] | 496 | #else |
| 497 | printf(", but runtime SIMD detection disabled\n"); |
| 498 | #endif |
Philipp Maier | e51a8f0 | 2017-03-16 12:09:34 +0100 | [diff] [blame] | 499 | #endif |
| 500 | |
| 501 | #ifdef HAVE_SSE4_1 |
| 502 | printf("Info: SSE4.1 support compiled in"); |
Vadim Yanitskiy | 3bd763d | 2017-05-20 01:46:51 +0300 | [diff] [blame] | 503 | #ifdef HAVE___BUILTIN_CPU_SUPPORTS |
Philipp Maier | e51a8f0 | 2017-03-16 12:09:34 +0100 | [diff] [blame] | 504 | if (__builtin_cpu_supports("sse4.1")) |
| 505 | printf(" and supported by CPU\n"); |
| 506 | else |
| 507 | printf(", but not supported by CPU\n"); |
Vadim Yanitskiy | 3bd763d | 2017-05-20 01:46:51 +0300 | [diff] [blame] | 508 | #else |
| 509 | printf(", but runtime SIMD detection disabled\n"); |
| 510 | #endif |
Philipp Maier | e51a8f0 | 2017-03-16 12:09:34 +0100 | [diff] [blame] | 511 | #endif |
| 512 | |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 513 | convolve_init(); |
| 514 | convert_init(); |
| 515 | |
Pau Espin Pedrol | e1977fc | 2018-04-16 14:50:11 +0200 | [diff] [blame] | 516 | osmo_init_logging2(tall_trx_ctx, &log_info); |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 517 | osmo_stats_init(tall_trx_ctx); |
| 518 | vty_init(&g_vty_info); |
| 519 | ctrl_vty_init(tall_trx_ctx); |
| 520 | trx_vty_init(g_trx_ctx); |
| 521 | |
| 522 | logging_vty_add_cmds(); |
| 523 | osmo_talloc_vty_add_cmds(); |
| 524 | osmo_stats_vty_add_cmds(); |
| 525 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 526 | handle_options(argc, argv, g_trx_ctx); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 527 | |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 528 | rate_ctr_init(tall_trx_ctx); |
| 529 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 530 | rc = vty_read_config_file(config_file, NULL); |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 531 | if (rc < 0) { |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 532 | fprintf(stderr, "Failed to open config file: '%s'\n", config_file); |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 533 | exit(2); |
| 534 | } |
| 535 | |
| 536 | rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX); |
| 537 | if (rc < 0) |
| 538 | exit(1); |
| 539 | |
| 540 | g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL); |
| 541 | if (!g_ctrlh) { |
| 542 | fprintf(stderr, "Failed to create CTRL interface.\n"); |
| 543 | exit(1); |
| 544 | } |
| 545 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 546 | /* Backward compatibility: Hack to have 1 channel allocated by default. |
| 547 | * Can be Dropped once we * get rid of "-c" cmdline param */ |
| 548 | if (g_trx_ctx->cfg.num_chans == 0) { |
| 549 | g_trx_ctx->cfg.num_chans = 1; |
| 550 | g_trx_ctx->cfg.chans[0].trx = g_trx_ctx; |
| 551 | g_trx_ctx->cfg.chans[0].idx = 0; |
| 552 | LOG(ERROR) << "No explicit channel config found. Make sure you" \ |
| 553 | " configure channels in VTY config. Using 1 channel as default," \ |
| 554 | " but expect your config to break in the future."; |
Harald Welte | 81486e0 | 2017-06-29 15:35:22 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 557 | print_config(g_trx_ctx); |
| 558 | |
| 559 | if (trx_validate_config(g_trx_ctx) < 0) { |
| 560 | LOG(ERROR) << "Config failure - exiting"; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 561 | return EXIT_FAILURE; |
| 562 | } |
| 563 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 564 | if (g_trx_ctx->cfg.sched_rr) { |
| 565 | if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0) |
| 566 | return EXIT_FAILURE; |
| 567 | } |
| 568 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 569 | srandom(time(NULL)); |
| 570 | |
Pau Espin Pedrol | 408f250 | 2018-02-21 18:47:35 +0100 | [diff] [blame] | 571 | if(trx_start(g_trx_ctx) < 0) |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 572 | return EXIT_FAILURE; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 573 | |
| 574 | while (!gshutdown) |
Pau Espin Pedrol | 5ea1817 | 2018-02-20 16:48:15 +0100 | [diff] [blame] | 575 | osmo_select_main(0); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 576 | |
Pau Espin Pedrol | 0bbd892 | 2018-02-21 11:59:26 +0100 | [diff] [blame] | 577 | trx_stop(); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | } |