Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 sysmocom - s.f.m.c. GmbH |
| 3 | * |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Affero General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program 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 |
| 12 | GNU Affero General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Affero General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include "Logger.h" |
| 22 | #include "Threads.h" |
| 23 | #include "LMSDevice.h" |
| 24 | |
| 25 | #include <lime/LimeSuite.h> |
| 26 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 27 | #include <osmocom/core/utils.h> |
| 28 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
| 33 | using namespace std; |
| 34 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 35 | constexpr double LMSDevice::masterClockRate; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 36 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 37 | #define MAX_ANTENNA_LIST_SIZE 10 |
| 38 | #define LMS_SAMPLE_RATE GSMRATE*32 |
| 39 | #define GSM_CARRIER_BW 270000.0 /* 270kHz */ |
| 40 | #define LMS_MIN_BW_SUPPORTED 2.5e6 /* 2.5mHz, minimum supported by LMS */ |
| 41 | #define LMS_CALIBRATE_BW_HZ OSMO_MAX(GSM_CARRIER_BW, LMS_MIN_BW_SUPPORTED) |
| 42 | |
| 43 | LMSDevice::LMSDevice(size_t sps, size_t chans): |
| 44 | m_lms_dev(NULL), sps(sps), chans(chans) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 45 | { |
| 46 | LOG(INFO) << "creating LMS device..."; |
| 47 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 48 | m_lms_stream_rx.resize(chans); |
| 49 | m_lms_stream_tx.resize(chans); |
| 50 | |
| 51 | m_last_rx_underruns.resize(chans, 0); |
| 52 | m_last_rx_overruns.resize(chans, 0); |
| 53 | m_last_tx_underruns.resize(chans, 0); |
| 54 | m_last_tx_overruns.resize(chans, 0); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void lms_log_callback(int lvl, const char *msg) |
| 58 | { |
| 59 | /* map lime specific log levels */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 60 | static const int lvl_map[5] = { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 61 | [0] = LOGL_FATAL, |
| 62 | [1] = LOGL_ERROR, |
| 63 | [2] = LOGL_NOTICE, |
| 64 | [3] = LOGL_INFO, |
| 65 | [4] = LOGL_DEBUG, |
| 66 | }; |
| 67 | /* protect against future higher log level values (lower importance) */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 68 | if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map)) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 69 | lvl = ARRAY_SIZE(lvl_map)-1; |
| 70 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 71 | LOGLV(DLMS, lvl) << msg; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 74 | static void thread_enable_cancel(bool cancel) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 75 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 76 | cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) : |
| 77 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
| 78 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 79 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 80 | int LMSDevice::open(const std::string &args, int ref, bool swap_channels) |
| 81 | { |
| 82 | //lms_info_str_t dev_str; |
| 83 | lms_info_str_t* info_list; |
| 84 | uint16_t dac_val; |
| 85 | unsigned int i, n; |
| 86 | int rc; |
| 87 | |
| 88 | LOG(INFO) << "Opening LMS device.."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 89 | |
| 90 | LMS_RegisterLogHandler(&lms_log_callback); |
| 91 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 92 | if ((n = LMS_GetDeviceList(NULL)) < 0) |
| 93 | LOG(ERROR) << "LMS_GetDeviceList(NULL) failed"; |
| 94 | LOG(DEBUG) << "Devices found: " << n; |
| 95 | if (n < 1) |
| 96 | return -1; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 97 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 98 | info_list = new lms_info_str_t[n]; |
| 99 | |
| 100 | if (LMS_GetDeviceList(info_list) < 0) //Populate device list |
| 101 | LOG(ERROR) << "LMS_GetDeviceList(info_list) failed"; |
| 102 | |
| 103 | for (i = 0; i < n; i++) //print device list |
| 104 | LOG(DEBUG) << "Device [" << i << "]: " << info_list[i]; |
| 105 | |
| 106 | rc = LMS_Open(&m_lms_dev, info_list[0], NULL); |
| 107 | if (rc != 0) { |
| 108 | LOG(ERROR) << "LMS_GetDeviceList() failed)"; |
| 109 | delete [] info_list; |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | delete [] info_list; |
| 114 | |
Harald Welte | 9cb4f27 | 2018-04-28 21:38:58 +0200 | [diff] [blame^] | 115 | LOG(INFO) << "Init LMS device"; |
| 116 | if (LMS_Init(m_lms_dev) != 0) { |
| 117 | LOG(ERROR) << "LMS_Init() failed"; |
| 118 | return -1; |
| 119 | } |
| 120 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 121 | LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps; |
| 122 | if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 123 | goto out_close; |
| 124 | /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 125 | ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 126 | |
| 127 | switch (ref) { |
| 128 | case REF_INTERNAL: |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 129 | LOG(DEBUG) << "Setting Internal clock reference"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 130 | /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */ |
| 131 | if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0) |
| 132 | goto out_close; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 133 | LOG(DEBUG) << "Setting VCTCXO to " << dac_val; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 134 | if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0) |
| 135 | goto out_close; |
| 136 | break; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 137 | case REF_EXTERNAL: |
| 138 | LOG(DEBUG) << "Setting Internal clock reference to " << 10000000.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 139 | /* Assume an external 10 MHz reference clock */ |
| 140 | if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0) |
| 141 | goto out_close; |
| 142 | break; |
| 143 | default: |
| 144 | LOG(ALERT) << "Invalid reference type"; |
| 145 | goto out_close; |
| 146 | } |
| 147 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 148 | /* Perform Rx and Tx calibration */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 149 | for (i=0; i<chans; i++) { |
| 150 | LOG(INFO) << "Calibrating chan " << i; |
| 151 | if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 152 | goto out_close; |
| 153 | if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 154 | goto out_close; |
| 155 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 156 | |
| 157 | samplesRead = 0; |
| 158 | samplesWritten = 0; |
| 159 | started = false; |
| 160 | |
| 161 | return NORMAL; |
| 162 | |
| 163 | out_close: |
| 164 | LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage(); |
| 165 | LMS_Close(m_lms_dev); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | bool LMSDevice::start() |
| 170 | { |
| 171 | LOG(INFO) << "starting LMS..."; |
| 172 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 173 | unsigned int i; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 174 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 175 | for (i=0; i<chans; i++) { |
| 176 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0) |
| 177 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 178 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 179 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0) |
| 180 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 181 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 182 | // Set gains to midpoint |
| 183 | setTxGain((minTxGain() + maxTxGain()) / 2, i); |
| 184 | setRxGain((minRxGain() + maxRxGain()) / 2, i); |
| 185 | |
| 186 | m_lms_stream_rx[i] = {}; |
| 187 | m_lms_stream_rx[i].isTx = false; |
| 188 | m_lms_stream_rx[i].channel = i; |
| 189 | m_lms_stream_rx[i].fifoSize = 1024 * 1024; |
| 190 | m_lms_stream_rx[i].throughputVsLatency = 0.3; |
| 191 | m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 192 | |
| 193 | m_lms_stream_tx[i] = {}; |
| 194 | m_lms_stream_tx[i].isTx = true; |
| 195 | m_lms_stream_tx[i].channel = i; |
| 196 | m_lms_stream_tx[i].fifoSize = 1024 * 1024; |
| 197 | m_lms_stream_tx[i].throughputVsLatency = 0.3; |
| 198 | m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 199 | |
| 200 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0) |
| 201 | return false; |
| 202 | |
| 203 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0) |
| 204 | return false; |
| 205 | |
| 206 | if (LMS_StartStream(&m_lms_stream_rx[i]) < 0) |
| 207 | return false; |
| 208 | |
| 209 | if (LMS_StartStream(&m_lms_stream_tx[i]) < 0) |
| 210 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 213 | flush_recv(10); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 214 | |
| 215 | started = true; |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | bool LMSDevice::stop() |
| 220 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 221 | unsigned int i; |
| 222 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 223 | if (!started) |
| 224 | return true; |
| 225 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 226 | for (i=0; i<chans; i++) { |
| 227 | LMS_StopStream(&m_lms_stream_tx[i]); |
| 228 | LMS_StopStream(&m_lms_stream_rx[i]); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 229 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 230 | LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false); |
| 231 | LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false); |
| 232 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | double LMSDevice::maxTxGain() |
| 238 | { |
| 239 | return 60.0; |
| 240 | } |
| 241 | |
| 242 | double LMSDevice::minTxGain() |
| 243 | { |
| 244 | return 0.0; |
| 245 | } |
| 246 | |
| 247 | double LMSDevice::maxRxGain() |
| 248 | { |
| 249 | return 70.0; |
| 250 | } |
| 251 | |
| 252 | double LMSDevice::minRxGain() |
| 253 | { |
| 254 | return 0.0; |
| 255 | } |
| 256 | |
| 257 | double LMSDevice::setTxGain(double dB, size_t chan) |
| 258 | { |
| 259 | if (chan) { |
| 260 | LOG(ALERT) << "Invalid channel " << chan; |
| 261 | return 0.0; |
| 262 | } |
| 263 | |
| 264 | if (dB > maxTxGain()) |
| 265 | dB = maxTxGain(); |
| 266 | if (dB < minTxGain()) |
| 267 | dB = minTxGain(); |
| 268 | |
| 269 | LOG(NOTICE) << "Setting TX gain to " << dB << " dB."; |
| 270 | |
| 271 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0) |
| 272 | LOG(ERR) << "Error setting TX gain"; |
| 273 | |
| 274 | return dB; |
| 275 | } |
| 276 | |
| 277 | double LMSDevice::setRxGain(double dB, size_t chan) |
| 278 | { |
| 279 | if (chan) { |
| 280 | LOG(ALERT) << "Invalid channel " << chan; |
| 281 | return 0.0; |
| 282 | } |
| 283 | |
| 284 | dB = 47.0; |
| 285 | |
| 286 | if (dB > maxRxGain()) |
| 287 | dB = maxRxGain(); |
| 288 | if (dB < minRxGain()) |
| 289 | dB = minRxGain(); |
| 290 | |
| 291 | LOG(NOTICE) << "Setting RX gain to " << dB << " dB."; |
| 292 | |
| 293 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0) |
| 294 | LOG(ERR) << "Error setting RX gain"; |
| 295 | |
| 296 | return dB; |
| 297 | } |
| 298 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 299 | int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 300 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 301 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 302 | const char* c_name = name.c_str(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 303 | int num_names; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 304 | int i; |
| 305 | |
| 306 | num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 307 | for (i = 0; i < num_names; i++) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 308 | if (!strcmp(c_name, name_list[i])) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 309 | return i; |
| 310 | } |
| 311 | return -1; |
| 312 | } |
| 313 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 314 | bool LMSDevice::flush_recv(size_t num_pkts) |
| 315 | { |
| 316 | #define CHUNK 625 |
| 317 | int len = CHUNK * sps; |
| 318 | short *buffer = new short[len * 2]; |
| 319 | int rc; |
| 320 | lms_stream_meta_t rx_metadata = {}; |
| 321 | rx_metadata.flushPartialPacket = false; |
| 322 | rx_metadata.waitForTimestamp = false; |
| 323 | |
| 324 | ts_initial = 0; |
| 325 | |
| 326 | while (!ts_initial || (num_pkts-- > 0)) { |
| 327 | rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100); |
| 328 | LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp; |
| 329 | if (rc != len) { |
| 330 | LOG(ALERT) << "LMS: Device receive timed out"; |
| 331 | delete[] buffer; |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | ts_initial = rx_metadata.timestamp; |
| 336 | } |
| 337 | |
| 338 | LOG(INFO) << "Initial timestamp " << ts_initial << std::endl; |
| 339 | delete[] buffer; |
| 340 | return true; |
| 341 | } |
| 342 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 343 | bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan) |
| 344 | { |
| 345 | int idx; |
| 346 | |
| 347 | if (chan >= rx_paths.size()) { |
| 348 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 349 | return false; |
| 350 | } |
| 351 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 352 | idx = get_ant_idx(ant, LMS_CH_RX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 353 | if (idx < 0) { |
| 354 | LOG(ALERT) << "Invalid Rx Antenna"; |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) { |
| 359 | LOG(ALERT) << "Unable to set Rx Antenna"; |
| 360 | } |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | std::string LMSDevice::getRxAntenna(size_t chan) |
| 366 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 367 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 368 | int idx; |
| 369 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 370 | if (chan >= rx_paths.size()) { |
| 371 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 372 | return ""; |
| 373 | } |
| 374 | |
| 375 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan); |
| 376 | if (idx < 0) { |
| 377 | LOG(ALERT) << "Error getting Rx Antenna"; |
| 378 | return ""; |
| 379 | } |
| 380 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 381 | if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 382 | LOG(ALERT) << "Error getting Rx Antenna List"; |
| 383 | return ""; |
| 384 | } |
| 385 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 386 | return name_list[idx]; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan) |
| 390 | { |
| 391 | int idx; |
| 392 | |
| 393 | if (chan >= tx_paths.size()) { |
| 394 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 395 | return false; |
| 396 | } |
| 397 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 398 | idx = get_ant_idx(ant, LMS_CH_TX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 399 | if (idx < 0) { |
| 400 | LOG(ALERT) << "Invalid Rx Antenna"; |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) { |
| 405 | LOG(ALERT) << "Unable to set Rx Antenna"; |
| 406 | } |
| 407 | |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | std::string LMSDevice::getTxAntenna(size_t chan) |
| 412 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 413 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 414 | int idx; |
| 415 | |
| 416 | if (chan >= tx_paths.size()) { |
| 417 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 418 | return ""; |
| 419 | } |
| 420 | |
| 421 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan); |
| 422 | if (idx < 0) { |
| 423 | LOG(ALERT) << "Error getting Tx Antenna"; |
| 424 | return ""; |
| 425 | } |
| 426 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 427 | if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 428 | LOG(ALERT) << "Error getting Tx Antenna List"; |
| 429 | return ""; |
| 430 | } |
| 431 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 432 | return name_list[idx]; |
| 433 | } |
| 434 | |
| 435 | bool LMSDevice::requiresRadioAlign() |
| 436 | { |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | GSM::Time LMSDevice::minLatency() { |
| 441 | /* Empirical data from a handful of |
| 442 | relatively recent machines shows that the B100 will underrun when |
| 443 | the transmit threshold is reduced to a time of 6 and a half frames, |
| 444 | so we set a minimum 7 frame threshold. */ |
| 445 | return GSM::Time(6,7); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // NOTE: Assumes sequential reads |
| 449 | int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun, |
| 450 | TIMESTAMP timestamp, bool * underrun, unsigned *RSSI) |
| 451 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 452 | int rc = 0; |
| 453 | unsigned int i; |
| 454 | lms_stream_status_t status; |
| 455 | lms_stream_meta_t rx_metadata = {}; |
| 456 | rx_metadata.flushPartialPacket = false; |
| 457 | rx_metadata.waitForTimestamp = false; |
| 458 | /* Shift read time with respect to transmit clock */ |
| 459 | timestamp += ts_offset; |
| 460 | rx_metadata.timestamp = 0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 461 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 462 | if (bufs.size() != chans) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 463 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 464 | return -1; |
| 465 | } |
| 466 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 467 | *overrun = false; |
| 468 | *underrun = false; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 469 | for (i = 0; i<chans; i++) { |
| 470 | thread_enable_cancel(false); |
| 471 | rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100); |
| 472 | LOG(ALERT) << "chan "<< i << " recv buffer of len " << rc << " expect " << std::hex << timestamp << " got " << std::hex << (TIMESTAMP)rx_metadata.timestamp << " (" << std::hex << rx_metadata.timestamp <<") diff=" << rx_metadata.timestamp - timestamp; |
| 473 | if (rc != len) { |
| 474 | LOG(ALERT) << "LMS: Device receive timed out"; |
| 475 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 476 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 477 | if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) { |
| 478 | if (status.underrun > m_last_rx_underruns[i]) |
| 479 | *underrun = true; |
| 480 | m_last_rx_underruns[i] = status.underrun; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 481 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 482 | if (status.overrun > m_last_rx_overruns[i]) |
| 483 | *overrun = true; |
| 484 | m_last_rx_overruns[i] = status.overrun; |
| 485 | } |
| 486 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | samplesRead += rc; |
| 490 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 491 | if (((TIMESTAMP) rx_metadata.timestamp) < timestamp) |
| 492 | rc = 0; |
| 493 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 494 | return rc; |
| 495 | } |
| 496 | |
| 497 | int LMSDevice::writeSamples(std::vector < short *>&bufs, int len, |
| 498 | bool * underrun, unsigned long long timestamp, |
| 499 | bool isControl) |
| 500 | { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 501 | int rc; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 502 | unsigned int i; |
| 503 | lms_stream_status_t status; |
| 504 | lms_stream_meta_t tx_metadata = {}; |
| 505 | tx_metadata.flushPartialPacket = false; |
| 506 | tx_metadata.waitForTimestamp = true; |
| 507 | tx_metadata.timestamp = timestamp; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 508 | |
| 509 | if (isControl) { |
| 510 | LOG(ERR) << "Control packets not supported"; |
| 511 | return 0; |
| 512 | } |
| 513 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 514 | if (bufs.size() != chans) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 515 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 516 | return -1; |
| 517 | } |
| 518 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 519 | *underrun = false; |
| 520 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 521 | for (i = 0; i<chans; i++) { |
| 522 | LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp; |
| 523 | thread_enable_cancel(false); |
| 524 | rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100); |
| 525 | if (rc != len) { |
| 526 | LOG(ALERT) << "LMS: Device send timed out"; |
| 527 | } |
| 528 | |
| 529 | if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) { |
| 530 | if (status.underrun > m_last_tx_underruns[i]) |
| 531 | *underrun = true; |
| 532 | m_last_tx_underruns[i] = status.underrun; |
| 533 | } |
| 534 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | samplesWritten += rc; |
| 538 | |
| 539 | return rc; |
| 540 | } |
| 541 | |
| 542 | bool LMSDevice::updateAlignment(TIMESTAMP timestamp) |
| 543 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 544 | return true; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | bool LMSDevice::setTxFreq(double wFreq, size_t chan) |
| 548 | { |
| 549 | |
| 550 | if (chan) { |
| 551 | LOG(ALERT) << "Invalid channel " << chan; |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) { |
| 556 | LOG(ALERT) << "set Tx: " << wFreq << " failed!"; |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | bool LMSDevice::setRxFreq(double wFreq, size_t chan) |
| 564 | { |
| 565 | if (chan) { |
| 566 | LOG(ALERT) << "Invalid channel " << chan; |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) { |
| 571 | LOG(ALERT) << "set Rx: " << wFreq << " failed!"; |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps, |
| 579 | InterfaceType iface, size_t chans, double offset, |
| 580 | const std::vector < std::string > &tx_paths, |
| 581 | const std::vector < std::string > &rx_paths) |
| 582 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 583 | return new LMSDevice(tx_sps, chans); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 584 | } |