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