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 |
| 40 | * downsampled to 1 sps. Default to 4 sps for all cases except for |
| 41 | * ARM and non-SIMD enabled architectures. |
| 42 | */ |
| 43 | #if defined(HAVE_NEON) || !defined(HAVE_SSE3) |
| 44 | #define DEFAULT_SPS 1 |
| 45 | #else |
| 46 | #define DEFAULT_SPS 4 |
| 47 | #endif |
| 48 | |
| 49 | /* Default configuration parameters |
| 50 | * Note that these values are only used if the particular key does not |
| 51 | * exist in the configuration database. IP port and address values will |
| 52 | * typically be overwritten by the OpenBTS.db values. Other values will |
| 53 | * not be in the database by default. |
| 54 | */ |
| 55 | #define DEFAULT_TRX_PORT 5700 |
| 56 | #define DEFAULT_TRX_IP "127.0.0.1" |
| 57 | #define DEFAULT_EXTREF false |
| 58 | #define DEFAULT_DIVERSITY false |
| 59 | #define DEFAULT_CHANS 1 |
| 60 | |
| 61 | struct trx_config { |
| 62 | std::string log_level; |
| 63 | std::string addr; |
| 64 | std::string dev_args; |
| 65 | unsigned port; |
| 66 | unsigned sps; |
| 67 | unsigned chans; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 68 | unsigned rtsc; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 69 | bool extref; |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame^] | 70 | Transceiver::FillerType filler; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 71 | bool diversity; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 72 | double offset; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 75 | ConfigurationTable gConfig; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 76 | |
| 77 | volatile bool gshutdown = false; |
| 78 | |
| 79 | /* Run sanity check on configuration table |
| 80 | * The global table constructor cannot provide notification in the |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 81 | * event of failure. Make sure that we can access the database, |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 82 | * write to it, and that it contains the bare minimum required keys. |
| 83 | */ |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 84 | bool testConfig() |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 85 | { |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 86 | int val = 9999; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 87 | std::string test = "asldfkjsaldkf"; |
| 88 | const char *key = "Log.Level"; |
| 89 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 90 | /* Attempt to query */ |
| 91 | try { |
| 92 | gConfig.getStr(key); |
| 93 | } catch (...) { |
| 94 | std::cerr << std::endl; |
| 95 | std::cerr << "Config: Failed query required key " << key |
| 96 | << std::endl; |
| 97 | return false; |
| 98 | } |
| 99 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 100 | /* Attempt to set a test value in the global config */ |
| 101 | if (!gConfig.set(test, val)) { |
| 102 | std::cerr << std::endl; |
| 103 | std::cerr << "Config: Failed to set test key" << std::endl; |
| 104 | return false; |
| 105 | } else { |
| 106 | gConfig.remove(test); |
| 107 | } |
| 108 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 112 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 113 | /* Setup configuration values |
| 114 | * Don't query the existence of the Log.Level because it's a |
| 115 | * mandatory value. That is, if it doesn't exist, the configuration |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 116 | * table will crash or will have already crashed. Everything else we |
| 117 | * can survive without and use default values if the database entries |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 118 | * are empty. |
| 119 | */ |
| 120 | bool trx_setup_config(struct trx_config *config) |
| 121 | { |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 122 | std::string refstr, fillstr, divstr; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 123 | |
Thomas Tsou | 4de70be | 2013-11-17 18:54:52 -0500 | [diff] [blame] | 124 | if (!testConfig()) |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 125 | return false; |
| 126 | |
| 127 | if (config->log_level == "") |
| 128 | config->log_level = gConfig.getStr("Log.Level"); |
| 129 | |
| 130 | if (!config->port) { |
| 131 | if (gConfig.defines("TRX.Port")) |
| 132 | config->port = gConfig.getNum("TRX.Port"); |
| 133 | else |
| 134 | config->port = DEFAULT_TRX_PORT; |
| 135 | } |
| 136 | |
| 137 | if (config->addr == "") { |
| 138 | if (gConfig.defines("TRX.IP")) |
| 139 | config->addr = gConfig.getStr("TRX.IP"); |
| 140 | else |
| 141 | config->addr = DEFAULT_TRX_IP; |
| 142 | } |
| 143 | |
| 144 | if (!config->extref) { |
| 145 | if (gConfig.defines("TRX.Reference")) |
| 146 | config->extref = gConfig.getNum("TRX.Reference"); |
| 147 | else |
| 148 | config->extref = DEFAULT_EXTREF; |
| 149 | } |
| 150 | |
| 151 | if (!config->diversity) { |
| 152 | if (gConfig.defines("TRX.Diversity")) |
| 153 | config->diversity = gConfig.getNum("TRX.Diversity"); |
| 154 | else |
| 155 | config->diversity = DEFAULT_DIVERSITY; |
| 156 | } |
| 157 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 158 | /* Diversity only supported on 2 channels */ |
| 159 | if (config->diversity) |
| 160 | config->chans = 2; |
| 161 | |
| 162 | refstr = config->extref ? "Enabled" : "Disabled"; |
| 163 | divstr = config->diversity ? "Enabled" : "Disabled"; |
Alexander Chemeris | f5fd578 | 2015-05-24 18:56:51 -0400 | [diff] [blame^] | 164 | switch (config->filler) { |
| 165 | case Transceiver::FILLER_DUMMY: |
| 166 | fillstr = "Dummy bursts"; |
| 167 | break; |
| 168 | case Transceiver::FILLER_ZERO: |
| 169 | fillstr = "Disabled"; |
| 170 | break; |
| 171 | case Transceiver::FILLER_RAND: |
| 172 | fillstr = "Normal busrts with random payload"; |
| 173 | break; |
| 174 | } |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 175 | |
| 176 | std::ostringstream ost(""); |
| 177 | ost << "Config Settings" << std::endl; |
| 178 | ost << " Log Level............... " << config->log_level << std::endl; |
| 179 | ost << " Device args............. " << config->dev_args << std::endl; |
| 180 | ost << " TRX Base Port........... " << config->port << std::endl; |
| 181 | ost << " TRX Address............. " << config->addr << std::endl; |
| 182 | ost << " Channels................ " << config->chans << std::endl; |
| 183 | ost << " Samples-per-Symbol...... " << config->sps << std::endl; |
| 184 | ost << " External Reference...... " << refstr << std::endl; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 185 | ost << " C0 Filler Table......... " << fillstr << std::endl; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 186 | ost << " Diversity............... " << divstr << std::endl; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 187 | ost << " Tuning offset........... " << config->offset << std::endl; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 188 | std::cout << ost << std::endl; |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /* Create radio interface |
| 194 | * The interface consists of sample rate changes, frequency shifts, |
| 195 | * channel multiplexing, and other conversions. The transceiver core |
| 196 | * accepts input vectors sampled at multiples of the GSM symbol rate. |
| 197 | * The radio interface connects the main transceiver with the device |
| 198 | * object, which may be operating some other rate. |
| 199 | */ |
| 200 | RadioInterface *makeRadioInterface(struct trx_config *config, |
| 201 | RadioDevice *usrp, int type) |
| 202 | { |
| 203 | RadioInterface *radio = NULL; |
| 204 | |
| 205 | switch (type) { |
| 206 | case RadioDevice::NORMAL: |
| 207 | radio = new RadioInterface(usrp, config->sps, config->chans); |
| 208 | break; |
| 209 | case RadioDevice::RESAMP_64M: |
| 210 | case RadioDevice::RESAMP_100M: |
| 211 | radio = new RadioInterfaceResamp(usrp, |
| 212 | config->sps, config->chans); |
| 213 | break; |
| 214 | case RadioDevice::DIVERSITY: |
| 215 | radio = new RadioInterfaceDiversity(usrp, |
| 216 | config->sps, config->chans); |
| 217 | break; |
| 218 | default: |
| 219 | LOG(ALERT) << "Unsupported radio interface configuration"; |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | if (!radio->init(type)) { |
| 224 | LOG(ALERT) << "Failed to initialize radio interface"; |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | return radio; |
| 229 | } |
| 230 | |
| 231 | /* Create transceiver core |
| 232 | * The multi-threaded modem core operates at multiples of the GSM rate of |
| 233 | * 270.8333 ksps and consists of GSM specific modulation, demodulation, |
| 234 | * and decoding schemes. Also included are the socket interfaces for |
| 235 | * connecting to the upper layer stack. |
| 236 | */ |
| 237 | Transceiver *makeTransceiver(struct trx_config *config, RadioInterface *radio) |
| 238 | { |
| 239 | Transceiver *trx; |
| 240 | VectorFIFO *fifo; |
| 241 | |
| 242 | trx = new Transceiver(config->port, config->addr.c_str(), config->sps, |
| 243 | config->chans, GSM::Time(3,0), radio); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 244 | if (!trx->init(config->filler, config->rtsc)) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 245 | LOG(ALERT) << "Failed to initialize transceiver"; |
| 246 | delete trx; |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | for (size_t i = 0; i < config->chans; i++) { |
| 251 | fifo = radio->receiveFIFO(i); |
| 252 | if (fifo && trx->receiveFIFO(fifo, i)) |
| 253 | continue; |
| 254 | |
| 255 | LOG(ALERT) << "Could not attach FIFO to channel " << i; |
| 256 | delete trx; |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | return trx; |
| 261 | } |
| 262 | |
| 263 | static void sig_handler(int signo) |
| 264 | { |
| 265 | fprintf(stdout, "Received shutdown signal"); |
| 266 | gshutdown = true; |
| 267 | } |
| 268 | |
| 269 | static void setup_signal_handlers() |
| 270 | { |
| 271 | if (signal(SIGINT, sig_handler) == SIG_ERR) { |
| 272 | fprintf(stderr, "Failed to install SIGINT signal handler\n"); |
| 273 | exit(EXIT_FAILURE); |
| 274 | } |
| 275 | if (signal(SIGTERM, sig_handler) == SIG_ERR) { |
| 276 | fprintf(stderr, "Couldn't install SIGTERM signal handler\n"); |
| 277 | exit( EXIT_FAILURE); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static void print_help() |
| 282 | { |
| 283 | fprintf(stdout, "Options:\n" |
| 284 | " -h This text\n" |
| 285 | " -a UHD device args\n" |
| 286 | " -l Logging level (%s)\n" |
| 287 | " -i IP address of GSM core\n" |
| 288 | " -p Base port number\n" |
| 289 | " -d Enable dual channel diversity receiver\n" |
| 290 | " -x Enable external 10 MHz reference\n" |
| 291 | " -s Samples-per-symbol (1 or 4)\n" |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 292 | " -c Number of ARFCN channels (default=1)\n" |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 293 | " -f Enable C0 filler table\n" |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 294 | " -o Set baseband frequency offset (default=auto)\n" |
| 295 | " -r Random burst test mode with TSC\n", |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 296 | "EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG"); |
| 297 | } |
| 298 | |
| 299 | static void handle_options(int argc, char **argv, struct trx_config *config) |
| 300 | { |
| 301 | int option; |
| 302 | |
| 303 | config->port = 0; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 304 | config->sps = DEFAULT_SPS; |
| 305 | config->chans = DEFAULT_CHANS; |
| 306 | config->rtsc = 0; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 307 | config->extref = false; |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 308 | config->filler = Transceiver::FILLER_ZERO; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 309 | config->diversity = false; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 310 | config->offset = 0.0; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 311 | |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 312 | while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:")) != -1) { |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 313 | switch (option) { |
| 314 | case 'h': |
| 315 | print_help(); |
| 316 | exit(0); |
| 317 | break; |
| 318 | case 'a': |
| 319 | config->dev_args = optarg; |
| 320 | break; |
| 321 | case 'l': |
| 322 | config->log_level = optarg; |
| 323 | break; |
| 324 | case 'i': |
| 325 | config->addr = optarg; |
| 326 | break; |
| 327 | case 'p': |
| 328 | config->port = atoi(optarg); |
| 329 | break; |
| 330 | case 'c': |
| 331 | config->chans = atoi(optarg); |
| 332 | break; |
| 333 | case 'd': |
| 334 | config->diversity = true; |
| 335 | break; |
| 336 | case 'x': |
| 337 | config->extref = true; |
| 338 | break; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 339 | case 'f': |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 340 | config->filler = Transceiver::FILLER_DUMMY; |
Thomas Tsou | 15d743e | 2014-01-25 02:34:03 -0500 | [diff] [blame] | 341 | break; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 342 | case 'o': |
| 343 | config->offset = atof(optarg); |
| 344 | break; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 345 | case 's': |
| 346 | config->sps = atoi(optarg); |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 347 | break; |
| 348 | case 'r': |
| 349 | config->rtsc = atoi(optarg); |
| 350 | config->filler = Transceiver::FILLER_RAND; |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 351 | break; |
| 352 | default: |
| 353 | print_help(); |
| 354 | exit(0); |
| 355 | } |
| 356 | } |
Tom Tsou | 64ad712 | 2015-05-19 18:26:31 -0700 | [diff] [blame] | 357 | |
| 358 | if ((config->sps != 1) && (config->sps != 4)) { |
| 359 | printf("Unsupported samples-per-symbol %i\n\n", config->sps); |
| 360 | print_help(); |
| 361 | exit(0); |
| 362 | } |
| 363 | |
| 364 | if (config->rtsc > 7) { |
| 365 | printf("Invalid training sequence %i\n\n", config->rtsc); |
| 366 | print_help(); |
| 367 | exit(0); |
| 368 | } |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | int main(int argc, char *argv[]) |
| 372 | { |
| 373 | int type, chans; |
| 374 | RadioDevice *usrp; |
| 375 | RadioInterface *radio = NULL; |
| 376 | Transceiver *trx = NULL; |
| 377 | struct trx_config config; |
| 378 | |
| 379 | handle_options(argc, argv, &config); |
| 380 | |
| 381 | setup_signal_handlers(); |
| 382 | |
| 383 | /* Check database sanity */ |
| 384 | if (!trx_setup_config(&config)) { |
| 385 | std::cerr << "Config: Database failure - exiting" << std::endl; |
| 386 | return EXIT_FAILURE; |
| 387 | } |
| 388 | |
| 389 | gLogInit("transceiver", config.log_level.c_str(), LOG_LOCAL7); |
| 390 | |
| 391 | srandom(time(NULL)); |
| 392 | |
| 393 | /* Create the low level device object */ |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 394 | usrp = RadioDevice::make(config.sps, config.chans, |
| 395 | config.diversity, config.offset); |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 396 | type = usrp->open(config.dev_args, config.extref); |
| 397 | if (type < 0) { |
| 398 | LOG(ALERT) << "Failed to create radio device" << std::endl; |
| 399 | goto shutdown; |
| 400 | } |
| 401 | |
| 402 | /* Setup the appropriate device interface */ |
| 403 | radio = makeRadioInterface(&config, usrp, type); |
| 404 | if (!radio) |
| 405 | goto shutdown; |
| 406 | |
| 407 | /* Create the transceiver core */ |
| 408 | trx = makeTransceiver(&config, radio); |
| 409 | if (!trx) |
| 410 | goto shutdown; |
| 411 | |
Thomas Tsou | 85b179d | 2013-11-15 21:14:33 -0500 | [diff] [blame] | 412 | chans = trx->numChans(); |
| 413 | std::cout << "-- Transceiver active with " |
| 414 | << chans << " channel(s)" << std::endl; |
| 415 | |
| 416 | while (!gshutdown) |
| 417 | sleep(1); |
| 418 | |
| 419 | shutdown: |
| 420 | std::cout << "Shutting down transceiver..." << std::endl; |
| 421 | |
| 422 | delete trx; |
| 423 | delete radio; |
| 424 | delete usrp; |
| 425 | |
| 426 | return 0; |
| 427 | } |