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