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 | |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 121 | lms_range_t range; |
| 122 | if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range)) |
| 123 | goto out_close; |
| 124 | LOG(DEBUG) << "Sample Rate: Min=" << range.min << " Max=" << range.max << " Step=" << range.step; |
| 125 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 126 | LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps; |
| 127 | if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 128 | goto out_close; |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 129 | float_type sr_host, sr_rf; |
| 130 | if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf)) |
| 131 | goto out_close; |
| 132 | LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 133 | /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 134 | ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 135 | |
| 136 | switch (ref) { |
| 137 | case REF_INTERNAL: |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 138 | LOG(DEBUG) << "Setting Internal clock reference"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 139 | /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */ |
| 140 | if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0) |
| 141 | goto out_close; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 142 | LOG(DEBUG) << "Setting VCTCXO to " << dac_val; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 143 | if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0) |
| 144 | goto out_close; |
| 145 | break; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 146 | case REF_EXTERNAL: |
Harald Welte | a5054b3 | 2018-04-28 21:41:34 +0200 | [diff] [blame^] | 147 | LOG(DEBUG) << "Setting External clock reference to " << 10000000.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 148 | /* Assume an external 10 MHz reference clock */ |
| 149 | if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0) |
| 150 | goto out_close; |
| 151 | break; |
| 152 | default: |
| 153 | LOG(ALERT) << "Invalid reference type"; |
| 154 | goto out_close; |
| 155 | } |
| 156 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 157 | /* Perform Rx and Tx calibration */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 158 | for (i=0; i<chans; i++) { |
| 159 | LOG(INFO) << "Calibrating chan " << i; |
| 160 | if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 161 | goto out_close; |
| 162 | if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 163 | goto out_close; |
| 164 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 165 | |
| 166 | samplesRead = 0; |
| 167 | samplesWritten = 0; |
| 168 | started = false; |
| 169 | |
| 170 | return NORMAL; |
| 171 | |
| 172 | out_close: |
| 173 | LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage(); |
| 174 | LMS_Close(m_lms_dev); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | bool LMSDevice::start() |
| 179 | { |
| 180 | LOG(INFO) << "starting LMS..."; |
| 181 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 182 | unsigned int i; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 183 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 184 | for (i=0; i<chans; i++) { |
| 185 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0) |
| 186 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 187 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 188 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0) |
| 189 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 190 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 191 | // Set gains to midpoint |
| 192 | setTxGain((minTxGain() + maxTxGain()) / 2, i); |
| 193 | setRxGain((minRxGain() + maxRxGain()) / 2, i); |
| 194 | |
| 195 | m_lms_stream_rx[i] = {}; |
| 196 | m_lms_stream_rx[i].isTx = false; |
| 197 | m_lms_stream_rx[i].channel = i; |
| 198 | m_lms_stream_rx[i].fifoSize = 1024 * 1024; |
| 199 | m_lms_stream_rx[i].throughputVsLatency = 0.3; |
| 200 | m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 201 | |
| 202 | m_lms_stream_tx[i] = {}; |
| 203 | m_lms_stream_tx[i].isTx = true; |
| 204 | m_lms_stream_tx[i].channel = i; |
| 205 | m_lms_stream_tx[i].fifoSize = 1024 * 1024; |
| 206 | m_lms_stream_tx[i].throughputVsLatency = 0.3; |
| 207 | m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 208 | |
| 209 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0) |
| 210 | return false; |
| 211 | |
| 212 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0) |
| 213 | return false; |
| 214 | |
| 215 | if (LMS_StartStream(&m_lms_stream_rx[i]) < 0) |
| 216 | return false; |
| 217 | |
| 218 | if (LMS_StartStream(&m_lms_stream_tx[i]) < 0) |
| 219 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 222 | flush_recv(10); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 223 | |
| 224 | started = true; |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool LMSDevice::stop() |
| 229 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 230 | unsigned int i; |
| 231 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 232 | if (!started) |
| 233 | return true; |
| 234 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 235 | for (i=0; i<chans; i++) { |
| 236 | LMS_StopStream(&m_lms_stream_tx[i]); |
| 237 | LMS_StopStream(&m_lms_stream_rx[i]); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 238 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 239 | LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false); |
| 240 | LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false); |
| 241 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | double LMSDevice::maxTxGain() |
| 247 | { |
| 248 | return 60.0; |
| 249 | } |
| 250 | |
| 251 | double LMSDevice::minTxGain() |
| 252 | { |
| 253 | return 0.0; |
| 254 | } |
| 255 | |
| 256 | double LMSDevice::maxRxGain() |
| 257 | { |
| 258 | return 70.0; |
| 259 | } |
| 260 | |
| 261 | double LMSDevice::minRxGain() |
| 262 | { |
| 263 | return 0.0; |
| 264 | } |
| 265 | |
| 266 | double LMSDevice::setTxGain(double dB, size_t chan) |
| 267 | { |
| 268 | if (chan) { |
| 269 | LOG(ALERT) << "Invalid channel " << chan; |
| 270 | return 0.0; |
| 271 | } |
| 272 | |
| 273 | if (dB > maxTxGain()) |
| 274 | dB = maxTxGain(); |
| 275 | if (dB < minTxGain()) |
| 276 | dB = minTxGain(); |
| 277 | |
| 278 | LOG(NOTICE) << "Setting TX gain to " << dB << " dB."; |
| 279 | |
| 280 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0) |
| 281 | LOG(ERR) << "Error setting TX gain"; |
| 282 | |
| 283 | return dB; |
| 284 | } |
| 285 | |
| 286 | double LMSDevice::setRxGain(double dB, size_t chan) |
| 287 | { |
| 288 | if (chan) { |
| 289 | LOG(ALERT) << "Invalid channel " << chan; |
| 290 | return 0.0; |
| 291 | } |
| 292 | |
| 293 | dB = 47.0; |
| 294 | |
| 295 | if (dB > maxRxGain()) |
| 296 | dB = maxRxGain(); |
| 297 | if (dB < minRxGain()) |
| 298 | dB = minRxGain(); |
| 299 | |
| 300 | LOG(NOTICE) << "Setting RX gain to " << dB << " dB."; |
| 301 | |
| 302 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0) |
| 303 | LOG(ERR) << "Error setting RX gain"; |
| 304 | |
| 305 | return dB; |
| 306 | } |
| 307 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 308 | 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] | 309 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 310 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 311 | const char* c_name = name.c_str(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 312 | int num_names; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 313 | int i; |
| 314 | |
| 315 | num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 316 | for (i = 0; i < num_names; i++) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 317 | if (!strcmp(c_name, name_list[i])) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 318 | return i; |
| 319 | } |
| 320 | return -1; |
| 321 | } |
| 322 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 323 | bool LMSDevice::flush_recv(size_t num_pkts) |
| 324 | { |
| 325 | #define CHUNK 625 |
| 326 | int len = CHUNK * sps; |
| 327 | short *buffer = new short[len * 2]; |
| 328 | int rc; |
| 329 | lms_stream_meta_t rx_metadata = {}; |
| 330 | rx_metadata.flushPartialPacket = false; |
| 331 | rx_metadata.waitForTimestamp = false; |
| 332 | |
| 333 | ts_initial = 0; |
| 334 | |
| 335 | while (!ts_initial || (num_pkts-- > 0)) { |
| 336 | rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100); |
| 337 | LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp; |
| 338 | if (rc != len) { |
| 339 | LOG(ALERT) << "LMS: Device receive timed out"; |
| 340 | delete[] buffer; |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | ts_initial = rx_metadata.timestamp; |
| 345 | } |
| 346 | |
| 347 | LOG(INFO) << "Initial timestamp " << ts_initial << std::endl; |
| 348 | delete[] buffer; |
| 349 | return true; |
| 350 | } |
| 351 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 352 | bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan) |
| 353 | { |
| 354 | int idx; |
| 355 | |
| 356 | if (chan >= rx_paths.size()) { |
| 357 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 358 | return false; |
| 359 | } |
| 360 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 361 | idx = get_ant_idx(ant, LMS_CH_RX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 362 | if (idx < 0) { |
| 363 | LOG(ALERT) << "Invalid Rx Antenna"; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) { |
| 368 | LOG(ALERT) << "Unable to set Rx Antenna"; |
| 369 | } |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | std::string LMSDevice::getRxAntenna(size_t chan) |
| 375 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 376 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 377 | int idx; |
| 378 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 379 | if (chan >= rx_paths.size()) { |
| 380 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 381 | return ""; |
| 382 | } |
| 383 | |
| 384 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan); |
| 385 | if (idx < 0) { |
| 386 | LOG(ALERT) << "Error getting Rx Antenna"; |
| 387 | return ""; |
| 388 | } |
| 389 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 390 | 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] | 391 | LOG(ALERT) << "Error getting Rx Antenna List"; |
| 392 | return ""; |
| 393 | } |
| 394 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 395 | return name_list[idx]; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan) |
| 399 | { |
| 400 | int idx; |
| 401 | |
| 402 | if (chan >= tx_paths.size()) { |
| 403 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 404 | return false; |
| 405 | } |
| 406 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 407 | idx = get_ant_idx(ant, LMS_CH_TX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 408 | if (idx < 0) { |
| 409 | LOG(ALERT) << "Invalid Rx Antenna"; |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) { |
| 414 | LOG(ALERT) << "Unable to set Rx Antenna"; |
| 415 | } |
| 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
| 420 | std::string LMSDevice::getTxAntenna(size_t chan) |
| 421 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 422 | 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] | 423 | int idx; |
| 424 | |
| 425 | if (chan >= tx_paths.size()) { |
| 426 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 427 | return ""; |
| 428 | } |
| 429 | |
| 430 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan); |
| 431 | if (idx < 0) { |
| 432 | LOG(ALERT) << "Error getting Tx Antenna"; |
| 433 | return ""; |
| 434 | } |
| 435 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 436 | 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] | 437 | LOG(ALERT) << "Error getting Tx Antenna List"; |
| 438 | return ""; |
| 439 | } |
| 440 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 441 | return name_list[idx]; |
| 442 | } |
| 443 | |
| 444 | bool LMSDevice::requiresRadioAlign() |
| 445 | { |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | GSM::Time LMSDevice::minLatency() { |
| 450 | /* Empirical data from a handful of |
| 451 | relatively recent machines shows that the B100 will underrun when |
| 452 | the transmit threshold is reduced to a time of 6 and a half frames, |
| 453 | so we set a minimum 7 frame threshold. */ |
| 454 | return GSM::Time(6,7); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // NOTE: Assumes sequential reads |
| 458 | int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun, |
| 459 | TIMESTAMP timestamp, bool * underrun, unsigned *RSSI) |
| 460 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 461 | int rc = 0; |
| 462 | unsigned int i; |
| 463 | lms_stream_status_t status; |
| 464 | lms_stream_meta_t rx_metadata = {}; |
| 465 | rx_metadata.flushPartialPacket = false; |
| 466 | rx_metadata.waitForTimestamp = false; |
| 467 | /* Shift read time with respect to transmit clock */ |
| 468 | timestamp += ts_offset; |
| 469 | rx_metadata.timestamp = 0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 470 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 471 | if (bufs.size() != chans) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 472 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 473 | return -1; |
| 474 | } |
| 475 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 476 | *overrun = false; |
| 477 | *underrun = false; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 478 | for (i = 0; i<chans; i++) { |
| 479 | thread_enable_cancel(false); |
| 480 | rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100); |
| 481 | 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; |
| 482 | if (rc != len) { |
| 483 | LOG(ALERT) << "LMS: Device receive timed out"; |
| 484 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 485 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 486 | if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) { |
| 487 | if (status.underrun > m_last_rx_underruns[i]) |
| 488 | *underrun = true; |
| 489 | m_last_rx_underruns[i] = status.underrun; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 490 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 491 | if (status.overrun > m_last_rx_overruns[i]) |
| 492 | *overrun = true; |
| 493 | m_last_rx_overruns[i] = status.overrun; |
| 494 | } |
| 495 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | samplesRead += rc; |
| 499 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 500 | if (((TIMESTAMP) rx_metadata.timestamp) < timestamp) |
| 501 | rc = 0; |
| 502 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 503 | return rc; |
| 504 | } |
| 505 | |
| 506 | int LMSDevice::writeSamples(std::vector < short *>&bufs, int len, |
| 507 | bool * underrun, unsigned long long timestamp, |
| 508 | bool isControl) |
| 509 | { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 510 | int rc; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 511 | unsigned int i; |
| 512 | lms_stream_status_t status; |
| 513 | lms_stream_meta_t tx_metadata = {}; |
| 514 | tx_metadata.flushPartialPacket = false; |
| 515 | tx_metadata.waitForTimestamp = true; |
| 516 | tx_metadata.timestamp = timestamp; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 517 | |
| 518 | if (isControl) { |
| 519 | LOG(ERR) << "Control packets not supported"; |
| 520 | return 0; |
| 521 | } |
| 522 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 523 | if (bufs.size() != chans) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 524 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 525 | return -1; |
| 526 | } |
| 527 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 528 | *underrun = false; |
| 529 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 530 | for (i = 0; i<chans; i++) { |
| 531 | LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp; |
| 532 | thread_enable_cancel(false); |
| 533 | rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100); |
| 534 | if (rc != len) { |
| 535 | LOG(ALERT) << "LMS: Device send timed out"; |
| 536 | } |
| 537 | |
| 538 | if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) { |
| 539 | if (status.underrun > m_last_tx_underruns[i]) |
| 540 | *underrun = true; |
| 541 | m_last_tx_underruns[i] = status.underrun; |
| 542 | } |
| 543 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | samplesWritten += rc; |
| 547 | |
| 548 | return rc; |
| 549 | } |
| 550 | |
| 551 | bool LMSDevice::updateAlignment(TIMESTAMP timestamp) |
| 552 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 553 | return true; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | bool LMSDevice::setTxFreq(double wFreq, size_t chan) |
| 557 | { |
| 558 | |
| 559 | if (chan) { |
| 560 | LOG(ALERT) << "Invalid channel " << chan; |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) { |
| 565 | LOG(ALERT) << "set Tx: " << wFreq << " failed!"; |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | bool LMSDevice::setRxFreq(double wFreq, size_t chan) |
| 573 | { |
| 574 | if (chan) { |
| 575 | LOG(ALERT) << "Invalid channel " << chan; |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) { |
| 580 | LOG(ALERT) << "set Rx: " << wFreq << " failed!"; |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps, |
| 588 | InterfaceType iface, size_t chans, double offset, |
| 589 | const std::vector < std::string > &tx_paths, |
| 590 | const std::vector < std::string > &rx_paths) |
| 591 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 592 | return new LMSDevice(tx_sps, chans); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 593 | } |