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