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> |
| 30 | |
| 31 | #include <GSMCommon.h> |
| 32 | #include <Logger.h> |
| 33 | #include <Configuration.h> |
| 34 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 35 | /* Samples-per-symbol for downlink path |
| 36 | * 4 - Uses precision modulator (more computation, less distortion) |
| 37 | * 1 - Uses minimized modulator (less computation, more distortion) |
| 38 | * |
| 39 | * Other values are invalid. Receive path (uplink) is always |
Tom Tsou | 0fe41a5 | 2016-05-03 15:14:45 -0700 | [diff] [blame] | 40 | * downsampled to 1 sps. Default to 4 sps for all cases. |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 41 | */ |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 42 | #define DEFAULT_TX_SPS 4 |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 43 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 44 | /* |
| 45 | * Samples-per-symbol for uplink (receiver) path |
| 46 | * Do not modify this value. EDGE configures 4 sps automatically on |
| 47 | * B200/B210 devices only. Use of 4 sps on the receive path for other |
| 48 | * configurations is not supported. |
| 49 | */ |
| 50 | #define DEFAULT_RX_SPS 1 |
| 51 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 52 | /* Default configuration parameters |
| 53 | * Note that these values are only used if the particular key does not |
| 54 | * exist in the configuration database. IP port and address values will |
| 55 | * typically be overwritten by the OpenBTS.db values. Other values will |
| 56 | * not be in the database by default. |
| 57 | */ |
| 58 | #define DEFAULT_TRX_PORT 5700 |
| 59 | #define DEFAULT_TRX_IP "127.0.0.1" |
| 60 | #define DEFAULT_EXTREF false |
| 61 | #define DEFAULT_DIVERSITY false |
| 62 | #define DEFAULT_CHANS 1 |
| 63 | |
| 64 | struct trx_config { |
| 65 | std::string log_level; |
| 66 | std::string addr; |
| 67 | std::string dev_args; |
| 68 | unsigned port; |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 69 | unsigned tx_sps; |
| 70 | unsigned rx_sps; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 71 | unsigned chans; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 72 | unsigned rtsc; |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 73 | unsigned rach_delay; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 74 | bool extref; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 75 | bool gpsref; |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame] | 76 | Transceiver::FillerType filler; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 77 | bool diversity; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 78 | bool mcbts; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 79 | double offset; |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 80 | double rssi_offset; |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 81 | bool swap_channels; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 82 | bool edge; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 85 | ConfigurationTable gConfig; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 86 | |
| 87 | volatile bool gshutdown = false; |
| 88 | |
| 89 | /* Run sanity check on configuration table |
| 90 | * The global table constructor cannot provide notification in the |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 91 | * event of failure. Make sure that we can access the database, |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 92 | * write to it, and that it contains the bare minimum required keys. |
| 93 | */ |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 94 | bool testConfig() |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 95 | { |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 96 | int val = 9999; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 97 | std::string test = "asldfkjsaldkf"; |
| 98 | const char *key = "Log.Level"; |
| 99 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 100 | /* Attempt to query */ |
| 101 | try { |
| 102 | gConfig.getStr(key); |
| 103 | } catch (...) { |
| 104 | std::cerr << std::endl; |
| 105 | std::cerr << "Config: Failed query required key " << key |
| 106 | << std::endl; |
| 107 | return false; |
| 108 | } |
| 109 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 110 | /* Attempt to set a test value in the global config */ |
| 111 | if (!gConfig.set(test, val)) { |
| 112 | std::cerr << std::endl; |
| 113 | std::cerr << "Config: Failed to set test key" << std::endl; |
| 114 | return false; |
| 115 | } else { |
| 116 | gConfig.remove(test); |
| 117 | } |
| 118 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 122 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 123 | /* Setup configuration values |
| 124 | * Don't query the existence of the Log.Level because it's a |
| 125 | * mandatory value. That is, if it doesn't exist, the configuration |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 126 | * table will crash or will have already crashed. Everything else we |
| 127 | * can survive without and use default values if the database entries |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 128 | * are empty. |
| 129 | */ |
| 130 | bool trx_setup_config(struct trx_config *config) |
| 131 | { |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 132 | std::string refstr, fillstr, divstr, mcstr, edgestr; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 133 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 134 | if (!testConfig()) |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 135 | return false; |
| 136 | |
| 137 | if (config->log_level == "") |
| 138 | config->log_level = gConfig.getStr("Log.Level"); |
| 139 | |
| 140 | if (!config->port) { |
| 141 | if (gConfig.defines("TRX.Port")) |
| 142 | config->port = gConfig.getNum("TRX.Port"); |
| 143 | else |
| 144 | config->port = DEFAULT_TRX_PORT; |
| 145 | } |
| 146 | |
| 147 | if (config->addr == "") { |
| 148 | if (gConfig.defines("TRX.IP")) |
| 149 | config->addr = gConfig.getStr("TRX.IP"); |
| 150 | else |
| 151 | config->addr = DEFAULT_TRX_IP; |
| 152 | } |
| 153 | |
| 154 | if (!config->extref) { |
| 155 | if (gConfig.defines("TRX.Reference")) |
| 156 | config->extref = gConfig.getNum("TRX.Reference"); |
| 157 | else |
| 158 | config->extref = DEFAULT_EXTREF; |
| 159 | } |
| 160 | |
| 161 | if (!config->diversity) { |
| 162 | if (gConfig.defines("TRX.Diversity")) |
| 163 | config->diversity = gConfig.getNum("TRX.Diversity"); |
| 164 | else |
| 165 | config->diversity = DEFAULT_DIVERSITY; |
| 166 | } |
| 167 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 168 | if (!config->chans) |
| 169 | config->chans = DEFAULT_CHANS; |
| 170 | |
Tom Tsou | 6031734 | 2017-03-30 19:36:41 -0700 | [diff] [blame^] | 171 | if (config->mcbts && config->chans > 5) { |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 172 | std::cout << "Unsupported number of channels" << std::endl; |
| 173 | return false; |
| 174 | } |
| 175 | |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 176 | edgestr = config->edge ? "Enabled" : "Disabled"; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 177 | divstr = config->diversity ? "Enabled" : "Disabled"; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 178 | mcstr = config->mcbts ? "Enabled" : "Disabled"; |
| 179 | |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 180 | if (config->extref) |
| 181 | refstr = "External"; |
| 182 | else if (config->gpsref) |
| 183 | refstr = "GPS"; |
| 184 | else |
| 185 | refstr = "Internal"; |
| 186 | |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame] | 187 | switch (config->filler) { |
| 188 | case Transceiver::FILLER_DUMMY: |
| 189 | fillstr = "Dummy bursts"; |
| 190 | break; |
| 191 | case Transceiver::FILLER_ZERO: |
| 192 | fillstr = "Disabled"; |
| 193 | break; |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame] | 194 | case Transceiver::FILLER_NORM_RAND: |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame] | 195 | fillstr = "Normal busrts with random payload"; |
| 196 | break; |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame] | 197 | case Transceiver::FILLER_EDGE_RAND: |
| 198 | fillstr = "EDGE busrts with random payload"; |
| 199 | break; |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 200 | case Transceiver::FILLER_ACCESS_RAND: |
| 201 | fillstr = "Access busrts with random payload"; |
| 202 | break; |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame] | 203 | } |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 204 | |
| 205 | std::ostringstream ost(""); |
| 206 | ost << "Config Settings" << std::endl; |
| 207 | ost << " Log Level............... " << config->log_level << std::endl; |
| 208 | ost << " Device args............. " << config->dev_args << std::endl; |
| 209 | ost << " TRX Base Port........... " << config->port << std::endl; |
| 210 | ost << " TRX Address............. " << config->addr << std::endl; |
| 211 | ost << " Channels................ " << config->chans << std::endl; |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 212 | ost << " Tx Samples-per-Symbol... " << config->tx_sps << std::endl; |
Alexander Chemeris | 1ab5e7f | 2016-04-20 08:44:55 +0300 | [diff] [blame] | 213 | ost << " Rx Samples-per-Symbol... " << config->rx_sps << std::endl; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 214 | ost << " EDGE support............ " << edgestr << std::endl; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 215 | ost << " Reference............... " << refstr << std::endl; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 216 | ost << " C0 Filler Table......... " << fillstr << std::endl; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 217 | ost << " Multi-Carrier........... " << mcstr << std::endl; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 218 | ost << " Diversity............... " << divstr << std::endl; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 219 | ost << " Tuning offset........... " << config->offset << std::endl; |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 220 | ost << " RSSI to dBm offset...... " << config->rssi_offset << std::endl; |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 221 | ost << " Swap channels........... " << config->swap_channels << std::endl; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 222 | std::cout << ost << std::endl; |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /* Create radio interface |
| 228 | * The interface consists of sample rate changes, frequency shifts, |
| 229 | * channel multiplexing, and other conversions. The transceiver core |
| 230 | * accepts input vectors sampled at multiples of the GSM symbol rate. |
| 231 | * The radio interface connects the main transceiver with the device |
| 232 | * object, which may be operating some other rate. |
| 233 | */ |
| 234 | RadioInterface *makeRadioInterface(struct trx_config *config, |
| 235 | RadioDevice *usrp, int type) |
| 236 | { |
| 237 | RadioInterface *radio = NULL; |
| 238 | |
| 239 | switch (type) { |
| 240 | case RadioDevice::NORMAL: |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 241 | radio = new RadioInterface(usrp, config->tx_sps, |
| 242 | config->rx_sps, config->chans); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 243 | break; |
| 244 | case RadioDevice::RESAMP_64M: |
| 245 | case RadioDevice::RESAMP_100M: |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 246 | radio = new RadioInterfaceResamp(usrp, config->tx_sps, |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 247 | config->rx_sps); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 248 | break; |
| 249 | case RadioDevice::DIVERSITY: |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 250 | |
| 251 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 252 | radio = new RadioInterfaceDiversity(usrp, config->tx_sps, |
| 253 | config->chans); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 254 | break; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 255 | case RadioDevice::MULTI_ARFCN: |
| 256 | radio = new RadioInterfaceMulti(usrp, config->tx_sps, |
| 257 | config->rx_sps, config->chans); |
| 258 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 259 | default: |
| 260 | LOG(ALERT) << "Unsupported radio interface configuration"; |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | if (!radio->init(type)) { |
| 265 | LOG(ALERT) << "Failed to initialize radio interface"; |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | return radio; |
| 270 | } |
| 271 | |
| 272 | /* Create transceiver core |
| 273 | * The multi-threaded modem core operates at multiples of the GSM rate of |
| 274 | * 270.8333 ksps and consists of GSM specific modulation, demodulation, |
| 275 | * and decoding schemes. Also included are the socket interfaces for |
| 276 | * connecting to the upper layer stack. |
| 277 | */ |
| 278 | Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio) |
| 279 | { |
| 280 | Transceiver *trx; |
| 281 | VectorFIFO *fifo; |
| 282 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 283 | trx = new Transceiver(config->port, config->addr.c_str(), |
| 284 | config->tx_sps, config->rx_sps, config->chans, |
| 285 | GSM::Time(3,0), radio, config->rssi_offset); |
Tom Tsou | 64464e6 | 2016-07-01 03:46:46 -0700 | [diff] [blame] | 286 | if (!trx->init(config->filler, config->rtsc, |
| 287 | config->rach_delay, config->edge)) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 288 | LOG(ALERT) << "Failed to initialize transceiver"; |
| 289 | delete trx; |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | for (size_t i = 0; i < config->chans; i++) { |
| 294 | fifo = radio->receiveFIFO(i); |
| 295 | if (fifo && trx->receiveFIFO(fifo, i)) |
| 296 | continue; |
| 297 | |
| 298 | LOG(ALERT) << "Could not attach FIFO to channel " << i; |
| 299 | delete trx; |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | return trx; |
| 304 | } |
| 305 | |
| 306 | static void sig_handler(int signo) |
| 307 | { |
| 308 | fprintf(stdout, "Received shutdown signal"); |
| 309 | gshutdown = true; |
| 310 | } |
| 311 | |
| 312 | static void setup_signal_handlers() |
| 313 | { |
| 314 | if (signal(SIGINT, sig_handler) == SIG_ERR) { |
| 315 | fprintf(stderr, "Failed to install SIGINT signal handler\n"); |
| 316 | exit(EXIT_FAILURE); |
| 317 | } |
| 318 | if (signal(SIGTERM, sig_handler) == SIG_ERR) { |
| 319 | fprintf(stderr, "Couldn't install SIGTERM signal handler\n"); |
| 320 | exit( EXIT_FAILURE); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static void print_help() |
| 325 | { |
| 326 | fprintf(stdout, "Options:\n" |
| 327 | " -h This text\n" |
| 328 | " -a UHD device args\n" |
| 329 | " -l Logging level (%s)\n" |
| 330 | " -i IP address of GSM core\n" |
| 331 | " -p Base port number\n" |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 332 | " -e Enable EDGE receiver\n" |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 333 | " -d Enable dual channel diversity receiver (deprecated)\n" |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 334 | " -m Enable multi-ARFCN transceiver (default=disabled)\n" |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 335 | " -x Enable external 10 MHz reference\n" |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 336 | " -g Enable GPSDO reference\n" |
Tom Tsou | 2e4ed10 | 2016-06-27 15:39:16 -0700 | [diff] [blame] | 337 | " -s Tx samples-per-symbol (1 or 4)\n" |
| 338 | " -b Rx samples-per-symbol (1 or 4)\n" |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 339 | " -c Number of ARFCN channels (default=1)\n" |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 340 | " -f Enable C0 filler table\n" |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 341 | " -o Set baseband frequency offset (default=auto)\n" |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 342 | " -r Random Normal Burst test mode with TSC\n" |
| 343 | " -A Random Access Burst test mode with delay\n" |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 344 | " -R RSSI to dBm offset in dB (default=0)\n" |
| 345 | " -S Swap channels (UmTRX only)\n", |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 346 | "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG"); |
| 347 | } |
| 348 | |
| 349 | static void handle_options(int argc, char **argv, struct trx_config *config) |
| 350 | { |
| 351 | int option; |
| 352 | |
| 353 | config->port = 0; |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 354 | config->tx_sps = DEFAULT_TX_SPS; |
| 355 | config->rx_sps = DEFAULT_RX_SPS; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 356 | config->chans = DEFAULT_CHANS; |
| 357 | config->rtsc = 0; |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 358 | config->rach_delay = 0; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 359 | config->extref = false; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 360 | config->gpsref = false; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 361 | config->filler = Transceiver::FILLER_ZERO; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 362 | config->mcbts = false; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 363 | config->diversity = false; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 364 | config->offset = 0.0; |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 365 | config->rssi_offset = 0.0; |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 366 | config->swap_channels = false; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 367 | config->edge = false; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 368 | |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 369 | while ((option = getopt(argc, argv, "ha:l:i:p:c:dmxgfo:s:b:r:A:R:Se")) != -1) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 370 | switch (option) { |
| 371 | case 'h': |
| 372 | print_help(); |
| 373 | exit(0); |
| 374 | break; |
| 375 | case 'a': |
| 376 | config->dev_args = optarg; |
| 377 | break; |
| 378 | case 'l': |
| 379 | config->log_level = optarg; |
| 380 | break; |
| 381 | case 'i': |
| 382 | config->addr = optarg; |
| 383 | break; |
| 384 | case 'p': |
| 385 | config->port = atoi(optarg); |
| 386 | break; |
| 387 | case 'c': |
| 388 | config->chans = atoi(optarg); |
| 389 | break; |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 390 | case 'm': |
| 391 | config->mcbts = true; |
| 392 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 393 | case 'd': |
| 394 | config->diversity = true; |
| 395 | break; |
| 396 | case 'x': |
| 397 | config->extref = true; |
| 398 | break; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 399 | case 'g': |
| 400 | config->gpsref = true; |
| 401 | break; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 402 | case 'f': |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 403 | config->filler = Transceiver::FILLER_DUMMY; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 404 | break; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 405 | case 'o': |
| 406 | config->offset = atof(optarg); |
| 407 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 408 | case 's': |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 409 | config->tx_sps = atoi(optarg); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 410 | break; |
Tom Tsou | 2e4ed10 | 2016-06-27 15:39:16 -0700 | [diff] [blame] | 411 | case 'b': |
| 412 | config->rx_sps = atoi(optarg); |
| 413 | break; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 414 | case 'r': |
| 415 | config->rtsc = atoi(optarg); |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame] | 416 | config->filler = Transceiver::FILLER_NORM_RAND; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 417 | break; |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 418 | case 'A': |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 419 | config->rach_delay = atoi(optarg); |
Alexander Chemeris | 5efe050 | 2016-03-23 17:06:32 +0300 | [diff] [blame] | 420 | config->filler = Transceiver::FILLER_ACCESS_RAND; |
| 421 | break; |
Alexander Chemeris | e8905a0 | 2015-06-03 23:47:56 -0400 | [diff] [blame] | 422 | case 'R': |
| 423 | config->rssi_offset = atof(optarg); |
| 424 | break; |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 425 | case 'S': |
| 426 | config->swap_channels = true; |
| 427 | break; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 428 | case 'e': |
| 429 | config->edge = true; |
Tom Tsou | b0aefcb | 2016-03-06 03:44:34 -0800 | [diff] [blame] | 430 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 431 | default: |
| 432 | print_help(); |
| 433 | exit(0); |
| 434 | } |
| 435 | } |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 436 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 437 | /* Force 4 SPS for EDGE or multi-ARFCN configurations */ |
| 438 | if ((config->edge) || (config->mcbts)) { |
Tom Tsou | 2e4ed10 | 2016-06-27 15:39:16 -0700 | [diff] [blame] | 439 | config->tx_sps = 4; |
| 440 | config->rx_sps = 4; |
| 441 | } |
| 442 | |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 443 | if (config->gpsref && config->extref) { |
| 444 | printf("External and GPSDO references unavailable at the same time\n\n"); |
| 445 | goto bad_config; |
| 446 | } |
| 447 | |
| 448 | /* Special restrictions on (deprecated) diversity configuration */ |
| 449 | if (config->diversity) { |
| 450 | if (config->mcbts || config->edge) { |
| 451 | std::cout << "Multi-carrier/EDGE diversity unsupported" << std::endl; |
| 452 | goto bad_config; |
| 453 | } |
| 454 | |
| 455 | if (config->rx_sps != 1) { |
| 456 | std::cout << "Diversity only supported with 1 SPS" << std::endl; |
| 457 | goto bad_config; |
| 458 | } |
| 459 | |
| 460 | if (config->chans != 2) { |
| 461 | std::cout << "Diversity only supported with 2 channels" << std::endl; |
| 462 | goto bad_config; |
| 463 | } |
| 464 | } |
| 465 | |
Tom Tsou | af717b2 | 2016-03-06 22:19:15 -0800 | [diff] [blame] | 466 | if (config->edge && (config->filler == Transceiver::FILLER_NORM_RAND)) |
| 467 | config->filler = Transceiver::FILLER_EDGE_RAND; |
| 468 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 469 | if ((config->tx_sps != 1) && (config->tx_sps != 4) && |
| 470 | (config->rx_sps != 1) && (config->rx_sps != 4)) { |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 471 | printf("Unsupported samples-per-symbol %i\n\n", config->tx_sps); |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 472 | goto bad_config; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (config->rtsc > 7) { |
| 476 | printf("Invalid training sequence %i\n\n", config->rtsc); |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 477 | goto bad_config; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 478 | } |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 479 | |
| 480 | if (config->rach_delay > 68) { |
| 481 | printf("RACH delay is too big %i\n\n", config->rach_delay); |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 482 | goto bad_config; |
Alexander Chemeris | 37c52c7 | 2016-03-25 18:28:34 +0300 | [diff] [blame] | 483 | } |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 484 | |
| 485 | return; |
| 486 | |
| 487 | bad_config: |
| 488 | print_help(); |
| 489 | exit(0); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | int main(int argc, char *argv[]) |
| 493 | { |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 494 | int type, chans, ref; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 495 | RadioDevice *usrp; |
| 496 | RadioInterface *radio = NULL; |
| 497 | Transceiver *trx = NULL; |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 498 | RadioDevice::InterfaceType iface = RadioDevice::NORMAL; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 499 | struct trx_config config; |
| 500 | |
| 501 | handle_options(argc, argv, &config); |
| 502 | |
| 503 | setup_signal_handlers(); |
| 504 | |
| 505 | /* Check database sanity */ |
| 506 | if (!trx_setup_config(&config)) { |
| 507 | std::cerr << "Config: Database failure - exiting" << std::endl; |
| 508 | return EXIT_FAILURE; |
| 509 | } |
| 510 | |
| 511 | gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7); |
| 512 | |
| 513 | srandom(time(NULL)); |
| 514 | |
| 515 | /* Create the low level device object */ |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 516 | if (config.mcbts) |
| 517 | iface = RadioDevice::MULTI_ARFCN; |
| 518 | |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 519 | if (config.extref) |
| 520 | ref = RadioDevice::REF_EXTERNAL; |
| 521 | else if (config.gpsref) |
| 522 | ref = RadioDevice::REF_GPS; |
| 523 | else |
| 524 | ref = RadioDevice::REF_INTERNAL; |
| 525 | |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 526 | usrp = RadioDevice::make(config.tx_sps, config.rx_sps, iface, |
| 527 | config.chans, config.offset); |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 528 | type = usrp->open(config.dev_args, ref, config.swap_channels); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 529 | if (type < 0) { |
| 530 | LOG(ALERT) << "Failed to create radio device" << std::endl; |
| 531 | goto shutdown; |
| 532 | } |
| 533 | |
| 534 | /* Setup the appropriate device interface */ |
| 535 | radio = makeRadioInterface(&config, usrp, type); |
| 536 | if (!radio) |
| 537 | goto shutdown; |
| 538 | |
| 539 | /* Create the transceiver core */ |
| 540 | trx = makeTransceiver(&config, radio); |
| 541 | if (!trx) |
| 542 | goto shutdown; |
| 543 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 544 | chans = trx->numChans(); |
| 545 | std::cout << "-- Transceiver active with " |
| 546 | << chans << " channel(s)" << std::endl; |
| 547 | |
| 548 | while (!gshutdown) |
| 549 | sleep(1); |
| 550 | |
| 551 | shutdown: |
| 552 | std::cout << "Shutting down transceiver..." << std::endl; |
| 553 | |
| 554 | delete trx; |
| 555 | delete radio; |
| 556 | delete usrp; |
| 557 | |
| 558 | return 0; |
| 559 | } |