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